Debian Codegraph @

include/private/base/SkTArray.h

raw
1 /*
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
8 #ifndef SkTArray_DEFINED
9 #define SkTArray_DEFINED
10
11 #include "include/private/base/SkASAN.h" // IWYU pragma: keep
12 #include "include/private/base/SkAlignedStorage.h"
13 #include "include/private/base/SkAssert.h"
14 #include "include/private/base/SkAttributes.h"
15 #include "include/private/base/SkContainers.h"
16 #include "include/private/base/SkDebug.h"
17 #include "include/private/base/SkMalloc.h"
18 #include "include/private/base/SkMath.h"
19 #include "include/private/base/SkSpan_impl.h"
20 #include "include/private/base/SkTo.h"
21 #include "include/private/base/SkTypeTraits.h" // IWYU pragma: keep
22
23 #include <algorithm>
24 #include <climits>
25 #include <cstddef>
26 #include <cstdint>
27 #include <cstring>
28 #include <initializer_list>
29 #include <new>
30 #include <utility>
31
32 namespace skia_private {
33 /** TArray<T> implements a typical, mostly std::vector-like array.
34 Each T will be default-initialized on allocation, and ~T will be called on destruction.
35
36 MEM_MOVE controls the behavior when a T needs to be moved (e.g. when the array is resized)
37 - true: T will be bit-copied via memcpy.
38 - false: T will be moved via move-constructors.
39 */
40 template <typename T, bool MEM_MOVE = sk_is_trivially_relocatable_v<T>> class TArray {
41 public:
42 using value_type = T;
43
44 /**
45 * Creates an empty array with no initial storage
46 */
47 TArray() : fOwnMemory(true), fCapacity{0} {}
48
49 /**
50 * Creates an empty array that will preallocate space for reserveCount elements.
51 */
52 explicit TArray(int reserveCount) : TArray() { this->reserve_exact(reserveCount); }
53
54 /**
55 * Copies one array to another. The new array will be heap allocated.
56 */
57 TArray(const TArray& that) : TArray(that.fData, that.fSize) {}
58
59 TArray(TArray&& that) {
60 if (that.fOwnMemory) {
61 this->setData(that);
62 that.setData({});
63 } else {
64 this->initData(that.fSize);
65 that.move(fData);
66 }
67 this->changeSize(that.fSize);
68 that.changeSize(0);
69 }
70
71 /**
72 * Creates a TArray by copying contents of a standard C array. The new
73 * array will be heap allocated. Be careful not to use this constructor
74 * when you really want the (void*, int) version.
75 */
76 TArray(const T* array, int count) {
77 this->initData(count);
78 this->copy(array);
79 }
80
81 /**
82 * Creates a TArray by copying contents from an SkSpan. The new array will be heap allocated.
83 */
84 TArray(SkSpan<const T> data) : TArray(data.data(), static_cast<int>(data.size())) {}
85
86 /**
87 * Creates a TArray by copying contents of an initializer list.
88 */
89 TArray(std::initializer_list<T> data) : TArray(data.begin(), data.size()) {}
90
91 TArray& operator=(const TArray& that) {
92 if (this == &that) {
93 return *this;
94 }
95 this->clear();
96 this->checkRealloc(that.size(), kExactFit);
97 this->changeSize(that.fSize);
98 this->copy(that.fData);
99 return *this;
100 }
101
102 TArray& operator=(TArray&& that) {
103 if (this != &that) {
104 this->clear();
105 this->unpoison();
106 that.unpoison();
107 if (that.fOwnMemory) {
108 // The storage is on the heap, so move the data pointer.
109 if (fOwnMemory) {
110 sk_free(fData);
111 }
112
113 fData = std::exchange(that.fData, nullptr);
114
115 // Can't use exchange with bitfields.
116 fCapacity = that.fCapacity;
117 that.fCapacity = 0;
118
119 fOwnMemory = true;
120
121 this->changeSize(that.fSize);
122 } else {
123 // The data is stored inline in that, so move it element-by-element.
124 this->checkRealloc(that.size(), kExactFit);
125 this->changeSize(that.fSize);
126 that.move(fData);
127 }
128 that.changeSize(0);
129 }
130 return *this;
131 }
132
133 ~TArray() {
134 this->destroyAll();
135 this->unpoison();
136 if (fOwnMemory) {
137 sk_free(fData);
138 }
139 }
140
141 /**
142 * Resets to size() = n newly constructed T objects and resets any reserve count.
143 */
144 void reset(int n) {
145 SkASSERT(n >= 0);
146 this->clear();
147 this->checkRealloc(n, kExactFit);
148 this->changeSize(n);
149 for (int i = 0; i < this->size(); ++i) {
150 new (fData + i) T;
151 }
152 }
153
154 /**
155 * Resets to a copy of a C array and resets any reserve count.
156 */
157 void reset(SkSpan<const T> src) {
158 this->clear();
159 this->checkRealloc(src.size(), kExactFit);
160 this->changeSize(src.size());
161 this->copy(src.data());
162 }
163
164 /**
165 * Ensures there is enough reserved space for at least n elements. This is guaranteed at least
166 * until the array size grows above n and subsequently shrinks below n, any version of reset()
167 * is called, or reserve() is called again.
168 */
169 void reserve(int n) {
170 SkASSERT(n >= 0);
171 if (n > this->size()) {
172 this->checkRealloc(n - this->size(), kGrowing);
173 }
174 }
175
176 /**
177 * Ensures there is enough reserved space for exactly n elements. The same capacity guarantees
178 * as above apply.
179 */
180 void reserve_exact(int n) {
181 SkASSERT(n >= 0);
182 if (n > this->size()) {
183 this->checkRealloc(n - this->size(), kExactFit);
184 }
185 }
186
187 void removeShuffle(int n) {
188 SkASSERT(n < this->size());
189 int newCount = fSize - 1;
190 fData[n].~T();
191 if (n != newCount) {
192 this->move(n, newCount);
193 }
194 this->changeSize(newCount);
195 }
196
197 // Is the array empty.
198 bool empty() const { return fSize == 0; }
199
200 /**
201 * Adds one new default-initialized T value and returns it by reference. Note that the reference
202 * only remains valid until the next call that adds or removes elements.
203 */
204 T& push_back() {
205 void* newT = this->push_back_raw(1);
206 return *new (newT) T;
207 }
208
209 /**
210 * Adds one new T value which is copy-constructed, returning it by reference. As always,
211 * the reference only remains valid until the next call that adds or removes elements.
212 */
213 T& push_back(const T& t) {
214 this->unpoison();
215 T* newT;
216 if (this->capacity() > fSize) SK_LIKELY {
217 // Copy over the element directly.
218 newT = new (fData + fSize) T(t);
219 } else {
220 newT = this->growAndConstructAtEnd(t);
221 }
222
223 this->changeSize(fSize + 1);
224 return *newT;
225 }
226
227 /**
228 * Adds one new T value which is copy-constructed, returning it by reference.
229 */
230 T& push_back(T&& t) {
231 this->unpoison();
232 T* newT;
233 if (this->capacity() > fSize) SK_LIKELY {
234 // Move over the element directly.
235 newT = new (fData + fSize) T(std::move(t));
236 } else {
237 newT = this->growAndConstructAtEnd(std::move(t));
238 }
239
240 this->changeSize(fSize + 1);
241 return *newT;
242 }
243
244 /**
245 * Constructs a new T at the back of this array, returning it by reference.
246 */
247 template <typename... Args> T& emplace_back(Args&&... args) {
248 this->unpoison();
249 T* newT;
250 if (this->capacity() > fSize) SK_LIKELY {
251 // Emplace the new element in directly.
252 newT = new (fData + fSize) T(std::forward<Args>(args)...);
253 } else {
254 newT = this->growAndConstructAtEnd(std::forward<Args>(args)...);
255 }
256
257 this->changeSize(fSize + 1);
258 return *newT;
259 }
260
261 /**
262 * Allocates n more default-initialized T values, and returns the address of
263 * the start of that new range. Note: this address is only valid until the
264 * next API call made on the array that might add or remove elements.
265 */
266 T* push_back_n(int n) {
267 SkASSERT(n >= 0);
268 T* newTs = TCast(this->push_back_raw(n));
269 for (int i = 0; i < n; ++i) {
270 new (&newTs[i]) T;
271 }
272 return newTs;
273 }
274
275 /**
276 * Version of above that uses a copy constructor to initialize all n items
277 * to the same T.
278 */
279 T* push_back_n(int n, const T& t) {
280 SkASSERT(n >= 0);
281 T* newTs = TCast(this->push_back_raw(n));
282 for (int i = 0; i < n; ++i) {
283 new (&newTs[i]) T(t);
284 }
285 return static_cast<T*>(newTs);
286 }
287
288 /**
289 * Version of above that uses a copy constructor to initialize the n items
290 * to separate T values.
291 */
292 T* push_back_n(int n, const T t[]) {
293 SkASSERT(n >= 0);
294 this->checkRealloc(n, kGrowing);
295 T* end = this->end();
296 this->changeSize(fSize + n);
297 for (int i = 0; i < n; ++i) {
298 new (end + i) T(t[i]);
299 }
300 return end;
301 }
302
303 /**
304 * Version of above that uses the move constructor to set n items.
305 */
306 T* move_back_n(int n, T* t) {
307 SkASSERT(n >= 0);
308 this->checkRealloc(n, kGrowing);
309 T* end = this->end();
310 this->changeSize(fSize + n);
311 for (int i = 0; i < n; ++i) {
312 new (end + i) T(std::move(t[i]));
313 }
314 return end;
315 }
316
317 /**
318 * Removes the last element. Not safe to call when size() == 0.
319 */
320 void pop_back() {
321 sk_collection_not_empty(this->empty());
322 fData[fSize - 1].~T();
323 this->changeSize(fSize - 1);
324 }
325
326 /**
327 * Removes the last n elements. Not safe to call when size() < n.
328 */
329 void pop_back_n(int n) {
330 SkASSERT(n >= 0);
331 SkASSERT(this->size() >= n);
332 int i = fSize;
333 while (i-- > fSize - n) {
334 (*this)[i].~T();
335 }
336 this->changeSize(fSize - n);
337 }
338
339 /**
340 * Pushes or pops from the back to resize. Pushes will be default initialized.
341 */
342 void resize_back(int newCount) {
343 SkASSERT(newCount >= 0);
344 if (newCount > this->size()) {
345 if (this->empty()) {
346 // When the container is completely empty, grow to exactly the requested size.
347 this->checkRealloc(newCount, kExactFit);
348 }
349 this->push_back_n(newCount - fSize);
350 } else if (newCount < this->size()) {
351 this->pop_back_n(fSize - newCount);
352 }
353 }
354
355 /** Swaps the contents of this array with that array. Does a pointer swap if possible,
356 otherwise copies the T values. */
357 void swap(TArray& that) {
358 using std::swap;
359 if (this == &that) {
360 return;
361 }
362 if (fOwnMemory && that.fOwnMemory) {
363 swap(fData, that.fData);
364 swap(fSize, that.fSize);
365
366 // Can't use swap because fCapacity is a bit field.
367 auto allocCount = fCapacity;
368 fCapacity = that.fCapacity;
369 that.fCapacity = allocCount;
370 } else {
371 // This could be more optimal...
372 TArray copy(std::move(that));
373 that = std::move(*this);
374 *this = std::move(copy);
375 }
376 }
377
378 /**
379 * Moves all elements of `that` to the end of this array, leaving `that` empty.
380 * This is a no-op if `that` is empty or equal to this array.
381 */
382 void move_back(TArray& that) {
383 if (that.empty() || &that == this) {
384 return;
385 }
386 void* dst = this->push_back_raw(that.size());
387 // After move() returns, the contents of `dst` will have either been in-place initialized
388 // using a the move constructor (per-item from `that`'s elements), or will have been
389 // mem-copied into when MEM_MOVE is true (now valid objects).
390 that.move(dst);
391 // All items in `that` have either been destroyed (when MEM_MOVE is false) or should be
392 // considered invalid (when MEM_MOVE is true). Reset fSize to 0 directly to skip any further
393 // per-item destruction.
394 that.changeSize(0);
395 }
396
397 T* begin() {
398 return fData;
399 }
400 const T* begin() const {
401 return fData;
402 }
403
404 // It's safe to use fItemArray + fSize because if fItemArray is nullptr then adding 0 is
405 // valid and returns nullptr. See [expr.add] in the C++ standard.
406 T* end() {
407 if (fData == nullptr) {
408 SkASSERT(fSize == 0);
409 }
410 return fData + fSize;
411 }
412 const T* end() const {
413 if (fData == nullptr) {
414 SkASSERT(fSize == 0);
415 }
416 return fData + fSize;
417 }
418 T* data() { return fData; }
419 const T* data() const { return fData; }
420 int size() const { return fSize; }
421 size_t size_bytes() const { return Bytes(fSize); }
422 void resize(size_t count) { this->resize_back((int)count); }
423
424 void clear() {
425 this->destroyAll();
426 this->changeSize(0);
427 }
428
429 void shrink_to_fit() {
430 if (!fOwnMemory || fSize == fCapacity) {
431 return;
432 }
433 this->unpoison();
434 if (fSize == 0) {
435 sk_free(fData);
436 fData = nullptr;
437 fCapacity = 0;
438 } else {
439 SkSpan<std::byte> allocation = Allocate(fSize);
440 this->move(TCast(allocation.data()));
441 if (fOwnMemory) {
442 sk_free(fData);
443 }
444 // Poison is applied in `setDataFromBytes`.
445 this->setDataFromBytes(allocation);
446 }
447 }
448
449 /**
450 * Get the i^th element.
451 */
452 T& operator[] (int i) {
453 return fData[sk_collection_check_bounds(i, this->size())];
454 }
455
456 const T& operator[] (int i) const {
457 return fData[sk_collection_check_bounds(i, this->size())];
458 }
459
460 T& at(int i) { return (*this)[i]; }
461 const T& at(int i) const { return (*this)[i]; }
462
463 /**
464 * equivalent to operator[](0)
465 */
466 T& front() {
467 sk_collection_not_empty(this->empty());
468 return fData[0];
469 }
470
471 const T& front() const {
472 sk_collection_not_empty(this->empty());
473 return fData[0];
474 }
475
476 /**
477 * equivalent to operator[](size() - 1)
478 */
479 T& back() {
480 sk_collection_not_empty(this->empty());
481 return fData[fSize - 1];
482 }
483
484 const T& back() const {
485 sk_collection_not_empty(this->empty());
486 return fData[fSize - 1];
487 }
488
489 /**
490 * equivalent to operator[](size()-1-i)
491 */
492 T& fromBack(int i) {
493 return (*this)[fSize - i - 1];
494 }
495
496 const T& fromBack(int i) const {
497 return (*this)[fSize - i - 1];
498 }
499
500 bool operator==(const TArray<T, MEM_MOVE>& right) const {
501 int leftCount = this->size();
502 if (leftCount != right.size()) {
503 return false;
504 }
505 for (int index = 0; index < leftCount; ++index) {
506 if (fData[index] != right.fData[index]) {
507 return false;
508 }
509 }
510 return true;
511 }
512
513 bool operator!=(const TArray<T, MEM_MOVE>& right) const {
514 return !(*this == right);
515 }
516
517 int capacity() const {
518 return fCapacity;
519 }
520
521 protected:
522 // Creates an empty array that will use the passed storage block until it is insufficiently
523 // large to hold the entire array.
524 template <int InitialCapacity>
525 TArray(SkAlignedSTStorage<InitialCapacity, T>* storage, int size = 0) {
526 static_assert(InitialCapacity >= 0);
527 SkASSERT(size >= 0);
528 SkASSERT(storage->get() != nullptr);
529 if (size > InitialCapacity) {
530 this->initData(size);
531 } else {
532 this->setDataFromBytes({storage->data(), storage->size()});
533 this->changeSize(size);
534
535 // setDataFromBytes always sets fOwnMemory to true, but we are actually using static
536 // storage here, which shouldn't ever be freed.
537 fOwnMemory = false;
538 }
539 }
540
541 // Copy a C array, using pre-allocated storage if preAllocCount >= count. Otherwise, storage
542 // will only be used when array shrinks to fit.
543 template <int InitialCapacity>
544 TArray(const T* array, int size, SkAlignedSTStorage<InitialCapacity, T>* storage)
545 : TArray{storage, size} {
546 this->copy(array);
547 }
548 template <int InitialCapacity>
549 TArray(SkSpan<const T> data, SkAlignedSTStorage<InitialCapacity, T>* storage)
550 : TArray{storage, static_cast<int>(data.size())} {
551 this->copy(data.data());
552 }
553
554 private:
555 // Growth factors for checkRealloc.
556 static constexpr double kExactFit = 1.0;
557 static constexpr double kGrowing = 1.5;
558
559 static constexpr int kMinHeapAllocCount = 8;
560 static_assert(SkIsPow2(kMinHeapAllocCount), "min alloc count not power of two.");
561
562 // Note for 32-bit machines kMaxCapacity will be <= SIZE_MAX. For 64-bit machines it will
563 // just be INT_MAX if the sizeof(T) < 2^32.
564 static constexpr int kMaxCapacity = SkToInt(std::min(SIZE_MAX / sizeof(T), (size_t)INT_MAX));
565
566 void setDataFromBytes(SkSpan<std::byte> allocation) {
567 T* data = TCast(allocation.data());
568 // We have gotten extra bytes back from the allocation limit, pin to kMaxCapacity. It
569 // would seem like the SkContainerAllocator should handle the divide, but it would have
570 // to a full divide instruction. If done here the size is known at compile, and usually
571 // can be implemented by a right shift. The full divide takes ~50X longer than the shift.
572 size_t size = std::min(allocation.size() / sizeof(T), SkToSizeT(kMaxCapacity));
573 this->setData(SkSpan<T>(data, size));
574 }
575
576 void setData(SkSpan<T> array) {
577 this->unpoison();
578
579 fData = array.data();
580 fCapacity = SkToU32(array.size());
581 fOwnMemory = true;
582
583 this->poison();
584 }
585
586 void unpoison() {
587 #ifdef SK_SANITIZE_ADDRESS
588 if (fData && fPoisoned) {
589 // SkDebugf("UNPOISONING %p : 0 -> %zu\n", fData, Bytes(fCapacity));
590 sk_asan_unpoison_memory_region(this->begin(), Bytes(fCapacity));
591 fPoisoned = false;
592 }
593 #endif
594 }
595
596 void poison() {
597 #ifdef SK_SANITIZE_ADDRESS
598 if (fData && fCapacity > SkToUInt(fSize)) {
599 // SkDebugf(" POISONING %p : %zu -> %zu\n", fData, Bytes(fSize), Bytes(fCapacity));
600 sk_asan_poison_memory_region(this->end(), Bytes(fCapacity - fSize));
601 fPoisoned = true;
602 }
603 #endif
604 }
605
606 void changeSize(int n) {
607 this->unpoison();
608 fSize = n;
609 this->poison();
610 }
611
612 // We disable Control-Flow Integrity sanitization (go/cfi) when casting item-array buffers.
613 // CFI flags this code as dangerous because we are casting `buffer` to a T* while the buffer's
614 // contents might still be uninitialized memory. When T has a vtable, this is especially risky
615 // because we could hypothetically access a virtual method on fItemArray and jump to an
616 // unpredictable location in memory. Of course, TArray won't actually use fItemArray in this
617 // way, and we don't want to construct a T before the user requests one. There's no real risk
618 // here, so disable CFI when doing these casts.
619 SK_NO_SANITIZE_CFI
620 static T* TCast(void* buffer) {
621 return (T*)buffer;
622 }
623
624 static size_t Bytes(int n) {
625 SkASSERT(n <= kMaxCapacity);
626 return SkToSizeT(n) * sizeof(T);
627 }
628
629 static SkSpan<std::byte> Allocate(int capacity, double growthFactor = 1.0) {
630 return SkContainerAllocator{sizeof(T), kMaxCapacity}.allocate(capacity, growthFactor);
631 }
632
633 void initData(int count) {
634 this->setDataFromBytes(Allocate(count));
635 this->changeSize(count);
636 }
637
638 void destroyAll() {
639 if (!this->empty()) {
640 T* cursor = this->begin();
641 T* const end = this->end();
642 do {
643 cursor->~T();
644 cursor++;
645 } while (cursor < end);
646 }
647 }
648
649 /** In the following move and copy methods, 'dst' is assumed to be uninitialized raw storage.
650 * In the following move methods, 'src' is destroyed leaving behind uninitialized raw storage.
651 */
652 void copy(const T* src) {
653 if constexpr (std::is_trivially_copyable_v<T>) {
654 if (!this->empty() && src != nullptr) {
655 sk_careful_memcpy(fData, src, this->size_bytes());
656 }
657 } else {
658 for (int i = 0; i < this->size(); ++i) {
659 new (fData + i) T(src[i]);
660 }
661 }
662 }
663
664 void move(int dst, int src) {
665 if constexpr (MEM_MOVE) {
666 memcpy(static_cast<void*>(&fData[dst]),
667 static_cast<const void*>(&fData[src]),
668 sizeof(T));
669 } else {
670 new (&fData[dst]) T(std::move(fData[src]));
671 fData[src].~T();
672 }
673 }
674
675 void move(void* dst) {
676 if constexpr (MEM_MOVE) {
677 sk_careful_memcpy(dst, fData, Bytes(fSize));
678 } else {
679 for (int i = 0; i < this->size(); ++i) {
680 new (static_cast<char*>(dst) + Bytes(i)) T(std::move(fData[i]));
681 fData[i].~T();
682 }
683 }
684 }
685
686 // Helper function that makes space for n objects, adjusts the count, but does not initialize
687 // the new objects.
688 void* push_back_raw(int n) {
689 this->checkRealloc(n, kGrowing);
690 void* ptr = fData + fSize;
691 this->changeSize(fSize + n);
692 return ptr;
693 }
694
695 template <typename... Args>
696 SK_ALWAYS_INLINE T* growAndConstructAtEnd(Args&&... args) {
697 SkSpan<std::byte> buffer = this->preallocateNewData(/*delta=*/1, kGrowing);
698 T* newT = new (TCast(buffer.data()) + fSize) T(std::forward<Args>(args)...);
699 this->installDataAndUpdateCapacity(buffer);
700
701 return newT;
702 }
703
704 void checkRealloc(int delta, double growthFactor) {
705 SkASSERT(delta >= 0);
706 SkASSERT(fSize >= 0);
707 SkASSERT(fCapacity >= 0);
708
709 // Check if there are enough remaining allocated elements to satisfy the request.
710 if (this->capacity() - fSize < delta) {
711 // Looks like we need to reallocate.
712 this->installDataAndUpdateCapacity(this->preallocateNewData(delta, growthFactor));
713 }
714 }
715
716 SkSpan<std::byte> preallocateNewData(int delta, double growthFactor) {
717 SkASSERT(delta >= 0);
718 SkASSERT(fSize >= 0);
719 SkASSERT(fCapacity >= 0);
720
721 // Don't overflow fSize or size_t later in the memory allocation. Overflowing memory
722 // allocation really only applies to fSizes on 32-bit machines; on 64-bit machines this
723 // will probably never produce a check. Since kMaxCapacity is bounded above by INT_MAX,
724 // this also checks the bounds of fSize.
725 if (delta > kMaxCapacity - fSize) {
726 sk_report_container_overflow_and_die();
727 }
728 const int newCount = fSize + delta;
729
730 return Allocate(newCount, growthFactor);
731 }
732
733 void installDataAndUpdateCapacity(SkSpan<std::byte> allocation) {
734 this->move(TCast(allocation.data()));
735 if (fOwnMemory) {
736 sk_free(fData);
737 }
738 this->setDataFromBytes(allocation);
739 SkASSERT(fData != nullptr);
740 }
741
742 T* fData{nullptr};
743 int fSize{0};
744 uint32_t fOwnMemory : 1;
745 uint32_t fCapacity : 31;
746 #ifdef SK_SANITIZE_ADDRESS
747 bool fPoisoned = false;
748 #endif
749 };
750
751 template <typename T, bool M> static inline void swap(TArray<T, M>& a, TArray<T, M>& b) {
752 a.swap(b);
753 }
754
755 // Subclass of TArray that contains a pre-allocated memory block for the array.
756 template <int Nreq, typename T, bool MEM_MOVE = sk_is_trivially_relocatable_v<T>>
757 class STArray : private SkAlignedSTStorage<SkContainerAllocator::RoundUp<T>(Nreq), T>,
758 public TArray<T, MEM_MOVE> {
759 // We round up the requested array size to the next capacity multiple.
760 // This space would likely otherwise go to waste.
761 static constexpr int N = SkContainerAllocator::RoundUp<T>(Nreq);
762 static_assert(Nreq > 0);
763 static_assert(N >= Nreq);
764
765 using Storage = SkAlignedSTStorage<N,T>;
766
767 public:
768 STArray()
769 : Storage{}
770 , TArray<T, MEM_MOVE>(this) {} // Must use () to avoid confusion with initializer_list
771 // when T=bool because * are convertable to bool.
772
773 STArray(const T* array, int count)
774 : Storage{}
775 , TArray<T, MEM_MOVE>{array, count, this} {}
776
777 STArray(SkSpan<const T> data)
778 : Storage{}
779 , TArray<T, MEM_MOVE>{data, this} {}
780
781 STArray(std::initializer_list<T> data)
782 : STArray{data.begin(), SkToInt(data.size())} {}
783
784 explicit STArray(int reserveCount)
785 : STArray() { this->reserve_exact(reserveCount); }
786
787 STArray(const STArray& that)
788 : STArray() { *this = that; }
789
790 explicit STArray(const TArray<T, MEM_MOVE>& that)
791 : STArray() { *this = that; }
792
793 STArray(STArray&& that)
794 : STArray() { *this = std::move(that); }
795
796 explicit STArray(TArray<T, MEM_MOVE>&& that)
797 : STArray() { *this = std::move(that); }
798
799 STArray& operator=(const STArray& that) {
800 TArray<T, MEM_MOVE>::operator=(that);
801 return *this;
802 }
803
804 STArray& operator=(const TArray<T, MEM_MOVE>& that) {
805 TArray<T, MEM_MOVE>::operator=(that);
806 return *this;
807 }
808
809 STArray& operator=(STArray&& that) {
810 TArray<T, MEM_MOVE>::operator=(std::move(that));
811 return *this;
812 }
813
814 STArray& operator=(TArray<T, MEM_MOVE>&& that) {
815 TArray<T, MEM_MOVE>::operator=(std::move(that));
816 return *this;
817 }
818
819 // Force the use of TArray for data() and size().
820 using TArray<T, MEM_MOVE>::data;
821 using TArray<T, MEM_MOVE>::size;
822 };
823 } // namespace skia_private
824 #endif // SkTArray_DEFINED