Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
StackAllocator Class Reference

#include <elm/alloc/StackAllocator.h>

Classes

struct  chunk_t
 
class  ChunkIter
 

Public Types

typedef char * mark_t
 

Public Member Functions

 StackAllocator (t::size size=4096)
 
virtual ~StackAllocator (void)
 
voidallocate (t::size size)
 
template<class T >
voidallocate ()
 
void free (void *block)
 
void clear (void)
 
mark_t mark (void)
 
void release (mark_t mark)
 
template<class T >
T * allocate (int n=1)
 

Static Public Attributes

static StackAllocator DEFAULT
 

Protected Types

typedef struct elm::StackAllocator::chunk_t chunk_t
 

Protected Member Functions

virtual voidchunkFilled (t::size size)
 
t::size chunkSize (void) const
 
void newChunk (void)
 

Detailed Description

A stack allocator allows to implement quick allocation scheme with even quicker de-allocation. The allocation is performed as in stack, moving a simple pointer. Conversely, the de-allocation is as quick as resetting this pointer to a previous position.

Note that the allocation is only bound by the system memory: the stack is split in chunks. Each time a chunk is full, a new one is allocated.

Member Typedef Documentation

◆ chunk_t

typedef struct elm::StackAllocator::chunk_t chunk_t
protected

◆ mark_t

This type represents the marks of the stack position.

Constructor & Destructor Documentation

◆ StackAllocator()

StackAllocator ( t::size  size = 4096)

Build a new stack allocator.

Parameters
sizeThe size in bytes of the chunks (default to 4Kb).

◆ ~StackAllocator()

~StackAllocator ( void  )
virtual

Member Function Documentation

◆ allocate() [1/3]

void* allocate ( )
inline

◆ allocate() [2/3]

T* allocate ( int  n = 1)
inline

◆ allocate() [3/3]

void * allocate ( t::size  size)

Allocate a new block.

Parameters
sizeSize of the block.
Returns
Allocated block.
Exceptions
BadAllocIf there is no more memory.
Note
The size must not be greater than the size of the chunks.

References StackAllocator::chunkFilled().

Referenced by BlockAllocator< T >::allocate().

◆ chunkFilled()

void * chunkFilled ( t::size  size)
protectedvirtual

This method is called when there is no more place in the current chunk. It may be overload to provide custom behavior of the allocator.

Parameters
sizeSize of block to allocate.
Returns
Allocated block. @throiw BadAlloc In case of fatal allocation error.

References StackAllocator::allocate(), and StackAllocator::newChunk().

Referenced by StackAllocator::allocate().

◆ chunkSize()

t::size chunkSize ( void  ) const
inlineprotected

◆ clear()

void clear ( void  )

Clear all allocated memory.

References StackAllocator::chunk_t::next.

Referenced by StackAllocator::~StackAllocator().

◆ free()

void free ( void block)
inline

Free the given block.

Parameters
blockBlock to free.
Note
In this allocator, do nothing.

◆ mark()

Return a mark on the current stack position. The returned mark may be used to free the next allocated object after the mark set.

Returns
Current stack position.

Referenced by StackAllocator::release().

◆ newChunk()

void newChunk ( void  )
protected

◆ release()

void release ( mark_t  mark)

Release the stack onto the given mark.

Parameters
markStack position to free onto.

References StackAllocator::chunk_t::buffer, StackAllocator::mark(), and StackAllocator::chunk_t::next.

Member Data Documentation

◆ DEFAULT

StackAllocator DEFAULT
static

Default stack allocator.


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