Flutter Engine
The Flutter Engine
SkArenaAlloc.cpp
Go to the documentation of this file.
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
9
11
12#include <algorithm>
13#include <cassert>
14#include <cstddef>
15
16static char* end_chain(char*) { return nullptr; }
17
18SkArenaAlloc::SkArenaAlloc(char* block, size_t size, size_t firstHeapAllocation)
19 : fDtorCursor {block}
20 , fCursor {block}
21 , fEnd {block + SkToU32(size)}
22 , fFibonacciProgression{SkToU32(size), SkToU32(firstHeapAllocation)}
23{
24 if (size < sizeof(Footer)) {
25 fEnd = fCursor = fDtorCursor = nullptr;
26 }
27
28 if (fCursor != nullptr) {
29 this->installFooter(end_chain, 0);
30 sk_asan_poison_memory_region(fCursor, fEnd - fCursor);
31 }
32}
33
35 RunDtorsOnBlock(fDtorCursor);
36}
37
38void SkArenaAlloc::installFooter(FooterAction* action, uint32_t padding) {
39 assert(SkTFitsIn<uint8_t>(padding));
40 this->installRaw(action);
41 this->installRaw((uint8_t)padding);
42 fDtorCursor = fCursor;
43}
44
45char* SkArenaAlloc::SkipPod(char* footerEnd) {
46 char* objEnd = footerEnd - (sizeof(Footer) + sizeof(uint32_t));
47 uint32_t skip;
48 memmove(&skip, objEnd, sizeof(uint32_t));
49 return objEnd - (ptrdiff_t) skip;
50}
51
52void SkArenaAlloc::RunDtorsOnBlock(char* footerEnd) {
53 while (footerEnd != nullptr) {
55 uint8_t padding;
56
57 memcpy(&action, footerEnd - sizeof( Footer), sizeof( action));
58 memcpy(&padding, footerEnd - sizeof(padding), sizeof(padding));
59
60 footerEnd = action(footerEnd) - (ptrdiff_t)padding;
61 }
62}
63
64char* SkArenaAlloc::NextBlock(char* footerEnd) {
65 char* objEnd = footerEnd - (sizeof(char*) + sizeof(Footer));
66 char* next;
67 memmove(&next, objEnd, sizeof(char*));
68 RunDtorsOnBlock(next);
69 sk_free(objEnd);
70 return nullptr;
71}
72
73void SkArenaAlloc::ensureSpace(uint32_t size, uint32_t alignment) {
74 constexpr uint32_t headerSize = sizeof(Footer) + sizeof(ptrdiff_t);
75 constexpr uint32_t maxSize = std::numeric_limits<uint32_t>::max();
76 constexpr uint32_t overhead = headerSize + sizeof(Footer);
77 AssertRelease(size <= maxSize - overhead);
78 uint32_t objSizeAndOverhead = size + overhead;
79
80 const uint32_t alignmentOverhead = alignment - 1;
81 AssertRelease(objSizeAndOverhead <= maxSize - alignmentOverhead);
82 objSizeAndOverhead += alignmentOverhead;
83
84 uint32_t minAllocationSize = fFibonacciProgression.nextBlockSize();
85 uint32_t allocationSize = std::max(objSizeAndOverhead, minAllocationSize);
86
87 // Round up to a nice size. If > 32K align to 4K boundary else up to max_align_t. The > 32K
88 // heuristic is from the JEMalloc behavior.
89 {
90 uint32_t mask = allocationSize > (1 << 15) ? (1 << 12) - 1 : 16 - 1;
91 AssertRelease(allocationSize <= maxSize - mask);
92 allocationSize = (allocationSize + mask) & ~mask;
93 }
94
95 char* newBlock = static_cast<char*>(sk_malloc_throw(allocationSize));
96
97 auto previousDtor = fDtorCursor;
98 fCursor = newBlock;
99 fDtorCursor = newBlock;
100 fEnd = fCursor + allocationSize;
101
102 // poison the unused bytes in the block.
103 sk_asan_poison_memory_region(fCursor, fEnd - fCursor);
104
105 this->installRaw(previousDtor);
106 this->installFooter(NextBlock, 0);
107}
108
109char* SkArenaAlloc::allocObjectWithFooter(uint32_t sizeIncludingFooter, uint32_t alignment) {
110 uintptr_t mask = alignment - 1;
111
112restart:
113 uint32_t skipOverhead = 0;
114 const bool needsSkipFooter = fCursor != fDtorCursor;
115 if (needsSkipFooter) {
116 skipOverhead = sizeof(Footer) + sizeof(uint32_t);
117 }
118 const uint32_t totalSize = sizeIncludingFooter + skipOverhead;
119
120 // Math on null fCursor/fEnd is undefined behavior, so explicitly check for first alloc.
121 if (!fCursor) {
122 this->ensureSpace(totalSize, alignment);
123 goto restart;
124 }
125
126 assert(fEnd);
127 // This test alone would be enough nullptr were defined to be 0, but it's not.
128 char* objStart = (char*)((uintptr_t)(fCursor + skipOverhead + mask) & ~mask);
129 if ((ptrdiff_t)totalSize > fEnd - objStart) {
130 this->ensureSpace(totalSize, alignment);
131 goto restart;
132 }
133
134 AssertRelease((ptrdiff_t)totalSize <= fEnd - objStart);
135
136 // Install a skip footer if needed, thus terminating a run of POD data. The calling code is
137 // responsible for installing the footer after the object.
138 if (needsSkipFooter) {
139 this->installRaw(SkToU32(fCursor - fDtorCursor));
140 this->installFooter(SkipPod, 0);
141 }
142
143 return objStart;
144}
145
147 size_t size,
148 size_t firstHeapAllocation)
149 : SkArenaAlloc(block, size, firstHeapAllocation)
150 , fFirstBlock{block}
151 , fFirstSize{SkToU32(size)}
152 , fFirstHeapAllocationSize{SkToU32(firstHeapAllocation)} {}
153
155 char* const firstBlock = fFirstBlock;
156 const uint32_t firstSize = fFirstSize;
157 const uint32_t firstHeapAllocationSize = fFirstHeapAllocationSize;
159 new (this) SkArenaAllocWithReset{firstBlock, firstSize, firstHeapAllocationSize};
160}
161
163 return this->cursor() == nullptr ||
164 this->cursor() == fFirstBlock + sizeof(Footer);
165}
166
167// SkFibonacci47 is the first 47 Fibonacci numbers. Fib(47) is the largest value less than 2 ^ 32.
168// Used by SkFibBlockSizes.
169std::array<const uint32_t, 47> SkFibonacci47 {
170 1, 1, 2, 3, 5, 8,
171 13, 21, 34, 55, 89, 144,
172 233, 377, 610, 987, 1597, 2584,
173 4181, 6765, 10946, 17711, 28657, 46368,
174 75025, 121393, 196418, 317811, 514229, 832040,
175 1346269, 2178309, 3524578, 5702887, 9227465, 14930352,
176 24157817, 39088169, 63245986, 102334155, 165580141, 267914296,
177 433494437, 701408733, 1134903170, 1836311903, 2971215073,
178};
static float next(float f)
static void sk_asan_poison_memory_region(void const volatile *addr, size_t size)
Definition: SkASAN.h:34
std::array< const uint32_t, 47 > SkFibonacci47
static char * end_chain(char *)
static bool skip(SkStream *stream, size_t amount)
SK_API void sk_free(void *)
static void * sk_malloc_throw(size_t size)
Definition: SkMalloc.h:67
constexpr uint32_t SkToU32(S x)
Definition: SkTo.h:26
SkArenaAllocWithReset(char *block, size_t blockSize, size_t firstHeapAllocation)
char * cursor()
Definition: SkArenaAlloc.h:215
SkArenaAlloc(char *block, size_t blockSize, size_t firstHeapAllocation)
char *(char *) FooterAction
Definition: SkArenaAlloc.h:209
uint32_t nextBlockSize()
Definition: SkArenaAlloc.h:48
static float max(float r, float g, float b)
Definition: hsl.cpp:49
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259