• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 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 SkArenaAlloc_DEFINED
9 #define SkArenaAlloc_DEFINED
10 
11 #include "include/core/SkTypes.h"
12 #include "include/private/SkTFitsIn.h"
13 #include "include/private/SkTo.h"
14 
15 #include <array>
16 #include <cassert>
17 #include <cstddef>
18 #include <cstdint>
19 #include <cstdlib>
20 #include <cstring>
21 #include <limits>
22 #include <new>
23 #include <type_traits>
24 #include <utility>
25 #include <vector>
26 
27 // We found allocating strictly doubling amounts of memory from the heap left too
28 // much unused slop, particularly on Android.  Instead we'll follow a Fibonacci-like
29 // progression.
30 
31 // SkFibonacci47 is the first 47 Fibonacci numbers. Fib(47) is the largest value less than 2 ^ 32.
32 extern std::array<const uint32_t, 47> SkFibonacci47;
33 template<uint32_t kMaxSize>
34 class SkFibBlockSizes {
35 public:
36     // staticBlockSize, and firstAllocationSize are parameters describing the initial memory
37     // layout. staticBlockSize describes the size of the inlined memory, and firstAllocationSize
38     // describes the size of the first block to be allocated if the static block is exhausted. By
39     // convention, firstAllocationSize is the first choice for the block unit size followed by
40     // staticBlockSize followed by the default of 1024 bytes.
SkFibBlockSizes(uint32_t staticBlockSize,uint32_t firstAllocationSize)41     SkFibBlockSizes(uint32_t staticBlockSize, uint32_t firstAllocationSize) : fIndex{0} {
42         fBlockUnitSize = firstAllocationSize > 0 ? firstAllocationSize :
43                          staticBlockSize     > 0 ? staticBlockSize     : 1024;
44 
45         SkASSERT_RELEASE(0 < fBlockUnitSize);
46         SkASSERT_RELEASE(fBlockUnitSize < std::min(kMaxSize, (1u << 26) - 1));
47     }
48 
nextBlockSize()49     uint32_t nextBlockSize() {
50         uint32_t result = SkFibonacci47[fIndex] * fBlockUnitSize;
51 
52         if (SkTo<size_t>(fIndex + 1) < SkFibonacci47.size() &&
53             SkFibonacci47[fIndex + 1] < kMaxSize / fBlockUnitSize)
54         {
55             fIndex += 1;
56         }
57 
58         return result;
59     }
60 
61 private:
62     uint32_t fIndex : 6;
63     uint32_t fBlockUnitSize : 26;
64 };
65 
66 // SkArenaAlloc allocates object and destroys the allocated objects when destroyed. It's designed
67 // to minimize the number of underlying block allocations. SkArenaAlloc allocates first out of an
68 // (optional) user-provided block of memory, and when that's exhausted it allocates on the heap,
69 // starting with an allocation of firstHeapAllocation bytes.  If your data (plus a small overhead)
70 // fits in the user-provided block, SkArenaAlloc never uses the heap, and if it fits in
71 // firstHeapAllocation bytes, it'll use the heap only once. If 0 is specified for
72 // firstHeapAllocation, then blockSize is used unless that too is 0, then 1024 is used.
73 //
74 // Examples:
75 //
76 //   char block[mostCasesSize];
77 //   SkArenaAlloc arena(block, mostCasesSize);
78 //
79 // If mostCasesSize is too large for the stack, you can use the following pattern.
80 //
81 //   std::unique_ptr<char[]> block{new char[mostCasesSize]};
82 //   SkArenaAlloc arena(block.get(), mostCasesSize, almostAllCasesSize);
83 //
84 // If the program only sometimes allocates memory, use the following pattern.
85 //
86 //   SkArenaAlloc arena(nullptr, 0, almostAllCasesSize);
87 //
88 // The storage does not necessarily need to be on the stack. Embedding the storage in a class also
89 // works.
90 //
91 //   class Foo {
92 //       char storage[mostCasesSize];
93 //       SkArenaAlloc arena (storage, mostCasesSize);
94 //   };
95 //
96 // In addition, the system is optimized to handle POD data including arrays of PODs (where
97 // POD is really data with no destructors). For POD data it has zero overhead per item, and a
98 // typical per block overhead of 8 bytes. For non-POD objects there is a per item overhead of 4
99 // bytes. For arrays of non-POD objects there is a per array overhead of typically 8 bytes. There
100 // is an addition overhead when switching from POD data to non-POD data of typically 8 bytes.
101 //
102 // If additional blocks are needed they are increased exponentially. This strategy bounds the
103 // recursion of the RunDtorsOnBlock to be limited to O(log size-of-memory). Block size grow using
104 // the Fibonacci sequence which means that for 2^32 memory there are 48 allocations, and for 2^48
105 // there are 71 allocations.
106 class SkArenaAlloc {
107 public:
108     SkArenaAlloc(char* block, size_t blockSize, size_t firstHeapAllocation);
109 
SkArenaAlloc(size_t firstHeapAllocation)110     explicit SkArenaAlloc(size_t firstHeapAllocation)
111         : SkArenaAlloc(nullptr, 0, firstHeapAllocation) {}
112 
113     SkArenaAlloc(const SkArenaAlloc&) = delete;
114     SkArenaAlloc& operator=(const SkArenaAlloc&) = delete;
115     SkArenaAlloc(SkArenaAlloc&&) = delete;
116     SkArenaAlloc& operator=(SkArenaAlloc&&) = delete;
117 
118     ~SkArenaAlloc();
119 
120     template <typename Ctor>
121     auto make(Ctor&& ctor) -> decltype(ctor(nullptr)) {
122         using T = std::remove_pointer_t<decltype(ctor(nullptr))>;
123 
124         uint32_t size      = SkToU32(sizeof(T));
125         uint32_t alignment = SkToU32(alignof(T));
126         char* objStart;
127         if (std::is_trivially_destructible<T>::value) {
128             objStart = this->allocObject(size, alignment);
129             fCursor = objStart + size;
130         } else {
131             objStart = this->allocObjectWithFooter(size + sizeof(Footer), alignment);
132             // Can never be UB because max value is alignof(T).
133             uint32_t padding = SkToU32(objStart - fCursor);
134 
135             // Advance to end of object to install footer.
136             fCursor = objStart + size;
137             FooterAction* releaser = [](char* objEnd) {
138                 char* objStart = objEnd - (sizeof(T) + sizeof(Footer));
139                 ((T*)objStart)->~T();
140                 return objStart;
141             };
142             this->installFooter(releaser, padding);
143         }
144 
145         // This must be last to make objects with nested use of this allocator work.
146         return ctor(objStart);
147     }
148 
149     template <typename T, typename... Args>
make(Args &&...args)150     T* make(Args&&... args) {
151         return this->make([&](void* objStart) {
152             return new(objStart) T(std::forward<Args>(args)...);
153         });
154     }
155 
156     template <typename T>
makeArrayDefault(size_t count)157     T* makeArrayDefault(size_t count) {
158         T* array = this->allocUninitializedArray<T>(count);
159         for (size_t i = 0; i < count; i++) {
160             // Default initialization: if T is primitive then the value is left uninitialized.
161             new (&array[i]) T;
162         }
163         return array;
164     }
165 
166     template <typename T>
makeArray(size_t count)167     T* makeArray(size_t count) {
168         T* array = this->allocUninitializedArray<T>(count);
169         for (size_t i = 0; i < count; i++) {
170             // Value initialization: if T is primitive then the value is zero-initialized.
171             new (&array[i]) T();
172         }
173         return array;
174     }
175 
176     template <typename T, typename Initializer>
makeInitializedArray(size_t count,Initializer initializer)177     T* makeInitializedArray(size_t count, Initializer initializer) {
178         T* array = this->allocUninitializedArray<T>(count);
179         for (size_t i = 0; i < count; i++) {
180             new (&array[i]) T(initializer(i));
181         }
182         return array;
183     }
184 
185     // Only use makeBytesAlignedTo if none of the typed variants are impractical to use.
makeBytesAlignedTo(size_t size,size_t align)186     void* makeBytesAlignedTo(size_t size, size_t align) {
187         AssertRelease(SkTFitsIn<uint32_t>(size));
188         auto objStart = this->allocObject(SkToU32(size), SkToU32(align));
189         fCursor = objStart + size;
190         return objStart;
191     }
192 
193 private:
AssertRelease(bool cond)194     static void AssertRelease(bool cond) { if (!cond) { ::abort(); } }
195 
196     using FooterAction = char* (char*);
197     struct Footer {
198         uint8_t unaligned_action[sizeof(FooterAction*)];
199         uint8_t padding;
200     };
201 
202     static char* SkipPod(char* footerEnd);
203     static void RunDtorsOnBlock(char* footerEnd);
204     static char* NextBlock(char* footerEnd);
205 
206     template <typename T>
installRaw(const T & val)207     void installRaw(const T& val) {
208         memcpy(fCursor, &val, sizeof(val));
209         fCursor += sizeof(val);
210     }
211     void installFooter(FooterAction* releaser, uint32_t padding);
212 
213     void ensureSpace(uint32_t size, uint32_t alignment);
214 
allocObject(uint32_t size,uint32_t alignment)215     char* allocObject(uint32_t size, uint32_t alignment) {
216         uintptr_t mask = alignment - 1;
217         uintptr_t alignedOffset = (~reinterpret_cast<uintptr_t>(fCursor) + 1) & mask;
218         uintptr_t totalSize = size + alignedOffset;
219         AssertRelease(totalSize >= size);
220         if (totalSize > static_cast<uintptr_t>(fEnd - fCursor)) {
221             this->ensureSpace(size, alignment);
222             alignedOffset = (~reinterpret_cast<uintptr_t>(fCursor) + 1) & mask;
223         }
224 
225         char* object = fCursor + alignedOffset;
226 
227         SkASSERT((reinterpret_cast<uintptr_t>(object) & (alignment - 1)) == 0);
228         SkASSERT(object + size <= fEnd);
229 
230         return object;
231     }
232 
233     char* allocObjectWithFooter(uint32_t sizeIncludingFooter, uint32_t alignment);
234 
235     template <typename T>
allocUninitializedArray(size_t countZ)236     T* allocUninitializedArray(size_t countZ) {
237         AssertRelease(SkTFitsIn<uint32_t>(countZ));
238         uint32_t count = SkToU32(countZ);
239 
240         char* objStart;
241         AssertRelease(count <= std::numeric_limits<uint32_t>::max() / sizeof(T));
242         uint32_t arraySize = SkToU32(count * sizeof(T));
243         uint32_t alignment = SkToU32(alignof(T));
244 
245         if (std::is_trivially_destructible<T>::value) {
246             objStart = this->allocObject(arraySize, alignment);
247             fCursor = objStart + arraySize;
248         } else {
249             constexpr uint32_t overhead = sizeof(Footer) + sizeof(uint32_t);
250             AssertRelease(arraySize <= std::numeric_limits<uint32_t>::max() - overhead);
251             uint32_t totalSize = arraySize + overhead;
252             objStart = this->allocObjectWithFooter(totalSize, alignment);
253 
254             // Can never be UB because max value is alignof(T).
255             uint32_t padding = SkToU32(objStart - fCursor);
256 
257             // Advance to end of array to install footer.
258             fCursor = objStart + arraySize;
259             this->installRaw(SkToU32(count));
260             this->installFooter(
261                 [](char* footerEnd) {
262                     char* objEnd = footerEnd - (sizeof(Footer) + sizeof(uint32_t));
263                     uint32_t count;
264                     memmove(&count, objEnd, sizeof(uint32_t));
265                     char* objStart = objEnd - count * sizeof(T);
266                     T* array = (T*) objStart;
267                     for (uint32_t i = 0; i < count; i++) {
268                         array[i].~T();
269                     }
270                     return objStart;
271                 },
272                 padding);
273         }
274 
275         return (T*)objStart;
276     }
277 
278     char*          fDtorCursor;
279     char*          fCursor;
280     char*          fEnd;
281 
282     SkFibBlockSizes<std::numeric_limits<uint32_t>::max()> fFibonacciProgression;
283 };
284 
285 class SkArenaAllocWithReset : public SkArenaAlloc {
286 public:
287     SkArenaAllocWithReset(char* block, size_t blockSize, size_t firstHeapAllocation);
288 
SkArenaAllocWithReset(size_t firstHeapAllocation)289     explicit SkArenaAllocWithReset(size_t firstHeapAllocation)
290             : SkArenaAllocWithReset(nullptr, 0, firstHeapAllocation) {}
291 
292     // Destroy all allocated objects, free any heap allocations.
293     void reset();
294 
295 private:
296     char* const    fFirstBlock;
297     const uint32_t fFirstSize;
298     const uint32_t fFirstHeapAllocationSize;
299 };
300 
301 // Helper for defining allocators with inline/reserved storage.
302 // For argument declarations, stick to the base type (SkArenaAlloc).
303 // Note: Inheriting from the storage first means the storage will outlive the
304 // SkArenaAlloc, letting ~SkArenaAlloc read it as it calls destructors.
305 // (This is mostly only relevant for strict tools like MSAN.)
306 template <size_t InlineStorageSize>
307 class SkSTArenaAlloc : private std::array<char, InlineStorageSize>, public SkArenaAlloc {
308 public:
309     explicit SkSTArenaAlloc(size_t firstHeapAllocation = InlineStorageSize)
310         : SkArenaAlloc{this->data(), this->size(), firstHeapAllocation} {}
311 };
312 
313 template <size_t InlineStorageSize>
314 class SkSTArenaAllocWithReset
315         : private std::array<char, InlineStorageSize>, public SkArenaAllocWithReset {
316 public:
317     explicit SkSTArenaAllocWithReset(size_t firstHeapAllocation = InlineStorageSize)
318             : SkArenaAllocWithReset{this->data(), this->size(), firstHeapAllocation} {}
319 };
320 
321 #endif  // SkArenaAlloc_DEFINED
322