Elm
1.0
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
|
#include <elm/alloc/BlockAllocatorWithGC.h>
Public Member Functions | |
BlockAllocatorWithGC (t::size chunk_size=1<< 20) | |
T * | allocate (void) throw (BadAlloc) |
Public Member Functions inherited from elm::AbstractBlockAllocatorWithGC | |
AbstractBlockAllocatorWithGC (t::size block_size, t::size chunk_size=1<< 20) | |
virtual | ~AbstractBlockAllocatorWithGC (void) |
void * | allocate (void) throw (BadAlloc) |
void | collectGarbage (void) |
t::size | blockSize (void) const |
t::size | chunkSize (void) const |
int | freeCount (void) const |
int | totalCount (void) const |
int | usedCount (void) const |
void * | allocate (t::size size) throw (BadAlloc) |
void | free (void *block) |
Protected Member Functions | |
bool | mark (T *b) |
Protected Member Functions inherited from elm::AbstractBlockAllocatorWithGC | |
bool | mark (void *ptr) |
virtual void | collect (void)=0 |
virtual void | beginGC (void) |
virtual void | endGC (void) |
Additional Inherited Members | |
Protected Types inherited from elm::AbstractBlockAllocatorWithGC | |
typedef struct elm::AbstractBlockAllocatorWithGC::free_t | free_t |
Protected Attributes inherited from elm::AbstractBlockAllocatorWithGC | |
free_t * | free_list |
int | free_cnt |
This class provides an allocator of block of same size supporting garbage collection. The blocks are allocated with a call to BlockAllocatorWithGC::allocate(). When no more memory is available, a garbage collection is launched: it is performed with the help of the user class that must provides all living blocks by overriding the method BlockAllocatorWithGC::collect(). For each living block, this method must perform a call to BlockAllocatorWithGC::mark() to record it. If the block has already been collected, this method return false else, true returned and the block may be deeply collected (sub-blocks linked to this one must be collected in turn).
Below, a simple example implementing a-la Lisp / Scheme lists.
T | Type of the allocated blocks. |
elm::BlockAllocatorWithGC< T >::BlockAllocatorWithGC | ( | t::size | chunk_size = 1 << 20 | ) |
Build a block allocator.
chunk_size | Size of the memory chunks (1Mb as a default). |
T* elm::BlockAllocatorWithGC< T >::allocate | ( | void | ) | ||
throw | ( | BadAlloc | |||
) |
|
protected |
T *allocate BlockAllocatorWithGC::(void) throw(BadAlloc); Allocate a block. This method is very fast unless a garbage collection needs to be invoked.
BadAlloc | If no more memory is available. |
Mark the given block as living.
b | Block to mark as living. |