30 while (
_block.hasPreviousBlock())
32 delete[]
_block._blockBegin;
44 size_t size = std::max(needed,
_block.getSize());
45 if (size > std::numeric_limits<size_t>::max() / 2)
48 const size_t minimumAlloc = 16 * 1024 -
sizeof(
Block) - 16;
49 size = std::max(size, minimumAlloc);
54 if (size > std::numeric_limits<size_t>::max() -
sizeof(
Block))
61 _block._previousBlock = previousBlock;
65 char* buffer =
new char[size +
sizeof(
Block)];
66 _block._blockBegin = buffer;
67 _block._freeBegin = buffer;
68 _block._blockEnd = buffer + size;
77 _block._previousBlock->_freeBegin =
static_cast<char*
>(ptr);
78 if (
_block._previousBlock->isEmpty())
87 while (!(
_block._previousBlock->isInBlock(ptr))) {
93 _block._previousBlock->_freeBegin =
static_cast<char*
>(ptr);
94 if (
_block._previousBlock->isEmpty())
100 Block* before =
_block._previousBlock->_previousBlock;
101 delete[]
_block._previousBlock->_blockBegin;
102 _block._previousBlock = before;
106bool Arena::Block::debugIsValid(
const void* ptr)
const {
107 return _blockBegin <= ptr && ptr < _freeBegin;
This is an arena allocator.
struct Arena::Block _block
void freeAndAllAfterFromOldBlock(void *ptr)
As Arena::freeAndAllAfter where ptr was allocated from an old block.
static Arena _scratchArena
void discardPreviousBlock()
Free the memory for the previous block.
void freeTopFromOldBlock(void *ptr)
As Arena::freeTop where ptr was allocated from an old block.
void growCapacity(size_t needed)
Allocate a new block with at least needed bytes.
This header file includes common definitions and is included as the first line of code in every imple...
static const size_t MemoryAlignment
The alignment that memory allocators must ensure.
Block * _previousBlock
one past last byte (aligned)
char * _blockEnd
pointer to first free byte (aligned)
char * _freeBegin
beginning of current block (aligned)