blob: 1c06bf331ae8e92f3efcdb54b60fd3af3572443e [file] [log] [blame]
bsalomon@google.com49313f62011-09-14 13:54:05 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
bsalomon@google.com49313f62011-09-14 13:54:05 +00008#ifndef SkTArray_DEFINED
9#define SkTArray_DEFINED
10
bungeman918090c2016-02-09 09:14:28 -080011#include "../private/SkTLogic.h"
bungemanf3c15b72015-08-19 11:56:48 -070012#include "../private/SkTemplates.h"
bsalomon@google.com49313f62011-09-14 13:54:05 +000013#include "SkTypes.h"
bungemanf3c15b72015-08-19 11:56:48 -070014
15#include <new>
bungeman221524d2016-01-05 14:59:40 -080016#include <utility>
bsalomon@google.com49313f62011-09-14 13:54:05 +000017
bungeman@google.coma12cc7f2011-10-07 20:54:15 +000018/** When MEM_COPY is true T will be bit copied when moved.
19 When MEM_COPY is false, T will be copy constructed / destructed.
bungeman@google.comd58a8562014-03-24 15:55:01 +000020 In all cases T will be default-initialized on allocation,
bungeman@google.coma12cc7f2011-10-07 20:54:15 +000021 and its destructor will be called from this object's destructor.
22*/
bungeman85dc3592016-02-09 11:32:56 -080023template <typename T, bool MEM_COPY = false> class SkTArray {
bsalomon@google.com49313f62011-09-14 13:54:05 +000024public:
25 /**
26 * Creates an empty array with no initial storage
27 */
28 SkTArray() {
29 fCount = 0;
30 fReserveCount = gMIN_ALLOC_COUNT;
31 fAllocCount = 0;
32 fMemArray = NULL;
33 fPreAllocMemArray = NULL;
34 }
35
36 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000037 * Creates an empty array that will preallocate space for reserveCount
bsalomon@google.com49313f62011-09-14 13:54:05 +000038 * elements.
39 */
40 explicit SkTArray(int reserveCount) {
bungeman0d9e9be2016-04-20 10:22:20 -070041 this->init(0, NULL, reserveCount);
bsalomon@google.com49313f62011-09-14 13:54:05 +000042 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000043
bsalomon@google.com49313f62011-09-14 13:54:05 +000044 /**
45 * Copies one array to another. The new array will be heap allocated.
46 */
bungeman0d9e9be2016-04-20 10:22:20 -070047 explicit SkTArray(const SkTArray& that) {
48 this->init(that.fCount, NULL, 0);
49 this->copy(that.fItemArray);
50 }
51 explicit SkTArray(SkTArray&& that) {
52 this->init(that.fCount, NULL, 0);
53 that.move(fMemArray);
54 that.fCount = 0;
bsalomon@google.com49313f62011-09-14 13:54:05 +000055 }
56
57 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000058 * Creates a SkTArray by copying contents of a standard C array. The new
bsalomon@google.com49313f62011-09-14 13:54:05 +000059 * array will be heap allocated. Be careful not to use this constructor
60 * when you really want the (void*, int) version.
61 */
62 SkTArray(const T* array, int count) {
bungeman0d9e9be2016-04-20 10:22:20 -070063 this->init(count, NULL, 0);
64 this->copy(array);
bsalomon@google.com49313f62011-09-14 13:54:05 +000065 }
66
67 /**
68 * assign copy of array to this
69 */
bungeman0d9e9be2016-04-20 10:22:20 -070070 SkTArray& operator =(const SkTArray& that) {
bsalomon@google.com49313f62011-09-14 13:54:05 +000071 for (int i = 0; i < fCount; ++i) {
72 fItemArray[i].~T();
73 }
74 fCount = 0;
bungeman0d9e9be2016-04-20 10:22:20 -070075 this->checkRealloc(that.count());
76 fCount = that.count();
77 this->copy(that.fItemArray);
78 return *this;
79 }
80 SkTArray& operator =(SkTArray&& that) {
81 for (int i = 0; i < fCount; ++i) {
82 fItemArray[i].~T();
83 }
84 fCount = 0;
85 this->checkRealloc(that.count());
86 fCount = that.count();
87 that.move(fMemArray);
88 that.fCount = 0;
bsalomon@google.com49313f62011-09-14 13:54:05 +000089 return *this;
90 }
91
bsalomon383ff102015-07-31 11:53:11 -070092 ~SkTArray() {
bsalomon@google.com49313f62011-09-14 13:54:05 +000093 for (int i = 0; i < fCount; ++i) {
94 fItemArray[i].~T();
95 }
96 if (fMemArray != fPreAllocMemArray) {
97 sk_free(fMemArray);
98 }
99 }
100
101 /**
102 * Resets to count() == 0
103 */
104 void reset() { this->pop_back_n(fCount); }
105
106 /**
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000107 * Resets to count() = n newly constructed T objects.
108 */
109 void reset(int n) {
110 SkASSERT(n >= 0);
111 for (int i = 0; i < fCount; ++i) {
112 fItemArray[i].~T();
113 }
bungeman0d9e9be2016-04-20 10:22:20 -0700114 // Set fCount to 0 before calling checkRealloc so that no elements are moved.
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000115 fCount = 0;
116 this->checkRealloc(n);
117 fCount = n;
118 for (int i = 0; i < fCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -0700119 new (fItemArray + i) T;
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000120 }
121 }
122
123 /**
bungeman06ca8ec2016-06-09 08:01:03 -0700124 * Ensures there is enough reserved space for n elements.
125 */
126 void reserve(int n) {
127 if (fCount < n) {
128 this->checkRealloc(n - fCount);
129 }
130 }
131
132 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +0000133 * Resets to a copy of a C array.
134 */
135 void reset(const T* array, int count) {
136 for (int i = 0; i < fCount; ++i) {
137 fItemArray[i].~T();
138 }
bungeman0d9e9be2016-04-20 10:22:20 -0700139 fCount = 0;
140 this->checkRealloc(count);
jvanverth@google.com054ae992013-04-01 20:06:51 +0000141 fCount = count;
bungeman918090c2016-02-09 09:14:28 -0800142 this->copy(array);
bungeman@google.com95ebd172014-03-21 19:39:02 +0000143 }
144
145 void removeShuffle(int n) {
146 SkASSERT(n < fCount);
147 int newCount = fCount - 1;
148 fCount = newCount;
149 fItemArray[n].~T();
150 if (n != newCount) {
bungeman918090c2016-02-09 09:14:28 -0800151 this->move(n, newCount);
jvanverth@google.com054ae992013-04-01 20:06:51 +0000152 }
153 }
154
155 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000156 * Number of elements in the array.
157 */
158 int count() const { return fCount; }
159
160 /**
161 * Is the array empty.
162 */
163 bool empty() const { return !fCount; }
164
165 /**
bungeman@google.comd58a8562014-03-24 15:55:01 +0000166 * Adds 1 new default-initialized T value and returns it by reference. Note
bsalomon@google.com49313f62011-09-14 13:54:05 +0000167 * the reference only remains valid until the next call that adds or removes
168 * elements.
169 */
170 T& push_back() {
krasine0c1d282016-04-21 08:34:00 -0700171 void* newT = this->push_back_raw(1);
172 return *new (newT) T;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000173 }
174
175 /**
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000176 * Version of above that uses a copy constructor to initialize the new item
177 */
178 T& push_back(const T& t) {
krasine0c1d282016-04-21 08:34:00 -0700179 void* newT = this->push_back_raw(1);
180 return *new (newT) T(t);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000181 }
182
183 /**
halcanary91fcb3e2016-03-04 13:53:22 -0800184 * Version of above that uses a move constructor to initialize the new item
185 */
186 T& push_back(T&& t) {
krasine0c1d282016-04-21 08:34:00 -0700187 void* newT = this->push_back_raw(1);
188 return *new (newT) T(std::move(t));
halcanary91fcb3e2016-03-04 13:53:22 -0800189 }
190
191 /**
halcanaryf12a1672015-09-23 12:45:49 -0700192 * Construct a new T at the back of this array.
193 */
194 template<class... Args> T& emplace_back(Args&&... args) {
krasine0c1d282016-04-21 08:34:00 -0700195 void* newT = this->push_back_raw(1);
bungeman221524d2016-01-05 14:59:40 -0800196 return *new (newT) T(std::forward<Args>(args)...);
halcanaryf12a1672015-09-23 12:45:49 -0700197 }
198
199 /**
bungeman@google.comd58a8562014-03-24 15:55:01 +0000200 * Allocates n more default-initialized T values, and returns the address of
201 * the start of that new range. Note: this address is only valid until the
202 * next API call made on the array that might add or remove elements.
bsalomon@google.com49313f62011-09-14 13:54:05 +0000203 */
204 T* push_back_n(int n) {
205 SkASSERT(n >= 0);
bungeman6a6f3c52016-04-21 10:52:03 -0700206 void* newTs = this->push_back_raw(n);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000207 for (int i = 0; i < n; ++i) {
bungeman6a6f3c52016-04-21 10:52:03 -0700208 new (static_cast<char*>(newTs) + i * sizeof(T)) T;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000209 }
bungeman6a6f3c52016-04-21 10:52:03 -0700210 return static_cast<T*>(newTs);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000211 }
212
213 /**
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000214 * Version of above that uses a copy constructor to initialize all n items
215 * to the same T.
216 */
217 T* push_back_n(int n, const T& t) {
218 SkASSERT(n >= 0);
bungeman6a6f3c52016-04-21 10:52:03 -0700219 void* newTs = this->push_back_raw(n);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000220 for (int i = 0; i < n; ++i) {
bungeman6a6f3c52016-04-21 10:52:03 -0700221 new (static_cast<char*>(newTs) + i * sizeof(T)) T(t);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000222 }
bungeman6a6f3c52016-04-21 10:52:03 -0700223 return static_cast<T*>(newTs);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000224 }
225
226 /**
227 * Version of above that uses a copy constructor to initialize the n items
228 * to separate T values.
229 */
230 T* push_back_n(int n, const T t[]) {
231 SkASSERT(n >= 0);
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000232 this->checkRealloc(n);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000233 for (int i = 0; i < n; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -0700234 new (fItemArray + fCount + i) T(t[i]);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000235 }
236 fCount += n;
237 return fItemArray + fCount - n;
238 }
239
240 /**
msarett10e3d9b2016-08-18 15:46:03 -0700241 * Version of above that uses the move constructor to set n items.
242 */
243 T* move_back_n(int n, T* t) {
244 SkASSERT(n >= 0);
245 this->checkRealloc(n);
246 for (int i = 0; i < n; ++i) {
247 new (fItemArray + fCount + i) T(std::move(t[i]));
248 }
249 fCount += n;
250 return fItemArray + fCount - n;
251 }
252
253 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000254 * Removes the last element. Not safe to call when count() == 0.
255 */
256 void pop_back() {
257 SkASSERT(fCount > 0);
258 --fCount;
259 fItemArray[fCount].~T();
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000260 this->checkRealloc(0);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000261 }
262
263 /**
264 * Removes the last n elements. Not safe to call when count() < n.
265 */
266 void pop_back_n(int n) {
267 SkASSERT(n >= 0);
268 SkASSERT(fCount >= n);
269 fCount -= n;
270 for (int i = 0; i < n; ++i) {
bsalomon@google.comd5104142013-06-13 15:13:46 +0000271 fItemArray[fCount + i].~T();
bsalomon@google.com49313f62011-09-14 13:54:05 +0000272 }
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000273 this->checkRealloc(0);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000274 }
275
276 /**
277 * Pushes or pops from the back to resize. Pushes will be default
278 * initialized.
279 */
280 void resize_back(int newCount) {
281 SkASSERT(newCount >= 0);
282
283 if (newCount > fCount) {
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000284 this->push_back_n(newCount - fCount);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000285 } else if (newCount < fCount) {
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000286 this->pop_back_n(fCount - newCount);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000287 }
288 }
289
bsalomon23e619c2015-02-06 11:54:28 -0800290 /** Swaps the contents of this array with that array. Does a pointer swap if possible,
291 otherwise copies the T values. */
292 void swap(SkTArray* that) {
bsalomon3632f842015-02-10 19:46:58 -0800293 if (this == that) {
294 return;
295 }
bsalomon23e619c2015-02-06 11:54:28 -0800296 if (this->fPreAllocMemArray != this->fItemArray &&
297 that->fPreAllocMemArray != that->fItemArray) {
298 // If neither is using a preallocated array then just swap.
299 SkTSwap(fItemArray, that->fItemArray);
300 SkTSwap(fCount, that->fCount);
301 SkTSwap(fAllocCount, that->fAllocCount);
302 } else {
303 // This could be more optimal...
bungeman0d9e9be2016-04-20 10:22:20 -0700304 SkTArray copy(std::move(*that));
305 *that = std::move(*this);
306 *this = std::move(copy);
bsalomon23e619c2015-02-06 11:54:28 -0800307 }
308 }
309
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000310 T* begin() {
311 return fItemArray;
312 }
313 const T* begin() const {
314 return fItemArray;
315 }
316 T* end() {
317 return fItemArray ? fItemArray + fCount : NULL;
318 }
319 const T* end() const {
tfarina567ff2f2015-04-27 07:01:44 -0700320 return fItemArray ? fItemArray + fCount : NULL;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000321 }
322
323 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000324 * Get the i^th element.
325 */
326 T& operator[] (int i) {
327 SkASSERT(i < fCount);
328 SkASSERT(i >= 0);
329 return fItemArray[i];
330 }
331
332 const T& operator[] (int i) const {
333 SkASSERT(i < fCount);
334 SkASSERT(i >= 0);
335 return fItemArray[i];
336 }
337
338 /**
339 * equivalent to operator[](0)
340 */
341 T& front() { SkASSERT(fCount > 0); return fItemArray[0];}
342
343 const T& front() const { SkASSERT(fCount > 0); return fItemArray[0];}
344
345 /**
346 * equivalent to operator[](count() - 1)
347 */
348 T& back() { SkASSERT(fCount); return fItemArray[fCount - 1];}
349
350 const T& back() const { SkASSERT(fCount > 0); return fItemArray[fCount - 1];}
351
352 /**
353 * equivalent to operator[](count()-1-i)
354 */
355 T& fromBack(int i) {
356 SkASSERT(i >= 0);
357 SkASSERT(i < fCount);
358 return fItemArray[fCount - i - 1];
359 }
360
361 const T& fromBack(int i) const {
362 SkASSERT(i >= 0);
363 SkASSERT(i < fCount);
364 return fItemArray[fCount - i - 1];
365 }
366
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000367 bool operator==(const SkTArray<T, MEM_COPY>& right) const {
368 int leftCount = this->count();
369 if (leftCount != right.count()) {
370 return false;
371 }
372 for (int index = 0; index < leftCount; ++index) {
373 if (fItemArray[index] != right.fItemArray[index]) {
374 return false;
375 }
376 }
377 return true;
378 }
379
380 bool operator!=(const SkTArray<T, MEM_COPY>& right) const {
381 return !(*this == right);
382 }
383
bsalomon@google.com92669012011-09-27 19:10:05 +0000384protected:
385 /**
386 * Creates an empty array that will use the passed storage block until it
387 * is insufficiently large to hold the entire array.
388 */
389 template <int N>
390 SkTArray(SkAlignedSTStorage<N,T>* storage) {
bungeman0d9e9be2016-04-20 10:22:20 -0700391 this->init(0, storage->get(), N);
bsalomon@google.com92669012011-09-27 19:10:05 +0000392 }
393
394 /**
395 * Copy another array, using preallocated storage if preAllocCount >=
396 * array.count(). Otherwise storage will only be used when array shrinks
397 * to fit.
398 */
399 template <int N>
400 SkTArray(const SkTArray& array, SkAlignedSTStorage<N,T>* storage) {
bungeman0d9e9be2016-04-20 10:22:20 -0700401 this->init(array.fCount, storage->get(), N);
402 this->copy(array.fItemArray);
bsalomon@google.com92669012011-09-27 19:10:05 +0000403 }
404
405 /**
406 * Copy a C array, using preallocated storage if preAllocCount >=
407 * count. Otherwise storage will only be used when array shrinks
408 * to fit.
409 */
410 template <int N>
411 SkTArray(const T* array, int count, SkAlignedSTStorage<N,T>* storage) {
bungeman0d9e9be2016-04-20 10:22:20 -0700412 this->init(count, storage->get(), N);
413 this->copy(array);
bsalomon@google.com92669012011-09-27 19:10:05 +0000414 }
415
bungeman0d9e9be2016-04-20 10:22:20 -0700416 void init(int count, void* preAllocStorage, int preAllocOrReserveCount) {
junov@chromium.orgd80a5092011-11-30 18:35:19 +0000417 SkASSERT(count >= 0);
418 SkASSERT(preAllocOrReserveCount >= 0);
bsalomon@google.com92669012011-09-27 19:10:05 +0000419 fCount = count;
420 fReserveCount = (preAllocOrReserveCount > 0) ?
421 preAllocOrReserveCount :
422 gMIN_ALLOC_COUNT;
423 fPreAllocMemArray = preAllocStorage;
424 if (fReserveCount >= fCount &&
bsalomon49f085d2014-09-05 13:34:00 -0700425 preAllocStorage) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000426 fAllocCount = fReserveCount;
427 fMemArray = preAllocStorage;
428 } else {
junov@chromium.orgd80a5092011-11-30 18:35:19 +0000429 fAllocCount = SkMax32(fCount, fReserveCount);
430 fMemArray = sk_malloc_throw(fAllocCount * sizeof(T));
bsalomon@google.com92669012011-09-27 19:10:05 +0000431 }
bsalomon@google.com92669012011-09-27 19:10:05 +0000432 }
433
bsalomon@google.com49313f62011-09-14 13:54:05 +0000434private:
bungeman918090c2016-02-09 09:14:28 -0800435 /** In the following move and copy methods, 'dst' is assumed to be uninitialized raw storage.
436 * In the following move methods, 'src' is destroyed leaving behind uninitialized raw storage.
437 */
438 template <bool E = MEM_COPY> SK_WHEN(E, void) copy(const T* src) {
439 sk_careful_memcpy(fMemArray, src, fCount * sizeof(T));
440 }
441 template <bool E = MEM_COPY> SK_WHEN(E, void) move(int dst, int src) {
442 memcpy(&fItemArray[dst], &fItemArray[src], sizeof(T));
443 }
bungeman0d9e9be2016-04-20 10:22:20 -0700444 template <bool E = MEM_COPY> SK_WHEN(E, void) move(void* dst) {
bungeman918090c2016-02-09 09:14:28 -0800445 sk_careful_memcpy(dst, fMemArray, fCount * sizeof(T));
446 }
447
448 template <bool E = MEM_COPY> SK_WHEN(!E, void) copy(const T* src) {
449 for (int i = 0; i < fCount; ++i) {
450 new (fItemArray + i) T(src[i]);
451 }
452 }
453 template <bool E = MEM_COPY> SK_WHEN(!E, void) move(int dst, int src) {
454 new (&fItemArray[dst]) T(std::move(fItemArray[src]));
455 fItemArray[src].~T();
456 }
bungeman0d9e9be2016-04-20 10:22:20 -0700457 template <bool E = MEM_COPY> SK_WHEN(!E, void) move(void* dst) {
bungeman918090c2016-02-09 09:14:28 -0800458 for (int i = 0; i < fCount; ++i) {
bungeman0d9e9be2016-04-20 10:22:20 -0700459 new (static_cast<char*>(dst) + sizeof(T) * i) T(std::move(fItemArray[i]));
bungeman918090c2016-02-09 09:14:28 -0800460 fItemArray[i].~T();
461 }
462 }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000463
464 static const int gMIN_ALLOC_COUNT = 8;
465
bsalomon@google.comd5104142013-06-13 15:13:46 +0000466 // Helper function that makes space for n objects, adjusts the count, but does not initialize
467 // the new objects.
468 void* push_back_raw(int n) {
469 this->checkRealloc(n);
470 void* ptr = fItemArray + fCount;
471 fCount += n;
472 return ptr;
473 }
474
bsalomon@google.com49313f62011-09-14 13:54:05 +0000475 inline void checkRealloc(int delta) {
476 SkASSERT(fCount >= 0);
477 SkASSERT(fAllocCount >= 0);
478
479 SkASSERT(-delta <= fCount);
480
481 int newCount = fCount + delta;
bungeman@google.coma12cc7f2011-10-07 20:54:15 +0000482 int newAllocCount = fAllocCount;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000483
bsalomon@google.com137209f2012-08-14 17:19:08 +0000484 if (newCount > fAllocCount || newCount < (fAllocCount / 3)) {
485 // whether we're growing or shrinking, we leave at least 50% extra space for future
486 // growth (clamped to the reserve count).
487 newAllocCount = SkMax32(newCount + ((newCount + 1) >> 1), fReserveCount);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000488 }
bungeman@google.coma12cc7f2011-10-07 20:54:15 +0000489 if (newAllocCount != fAllocCount) {
bsalomon@google.com49313f62011-09-14 13:54:05 +0000490
bungeman@google.coma12cc7f2011-10-07 20:54:15 +0000491 fAllocCount = newAllocCount;
bungeman0d9e9be2016-04-20 10:22:20 -0700492 void* newMemArray;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000493
bsalomon49f085d2014-09-05 13:34:00 -0700494 if (fAllocCount == fReserveCount && fPreAllocMemArray) {
bungeman0d9e9be2016-04-20 10:22:20 -0700495 newMemArray = fPreAllocMemArray;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000496 } else {
bungeman0d9e9be2016-04-20 10:22:20 -0700497 newMemArray = sk_malloc_throw(fAllocCount*sizeof(T));
bsalomon@google.com49313f62011-09-14 13:54:05 +0000498 }
499
bungeman918090c2016-02-09 09:14:28 -0800500 this->move(newMemArray);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000501
502 if (fMemArray != fPreAllocMemArray) {
503 sk_free(fMemArray);
504 }
bungeman@google.coma12cc7f2011-10-07 20:54:15 +0000505 fMemArray = newMemArray;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000506 }
507 }
508
bsalomon23e619c2015-02-06 11:54:28 -0800509 int fReserveCount;
510 int fCount;
511 int fAllocCount;
512 void* fPreAllocMemArray;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000513 union {
514 T* fItemArray;
515 void* fMemArray;
516 };
517};
518
bsalomon@google.com92669012011-09-27 19:10:05 +0000519/**
520 * Subclass of SkTArray that contains a preallocated memory block for the array.
521 */
bsalomon@google.comf47dd742013-02-26 15:40:01 +0000522template <int N, typename T, bool MEM_COPY = false>
523class SkSTArray : public SkTArray<T, MEM_COPY> {
bsalomon@google.com92669012011-09-27 19:10:05 +0000524private:
bsalomon@google.comf47dd742013-02-26 15:40:01 +0000525 typedef SkTArray<T, MEM_COPY> INHERITED;
bsalomon@google.com92669012011-09-27 19:10:05 +0000526
527public:
528 SkSTArray() : INHERITED(&fStorage) {
529 }
530
531 SkSTArray(const SkSTArray& array)
532 : INHERITED(array, &fStorage) {
533 }
534
535 explicit SkSTArray(const INHERITED& array)
536 : INHERITED(array, &fStorage) {
537 }
538
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000539 explicit SkSTArray(int reserveCount)
540 : INHERITED(reserveCount) {
541 }
542
bsalomon@google.com92669012011-09-27 19:10:05 +0000543 SkSTArray(const T* array, int count)
544 : INHERITED(array, count, &fStorage) {
545 }
546
547 SkSTArray& operator= (const SkSTArray& array) {
548 return *this = *(const INHERITED*)&array;
549 }
550
551 SkSTArray& operator= (const INHERITED& array) {
552 INHERITED::operator=(array);
553 return *this;
554 }
555
556private:
557 SkAlignedSTStorage<N,T> fStorage;
558};
559
bsalomon@google.com49313f62011-09-14 13:54:05 +0000560#endif