blob: c70164abf3b5e930ecdeed97aa75fe315c9b2bb4 [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
Ben Wagner0b9b1f12019-04-04 18:00:05 -040011#include "include/core/SkMath.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkTypes.h"
Ben Wagner0b9b1f12019-04-04 18:00:05 -040013#include "include/private/SkMalloc.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "include/private/SkSafe32.h"
15#include "include/private/SkTLogic.h"
16#include "include/private/SkTemplates.h"
bungemanf3c15b72015-08-19 11:56:48 -070017
Ben Wagner0b9b1f12019-04-04 18:00:05 -040018#include <string.h>
19#include <memory>
bungemanf3c15b72015-08-19 11:56:48 -070020#include <new>
bungeman221524d2016-01-05 14:59:40 -080021#include <utility>
bsalomon@google.com49313f62011-09-14 13:54:05 +000022
Florin Malitac2d5bd02017-03-09 13:34:09 -050023/** When MEM_MOVE is true T will be bit copied when moved.
24 When MEM_MOVE is false, T will be copy constructed / destructed.
bungeman@google.comd58a8562014-03-24 15:55:01 +000025 In all cases T will be default-initialized on allocation,
bungeman@google.coma12cc7f2011-10-07 20:54:15 +000026 and its destructor will be called from this object's destructor.
27*/
Florin Malitac2d5bd02017-03-09 13:34:09 -050028template <typename T, bool MEM_MOVE = false> class SkTArray {
bsalomon@google.com49313f62011-09-14 13:54:05 +000029public:
30 /**
31 * Creates an empty array with no initial storage
32 */
Brian Salomon69225d02017-03-15 20:52:35 -040033 SkTArray() { this->init(); }
bsalomon@google.com49313f62011-09-14 13:54:05 +000034
35 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000036 * Creates an empty array that will preallocate space for reserveCount
bsalomon@google.com49313f62011-09-14 13:54:05 +000037 * elements.
38 */
Brian Salomon69225d02017-03-15 20:52:35 -040039 explicit SkTArray(int reserveCount) { this->init(0, reserveCount); }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000040
bsalomon@google.com49313f62011-09-14 13:54:05 +000041 /**
42 * Copies one array to another. The new array will be heap allocated.
43 */
Hal Canaryda7a5672018-10-19 10:16:01 -040044 SkTArray(const SkTArray& that) {
Brian Salomon69225d02017-03-15 20:52:35 -040045 this->init(that.fCount);
bungeman0d9e9be2016-04-20 10:22:20 -070046 this->copy(that.fItemArray);
47 }
Brian Salomon69225d02017-03-15 20:52:35 -040048
Hal Canaryda7a5672018-10-19 10:16:01 -040049 SkTArray(SkTArray&& that) {
Brian Salomon69225d02017-03-15 20:52:35 -040050 // TODO: If 'that' owns its memory why don't we just steal the pointer?
51 this->init(that.fCount);
bungeman0d9e9be2016-04-20 10:22:20 -070052 that.move(fMemArray);
53 that.fCount = 0;
bsalomon@google.com49313f62011-09-14 13:54:05 +000054 }
55
56 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000057 * Creates a SkTArray by copying contents of a standard C array. The new
bsalomon@google.com49313f62011-09-14 13:54:05 +000058 * array will be heap allocated. Be careful not to use this constructor
59 * when you really want the (void*, int) version.
60 */
61 SkTArray(const T* array, int count) {
Brian Salomon69225d02017-03-15 20:52:35 -040062 this->init(count);
bungeman0d9e9be2016-04-20 10:22:20 -070063 this->copy(array);
bsalomon@google.com49313f62011-09-14 13:54:05 +000064 }
65
Brian Salomon69225d02017-03-15 20:52:35 -040066 SkTArray& operator=(const SkTArray& that) {
Greg Daniel70131b92017-03-22 13:33:21 -040067 if (this == &that) {
68 return *this;
69 }
Mike Kleinc1db6102018-10-10 21:01:37 +000070 for (int i = 0; i < fCount; ++i) {
71 fItemArray[i].~T();
72 }
bsalomon@google.com49313f62011-09-14 13:54:05 +000073 fCount = 0;
bungeman0d9e9be2016-04-20 10:22:20 -070074 this->checkRealloc(that.count());
75 fCount = that.count();
76 this->copy(that.fItemArray);
77 return *this;
78 }
Brian Salomon69225d02017-03-15 20:52:35 -040079 SkTArray& operator=(SkTArray&& that) {
Greg Daniel70131b92017-03-22 13:33:21 -040080 if (this == &that) {
81 return *this;
82 }
Mike Kleinc1db6102018-10-10 21:01:37 +000083 for (int i = 0; i < fCount; ++i) {
84 fItemArray[i].~T();
85 }
bungeman0d9e9be2016-04-20 10:22:20 -070086 fCount = 0;
87 this->checkRealloc(that.count());
88 fCount = that.count();
89 that.move(fMemArray);
90 that.fCount = 0;
bsalomon@google.com49313f62011-09-14 13:54:05 +000091 return *this;
92 }
93
bsalomon383ff102015-07-31 11:53:11 -070094 ~SkTArray() {
Mike Kleinc1db6102018-10-10 21:01:37 +000095 for (int i = 0; i < fCount; ++i) {
96 fItemArray[i].~T();
97 }
Brian Salomon69225d02017-03-15 20:52:35 -040098 if (fOwnMemory) {
bsalomon@google.com49313f62011-09-14 13:54:05 +000099 sk_free(fMemArray);
100 }
101 }
102
103 /**
Brian Salomon610842a2017-06-16 06:47:30 -0400104 * Resets to count() == 0 and resets any reserve count.
bsalomon@google.com49313f62011-09-14 13:54:05 +0000105 */
Brian Salomon610842a2017-06-16 06:47:30 -0400106 void reset() {
107 this->pop_back_n(fCount);
108 fReserved = false;
109 }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000110
111 /**
Brian Salomon610842a2017-06-16 06:47:30 -0400112 * Resets to count() = n newly constructed T objects and resets any reserve count.
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000113 */
114 void reset(int n) {
115 SkASSERT(n >= 0);
Mike Kleinc1db6102018-10-10 21:01:37 +0000116 for (int i = 0; i < fCount; ++i) {
117 fItemArray[i].~T();
118 }
bungeman0d9e9be2016-04-20 10:22:20 -0700119 // Set fCount to 0 before calling checkRealloc so that no elements are moved.
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000120 fCount = 0;
121 this->checkRealloc(n);
122 fCount = n;
Mike Kleinc1db6102018-10-10 21:01:37 +0000123 for (int i = 0; i < fCount; ++i) {
124 new (fItemArray + i) T;
125 }
Brian Salomon610842a2017-06-16 06:47:30 -0400126 fReserved = false;
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000127 }
128
129 /**
Brian Salomon610842a2017-06-16 06:47:30 -0400130 * Resets to a copy of a C array and resets any reserve count.
jvanverth@google.com054ae992013-04-01 20:06:51 +0000131 */
132 void reset(const T* array, int count) {
Mike Kleinc1db6102018-10-10 21:01:37 +0000133 for (int i = 0; i < fCount; ++i) {
134 fItemArray[i].~T();
135 }
bungeman0d9e9be2016-04-20 10:22:20 -0700136 fCount = 0;
137 this->checkRealloc(count);
jvanverth@google.com054ae992013-04-01 20:06:51 +0000138 fCount = count;
bungeman918090c2016-02-09 09:14:28 -0800139 this->copy(array);
Brian Salomon610842a2017-06-16 06:47:30 -0400140 fReserved = false;
141 }
142
143 /**
144 * Ensures there is enough reserved space for n additional elements. The is guaranteed at least
145 * until the array size grows above n and subsequently shrinks below n, any version of reset()
146 * is called, or reserve() is called again.
147 */
148 void reserve(int n) {
149 SkASSERT(n >= 0);
150 if (n > 0) {
151 this->checkRealloc(n);
152 fReserved = fOwnMemory;
153 } else {
154 fReserved = false;
155 }
bungeman@google.com95ebd172014-03-21 19:39:02 +0000156 }
157
158 void removeShuffle(int n) {
159 SkASSERT(n < fCount);
160 int newCount = fCount - 1;
161 fCount = newCount;
162 fItemArray[n].~T();
163 if (n != newCount) {
bungeman918090c2016-02-09 09:14:28 -0800164 this->move(n, newCount);
jvanverth@google.com054ae992013-04-01 20:06:51 +0000165 }
166 }
167
168 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000169 * Number of elements in the array.
170 */
171 int count() const { return fCount; }
172
173 /**
174 * Is the array empty.
175 */
176 bool empty() const { return !fCount; }
177
178 /**
bungeman@google.comd58a8562014-03-24 15:55:01 +0000179 * Adds 1 new default-initialized T value and returns it by reference. Note
bsalomon@google.com49313f62011-09-14 13:54:05 +0000180 * the reference only remains valid until the next call that adds or removes
181 * elements.
182 */
Mike Kleinc1db6102018-10-10 21:01:37 +0000183 T& push_back() {
184 void* newT = this->push_back_raw(1);
185 return *new (newT) T;
186 }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000187
188 /**
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000189 * Version of above that uses a copy constructor to initialize the new item
190 */
Mike Kleinc1db6102018-10-10 21:01:37 +0000191 T& push_back(const T& t) {
192 void* newT = this->push_back_raw(1);
193 return *new (newT) T(t);
194 }
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000195
196 /**
halcanary91fcb3e2016-03-04 13:53:22 -0800197 * Version of above that uses a move constructor to initialize the new item
198 */
Mike Kleinc1db6102018-10-10 21:01:37 +0000199 T& push_back(T&& t) {
200 void* newT = this->push_back_raw(1);
201 return *new (newT) T(std::move(t));
202 }
halcanary91fcb3e2016-03-04 13:53:22 -0800203
204 /**
halcanaryf12a1672015-09-23 12:45:49 -0700205 * Construct a new T at the back of this array.
206 */
207 template<class... Args> T& emplace_back(Args&&... args) {
Mike Kleinc1db6102018-10-10 21:01:37 +0000208 void* newT = this->push_back_raw(1);
209 return *new (newT) T(std::forward<Args>(args)...);
halcanaryf12a1672015-09-23 12:45:49 -0700210 }
211
212 /**
bungeman@google.comd58a8562014-03-24 15:55:01 +0000213 * Allocates n more default-initialized T values, and returns the address of
214 * the start of that new range. Note: this address is only valid until the
215 * next API call made on the array that might add or remove elements.
bsalomon@google.com49313f62011-09-14 13:54:05 +0000216 */
217 T* push_back_n(int n) {
218 SkASSERT(n >= 0);
Mike Kleinc1db6102018-10-10 21:01:37 +0000219 void* newTs = this->push_back_raw(n);
220 for (int i = 0; i < n; ++i) {
221 new (static_cast<char*>(newTs) + i * sizeof(T)) T;
222 }
223 return static_cast<T*>(newTs);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000224 }
225
226 /**
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000227 * Version of above that uses a copy constructor to initialize all n items
228 * to the same T.
229 */
230 T* push_back_n(int n, const T& t) {
231 SkASSERT(n >= 0);
Mike Kleinc1db6102018-10-10 21:01:37 +0000232 void* newTs = this->push_back_raw(n);
233 for (int i = 0; i < n; ++i) {
234 new (static_cast<char*>(newTs) + i * sizeof(T)) T(t);
235 }
236 return static_cast<T*>(newTs);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000237 }
238
239 /**
240 * Version of above that uses a copy constructor to initialize the n items
241 * to separate T values.
242 */
Mike Kleinc1db6102018-10-10 21:01:37 +0000243 T* push_back_n(int n, const T t[]) {
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000244 SkASSERT(n >= 0);
Mike Kleinc1db6102018-10-10 21:01:37 +0000245 this->checkRealloc(n);
246 for (int i = 0; i < n; ++i) {
247 new (fItemArray + fCount + i) T(t[i]);
248 }
249 fCount += n;
250 return fItemArray + fCount - n;
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000251 }
252
253 /**
msarett10e3d9b2016-08-18 15:46:03 -0700254 * Version of above that uses the move constructor to set n items.
255 */
Mike Kleinc1db6102018-10-10 21:01:37 +0000256 T* move_back_n(int n, T* t) {
msarett10e3d9b2016-08-18 15:46:03 -0700257 SkASSERT(n >= 0);
Mike Kleinc1db6102018-10-10 21:01:37 +0000258 this->checkRealloc(n);
259 for (int i = 0; i < n; ++i) {
260 new (fItemArray + fCount + i) T(std::move(t[i]));
261 }
262 fCount += n;
263 return fItemArray + fCount - n;
msarett10e3d9b2016-08-18 15:46:03 -0700264 }
265
266 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000267 * Removes the last element. Not safe to call when count() == 0.
268 */
269 void pop_back() {
270 SkASSERT(fCount > 0);
271 --fCount;
272 fItemArray[fCount].~T();
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000273 this->checkRealloc(0);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000274 }
275
276 /**
277 * Removes the last n elements. Not safe to call when count() < n.
278 */
279 void pop_back_n(int n) {
280 SkASSERT(n >= 0);
281 SkASSERT(fCount >= n);
282 fCount -= n;
Mike Kleinc1db6102018-10-10 21:01:37 +0000283 for (int i = 0; i < n; ++i) {
284 fItemArray[fCount + i].~T();
285 }
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000286 this->checkRealloc(0);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000287 }
288
289 /**
290 * Pushes or pops from the back to resize. Pushes will be default
291 * initialized.
292 */
293 void resize_back(int newCount) {
294 SkASSERT(newCount >= 0);
295
296 if (newCount > fCount) {
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000297 this->push_back_n(newCount - fCount);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000298 } else if (newCount < fCount) {
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000299 this->pop_back_n(fCount - newCount);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000300 }
301 }
302
bsalomon23e619c2015-02-06 11:54:28 -0800303 /** Swaps the contents of this array with that array. Does a pointer swap if possible,
304 otherwise copies the T values. */
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400305 void swap(SkTArray& that) {
306 using std::swap;
307 if (this == &that) {
bsalomon3632f842015-02-10 19:46:58 -0800308 return;
309 }
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400310 if (fOwnMemory && that.fOwnMemory) {
311 swap(fItemArray, that.fItemArray);
312 swap(fCount, that.fCount);
313 swap(fAllocCount, that.fAllocCount);
bsalomon23e619c2015-02-06 11:54:28 -0800314 } else {
315 // This could be more optimal...
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400316 SkTArray copy(std::move(that));
317 that = std::move(*this);
bungeman0d9e9be2016-04-20 10:22:20 -0700318 *this = std::move(copy);
bsalomon23e619c2015-02-06 11:54:28 -0800319 }
320 }
321
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000322 T* begin() {
323 return fItemArray;
324 }
325 const T* begin() const {
326 return fItemArray;
327 }
328 T* end() {
Ben Wagnera93a14a2017-08-28 10:34:05 -0400329 return fItemArray ? fItemArray + fCount : nullptr;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000330 }
331 const T* end() const {
Ben Wagnera93a14a2017-08-28 10:34:05 -0400332 return fItemArray ? fItemArray + fCount : nullptr;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000333 }
Hal Canaryda7a5672018-10-19 10:16:01 -0400334 T* data() { return fItemArray; }
335 const T* data() const { return fItemArray; }
336 size_t size() const { return (size_t)fCount; }
337 void resize(size_t count) { this->resize_back((int)count); }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000338
339 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000340 * Get the i^th element.
341 */
342 T& operator[] (int i) {
343 SkASSERT(i < fCount);
344 SkASSERT(i >= 0);
345 return fItemArray[i];
346 }
347
348 const T& operator[] (int i) const {
349 SkASSERT(i < fCount);
350 SkASSERT(i >= 0);
351 return fItemArray[i];
352 }
353
354 /**
355 * equivalent to operator[](0)
356 */
357 T& front() { SkASSERT(fCount > 0); return fItemArray[0];}
358
359 const T& front() const { SkASSERT(fCount > 0); return fItemArray[0];}
360
361 /**
362 * equivalent to operator[](count() - 1)
363 */
364 T& back() { SkASSERT(fCount); return fItemArray[fCount - 1];}
365
366 const T& back() const { SkASSERT(fCount > 0); return fItemArray[fCount - 1];}
367
368 /**
369 * equivalent to operator[](count()-1-i)
370 */
371 T& fromBack(int i) {
372 SkASSERT(i >= 0);
373 SkASSERT(i < fCount);
374 return fItemArray[fCount - i - 1];
375 }
376
377 const T& fromBack(int i) const {
378 SkASSERT(i >= 0);
379 SkASSERT(i < fCount);
380 return fItemArray[fCount - i - 1];
381 }
382
Florin Malitac2d5bd02017-03-09 13:34:09 -0500383 bool operator==(const SkTArray<T, MEM_MOVE>& right) const {
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000384 int leftCount = this->count();
385 if (leftCount != right.count()) {
386 return false;
387 }
388 for (int index = 0; index < leftCount; ++index) {
389 if (fItemArray[index] != right.fItemArray[index]) {
390 return false;
391 }
392 }
393 return true;
394 }
395
Florin Malitac2d5bd02017-03-09 13:34:09 -0500396 bool operator!=(const SkTArray<T, MEM_MOVE>& right) const {
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000397 return !(*this == right);
398 }
399
Brian Salomon69225d02017-03-15 20:52:35 -0400400 inline int allocCntForTest() const;
401
bsalomon@google.com92669012011-09-27 19:10:05 +0000402protected:
403 /**
404 * Creates an empty array that will use the passed storage block until it
405 * is insufficiently large to hold the entire array.
406 */
407 template <int N>
408 SkTArray(SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400409 this->initWithPreallocatedStorage(0, storage->get(), N);
bsalomon@google.com92669012011-09-27 19:10:05 +0000410 }
411
412 /**
413 * Copy another array, using preallocated storage if preAllocCount >=
414 * array.count(). Otherwise storage will only be used when array shrinks
415 * to fit.
416 */
417 template <int N>
418 SkTArray(const SkTArray& array, SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400419 this->initWithPreallocatedStorage(array.fCount, storage->get(), N);
bungeman0d9e9be2016-04-20 10:22:20 -0700420 this->copy(array.fItemArray);
bsalomon@google.com92669012011-09-27 19:10:05 +0000421 }
422
423 /**
Florin Malitab1d800d2017-03-09 12:17:15 -0500424 * Move another array, using preallocated storage if preAllocCount >=
425 * array.count(). Otherwise storage will only be used when array shrinks
426 * to fit.
427 */
428 template <int N>
429 SkTArray(SkTArray&& array, SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400430 this->initWithPreallocatedStorage(array.fCount, storage->get(), N);
Florin Malitab1d800d2017-03-09 12:17:15 -0500431 array.move(fMemArray);
432 array.fCount = 0;
433 }
434
435 /**
bsalomon@google.com92669012011-09-27 19:10:05 +0000436 * Copy a C array, using preallocated storage if preAllocCount >=
437 * count. Otherwise storage will only be used when array shrinks
438 * to fit.
439 */
440 template <int N>
441 SkTArray(const T* array, int count, SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400442 this->initWithPreallocatedStorage(count, storage->get(), N);
bungeman0d9e9be2016-04-20 10:22:20 -0700443 this->copy(array);
bsalomon@google.com92669012011-09-27 19:10:05 +0000444 }
445
Brian Salomon69225d02017-03-15 20:52:35 -0400446private:
447 void init(int count = 0, int reserveCount = 0) {
junov@chromium.orgd80a5092011-11-30 18:35:19 +0000448 SkASSERT(count >= 0);
Brian Salomon69225d02017-03-15 20:52:35 -0400449 SkASSERT(reserveCount >= 0);
450 fCount = count;
451 if (!count && !reserveCount) {
452 fAllocCount = 0;
453 fMemArray = nullptr;
Jim Van Verth474d6872017-12-14 13:00:05 -0500454 fOwnMemory = true;
Brian Salomon610842a2017-06-16 06:47:30 -0400455 fReserved = false;
bsalomon@google.com92669012011-09-27 19:10:05 +0000456 } else {
Brian Salomon69225d02017-03-15 20:52:35 -0400457 fAllocCount = SkTMax(count, SkTMax(kMinHeapAllocCount, reserveCount));
Mike Reed8dc8dbc2018-01-05 11:20:10 -0500458 fMemArray = sk_malloc_throw(fAllocCount, sizeof(T));
Brian Salomon69225d02017-03-15 20:52:35 -0400459 fOwnMemory = true;
Brian Salomon610842a2017-06-16 06:47:30 -0400460 fReserved = reserveCount > 0;
bsalomon@google.com92669012011-09-27 19:10:05 +0000461 }
bsalomon@google.com92669012011-09-27 19:10:05 +0000462 }
463
Brian Salomon69225d02017-03-15 20:52:35 -0400464 void initWithPreallocatedStorage(int count, void* preallocStorage, int preallocCount) {
465 SkASSERT(count >= 0);
466 SkASSERT(preallocCount > 0);
467 SkASSERT(preallocStorage);
468 fCount = count;
469 fMemArray = nullptr;
Brian Salomon610842a2017-06-16 06:47:30 -0400470 fReserved = false;
Brian Salomon69225d02017-03-15 20:52:35 -0400471 if (count > preallocCount) {
472 fAllocCount = SkTMax(count, kMinHeapAllocCount);
Mike Reed8dc8dbc2018-01-05 11:20:10 -0500473 fMemArray = sk_malloc_throw(fAllocCount, sizeof(T));
Brian Salomon69225d02017-03-15 20:52:35 -0400474 fOwnMemory = true;
475 } else {
476 fAllocCount = preallocCount;
477 fMemArray = preallocStorage;
478 fOwnMemory = false;
479 }
480 }
481
bungeman918090c2016-02-09 09:14:28 -0800482 /** In the following move and copy methods, 'dst' is assumed to be uninitialized raw storage.
483 * In the following move methods, 'src' is destroyed leaving behind uninitialized raw storage.
484 */
Florin Malitac2d5bd02017-03-09 13:34:09 -0500485 void copy(const T* src) {
486 // Some types may be trivially copyable, in which case we *could* use memcopy; but
487 // MEM_MOVE == true implies that the type is trivially movable, and not necessarily
488 // trivially copyable (think sk_sp<>). So short of adding another template arg, we
489 // must be conservative and use copy construction.
Mike Kleinc1db6102018-10-10 21:01:37 +0000490 for (int i = 0; i < fCount; ++i) {
491 new (fItemArray + i) T(src[i]);
492 }
bungeman918090c2016-02-09 09:14:28 -0800493 }
Florin Malitac2d5bd02017-03-09 13:34:09 -0500494
495 template <bool E = MEM_MOVE> SK_WHEN(E, void) move(int dst, int src) {
496 memcpy(&fItemArray[dst], &fItemArray[src], sizeof(T));
497 }
498 template <bool E = MEM_MOVE> SK_WHEN(E, void) move(void* dst) {
499 sk_careful_memcpy(dst, fMemArray, fCount * sizeof(T));
500 }
501
502 template <bool E = MEM_MOVE> SK_WHEN(!E, void) move(int dst, int src) {
bungeman918090c2016-02-09 09:14:28 -0800503 new (&fItemArray[dst]) T(std::move(fItemArray[src]));
504 fItemArray[src].~T();
505 }
Florin Malitac2d5bd02017-03-09 13:34:09 -0500506 template <bool E = MEM_MOVE> SK_WHEN(!E, void) move(void* dst) {
Mike Kleinc1db6102018-10-10 21:01:37 +0000507 for (int i = 0; i < fCount; ++i) {
508 new (static_cast<char*>(dst) + sizeof(T) * i) T(std::move(fItemArray[i]));
509 fItemArray[i].~T();
510 }
bungeman918090c2016-02-09 09:14:28 -0800511 }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000512
Brian Salomon69225d02017-03-15 20:52:35 -0400513 static constexpr int kMinHeapAllocCount = 8;
Mike Reed6c14c8d2017-03-09 16:36:26 -0500514
bsalomon@google.comd5104142013-06-13 15:13:46 +0000515 // Helper function that makes space for n objects, adjusts the count, but does not initialize
516 // the new objects.
Mike Kleinc1db6102018-10-10 21:01:37 +0000517 void* push_back_raw(int n) {
bsalomon@google.comd5104142013-06-13 15:13:46 +0000518 this->checkRealloc(n);
Mike Kleinc1db6102018-10-10 21:01:37 +0000519 void* ptr = fItemArray + fCount;
bsalomon@google.comd5104142013-06-13 15:13:46 +0000520 fCount += n;
521 return ptr;
522 }
523
Brian Salomon69225d02017-03-15 20:52:35 -0400524 void checkRealloc(int delta) {
bsalomon@google.com49313f62011-09-14 13:54:05 +0000525 SkASSERT(fCount >= 0);
526 SkASSERT(fAllocCount >= 0);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000527 SkASSERT(-delta <= fCount);
528
Mike Reedcab25492018-05-10 10:55:43 -0400529 // Move into 64bit math temporarily, to avoid local overflows
530 int64_t newCount = fCount + delta;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000531
Brian Salomon69225d02017-03-15 20:52:35 -0400532 // We allow fAllocCount to be in the range [newCount, 3*newCount]. We also never shrink
Brian Salomon610842a2017-06-16 06:47:30 -0400533 // when we're currently using preallocated memory, would allocate less than
534 // kMinHeapAllocCount, or a reserve count was specified that has yet to be exceeded.
Brian Salomon69225d02017-03-15 20:52:35 -0400535 bool mustGrow = newCount > fAllocCount;
Brian Salomon610842a2017-06-16 06:47:30 -0400536 bool shouldShrink = fAllocCount > 3 * newCount && fOwnMemory && !fReserved;
Brian Salomon69225d02017-03-15 20:52:35 -0400537 if (!mustGrow && !shouldShrink) {
538 return;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000539 }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000540
Mike Reedcab25492018-05-10 10:55:43 -0400541
Brian Salomon69225d02017-03-15 20:52:35 -0400542 // Whether we're growing or shrinking, we leave at least 50% extra space for future growth.
Mike Reedcab25492018-05-10 10:55:43 -0400543 int64_t newAllocCount = newCount + ((newCount + 1) >> 1);
Brian Salomon69225d02017-03-15 20:52:35 -0400544 // Align the new allocation count to kMinHeapAllocCount.
545 static_assert(SkIsPow2(kMinHeapAllocCount), "min alloc count not power of two.");
546 newAllocCount = (newAllocCount + (kMinHeapAllocCount - 1)) & ~(kMinHeapAllocCount - 1);
547 // At small sizes the old and new alloc count can both be kMinHeapAllocCount.
548 if (newAllocCount == fAllocCount) {
549 return;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000550 }
Mike Reedcab25492018-05-10 10:55:43 -0400551
552 fAllocCount = Sk64_pin_to_s32(newAllocCount);
553 SkASSERT(fAllocCount >= newCount);
Mike Reed8dc8dbc2018-01-05 11:20:10 -0500554 void* newMemArray = sk_malloc_throw(fAllocCount, sizeof(T));
Brian Salomon69225d02017-03-15 20:52:35 -0400555 this->move(newMemArray);
556 if (fOwnMemory) {
557 sk_free(fMemArray);
558
559 }
560 fMemArray = newMemArray;
561 fOwnMemory = true;
Brian Salomon610842a2017-06-16 06:47:30 -0400562 fReserved = false;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000563 }
564
bsalomon@google.com49313f62011-09-14 13:54:05 +0000565 union {
566 T* fItemArray;
567 void* fMemArray;
568 };
Brian Salomon610842a2017-06-16 06:47:30 -0400569 int fCount;
570 int fAllocCount;
571 bool fOwnMemory : 1;
572 bool fReserved : 1;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000573};
574
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400575template <typename T, bool M> static inline void swap(SkTArray<T, M>& a, SkTArray<T, M>& b) {
576 a.swap(b);
577}
578
Brian Salomon69225d02017-03-15 20:52:35 -0400579template<typename T, bool MEM_MOVE> constexpr int SkTArray<T, MEM_MOVE>::kMinHeapAllocCount;
580
bsalomon@google.com92669012011-09-27 19:10:05 +0000581/**
582 * Subclass of SkTArray that contains a preallocated memory block for the array.
583 */
Florin Malitac2d5bd02017-03-09 13:34:09 -0500584template <int N, typename T, bool MEM_MOVE= false>
585class SkSTArray : public SkTArray<T, MEM_MOVE> {
bsalomon@google.com92669012011-09-27 19:10:05 +0000586private:
Florin Malitac2d5bd02017-03-09 13:34:09 -0500587 typedef SkTArray<T, MEM_MOVE> INHERITED;
bsalomon@google.com92669012011-09-27 19:10:05 +0000588
589public:
590 SkSTArray() : INHERITED(&fStorage) {
591 }
592
593 SkSTArray(const SkSTArray& array)
594 : INHERITED(array, &fStorage) {
595 }
596
Florin Malitab1d800d2017-03-09 12:17:15 -0500597 SkSTArray(SkSTArray&& array)
598 : INHERITED(std::move(array), &fStorage) {
599 }
600
bsalomon@google.com92669012011-09-27 19:10:05 +0000601 explicit SkSTArray(const INHERITED& array)
602 : INHERITED(array, &fStorage) {
603 }
604
Florin Malitab1d800d2017-03-09 12:17:15 -0500605 explicit SkSTArray(INHERITED&& array)
606 : INHERITED(std::move(array), &fStorage) {
607 }
608
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000609 explicit SkSTArray(int reserveCount)
610 : INHERITED(reserveCount) {
611 }
612
bsalomon@google.com92669012011-09-27 19:10:05 +0000613 SkSTArray(const T* array, int count)
614 : INHERITED(array, count, &fStorage) {
615 }
616
Brian Salomon69225d02017-03-15 20:52:35 -0400617 SkSTArray& operator=(const SkSTArray& array) {
Florin Malitad54639f2017-03-12 10:40:13 -0400618 INHERITED::operator=(array);
619 return *this;
620 }
621
Brian Salomon69225d02017-03-15 20:52:35 -0400622 SkSTArray& operator=(SkSTArray&& array) {
Florin Malitad54639f2017-03-12 10:40:13 -0400623 INHERITED::operator=(std::move(array));
624 return *this;
bsalomon@google.com92669012011-09-27 19:10:05 +0000625 }
626
Brian Salomon69225d02017-03-15 20:52:35 -0400627 SkSTArray& operator=(const INHERITED& array) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000628 INHERITED::operator=(array);
629 return *this;
630 }
631
Brian Salomon69225d02017-03-15 20:52:35 -0400632 SkSTArray& operator=(INHERITED&& array) {
Florin Malitad54639f2017-03-12 10:40:13 -0400633 INHERITED::operator=(std::move(array));
634 return *this;
635 }
636
bsalomon@google.com92669012011-09-27 19:10:05 +0000637private:
638 SkAlignedSTStorage<N,T> fStorage;
639};
640
bsalomon@google.com49313f62011-09-14 13:54:05 +0000641#endif