blob: 4b700d2d297a3d31c80d97bbf484fe2c15cf663b [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) {
Greg Daniel70131b92017-03-22 13:33:21 -040062 if (this == &that) {
63 return *this;
64 }
bsalomon@google.com49313f62011-09-14 13:54:05 +000065 for (int i = 0; i < fCount; ++i) {
66 fItemArray[i].~T();
67 }
68 fCount = 0;
bungeman0d9e9be2016-04-20 10:22:20 -070069 this->checkRealloc(that.count());
70 fCount = that.count();
71 this->copy(that.fItemArray);
72 return *this;
73 }
Brian Salomon69225d02017-03-15 20:52:35 -040074 SkTArray& operator=(SkTArray&& that) {
Greg Daniel70131b92017-03-22 13:33:21 -040075 if (this == &that) {
76 return *this;
77 }
bungeman0d9e9be2016-04-20 10:22:20 -070078 for (int i = 0; i < fCount; ++i) {
79 fItemArray[i].~T();
80 }
81 fCount = 0;
82 this->checkRealloc(that.count());
83 fCount = that.count();
84 that.move(fMemArray);
85 that.fCount = 0;
bsalomon@google.com49313f62011-09-14 13:54:05 +000086 return *this;
87 }
88
bsalomon383ff102015-07-31 11:53:11 -070089 ~SkTArray() {
bsalomon@google.com49313f62011-09-14 13:54:05 +000090 for (int i = 0; i < fCount; ++i) {
91 fItemArray[i].~T();
92 }
Brian Salomon69225d02017-03-15 20:52:35 -040093 if (fOwnMemory) {
bsalomon@google.com49313f62011-09-14 13:54:05 +000094 sk_free(fMemArray);
95 }
96 }
97
98 /**
99 * Resets to count() == 0
100 */
101 void reset() { this->pop_back_n(fCount); }
102
103 /**
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000104 * Resets to count() = n newly constructed T objects.
105 */
106 void reset(int n) {
107 SkASSERT(n >= 0);
108 for (int i = 0; i < fCount; ++i) {
109 fItemArray[i].~T();
110 }
bungeman0d9e9be2016-04-20 10:22:20 -0700111 // Set fCount to 0 before calling checkRealloc so that no elements are moved.
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000112 fCount = 0;
113 this->checkRealloc(n);
114 fCount = n;
115 for (int i = 0; i < fCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -0700116 new (fItemArray + i) T;
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000117 }
118 }
119
120 /**
bungeman06ca8ec2016-06-09 08:01:03 -0700121 * Ensures there is enough reserved space for n elements.
122 */
123 void reserve(int n) {
124 if (fCount < n) {
125 this->checkRealloc(n - fCount);
126 }
127 }
128
129 /**
jvanverth@google.com054ae992013-04-01 20:06:51 +0000130 * Resets to a copy of a C array.
131 */
132 void reset(const T* array, int count) {
133 for (int i = 0; i < fCount; ++i) {
134 fItemArray[i].~T();
135 }
bungeman0d9e9be2016-04-20 10:22:20 -0700136 fCount = 0;
137 this->checkRealloc(count);
jvanverth@google.com054ae992013-04-01 20:06:51 +0000138 fCount = count;
bungeman918090c2016-02-09 09:14:28 -0800139 this->copy(array);
bungeman@google.com95ebd172014-03-21 19:39:02 +0000140 }
141
142 void removeShuffle(int n) {
143 SkASSERT(n < fCount);
144 int newCount = fCount - 1;
145 fCount = newCount;
146 fItemArray[n].~T();
147 if (n != newCount) {
bungeman918090c2016-02-09 09:14:28 -0800148 this->move(n, newCount);
jvanverth@google.com054ae992013-04-01 20:06:51 +0000149 }
150 }
151
152 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000153 * Number of elements in the array.
154 */
155 int count() const { return fCount; }
156
157 /**
158 * Is the array empty.
159 */
160 bool empty() const { return !fCount; }
161
162 /**
bungeman@google.comd58a8562014-03-24 15:55:01 +0000163 * Adds 1 new default-initialized T value and returns it by reference. Note
bsalomon@google.com49313f62011-09-14 13:54:05 +0000164 * the reference only remains valid until the next call that adds or removes
165 * elements.
166 */
167 T& push_back() {
krasine0c1d282016-04-21 08:34:00 -0700168 void* newT = this->push_back_raw(1);
169 return *new (newT) T;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000170 }
171
172 /**
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000173 * Version of above that uses a copy constructor to initialize the new item
174 */
175 T& push_back(const T& t) {
krasine0c1d282016-04-21 08:34:00 -0700176 void* newT = this->push_back_raw(1);
177 return *new (newT) T(t);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000178 }
179
180 /**
halcanary91fcb3e2016-03-04 13:53:22 -0800181 * Version of above that uses a move constructor to initialize the new item
182 */
183 T& push_back(T&& t) {
krasine0c1d282016-04-21 08:34:00 -0700184 void* newT = this->push_back_raw(1);
185 return *new (newT) T(std::move(t));
halcanary91fcb3e2016-03-04 13:53:22 -0800186 }
187
188 /**
halcanaryf12a1672015-09-23 12:45:49 -0700189 * Construct a new T at the back of this array.
190 */
191 template<class... Args> T& emplace_back(Args&&... args) {
krasine0c1d282016-04-21 08:34:00 -0700192 void* newT = this->push_back_raw(1);
bungeman221524d2016-01-05 14:59:40 -0800193 return *new (newT) T(std::forward<Args>(args)...);
halcanaryf12a1672015-09-23 12:45:49 -0700194 }
195
196 /**
bungeman@google.comd58a8562014-03-24 15:55:01 +0000197 * Allocates n more default-initialized T values, and returns the address of
198 * the start of that new range. Note: this address is only valid until the
199 * next API call made on the array that might add or remove elements.
bsalomon@google.com49313f62011-09-14 13:54:05 +0000200 */
201 T* push_back_n(int n) {
202 SkASSERT(n >= 0);
bungeman6a6f3c52016-04-21 10:52:03 -0700203 void* newTs = this->push_back_raw(n);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000204 for (int i = 0; i < n; ++i) {
bungeman6a6f3c52016-04-21 10:52:03 -0700205 new (static_cast<char*>(newTs) + i * sizeof(T)) T;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000206 }
bungeman6a6f3c52016-04-21 10:52:03 -0700207 return static_cast<T*>(newTs);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000208 }
209
210 /**
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000211 * Version of above that uses a copy constructor to initialize all n items
212 * to the same T.
213 */
214 T* push_back_n(int n, const T& t) {
215 SkASSERT(n >= 0);
bungeman6a6f3c52016-04-21 10:52:03 -0700216 void* newTs = this->push_back_raw(n);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000217 for (int i = 0; i < n; ++i) {
bungeman6a6f3c52016-04-21 10:52:03 -0700218 new (static_cast<char*>(newTs) + i * sizeof(T)) T(t);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000219 }
bungeman6a6f3c52016-04-21 10:52:03 -0700220 return static_cast<T*>(newTs);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000221 }
222
223 /**
224 * Version of above that uses a copy constructor to initialize the n items
225 * to separate T values.
226 */
227 T* push_back_n(int n, const T t[]) {
228 SkASSERT(n >= 0);
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000229 this->checkRealloc(n);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000230 for (int i = 0; i < n; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -0700231 new (fItemArray + fCount + i) T(t[i]);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000232 }
233 fCount += n;
234 return fItemArray + fCount - n;
235 }
236
237 /**
msarett10e3d9b2016-08-18 15:46:03 -0700238 * Version of above that uses the move constructor to set n items.
239 */
240 T* move_back_n(int n, T* t) {
241 SkASSERT(n >= 0);
242 this->checkRealloc(n);
243 for (int i = 0; i < n; ++i) {
244 new (fItemArray + fCount + i) T(std::move(t[i]));
245 }
246 fCount += n;
247 return fItemArray + fCount - n;
248 }
249
250 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000251 * Removes the last element. Not safe to call when count() == 0.
252 */
253 void pop_back() {
254 SkASSERT(fCount > 0);
255 --fCount;
256 fItemArray[fCount].~T();
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000257 this->checkRealloc(0);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000258 }
259
260 /**
261 * Removes the last n elements. Not safe to call when count() < n.
262 */
263 void pop_back_n(int n) {
264 SkASSERT(n >= 0);
265 SkASSERT(fCount >= n);
266 fCount -= n;
267 for (int i = 0; i < n; ++i) {
bsalomon@google.comd5104142013-06-13 15:13:46 +0000268 fItemArray[fCount + i].~T();
bsalomon@google.com49313f62011-09-14 13:54:05 +0000269 }
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000270 this->checkRealloc(0);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000271 }
272
273 /**
274 * Pushes or pops from the back to resize. Pushes will be default
275 * initialized.
276 */
277 void resize_back(int newCount) {
278 SkASSERT(newCount >= 0);
279
280 if (newCount > fCount) {
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000281 this->push_back_n(newCount - fCount);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000282 } else if (newCount < fCount) {
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000283 this->pop_back_n(fCount - newCount);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000284 }
285 }
286
bsalomon23e619c2015-02-06 11:54:28 -0800287 /** Swaps the contents of this array with that array. Does a pointer swap if possible,
288 otherwise copies the T values. */
289 void swap(SkTArray* that) {
bsalomon3632f842015-02-10 19:46:58 -0800290 if (this == that) {
291 return;
292 }
Brian Salomon69225d02017-03-15 20:52:35 -0400293 if (fOwnMemory && that->fOwnMemory) {
bsalomon23e619c2015-02-06 11:54:28 -0800294 SkTSwap(fItemArray, that->fItemArray);
295 SkTSwap(fCount, that->fCount);
296 SkTSwap(fAllocCount, that->fAllocCount);
297 } else {
298 // This could be more optimal...
bungeman0d9e9be2016-04-20 10:22:20 -0700299 SkTArray copy(std::move(*that));
300 *that = std::move(*this);
301 *this = std::move(copy);
bsalomon23e619c2015-02-06 11:54:28 -0800302 }
303 }
304
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000305 T* begin() {
306 return fItemArray;
307 }
308 const T* begin() const {
309 return fItemArray;
310 }
311 T* end() {
312 return fItemArray ? fItemArray + fCount : NULL;
313 }
314 const T* end() const {
tfarina567ff2f2015-04-27 07:01:44 -0700315 return fItemArray ? fItemArray + fCount : NULL;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000316 }
317
318 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000319 * Get the i^th element.
320 */
321 T& operator[] (int i) {
322 SkASSERT(i < fCount);
323 SkASSERT(i >= 0);
324 return fItemArray[i];
325 }
326
327 const T& operator[] (int i) const {
328 SkASSERT(i < fCount);
329 SkASSERT(i >= 0);
330 return fItemArray[i];
331 }
332
333 /**
334 * equivalent to operator[](0)
335 */
336 T& front() { SkASSERT(fCount > 0); return fItemArray[0];}
337
338 const T& front() const { SkASSERT(fCount > 0); return fItemArray[0];}
339
340 /**
341 * equivalent to operator[](count() - 1)
342 */
343 T& back() { SkASSERT(fCount); return fItemArray[fCount - 1];}
344
345 const T& back() const { SkASSERT(fCount > 0); return fItemArray[fCount - 1];}
346
347 /**
348 * equivalent to operator[](count()-1-i)
349 */
350 T& fromBack(int i) {
351 SkASSERT(i >= 0);
352 SkASSERT(i < fCount);
353 return fItemArray[fCount - i - 1];
354 }
355
356 const T& fromBack(int i) const {
357 SkASSERT(i >= 0);
358 SkASSERT(i < fCount);
359 return fItemArray[fCount - i - 1];
360 }
361
Florin Malitac2d5bd02017-03-09 13:34:09 -0500362 bool operator==(const SkTArray<T, MEM_MOVE>& right) const {
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000363 int leftCount = this->count();
364 if (leftCount != right.count()) {
365 return false;
366 }
367 for (int index = 0; index < leftCount; ++index) {
368 if (fItemArray[index] != right.fItemArray[index]) {
369 return false;
370 }
371 }
372 return true;
373 }
374
Florin Malitac2d5bd02017-03-09 13:34:09 -0500375 bool operator!=(const SkTArray<T, MEM_MOVE>& right) const {
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000376 return !(*this == right);
377 }
378
Brian Salomon69225d02017-03-15 20:52:35 -0400379 inline int allocCntForTest() const;
380
bsalomon@google.com92669012011-09-27 19:10:05 +0000381protected:
382 /**
383 * Creates an empty array that will use the passed storage block until it
384 * is insufficiently large to hold the entire array.
385 */
386 template <int N>
387 SkTArray(SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400388 this->initWithPreallocatedStorage(0, storage->get(), N);
bsalomon@google.com92669012011-09-27 19:10:05 +0000389 }
390
391 /**
392 * Copy another array, using preallocated storage if preAllocCount >=
393 * array.count(). Otherwise storage will only be used when array shrinks
394 * to fit.
395 */
396 template <int N>
397 SkTArray(const SkTArray& array, SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400398 this->initWithPreallocatedStorage(array.fCount, storage->get(), N);
bungeman0d9e9be2016-04-20 10:22:20 -0700399 this->copy(array.fItemArray);
bsalomon@google.com92669012011-09-27 19:10:05 +0000400 }
401
402 /**
Florin Malitab1d800d2017-03-09 12:17:15 -0500403 * Move another array, using preallocated storage if preAllocCount >=
404 * array.count(). Otherwise storage will only be used when array shrinks
405 * to fit.
406 */
407 template <int N>
408 SkTArray(SkTArray&& array, SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400409 this->initWithPreallocatedStorage(array.fCount, storage->get(), N);
Florin Malitab1d800d2017-03-09 12:17:15 -0500410 array.move(fMemArray);
411 array.fCount = 0;
412 }
413
414 /**
bsalomon@google.com92669012011-09-27 19:10:05 +0000415 * Copy a C array, using preallocated storage if preAllocCount >=
416 * count. Otherwise storage will only be used when array shrinks
417 * to fit.
418 */
419 template <int N>
420 SkTArray(const T* array, int count, SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400421 this->initWithPreallocatedStorage(count, storage->get(), N);
bungeman0d9e9be2016-04-20 10:22:20 -0700422 this->copy(array);
bsalomon@google.com92669012011-09-27 19:10:05 +0000423 }
424
Brian Salomon69225d02017-03-15 20:52:35 -0400425private:
426 void init(int count = 0, int reserveCount = 0) {
junov@chromium.orgd80a5092011-11-30 18:35:19 +0000427 SkASSERT(count >= 0);
Brian Salomon69225d02017-03-15 20:52:35 -0400428 SkASSERT(reserveCount >= 0);
429 fCount = count;
430 if (!count && !reserveCount) {
431 fAllocCount = 0;
432 fMemArray = nullptr;
433 fOwnMemory = false;
bsalomon@google.com92669012011-09-27 19:10:05 +0000434 } else {
Brian Salomon69225d02017-03-15 20:52:35 -0400435 fAllocCount = SkTMax(count, SkTMax(kMinHeapAllocCount, reserveCount));
junov@chromium.orgd80a5092011-11-30 18:35:19 +0000436 fMemArray = sk_malloc_throw(fAllocCount * sizeof(T));
Brian Salomon69225d02017-03-15 20:52:35 -0400437 fOwnMemory = true;
bsalomon@google.com92669012011-09-27 19:10:05 +0000438 }
bsalomon@google.com92669012011-09-27 19:10:05 +0000439 }
440
Brian Salomon69225d02017-03-15 20:52:35 -0400441 void initWithPreallocatedStorage(int count, void* preallocStorage, int preallocCount) {
442 SkASSERT(count >= 0);
443 SkASSERT(preallocCount > 0);
444 SkASSERT(preallocStorage);
445 fCount = count;
446 fMemArray = nullptr;
447 if (count > preallocCount) {
448 fAllocCount = SkTMax(count, kMinHeapAllocCount);
449 fMemArray = sk_malloc_throw(fAllocCount * sizeof(T));
450 fOwnMemory = true;
451 } else {
452 fAllocCount = preallocCount;
453 fMemArray = preallocStorage;
454 fOwnMemory = false;
455 }
456 }
457
bungeman918090c2016-02-09 09:14:28 -0800458 /** In the following move and copy methods, 'dst' is assumed to be uninitialized raw storage.
459 * In the following move methods, 'src' is destroyed leaving behind uninitialized raw storage.
460 */
Florin Malitac2d5bd02017-03-09 13:34:09 -0500461 void copy(const T* src) {
462 // Some types may be trivially copyable, in which case we *could* use memcopy; but
463 // MEM_MOVE == true implies that the type is trivially movable, and not necessarily
464 // trivially copyable (think sk_sp<>). So short of adding another template arg, we
465 // must be conservative and use copy construction.
bungeman918090c2016-02-09 09:14:28 -0800466 for (int i = 0; i < fCount; ++i) {
467 new (fItemArray + i) T(src[i]);
468 }
469 }
Florin Malitac2d5bd02017-03-09 13:34:09 -0500470
471 template <bool E = MEM_MOVE> SK_WHEN(E, void) move(int dst, int src) {
472 memcpy(&fItemArray[dst], &fItemArray[src], sizeof(T));
473 }
474 template <bool E = MEM_MOVE> SK_WHEN(E, void) move(void* dst) {
475 sk_careful_memcpy(dst, fMemArray, fCount * sizeof(T));
476 }
477
478 template <bool E = MEM_MOVE> SK_WHEN(!E, void) move(int dst, int src) {
bungeman918090c2016-02-09 09:14:28 -0800479 new (&fItemArray[dst]) T(std::move(fItemArray[src]));
480 fItemArray[src].~T();
481 }
Florin Malitac2d5bd02017-03-09 13:34:09 -0500482 template <bool E = MEM_MOVE> SK_WHEN(!E, void) move(void* dst) {
bungeman918090c2016-02-09 09:14:28 -0800483 for (int i = 0; i < fCount; ++i) {
bungeman0d9e9be2016-04-20 10:22:20 -0700484 new (static_cast<char*>(dst) + sizeof(T) * i) T(std::move(fItemArray[i]));
bungeman918090c2016-02-09 09:14:28 -0800485 fItemArray[i].~T();
486 }
487 }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000488
Brian Salomon69225d02017-03-15 20:52:35 -0400489 static constexpr int kMinHeapAllocCount = 8;
Mike Reed6c14c8d2017-03-09 16:36:26 -0500490
bsalomon@google.comd5104142013-06-13 15:13:46 +0000491 // Helper function that makes space for n objects, adjusts the count, but does not initialize
492 // the new objects.
493 void* push_back_raw(int n) {
494 this->checkRealloc(n);
495 void* ptr = fItemArray + fCount;
496 fCount += n;
497 return ptr;
498 }
499
Brian Salomon69225d02017-03-15 20:52:35 -0400500 void checkRealloc(int delta) {
bsalomon@google.com49313f62011-09-14 13:54:05 +0000501 SkASSERT(fCount >= 0);
502 SkASSERT(fAllocCount >= 0);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000503 SkASSERT(-delta <= fCount);
504
505 int newCount = fCount + delta;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000506
Brian Salomon69225d02017-03-15 20:52:35 -0400507 // We allow fAllocCount to be in the range [newCount, 3*newCount]. We also never shrink
508 // when we're currently using preallocated memory or would allocate less than
509 // kMinHeapAllocCount.
510 bool mustGrow = newCount > fAllocCount;
511 bool shouldShrink = fAllocCount > 3 * newCount && fOwnMemory;
512 if (!mustGrow && !shouldShrink) {
513 return;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000514 }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000515
Brian Salomon69225d02017-03-15 20:52:35 -0400516 // Whether we're growing or shrinking, we leave at least 50% extra space for future growth.
517 int newAllocCount = newCount + ((newCount + 1) >> 1);
518 // Align the new allocation count to kMinHeapAllocCount.
519 static_assert(SkIsPow2(kMinHeapAllocCount), "min alloc count not power of two.");
520 newAllocCount = (newAllocCount + (kMinHeapAllocCount - 1)) & ~(kMinHeapAllocCount - 1);
521 // At small sizes the old and new alloc count can both be kMinHeapAllocCount.
522 if (newAllocCount == fAllocCount) {
523 return;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000524 }
Brian Salomon69225d02017-03-15 20:52:35 -0400525 fAllocCount = newAllocCount;
526 void* newMemArray = sk_malloc_throw(fAllocCount * sizeof(T));
527 this->move(newMemArray);
528 if (fOwnMemory) {
529 sk_free(fMemArray);
530
531 }
532 fMemArray = newMemArray;
533 fOwnMemory = true;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000534 }
535
Brian Salomon69225d02017-03-15 20:52:35 -0400536 int fCount;
537 int fAllocCount;
538 bool fOwnMemory;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000539 union {
540 T* fItemArray;
541 void* fMemArray;
542 };
543};
544
Brian Salomon69225d02017-03-15 20:52:35 -0400545template<typename T, bool MEM_MOVE> constexpr int SkTArray<T, MEM_MOVE>::kMinHeapAllocCount;
546
bsalomon@google.com92669012011-09-27 19:10:05 +0000547/**
548 * Subclass of SkTArray that contains a preallocated memory block for the array.
549 */
Florin Malitac2d5bd02017-03-09 13:34:09 -0500550template <int N, typename T, bool MEM_MOVE= false>
551class SkSTArray : public SkTArray<T, MEM_MOVE> {
bsalomon@google.com92669012011-09-27 19:10:05 +0000552private:
Florin Malitac2d5bd02017-03-09 13:34:09 -0500553 typedef SkTArray<T, MEM_MOVE> INHERITED;
bsalomon@google.com92669012011-09-27 19:10:05 +0000554
555public:
556 SkSTArray() : INHERITED(&fStorage) {
557 }
558
559 SkSTArray(const SkSTArray& array)
560 : INHERITED(array, &fStorage) {
561 }
562
Florin Malitab1d800d2017-03-09 12:17:15 -0500563 SkSTArray(SkSTArray&& array)
564 : INHERITED(std::move(array), &fStorage) {
565 }
566
bsalomon@google.com92669012011-09-27 19:10:05 +0000567 explicit SkSTArray(const INHERITED& array)
568 : INHERITED(array, &fStorage) {
569 }
570
Florin Malitab1d800d2017-03-09 12:17:15 -0500571 explicit SkSTArray(INHERITED&& array)
572 : INHERITED(std::move(array), &fStorage) {
573 }
574
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000575 explicit SkSTArray(int reserveCount)
576 : INHERITED(reserveCount) {
577 }
578
bsalomon@google.com92669012011-09-27 19:10:05 +0000579 SkSTArray(const T* array, int count)
580 : INHERITED(array, count, &fStorage) {
581 }
582
Brian Salomon69225d02017-03-15 20:52:35 -0400583 SkSTArray& operator=(const SkSTArray& array) {
Florin Malitad54639f2017-03-12 10:40:13 -0400584 INHERITED::operator=(array);
585 return *this;
586 }
587
Brian Salomon69225d02017-03-15 20:52:35 -0400588 SkSTArray& operator=(SkSTArray&& array) {
Florin Malitad54639f2017-03-12 10:40:13 -0400589 INHERITED::operator=(std::move(array));
590 return *this;
bsalomon@google.com92669012011-09-27 19:10:05 +0000591 }
592
Brian Salomon69225d02017-03-15 20:52:35 -0400593 SkSTArray& operator=(const INHERITED& array) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000594 INHERITED::operator=(array);
595 return *this;
596 }
597
Brian Salomon69225d02017-03-15 20:52:35 -0400598 SkSTArray& operator=(INHERITED&& array) {
Florin Malitad54639f2017-03-12 10:40:13 -0400599 INHERITED::operator=(std::move(array));
600 return *this;
601 }
602
bsalomon@google.com92669012011-09-27 19:10:05 +0000603private:
604 SkAlignedSTStorage<N,T> fStorage;
605};
606
bsalomon@google.com49313f62011-09-14 13:54:05 +0000607#endif