blob: 9e0b954fbff1101fd4d15a762da0c006e1e68783 [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
bungeman918090c2016-02-09 09:14:28 -080011#include "../private/SkTLogic.h"
bungemanf3c15b72015-08-19 11:56:48 -070012#include "../private/SkTemplates.h"
bsalomon@google.com49313f62011-09-14 13:54:05 +000013#include "SkTypes.h"
bungemanf3c15b72015-08-19 11:56:48 -070014
15#include <new>
bungeman221524d2016-01-05 14:59:40 -080016#include <utility>
bsalomon@google.com49313f62011-09-14 13:54:05 +000017
Florin Malitac2d5bd02017-03-09 13:34:09 -050018/** When MEM_MOVE is true T will be bit copied when moved.
19 When MEM_MOVE is false, T will be copy constructed / destructed.
bungeman@google.comd58a8562014-03-24 15:55:01 +000020 In all cases T will be default-initialized on allocation,
bungeman@google.coma12cc7f2011-10-07 20:54:15 +000021 and its destructor will be called from this object's destructor.
22*/
Florin Malitac2d5bd02017-03-09 13:34:09 -050023template <typename T, bool MEM_MOVE = false> class SkTArray {
bsalomon@google.com49313f62011-09-14 13:54:05 +000024public:
25 /**
26 * Creates an empty array with no initial storage
27 */
Brian Salomon69225d02017-03-15 20:52:35 -040028 SkTArray() { this->init(); }
bsalomon@google.com49313f62011-09-14 13:54:05 +000029
30 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000031 * Creates an empty array that will preallocate space for reserveCount
bsalomon@google.com49313f62011-09-14 13:54:05 +000032 * elements.
33 */
Brian Salomon69225d02017-03-15 20:52:35 -040034 explicit SkTArray(int reserveCount) { this->init(0, reserveCount); }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000035
bsalomon@google.com49313f62011-09-14 13:54:05 +000036 /**
37 * Copies one array to another. The new array will be heap allocated.
38 */
bungeman0d9e9be2016-04-20 10:22:20 -070039 explicit SkTArray(const SkTArray& that) {
Brian Salomon69225d02017-03-15 20:52:35 -040040 this->init(that.fCount);
bungeman0d9e9be2016-04-20 10:22:20 -070041 this->copy(that.fItemArray);
42 }
Brian Salomon69225d02017-03-15 20:52:35 -040043
bungeman0d9e9be2016-04-20 10:22:20 -070044 explicit SkTArray(SkTArray&& that) {
Brian Salomon69225d02017-03-15 20:52:35 -040045 // TODO: If 'that' owns its memory why don't we just steal the pointer?
46 this->init(that.fCount);
bungeman0d9e9be2016-04-20 10:22:20 -070047 that.move(fMemArray);
48 that.fCount = 0;
bsalomon@google.com49313f62011-09-14 13:54:05 +000049 }
50
51 /**
rmistry@google.comfbfcd562012-08-23 18:09:54 +000052 * Creates a SkTArray by copying contents of a standard C array. The new
bsalomon@google.com49313f62011-09-14 13:54:05 +000053 * array will be heap allocated. Be careful not to use this constructor
54 * when you really want the (void*, int) version.
55 */
56 SkTArray(const T* array, int count) {
Brian Salomon69225d02017-03-15 20:52:35 -040057 this->init(count);
bungeman0d9e9be2016-04-20 10:22:20 -070058 this->copy(array);
bsalomon@google.com49313f62011-09-14 13:54:05 +000059 }
60
Brian Salomon69225d02017-03-15 20:52:35 -040061 SkTArray& operator=(const SkTArray& that) {
Greg Daniel70131b92017-03-22 13:33:21 -040062 if (this == &that) {
63 return *this;
64 }
bsalomon@google.com49313f62011-09-14 13:54:05 +000065 for (int i = 0; i < fCount; ++i) {
66 fItemArray[i].~T();
67 }
68 fCount = 0;
bungeman0d9e9be2016-04-20 10:22:20 -070069 this->checkRealloc(that.count());
70 fCount = that.count();
71 this->copy(that.fItemArray);
72 return *this;
73 }
Brian Salomon69225d02017-03-15 20:52:35 -040074 SkTArray& operator=(SkTArray&& that) {
Greg Daniel70131b92017-03-22 13:33:21 -040075 if (this == &that) {
76 return *this;
77 }
bungeman0d9e9be2016-04-20 10:22:20 -070078 for (int i = 0; i < fCount; ++i) {
79 fItemArray[i].~T();
80 }
81 fCount = 0;
82 this->checkRealloc(that.count());
83 fCount = that.count();
84 that.move(fMemArray);
85 that.fCount = 0;
bsalomon@google.com49313f62011-09-14 13:54:05 +000086 return *this;
87 }
88
bsalomon383ff102015-07-31 11:53:11 -070089 ~SkTArray() {
bsalomon@google.com49313f62011-09-14 13:54:05 +000090 for (int i = 0; i < fCount; ++i) {
91 fItemArray[i].~T();
92 }
Brian Salomon69225d02017-03-15 20:52:35 -040093 if (fOwnMemory) {
bsalomon@google.com49313f62011-09-14 13:54:05 +000094 sk_free(fMemArray);
95 }
96 }
97
98 /**
Brian Salomon610842a2017-06-16 06:47:30 -040099 * Resets to count() == 0 and resets any reserve count.
bsalomon@google.com49313f62011-09-14 13:54:05 +0000100 */
Brian Salomon610842a2017-06-16 06:47:30 -0400101 void reset() {
102 this->pop_back_n(fCount);
103 fReserved = false;
104 }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000105
106 /**
Brian Salomon610842a2017-06-16 06:47:30 -0400107 * Resets to count() = n newly constructed T objects and resets any reserve count.
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000108 */
109 void reset(int n) {
110 SkASSERT(n >= 0);
111 for (int i = 0; i < fCount; ++i) {
112 fItemArray[i].~T();
113 }
bungeman0d9e9be2016-04-20 10:22:20 -0700114 // Set fCount to 0 before calling checkRealloc so that no elements are moved.
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000115 fCount = 0;
116 this->checkRealloc(n);
117 fCount = n;
118 for (int i = 0; i < fCount; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -0700119 new (fItemArray + i) T;
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000120 }
Brian Salomon610842a2017-06-16 06:47:30 -0400121 fReserved = false;
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000122 }
123
124 /**
Brian Salomon610842a2017-06-16 06:47:30 -0400125 * Resets to a copy of a C array and resets any reserve count.
jvanverth@google.com054ae992013-04-01 20:06:51 +0000126 */
127 void reset(const T* array, int count) {
128 for (int i = 0; i < fCount; ++i) {
129 fItemArray[i].~T();
130 }
bungeman0d9e9be2016-04-20 10:22:20 -0700131 fCount = 0;
132 this->checkRealloc(count);
jvanverth@google.com054ae992013-04-01 20:06:51 +0000133 fCount = count;
bungeman918090c2016-02-09 09:14:28 -0800134 this->copy(array);
Brian Salomon610842a2017-06-16 06:47:30 -0400135 fReserved = false;
136 }
137
138 /**
139 * Ensures there is enough reserved space for n additional elements. The is guaranteed at least
140 * until the array size grows above n and subsequently shrinks below n, any version of reset()
141 * is called, or reserve() is called again.
142 */
143 void reserve(int n) {
144 SkASSERT(n >= 0);
145 if (n > 0) {
146 this->checkRealloc(n);
147 fReserved = fOwnMemory;
148 } else {
149 fReserved = false;
150 }
bungeman@google.com95ebd172014-03-21 19:39:02 +0000151 }
152
153 void removeShuffle(int n) {
154 SkASSERT(n < fCount);
155 int newCount = fCount - 1;
156 fCount = newCount;
157 fItemArray[n].~T();
158 if (n != newCount) {
bungeman918090c2016-02-09 09:14:28 -0800159 this->move(n, newCount);
jvanverth@google.com054ae992013-04-01 20:06:51 +0000160 }
161 }
162
163 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000164 * Number of elements in the array.
165 */
166 int count() const { return fCount; }
167
168 /**
169 * Is the array empty.
170 */
171 bool empty() const { return !fCount; }
172
173 /**
bungeman@google.comd58a8562014-03-24 15:55:01 +0000174 * Adds 1 new default-initialized T value and returns it by reference. Note
bsalomon@google.com49313f62011-09-14 13:54:05 +0000175 * the reference only remains valid until the next call that adds or removes
176 * elements.
177 */
178 T& push_back() {
krasine0c1d282016-04-21 08:34:00 -0700179 void* newT = this->push_back_raw(1);
180 return *new (newT) T;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000181 }
182
183 /**
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000184 * Version of above that uses a copy constructor to initialize the new item
185 */
186 T& push_back(const T& t) {
krasine0c1d282016-04-21 08:34:00 -0700187 void* newT = this->push_back_raw(1);
188 return *new (newT) T(t);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000189 }
190
191 /**
halcanary91fcb3e2016-03-04 13:53:22 -0800192 * Version of above that uses a move constructor to initialize the new item
193 */
194 T& push_back(T&& t) {
krasine0c1d282016-04-21 08:34:00 -0700195 void* newT = this->push_back_raw(1);
196 return *new (newT) T(std::move(t));
halcanary91fcb3e2016-03-04 13:53:22 -0800197 }
198
199 /**
halcanaryf12a1672015-09-23 12:45:49 -0700200 * Construct a new T at the back of this array.
201 */
202 template<class... Args> T& emplace_back(Args&&... args) {
krasine0c1d282016-04-21 08:34:00 -0700203 void* newT = this->push_back_raw(1);
bungeman221524d2016-01-05 14:59:40 -0800204 return *new (newT) T(std::forward<Args>(args)...);
halcanaryf12a1672015-09-23 12:45:49 -0700205 }
206
207 /**
bungeman@google.comd58a8562014-03-24 15:55:01 +0000208 * Allocates n more default-initialized T values, and returns the address of
209 * the start of that new range. Note: this address is only valid until the
210 * next API call made on the array that might add or remove elements.
bsalomon@google.com49313f62011-09-14 13:54:05 +0000211 */
212 T* push_back_n(int n) {
213 SkASSERT(n >= 0);
bungeman6a6f3c52016-04-21 10:52:03 -0700214 void* newTs = this->push_back_raw(n);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000215 for (int i = 0; i < n; ++i) {
bungeman6a6f3c52016-04-21 10:52:03 -0700216 new (static_cast<char*>(newTs) + i * sizeof(T)) T;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000217 }
bungeman6a6f3c52016-04-21 10:52:03 -0700218 return static_cast<T*>(newTs);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000219 }
220
221 /**
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000222 * Version of above that uses a copy constructor to initialize all n items
223 * to the same T.
224 */
225 T* push_back_n(int n, const T& t) {
226 SkASSERT(n >= 0);
bungeman6a6f3c52016-04-21 10:52:03 -0700227 void* newTs = this->push_back_raw(n);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000228 for (int i = 0; i < n; ++i) {
bungeman6a6f3c52016-04-21 10:52:03 -0700229 new (static_cast<char*>(newTs) + i * sizeof(T)) T(t);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000230 }
bungeman6a6f3c52016-04-21 10:52:03 -0700231 return static_cast<T*>(newTs);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000232 }
233
234 /**
235 * Version of above that uses a copy constructor to initialize the n items
236 * to separate T values.
237 */
238 T* push_back_n(int n, const T t[]) {
239 SkASSERT(n >= 0);
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000240 this->checkRealloc(n);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000241 for (int i = 0; i < n; ++i) {
halcanary385fe4d2015-08-26 13:07:48 -0700242 new (fItemArray + fCount + i) T(t[i]);
bsalomon@google.com4fa66942011-09-20 19:06:12 +0000243 }
244 fCount += n;
245 return fItemArray + fCount - n;
246 }
247
248 /**
msarett10e3d9b2016-08-18 15:46:03 -0700249 * Version of above that uses the move constructor to set n items.
250 */
251 T* move_back_n(int n, T* t) {
252 SkASSERT(n >= 0);
253 this->checkRealloc(n);
254 for (int i = 0; i < n; ++i) {
255 new (fItemArray + fCount + i) T(std::move(t[i]));
256 }
257 fCount += n;
258 return fItemArray + fCount - n;
259 }
260
261 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000262 * Removes the last element. Not safe to call when count() == 0.
263 */
264 void pop_back() {
265 SkASSERT(fCount > 0);
266 --fCount;
267 fItemArray[fCount].~T();
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000268 this->checkRealloc(0);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000269 }
270
271 /**
272 * Removes the last n elements. Not safe to call when count() < n.
273 */
274 void pop_back_n(int n) {
275 SkASSERT(n >= 0);
276 SkASSERT(fCount >= n);
277 fCount -= n;
278 for (int i = 0; i < n; ++i) {
bsalomon@google.comd5104142013-06-13 15:13:46 +0000279 fItemArray[fCount + i].~T();
bsalomon@google.com49313f62011-09-14 13:54:05 +0000280 }
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000281 this->checkRealloc(0);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000282 }
283
284 /**
285 * Pushes or pops from the back to resize. Pushes will be default
286 * initialized.
287 */
288 void resize_back(int newCount) {
289 SkASSERT(newCount >= 0);
290
291 if (newCount > fCount) {
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000292 this->push_back_n(newCount - fCount);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000293 } else if (newCount < fCount) {
commit-bot@chromium.orgb4a8d972013-06-05 15:40:59 +0000294 this->pop_back_n(fCount - newCount);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000295 }
296 }
297
bsalomon23e619c2015-02-06 11:54:28 -0800298 /** Swaps the contents of this array with that array. Does a pointer swap if possible,
299 otherwise copies the T values. */
300 void swap(SkTArray* that) {
bsalomon3632f842015-02-10 19:46:58 -0800301 if (this == that) {
302 return;
303 }
Brian Salomon69225d02017-03-15 20:52:35 -0400304 if (fOwnMemory && that->fOwnMemory) {
bsalomon23e619c2015-02-06 11:54:28 -0800305 SkTSwap(fItemArray, that->fItemArray);
306 SkTSwap(fCount, that->fCount);
307 SkTSwap(fAllocCount, that->fAllocCount);
308 } else {
309 // This could be more optimal...
bungeman0d9e9be2016-04-20 10:22:20 -0700310 SkTArray copy(std::move(*that));
311 *that = std::move(*this);
312 *this = std::move(copy);
bsalomon23e619c2015-02-06 11:54:28 -0800313 }
314 }
315
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000316 T* begin() {
317 return fItemArray;
318 }
319 const T* begin() const {
320 return fItemArray;
321 }
322 T* end() {
Ben Wagnera93a14a2017-08-28 10:34:05 -0400323 return fItemArray ? fItemArray + fCount : nullptr;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000324 }
325 const T* end() const {
Ben Wagnera93a14a2017-08-28 10:34:05 -0400326 return fItemArray ? fItemArray + fCount : nullptr;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000327 }
328
329 /**
bsalomon@google.com49313f62011-09-14 13:54:05 +0000330 * Get the i^th element.
331 */
332 T& operator[] (int i) {
333 SkASSERT(i < fCount);
334 SkASSERT(i >= 0);
335 return fItemArray[i];
336 }
337
338 const T& operator[] (int i) const {
339 SkASSERT(i < fCount);
340 SkASSERT(i >= 0);
341 return fItemArray[i];
342 }
343
344 /**
345 * equivalent to operator[](0)
346 */
347 T& front() { SkASSERT(fCount > 0); return fItemArray[0];}
348
349 const T& front() const { SkASSERT(fCount > 0); return fItemArray[0];}
350
351 /**
352 * equivalent to operator[](count() - 1)
353 */
354 T& back() { SkASSERT(fCount); return fItemArray[fCount - 1];}
355
356 const T& back() const { SkASSERT(fCount > 0); return fItemArray[fCount - 1];}
357
358 /**
359 * equivalent to operator[](count()-1-i)
360 */
361 T& fromBack(int i) {
362 SkASSERT(i >= 0);
363 SkASSERT(i < fCount);
364 return fItemArray[fCount - i - 1];
365 }
366
367 const T& fromBack(int i) const {
368 SkASSERT(i >= 0);
369 SkASSERT(i < fCount);
370 return fItemArray[fCount - i - 1];
371 }
372
Florin Malitac2d5bd02017-03-09 13:34:09 -0500373 bool operator==(const SkTArray<T, MEM_MOVE>& right) const {
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000374 int leftCount = this->count();
375 if (leftCount != right.count()) {
376 return false;
377 }
378 for (int index = 0; index < leftCount; ++index) {
379 if (fItemArray[index] != right.fItemArray[index]) {
380 return false;
381 }
382 }
383 return true;
384 }
385
Florin Malitac2d5bd02017-03-09 13:34:09 -0500386 bool operator!=(const SkTArray<T, MEM_MOVE>& right) const {
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000387 return !(*this == right);
388 }
389
Brian Salomon69225d02017-03-15 20:52:35 -0400390 inline int allocCntForTest() const;
391
bsalomon@google.com92669012011-09-27 19:10:05 +0000392protected:
393 /**
394 * Creates an empty array that will use the passed storage block until it
395 * is insufficiently large to hold the entire array.
396 */
397 template <int N>
398 SkTArray(SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400399 this->initWithPreallocatedStorage(0, storage->get(), N);
bsalomon@google.com92669012011-09-27 19:10:05 +0000400 }
401
402 /**
403 * Copy another array, using preallocated storage if preAllocCount >=
404 * array.count(). Otherwise storage will only be used when array shrinks
405 * to fit.
406 */
407 template <int N>
408 SkTArray(const SkTArray& array, SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400409 this->initWithPreallocatedStorage(array.fCount, storage->get(), N);
bungeman0d9e9be2016-04-20 10:22:20 -0700410 this->copy(array.fItemArray);
bsalomon@google.com92669012011-09-27 19:10:05 +0000411 }
412
413 /**
Florin Malitab1d800d2017-03-09 12:17:15 -0500414 * Move another array, using preallocated storage if preAllocCount >=
415 * array.count(). Otherwise storage will only be used when array shrinks
416 * to fit.
417 */
418 template <int N>
419 SkTArray(SkTArray&& array, SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400420 this->initWithPreallocatedStorage(array.fCount, storage->get(), N);
Florin Malitab1d800d2017-03-09 12:17:15 -0500421 array.move(fMemArray);
422 array.fCount = 0;
423 }
424
425 /**
bsalomon@google.com92669012011-09-27 19:10:05 +0000426 * Copy a C array, using preallocated storage if preAllocCount >=
427 * count. Otherwise storage will only be used when array shrinks
428 * to fit.
429 */
430 template <int N>
431 SkTArray(const T* array, int count, SkAlignedSTStorage<N,T>* storage) {
Brian Salomon69225d02017-03-15 20:52:35 -0400432 this->initWithPreallocatedStorage(count, storage->get(), N);
bungeman0d9e9be2016-04-20 10:22:20 -0700433 this->copy(array);
bsalomon@google.com92669012011-09-27 19:10:05 +0000434 }
435
Brian Salomon69225d02017-03-15 20:52:35 -0400436private:
437 void init(int count = 0, int reserveCount = 0) {
junov@chromium.orgd80a5092011-11-30 18:35:19 +0000438 SkASSERT(count >= 0);
Brian Salomon69225d02017-03-15 20:52:35 -0400439 SkASSERT(reserveCount >= 0);
440 fCount = count;
441 if (!count && !reserveCount) {
442 fAllocCount = 0;
443 fMemArray = nullptr;
444 fOwnMemory = false;
Brian Salomon610842a2017-06-16 06:47:30 -0400445 fReserved = false;
bsalomon@google.com92669012011-09-27 19:10:05 +0000446 } else {
Brian Salomon69225d02017-03-15 20:52:35 -0400447 fAllocCount = SkTMax(count, SkTMax(kMinHeapAllocCount, reserveCount));
junov@chromium.orgd80a5092011-11-30 18:35:19 +0000448 fMemArray = sk_malloc_throw(fAllocCount * sizeof(T));
Brian Salomon69225d02017-03-15 20:52:35 -0400449 fOwnMemory = true;
Brian Salomon610842a2017-06-16 06:47:30 -0400450 fReserved = reserveCount > 0;
bsalomon@google.com92669012011-09-27 19:10:05 +0000451 }
bsalomon@google.com92669012011-09-27 19:10:05 +0000452 }
453
Brian Salomon69225d02017-03-15 20:52:35 -0400454 void initWithPreallocatedStorage(int count, void* preallocStorage, int preallocCount) {
455 SkASSERT(count >= 0);
456 SkASSERT(preallocCount > 0);
457 SkASSERT(preallocStorage);
458 fCount = count;
459 fMemArray = nullptr;
Brian Salomon610842a2017-06-16 06:47:30 -0400460 fReserved = false;
Brian Salomon69225d02017-03-15 20:52:35 -0400461 if (count > preallocCount) {
462 fAllocCount = SkTMax(count, kMinHeapAllocCount);
463 fMemArray = sk_malloc_throw(fAllocCount * sizeof(T));
464 fOwnMemory = true;
465 } else {
466 fAllocCount = preallocCount;
467 fMemArray = preallocStorage;
468 fOwnMemory = false;
469 }
470 }
471
bungeman918090c2016-02-09 09:14:28 -0800472 /** In the following move and copy methods, 'dst' is assumed to be uninitialized raw storage.
473 * In the following move methods, 'src' is destroyed leaving behind uninitialized raw storage.
474 */
Florin Malitac2d5bd02017-03-09 13:34:09 -0500475 void copy(const T* src) {
476 // Some types may be trivially copyable, in which case we *could* use memcopy; but
477 // MEM_MOVE == true implies that the type is trivially movable, and not necessarily
478 // trivially copyable (think sk_sp<>). So short of adding another template arg, we
479 // must be conservative and use copy construction.
bungeman918090c2016-02-09 09:14:28 -0800480 for (int i = 0; i < fCount; ++i) {
481 new (fItemArray + i) T(src[i]);
482 }
483 }
Florin Malitac2d5bd02017-03-09 13:34:09 -0500484
485 template <bool E = MEM_MOVE> SK_WHEN(E, void) move(int dst, int src) {
486 memcpy(&fItemArray[dst], &fItemArray[src], sizeof(T));
487 }
488 template <bool E = MEM_MOVE> SK_WHEN(E, void) move(void* dst) {
489 sk_careful_memcpy(dst, fMemArray, fCount * sizeof(T));
490 }
491
492 template <bool E = MEM_MOVE> SK_WHEN(!E, void) move(int dst, int src) {
bungeman918090c2016-02-09 09:14:28 -0800493 new (&fItemArray[dst]) T(std::move(fItemArray[src]));
494 fItemArray[src].~T();
495 }
Florin Malitac2d5bd02017-03-09 13:34:09 -0500496 template <bool E = MEM_MOVE> SK_WHEN(!E, void) move(void* dst) {
bungeman918090c2016-02-09 09:14:28 -0800497 for (int i = 0; i < fCount; ++i) {
bungeman0d9e9be2016-04-20 10:22:20 -0700498 new (static_cast<char*>(dst) + sizeof(T) * i) T(std::move(fItemArray[i]));
bungeman918090c2016-02-09 09:14:28 -0800499 fItemArray[i].~T();
500 }
501 }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000502
Brian Salomon69225d02017-03-15 20:52:35 -0400503 static constexpr int kMinHeapAllocCount = 8;
Mike Reed6c14c8d2017-03-09 16:36:26 -0500504
bsalomon@google.comd5104142013-06-13 15:13:46 +0000505 // Helper function that makes space for n objects, adjusts the count, but does not initialize
506 // the new objects.
507 void* push_back_raw(int n) {
508 this->checkRealloc(n);
509 void* ptr = fItemArray + fCount;
510 fCount += n;
511 return ptr;
512 }
513
Brian Salomon69225d02017-03-15 20:52:35 -0400514 void checkRealloc(int delta) {
bsalomon@google.com49313f62011-09-14 13:54:05 +0000515 SkASSERT(fCount >= 0);
516 SkASSERT(fAllocCount >= 0);
bsalomon@google.com49313f62011-09-14 13:54:05 +0000517 SkASSERT(-delta <= fCount);
518
519 int newCount = fCount + delta;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000520
Brian Salomon69225d02017-03-15 20:52:35 -0400521 // We allow fAllocCount to be in the range [newCount, 3*newCount]. We also never shrink
Brian Salomon610842a2017-06-16 06:47:30 -0400522 // when we're currently using preallocated memory, would allocate less than
523 // kMinHeapAllocCount, or a reserve count was specified that has yet to be exceeded.
Brian Salomon69225d02017-03-15 20:52:35 -0400524 bool mustGrow = newCount > fAllocCount;
Brian Salomon610842a2017-06-16 06:47:30 -0400525 bool shouldShrink = fAllocCount > 3 * newCount && fOwnMemory && !fReserved;
Brian Salomon69225d02017-03-15 20:52:35 -0400526 if (!mustGrow && !shouldShrink) {
527 return;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000528 }
bsalomon@google.com49313f62011-09-14 13:54:05 +0000529
Brian Salomon69225d02017-03-15 20:52:35 -0400530 // Whether we're growing or shrinking, we leave at least 50% extra space for future growth.
531 int newAllocCount = newCount + ((newCount + 1) >> 1);
532 // Align the new allocation count to kMinHeapAllocCount.
533 static_assert(SkIsPow2(kMinHeapAllocCount), "min alloc count not power of two.");
534 newAllocCount = (newAllocCount + (kMinHeapAllocCount - 1)) & ~(kMinHeapAllocCount - 1);
535 // At small sizes the old and new alloc count can both be kMinHeapAllocCount.
536 if (newAllocCount == fAllocCount) {
537 return;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000538 }
Brian Salomon69225d02017-03-15 20:52:35 -0400539 fAllocCount = newAllocCount;
540 void* newMemArray = sk_malloc_throw(fAllocCount * sizeof(T));
541 this->move(newMemArray);
542 if (fOwnMemory) {
543 sk_free(fMemArray);
544
545 }
546 fMemArray = newMemArray;
547 fOwnMemory = true;
Brian Salomon610842a2017-06-16 06:47:30 -0400548 fReserved = false;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000549 }
550
bsalomon@google.com49313f62011-09-14 13:54:05 +0000551 union {
552 T* fItemArray;
553 void* fMemArray;
554 };
Brian Salomon610842a2017-06-16 06:47:30 -0400555 int fCount;
556 int fAllocCount;
557 bool fOwnMemory : 1;
558 bool fReserved : 1;
bsalomon@google.com49313f62011-09-14 13:54:05 +0000559};
560
Brian Salomon69225d02017-03-15 20:52:35 -0400561template<typename T, bool MEM_MOVE> constexpr int SkTArray<T, MEM_MOVE>::kMinHeapAllocCount;
562
bsalomon@google.com92669012011-09-27 19:10:05 +0000563/**
564 * Subclass of SkTArray that contains a preallocated memory block for the array.
565 */
Florin Malitac2d5bd02017-03-09 13:34:09 -0500566template <int N, typename T, bool MEM_MOVE= false>
567class SkSTArray : public SkTArray<T, MEM_MOVE> {
bsalomon@google.com92669012011-09-27 19:10:05 +0000568private:
Florin Malitac2d5bd02017-03-09 13:34:09 -0500569 typedef SkTArray<T, MEM_MOVE> INHERITED;
bsalomon@google.com92669012011-09-27 19:10:05 +0000570
571public:
572 SkSTArray() : INHERITED(&fStorage) {
573 }
574
575 SkSTArray(const SkSTArray& array)
576 : INHERITED(array, &fStorage) {
577 }
578
Florin Malitab1d800d2017-03-09 12:17:15 -0500579 SkSTArray(SkSTArray&& array)
580 : INHERITED(std::move(array), &fStorage) {
581 }
582
bsalomon@google.com92669012011-09-27 19:10:05 +0000583 explicit SkSTArray(const INHERITED& array)
584 : INHERITED(array, &fStorage) {
585 }
586
Florin Malitab1d800d2017-03-09 12:17:15 -0500587 explicit SkSTArray(INHERITED&& array)
588 : INHERITED(std::move(array), &fStorage) {
589 }
590
commit-bot@chromium.org3390b9a2013-10-03 15:17:58 +0000591 explicit SkSTArray(int reserveCount)
592 : INHERITED(reserveCount) {
593 }
594
bsalomon@google.com92669012011-09-27 19:10:05 +0000595 SkSTArray(const T* array, int count)
596 : INHERITED(array, count, &fStorage) {
597 }
598
Brian Salomon69225d02017-03-15 20:52:35 -0400599 SkSTArray& operator=(const SkSTArray& array) {
Florin Malitad54639f2017-03-12 10:40:13 -0400600 INHERITED::operator=(array);
601 return *this;
602 }
603
Brian Salomon69225d02017-03-15 20:52:35 -0400604 SkSTArray& operator=(SkSTArray&& array) {
Florin Malitad54639f2017-03-12 10:40:13 -0400605 INHERITED::operator=(std::move(array));
606 return *this;
bsalomon@google.com92669012011-09-27 19:10:05 +0000607 }
608
Brian Salomon69225d02017-03-15 20:52:35 -0400609 SkSTArray& operator=(const INHERITED& array) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000610 INHERITED::operator=(array);
611 return *this;
612 }
613
Brian Salomon69225d02017-03-15 20:52:35 -0400614 SkSTArray& operator=(INHERITED&& array) {
Florin Malitad54639f2017-03-12 10:40:13 -0400615 INHERITED::operator=(std::move(array));
616 return *this;
617 }
618
bsalomon@google.com92669012011-09-27 19:10:05 +0000619private:
620 SkAlignedSTStorage<N,T> fStorage;
621};
622
bsalomon@google.com49313f62011-09-14 13:54:05 +0000623#endif