63 void*
alloc(
size_t size);
149 inline bool isInBlock(
const void* ptr)
const;
155 IF_DEBUG(
bool debugIsValid(
const void* ptr)
const;)
165 IF_DEBUG(stack<void*> _debugAllocs;)
172 const size_t aligned = (value + decAlign) & (~decAlign);
188 if (size - 1 >= capacity) {
189 ASSERT(size == 0 || size > capacity);
212 ASSERT(!_debugAllocs.empty());
213 ASSERT(_debugAllocs.top() == ptr);
227 while (!_debugAllocs.empty() && ptr != _debugAllocs.top())
229 ASSERT(!_debugAllocs.empty());
230 ASSERT(_debugAllocs.top() == ptr);
242 const char* p =
static_cast<const char*
>(ptr);
243 const size_t offset =
static_cast<size_t>(p -
_blockBegin);
251 if (elementCount >
static_cast<size_t>(-1) /
sizeof(T))
253 const size_t size = elementCount *
sizeof(T);
254 ASSERT(size /
sizeof(T) == elementCount);
255 char* buffer =
static_cast<char*
>(
alloc(size));
256 T* array =
reinterpret_cast<T*
>(buffer);
257 T* arrayEnd =
reinterpret_cast<T*
>(buffer + size);
258 return make_pair(array, arrayEnd);
264 pair<T*, T*> p = allocArrayNoCon<T>(elementCount);
267 for (; it != p.second; ++it)
270 freeTopArray<T>(p.first, it);
276 #define new NEW_MACRO
282 ASSERT(array <= arrayEnd);
284 while (arrayEnd != array) {
294 ASSERT(array <= arrayEnd);
296 while (arrayEnd != array) {
This is an arena allocator.
static Arena & getArena()
Returns an arena object that can be used for non-thread safe scratch memory after static objects have...
struct Arena::Block _block
void freeArrayAndAllAfter(T *array, T *arrayEnd)
As freeAndAllAfter(array) except that the elements of the array in the range (array,...
void freeAndAllAfterFromOldBlock(void *ptr)
As Arena::freeAndAllAfter where ptr was allocated from an old block.
static Arena _scratchArena
void * alloc(size_t size)
Returns a pointer to a buffer of size bytes.
void discardPreviousBlock()
Free the memory for the previous block.
bool isEmpty() const
Returns true if there are no live allocations for this Arena.
pair< T *, T * > allocArrayNoCon(size_t elementCount)
As alloc(elementCount * sizeof(T)).
void freeTop(void *ptr)
Frees the buffer pointed to by ptr.
void freeTopArray(T *array, T *arrayEnd)
As freeTop(array) except that the elements of the array in the range (array, arrayEnd] are deconstruc...
static size_t alignNoOverflow(size_t value)
Rounds value up to the nearest multiple of MemoryAlignment.
void freeTopFromOldBlock(void *ptr)
As Arena::freeTop where ptr was allocated from an old block.
void freeArrayAndAllAfter(pair< T *, T * > p)
As freeTopArrayAndAllAfter(p.first, p.second).
void freeTopArray(pair< T *, T * > p)
As freeTopArray(p.first, p.second).
void growCapacity(size_t needed)
Allocate a new block with at least needed bytes.
void freeAndAllAfter(void *ptr)
Frees the buffer pointed to by ptr and all not yet freed allocations that have happened since that bu...
pair< T *, T * > allocArray(size_t elementCount)
As allocArrayNoCon except that constructors for the elements of the array are called.
static const size_t MemoryAlignment
The alignment that memory allocators must ensure.
size_t getFreeCapacity() const
bool hasPreviousBlock() const
Block * _previousBlock
one past last byte (aligned)
char * _blockEnd
pointer to first free byte (aligned)
char * _freeBegin
beginning of current block (aligned)
bool isInBlock(const void *ptr) const