Flutter Engine
The Flutter Engine
Public Member Functions | Friends | List of all members
SkTDStorage Class Reference

#include <SkTDArray.h>

Public Member Functions

 SkTDStorage (int sizeOfT)
 
 SkTDStorage (const void *src, int size, int sizeOfT)
 
 SkTDStorage (const SkTDStorage &that)
 
SkTDStorageoperator= (const SkTDStorage &that)
 
 SkTDStorage (SkTDStorage &&that)
 
SkTDStorageoperator= (SkTDStorage &&that)
 
 ~SkTDStorage ()
 
void reset ()
 
void swap (SkTDStorage &that)
 
bool empty () const
 
void clear ()
 
int size () const
 
void resize (int newSize)
 
size_t size_bytes () const
 
int capacity () const
 
void reserve (int newCapacity)
 
void shrink_to_fit ()
 
void * data ()
 
const void * data () const
 
void erase (int index, int count)
 
void removeShuffle (int index)
 
void * prepend ()
 
void append ()
 
void append (int count)
 
void * append (const void *src, int count)
 
void * insert (int index)
 
void * insert (int index, int count, const void *src)
 
void pop_back ()
 

Friends

bool operator== (const SkTDStorage &a, const SkTDStorage &b)
 
bool operator!= (const SkTDStorage &a, const SkTDStorage &b)
 

Detailed Description

Definition at line 20 of file SkTDArray.h.

Constructor & Destructor Documentation

◆ SkTDStorage() [1/4]

SkTDStorage::SkTDStorage ( int  sizeOfT)
explicit

Definition at line 21 of file SkTDArray.cpp.

21: fSizeOfT{sizeOfT} {}

◆ SkTDStorage() [2/4]

SkTDStorage::SkTDStorage ( const void *  src,
int  size,
int  sizeOfT 
)

Definition at line 23 of file SkTDArray.cpp.

24 : fSizeOfT{sizeOfT}
25 , fCapacity{size}
26 , fSize{size} {
27 if (size > 0) {
28 SkASSERT(src != nullptr);
29 size_t storageSize = this->bytes(size);
30 fStorage = static_cast<std::byte*>(sk_malloc_throw(storageSize));
31 memcpy(fStorage, src, storageSize);
32 }
33}
#define SkASSERT(cond)
Definition: SkAssert.h:116
static void * sk_malloc_throw(size_t size)
Definition: SkMalloc.h:67
int size() const
Definition: SkTDArray.h:41

◆ SkTDStorage() [3/4]

SkTDStorage::SkTDStorage ( const SkTDStorage that)

Definition at line 35 of file SkTDArray.cpp.

36 : SkTDStorage{that.fStorage, that.fSize, that.fSizeOfT} {}

◆ SkTDStorage() [4/4]

SkTDStorage::SkTDStorage ( SkTDStorage &&  that)

Definition at line 52 of file SkTDArray.cpp.

53 : fSizeOfT{that.fSizeOfT}
54 , fStorage(std::exchange(that.fStorage, nullptr))
55 , fCapacity{that.fCapacity}
56 , fSize{that.fSize} {}

◆ ~SkTDStorage()

SkTDStorage::~SkTDStorage ( )

Definition at line 66 of file SkTDArray.cpp.

66 {
67 sk_free(fStorage);
68}
SK_API void sk_free(void *)

Member Function Documentation

◆ append() [1/3]

void SkTDStorage::append ( )

Definition at line 167 of file SkTDArray.cpp.

167 {
168 if (fSize < fCapacity) {
169 fSize++;
170 } else {
171 this->insert(fSize);
172 }
173}
void * insert(int index)
Definition: SkTDArray.cpp:189

◆ append() [2/3]

void * SkTDStorage::append ( const void *  src,
int  count 
)

Definition at line 185 of file SkTDArray.cpp.

185 {
186 return this->insert(fSize, count, src);
187}
int count
Definition: FontMgrTest.cpp:50

◆ append() [3/3]

void SkTDStorage::append ( int  count)

Definition at line 175 of file SkTDArray.cpp.

175 {
176 SkASSERT(count >= 0);
177 // Read as: if (fSize + count <= fCapacity) {...}. This is a UB safe way to avoid the add.
178 if (fCapacity - fSize >= count) {
179 fSize += count;
180 } else {
181 this->insert(fSize, count, nullptr);
182 }
183}

