mirrored from https://chromium.googlesource.com/angle/angle
-
Notifications
You must be signed in to change notification settings - Fork 733
Expand file tree
/
Copy pathPoolAlloc.cpp
More file actions
410 lines (353 loc) · 12.9 KB
/
Copy pathPoolAlloc.cpp
File metadata and controls
410 lines (353 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//
// Copyright 2019 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// PoolAlloc.cpp:
// Implements the class methods for PoolAllocator and Allocation classes.
//
#ifdef UNSAFE_BUFFERS_BUILD
# pragma allow_unsafe_buffers
#endif
#include "common/PoolAlloc.h"
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <utility>
#include <utility>
#include "common/mathutil.h"
#include "common/platform.h"
#include "common/tls.h"
#if defined(ANGLE_WITH_ASAN)
# include <sanitizer/asan_interface.h>
#endif
namespace angle
{
// If we are using guard blocks, we must track each individual allocation. If we aren't using guard
// blocks, these never get instantiated, so won't have any impact.
class Allocation
{
public:
Allocation(size_t size, unsigned char *mem, Allocation *prev = 0)
: mSize(size), mMem(mem), mPrevAlloc(prev)
{
// Allocations are bracketed:
//
// [allocationHeader][initialGuardBlock][userData][finalGuardBlock]
//
// This would be cleaner with if (kGuardBlockSize)..., but that makes the compiler print
// warnings about 0 length memsets, even with the if() protecting them.
#if defined(ANGLE_POOL_ALLOC_GUARD_BLOCKS)
memset(preGuard(), kGuardBlockBeginVal, kGuardBlockSize);
memset(data(), kUserDataFill, mSize);
memset(postGuard(), kGuardBlockEndVal, kGuardBlockSize);
#endif
}
void checkAllocList() const;
static size_t AlignedHeaderSize(uint8_t *allocationBasePtr, size_t alignment)
{
// Make sure that the data offset after the header is aligned to the given alignment.
size_t base = reinterpret_cast<size_t>(allocationBasePtr);
return rx::roundUpPow2(base + kGuardBlockSize + HeaderSize(), alignment) - base;
}
// Return total size needed to accommodate user buffer of 'size',
// plus our tracking data and any necessary alignments.
static size_t AllocationSize(uint8_t *allocationBasePtr,
size_t size,
size_t alignment,
size_t *preAllocationPaddingOut)
{
// The allocation will be laid out as such:
//
// Aligned to |alignment|
// ^
// preAllocationPaddingOut |
// ___^___ |
// / \ |
// <padding>[header][guard][data][guard]
// \___________ __________/
// V
// dataOffset
//
// Note that alignment is at least as much as a pointer alignment, so the pointers in the
// header are also necessarily aligned appropriately.
//
size_t dataOffset = AlignedHeaderSize(allocationBasePtr, alignment);
*preAllocationPaddingOut = dataOffset - HeaderSize() - kGuardBlockSize;
return dataOffset + size + kGuardBlockSize;
}
// Given memory pointing to |header|, returns |data|.
static uint8_t *GetDataPointer(uint8_t *memory, size_t alignment)
{
uint8_t *alignedPtr = memory + kGuardBlockSize + HeaderSize();
// |memory| must be aligned already such that user data is aligned to |alignment|.
ASSERT((reinterpret_cast<uintptr_t>(alignedPtr) & (alignment - 1)) == 0);
return alignedPtr;
}
private:
void checkGuardBlock(unsigned char *blockMem, unsigned char val, const char *locText) const;
void checkAlloc() const
{
checkGuardBlock(preGuard(), kGuardBlockBeginVal, "before");
checkGuardBlock(postGuard(), kGuardBlockEndVal, "after");
}
// Find offsets to pre and post guard blocks, and user data buffer
unsigned char *preGuard() const { return mMem + HeaderSize(); }
unsigned char *data() const { return preGuard() + kGuardBlockSize; }
unsigned char *postGuard() const { return data() + mSize; }
size_t mSize; // size of the user data area
unsigned char *mMem; // beginning of our allocation (points to header)
Allocation *mPrevAlloc; // prior allocation in the chain
static constexpr unsigned char kGuardBlockBeginVal = 0xfb;
static constexpr unsigned char kGuardBlockEndVal = 0xfe;
static constexpr unsigned char kUserDataFill = 0xcd;
#if defined(ANGLE_POOL_ALLOC_GUARD_BLOCKS)
static constexpr size_t kGuardBlockSize = 16;
static constexpr size_t HeaderSize() { return sizeof(Allocation); }
#else
static constexpr size_t kGuardBlockSize = 0;
static constexpr size_t HeaderSize() { return 0; }
#endif
};
#if !defined(ANGLE_DISABLE_POOL_ALLOC)
class PageHeader
{
public:
PageHeader(PageHeader *nextPage, size_t pageCount)
: nextPage(nextPage),
pageCount(pageCount)
{
}
PageHeader *nextPage;
size_t pageCount;
# if defined(ANGLE_POOL_ALLOC_GUARD_BLOCKS)
Allocation *lastAllocation = nullptr;
# endif
};
#endif
//
// Implement the functionality of the PoolAllocator class, which
// is documented in PoolAlloc.h.
//
PoolAllocator::PoolAllocator(int growthIncrement, int allocationAlignment)
:
#if !defined(ANGLE_DISABLE_POOL_ALLOC)
mPageSize(growthIncrement),
mFreeList(nullptr),
mInUseList(nullptr),
mNumCalls(0),
mTotalBytes(0),
#endif
mAlignment(allocationAlignment)
{
#if !defined(ANGLE_DISABLE_POOL_ALLOC)
mPageHeaderSkip = sizeof(PageHeader);
// Alignment == 1 is a special fast-path where fastAllocate() is enabled
if (mAlignment != 1)
{
#endif
// Adjust mAlignment to be at least pointer aligned and
// power of 2.
//
size_t minAlign = sizeof(void *);
if (mAlignment < minAlign)
{
mAlignment = minAlign;
}
mAlignment = gl::ceilPow2(static_cast<unsigned int>(mAlignment));
#if !defined(ANGLE_DISABLE_POOL_ALLOC)
}
//
// Don't allow page sizes we know are smaller than all common
// OS page sizes.
//
if (mPageSize < 4 * 1024)
{
mPageSize = 4 * 1024;
}
//
// A large mCurrentPageOffset indicates a new page needs to
// be obtained to allocate memory.
//
mCurrentPageOffset = mPageSize;
#endif
}
PoolAllocator::~PoolAllocator()
{
reset();
#if !defined(ANGLE_DISABLE_POOL_ALLOC)
while (mFreeList)
{
PageHeader *next = mFreeList->nextPage;
delete[] reinterpret_cast<char *>(mFreeList);
mFreeList = next;
}
#endif
}
//
// Check a single guard block for damage
//
void Allocation::checkGuardBlock(unsigned char *blockMem,
unsigned char val,
const char *locText) const
{
#if defined(ANGLE_POOL_ALLOC_GUARD_BLOCKS)
for (size_t x = 0; x < kGuardBlockSize; x++)
{
if (blockMem[x] != val)
{
char assertMsg[80];
// We don't print the assert message. It's here just to be helpful.
snprintf(assertMsg, sizeof(assertMsg),
"PoolAlloc: Damage %s %zu byte allocation at 0x%p\n", locText, mSize, data());
assert(0 && "PoolAlloc: Damage in guard block");
}
}
#endif
}
void PoolAllocator::reset()
{
#if !defined(ANGLE_DISABLE_POOL_ALLOC)
mNumCalls = 0;
mTotalBytes = 0;
mCurrentPageOffset = mPageSize;
PageHeader *page = std::exchange(mInUseList, nullptr);
while (page)
{
const size_t pageCount = page->pageCount;
PageHeader *nextInUse = page->nextPage;
# if defined(ANGLE_POOL_ALLOC_GUARD_BLOCKS)
if (page->lastAllocation)
{
Allocation *allocations = std::exchange(page->lastAllocation, nullptr);
allocations->checkAllocList();
}
# endif
if (pageCount > 1)
{
delete[] reinterpret_cast<uint8_t *>(page);
}
else
{
# if defined(ANGLE_WITH_ASAN)
// Clear any container annotations left over from when the memory
// was last used. (crbug.com/1419798)
__asan_unpoison_memory_region(page, mPageSize);
# endif
page->nextPage = mFreeList;
mFreeList = page;
}
page = nextInUse;
}
#else // !defined(ANGLE_DISABLE_POOL_ALLOC)
mStack.clear();
#endif
}
void *PoolAllocator::allocate(size_t numBytes)
{
#if !defined(ANGLE_DISABLE_POOL_ALLOC)
//
// Just keep some interesting statistics.
//
++mNumCalls;
mTotalBytes += numBytes;
uint8_t *currentPagePtr = reinterpret_cast<uint8_t *>(mInUseList) + mCurrentPageOffset;
size_t preAllocationPadding = 0;
size_t allocationSize =
Allocation::AllocationSize(currentPagePtr, numBytes, mAlignment, &preAllocationPadding);
// Integer overflow is unexpected.
ASSERT(allocationSize >= numBytes);
// Do the allocation, most likely case first, for efficiency.
if (allocationSize <= mPageSize - mCurrentPageOffset)
{
// There is enough room to allocate from the current page at mCurrentPageOffset.
uint8_t *memory = currentPagePtr + preAllocationPadding;
mCurrentPageOffset += allocationSize;
return initializeAllocation(memory, numBytes);
}
if (allocationSize > mPageSize - mPageHeaderSkip)
{
// If the allocation is larger than a whole page, do a multi-page allocation. These are not
// mixed with the others. The OS is efficient in allocating and freeing multiple pages.
// We don't know what the alignment of the new allocated memory will be, so conservatively
// allocate enough memory for up to alignment extra bytes being needed.
allocationSize = Allocation::AllocationSize(reinterpret_cast<uint8_t *>(mPageHeaderSkip),
numBytes, mAlignment, &preAllocationPadding);
size_t numBytesToAlloc = allocationSize + mPageHeaderSkip + mAlignment;
// Integer overflow is unexpected.
ASSERT(numBytesToAlloc >= allocationSize);
uint8_t *memory = new (std::nothrow) uint8_t[numBytesToAlloc];
if (memory == nullptr)
{
return nullptr;
}
mInUseList =
new (memory) PageHeader(mInUseList, (numBytesToAlloc + mPageSize - 1) / mPageSize);
// Make next allocation come from a new page
mCurrentPageOffset = mPageSize;
// Now that we actually have the pointer, make sure the data pointer will be aligned.
currentPagePtr = reinterpret_cast<uint8_t *>(mInUseList) + mPageHeaderSkip;
Allocation::AllocationSize(currentPagePtr, numBytes, mAlignment, &preAllocationPadding);
return initializeAllocation(currentPagePtr + preAllocationPadding, numBytes);
}
uint8_t *newPageAddr = allocateNewPage(numBytes);
return initializeAllocation(newPageAddr, numBytes);
#else // !defined(ANGLE_DISABLE_POOL_ALLOC)
uint8_t *alloc = new (std::nothrow) uint8_t[numBytes + mAlignment - 1];
mStack.emplace_back(std::unique_ptr<uint8_t[]>(alloc));
intptr_t intAlloc = reinterpret_cast<intptr_t>(alloc);
intAlloc = rx::roundUpPow2<intptr_t>(intAlloc, mAlignment);
return reinterpret_cast<void *>(intAlloc);
#endif
}
#if !defined(ANGLE_DISABLE_POOL_ALLOC)
uint8_t *PoolAllocator::allocateNewPage(size_t numBytes)
{
// Need a simple page to allocate from. Pick a page from the free list, if any. Otherwise need
// to make the allocation.
if (mFreeList)
{
PageHeader *page = mFreeList;
mFreeList = mFreeList->nextPage;
page->nextPage = mInUseList;
mInUseList = page;
}
else
{
uint8_t *memory = new (std::nothrow) uint8_t[mPageSize];
if (memory == nullptr)
{
return nullptr;
}
mInUseList = new (memory) PageHeader(mInUseList, 1);
}
// Leave room for the page header.
mCurrentPageOffset = mPageHeaderSkip;
uint8_t *currentPagePtr = reinterpret_cast<uint8_t *>(mInUseList) + mCurrentPageOffset;
size_t preAllocationPadding = 0;
size_t allocationSize =
Allocation::AllocationSize(currentPagePtr, numBytes, mAlignment, &preAllocationPadding);
mCurrentPageOffset += allocationSize;
// The new allocation is made after the page header and any alignment required before it.
return reinterpret_cast<uint8_t *>(mInUseList) + mPageHeaderSkip + preAllocationPadding;
}
void *PoolAllocator::initializeAllocation(uint8_t *memory, size_t numBytes)
{
# if defined(ANGLE_POOL_ALLOC_GUARD_BLOCKS)
mInUseList->lastAllocation =
new (memory) Allocation(numBytes, memory, mInUseList->lastAllocation);
# endif
return Allocation::GetDataPointer(memory, mAlignment);
}
#endif
//
// Check all allocations in a list for damage by calling check on each.
//
void Allocation::checkAllocList() const
{
for (const Allocation *alloc = this; alloc != nullptr; alloc = alloc->mPrevAlloc)
{
alloc->checkAlloc();
}
}
} // namespace angle