blob: 75cd001b72d95e650dc52b83dc989fbe0b8a4ba4 [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 }
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 }
330
331 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000332 * Get the i^th element.
333 */
334 T& operator[] (int i) {
335 SkASSERT(i < fCount);
336 SkASSERT(i >= 0);
337 return fItemArray[i];
338 }
339
340 const T& operator[] (int i) const {
341 SkASSERT(i < fCount);
342 SkASSERT(i >= 0);
343 return fItemArray[i];
344 }
345
346 /**
347 * equivalent to operator[](0)
348 */
349 T& front() { SkASSERT(fCount > 0); return fItemArray[0];}
350
351 const T& front() const { SkASSERT(fCount > 0); return fItemArray[0];}
352
353 /**
354 * equivalent to operator[](count() - 1)
355 */
356 T& back() { SkASSERT(fCount); return fItemArray[fCount - 1];}
357
358 const T& back() const { SkASSERT(fCount > 0); return fItemArray[fCount - 1];}
359
360 /**
361 * equivalent to operator[](count()-1-i)
362 */
363 T& fromBack(int i) {
364 SkASSERT(i >= 0);
365 SkASSERT(i < fCount);
366 return fItemArray[fCount - i - 1];
367 }
368
369 const T& fromBack(int i) const {
370 SkASSERT(i >= 0);
371 SkASSERT(i < fCount);
372 return fItemArray[fCount - i - 1];
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 int leftCount = this->count();
377 if (leftCount != right.count()) {
378 return false;
379 }
380 for (int index = 0; index < leftCount; ++index) {
381 if (fItemArray[index] != right.fItemArray[index]) {
382 return false;
383 }
384 }
385 return true;
386 }
387
Florin Malitac2d5bd02017-03-09 13:34:09 -0500388 bool operator!=(const SkTArray<T, MEM_MOVE>& right) const {
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000389 return !(*this == right);
390 }
391
Brian Salomon69225d02017-03-15 20:52:35 -0400392 inline int allocCntForTest() const;
393
bsalomon@google.com92669012011-09-27 19:10:05 +0000394protected:
395 /**
396 * Creates an empty array that will use the passed storage block until it
397 * is insufficiently large to hold the entire array.
398 */
399 template <int N>
400 SkTArray(SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400401 this->initWithPreallocatedStorage(0, storage->get(), N);
bsalomon@google.com92669012011-09-27 19:10:05 +0000402 }
403
404 /**
405 * Copy another array, using preallocated storage if preAllocCount >=
406 * array.count(). Otherwise storage will only be used when array shrinks
407 * to fit.
408 */
409 template <int N>
410 SkTArray(const SkTArray& array, SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400411 this->initWithPreallocatedStorage(array.fCount, storage->get(), N);
bungeman0d9e9be2016-04-20 10:22:20 -0700412 this->copy(array.fItemArray);
bsalomon@google.com92669012011-09-27 19:10:05 +0000413 }
414
415 /**
Florin Malitab1d800d2017-03-09 12:17:15 -0500416 * Move another array, using preallocated storage if preAllocCount >=
417 * array.count(). Otherwise storage will only be used when array shrinks
418 * to fit.
419 */
420 template <int N>
421 SkTArray(SkTArray&& array, SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400422 this->initWithPreallocatedStorage(array.fCount, storage->get(), N);
Florin Malitab1d800d2017-03-09 12:17:15 -0500423 array.move(fMemArray);
424 array.fCount = 0;
425 }
426
427 /**
bsalomon@google.com92669012011-09-27 19:10:05 +0000428 * Copy a C array, using preallocated storage if preAllocCount >=
429 * count. Otherwise storage will only be used when array shrinks
430 * to fit.
431 */
432 template <int N>
433 SkTArray(const T* array, int count, SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400434 this->initWithPreallocatedStorage(count, storage->get(), N);
bungeman0d9e9be2016-04-20 10:22:20 -0700435 this->copy(array);
bsalomon@google.com92669012011-09-27 19:10:05 +0000436 }
437
Brian Salomon69225d02017-03-15 20:52:35 -0400438private:
439 void init(int count = 0, int reserveCount = 0) {
junov@chromium.orgd80a5092011-11-30 18:35:19 +0000440 SkASSERT(count >= 0);
Brian Salomon69225d02017-03-15 20:52:35 -0400441 SkASSERT(reserveCount >= 0);
442 fCount = count;
443 if (!count && !reserveCount) {
444 fAllocCount = 0;
445 fMemArray = nullptr;
Jim Van Verth474d6872017-12-14 13:00:05 -0500446 fOwnMemory = true;
Brian Salomon610842a2017-06-16 06:47:30 -0400447 fReserved = false;
bsalomon@google.com92669012011-09-27 19:10:05 +0000448 } else {
Brian Salomon69225d02017-03-15 20:52:35 -0400449 fAllocCount = SkTMax(count, SkTMax(kMinHeapAllocCount, reserveCount));
Mike Reed8dc8dbc2018-01-05 11:20:10 -0500450 fMemArray = sk_malloc_throw(fAllocCount, sizeof(T));
Brian Salomon69225d02017-03-15 20:52:35 -0400451 fOwnMemory = true;
Brian Salomon610842a2017-06-16 06:47:30 -0400452 fReserved = reserveCount > 0;
bsalomon@google.com92669012011-09-27 19:10:05 +0000453 }
bsalomon@google.com92669012011-09-27 19:10:05 +0000454 }
455
Brian Salomon69225d02017-03-15 20:52:35 -0400456 void initWithPreallocatedStorage(int count, void* preallocStorage, int preallocCount) {
457 SkASSERT(count >= 0);
458 SkASSERT(preallocCount > 0);
459 SkASSERT(preallocStorage);
460 fCount = count;
461 fMemArray = nullptr;
Brian Salomon610842a2017-06-16 06:47:30 -0400462 fReserved = false;
Brian Salomon69225d02017-03-15 20:52:35 -0400463 if (count > preallocCount) {
464 fAllocCount = SkTMax(count, kMinHeapAllocCount);
Mike Reed8dc8dbc2018-01-05 11:20:10 -0500465 fMemArray = sk_malloc_throw(fAllocCount, sizeof(T));
Brian Salomon69225d02017-03-15 20:52:35 -0400466 fOwnMemory = true;
467 } else {
468 fAllocCount = preallocCount;
469 fMemArray = preallocStorage;
470 fOwnMemory = false;
471 }
472 }
473
bungeman918090c2016-02-09 09:14:28 -0800474 /** In the following move and copy methods, 'dst' is assumed to be uninitialized raw storage.
475 * In the following move methods, 'src' is destroyed leaving behind uninitialized raw storage.
476 */
Florin Malitac2d5bd02017-03-09 13:34:09 -0500477 void copy(const T* src) {
478 // Some types may be trivially copyable, in which case we *could* use memcopy; but
479 // MEM_MOVE == true implies that the type is trivially movable, and not necessarily
480 // trivially copyable (think sk_sp<>). So short of adding another template arg, we
481 // must be conservative and use copy construction.
Mike Kleinc1db6102018-10-10 21:01:37 +0000482 for (int i = 0; i < fCount; ++i) {
483 new (fItemArray + i) T(src[i]);
484 }
bungeman918090c2016-02-09 09:14:28 -0800485 }
Florin Malitac2d5bd02017-03-09 13:34:09 -0500486
487 template <bool E = MEM_MOVE> SK_WHEN(E, void) move(int dst, int src) {
488 memcpy(&fItemArray[dst], &fItemArray[src], sizeof(T));
489 }
490 template <bool E = MEM_MOVE> SK_WHEN(E, void) move(void* dst) {
491 sk_careful_memcpy(dst, fMemArray, fCount * sizeof(T));
492 }
493
494 template <bool E = MEM_MOVE> SK_WHEN(!E, void) move(int dst, int src) {
bungeman918090c2016-02-09 09:14:28 -0800495 new (&fItemArray[dst]) T(std::move(fItemArray[src]));
496 fItemArray[src].~T();
497 }
Florin Malitac2d5bd02017-03-09 13:34:09 -0500498 template <bool E = MEM_MOVE> SK_WHEN(!E, void) move(void* dst) {
Mike Kleinc1db6102018-10-10 21:01:37 +0000499 for (int i = 0; i < fCount; ++i) {
500 new (static_cast<char*>(dst) + sizeof(T) * i) T(std::move(fItemArray[i]));
501 fItemArray[i].~T();
502 }
bungeman918090c2016-02-09 09:14:28 -0800503 }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000504
Brian Salomon69225d02017-03-15 20:52:35 -0400505 static constexpr int kMinHeapAllocCount = 8;
Mike Reed6c14c8d2017-03-09 16:36:26 -0500506
bsalomon@google.comd5104142013-06-13 15:13:46 +0000507 // Helper function that makes space for n objects, adjusts the count, but does not initialize
508 // the new objects.
Mike Kleinc1db6102018-10-10 21:01:37 +0000509 void* push_back_raw(int n) {
bsalomon@google.comd5104142013-06-13 15:13:46 +0000510 this->checkRealloc(n);
Mike Kleinc1db6102018-10-10 21:01:37 +0000511 void* ptr = fItemArray + fCount;
bsalomon@google.comd5104142013-06-13 15:13:46 +0000512 fCount += n;
513 return ptr;
514 }
515
Brian Salomon69225d02017-03-15 20:52:35 -0400516 void checkRealloc(int delta) {
bsalomon@google.com49313f62011-09-14 13:54:05 +0000517 SkASSERT(fCount >= 0);
518 SkASSERT(fAllocCount >= 0);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000519 SkASSERT(-delta <= fCount);
520
Mike Reedcab25492018-05-10 10:55:43 -0400521 // Move into 64bit math temporarily, to avoid local overflows
522 int64_t newCount = fCount + delta;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000523
Brian Salomon69225d02017-03-15 20:52:35 -0400524 // We allow fAllocCount to be in the range [newCount, 3*newCount]. We also never shrink
Brian Salomon610842a2017-06-16 06:47:30 -0400525 // when we're currently using preallocated memory, would allocate less than
526 // kMinHeapAllocCount, or a reserve count was specified that has yet to be exceeded.
Brian Salomon69225d02017-03-15 20:52:35 -0400527 bool mustGrow = newCount > fAllocCount;
Brian Salomon610842a2017-06-16 06:47:30 -0400528 bool shouldShrink = fAllocCount > 3 * newCount && fOwnMemory && !fReserved;
Brian Salomon69225d02017-03-15 20:52:35 -0400529 if (!mustGrow && !shouldShrink) {
530 return;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000531 }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000532
Mike Reedcab25492018-05-10 10:55:43 -0400533
Brian Salomon69225d02017-03-15 20:52:35 -0400534 // Whether we're growing or shrinking, we leave at least 50% extra space for future growth.
Mike Reedcab25492018-05-10 10:55:43 -0400535 int64_t newAllocCount = newCount + ((newCount + 1) >> 1);
Brian Salomon69225d02017-03-15 20:52:35 -0400536 // Align the new allocation count to kMinHeapAllocCount.
537 static_assert(SkIsPow2(kMinHeapAllocCount), "min alloc count not power of two.");
538 newAllocCount = (newAllocCount + (kMinHeapAllocCount - 1)) & ~(kMinHeapAllocCount - 1);
539 // At small sizes the old and new alloc count can both be kMinHeapAllocCount.
540 if (newAllocCount == fAllocCount) {
541 return;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000542 }
Mike Reedcab25492018-05-10 10:55:43 -0400543
544 fAllocCount = Sk64_pin_to_s32(newAllocCount);
545 SkASSERT(fAllocCount >= newCount);
Mike Reed8dc8dbc2018-01-05 11:20:10 -0500546 void* newMemArray = sk_malloc_throw(fAllocCount, sizeof(T));
Brian Salomon69225d02017-03-15 20:52:35 -0400547 this->move(newMemArray);
548 if (fOwnMemory) {
549 sk_free(fMemArray);
550
551 }
552 fMemArray = newMemArray;
553 fOwnMemory = true;
Brian Salomon610842a2017-06-16 06:47:30 -0400554 fReserved = false;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000555 }
556
bsalomon@google.com49313f62011-09-14 13:54:05 +0000557 union {
558 T* fItemArray;
559 void* fMemArray;
560 };
Brian Salomon610842a2017-06-16 06:47:30 -0400561 int fCount;
562 int fAllocCount;
563 bool fOwnMemory : 1;
564 bool fReserved : 1;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000565};
566
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400567template <typename T, bool M> static inline void swap(SkTArray<T, M>& a, SkTArray<T, M>& b) {
568 a.swap(b);
569}
570
Brian Salomon69225d02017-03-15 20:52:35 -0400571template<typename T, bool MEM_MOVE> constexpr int SkTArray<T, MEM_MOVE>::kMinHeapAllocCount;
572
bsalomon@google.com92669012011-09-27 19:10:05 +0000573/**
574 * Subclass of SkTArray that contains a preallocated memory block for the array.
575 */
Florin Malitac2d5bd02017-03-09 13:34:09 -0500576template <int N, typename T, bool MEM_MOVE= false>
577class SkSTArray : public SkTArray<T, MEM_MOVE> {
bsalomon@google.com92669012011-09-27 19:10:05 +0000578private:
Florin Malitac2d5bd02017-03-09 13:34:09 -0500579 typedef SkTArray<T, MEM_MOVE> INHERITED;
bsalomon@google.com92669012011-09-27 19:10:05 +0000580
581public:
582 SkSTArray() : INHERITED(&fStorage) {
583 }
584
585 SkSTArray(const SkSTArray& array)
586 : INHERITED(array, &fStorage) {
587 }
588
Florin Malitab1d800d2017-03-09 12:17:15 -0500589 SkSTArray(SkSTArray&& array)
590 : INHERITED(std::move(array), &fStorage) {
591 }
592
bsalomon@google.com92669012011-09-27 19:10:05 +0000593 explicit SkSTArray(const INHERITED& array)
594 : INHERITED(array, &fStorage) {
595 }
596
Florin Malitab1d800d2017-03-09 12:17:15 -0500597 explicit SkSTArray(INHERITED&& array)
598 : INHERITED(std::move(array), &fStorage) {
599 }
600
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000601 explicit SkSTArray(int reserveCount)
602 : INHERITED(reserveCount) {
603 }
604
bsalomon@google.com92669012011-09-27 19:10:05 +0000605 SkSTArray(const T* array, int count)
606 : INHERITED(array, count, &fStorage) {
607 }
608
Brian Salomon69225d02017-03-15 20:52:35 -0400609 SkSTArray& operator=(const SkSTArray& array) {
Florin Malitad54639f2017-03-12 10:40:13 -0400610 INHERITED::operator=(array);
611 return *this;
612 }
613
Brian Salomon69225d02017-03-15 20:52:35 -0400614 SkSTArray& operator=(SkSTArray&& array) {
Florin Malitad54639f2017-03-12 10:40:13 -0400615 INHERITED::operator=(std::move(array));
616 return *this;
bsalomon@google.com92669012011-09-27 19:10:05 +0000617 }
618
Brian Salomon69225d02017-03-15 20:52:35 -0400619 SkSTArray& operator=(const INHERITED& array) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000620 INHERITED::operator=(array);
621 return *this;
622 }
623
Brian Salomon69225d02017-03-15 20:52:35 -0400624 SkSTArray& operator=(INHERITED&& array) {
Florin Malitad54639f2017-03-12 10:40:13 -0400625 INHERITED::operator=(std::move(array));
626 return *this;
627 }
628
bsalomon@google.com92669012011-09-27 19:10:05 +0000629private:
630 SkAlignedSTStorage<N,T> fStorage;
631};
632
bsalomon@google.com49313f62011-09-14 13:54:05 +0000633#endif