◆ capacity()

int SkTDStorage::capacity ( ) const
inline

Definition at line 46 of file SkTDArray.h.

46{ return fCapacity; }

◆ clear()

void SkTDStorage::clear ( )
inline

Definition at line 40 of file SkTDArray.h.

40{ fSize = 0; }

◆ data() [1/2]

void * SkTDStorage::data ( )
inline

Definition at line 50 of file SkTDArray.h.

50{ return fStorage; }

◆ data() [2/2]

const void * SkTDStorage::data ( ) const
inline

Definition at line 51 of file SkTDArray.h.

51{ return fStorage; }

◆ empty()

bool SkTDStorage::empty ( ) const
inline

Definition at line 39 of file SkTDArray.h.

39{ return fSize == 0; }

◆ erase()

void SkTDStorage::erase ( int  index,
int  count 
)

Definition at line 141 of file SkTDArray.cpp.

141 {
142 SkASSERT(count >= 0);
143 SkASSERT(fSize >= count);
144 SkASSERT(0 <= index && index <= fSize);
145
146 if (count > 0) {
147 // Check that the resulting size fits in an int. This will abort if not.
148 const int newCount = this->calculateSizeOrDie(-count);
149 this->moveTail(index, index + count, fSize);
150 this->resize(newCount);
151 }
152}
void resize(int newSize)
Definition: SkTDArray.cpp:84

◆ insert() [1/2]

void * SkTDStorage::insert ( int  index)

Definition at line 189 of file SkTDArray.cpp.

189 {
190 return this->insert(index, /*count=*/1, nullptr);
191}

◆ insert() [2/2]

void * SkTDStorage::insert ( int  index,
int  count,
const void *  src 
)

Definition at line 193 of file SkTDArray.cpp.

193 {
194 SkASSERT(0 <= index && index <= fSize);
195 SkASSERT(count >= 0);
196
197 if (count > 0) {
198 const int oldCount = fSize;
199 const int newCount = this->calculateSizeOrDie(count);
200 this->resize(newCount);
201 this->moveTail(index + count, index, oldCount);
202
203 if (src != nullptr) {
204 this->copySrc(index, src, count);
205 }
206 }
207
208 return this->address(index);
209}

◆ operator=() [1/2]

SkTDStorage & SkTDStorage::operator= ( const SkTDStorage that)

Definition at line 38 of file SkTDArray.cpp.

38 {
39 if (this != &that) {
40 if (that.fSize <= fCapacity) {
41 fSize = that.fSize;
42 if (fSize > 0) {
43 memcpy(fStorage, that.data(), that.size_bytes());
44 }
45 } else {
46 *this = SkTDStorage{that.data(), that.size(), that.fSizeOfT};
47 }
48 }
49 return *this;
50}
void * data()
Definition: SkTDArray.h:50
size_t size_bytes() const
Definition: SkTDArray.h:43

◆ operator=() [2/2]

SkTDStorage & SkTDStorage::operator= ( SkTDStorage &&  that)

Definition at line 58 of file SkTDArray.cpp.

58 {
59 if (this != &that) {
60 this->~SkTDStorage();
61 new (this) SkTDStorage{std::move(that)};
62 }
63 return *this;
64}

◆ pop_back()

void SkTDStorage::pop_back ( )
inline

Definition at line 68 of file SkTDArray.h.

68 {
69 SkASSERT(fSize > 0);
70 fSize--;
71 }

◆ prepend()

void * SkTDStorage::prepend ( )

Definition at line 163 of file SkTDArray.cpp.

163 {
164 return this->insert(/*index=*/0);
165}

◆ removeShuffle()

void SkTDStorage::removeShuffle ( int  index)

Definition at line 154 of file SkTDArray.cpp.

154 {
155 SkASSERT(fSize > 0);
156 SkASSERT(0 <= index && index < fSize);
157 // Check that the new count is valid.
158 const int newCount = this->calculateSizeOrDie(-1);
159 this->moveTail(index, fSize - 1, fSize);
160 this->resize(newCount);
161}

◆ reserve()

void SkTDStorage::reserve ( int  newCapacity)

Definition at line 92 of file SkTDArray.cpp.

