blob: b90b41d7c54aa04718bacd564f4a36065d187244 [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
Florin Malitac2d5bd02017-03-09 13:34:09 -050018/** When MEM_MOVE is true T will be bit copied when moved.
19 When MEM_MOVE 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*/
Florin Malitac2d5bd02017-03-09 13:34:09 -050023template <typename T, bool MEM_MOVE = 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 }
Mike Reed6c14c8d2017-03-09 16:36:26 -0500296 if (this->isNotUsingPreAlloc() && that->isNotUsingPreAlloc()) {
bsalomon23e619c2015-02-06 11:54:28 -0800297 SkTSwap(fItemArray, that->fItemArray);
298 SkTSwap(fCount, that->fCount);
299 SkTSwap(fAllocCount, that->fAllocCount);
300 } else {
301 // This could be more optimal...
bungeman0d9e9be2016-04-20 10:22:20 -0700302 SkTArray copy(std::move(*that));
303 *that = std::move(*this);
304 *this = std::move(copy);
bsalomon23e619c2015-02-06 11:54:28 -0800305 }
306 }
307
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000308 T* begin() {
309 return fItemArray;
310 }
311 const T* begin() const {
312 return fItemArray;
313 }
314 T* end() {
315 return fItemArray ? fItemArray + fCount : NULL;
316 }
317 const T* end() const {
tfarina567ff2f2015-04-27 07:01:44 -0700318 return fItemArray ? fItemArray + fCount : NULL;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000319 }
320
321 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000322 * Get the i^th element.
323 */
324 T& operator[] (int i) {
325 SkASSERT(i < fCount);
326 SkASSERT(i >= 0);
327 return fItemArray[i];
328 }
329
330 const T& operator[] (int i) const {
331 SkASSERT(i < fCount);
332 SkASSERT(i >= 0);
333 return fItemArray[i];
334 }
335
336 /**
337 * equivalent to operator[](0)
338 */
339 T& front() { SkASSERT(fCount > 0); return fItemArray[0];}
340
341 const T& front() const { SkASSERT(fCount > 0); return fItemArray[0];}
342
343 /**
344 * equivalent to operator[](count() - 1)
345 */
346 T& back() { SkASSERT(fCount); return fItemArray[fCount - 1];}
347
348 const T& back() const { SkASSERT(fCount > 0); return fItemArray[fCount - 1];}
349
350 /**
351 * equivalent to operator[](count()-1-i)
352 */
353 T& fromBack(int i) {
354 SkASSERT(i >= 0);
355 SkASSERT(i < fCount);
356 return fItemArray[fCount - i - 1];
357 }
358
359 const T& fromBack(int i) const {
360 SkASSERT(i >= 0);
361 SkASSERT(i < fCount);
362 return fItemArray[fCount - i - 1];
363 }
364
Florin Malitac2d5bd02017-03-09 13:34:09 -0500365 bool operator==(const SkTArray<T, MEM_MOVE>& right) const {
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000366 int leftCount = this->count();
367 if (leftCount != right.count()) {
368 return false;
369 }
370 for (int index = 0; index < leftCount; ++index) {
371 if (fItemArray[index] != right.fItemArray[index]) {
372 return false;
373 }
374 }
375 return true;
376 }
377
Florin Malitac2d5bd02017-03-09 13:34:09 -0500378 bool operator!=(const SkTArray<T, MEM_MOVE>& right) const {
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000379 return !(*this == right);
380 }
381
bsalomon@google.com92669012011-09-27 19:10:05 +0000382protected:
383 /**
384 * Creates an empty array that will use the passed storage block until it
385 * is insufficiently large to hold the entire array.
386 */
387 template <int N>
388 SkTArray(SkAlignedSTStorage<N,T>* storage) {
bungeman0d9e9be2016-04-20 10:22:20 -0700389 this->init(0, storage->get(), N);
bsalomon@google.com92669012011-09-27 19:10:05 +0000390 }
391
392 /**
393 * Copy another array, using preallocated storage if preAllocCount >=
394 * array.count(). Otherwise storage will only be used when array shrinks
395 * to fit.
396 */
397 template <int N>
398 SkTArray(const SkTArray& array, SkAlignedSTStorage<N,T>* storage) {
bungeman0d9e9be2016-04-20 10:22:20 -0700399 this->init(array.fCount, storage->get(), N);
400 this->copy(array.fItemArray);
bsalomon@google.com92669012011-09-27 19:10:05 +0000401 }
402
403 /**
Florin Malitab1d800d2017-03-09 12:17:15 -0500404 * Move another array, using preallocated storage if preAllocCount >=
405 * array.count(). Otherwise storage will only be used when array shrinks
406 * to fit.
407 */
408 template <int N>
409 SkTArray(SkTArray&& array, SkAlignedSTStorage<N,T>* storage) {
410 this->init(array.fCount, storage->get(), N);
411 array.move(fMemArray);
412 array.fCount = 0;
413 }
414
415 /**
bsalomon@google.com92669012011-09-27 19:10:05 +0000416 * Copy a C array, using preallocated storage if preAllocCount >=
417 * count. Otherwise storage will only be used when array shrinks
418 * to fit.
419 */
420 template <int N>
421 SkTArray(const T* array, int count, SkAlignedSTStorage<N,T>* storage) {
bungeman0d9e9be2016-04-20 10:22:20 -0700422 this->init(count, storage->get(), N);
423 this->copy(array);
bsalomon@google.com92669012011-09-27 19:10:05 +0000424 }
425
bungeman0d9e9be2016-04-20 10:22:20 -0700426 void init(int count, void* preAllocStorage, int preAllocOrReserveCount) {
junov@chromium.orgd80a5092011-11-30 18:35:19 +0000427 SkASSERT(count >= 0);
428 SkASSERT(preAllocOrReserveCount >= 0);
bsalomon@google.com92669012011-09-27 19:10:05 +0000429 fCount = count;
430 fReserveCount = (preAllocOrReserveCount > 0) ?
431 preAllocOrReserveCount :
432 gMIN_ALLOC_COUNT;
433 fPreAllocMemArray = preAllocStorage;
434 if (fReserveCount >= fCount &&
bsalomon49f085d2014-09-05 13:34:00 -0700435 preAllocStorage) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000436 fAllocCount = fReserveCount;
437 fMemArray = preAllocStorage;
438 } else {
junov@chromium.orgd80a5092011-11-30 18:35:19 +0000439 fAllocCount = SkMax32(fCount, fReserveCount);
440 fMemArray = sk_malloc_throw(fAllocCount * sizeof(T));
bsalomon@google.com92669012011-09-27 19:10:05 +0000441 }
bsalomon@google.com92669012011-09-27 19:10:05 +0000442 }
443
bsalomon@google.com49313f62011-09-14 13:54:05 +0000444private:
bungeman918090c2016-02-09 09:14:28 -0800445 /** In the following move and copy methods, 'dst' is assumed to be uninitialized raw storage.
446 * In the following move methods, 'src' is destroyed leaving behind uninitialized raw storage.
447 */
Florin Malitac2d5bd02017-03-09 13:34:09 -0500448 void copy(const T* src) {
449 // Some types may be trivially copyable, in which case we *could* use memcopy; but
450 // MEM_MOVE == true implies that the type is trivially movable, and not necessarily
451 // trivially copyable (think sk_sp<>). So short of adding another template arg, we
452 // must be conservative and use copy construction.
bungeman918090c2016-02-09 09:14:28 -0800453 for (int i = 0; i < fCount; ++i) {
454 new (fItemArray + i) T(src[i]);
455 }
456 }
Florin Malitac2d5bd02017-03-09 13:34:09 -0500457
458 template <bool E = MEM_MOVE> SK_WHEN(E, void) move(int dst, int src) {
459 memcpy(&fItemArray[dst], &fItemArray[src], sizeof(T));
460 }
461 template <bool E = MEM_MOVE> SK_WHEN(E, void) move(void* dst) {
462 sk_careful_memcpy(dst, fMemArray, fCount * sizeof(T));
463 }
464
465 template <bool E = MEM_MOVE> SK_WHEN(!E, void) move(int dst, int src) {
bungeman918090c2016-02-09 09:14:28 -0800466 new (&fItemArray[dst]) T(std::move(fItemArray[src]));
467 fItemArray[src].~T();
468 }
Florin Malitac2d5bd02017-03-09 13:34:09 -0500469 template <bool E = MEM_MOVE> SK_WHEN(!E, void) move(void* dst) {
bungeman918090c2016-02-09 09:14:28 -0800470 for (int i = 0; i < fCount; ++i) {
bungeman0d9e9be2016-04-20 10:22:20 -0700471 new (static_cast<char*>(dst) + sizeof(T) * i) T(std::move(fItemArray[i]));
bungeman918090c2016-02-09 09:14:28 -0800472 fItemArray[i].~T();
473 }
474 }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000475
476 static const int gMIN_ALLOC_COUNT = 8;
477
Mike Reed6c14c8d2017-03-09 16:36:26 -0500478 inline bool isNotUsingPreAlloc() const {
479 return !fItemArray || fPreAllocMemArray != fItemArray;
480 }
481
bsalomon@google.comd5104142013-06-13 15:13:46 +0000482 // Helper function that makes space for n objects, adjusts the count, but does not initialize
483 // the new objects.
484 void* push_back_raw(int n) {
485 this->checkRealloc(n);
486 void* ptr = fItemArray + fCount;
487 fCount += n;
488 return ptr;
489 }
490
bsalomon@google.com49313f62011-09-14 13:54:05 +0000491 inline void checkRealloc(int delta) {
492 SkASSERT(fCount >= 0);
493 SkASSERT(fAllocCount >= 0);
494
495 SkASSERT(-delta <= fCount);
496
497 int newCount = fCount + delta;
bungeman@google.coma12cc7f2011-10-07 20:54:15 +0000498 int newAllocCount = fAllocCount;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000499
bsalomon@google.com137209f2012-08-14 17:19:08 +0000500 if (newCount > fAllocCount || newCount < (fAllocCount / 3)) {
501 // whether we're growing or shrinking, we leave at least 50% extra space for future
502 // growth (clamped to the reserve count).
503 newAllocCount = SkMax32(newCount + ((newCount + 1) >> 1), fReserveCount);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000504 }
bungeman@google.coma12cc7f2011-10-07 20:54:15 +0000505 if (newAllocCount != fAllocCount) {
bsalomon@google.com49313f62011-09-14 13:54:05 +0000506
bungeman@google.coma12cc7f2011-10-07 20:54:15 +0000507 fAllocCount = newAllocCount;
bungeman0d9e9be2016-04-20 10:22:20 -0700508 void* newMemArray;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000509
bsalomon49f085d2014-09-05 13:34:00 -0700510 if (fAllocCount == fReserveCount && fPreAllocMemArray) {
bungeman0d9e9be2016-04-20 10:22:20 -0700511 newMemArray = fPreAllocMemArray;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000512 } else {
bungeman0d9e9be2016-04-20 10:22:20 -0700513 newMemArray = sk_malloc_throw(fAllocCount*sizeof(T));
bsalomon@google.com49313f62011-09-14 13:54:05 +0000514 }
515
bungeman918090c2016-02-09 09:14:28 -0800516 this->move(newMemArray);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000517
518 if (fMemArray != fPreAllocMemArray) {
519 sk_free(fMemArray);
520 }
bungeman@google.coma12cc7f2011-10-07 20:54:15 +0000521 fMemArray = newMemArray;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000522 }
523 }
524
bsalomon23e619c2015-02-06 11:54:28 -0800525 int fReserveCount;
526 int fCount;
527 int fAllocCount;
528 void* fPreAllocMemArray;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000529 union {
530 T* fItemArray;
531 void* fMemArray;
532 };
533};
534
bsalomon@google.com92669012011-09-27 19:10:05 +0000535/**
536 * Subclass of SkTArray that contains a preallocated memory block for the array.
537 */
Florin Malitac2d5bd02017-03-09 13:34:09 -0500538template <int N, typename T, bool MEM_MOVE= false>
539class SkSTArray : public SkTArray<T, MEM_MOVE> {
bsalomon@google.com92669012011-09-27 19:10:05 +0000540private:
Florin Malitac2d5bd02017-03-09 13:34:09 -0500541 typedef SkTArray<T, MEM_MOVE> INHERITED;
bsalomon@google.com92669012011-09-27 19:10:05 +0000542
543public:
544 SkSTArray() : INHERITED(&fStorage) {
545 }
546
547 SkSTArray(const SkSTArray& array)
548 : INHERITED(array, &fStorage) {
549 }
550
Florin Malitab1d800d2017-03-09 12:17:15 -0500551 SkSTArray(SkSTArray&& array)
552 : INHERITED(std::move(array), &fStorage) {
553 }
554
bsalomon@google.com92669012011-09-27 19:10:05 +0000555 explicit SkSTArray(const INHERITED& array)
556 : INHERITED(array, &fStorage) {
557 }
558
Florin Malitab1d800d2017-03-09 12:17:15 -0500559 explicit SkSTArray(INHERITED&& array)
560 : INHERITED(std::move(array), &fStorage) {
561 }
562
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000563 explicit SkSTArray(int reserveCount)
564 : INHERITED(reserveCount) {
565 }
566
bsalomon@google.com92669012011-09-27 19:10:05 +0000567 SkSTArray(const T* array, int count)
568 : INHERITED(array, count, &fStorage) {
569 }
570
571 SkSTArray& operator= (const SkSTArray& array) {
Florin Malitad54639f2017-03-12 10:40:13 -0400572 INHERITED::operator=(array);
573 return *this;
574 }
575
576 SkSTArray& operator= (SkSTArray&& array) {
577 INHERITED::operator=(std::move(array));
578 return *this;
bsalomon@google.com92669012011-09-27 19:10:05 +0000579 }
580
581 SkSTArray& operator= (const INHERITED& array) {
582 INHERITED::operator=(array);
583 return *this;
584 }
585
Florin Malitad54639f2017-03-12 10:40:13 -0400586 SkSTArray& operator= (INHERITED&& array) {
587 INHERITED::operator=(std::move(array));
588 return *this;
589 }
590
bsalomon@google.com92669012011-09-27 19:10:05 +0000591private:
592 SkAlignedSTStorage<N,T> fStorage;
593};
594
bsalomon@google.com49313f62011-09-14 13:54:05 +0000595#endif