Hash :
b954755f
Author :
Date :
2024-07-02T00:00:00
Release all memory allocated in TCompiler::compile Ensured that single-page allocations from compile jobs are released to OS. Fixed: angleproject:350528355 Change-Id: I5a0d9fd7dbc065f4b4127ceecb3fd6538eb8948d Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5673352 Reviewed-by: Kenneth Russell <kbr@chromium.org> Reviewed-by: Geoff Lang <geofflang@chromium.org> Commit-Queue: Alexey Knyazev <lexa.knyazev@gmail.com>
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 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
//
// 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.
//
#include "common/PoolAlloc.h"
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include "common/angleutils.h"
#include "common/debug.h"
#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)
# if defined(ANGLE_POOL_ALLOC_GUARD_BLOCKS)
,
lastAllocation(nullptr)
# endif
{}
~PageHeader()
{
# if defined(ANGLE_POOL_ALLOC_GUARD_BLOCKS)
if (lastAllocation)
{
lastAllocation->checkAllocList();
}
# endif
}
PageHeader *nextPage;
size_t pageCount;
# if defined(ANGLE_POOL_ALLOC_GUARD_BLOCKS)
Allocation *lastAllocation;
# endif
};
#endif
//
// Implement the functionality of the PoolAllocator class, which
// is documented in PoolAlloc.h.
//
PoolAllocator::PoolAllocator(int growthIncrement, int allocationAlignment)
: mAlignment(allocationAlignment),
#if !defined(ANGLE_DISABLE_POOL_ALLOC)
mPageSize(growthIncrement),
mFreeList(nullptr),
mInUseList(nullptr),
mNumCalls(0),
mTotalBytes(0),
#endif
mLocked(false)
{
initialize(growthIncrement, allocationAlignment);
}
void PoolAllocator::initialize(int pageSize, int alignment)
{
mAlignment = alignment;
#if !defined(ANGLE_DISABLE_POOL_ALLOC)
mPageSize = pageSize;
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;
#else // !defined(ANGLE_DISABLE_POOL_ALLOC)
mStack.push_back({});
#endif
}
PoolAllocator::~PoolAllocator()
{
#if !defined(ANGLE_DISABLE_POOL_ALLOC)
while (mInUseList)
{
PageHeader *next = mInUseList->nextPage;
mInUseList->~PageHeader();
delete[] reinterpret_cast<char *>(mInUseList);
mInUseList = next;
}
// We should not check the guard blocks
// here, because we did it already when the block was
// placed into the free list.
//
while (mFreeList)
{
PageHeader *next = mFreeList->nextPage;
delete[] reinterpret_cast<char *>(mFreeList);
mFreeList = next;
}
#else // !defined(ANGLE_DISABLE_POOL_ALLOC)
for (auto &allocs : mStack)
{
for (auto alloc : allocs)
{
free(alloc);
}
}
mStack.clear();
#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::push()
{
#if !defined(ANGLE_DISABLE_POOL_ALLOC)
AllocState state = {mCurrentPageOffset, mInUseList};
mStack.push_back(state);
//
// Indicate there is no current page to allocate from.
//
mCurrentPageOffset = mPageSize;
#else // !defined(ANGLE_DISABLE_POOL_ALLOC)
mStack.push_back({});
#endif
}
// Do a mass-deallocation of all the individual allocations that have occurred since the last
// push(), or since the last pop(), or since the object's creation.
//
// Single-page allocations are saved for future use unless the release strategy is All.
void PoolAllocator::pop(ReleaseStrategy releaseStrategy)
{
if (mStack.size() < 1)
{
return;
}
#if !defined(ANGLE_DISABLE_POOL_ALLOC)
PageHeader *page = mStack.back().page;
mCurrentPageOffset = mStack.back().offset;
while (mInUseList != page)
{
// Grab the pageCount before calling the destructor. While the destructor doesn't actually
// touch this variable, it's confusing MSAN.
const size_t pageCount = mInUseList->pageCount;
PageHeader *nextInUse = mInUseList->nextPage;
// invoke destructor to free allocation list
mInUseList->~PageHeader();
if (pageCount > 1 || releaseStrategy == ReleaseStrategy::All)
{
delete[] reinterpret_cast<char *>(mInUseList);
}
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(mInUseList, mPageSize);
# endif
mInUseList->nextPage = mFreeList;
mFreeList = mInUseList;
}
mInUseList = nextInUse;
}
mStack.pop_back();
#else // !defined(ANGLE_DISABLE_POOL_ALLOC)
for (auto &alloc : mStack.back())
{
free(alloc);
}
mStack.pop_back();
#endif
}
//
// Do a mass-deallocation of all the individual allocations
// that have occurred.
//
void PoolAllocator::popAll()
{
while (mStack.size() > 0)
pop();
}
void *PoolAllocator::allocate(size_t numBytes)
{
ASSERT(!mLocked);
#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);
PageHeader *memory = reinterpret_cast<PageHeader *>(::new char[numBytesToAlloc]);
if (memory == nullptr)
{
return nullptr;
}
// Use placement-new to initialize header
new (memory) PageHeader(mInUseList, (numBytesToAlloc + mPageSize - 1) / mPageSize);
mInUseList = memory;
// 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 *>(memory) + 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)
void *alloc = malloc(numBytes + mAlignment - 1);
mStack.back().push_back(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.
PageHeader *memory;
if (mFreeList)
{
memory = mFreeList;
mFreeList = mFreeList->nextPage;
}
else
{
memory = reinterpret_cast<PageHeader *>(::new char[mPageSize]);
if (memory == nullptr)
{
return nullptr;
}
}
// Use placement-new to initialize header
new (memory) PageHeader(mInUseList, 1);
mInUseList = memory;
// 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)
new (memory) Allocation(numBytes, memory, mInUseList->lastAllocation);
mInUseList->lastAllocation = reinterpret_cast<Allocation *>(memory);
# endif
return Allocation::GetDataPointer(memory, mAlignment);
}
#endif
void PoolAllocator::lock()
{
ASSERT(!mLocked);
mLocked = true;
}
void PoolAllocator::unlock()
{
ASSERT(mLocked);
mLocked = false;
}
//
// 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