OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkTArray_DEFINED | 8 #ifndef SkTArray_DEFINED |
9 #define SkTArray_DEFINED | 9 #define SkTArray_DEFINED |
10 | 10 |
11 #include "../private/SkTLogic.h" | |
12 #include "../private/SkTemplates.h" | 11 #include "../private/SkTemplates.h" |
13 #include "SkTypes.h" | 12 #include "SkTypes.h" |
14 | 13 |
15 #include <new> | 14 #include <new> |
16 #include <utility> | 15 #include <utility> |
17 | 16 |
18 template <typename T, bool MEM_COPY = false> class SkTArray; | 17 template <typename T, bool MEM_COPY = false> class SkTArray; |
| 18 |
| 19 namespace SkTArrayExt { |
| 20 |
| 21 template<typename T> |
| 22 inline void copy(SkTArray<T, true>* self, int dst, int src) { |
| 23 memcpy(&self->fItemArray[dst], &self->fItemArray[src], sizeof(T)); |
| 24 } |
| 25 template<typename T> |
| 26 inline void copy(SkTArray<T, true>* self, const T* array) { |
| 27 sk_careful_memcpy(self->fMemArray, array, self->fCount * sizeof(T)); |
| 28 } |
| 29 template<typename T> |
| 30 inline void copyAndDelete(SkTArray<T, true>* self, char* newMemArray) { |
| 31 sk_careful_memcpy(newMemArray, self->fMemArray, self->fCount * sizeof(T)); |
| 32 } |
| 33 |
| 34 template<typename T> |
| 35 inline void copy(SkTArray<T, false>* self, int dst, int src) { |
| 36 new (&self->fItemArray[dst]) T(self->fItemArray[src]); |
| 37 } |
| 38 template<typename T> |
| 39 inline void copy(SkTArray<T, false>* self, const T* array) { |
| 40 for (int i = 0; i < self->fCount; ++i) { |
| 41 new (self->fItemArray + i) T(array[i]); |
| 42 } |
| 43 } |
| 44 template<typename T> |
| 45 inline void copyAndDelete(SkTArray<T, false>* self, char* newMemArray) { |
| 46 for (int i = 0; i < self->fCount; ++i) { |
| 47 new (newMemArray + sizeof(T) * i) T(self->fItemArray[i]); |
| 48 self->fItemArray[i].~T(); |
| 49 } |
| 50 } |
| 51 |
| 52 } |
| 53 |
19 template <typename T, bool MEM_COPY> void* operator new(size_t, SkTArray<T, MEM_
COPY>*, int); | 54 template <typename T, bool MEM_COPY> void* operator new(size_t, SkTArray<T, MEM_
COPY>*, int); |
20 | 55 |
21 /** When MEM_COPY is true T will be bit copied when moved. | 56 /** When MEM_COPY is true T will be bit copied when moved. |
22 When MEM_COPY is false, T will be copy constructed / destructed. | 57 When MEM_COPY is false, T will be copy constructed / destructed. |
23 In all cases T will be default-initialized on allocation, | 58 In all cases T will be default-initialized on allocation, |
24 and its destructor will be called from this object's destructor. | 59 and its destructor will be called from this object's destructor. |
25 */ | 60 */ |
26 template <typename T, bool MEM_COPY> class SkTArray { | 61 template <typename T, bool MEM_COPY> class SkTArray { |
27 public: | 62 public: |
28 /** | 63 /** |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 /** | 98 /** |
64 * assign copy of array to this | 99 * assign copy of array to this |
65 */ | 100 */ |
66 SkTArray& operator =(const SkTArray& array) { | 101 SkTArray& operator =(const SkTArray& array) { |
67 for (int i = 0; i < fCount; ++i) { | 102 for (int i = 0; i < fCount; ++i) { |
68 fItemArray[i].~T(); | 103 fItemArray[i].~T(); |
69 } | 104 } |
70 fCount = 0; | 105 fCount = 0; |
71 this->checkRealloc((int)array.count()); | 106 this->checkRealloc((int)array.count()); |
72 fCount = array.count(); | 107 fCount = array.count(); |
73 this->copy(static_cast<const T*>(array.fMemArray)); | 108 SkTArrayExt::copy(this, static_cast<const T*>(array.fMemArray)); |
74 return *this; | 109 return *this; |
75 } | 110 } |
76 | 111 |
77 ~SkTArray() { | 112 ~SkTArray() { |
78 for (int i = 0; i < fCount; ++i) { | 113 for (int i = 0; i < fCount; ++i) { |
79 fItemArray[i].~T(); | 114 fItemArray[i].~T(); |
80 } | 115 } |
81 if (fMemArray != fPreAllocMemArray) { | 116 if (fMemArray != fPreAllocMemArray) { |
82 sk_free(fMemArray); | 117 sk_free(fMemArray); |
83 } | 118 } |
(...skipping 24 matching lines...) Expand all Loading... |
108 /** | 143 /** |
109 * Resets to a copy of a C array. | 144 * Resets to a copy of a C array. |
110 */ | 145 */ |
111 void reset(const T* array, int count) { | 146 void reset(const T* array, int count) { |
112 for (int i = 0; i < fCount; ++i) { | 147 for (int i = 0; i < fCount; ++i) { |
113 fItemArray[i].~T(); | 148 fItemArray[i].~T(); |
114 } | 149 } |
115 int delta = count - fCount; | 150 int delta = count - fCount; |
116 this->checkRealloc(delta); | 151 this->checkRealloc(delta); |
117 fCount = count; | 152 fCount = count; |
118 this->copy(array); | 153 SkTArrayExt::copy(this, array); |
119 } | 154 } |
120 | 155 |
121 void removeShuffle(int n) { | 156 void removeShuffle(int n) { |
122 SkASSERT(n < fCount); | 157 SkASSERT(n < fCount); |
123 int newCount = fCount - 1; | 158 int newCount = fCount - 1; |
124 fCount = newCount; | 159 fCount = newCount; |
125 fItemArray[n].~T(); | 160 fItemArray[n].~T(); |
126 if (n != newCount) { | 161 if (n != newCount) { |
127 this->move(n, newCount); | 162 SkTArrayExt::copy(this, n, newCount); |
| 163 fItemArray[newCount].~T(); |
128 } | 164 } |
129 } | 165 } |
130 | 166 |
131 /** | 167 /** |
132 * Number of elements in the array. | 168 * Number of elements in the array. |
133 */ | 169 */ |
134 int count() const { return fCount; } | 170 int count() const { return fCount; } |
135 | 171 |
136 /** | 172 /** |
137 * Is the array empty. | 173 * Is the array empty. |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 fPreAllocMemArray = preAllocStorage; | 415 fPreAllocMemArray = preAllocStorage; |
380 if (fReserveCount >= fCount && | 416 if (fReserveCount >= fCount && |
381 preAllocStorage) { | 417 preAllocStorage) { |
382 fAllocCount = fReserveCount; | 418 fAllocCount = fReserveCount; |
383 fMemArray = preAllocStorage; | 419 fMemArray = preAllocStorage; |
384 } else { | 420 } else { |
385 fAllocCount = SkMax32(fCount, fReserveCount); | 421 fAllocCount = SkMax32(fCount, fReserveCount); |
386 fMemArray = sk_malloc_throw(fAllocCount * sizeof(T)); | 422 fMemArray = sk_malloc_throw(fAllocCount * sizeof(T)); |
387 } | 423 } |
388 | 424 |
389 this->copy(array); | 425 SkTArrayExt::copy(this, array); |
390 } | 426 } |
391 | 427 |
392 private: | 428 private: |
393 /** In the following move and copy methods, 'dst' is assumed to be uninitial
ized raw storage. | |
394 * In the following move methods, 'src' is destroyed leaving behind uniniti
alized raw storage. | |
395 */ | |
396 template <bool E = MEM_COPY> SK_WHEN(E, void) copy(const T* src) { | |
397 sk_careful_memcpy(fMemArray, src, fCount * sizeof(T)); | |
398 } | |
399 template <bool E = MEM_COPY> SK_WHEN(E, void) move(int dst, int src) { | |
400 memcpy(&fItemArray[dst], &fItemArray[src], sizeof(T)); | |
401 } | |
402 template <bool E = MEM_COPY> SK_WHEN(E, void) move(char* dst) { | |
403 sk_careful_memcpy(dst, fMemArray, fCount * sizeof(T)); | |
404 } | |
405 | |
406 template <bool E = MEM_COPY> SK_WHEN(!E, void) copy(const T* src) { | |
407 for (int i = 0; i < fCount; ++i) { | |
408 new (fItemArray + i) T(src[i]); | |
409 } | |
410 } | |
411 template <bool E = MEM_COPY> SK_WHEN(!E, void) move(int dst, int src) { | |
412 new (&fItemArray[dst]) T(std::move(fItemArray[src])); | |
413 fItemArray[src].~T(); | |
414 } | |
415 template <bool E = MEM_COPY> SK_WHEN(!E, void) move(char* dst) { | |
416 for (int i = 0; i < fCount; ++i) { | |
417 new (dst + sizeof(T) * i) T(std::move(fItemArray[i])); | |
418 fItemArray[i].~T(); | |
419 } | |
420 } | |
421 | 429 |
422 static const int gMIN_ALLOC_COUNT = 8; | 430 static const int gMIN_ALLOC_COUNT = 8; |
423 | 431 |
424 // Helper function that makes space for n objects, adjusts the count, but do
es not initialize | 432 // Helper function that makes space for n objects, adjusts the count, but do
es not initialize |
425 // the new objects. | 433 // the new objects. |
426 void* push_back_raw(int n) { | 434 void* push_back_raw(int n) { |
427 this->checkRealloc(n); | 435 this->checkRealloc(n); |
428 void* ptr = fItemArray + fCount; | 436 void* ptr = fItemArray + fCount; |
429 fCount += n; | 437 fCount += n; |
430 return ptr; | 438 return ptr; |
(...skipping 17 matching lines...) Expand all Loading... |
448 | 456 |
449 fAllocCount = newAllocCount; | 457 fAllocCount = newAllocCount; |
450 char* newMemArray; | 458 char* newMemArray; |
451 | 459 |
452 if (fAllocCount == fReserveCount && fPreAllocMemArray) { | 460 if (fAllocCount == fReserveCount && fPreAllocMemArray) { |
453 newMemArray = (char*) fPreAllocMemArray; | 461 newMemArray = (char*) fPreAllocMemArray; |
454 } else { | 462 } else { |
455 newMemArray = (char*) sk_malloc_throw(fAllocCount*sizeof(T)); | 463 newMemArray = (char*) sk_malloc_throw(fAllocCount*sizeof(T)); |
456 } | 464 } |
457 | 465 |
458 this->move(newMemArray); | 466 SkTArrayExt::copyAndDelete<T>(this, newMemArray); |
459 | 467 |
460 if (fMemArray != fPreAllocMemArray) { | 468 if (fMemArray != fPreAllocMemArray) { |
461 sk_free(fMemArray); | 469 sk_free(fMemArray); |
462 } | 470 } |
463 fMemArray = newMemArray; | 471 fMemArray = newMemArray; |
464 } | 472 } |
465 } | 473 } |
466 | 474 |
467 friend void* operator new<T>(size_t, SkTArray*, int); | 475 friend void* operator new<T>(size_t, SkTArray*, int); |
468 | 476 |
| 477 template<typename X> friend void SkTArrayExt::copy(SkTArray<X, true>* that,
int dst, int src); |
| 478 template<typename X> friend void SkTArrayExt::copy(SkTArray<X, true>* that,
const X*); |
| 479 template<typename X> friend void SkTArrayExt::copyAndDelete(SkTArray<X, true
>* that, char*); |
| 480 |
| 481 template<typename X> friend void SkTArrayExt::copy(SkTArray<X, false>* that,
int dst, int src); |
| 482 template<typename X> friend void SkTArrayExt::copy(SkTArray<X, false>* that,
const X*); |
| 483 template<typename X> friend void SkTArrayExt::copyAndDelete(SkTArray<X, fals
e>* that, char*); |
| 484 |
469 int fReserveCount; | 485 int fReserveCount; |
470 int fCount; | 486 int fCount; |
471 int fAllocCount; | 487 int fAllocCount; |
472 void* fPreAllocMemArray; | 488 void* fPreAllocMemArray; |
473 union { | 489 union { |
474 T* fItemArray; | 490 T* fItemArray; |
475 void* fMemArray; | 491 void* fMemArray; |
476 }; | 492 }; |
477 }; | 493 }; |
478 | 494 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 SkSTArray& operator= (const INHERITED& array) { | 550 SkSTArray& operator= (const INHERITED& array) { |
535 INHERITED::operator=(array); | 551 INHERITED::operator=(array); |
536 return *this; | 552 return *this; |
537 } | 553 } |
538 | 554 |
539 private: | 555 private: |
540 SkAlignedSTStorage<N,T> fStorage; | 556 SkAlignedSTStorage<N,T> fStorage; |
541 }; | 557 }; |
542 | 558 |
543 #endif | 559 #endif |
OLD | NEW |