92 {
93 SkASSERT(newCapacity >= 0);
94 if (newCapacity > fCapacity) {
95 // Establish the maximum number of elements that includes a valid count for end. In the
96 // largest case end() = &fArray[INT_MAX] which is 1 after the last indexable element.
97 static constexpr int kMaxCount = INT_MAX;
98
99 // Assume that the array will max out.
100 int expandedReserve = kMaxCount;
101 if (kMaxCount - newCapacity > 4) {
102 // Add 1/4 more than we need. Add 4 to ensure this grows by at least 1. Pin to
103 // kMaxCount if no room for 1/4 growth.
104 int growth = 4 + ((newCapacity + 4) >> 2);
105 // Read this line as: if (count + growth < kMaxCount) { ... }
106 // It's rewritten to avoid signed integer overflow.
107 if (kMaxCount - newCapacity > growth) {
108 expandedReserve = newCapacity + growth;
109 }
110 }
111
112
113 // With a T size of 1, the above allocator produces the progression of 7, 15, ... Since,
114 // the sizeof max_align_t is often 16, there is no reason to allocate anything less than
115 // 16 bytes. This eliminates a realloc when pushing back bytes to an SkTDArray.
116 if (fSizeOfT == 1) {
117 // Round up to the multiple of 16.
118 expandedReserve = (expandedReserve + 15) & ~15;
119 }
120
121 fCapacity = expandedReserve;
122 size_t newStorageSize = this->bytes(fCapacity);
123 fStorage = static_cast<std::byte*>(sk_realloc_throw(fStorage, newStorageSize));
124 }
125}
SK_API void * sk_realloc_throw(void *buffer, size_t size)
static constexpr size_t kMaxCount
Definition: dart_vm.cc:287

◆ reset()

void SkTDStorage::reset ( )

Definition at line 70 of file SkTDArray.cpp.

70 {
71 const int sizeOfT = fSizeOfT;
72 this->~SkTDStorage();
73 new (this) SkTDStorage{sizeOfT};
74}

◆ resize()

void SkTDStorage::resize ( int  newSize)

Definition at line 84 of file SkTDArray.cpp.

84 {
85 SkASSERT(newSize >= 0);
86 if (newSize > fCapacity) {
87 this->reserve(newSize);
88 }
89 fSize = newSize;
90}
void reserve(int newCapacity)
Definition: SkTDArray.cpp:92

◆ shrink_to_fit()

void SkTDStorage::shrink_to_fit ( )

Definition at line 127 of file SkTDArray.cpp.

127 {
128 if (fCapacity != fSize) {
129 fCapacity = fSize;
130 // Because calling realloc with size of 0 is implementation defined, force to a good state
131 // by freeing fStorage.
132 if (fCapacity > 0) {
133 fStorage = static_cast<std::byte*>(sk_realloc_throw(fStorage, this->bytes(fCapacity)));
134 } else {
135 sk_free(fStorage);
136 fStorage = nullptr;
137 }
138 }
139}

◆ size()

int SkTDStorage::size ( ) const
inline

Definition at line 41 of file SkTDArray.h.

41{ return fSize; }

◆ size_bytes()

size_t SkTDStorage::size_bytes ( ) const
inline

Definition at line 43 of file SkTDArray.h.

43{ return this->bytes(fSize); }

◆ swap()

void SkTDStorage::swap ( SkTDStorage that)

Definition at line 76 of file SkTDArray.cpp.

76 {
77 SkASSERT(fSizeOfT == that.fSizeOfT);
78 using std::swap;
79 swap(fStorage, that.fStorage);
80 swap(fCapacity, that.fCapacity);
81 swap(fSize, that.fSize);
82}
void swap(sk_sp< T > &a, sk_sp< T > &b)
Definition: SkRefCnt.h:341
void swap(SkTDStorage &that)
Definition: SkTDArray.cpp:76

Friends And Related Function Documentation

◆ operator!=

bool operator!= ( const SkTDStorage a,
const SkTDStorage b 
)
friend

Definition at line 74 of file SkTDArray.h.

74 {
75 return !(a == b);
76 }
static bool b
struct MyStruct a[10]

◆ operator==

bool operator== ( const SkTDStorage a,
const SkTDStorage b 
)
friend

Definition at line 211 of file SkTDArray.cpp.

211 {
212 return a.size() == b.size() && (a.empty() || !memcmp(a.data(), b.data(), a.bytes(a.size())));
213}

The documentation for this class was generated from the following files: