blob: fdf96c87bca6f876b43bb988f6b6d0d51c20dd76 [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 */
Brian Salomon69225d02017-03-15 20:52:35 -040028 SkTArray() { this->init(); }
bsalomon@google.com49313f62011-09-14 13:54:05 +000029
30 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000031 * Creates an empty array that will preallocate space for reserveCount
bsalomon@google.com49313f62011-09-14 13:54:05 +000032 * elements.
33 */
Brian Salomon69225d02017-03-15 20:52:35 -040034 explicit SkTArray(int reserveCount) { this->init(0, reserveCount); }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000035
bsalomon@google.com49313f62011-09-14 13:54:05 +000036 /**
37 * Copies one array to another. The new array will be heap allocated.
38 */
bungeman0d9e9be2016-04-20 10:22:20 -070039 explicit SkTArray(const SkTArray& that) {
Brian Salomon69225d02017-03-15 20:52:35 -040040 this->init(that.fCount);
bungeman0d9e9be2016-04-20 10:22:20 -070041 this->copy(that.fItemArray);
42 }
Brian Salomon69225d02017-03-15 20:52:35 -040043
bungeman0d9e9be2016-04-20 10:22:20 -070044 explicit SkTArray(SkTArray&& that) {
Brian Salomon69225d02017-03-15 20:52:35 -040045 // TODO: If 'that' owns its memory why don't we just steal the pointer?
46 this->init(that.fCount);
bungeman0d9e9be2016-04-20 10:22:20 -070047 that.move(fMemArray);
48 that.fCount = 0;
bsalomon@google.com49313f62011-09-14 13:54:05 +000049 }
50
51 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000052 * Creates a SkTArray by copying contents of a standard C array. The new
bsalomon@google.com49313f62011-09-14 13:54:05 +000053 * array will be heap allocated. Be careful not to use this constructor
54 * when you really want the (void*, int) version.
55 */
56 SkTArray(const T* array, int count) {
Brian Salomon69225d02017-03-15 20:52:35 -040057 this->init(count);
bungeman0d9e9be2016-04-20 10:22:20 -070058 this->copy(array);
bsalomon@google.com49313f62011-09-14 13:54:05 +000059 }
60
Brian Salomon69225d02017-03-15 20:52:35 -040061 SkTArray& operator=(const SkTArray& that) {
bsalomon@google.com49313f62011-09-14 13:54:05 +000062 for (int i = 0; i < fCount; ++i) {
63 fItemArray[i].~T();
64 }
65 fCount = 0;
bungeman0d9e9be2016-04-20 10:22:20 -070066 this->checkRealloc(that.count());
67 fCount = that.count();
68 this->copy(that.fItemArray);
69 return *this;
70 }
Brian Salomon69225d02017-03-15 20:52:35 -040071 SkTArray& operator=(SkTArray&& that) {
bungeman0d9e9be2016-04-20 10:22:20 -070072 for (int i = 0; i < fCount; ++i) {
73 fItemArray[i].~T();
74 }
75 fCount = 0;
76 this->checkRealloc(that.count());
77 fCount = that.count();
78 that.move(fMemArray);
79 that.fCount = 0;
bsalomon@google.com49313f62011-09-14 13:54:05 +000080 return *this;
81 }
82
bsalomon383ff102015-07-31 11:53:11 -070083 ~SkTArray() {
bsalomon@google.com49313f62011-09-14 13:54:05 +000084 for (int i = 0; i < fCount; ++i) {
85 fItemArray[i].~T();
86 }
Brian Salomon69225d02017-03-15 20:52:35 -040087 if (fOwnMemory) {
bsalomon@google.com49313f62011-09-14 13:54:05 +000088 sk_free(fMemArray);
89 }
90 }
91
92 /**
93 * Resets to count() == 0
94 */
95 void reset() { this->pop_back_n(fCount); }
96
97 /**
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +000098 * Resets to count() = n newly constructed T objects.
99 */
100 void reset(int n) {
101 SkASSERT(n >= 0);
102 for (int i = 0; i < fCount; ++i) {
103 fItemArray[i].~T();
104 }
bungeman0d9e9be2016-04-20 10:22:20 -0700105 // Set fCount to 0 before calling checkRealloc so that no elements are moved.
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000106 fCount = 0;
107 this->checkRealloc(n);
108 fCount = n;
109 for (int i = 0; i < fCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -0700110 new (fItemArray + i) T;
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000111 }
112 }
113
114 /**
bungeman06ca8ec2016-06-09 08:01:03 -0700115 * Ensures there is enough reserved space for n elements.
116 */
117 void reserve(int n) {
118 if (fCount < n) {
119 this->checkRealloc(n - fCount);
120 }
121 }
122
123 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +0000124 * Resets to a copy of a C array.
125 */
126 void reset(const T* array, int count) {
127 for (int i = 0; i < fCount; ++i) {
128 fItemArray[i].~T();
129 }
bungeman0d9e9be2016-04-20 10:22:20 -0700130 fCount = 0;
131 this->checkRealloc(count);
jvanverth@google.com054ae992013-04-01 20:06:51 +0000132 fCount = count;
bungeman918090c2016-02-09 09:14:28 -0800133 this->copy(array);
bungeman@google.com95ebd172014-03-21 19:39:02 +0000134 }
135
136 void removeShuffle(int n) {
137 SkASSERT(n < fCount);
138 int newCount = fCount - 1;
139 fCount = newCount;
140 fItemArray[n].~T();
141 if (n != newCount) {
bungeman918090c2016-02-09 09:14:28 -0800142 this->move(n, newCount);
jvanverth@google.com054ae992013-04-01 20:06:51 +0000143 }
144 }
145
146 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000147 * Number of elements in the array.
148 */
149 int count() const { return fCount; }
150
151 /**
152 * Is the array empty.
153 */
154 bool empty() const { return !fCount; }
155
156 /**
bungeman@google.comd58a8562014-03-24 15:55:01 +0000157 * Adds 1 new default-initialized T value and returns it by reference. Note
bsalomon@google.com49313f62011-09-14 13:54:05 +0000158 * the reference only remains valid until the next call that adds or removes
159 * elements.
160 */
161 T& push_back() {
krasine0c1d282016-04-21 08:34:00 -0700162 void* newT = this->push_back_raw(1);
163 return *new (newT) T;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000164 }
165
166 /**
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000167 * Version of above that uses a copy constructor to initialize the new item
168 */
169 T& push_back(const T& t) {
krasine0c1d282016-04-21 08:34:00 -0700170 void* newT = this->push_back_raw(1);
171 return *new (newT) T(t);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000172 }
173
174 /**
halcanary91fcb3e2016-03-04 13:53:22 -0800175 * Version of above that uses a move constructor to initialize the new item
176 */
177 T& push_back(T&& t) {
krasine0c1d282016-04-21 08:34:00 -0700178 void* newT = this->push_back_raw(1);
179 return *new (newT) T(std::move(t));
halcanary91fcb3e2016-03-04 13:53:22 -0800180 }
181
182 /**
halcanaryf12a1672015-09-23 12:45:49 -0700183 * Construct a new T at the back of this array.
184 */
185 template<class... Args> T& emplace_back(Args&&... args) {
krasine0c1d282016-04-21 08:34:00 -0700186 void* newT = this->push_back_raw(1);
bungeman221524d2016-01-05 14:59:40 -0800187 return *new (newT) T(std::forward<Args>(args)...);
halcanaryf12a1672015-09-23 12:45:49 -0700188 }
189
190 /**
bungeman@google.comd58a8562014-03-24 15:55:01 +0000191 * Allocates n more default-initialized T values, and returns the address of
192 * the start of that new range. Note: this address is only valid until the
193 * next API call made on the array that might add or remove elements.
bsalomon@google.com49313f62011-09-14 13:54:05 +0000194 */
195 T* push_back_n(int n) {
196 SkASSERT(n >= 0);
bungeman6a6f3c52016-04-21 10:52:03 -0700197 void* newTs = this->push_back_raw(n);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000198 for (int i = 0; i < n; ++i) {
bungeman6a6f3c52016-04-21 10:52:03 -0700199 new (static_cast<char*>(newTs) + i * sizeof(T)) T;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000200 }
bungeman6a6f3c52016-04-21 10:52:03 -0700201 return static_cast<T*>(newTs);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000202 }
203
204 /**
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000205 * Version of above that uses a copy constructor to initialize all n items
206 * to the same T.
207 */
208 T* push_back_n(int n, const T& t) {
209 SkASSERT(n >= 0);
bungeman6a6f3c52016-04-21 10:52:03 -0700210 void* newTs = this->push_back_raw(n);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000211 for (int i = 0; i < n; ++i) {
bungeman6a6f3c52016-04-21 10:52:03 -0700212 new (static_cast<char*>(newTs) + i * sizeof(T)) T(t);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000213 }
bungeman6a6f3c52016-04-21 10:52:03 -0700214 return static_cast<T*>(newTs);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000215 }
216
217 /**
218 * Version of above that uses a copy constructor to initialize the n items
219 * to separate T values.
220 */
221 T* push_back_n(int n, const T t[]) {
222 SkASSERT(n >= 0);
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000223 this->checkRealloc(n);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000224 for (int i = 0; i < n; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -0700225 new (fItemArray + fCount + i) T(t[i]);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000226 }
227 fCount += n;
228 return fItemArray + fCount - n;
229 }
230
231 /**
msarett10e3d9b2016-08-18 15:46:03 -0700232 * Version of above that uses the move constructor to set n items.
233 */
234 T* move_back_n(int n, T* t) {
235 SkASSERT(n >= 0);
236 this->checkRealloc(n);
237 for (int i = 0; i < n; ++i) {
238 new (fItemArray + fCount + i) T(std::move(t[i]));
239 }
240 fCount += n;
241 return fItemArray + fCount - n;
242 }
243
244 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000245 * Removes the last element. Not safe to call when count() == 0.
246 */
247 void pop_back() {
248 SkASSERT(fCount > 0);
249 --fCount;
250 fItemArray[fCount].~T();
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000251 this->checkRealloc(0);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000252 }
253
254 /**
255 * Removes the last n elements. Not safe to call when count() < n.
256 */
257 void pop_back_n(int n) {
258 SkASSERT(n >= 0);
259 SkASSERT(fCount >= n);
260 fCount -= n;
261 for (int i = 0; i < n; ++i) {
bsalomon@google.comd5104142013-06-13 15:13:46 +0000262 fItemArray[fCount + i].~T();
bsalomon@google.com49313f62011-09-14 13:54:05 +0000263 }
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000264 this->checkRealloc(0);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000265 }
266
267 /**
268 * Pushes or pops from the back to resize. Pushes will be default
269 * initialized.
270 */
271 void resize_back(int newCount) {
272 SkASSERT(newCount >= 0);
273
274 if (newCount > fCount) {
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000275 this->push_back_n(newCount - fCount);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000276 } else if (newCount < fCount) {
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000277 this->pop_back_n(fCount - newCount);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000278 }
279 }
280
bsalomon23e619c2015-02-06 11:54:28 -0800281 /** Swaps the contents of this array with that array. Does a pointer swap if possible,
282 otherwise copies the T values. */
283 void swap(SkTArray* that) {
bsalomon3632f842015-02-10 19:46:58 -0800284 if (this == that) {
285 return;
286 }
Brian Salomon69225d02017-03-15 20:52:35 -0400287 if (fOwnMemory && that->fOwnMemory) {
bsalomon23e619c2015-02-06 11:54:28 -0800288 SkTSwap(fItemArray, that->fItemArray);
289 SkTSwap(fCount, that->fCount);
290 SkTSwap(fAllocCount, that->fAllocCount);
291 } else {
292 // This could be more optimal...
bungeman0d9e9be2016-04-20 10:22:20 -0700293 SkTArray copy(std::move(*that));
294 *that = std::move(*this);
295 *this = std::move(copy);
bsalomon23e619c2015-02-06 11:54:28 -0800296 }
297 }
298
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000299 T* begin() {
300 return fItemArray;
301 }
302 const T* begin() const {
303 return fItemArray;
304 }
305 T* end() {
306 return fItemArray ? fItemArray + fCount : NULL;
307 }
308 const T* end() const {
tfarina567ff2f2015-04-27 07:01:44 -0700309 return fItemArray ? fItemArray + fCount : NULL;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000310 }
311
312 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000313 * Get the i^th element.
314 */
315 T& operator[] (int i) {
316 SkASSERT(i < fCount);
317 SkASSERT(i >= 0);
318 return fItemArray[i];
319 }
320
321 const T& operator[] (int i) const {
322 SkASSERT(i < fCount);
323 SkASSERT(i >= 0);
324 return fItemArray[i];
325 }
326
327 /**
328 * equivalent to operator[](0)
329 */
330 T& front() { SkASSERT(fCount > 0); return fItemArray[0];}
331
332 const T& front() const { SkASSERT(fCount > 0); return fItemArray[0];}
333
334 /**
335 * equivalent to operator[](count() - 1)
336 */
337 T& back() { SkASSERT(fCount); return fItemArray[fCount - 1];}
338
339 const T& back() const { SkASSERT(fCount > 0); return fItemArray[fCount - 1];}
340
341 /**
342 * equivalent to operator[](count()-1-i)
343 */
344 T& fromBack(int i) {
345 SkASSERT(i >= 0);
346 SkASSERT(i < fCount);
347 return fItemArray[fCount - i - 1];
348 }
349
350 const T& fromBack(int i) const {
351 SkASSERT(i >= 0);
352 SkASSERT(i < fCount);
353 return fItemArray[fCount - i - 1];
354 }
355
Florin Malitac2d5bd02017-03-09 13:34:09 -0500356 bool operator==(const SkTArray<T, MEM_MOVE>& right) const {
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000357 int leftCount = this->count();
358 if (leftCount != right.count()) {
359 return false;
360 }
361 for (int index = 0; index < leftCount; ++index) {
362 if (fItemArray[index] != right.fItemArray[index]) {
363 return false;
364 }
365 }
366 return true;
367 }
368
Florin Malitac2d5bd02017-03-09 13:34:09 -0500369 bool operator!=(const SkTArray<T, MEM_MOVE>& right) const {
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000370 return !(*this == right);
371 }
372
Brian Salomon69225d02017-03-15 20:52:35 -0400373 inline int allocCntForTest() const;
374
bsalomon@google.com92669012011-09-27 19:10:05 +0000375protected:
376 /**
377 * Creates an empty array that will use the passed storage block until it
378 * is insufficiently large to hold the entire array.
379 */
380 template <int N>
381 SkTArray(SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400382 this->initWithPreallocatedStorage(0, storage->get(), N);
bsalomon@google.com92669012011-09-27 19:10:05 +0000383 }
384
385 /**
386 * Copy another array, using preallocated storage if preAllocCount >=
387 * array.count(). Otherwise storage will only be used when array shrinks
388 * to fit.
389 */
390 template <int N>
391 SkTArray(const SkTArray& array, SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400392 this->initWithPreallocatedStorage(array.fCount, storage->get(), N);
bungeman0d9e9be2016-04-20 10:22:20 -0700393 this->copy(array.fItemArray);
bsalomon@google.com92669012011-09-27 19:10:05 +0000394 }
395
396 /**
Florin Malitab1d800d2017-03-09 12:17:15 -0500397 * Move another array, using preallocated storage if preAllocCount >=
398 * array.count(). Otherwise storage will only be used when array shrinks
399 * to fit.
400 */
401 template <int N>
402 SkTArray(SkTArray&& array, SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400403 this->initWithPreallocatedStorage(array.fCount, storage->get(), N);
Florin Malitab1d800d2017-03-09 12:17:15 -0500404 array.move(fMemArray);
405 array.fCount = 0;
406 }
407
408 /**
bsalomon@google.com92669012011-09-27 19:10:05 +0000409 * Copy a C array, using preallocated storage if preAllocCount >=
410 * count. Otherwise storage will only be used when array shrinks
411 * to fit.
412 */
413 template <int N>
414 SkTArray(const T* array, int count, SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400415 this->initWithPreallocatedStorage(count, storage->get(), N);
bungeman0d9e9be2016-04-20 10:22:20 -0700416 this->copy(array);
bsalomon@google.com92669012011-09-27 19:10:05 +0000417 }
418
Brian Salomon69225d02017-03-15 20:52:35 -0400419private:
420 void init(int count = 0, int reserveCount = 0) {
junov@chromium.orgd80a5092011-11-30 18:35:19 +0000421 SkASSERT(count >= 0);
Brian Salomon69225d02017-03-15 20:52:35 -0400422 SkASSERT(reserveCount >= 0);
423 fCount = count;
424 if (!count && !reserveCount) {
425 fAllocCount = 0;
426 fMemArray = nullptr;
427 fOwnMemory = false;
bsalomon@google.com92669012011-09-27 19:10:05 +0000428 } else {
Brian Salomon69225d02017-03-15 20:52:35 -0400429 fAllocCount = SkTMax(count, SkTMax(kMinHeapAllocCount, reserveCount));
junov@chromium.orgd80a5092011-11-30 18:35:19 +0000430 fMemArray = sk_malloc_throw(fAllocCount * sizeof(T));
Brian Salomon69225d02017-03-15 20:52:35 -0400431 fOwnMemory = true;
bsalomon@google.com92669012011-09-27 19:10:05 +0000432 }
bsalomon@google.com92669012011-09-27 19:10:05 +0000433 }
434
Brian Salomon69225d02017-03-15 20:52:35 -0400435 void initWithPreallocatedStorage(int count, void* preallocStorage, int preallocCount) {
436 SkASSERT(count >= 0);
437 SkASSERT(preallocCount > 0);
438 SkASSERT(preallocStorage);
439 fCount = count;
440 fMemArray = nullptr;
441 if (count > preallocCount) {
442 fAllocCount = SkTMax(count, kMinHeapAllocCount);
443 fMemArray = sk_malloc_throw(fAllocCount * sizeof(T));
444 fOwnMemory = true;
445 } else {
446 fAllocCount = preallocCount;
447 fMemArray = preallocStorage;
448 fOwnMemory = false;
449 }
450 }
451
bungeman918090c2016-02-09 09:14:28 -0800452 /** In the following move and copy methods, 'dst' is assumed to be uninitialized raw storage.
453 * In the following move methods, 'src' is destroyed leaving behind uninitialized raw storage.
454 */
Florin Malitac2d5bd02017-03-09 13:34:09 -0500455 void copy(const T* src) {
456 // Some types may be trivially copyable, in which case we *could* use memcopy; but
457 // MEM_MOVE == true implies that the type is trivially movable, and not necessarily
458 // trivially copyable (think sk_sp<>). So short of adding another template arg, we
459 // must be conservative and use copy construction.
bungeman918090c2016-02-09 09:14:28 -0800460 for (int i = 0; i < fCount; ++i) {
461 new (fItemArray + i) T(src[i]);
462 }
463 }
Florin Malitac2d5bd02017-03-09 13:34:09 -0500464
465 template <bool E = MEM_MOVE> SK_WHEN(E, void) move(int dst, int src) {
466 memcpy(&fItemArray[dst], &fItemArray[src], sizeof(T));
467 }
468 template <bool E = MEM_MOVE> SK_WHEN(E, void) move(void* dst) {
469 sk_careful_memcpy(dst, fMemArray, fCount * sizeof(T));
470 }
471
472 template <bool E = MEM_MOVE> SK_WHEN(!E, void) move(int dst, int src) {
bungeman918090c2016-02-09 09:14:28 -0800473 new (&fItemArray[dst]) T(std::move(fItemArray[src]));
474 fItemArray[src].~T();
475 }
Florin Malitac2d5bd02017-03-09 13:34:09 -0500476 template <bool E = MEM_MOVE> SK_WHEN(!E, void) move(void* dst) {
bungeman918090c2016-02-09 09:14:28 -0800477 for (int i = 0; i < fCount; ++i) {
bungeman0d9e9be2016-04-20 10:22:20 -0700478 new (static_cast<char*>(dst) + sizeof(T) * i) T(std::move(fItemArray[i]));
bungeman918090c2016-02-09 09:14:28 -0800479 fItemArray[i].~T();
480 }
481 }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000482
Brian Salomon69225d02017-03-15 20:52:35 -0400483 static constexpr int kMinHeapAllocCount = 8;
Mike Reed6c14c8d2017-03-09 16:36:26 -0500484
bsalomon@google.comd5104142013-06-13 15:13:46 +0000485 // Helper function that makes space for n objects, adjusts the count, but does not initialize
486 // the new objects.
487 void* push_back_raw(int n) {
488 this->checkRealloc(n);
489 void* ptr = fItemArray + fCount;
490 fCount += n;
491 return ptr;
492 }
493
Brian Salomon69225d02017-03-15 20:52:35 -0400494 void checkRealloc(int delta) {
bsalomon@google.com49313f62011-09-14 13:54:05 +0000495 SkASSERT(fCount >= 0);
496 SkASSERT(fAllocCount >= 0);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000497 SkASSERT(-delta <= fCount);
498
499 int newCount = fCount + delta;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000500
Brian Salomon69225d02017-03-15 20:52:35 -0400501 // We allow fAllocCount to be in the range [newCount, 3*newCount]. We also never shrink
502 // when we're currently using preallocated memory or would allocate less than
503 // kMinHeapAllocCount.
504 bool mustGrow = newCount > fAllocCount;
505 bool shouldShrink = fAllocCount > 3 * newCount && fOwnMemory;
506 if (!mustGrow && !shouldShrink) {
507 return;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000508 }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000509
Brian Salomon69225d02017-03-15 20:52:35 -0400510 // Whether we're growing or shrinking, we leave at least 50% extra space for future growth.
511 int newAllocCount = newCount + ((newCount + 1) >> 1);
512 // Align the new allocation count to kMinHeapAllocCount.
513 static_assert(SkIsPow2(kMinHeapAllocCount), "min alloc count not power of two.");
514 newAllocCount = (newAllocCount + (kMinHeapAllocCount - 1)) & ~(kMinHeapAllocCount - 1);
515 // At small sizes the old and new alloc count can both be kMinHeapAllocCount.
516 if (newAllocCount == fAllocCount) {
517 return;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000518 }
Brian Salomon69225d02017-03-15 20:52:35 -0400519 fAllocCount = newAllocCount;
520 void* newMemArray = sk_malloc_throw(fAllocCount * sizeof(T));
521 this->move(newMemArray);
522 if (fOwnMemory) {
523 sk_free(fMemArray);
524
525 }
526 fMemArray = newMemArray;
527 fOwnMemory = true;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000528 }
529
Brian Salomon69225d02017-03-15 20:52:35 -0400530 int fCount;
531 int fAllocCount;
532 bool fOwnMemory;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000533 union {
534 T* fItemArray;
535 void* fMemArray;
536 };
537};
538
Brian Salomon69225d02017-03-15 20:52:35 -0400539template<typename T, bool MEM_MOVE> constexpr int SkTArray<T, MEM_MOVE>::kMinHeapAllocCount;
540
bsalomon@google.com92669012011-09-27 19:10:05 +0000541/**
542 * Subclass of SkTArray that contains a preallocated memory block for the array.
543 */
Florin Malitac2d5bd02017-03-09 13:34:09 -0500544template <int N, typename T, bool MEM_MOVE= false>
545class SkSTArray : public SkTArray<T, MEM_MOVE> {
bsalomon@google.com92669012011-09-27 19:10:05 +0000546private:
Florin Malitac2d5bd02017-03-09 13:34:09 -0500547 typedef SkTArray<T, MEM_MOVE> INHERITED;
bsalomon@google.com92669012011-09-27 19:10:05 +0000548
549public:
550 SkSTArray() : INHERITED(&fStorage) {
551 }
552
553 SkSTArray(const SkSTArray& array)
554 : INHERITED(array, &fStorage) {
555 }
556
Florin Malitab1d800d2017-03-09 12:17:15 -0500557 SkSTArray(SkSTArray&& array)
558 : INHERITED(std::move(array), &fStorage) {
559 }
560
bsalomon@google.com92669012011-09-27 19:10:05 +0000561 explicit SkSTArray(const INHERITED& array)
562 : INHERITED(array, &fStorage) {
563 }
564
Florin Malitab1d800d2017-03-09 12:17:15 -0500565 explicit SkSTArray(INHERITED&& array)
566 : INHERITED(std::move(array), &fStorage) {
567 }
568
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000569 explicit SkSTArray(int reserveCount)
570 : INHERITED(reserveCount) {
571 }
572
bsalomon@google.com92669012011-09-27 19:10:05 +0000573 SkSTArray(const T* array, int count)
574 : INHERITED(array, count, &fStorage) {
575 }
576
Brian Salomon69225d02017-03-15 20:52:35 -0400577 SkSTArray& operator=(const SkSTArray& array) {
Florin Malitad54639f2017-03-12 10:40:13 -0400578 INHERITED::operator=(array);
579 return *this;
580 }
581
Brian Salomon69225d02017-03-15 20:52:35 -0400582 SkSTArray& operator=(SkSTArray&& array) {
Florin Malitad54639f2017-03-12 10:40:13 -0400583 INHERITED::operator=(std::move(array));
584 return *this;
bsalomon@google.com92669012011-09-27 19:10:05 +0000585 }
586
Brian Salomon69225d02017-03-15 20:52:35 -0400587 SkSTArray& operator=(const INHERITED& array) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000588 INHERITED::operator=(array);
589 return *this;
590 }
591
Brian Salomon69225d02017-03-15 20:52:35 -0400592 SkSTArray& operator=(INHERITED&& array) {
Florin Malitad54639f2017-03-12 10:40:13 -0400593 INHERITED::operator=(std::move(array));
594 return *this;
595 }
596
bsalomon@google.com92669012011-09-27 19:10:05 +0000597private:
598 SkAlignedSTStorage<N,T> fStorage;
599};
600
bsalomon@google.com49313f62011-09-14 13:54:05 +0000601#endif