Elm  1.0
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
elm::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)
 
void * allocate (t::size size) throw (BadAlloc)
 
void free (void *block)
 
void clear (void)
 
mark_t mark (void)
 
void release (mark_t mark)
 
template<class T >
T * allocate (int n=1) throw (BadAlloc)
 

Static Public Attributes

static StackAllocator DEFAULT
 

Protected Types

typedef struct
elm::StackAllocator::chunk_t 
chunk_t
 

Protected Member Functions

virtual void * chunkFilled (t::size size) throw (BadAlloc)
 
t::size chunkSize (void) const
 
void newChunk (void) throw (BadAlloc)
 

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

This type represents the marks of the stack position.

Constructor & Destructor Documentation

elm::StackAllocator::StackAllocator ( t::size  size = 4096)

Build a new stack allocator.

Parameters
sizeThe size in bytes of the chunks (default to 4Kb).
elm::StackAllocator::~StackAllocator ( void  )
virtual

References clear().

Member Function Documentation

void * elm::StackAllocator::allocate ( t::size  size)
throw (BadAlloc
)

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 elm::max().

Referenced by elm::BlockAllocator::allocate(), and allocate().

template<class T >
T* elm::StackAllocator::allocate ( int  n = 1)
throw (BadAlloc
)

References allocate().

void * elm::StackAllocator::chunkFilled ( t::size  size)
throw (BadAlloc
)
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. BadAlloc In case of fatal allocation error.
t::size elm::StackAllocator::chunkSize ( void  ) const
protected
void elm::StackAllocator::clear ( void  )

Clear all allocated memory.

References elm::StackAllocator::chunk_t::next.

Referenced by ~StackAllocator().

void elm::StackAllocator::free ( void *  block)

Free the given block.

Parameters
blockBlock to free.
Note
In this allocator, do nothing.
StackAllocator::mark_t elm::StackAllocator::mark ( void  )

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 release().

void elm::StackAllocator::newChunk ( void  )
throw (BadAlloc
)
protected
void elm::StackAllocator::release ( mark_t  mark)

Release the stack onto the given mark.

Parameters
markStack position to free onto.

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

Member Data Documentation

StackAllocator elm::StackAllocator::DEFAULT
static

Default stack allocator.


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