blob: 155f79a90ff96fa8beb0af4abacc4458116e95af [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 Kleind9187c02018-09-07 17:32:47 +000011#include "../private/SkSafe32.h"
12#include "../private/SkTLogic.h"
13#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 }
Hal Canaryc0a74a12018-10-10 10:01:19 -040066 this->forEach([](T& o) { o.~T(); });
bsalomon@google.com49313f62011-09-14 13:54:05 +000067 fCount = 0;
bungeman0d9e9be2016-04-20 10:22:20 -070068 this->checkRealloc(that.count());
69 fCount = that.count();
70 this->copy(that.fItemArray);
71 return *this;
72 }
Brian Salomon69225d02017-03-15 20:52:35 -040073 SkTArray& operator=(SkTArray&& that) {
Greg Daniel70131b92017-03-22 13:33:21 -040074 if (this == &that) {
75 return *this;
76 }
Hal Canaryc0a74a12018-10-10 10:01:19 -040077 this->forEach([](T& o) { o.~T(); });
bungeman0d9e9be2016-04-20 10:22:20 -070078 fCount = 0;
79 this->checkRealloc(that.count());
80 fCount = that.count();
81 that.move(fMemArray);
82 that.fCount = 0;
bsalomon@google.com49313f62011-09-14 13:54:05 +000083 return *this;
84 }
85
bsalomon383ff102015-07-31 11:53:11 -070086 ~SkTArray() {
Hal Canaryc0a74a12018-10-10 10:01:19 -040087 this->forEach([](T& o) { o.~T(); });
Brian Salomon69225d02017-03-15 20:52:35 -040088 if (fOwnMemory) {
bsalomon@google.com49313f62011-09-14 13:54:05 +000089 sk_free(fMemArray);
90 }
91 }
92
93 /**
Brian Salomon610842a2017-06-16 06:47:30 -040094 * Resets to count() == 0 and resets any reserve count.
bsalomon@google.com49313f62011-09-14 13:54:05 +000095 */
Brian Salomon610842a2017-06-16 06:47:30 -040096 void reset() {
97 this->pop_back_n(fCount);
98 fReserved = false;
99 }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000100
101 /**
Brian Salomon610842a2017-06-16 06:47:30 -0400102 * Resets to count() = n newly constructed T objects and resets any reserve count.
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000103 */
104 void reset(int n) {
105 SkASSERT(n >= 0);
Hal Canaryc0a74a12018-10-10 10:01:19 -0400106 this->forEach([](T& o) { o.~T(); });
bungeman0d9e9be2016-04-20 10:22:20 -0700107 // Set fCount to 0 before calling checkRealloc so that no elements are moved.
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000108 fCount = 0;
109 this->checkRealloc(n);
110 fCount = n;
Hal Canaryc0a74a12018-10-10 10:01:19 -0400111 this->forEach([](T& o) { new (&o) T; });
Brian Salomon610842a2017-06-16 06:47:30 -0400112 fReserved = false;
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000113 }
114
115 /**
Brian Salomon610842a2017-06-16 06:47:30 -0400116 * Resets to a copy of a C array and resets any reserve count.
jvanverth@google.com054ae992013-04-01 20:06:51 +0000117 */
118 void reset(const T* array, int count) {
Hal Canaryc0a74a12018-10-10 10:01:19 -0400119 this->forEach([](T& o) { o.~T(); });
bungeman0d9e9be2016-04-20 10:22:20 -0700120 fCount = 0;
121 this->checkRealloc(count);
jvanverth@google.com054ae992013-04-01 20:06:51 +0000122 fCount = count;
bungeman918090c2016-02-09 09:14:28 -0800123 this->copy(array);
Brian Salomon610842a2017-06-16 06:47:30 -0400124 fReserved = false;
125 }
126
127 /**
128 * Ensures there is enough reserved space for n additional elements. The is guaranteed at least
129 * until the array size grows above n and subsequently shrinks below n, any version of reset()
130 * is called, or reserve() is called again.
131 */
132 void reserve(int n) {
133 SkASSERT(n >= 0);
134 if (n > 0) {
135 this->checkRealloc(n);
136 fReserved = fOwnMemory;
137 } else {
138 fReserved = false;
139 }
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 */
Hal Canaryc0a74a12018-10-10 10:01:19 -0400167 T& push_back() { return *new (this->push_back_raw(1)) T; }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000168
169 /**
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000170 * Version of above that uses a copy constructor to initialize the new item
171 */
Hal Canaryc0a74a12018-10-10 10:01:19 -0400172 T& push_back(const T& t) { return *new (this->push_back_raw(1)) T(t); }
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000173
174 /**
halcanary91fcb3e2016-03-04 13:53:22 -0800175 * Version of above that uses a move constructor to initialize the new item
176 */
Hal Canaryc0a74a12018-10-10 10:01:19 -0400177 T& push_back(T&& t) { return *new (this->push_back_raw(1)) T(std::move(t)); }
halcanary91fcb3e2016-03-04 13:53:22 -0800178
179 /**
halcanaryf12a1672015-09-23 12:45:49 -0700180 * Construct a new T at the back of this array.
181 */
182 template<class... Args> T& emplace_back(Args&&... args) {
Hal Canaryc0a74a12018-10-10 10:01:19 -0400183 return *new (this->push_back_raw(1)) T(std::forward<Args>(args)...);
halcanaryf12a1672015-09-23 12:45:49 -0700184 }
185
186 /**
bungeman@google.comd58a8562014-03-24 15:55:01 +0000187 * Allocates n more default-initialized T values, and returns the address of
188 * the start of that new range. Note: this address is only valid until the
189 * next API call made on the array that might add or remove elements.
bsalomon@google.com49313f62011-09-14 13:54:05 +0000190 */
191 T* push_back_n(int n) {
192 SkASSERT(n >= 0);
Hal Canaryc0a74a12018-10-10 10:01:19 -0400193 T* newTs = this->push_back_raw(n);
194 ForEach(newTs, newTs + n, [](T& o) { new (&o) T; });
195 return newTs;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000196 }
197
198 /**
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000199 * Version of above that uses a copy constructor to initialize all n items
200 * to the same T.
201 */
202 T* push_back_n(int n, const T& t) {
203 SkASSERT(n >= 0);
Hal Canaryc0a74a12018-10-10 10:01:19 -0400204 T* newTs = this->push_back_raw(n);
205 ForEach(newTs, newTs + n, [&t](T& o) { new (&o) T(t); });
206 return newTs;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000207 }
208
209 /**
210 * Version of above that uses a copy constructor to initialize the n items
211 * to separate T values.
212 */
Hal Canaryc0a74a12018-10-10 10:01:19 -0400213 T* push_back_n(int n, const T* ts) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000214 SkASSERT(n >= 0);
Hal Canaryc0a74a12018-10-10 10:01:19 -0400215 T* newTs = this->push_back_raw(n);
216 ForEach(newTs, newTs + n, [&ts](T& o) { new (&o) T(*ts++); });
217 return newTs;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000218 }
219
220 /**
msarett10e3d9b2016-08-18 15:46:03 -0700221 * Version of above that uses the move constructor to set n items.
222 */
Hal Canaryc0a74a12018-10-10 10:01:19 -0400223 T* move_back_n(int n, T* ts) {
msarett10e3d9b2016-08-18 15:46:03 -0700224 SkASSERT(n >= 0);
Hal Canaryc0a74a12018-10-10 10:01:19 -0400225 T* newTs = this->push_back_raw(n);
226 ForEach(newTs, newTs + n, [&ts](T& o) { new (&o) T(std::move(*ts++)); });
227 return newTs;
msarett10e3d9b2016-08-18 15:46:03 -0700228 }
229
230 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000231 * Removes the last element. Not safe to call when count() == 0.
232 */
233 void pop_back() {
234 SkASSERT(fCount > 0);
235 --fCount;
236 fItemArray[fCount].~T();
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000237 this->checkRealloc(0);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000238 }
239
240 /**
241 * Removes the last n elements. Not safe to call when count() < n.
242 */
243 void pop_back_n(int n) {
244 SkASSERT(n >= 0);
245 SkASSERT(fCount >= n);
246 fCount -= n;
Hal Canaryc0a74a12018-10-10 10:01:19 -0400247 ForEach(this->end(), this->end() + n, [](T& o) { o.~T(); });
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000248 this->checkRealloc(0);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000249 }
250
251 /**
252 * Pushes or pops from the back to resize. Pushes will be default
253 * initialized.
254 */
255 void resize_back(int newCount) {
256 SkASSERT(newCount >= 0);
257
258 if (newCount > fCount) {
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000259 this->push_back_n(newCount - fCount);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000260 } else if (newCount < fCount) {
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000261 this->pop_back_n(fCount - newCount);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000262 }
263 }
264
bsalomon23e619c2015-02-06 11:54:28 -0800265 /** Swaps the contents of this array with that array. Does a pointer swap if possible,
266 otherwise copies the T values. */
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400267 void swap(SkTArray& that) {
268 using std::swap;
269 if (this == &that) {
bsalomon3632f842015-02-10 19:46:58 -0800270 return;
271 }
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400272 if (fOwnMemory && that.fOwnMemory) {
273 swap(fItemArray, that.fItemArray);
274 swap(fCount, that.fCount);
275 swap(fAllocCount, that.fAllocCount);
bsalomon23e619c2015-02-06 11:54:28 -0800276 } else {
277 // This could be more optimal...
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400278 SkTArray copy(std::move(that));
279 that = std::move(*this);
bungeman0d9e9be2016-04-20 10:22:20 -0700280 *this = std::move(copy);
bsalomon23e619c2015-02-06 11:54:28 -0800281 }
282 }
283
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000284 T* begin() {
285 return fItemArray;
286 }
287 const T* begin() const {
288 return fItemArray;
289 }
290 T* end() {
Ben Wagnera93a14a2017-08-28 10:34:05 -0400291 return fItemArray ? fItemArray + fCount : nullptr;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000292 }
293 const T* end() const {
Ben Wagnera93a14a2017-08-28 10:34:05 -0400294 return fItemArray ? fItemArray + fCount : nullptr;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000295 }
296
297 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000298 * Get the i^th element.
299 */
300 T& operator[] (int i) {
301 SkASSERT(i < fCount);
302 SkASSERT(i >= 0);
303 return fItemArray[i];
304 }
305
306 const T& operator[] (int i) const {
307 SkASSERT(i < fCount);
308 SkASSERT(i >= 0);
309 return fItemArray[i];
310 }
311
312 /**
313 * equivalent to operator[](0)
314 */
315 T& front() { SkASSERT(fCount > 0); return fItemArray[0];}
316
317 const T& front() const { SkASSERT(fCount > 0); return fItemArray[0];}
318
319 /**
320 * equivalent to operator[](count() - 1)
321 */
322 T& back() { SkASSERT(fCount); return fItemArray[fCount - 1];}
323
324 const T& back() const { SkASSERT(fCount > 0); return fItemArray[fCount - 1];}
325
326 /**
327 * equivalent to operator[](count()-1-i)
328 */
329 T& fromBack(int i) {
330 SkASSERT(i >= 0);
331 SkASSERT(i < fCount);
332 return fItemArray[fCount - i - 1];
333 }
334
335 const T& fromBack(int i) const {
336 SkASSERT(i >= 0);
337 SkASSERT(i < fCount);
338 return fItemArray[fCount - i - 1];
339 }
340
Florin Malitac2d5bd02017-03-09 13:34:09 -0500341 bool operator==(const SkTArray<T, MEM_MOVE>& right) const {
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000342 int leftCount = this->count();
343 if (leftCount != right.count()) {
344 return false;
345 }
346 for (int index = 0; index < leftCount; ++index) {
347 if (fItemArray[index] != right.fItemArray[index]) {
348 return false;
349 }
350 }
351 return true;
352 }
353
Florin Malitac2d5bd02017-03-09 13:34:09 -0500354 bool operator!=(const SkTArray<T, MEM_MOVE>& right) const {
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000355 return !(*this == right);
356 }
357
Brian Salomon69225d02017-03-15 20:52:35 -0400358 inline int allocCntForTest() const;
359
bsalomon@google.com92669012011-09-27 19:10:05 +0000360protected:
361 /**
362 * Creates an empty array that will use the passed storage block until it
363 * is insufficiently large to hold the entire array.
364 */
365 template <int N>
366 SkTArray(SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400367 this->initWithPreallocatedStorage(0, storage->get(), N);
bsalomon@google.com92669012011-09-27 19:10:05 +0000368 }
369
370 /**
371 * Copy another array, using preallocated storage if preAllocCount >=
372 * array.count(). Otherwise storage will only be used when array shrinks
373 * to fit.
374 */
375 template <int N>
376 SkTArray(const SkTArray& array, SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400377 this->initWithPreallocatedStorage(array.fCount, storage->get(), N);
bungeman0d9e9be2016-04-20 10:22:20 -0700378 this->copy(array.fItemArray);
bsalomon@google.com92669012011-09-27 19:10:05 +0000379 }
380
381 /**
Florin Malitab1d800d2017-03-09 12:17:15 -0500382 * Move another array, using preallocated storage if preAllocCount >=
383 * array.count(). Otherwise storage will only be used when array shrinks
384 * to fit.
385 */
386 template <int N>
387 SkTArray(SkTArray&& array, SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400388 this->initWithPreallocatedStorage(array.fCount, storage->get(), N);
Florin Malitab1d800d2017-03-09 12:17:15 -0500389 array.move(fMemArray);
390 array.fCount = 0;
391 }
392
393 /**
bsalomon@google.com92669012011-09-27 19:10:05 +0000394 * Copy a C array, using preallocated storage if preAllocCount >=
395 * count. Otherwise storage will only be used when array shrinks
396 * to fit.
397 */
398 template <int N>
399 SkTArray(const T* array, int count, SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400400 this->initWithPreallocatedStorage(count, storage->get(), N);
bungeman0d9e9be2016-04-20 10:22:20 -0700401 this->copy(array);
bsalomon@google.com92669012011-09-27 19:10:05 +0000402 }
403
Brian Salomon69225d02017-03-15 20:52:35 -0400404private:
405 void init(int count = 0, int reserveCount = 0) {
junov@chromium.orgd80a5092011-11-30 18:35:19 +0000406 SkASSERT(count >= 0);
Brian Salomon69225d02017-03-15 20:52:35 -0400407 SkASSERT(reserveCount >= 0);
408 fCount = count;
409 if (!count && !reserveCount) {
410 fAllocCount = 0;
411 fMemArray = nullptr;
Jim Van Verth474d6872017-12-14 13:00:05 -0500412 fOwnMemory = true;
Brian Salomon610842a2017-06-16 06:47:30 -0400413 fReserved = false;
bsalomon@google.com92669012011-09-27 19:10:05 +0000414 } else {
Brian Salomon69225d02017-03-15 20:52:35 -0400415 fAllocCount = SkTMax(count, SkTMax(kMinHeapAllocCount, reserveCount));
Mike Reed8dc8dbc2018-01-05 11:20:10 -0500416 fMemArray = sk_malloc_throw(fAllocCount, sizeof(T));
Brian Salomon69225d02017-03-15 20:52:35 -0400417 fOwnMemory = true;
Brian Salomon610842a2017-06-16 06:47:30 -0400418 fReserved = reserveCount > 0;
bsalomon@google.com92669012011-09-27 19:10:05 +0000419 }
bsalomon@google.com92669012011-09-27 19:10:05 +0000420 }
421
Brian Salomon69225d02017-03-15 20:52:35 -0400422 void initWithPreallocatedStorage(int count, void* preallocStorage, int preallocCount) {
423 SkASSERT(count >= 0);
424 SkASSERT(preallocCount > 0);
425 SkASSERT(preallocStorage);
426 fCount = count;
427 fMemArray = nullptr;
Brian Salomon610842a2017-06-16 06:47:30 -0400428 fReserved = false;
Brian Salomon69225d02017-03-15 20:52:35 -0400429 if (count > preallocCount) {
430 fAllocCount = SkTMax(count, kMinHeapAllocCount);
Mike Reed8dc8dbc2018-01-05 11:20:10 -0500431 fMemArray = sk_malloc_throw(fAllocCount, sizeof(T));
Brian Salomon69225d02017-03-15 20:52:35 -0400432 fOwnMemory = true;
433 } else {
434 fAllocCount = preallocCount;
435 fMemArray = preallocStorage;
436 fOwnMemory = false;
437 }
438 }
439
bungeman918090c2016-02-09 09:14:28 -0800440 /** In the following move and copy methods, 'dst' is assumed to be uninitialized raw storage.
441 * In the following move methods, 'src' is destroyed leaving behind uninitialized raw storage.
442 */
Florin Malitac2d5bd02017-03-09 13:34:09 -0500443 void copy(const T* src) {
444 // Some types may be trivially copyable, in which case we *could* use memcopy; but
445 // MEM_MOVE == true implies that the type is trivially movable, and not necessarily
446 // trivially copyable (think sk_sp<>). So short of adding another template arg, we
447 // must be conservative and use copy construction.
Hal Canaryc0a74a12018-10-10 10:01:19 -0400448 this->forEach([&src](T& o) { new (&o) T(*src++); });
bungeman918090c2016-02-09 09:14:28 -0800449 }
Florin Malitac2d5bd02017-03-09 13:34:09 -0500450
451 template <bool E = MEM_MOVE> SK_WHEN(E, void) move(int dst, int src) {
452 memcpy(&fItemArray[dst], &fItemArray[src], sizeof(T));
453 }
454 template <bool E = MEM_MOVE> SK_WHEN(E, void) move(void* dst) {
455 sk_careful_memcpy(dst, fMemArray, fCount * sizeof(T));
456 }
457
458 template <bool E = MEM_MOVE> SK_WHEN(!E, void) move(int dst, int src) {
bungeman918090c2016-02-09 09:14:28 -0800459 new (&fItemArray[dst]) T(std::move(fItemArray[src]));
460 fItemArray[src].~T();
461 }
Florin Malitac2d5bd02017-03-09 13:34:09 -0500462 template <bool E = MEM_MOVE> SK_WHEN(!E, void) move(void* dst) {
Hal Canaryc0a74a12018-10-10 10:01:19 -0400463 T* tDst = static_cast<T*>(dst);
464 this->forEach([&tDst](T& o) {
465 new (tDst++) T(std::move(o));
466 o.~T();
467 } );
bungeman918090c2016-02-09 09:14:28 -0800468 }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000469
Brian Salomon69225d02017-03-15 20:52:35 -0400470 static constexpr int kMinHeapAllocCount = 8;
Mike Reed6c14c8d2017-03-09 16:36:26 -0500471
bsalomon@google.comd5104142013-06-13 15:13:46 +0000472 // Helper function that makes space for n objects, adjusts the count, but does not initialize
473 // the new objects.
Hal Canaryc0a74a12018-10-10 10:01:19 -0400474 T* push_back_raw(int n) {
bsalomon@google.comd5104142013-06-13 15:13:46 +0000475 this->checkRealloc(n);
Hal Canaryc0a74a12018-10-10 10:01:19 -0400476 T* ptr = fItemArray + fCount;
bsalomon@google.comd5104142013-06-13 15:13:46 +0000477 fCount += n;
478 return ptr;
479 }
480
Brian Salomon69225d02017-03-15 20:52:35 -0400481 void checkRealloc(int delta) {
bsalomon@google.com49313f62011-09-14 13:54:05 +0000482 SkASSERT(fCount >= 0);
483 SkASSERT(fAllocCount >= 0);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000484 SkASSERT(-delta <= fCount);
485
Mike Reedcab25492018-05-10 10:55:43 -0400486 // Move into 64bit math temporarily, to avoid local overflows
487 int64_t newCount = fCount + delta;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000488
Brian Salomon69225d02017-03-15 20:52:35 -0400489 // We allow fAllocCount to be in the range [newCount, 3*newCount]. We also never shrink
Brian Salomon610842a2017-06-16 06:47:30 -0400490 // when we're currently using preallocated memory, would allocate less than
491 // kMinHeapAllocCount, or a reserve count was specified that has yet to be exceeded.
Brian Salomon69225d02017-03-15 20:52:35 -0400492 bool mustGrow = newCount > fAllocCount;
Brian Salomon610842a2017-06-16 06:47:30 -0400493 bool shouldShrink = fAllocCount > 3 * newCount && fOwnMemory && !fReserved;
Brian Salomon69225d02017-03-15 20:52:35 -0400494 if (!mustGrow && !shouldShrink) {
495 return;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000496 }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000497
Mike Reedcab25492018-05-10 10:55:43 -0400498
Brian Salomon69225d02017-03-15 20:52:35 -0400499 // Whether we're growing or shrinking, we leave at least 50% extra space for future growth.
Mike Reedcab25492018-05-10 10:55:43 -0400500 int64_t newAllocCount = newCount + ((newCount + 1) >> 1);
Brian Salomon69225d02017-03-15 20:52:35 -0400501 // Align the new allocation count to kMinHeapAllocCount.
502 static_assert(SkIsPow2(kMinHeapAllocCount), "min alloc count not power of two.");
503 newAllocCount = (newAllocCount + (kMinHeapAllocCount - 1)) & ~(kMinHeapAllocCount - 1);
504 // At small sizes the old and new alloc count can both be kMinHeapAllocCount.
505 if (newAllocCount == fAllocCount) {
506 return;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000507 }
Mike Reedcab25492018-05-10 10:55:43 -0400508
509 fAllocCount = Sk64_pin_to_s32(newAllocCount);
510 SkASSERT(fAllocCount >= newCount);
Mike Reed8dc8dbc2018-01-05 11:20:10 -0500511 void* newMemArray = sk_malloc_throw(fAllocCount, sizeof(T));
Brian Salomon69225d02017-03-15 20:52:35 -0400512 this->move(newMemArray);
513 if (fOwnMemory) {
514 sk_free(fMemArray);
515
516 }
517 fMemArray = newMemArray;
518 fOwnMemory = true;
Brian Salomon610842a2017-06-16 06:47:30 -0400519 fReserved = false;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000520 }
521
bsalomon@google.com49313f62011-09-14 13:54:05 +0000522 union {
523 T* fItemArray;
524 void* fMemArray;
525 };
Brian Salomon610842a2017-06-16 06:47:30 -0400526 int fCount;
527 int fAllocCount;
528 bool fOwnMemory : 1;
529 bool fReserved : 1;
Hal Canaryc0a74a12018-10-10 10:01:19 -0400530
531 template <typename Fn>
532 static void ForEach(T* begin, T* end, Fn&& f) { while (begin != end) { f(*begin++); } }
533
534 template <typename Fn>
535 void forEach(Fn&& f) { ForEach(this->begin(), this->end(), std::move(f)); }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000536};
537
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400538template <typename T, bool M> static inline void swap(SkTArray<T, M>& a, SkTArray<T, M>& b) {
539 a.swap(b);
540}
541
Brian Salomon69225d02017-03-15 20:52:35 -0400542template<typename T, bool MEM_MOVE> constexpr int SkTArray<T, MEM_MOVE>::kMinHeapAllocCount;
543
bsalomon@google.com92669012011-09-27 19:10:05 +0000544/**
545 * Subclass of SkTArray that contains a preallocated memory block for the array.
546 */
Florin Malitac2d5bd02017-03-09 13:34:09 -0500547template <int N, typename T, bool MEM_MOVE= false>
548class SkSTArray : public SkTArray<T, MEM_MOVE> {
bsalomon@google.com92669012011-09-27 19:10:05 +0000549private:
Florin Malitac2d5bd02017-03-09 13:34:09 -0500550 typedef SkTArray<T, MEM_MOVE> INHERITED;
bsalomon@google.com92669012011-09-27 19:10:05 +0000551
552public:
553 SkSTArray() : INHERITED(&fStorage) {
554 }
555
556 SkSTArray(const SkSTArray& array)
557 : INHERITED(array, &fStorage) {
558 }
559
Florin Malitab1d800d2017-03-09 12:17:15 -0500560 SkSTArray(SkSTArray&& array)
561 : INHERITED(std::move(array), &fStorage) {
562 }
563
bsalomon@google.com92669012011-09-27 19:10:05 +0000564 explicit SkSTArray(const INHERITED& array)
565 : INHERITED(array, &fStorage) {
566 }
567
Florin Malitab1d800d2017-03-09 12:17:15 -0500568 explicit SkSTArray(INHERITED&& array)
569 : INHERITED(std::move(array), &fStorage) {
570 }
571
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000572 explicit SkSTArray(int reserveCount)
573 : INHERITED(reserveCount) {
574 }
575
bsalomon@google.com92669012011-09-27 19:10:05 +0000576 SkSTArray(const T* array, int count)
577 : INHERITED(array, count, &fStorage) {
578 }
579
Brian Salomon69225d02017-03-15 20:52:35 -0400580 SkSTArray& operator=(const SkSTArray& array) {
Florin Malitad54639f2017-03-12 10:40:13 -0400581 INHERITED::operator=(array);
582 return *this;
583 }
584
Brian Salomon69225d02017-03-15 20:52:35 -0400585 SkSTArray& operator=(SkSTArray&& array) {
Florin Malitad54639f2017-03-12 10:40:13 -0400586 INHERITED::operator=(std::move(array));
587 return *this;
bsalomon@google.com92669012011-09-27 19:10:05 +0000588 }
589
Brian Salomon69225d02017-03-15 20:52:35 -0400590 SkSTArray& operator=(const INHERITED& array) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000591 INHERITED::operator=(array);
592 return *this;
593 }
594
Brian Salomon69225d02017-03-15 20:52:35 -0400595 SkSTArray& operator=(INHERITED&& array) {
Florin Malitad54639f2017-03-12 10:40:13 -0400596 INHERITED::operator=(std::move(array));
597 return *this;
598 }
599
bsalomon@google.com92669012011-09-27 19:10:05 +0000600private:
601 SkAlignedSTStorage<N,T> fStorage;
602};
603
bsalomon@google.com49313f62011-09-14 13:54:05 +0000604#endif