blob: c9bee99c6d985fd4da47276112137796cffdb8ec [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
Mike Reedcab25492018-05-10 10:55:43 -040011#include "../private/SkSafe32.h"
bungeman918090c2016-02-09 09:14:28 -080012#include "../private/SkTLogic.h"
bungemanf3c15b72015-08-19 11:56:48 -070013#include "../private/SkTemplates.h"
bsalomon@google.com49313f62011-09-14 13:54:05 +000014#include "SkTypes.h"
bungemanf3c15b72015-08-19 11:56:48 -070015
16#include <new>
bungeman221524d2016-01-05 14:59:40 -080017#include <utility>
bsalomon@google.com49313f62011-09-14 13:54:05 +000018
Florin Malitac2d5bd02017-03-09 13:34:09 -050019/** When MEM_MOVE is true T will be bit copied when moved.
20 When MEM_MOVE is false, T will be copy constructed / destructed.
bungeman@google.comd58a8562014-03-24 15:55:01 +000021 In all cases T will be default-initialized on allocation,
bungeman@google.coma12cc7f2011-10-07 20:54:15 +000022 and its destructor will be called from this object's destructor.
23*/
Florin Malitac2d5bd02017-03-09 13:34:09 -050024template <typename T, bool MEM_MOVE = false> class SkTArray {
bsalomon@google.com49313f62011-09-14 13:54:05 +000025public:
26 /**
27 * Creates an empty array with no initial storage
28 */
Brian Salomon69225d02017-03-15 20:52:35 -040029 SkTArray() { this->init(); }
bsalomon@google.com49313f62011-09-14 13:54:05 +000030
31 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000032 * Creates an empty array that will preallocate space for reserveCount
bsalomon@google.com49313f62011-09-14 13:54:05 +000033 * elements.
34 */
Brian Salomon69225d02017-03-15 20:52:35 -040035 explicit SkTArray(int reserveCount) { this->init(0, reserveCount); }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000036
bsalomon@google.com49313f62011-09-14 13:54:05 +000037 /**
38 * Copies one array to another. The new array will be heap allocated.
39 */
bungeman0d9e9be2016-04-20 10:22:20 -070040 explicit SkTArray(const SkTArray& that) {
Brian Salomon69225d02017-03-15 20:52:35 -040041 this->init(that.fCount);
bungeman0d9e9be2016-04-20 10:22:20 -070042 this->copy(that.fItemArray);
43 }
Brian Salomon69225d02017-03-15 20:52:35 -040044
bungeman0d9e9be2016-04-20 10:22:20 -070045 explicit SkTArray(SkTArray&& that) {
Brian Salomon69225d02017-03-15 20:52:35 -040046 // TODO: If 'that' owns its memory why don't we just steal the pointer?
47 this->init(that.fCount);
bungeman0d9e9be2016-04-20 10:22:20 -070048 that.move(fMemArray);
49 that.fCount = 0;
bsalomon@google.com49313f62011-09-14 13:54:05 +000050 }
51
52 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000053 * Creates a SkTArray by copying contents of a standard C array. The new
bsalomon@google.com49313f62011-09-14 13:54:05 +000054 * array will be heap allocated. Be careful not to use this constructor
55 * when you really want the (void*, int) version.
56 */
57 SkTArray(const T* array, int count) {
Brian Salomon69225d02017-03-15 20:52:35 -040058 this->init(count);
bungeman0d9e9be2016-04-20 10:22:20 -070059 this->copy(array);
bsalomon@google.com49313f62011-09-14 13:54:05 +000060 }
61
Brian Salomon69225d02017-03-15 20:52:35 -040062 SkTArray& operator=(const SkTArray& that) {
Greg Daniel70131b92017-03-22 13:33:21 -040063 if (this == &that) {
64 return *this;
65 }
bsalomon@google.com49313f62011-09-14 13:54:05 +000066 for (int i = 0; i < fCount; ++i) {
67 fItemArray[i].~T();
68 }
69 fCount = 0;
bungeman0d9e9be2016-04-20 10:22:20 -070070 this->checkRealloc(that.count());
71 fCount = that.count();
72 this->copy(that.fItemArray);
73 return *this;
74 }
Brian Salomon69225d02017-03-15 20:52:35 -040075 SkTArray& operator=(SkTArray&& that) {
Greg Daniel70131b92017-03-22 13:33:21 -040076 if (this == &that) {
77 return *this;
78 }
bungeman0d9e9be2016-04-20 10:22:20 -070079 for (int i = 0; i < fCount; ++i) {
80 fItemArray[i].~T();
81 }
82 fCount = 0;
83 this->checkRealloc(that.count());
84 fCount = that.count();
85 that.move(fMemArray);
86 that.fCount = 0;
bsalomon@google.com49313f62011-09-14 13:54:05 +000087 return *this;
88 }
89
bsalomon383ff102015-07-31 11:53:11 -070090 ~SkTArray() {
bsalomon@google.com49313f62011-09-14 13:54:05 +000091 for (int i = 0; i < fCount; ++i) {
92 fItemArray[i].~T();
93 }
Brian Salomon69225d02017-03-15 20:52:35 -040094 if (fOwnMemory) {
bsalomon@google.com49313f62011-09-14 13:54:05 +000095 sk_free(fMemArray);
96 }
97 }
98
99 /**
Brian Salomon610842a2017-06-16 06:47:30 -0400100 * Resets to count() == 0 and resets any reserve count.
bsalomon@google.com49313f62011-09-14 13:54:05 +0000101 */
Brian Salomon610842a2017-06-16 06:47:30 -0400102 void reset() {
103 this->pop_back_n(fCount);
104 fReserved = false;
105 }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000106
107 /**
Brian Salomon610842a2017-06-16 06:47:30 -0400108 * Resets to count() = n newly constructed T objects and resets any reserve count.
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000109 */
110 void reset(int n) {
111 SkASSERT(n >= 0);
112 for (int i = 0; i < fCount; ++i) {
113 fItemArray[i].~T();
114 }
bungeman0d9e9be2016-04-20 10:22:20 -0700115 // Set fCount to 0 before calling checkRealloc so that no elements are moved.
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000116 fCount = 0;
117 this->checkRealloc(n);
118 fCount = n;
119 for (int i = 0; i < fCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -0700120 new (fItemArray + i) T;
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000121 }
Brian Salomon610842a2017-06-16 06:47:30 -0400122 fReserved = false;
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000123 }
124
125 /**
Brian Salomon610842a2017-06-16 06:47:30 -0400126 * Resets to a copy of a C array and resets any reserve count.
jvanverth@google.com054ae992013-04-01 20:06:51 +0000127 */
128 void reset(const T* array, int count) {
129 for (int i = 0; i < fCount; ++i) {
130 fItemArray[i].~T();
131 }
bungeman0d9e9be2016-04-20 10:22:20 -0700132 fCount = 0;
133 this->checkRealloc(count);
jvanverth@google.com054ae992013-04-01 20:06:51 +0000134 fCount = count;
bungeman918090c2016-02-09 09:14:28 -0800135 this->copy(array);
Brian Salomon610842a2017-06-16 06:47:30 -0400136 fReserved = false;
137 }
138
139 /**
140 * Ensures there is enough reserved space for n additional elements. The is guaranteed at least
141 * until the array size grows above n and subsequently shrinks below n, any version of reset()
142 * is called, or reserve() is called again.
143 */
144 void reserve(int n) {
145 SkASSERT(n >= 0);
146 if (n > 0) {
147 this->checkRealloc(n);
148 fReserved = fOwnMemory;
149 } else {
150 fReserved = false;
151 }
bungeman@google.com95ebd172014-03-21 19:39:02 +0000152 }
153
154 void removeShuffle(int n) {
155 SkASSERT(n < fCount);
156 int newCount = fCount - 1;
157 fCount = newCount;
158 fItemArray[n].~T();
159 if (n != newCount) {
bungeman918090c2016-02-09 09:14:28 -0800160 this->move(n, newCount);
jvanverth@google.com054ae992013-04-01 20:06:51 +0000161 }
162 }
163
164 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000165 * Number of elements in the array.
166 */
167 int count() const { return fCount; }
168
169 /**
170 * Is the array empty.
171 */
172 bool empty() const { return !fCount; }
173
174 /**
bungeman@google.comd58a8562014-03-24 15:55:01 +0000175 * Adds 1 new default-initialized T value and returns it by reference. Note
bsalomon@google.com49313f62011-09-14 13:54:05 +0000176 * the reference only remains valid until the next call that adds or removes
177 * elements.
178 */
179 T& push_back() {
krasine0c1d282016-04-21 08:34:00 -0700180 void* newT = this->push_back_raw(1);
181 return *new (newT) T;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000182 }
183
184 /**
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000185 * Version of above that uses a copy constructor to initialize the new item
186 */
187 T& push_back(const T& t) {
krasine0c1d282016-04-21 08:34:00 -0700188 void* newT = this->push_back_raw(1);
189 return *new (newT) T(t);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000190 }
191
192 /**
halcanary91fcb3e2016-03-04 13:53:22 -0800193 * Version of above that uses a move constructor to initialize the new item
194 */
195 T& push_back(T&& t) {
krasine0c1d282016-04-21 08:34:00 -0700196 void* newT = this->push_back_raw(1);
197 return *new (newT) T(std::move(t));
halcanary91fcb3e2016-03-04 13:53:22 -0800198 }
199
200 /**
halcanaryf12a1672015-09-23 12:45:49 -0700201 * Construct a new T at the back of this array.
202 */
203 template<class... Args> T& emplace_back(Args&&... args) {
krasine0c1d282016-04-21 08:34:00 -0700204 void* newT = this->push_back_raw(1);
bungeman221524d2016-01-05 14:59:40 -0800205 return *new (newT) T(std::forward<Args>(args)...);
halcanaryf12a1672015-09-23 12:45:49 -0700206 }
207
208 /**
bungeman@google.comd58a8562014-03-24 15:55:01 +0000209 * Allocates n more default-initialized T values, and returns the address of
210 * the start of that new range. Note: this address is only valid until the
211 * next API call made on the array that might add or remove elements.
bsalomon@google.com49313f62011-09-14 13:54:05 +0000212 */
213 T* push_back_n(int n) {
214 SkASSERT(n >= 0);
bungeman6a6f3c52016-04-21 10:52:03 -0700215 void* newTs = this->push_back_raw(n);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000216 for (int i = 0; i < n; ++i) {
bungeman6a6f3c52016-04-21 10:52:03 -0700217 new (static_cast<char*>(newTs) + i * sizeof(T)) T;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000218 }
bungeman6a6f3c52016-04-21 10:52:03 -0700219 return static_cast<T*>(newTs);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000220 }
221
222 /**
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000223 * Version of above that uses a copy constructor to initialize all n items
224 * to the same T.
225 */
226 T* push_back_n(int n, const T& t) {
227 SkASSERT(n >= 0);
bungeman6a6f3c52016-04-21 10:52:03 -0700228 void* newTs = this->push_back_raw(n);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000229 for (int i = 0; i < n; ++i) {
bungeman6a6f3c52016-04-21 10:52:03 -0700230 new (static_cast<char*>(newTs) + i * sizeof(T)) T(t);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000231 }
bungeman6a6f3c52016-04-21 10:52:03 -0700232 return static_cast<T*>(newTs);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000233 }
234
235 /**
236 * Version of above that uses a copy constructor to initialize the n items
237 * to separate T values.
238 */
239 T* push_back_n(int n, const T t[]) {
240 SkASSERT(n >= 0);
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000241 this->checkRealloc(n);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000242 for (int i = 0; i < n; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -0700243 new (fItemArray + fCount + i) T(t[i]);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000244 }
245 fCount += n;
246 return fItemArray + fCount - n;
247 }
248
249 /**
msarett10e3d9b2016-08-18 15:46:03 -0700250 * Version of above that uses the move constructor to set n items.
251 */
252 T* move_back_n(int n, T* t) {
253 SkASSERT(n >= 0);
254 this->checkRealloc(n);
255 for (int i = 0; i < n; ++i) {
256 new (fItemArray + fCount + i) T(std::move(t[i]));
257 }
258 fCount += n;
259 return fItemArray + fCount - n;
260 }
261
262 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000263 * Removes the last element. Not safe to call when count() == 0.
264 */
265 void pop_back() {
266 SkASSERT(fCount > 0);
267 --fCount;
268 fItemArray[fCount].~T();
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000269 this->checkRealloc(0);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000270 }
271
272 /**
273 * Removes the last n elements. Not safe to call when count() < n.
274 */
275 void pop_back_n(int n) {
276 SkASSERT(n >= 0);
277 SkASSERT(fCount >= n);
278 fCount -= n;
279 for (int i = 0; i < n; ++i) {
bsalomon@google.comd5104142013-06-13 15:13:46 +0000280 fItemArray[fCount + i].~T();
bsalomon@google.com49313f62011-09-14 13:54:05 +0000281 }
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000282 this->checkRealloc(0);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000283 }
284
285 /**
286 * Pushes or pops from the back to resize. Pushes will be default
287 * initialized.
288 */
289 void resize_back(int newCount) {
290 SkASSERT(newCount >= 0);
291
292 if (newCount > fCount) {
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000293 this->push_back_n(newCount - fCount);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000294 } else if (newCount < fCount) {
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000295 this->pop_back_n(fCount - newCount);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000296 }
297 }
298
bsalomon23e619c2015-02-06 11:54:28 -0800299 /** Swaps the contents of this array with that array. Does a pointer swap if possible,
300 otherwise copies the T values. */
301 void swap(SkTArray* that) {
bsalomon3632f842015-02-10 19:46:58 -0800302 if (this == that) {
303 return;
304 }
Brian Salomon69225d02017-03-15 20:52:35 -0400305 if (fOwnMemory && that->fOwnMemory) {
bsalomon23e619c2015-02-06 11:54:28 -0800306 SkTSwap(fItemArray, that->fItemArray);
307 SkTSwap(fCount, that->fCount);
308 SkTSwap(fAllocCount, that->fAllocCount);
309 } else {
310 // This could be more optimal...
bungeman0d9e9be2016-04-20 10:22:20 -0700311 SkTArray copy(std::move(*that));
312 *that = std::move(*this);
313 *this = std::move(copy);
bsalomon23e619c2015-02-06 11:54:28 -0800314 }
315 }
316
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000317 T* begin() {
318 return fItemArray;
319 }
320 const T* begin() const {
321 return fItemArray;
322 }
323 T* end() {
Ben Wagnera93a14a2017-08-28 10:34:05 -0400324 return fItemArray ? fItemArray + fCount : nullptr;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000325 }
326 const T* end() const {
Ben Wagnera93a14a2017-08-28 10:34:05 -0400327 return fItemArray ? fItemArray + fCount : nullptr;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000328 }
329
330 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000331 * Get the i^th element.
332 */
333 T& operator[] (int i) {
334 SkASSERT(i < fCount);
335 SkASSERT(i >= 0);
336 return fItemArray[i];
337 }
338
339 const T& operator[] (int i) const {
340 SkASSERT(i < fCount);
341 SkASSERT(i >= 0);
342 return fItemArray[i];
343 }
344
345 /**
346 * equivalent to operator[](0)
347 */
348 T& front() { SkASSERT(fCount > 0); return fItemArray[0];}
349
350 const T& front() const { SkASSERT(fCount > 0); return fItemArray[0];}
351
352 /**
353 * equivalent to operator[](count() - 1)
354 */
355 T& back() { SkASSERT(fCount); return fItemArray[fCount - 1];}
356
357 const T& back() const { SkASSERT(fCount > 0); return fItemArray[fCount - 1];}
358
359 /**
360 * equivalent to operator[](count()-1-i)
361 */
362 T& fromBack(int i) {
363 SkASSERT(i >= 0);
364 SkASSERT(i < fCount);
365 return fItemArray[fCount - i - 1];
366 }
367
368 const T& fromBack(int i) const {
369 SkASSERT(i >= 0);
370 SkASSERT(i < fCount);
371 return fItemArray[fCount - i - 1];
372 }
373
Florin Malitac2d5bd02017-03-09 13:34:09 -0500374 bool operator==(const SkTArray<T, MEM_MOVE>& right) const {
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000375 int leftCount = this->count();
376 if (leftCount != right.count()) {
377 return false;
378 }
379 for (int index = 0; index < leftCount; ++index) {
380 if (fItemArray[index] != right.fItemArray[index]) {
381 return false;
382 }
383 }
384 return true;
385 }
386
Florin Malitac2d5bd02017-03-09 13:34:09 -0500387 bool operator!=(const SkTArray<T, MEM_MOVE>& right) const {
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000388 return !(*this == right);
389 }
390
Brian Salomon69225d02017-03-15 20:52:35 -0400391 inline int allocCntForTest() const;
392
bsalomon@google.com92669012011-09-27 19:10:05 +0000393protected:
394 /**
395 * Creates an empty array that will use the passed storage block until it
396 * is insufficiently large to hold the entire array.
397 */
398 template <int N>
399 SkTArray(SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400400 this->initWithPreallocatedStorage(0, storage->get(), N);
bsalomon@google.com92669012011-09-27 19:10:05 +0000401 }
402
403 /**
404 * Copy 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(const SkTArray& array, SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400410 this->initWithPreallocatedStorage(array.fCount, storage->get(), N);
bungeman0d9e9be2016-04-20 10:22:20 -0700411 this->copy(array.fItemArray);
bsalomon@google.com92669012011-09-27 19:10:05 +0000412 }
413
414 /**
Florin Malitab1d800d2017-03-09 12:17:15 -0500415 * Move another array, using preallocated storage if preAllocCount >=
416 * array.count(). Otherwise storage will only be used when array shrinks
417 * to fit.
418 */
419 template <int N>
420 SkTArray(SkTArray&& array, SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400421 this->initWithPreallocatedStorage(array.fCount, storage->get(), N);
Florin Malitab1d800d2017-03-09 12:17:15 -0500422 array.move(fMemArray);
423 array.fCount = 0;
424 }
425
426 /**
bsalomon@google.com92669012011-09-27 19:10:05 +0000427 * Copy a C array, using preallocated storage if preAllocCount >=
428 * count. Otherwise storage will only be used when array shrinks
429 * to fit.
430 */
431 template <int N>
432 SkTArray(const T* array, int count, SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400433 this->initWithPreallocatedStorage(count, storage->get(), N);
bungeman0d9e9be2016-04-20 10:22:20 -0700434 this->copy(array);
bsalomon@google.com92669012011-09-27 19:10:05 +0000435 }
436
Brian Salomon69225d02017-03-15 20:52:35 -0400437private:
438 void init(int count = 0, int reserveCount = 0) {
junov@chromium.orgd80a5092011-11-30 18:35:19 +0000439 SkASSERT(count >= 0);
Brian Salomon69225d02017-03-15 20:52:35 -0400440 SkASSERT(reserveCount >= 0);
441 fCount = count;
442 if (!count && !reserveCount) {
443 fAllocCount = 0;
444 fMemArray = nullptr;
Jim Van Verth474d6872017-12-14 13:00:05 -0500445 fOwnMemory = true;
Brian Salomon610842a2017-06-16 06:47:30 -0400446 fReserved = false;
bsalomon@google.com92669012011-09-27 19:10:05 +0000447 } else {
Brian Salomon69225d02017-03-15 20:52:35 -0400448 fAllocCount = SkTMax(count, SkTMax(kMinHeapAllocCount, reserveCount));
Mike Reed8dc8dbc2018-01-05 11:20:10 -0500449 fMemArray = sk_malloc_throw(fAllocCount, sizeof(T));
Brian Salomon69225d02017-03-15 20:52:35 -0400450 fOwnMemory = true;
Brian Salomon610842a2017-06-16 06:47:30 -0400451 fReserved = reserveCount > 0;
bsalomon@google.com92669012011-09-27 19:10:05 +0000452 }
bsalomon@google.com92669012011-09-27 19:10:05 +0000453 }
454
Brian Salomon69225d02017-03-15 20:52:35 -0400455 void initWithPreallocatedStorage(int count, void* preallocStorage, int preallocCount) {
456 SkASSERT(count >= 0);
457 SkASSERT(preallocCount > 0);
458 SkASSERT(preallocStorage);
459 fCount = count;
460 fMemArray = nullptr;
Brian Salomon610842a2017-06-16 06:47:30 -0400461 fReserved = false;
Brian Salomon69225d02017-03-15 20:52:35 -0400462 if (count > preallocCount) {
463 fAllocCount = SkTMax(count, kMinHeapAllocCount);
Mike Reed8dc8dbc2018-01-05 11:20:10 -0500464 fMemArray = sk_malloc_throw(fAllocCount, sizeof(T));
Brian Salomon69225d02017-03-15 20:52:35 -0400465 fOwnMemory = true;
466 } else {
467 fAllocCount = preallocCount;
468 fMemArray = preallocStorage;
469 fOwnMemory = false;
470 }
471 }
472
bungeman918090c2016-02-09 09:14:28 -0800473 /** In the following move and copy methods, 'dst' is assumed to be uninitialized raw storage.
474 * In the following move methods, 'src' is destroyed leaving behind uninitialized raw storage.
475 */
Florin Malitac2d5bd02017-03-09 13:34:09 -0500476 void copy(const T* src) {
477 // Some types may be trivially copyable, in which case we *could* use memcopy; but
478 // MEM_MOVE == true implies that the type is trivially movable, and not necessarily
479 // trivially copyable (think sk_sp<>). So short of adding another template arg, we
480 // must be conservative and use copy construction.
bungeman918090c2016-02-09 09:14:28 -0800481 for (int i = 0; i < fCount; ++i) {
482 new (fItemArray + i) T(src[i]);
483 }
484 }
Florin Malitac2d5bd02017-03-09 13:34:09 -0500485
486 template <bool E = MEM_MOVE> SK_WHEN(E, void) move(int dst, int src) {
487 memcpy(&fItemArray[dst], &fItemArray[src], sizeof(T));
488 }
489 template <bool E = MEM_MOVE> SK_WHEN(E, void) move(void* dst) {
490 sk_careful_memcpy(dst, fMemArray, fCount * sizeof(T));
491 }
492
493 template <bool E = MEM_MOVE> SK_WHEN(!E, void) move(int dst, int src) {
bungeman918090c2016-02-09 09:14:28 -0800494 new (&fItemArray[dst]) T(std::move(fItemArray[src]));
495 fItemArray[src].~T();
496 }
Florin Malitac2d5bd02017-03-09 13:34:09 -0500497 template <bool E = MEM_MOVE> SK_WHEN(!E, void) move(void* dst) {
bungeman918090c2016-02-09 09:14:28 -0800498 for (int i = 0; i < fCount; ++i) {
bungeman0d9e9be2016-04-20 10:22:20 -0700499 new (static_cast<char*>(dst) + sizeof(T) * i) T(std::move(fItemArray[i]));
bungeman918090c2016-02-09 09:14:28 -0800500 fItemArray[i].~T();
501 }
502 }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000503
Brian Salomon69225d02017-03-15 20:52:35 -0400504 static constexpr int kMinHeapAllocCount = 8;
Mike Reed6c14c8d2017-03-09 16:36:26 -0500505
bsalomon@google.comd5104142013-06-13 15:13:46 +0000506 // Helper function that makes space for n objects, adjusts the count, but does not initialize
507 // the new objects.
508 void* push_back_raw(int n) {
509 this->checkRealloc(n);
510 void* ptr = fItemArray + fCount;
511 fCount += n;
512 return ptr;
513 }
514
Brian Salomon69225d02017-03-15 20:52:35 -0400515 void checkRealloc(int delta) {
bsalomon@google.com49313f62011-09-14 13:54:05 +0000516 SkASSERT(fCount >= 0);
517 SkASSERT(fAllocCount >= 0);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000518 SkASSERT(-delta <= fCount);
519
Mike Reedcab25492018-05-10 10:55:43 -0400520 // Move into 64bit math temporarily, to avoid local overflows
521 int64_t newCount = fCount + delta;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000522
Brian Salomon69225d02017-03-15 20:52:35 -0400523 // We allow fAllocCount to be in the range [newCount, 3*newCount]. We also never shrink
Brian Salomon610842a2017-06-16 06:47:30 -0400524 // when we're currently using preallocated memory, would allocate less than
525 // kMinHeapAllocCount, or a reserve count was specified that has yet to be exceeded.
Brian Salomon69225d02017-03-15 20:52:35 -0400526 bool mustGrow = newCount > fAllocCount;
Brian Salomon610842a2017-06-16 06:47:30 -0400527 bool shouldShrink = fAllocCount > 3 * newCount && fOwnMemory && !fReserved;
Brian Salomon69225d02017-03-15 20:52:35 -0400528 if (!mustGrow && !shouldShrink) {
529 return;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000530 }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000531
Mike Reedcab25492018-05-10 10:55:43 -0400532
Brian Salomon69225d02017-03-15 20:52:35 -0400533 // Whether we're growing or shrinking, we leave at least 50% extra space for future growth.
Mike Reedcab25492018-05-10 10:55:43 -0400534 int64_t newAllocCount = newCount + ((newCount + 1) >> 1);
Brian Salomon69225d02017-03-15 20:52:35 -0400535 // Align the new allocation count to kMinHeapAllocCount.
536 static_assert(SkIsPow2(kMinHeapAllocCount), "min alloc count not power of two.");
537 newAllocCount = (newAllocCount + (kMinHeapAllocCount - 1)) & ~(kMinHeapAllocCount - 1);
538 // At small sizes the old and new alloc count can both be kMinHeapAllocCount.
539 if (newAllocCount == fAllocCount) {
540 return;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000541 }
Mike Reedcab25492018-05-10 10:55:43 -0400542
543 fAllocCount = Sk64_pin_to_s32(newAllocCount);
544 SkASSERT(fAllocCount >= newCount);
Mike Reed8dc8dbc2018-01-05 11:20:10 -0500545 void* newMemArray = sk_malloc_throw(fAllocCount, sizeof(T));
Brian Salomon69225d02017-03-15 20:52:35 -0400546 this->move(newMemArray);
547 if (fOwnMemory) {
548 sk_free(fMemArray);
549
550 }
551 fMemArray = newMemArray;
552 fOwnMemory = true;
Brian Salomon610842a2017-06-16 06:47:30 -0400553 fReserved = false;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000554 }
555
bsalomon@google.com49313f62011-09-14 13:54:05 +0000556 union {
557 T* fItemArray;
558 void* fMemArray;
559 };
Brian Salomon610842a2017-06-16 06:47:30 -0400560 int fCount;
561 int fAllocCount;
562 bool fOwnMemory : 1;
563 bool fReserved : 1;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000564};
565
Brian Salomon69225d02017-03-15 20:52:35 -0400566template<typename T, bool MEM_MOVE> constexpr int SkTArray<T, MEM_MOVE>::kMinHeapAllocCount;
567
bsalomon@google.com92669012011-09-27 19:10:05 +0000568/**
569 * Subclass of SkTArray that contains a preallocated memory block for the array.
570 */
Florin Malitac2d5bd02017-03-09 13:34:09 -0500571template <int N, typename T, bool MEM_MOVE= false>
572class SkSTArray : public SkTArray<T, MEM_MOVE> {
bsalomon@google.com92669012011-09-27 19:10:05 +0000573private:
Florin Malitac2d5bd02017-03-09 13:34:09 -0500574 typedef SkTArray<T, MEM_MOVE> INHERITED;
bsalomon@google.com92669012011-09-27 19:10:05 +0000575
576public:
577 SkSTArray() : INHERITED(&fStorage) {
578 }
579
580 SkSTArray(const SkSTArray& array)
581 : INHERITED(array, &fStorage) {
582 }
583
Florin Malitab1d800d2017-03-09 12:17:15 -0500584 SkSTArray(SkSTArray&& array)
585 : INHERITED(std::move(array), &fStorage) {
586 }
587
bsalomon@google.com92669012011-09-27 19:10:05 +0000588 explicit SkSTArray(const INHERITED& array)
589 : INHERITED(array, &fStorage) {
590 }
591
Florin Malitab1d800d2017-03-09 12:17:15 -0500592 explicit SkSTArray(INHERITED&& array)
593 : INHERITED(std::move(array), &fStorage) {
594 }
595
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000596 explicit SkSTArray(int reserveCount)
597 : INHERITED(reserveCount) {
598 }
599
bsalomon@google.com92669012011-09-27 19:10:05 +0000600 SkSTArray(const T* array, int count)
601 : INHERITED(array, count, &fStorage) {
602 }
603
Brian Salomon69225d02017-03-15 20:52:35 -0400604 SkSTArray& operator=(const SkSTArray& array) {
Florin Malitad54639f2017-03-12 10:40:13 -0400605 INHERITED::operator=(array);
606 return *this;
607 }
608
Brian Salomon69225d02017-03-15 20:52:35 -0400609 SkSTArray& operator=(SkSTArray&& array) {
Florin Malitad54639f2017-03-12 10:40:13 -0400610 INHERITED::operator=(std::move(array));
611 return *this;
bsalomon@google.com92669012011-09-27 19:10:05 +0000612 }
613
Brian Salomon69225d02017-03-15 20:52:35 -0400614 SkSTArray& operator=(const INHERITED& array) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000615 INHERITED::operator=(array);
616 return *this;
617 }
618
Brian Salomon69225d02017-03-15 20:52:35 -0400619 SkSTArray& operator=(INHERITED&& array) {
Florin Malitad54639f2017-03-12 10:40:13 -0400620 INHERITED::operator=(std::move(array));
621 return *this;
622 }
623
bsalomon@google.com92669012011-09-27 19:10:05 +0000624private:
625 SkAlignedSTStorage<N,T> fStorage;
626};
627
bsalomon@google.com49313f62011-09-14 13:54:05 +0000628#endif