Frobby  0.9.5
Classes | Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | List of all members
Arena Class Reference

This is an arena allocator. More...

#include <Arena.h>

Classes

struct  Block
 

Public Member Functions

 Arena ()
 
 ~Arena ()
 
void * alloc (size_t size)
 Returns a pointer to a buffer of size bytes. More...
 
void freeTop (void *ptr)
 Frees the buffer pointed to by ptr. More...
 
void freeAndAllAfter (void *ptr)
 Frees the buffer pointed to by ptr and all not yet freed allocations that have happened since that buffer was allocated. More...
 
template<class T >
pair< T *, T * > allocArrayNoCon (size_t elementCount)
 As alloc(elementCount * sizeof(T)). More...
 
template<class T >
pair< T *, T * > allocArray (size_t elementCount)
 As allocArrayNoCon except that constructors for the elements of the array are called. More...
 
template<class T >
void 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. More...
 
template<class T >
void freeTopArray (pair< T *, T * > p)
 As freeTopArray(p.first, p.second). More...
 
template<class T >
void 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. More...
 
template<class T >
void freeArrayAndAllAfter (pair< T *, T * > p)
 As freeTopArrayAndAllAfter(p.first, p.second). More...
 
bool isEmpty () const
 Returns true if there are no live allocations for this Arena. More...
 

Static Public Member Functions

static ArenagetArena ()
 Returns an arena object that can be used for non-thread safe scratch memory after static objects have been initialized. More...
 

Private Member Functions

void growCapacity (size_t needed)
 Allocate a new block with at least needed bytes. More...
 
void freeTopFromOldBlock (void *ptr)
 As Arena::freeTop where ptr was allocated from an old block. More...
 
void freeAndAllAfterFromOldBlock (void *ptr)
 As Arena::freeAndAllAfter where ptr was allocated from an old block. More...
 
void discardPreviousBlock ()
 Free the memory for the previous block. More...
 

Static Private Member Functions

static size_t alignNoOverflow (size_t value)
 Rounds value up to the nearest multiple of MemoryAlignment. More...
 

Private Attributes

struct Arena::Block _block
 

Static Private Attributes

static Arena _scratchArena
 

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.

Definition at line 53 of file Arena.h.

Constructor & Destructor Documentation

◆ Arena()

Arena::Arena ( )

Definition at line 26 of file Arena.cpp.

◆ ~Arena()

Arena::~Arena ( )

Definition at line 29 of file Arena.cpp.

Member Function Documentation

◆ alignNoOverflow()

size_t Arena::alignNoOverflow ( size_t  value)
inlinestaticprivate

Rounds value up to the nearest multiple of MemoryAlignment.

This number must be representable in a size_t.

Definition at line 168 of file Arena.h.

◆ 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.

Definition at line 180 of file Arena.h.

◆ 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.

Definition at line 263 of file Arena.h.

◆ 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.

Definition at line 250 of file Arena.h.

◆ discardPreviousBlock()

void Arena::discardPreviousBlock ( )
private

Free the memory for the previous block.

Definition at line 98 of file Arena.cpp.

◆ 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.

Definition at line 224 of file Arena.h.

◆ freeAndAllAfterFromOldBlock()

void Arena::freeAndAllAfterFromOldBlock ( void *  ptr)
private

As Arena::freeAndAllAfter where ptr was allocated from an old block.

Definition at line 82 of file Arena.cpp.

◆ freeArrayAndAllAfter() [1/2]

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

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

Definition at line 110 of file Arena.h.

◆ 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.

Definition at line 292 of file Arena.h.

◆ 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.

Definition at line 209 of file Arena.h.

◆ freeTopArray() [1/2]

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

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

Definition at line 100 of file Arena.h.

◆ 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.

Definition at line 280 of file Arena.h.

◆ freeTopFromOldBlock()

void Arena::freeTopFromOldBlock ( void *  ptr)
private

As Arena::freeTop where ptr was allocated from an old block.

Definition at line 71 of file Arena.cpp.

◆ getArena()

static 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.

Definition at line 126 of file Arena.h.

◆ growCapacity()

void Arena::growCapacity ( size_t  needed)
private

Allocate a new block with at least needed bytes.

Definition at line 42 of file Arena.cpp.

◆ isEmpty()

bool Arena::isEmpty ( ) const
inline

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

Definition at line 117 of file Arena.h.

Member Data Documentation

◆ _block

struct Arena::Block Arena::_block
private

◆ _scratchArena

Arena Arena::_scratchArena
staticprivate

Definition at line 163 of file Arena.h.


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