Frobby 0.10.0
Loading...
Searching...
No Matches
Arena Class Reference

#include <Arena.h>

Public Member Functions

void * alloc (size_t size)
void freeTop (void *ptr)
void freeAndAllAfter (void *ptr)
template<class T>
pair< T *, T * > allocArrayNoCon (size_t elementCount)
template<class T>
pair< T *, T * > allocArray (size_t elementCount)
template<class T>
void freeTopArray (T *array, T *arrayEnd)
template<class T>
void freeTopArray (pair< T *, T * > p)
template<class T>
void freeArrayAndAllAfter (T *array, T *arrayEnd)
template<class T>
void freeArrayAndAllAfter (pair< T *, T * > p)
bool isEmpty () const

Static Public Member Functions

static Arena & getArena ()

Detailed Description

This is an arena allocator. Arena allocators are very fast at the cost of imposing limitations on how memory can be deallocated.

Allocation and deallocation must occur in stack order (LIFO). In other words, only the most recently allocated buffer that has not been deallocated yet can be deallocated. It is also possible to deallocate all buffers that were deallocated after a given buffer. In DEBUG mode stack order is enforced by ASSERTs.

Arena satisfies allocation requests out of a larger block of memory. When a block is exhausted another block must be allocated using new. This new block is at least twice the size of the previous block. Old blocks are never re-used though they will be deallocated if they become old. So the current block is replaced if and only if it becomes exhausted.

The scheme of geometric block growth is used because it allows a very fast implementation with excellent locality of reference. This can consume memory beyond that which the user of the Arena needs - all allocators have memory overhead. Optimal performance on both speed and memory consumption can usully be reached by all code using the same Arena object when that is possible given the stack-order limitation on deallocation.

All methods throw bad_alloc if backing memory allocation using new fails.

Member Function Documentation

◆ alloc()

void * Arena::alloc ( size_t size)
inline

Returns a pointer to a buffer of size bytes. Throws bad_alloc if that is not possible. All allocated and not freed buffers have unique addresses even when size is zero.

◆ allocArray()

template<class T>
pair< T *, T * > Arena::allocArray ( size_t elementCount)

As allocArrayNoCon except that constructors for the elements of the array are called. The constructors are called in increasing order of index. Constructed objects are destructed in reverse order if a constructor throws an exception.

◆ allocArrayNoCon()

template<class T>
pair< T *, T * > Arena::allocArrayNoCon ( size_t elementCount)

As alloc(elementCount * sizeof(T)). Constructors for the elements of the array are not called.

◆ freeAndAllAfter()

void Arena::freeAndAllAfter ( void * ptr)
inline

Frees the buffer pointed to by ptr and all not yet freed allocations that have happened since that buffer was allocated. ptr must not be null.

◆ freeArrayAndAllAfter() [1/2]

template<class T>
void Arena::freeArrayAndAllAfter ( pair< T *, T * > p)
inline

As freeTopArrayAndAllAfter(p.first, p.second).

◆ freeArrayAndAllAfter() [2/2]

template<class T>
void Arena::freeArrayAndAllAfter ( T * array,
T * arrayEnd )

As freeAndAllAfter(array) except that the elements of the array in the range (array, arrayEnd] are deconstructed in decreasing order of index. The destructors must not throw exceptions.

◆ freeTop()

void Arena::freeTop ( void * ptr)
inline

Frees the buffer pointed to by ptr. That buffer must be the most recently allocated buffer from this Arena that has not yet been freed. Double frees are not allowed. ptr must not be null.

◆ freeTopArray() [1/2]

template<class T>
void Arena::freeTopArray ( pair< T *, T * > p)
inline

As freeTopArray(p.first, p.second).

◆ freeTopArray() [2/2]

template<class T>
void Arena::freeTopArray ( T * array,
T * arrayEnd )

As freeTop(array) except that the elements of the array in the range (array, arrayEnd] are deconstructed in decreasing order of index. The destructors must not throw exceptions.

array and arrayEnd must not be zero.

◆ getArena()

Arena & Arena::getArena ( )
inlinestatic

Returns an arena object that can be used for non-thread safe scratch memory after static objects have been initialized. The default contract is that each function leaves this arena with the exact same objects allocated as before the function was entered. It is fine for functions to collaborate for example by using the arena to return variable size objects without calling new, though care should be used in such cases.

◆ isEmpty()

bool Arena::isEmpty ( ) const
inline

Returns true if there are no live allocations for this Arena.


The documentation for this class was generated from the following files: