blob: a18d6e00d1310e3f7e1659430952bd62c00434d9 [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 */
Hal Canaryda7a5672018-10-19 10:16:01 -040040 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
Hal Canaryda7a5672018-10-19 10:16:01 -040045 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 }
Mike Kleinc1db6102018-10-10 21:01:37 +000066 for (int i = 0; i < fCount; ++i) {
67 fItemArray[i].~T();
68 }
bsalomon@google.com49313f62011-09-14 13:54:05 +000069 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 }
Mike Kleinc1db6102018-10-10 21:01:37 +000079 for (int i = 0; i < fCount; ++i) {
80 fItemArray[i].~T();
81 }
bungeman0d9e9be2016-04-20 10:22:20 -070082 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() {
Mike Kleinc1db6102018-10-10 21:01:37 +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);
Mike Kleinc1db6102018-10-10 21:01:37 +0000112 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;
Mike Kleinc1db6102018-10-10 21:01:37 +0000119 for (int i = 0; i < fCount; ++i) {
120 new (fItemArray + i) T;
121 }
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) {
Mike Kleinc1db6102018-10-10 21:01:37 +0000129 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 */
Mike Kleinc1db6102018-10-10 21:01:37 +0000179 T& push_back() {
180 void* newT = this->push_back_raw(1);
181 return *new (newT) T;
182 }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000183
184 /**
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000185 * Version of above that uses a copy constructor to initialize the new item
186 */
Mike Kleinc1db6102018-10-10 21:01:37 +0000187 T& push_back(const T& t) {
188 void* newT = this->push_back_raw(1);
189 return *new (newT) T(t);
190 }
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000191
192 /**
halcanary91fcb3e2016-03-04 13:53:22 -0800193 * Version of above that uses a move constructor to initialize the new item
194 */
Mike Kleinc1db6102018-10-10 21:01:37 +0000195 T& push_back(T&& t) {
196 void* newT = this->push_back_raw(1);
197 return *new (newT) T(std::move(t));
198 }
halcanary91fcb3e2016-03-04 13:53:22 -0800199
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) {
Mike Kleinc1db6102018-10-10 21:01:37 +0000204 void* newT = this->push_back_raw(1);
205 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);
Mike Kleinc1db6102018-10-10 21:01:37 +0000215 void* newTs = this->push_back_raw(n);
216 for (int i = 0; i < n; ++i) {
217 new (static_cast<char*>(newTs) + i * sizeof(T)) T;
218 }
219 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);
Mike Kleinc1db6102018-10-10 21:01:37 +0000228 void* newTs = this->push_back_raw(n);
229 for (int i = 0; i < n; ++i) {
230 new (static_cast<char*>(newTs) + i * sizeof(T)) T(t);
231 }
232 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 */
Mike Kleinc1db6102018-10-10 21:01:37 +0000239 T* push_back_n(int n, const T t[]) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000240 SkASSERT(n >= 0);
Mike Kleinc1db6102018-10-10 21:01:37 +0000241 this->checkRealloc(n);
242 for (int i = 0; i < n; ++i) {
243 new (fItemArray + fCount + i) T(t[i]);
244 }
245 fCount += n;
246 return fItemArray + fCount - n;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000247 }
248
249 /**
msarett10e3d9b2016-08-18 15:46:03 -0700250 * Version of above that uses the move constructor to set n items.
251 */
Mike Kleinc1db6102018-10-10 21:01:37 +0000252 T* move_back_n(int n, T* t) {
msarett10e3d9b2016-08-18 15:46:03 -0700253 SkASSERT(n >= 0);
Mike Kleinc1db6102018-10-10 21:01:37 +0000254 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;
msarett10e3d9b2016-08-18 15:46:03 -0700260 }
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;
Mike Kleinc1db6102018-10-10 21:01:37 +0000279 for (int i = 0; i < n; ++i) {
280 fItemArray[fCount + i].~T();
281 }
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. */
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400301 void swap(SkTArray& that) {
302 using std::swap;
303 if (this == &that) {
bsalomon3632f842015-02-10 19:46:58 -0800304 return;
305 }
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400306 if (fOwnMemory && that.fOwnMemory) {
307 swap(fItemArray, that.fItemArray);
308 swap(fCount, that.fCount);
309 swap(fAllocCount, that.fAllocCount);
bsalomon23e619c2015-02-06 11:54:28 -0800310 } else {
311 // This could be more optimal...
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400312 SkTArray copy(std::move(that));
313 that = std::move(*this);
bungeman0d9e9be2016-04-20 10:22:20 -0700314 *this = std::move(copy);
bsalomon23e619c2015-02-06 11:54:28 -0800315 }
316 }
317
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000318 T* begin() {
319 return fItemArray;
320 }
321 const T* begin() const {
322 return fItemArray;
323 }
324 T* end() {
Ben Wagnera93a14a2017-08-28 10:34:05 -0400325 return fItemArray ? fItemArray + fCount : nullptr;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000326 }
327 const T* end() const {
Ben Wagnera93a14a2017-08-28 10:34:05 -0400328 return fItemArray ? fItemArray + fCount : nullptr;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000329 }
Hal Canaryda7a5672018-10-19 10:16:01 -0400330 T* data() { return fItemArray; }
331 const T* data() const { return fItemArray; }
332 size_t size() const { return (size_t)fCount; }
333 void resize(size_t count) { this->resize_back((int)count); }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000334
335 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000336 * Get the i^th element.
337 */
338 T& operator[] (int i) {
339 SkASSERT(i < fCount);
340 SkASSERT(i >= 0);
341 return fItemArray[i];
342 }
343
344 const T& operator[] (int i) const {
345 SkASSERT(i < fCount);
346 SkASSERT(i >= 0);
347 return fItemArray[i];
348 }
349
350 /**
351 * equivalent to operator[](0)
352 */
353 T& front() { SkASSERT(fCount > 0); return fItemArray[0];}
354
355 const T& front() const { SkASSERT(fCount > 0); return fItemArray[0];}
356
357 /**
358 * equivalent to operator[](count() - 1)
359 */
360 T& back() { SkASSERT(fCount); return fItemArray[fCount - 1];}
361
362 const T& back() const { SkASSERT(fCount > 0); return fItemArray[fCount - 1];}
363
364 /**
365 * equivalent to operator[](count()-1-i)
366 */
367 T& fromBack(int i) {
368 SkASSERT(i >= 0);
369 SkASSERT(i < fCount);
370 return fItemArray[fCount - i - 1];
371 }
372
373 const T& fromBack(int i) const {
374 SkASSERT(i >= 0);
375 SkASSERT(i < fCount);
376 return fItemArray[fCount - i - 1];
377 }
378
Florin Malitac2d5bd02017-03-09 13:34:09 -0500379 bool operator==(const SkTArray<T, MEM_MOVE>& right) const {
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000380 int leftCount = this->count();
381 if (leftCount != right.count()) {
382 return false;
383 }
384 for (int index = 0; index < leftCount; ++index) {
385 if (fItemArray[index] != right.fItemArray[index]) {
386 return false;
387 }
388 }
389 return true;
390 }
391
Florin Malitac2d5bd02017-03-09 13:34:09 -0500392 bool operator!=(const SkTArray<T, MEM_MOVE>& right) const {
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000393 return !(*this == right);
394 }
395
Brian Salomon69225d02017-03-15 20:52:35 -0400396 inline int allocCntForTest() const;
397
bsalomon@google.com92669012011-09-27 19:10:05 +0000398protected:
399 /**
400 * Creates an empty array that will use the passed storage block until it
401 * is insufficiently large to hold the entire array.
402 */
403 template <int N>
404 SkTArray(SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400405 this->initWithPreallocatedStorage(0, storage->get(), N);
bsalomon@google.com92669012011-09-27 19:10:05 +0000406 }
407
408 /**
409 * Copy another array, using preallocated storage if preAllocCount >=
410 * array.count(). Otherwise storage will only be used when array shrinks
411 * to fit.
412 */
413 template <int N>
414 SkTArray(const SkTArray& array, SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400415 this->initWithPreallocatedStorage(array.fCount, storage->get(), N);
bungeman0d9e9be2016-04-20 10:22:20 -0700416 this->copy(array.fItemArray);
bsalomon@google.com92669012011-09-27 19:10:05 +0000417 }
418
419 /**
Florin Malitab1d800d2017-03-09 12:17:15 -0500420 * Move another array, using preallocated storage if preAllocCount >=
421 * array.count(). Otherwise storage will only be used when array shrinks
422 * to fit.
423 */
424 template <int N>
425 SkTArray(SkTArray&& array, SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400426 this->initWithPreallocatedStorage(array.fCount, storage->get(), N);
Florin Malitab1d800d2017-03-09 12:17:15 -0500427 array.move(fMemArray);
428 array.fCount = 0;
429 }
430
431 /**
bsalomon@google.com92669012011-09-27 19:10:05 +0000432 * Copy a C array, using preallocated storage if preAllocCount >=
433 * count. Otherwise storage will only be used when array shrinks
434 * to fit.
435 */
436 template <int N>
437 SkTArray(const T* array, int count, SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400438 this->initWithPreallocatedStorage(count, storage->get(), N);
bungeman0d9e9be2016-04-20 10:22:20 -0700439 this->copy(array);
bsalomon@google.com92669012011-09-27 19:10:05 +0000440 }
441
Brian Salomon69225d02017-03-15 20:52:35 -0400442private:
443 void init(int count = 0, int reserveCount = 0) {
junov@chromium.orgd80a5092011-11-30 18:35:19 +0000444 SkASSERT(count >= 0);
Brian Salomon69225d02017-03-15 20:52:35 -0400445 SkASSERT(reserveCount >= 0);
446 fCount = count;
447 if (!count && !reserveCount) {
448 fAllocCount = 0;
449 fMemArray = nullptr;
Jim Van Verth474d6872017-12-14 13:00:05 -0500450 fOwnMemory = true;
Brian Salomon610842a2017-06-16 06:47:30 -0400451 fReserved = false;
bsalomon@google.com92669012011-09-27 19:10:05 +0000452 } else {
Brian Salomon69225d02017-03-15 20:52:35 -0400453 fAllocCount = SkTMax(count, SkTMax(kMinHeapAllocCount, reserveCount));
Mike Reed8dc8dbc2018-01-05 11:20:10 -0500454 fMemArray = sk_malloc_throw(fAllocCount, sizeof(T));
Brian Salomon69225d02017-03-15 20:52:35 -0400455 fOwnMemory = true;
Brian Salomon610842a2017-06-16 06:47:30 -0400456 fReserved = reserveCount > 0;
bsalomon@google.com92669012011-09-27 19:10:05 +0000457 }
bsalomon@google.com92669012011-09-27 19:10:05 +0000458 }
459
Brian Salomon69225d02017-03-15 20:52:35 -0400460 void initWithPreallocatedStorage(int count, void* preallocStorage, int preallocCount) {
461 SkASSERT(count >= 0);
462 SkASSERT(preallocCount > 0);
463 SkASSERT(preallocStorage);
464 fCount = count;
465 fMemArray = nullptr;
Brian Salomon610842a2017-06-16 06:47:30 -0400466 fReserved = false;
Brian Salomon69225d02017-03-15 20:52:35 -0400467 if (count > preallocCount) {
468 fAllocCount = SkTMax(count, kMinHeapAllocCount);
Mike Reed8dc8dbc2018-01-05 11:20:10 -0500469 fMemArray = sk_malloc_throw(fAllocCount, sizeof(T));
Brian Salomon69225d02017-03-15 20:52:35 -0400470 fOwnMemory = true;
471 } else {
472 fAllocCount = preallocCount;
473 fMemArray = preallocStorage;
474 fOwnMemory = false;
475 }
476 }
477
bungeman918090c2016-02-09 09:14:28 -0800478 /** In the following move and copy methods, 'dst' is assumed to be uninitialized raw storage.
479 * In the following move methods, 'src' is destroyed leaving behind uninitialized raw storage.
480 */
Florin Malitac2d5bd02017-03-09 13:34:09 -0500481 void copy(const T* src) {
482 // Some types may be trivially copyable, in which case we *could* use memcopy; but
483 // MEM_MOVE == true implies that the type is trivially movable, and not necessarily
484 // trivially copyable (think sk_sp<>). So short of adding another template arg, we
485 // must be conservative and use copy construction.
Mike Kleinc1db6102018-10-10 21:01:37 +0000486 for (int i = 0; i < fCount; ++i) {
487 new (fItemArray + i) T(src[i]);
488 }
bungeman918090c2016-02-09 09:14:28 -0800489 }
Florin Malitac2d5bd02017-03-09 13:34:09 -0500490
491 template <bool E = MEM_MOVE> SK_WHEN(E, void) move(int dst, int src) {
492 memcpy(&fItemArray[dst], &fItemArray[src], sizeof(T));
493 }
494 template <bool E = MEM_MOVE> SK_WHEN(E, void) move(void* dst) {
495 sk_careful_memcpy(dst, fMemArray, fCount * sizeof(T));
496 }
497
498 template <bool E = MEM_MOVE> SK_WHEN(!E, void) move(int dst, int src) {
bungeman918090c2016-02-09 09:14:28 -0800499 new (&fItemArray[dst]) T(std::move(fItemArray[src]));
500 fItemArray[src].~T();
501 }
Florin Malitac2d5bd02017-03-09 13:34:09 -0500502 template <bool E = MEM_MOVE> SK_WHEN(!E, void) move(void* dst) {
Mike Kleinc1db6102018-10-10 21:01:37 +0000503 for (int i = 0; i < fCount; ++i) {
504 new (static_cast<char*>(dst) + sizeof(T) * i) T(std::move(fItemArray[i]));
505 fItemArray[i].~T();
506 }
bungeman918090c2016-02-09 09:14:28 -0800507 }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000508
Brian Salomon69225d02017-03-15 20:52:35 -0400509 static constexpr int kMinHeapAllocCount = 8;
Mike Reed6c14c8d2017-03-09 16:36:26 -0500510
bsalomon@google.comd5104142013-06-13 15:13:46 +0000511 // Helper function that makes space for n objects, adjusts the count, but does not initialize
512 // the new objects.
Mike Kleinc1db6102018-10-10 21:01:37 +0000513 void* push_back_raw(int n) {
bsalomon@google.comd5104142013-06-13 15:13:46 +0000514 this->checkRealloc(n);
Mike Kleinc1db6102018-10-10 21:01:37 +0000515 void* ptr = fItemArray + fCount;
bsalomon@google.comd5104142013-06-13 15:13:46 +0000516 fCount += n;
517 return ptr;
518 }
519
Brian Salomon69225d02017-03-15 20:52:35 -0400520 void checkRealloc(int delta) {
bsalomon@google.com49313f62011-09-14 13:54:05 +0000521 SkASSERT(fCount >= 0);
522 SkASSERT(fAllocCount >= 0);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000523 SkASSERT(-delta <= fCount);
524
Mike Reedcab25492018-05-10 10:55:43 -0400525 // Move into 64bit math temporarily, to avoid local overflows
526 int64_t newCount = fCount + delta;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000527
Brian Salomon69225d02017-03-15 20:52:35 -0400528 // We allow fAllocCount to be in the range [newCount, 3*newCount]. We also never shrink
Brian Salomon610842a2017-06-16 06:47:30 -0400529 // when we're currently using preallocated memory, would allocate less than
530 // kMinHeapAllocCount, or a reserve count was specified that has yet to be exceeded.
Brian Salomon69225d02017-03-15 20:52:35 -0400531 bool mustGrow = newCount > fAllocCount;
Brian Salomon610842a2017-06-16 06:47:30 -0400532 bool shouldShrink = fAllocCount > 3 * newCount && fOwnMemory && !fReserved;
Brian Salomon69225d02017-03-15 20:52:35 -0400533 if (!mustGrow && !shouldShrink) {
534 return;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000535 }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000536
Mike Reedcab25492018-05-10 10:55:43 -0400537
Brian Salomon69225d02017-03-15 20:52:35 -0400538 // Whether we're growing or shrinking, we leave at least 50% extra space for future growth.
Mike Reedcab25492018-05-10 10:55:43 -0400539 int64_t newAllocCount = newCount + ((newCount + 1) >> 1);
Brian Salomon69225d02017-03-15 20:52:35 -0400540 // Align the new allocation count to kMinHeapAllocCount.
541 static_assert(SkIsPow2(kMinHeapAllocCount), "min alloc count not power of two.");
542 newAllocCount = (newAllocCount + (kMinHeapAllocCount - 1)) & ~(kMinHeapAllocCount - 1);
543 // At small sizes the old and new alloc count can both be kMinHeapAllocCount.
544 if (newAllocCount == fAllocCount) {
545 return;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000546 }
Mike Reedcab25492018-05-10 10:55:43 -0400547
548 fAllocCount = Sk64_pin_to_s32(newAllocCount);
549 SkASSERT(fAllocCount >= newCount);
Mike Reed8dc8dbc2018-01-05 11:20:10 -0500550 void* newMemArray = sk_malloc_throw(fAllocCount, sizeof(T));
Brian Salomon69225d02017-03-15 20:52:35 -0400551 this->move(newMemArray);
552 if (fOwnMemory) {
553 sk_free(fMemArray);
554
555 }
556 fMemArray = newMemArray;
557 fOwnMemory = true;
Brian Salomon610842a2017-06-16 06:47:30 -0400558 fReserved = false;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000559 }
560
bsalomon@google.com49313f62011-09-14 13:54:05 +0000561 union {
562 T* fItemArray;
563 void* fMemArray;
564 };
Brian Salomon610842a2017-06-16 06:47:30 -0400565 int fCount;
566 int fAllocCount;
567 bool fOwnMemory : 1;
568 bool fReserved : 1;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000569};
570
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400571template <typename T, bool M> static inline void swap(SkTArray<T, M>& a, SkTArray<T, M>& b) {
572 a.swap(b);
573}
574
Brian Salomon69225d02017-03-15 20:52:35 -0400575template<typename T, bool MEM_MOVE> constexpr int SkTArray<T, MEM_MOVE>::kMinHeapAllocCount;
576
bsalomon@google.com92669012011-09-27 19:10:05 +0000577/**
578 * Subclass of SkTArray that contains a preallocated memory block for the array.
579 */
Florin Malitac2d5bd02017-03-09 13:34:09 -0500580template <int N, typename T, bool MEM_MOVE= false>
581class SkSTArray : public SkTArray<T, MEM_MOVE> {
bsalomon@google.com92669012011-09-27 19:10:05 +0000582private:
Florin Malitac2d5bd02017-03-09 13:34:09 -0500583 typedef SkTArray<T, MEM_MOVE> INHERITED;
bsalomon@google.com92669012011-09-27 19:10:05 +0000584
585public:
586 SkSTArray() : INHERITED(&fStorage) {
587 }
588
589 SkSTArray(const SkSTArray& array)
590 : INHERITED(array, &fStorage) {
591 }
592
Florin Malitab1d800d2017-03-09 12:17:15 -0500593 SkSTArray(SkSTArray&& array)
594 : INHERITED(std::move(array), &fStorage) {
595 }
596
bsalomon@google.com92669012011-09-27 19:10:05 +0000597 explicit SkSTArray(const INHERITED& array)
598 : INHERITED(array, &fStorage) {
599 }
600
Florin Malitab1d800d2017-03-09 12:17:15 -0500601 explicit SkSTArray(INHERITED&& array)
602 : INHERITED(std::move(array), &fStorage) {
603 }
604
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000605 explicit SkSTArray(int reserveCount)
606 : INHERITED(reserveCount) {
607 }
608
bsalomon@google.com92669012011-09-27 19:10:05 +0000609 SkSTArray(const T* array, int count)
610 : INHERITED(array, count, &fStorage) {
611 }
612
Brian Salomon69225d02017-03-15 20:52:35 -0400613 SkSTArray& operator=(const SkSTArray& array) {
Florin Malitad54639f2017-03-12 10:40:13 -0400614 INHERITED::operator=(array);
615 return *this;
616 }
617
Brian Salomon69225d02017-03-15 20:52:35 -0400618 SkSTArray& operator=(SkSTArray&& array) {
Florin Malitad54639f2017-03-12 10:40:13 -0400619 INHERITED::operator=(std::move(array));
620 return *this;
bsalomon@google.com92669012011-09-27 19:10:05 +0000621 }
622
Brian Salomon69225d02017-03-15 20:52:35 -0400623 SkSTArray& operator=(const INHERITED& array) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000624 INHERITED::operator=(array);
625 return *this;
626 }
627
Brian Salomon69225d02017-03-15 20:52:35 -0400628 SkSTArray& operator=(INHERITED&& array) {
Florin Malitad54639f2017-03-12 10:40:13 -0400629 INHERITED::operator=(std::move(array));
630 return *this;
631 }
632
bsalomon@google.com92669012011-09-27 19:10:05 +0000633private:
634 SkAlignedSTStorage<N,T> fStorage;
635};
636
bsalomon@google.com49313f62011-09-14 13:54:05 +0000637#endif