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::BlockAllocatorWithGC< T > Class Template Reference

#include <elm/alloc/BlockAllocatorWithGC.h>

+ Inheritance diagram for elm::BlockAllocatorWithGC< T >:

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_tfree_list
 
int free_cnt
 

Detailed Description

template<class T>
class elm::BlockAllocatorWithGC< T >

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.

class Cons {
public:
...
int hd;
Cons *tl;
};
class ConsAllocator: public BlockAllocatorWithGC<Cons> {
public:
...
protected:
virtual void collect(void) {
// for each list header hd
collect(hd);
}
private:
void collect(Cons *cons) {
while(mark(cons))
cons = cons->tl;
}
};
Parameters
TType of the allocated blocks.

Constructor & Destructor Documentation

template<class T>
elm::BlockAllocatorWithGC< T >::BlockAllocatorWithGC ( t::size  chunk_size = 1 << 20)

Build a block allocator.

Parameters
chunk_sizeSize of the memory chunks (1Mb as a default).

Member Function Documentation

template<class T>
T* elm::BlockAllocatorWithGC< T >::allocate ( void  )
throw (BadAlloc
)
template<class T>
bool elm::BlockAllocatorWithGC< T >::mark ( T *  b)
protected

T *allocate BlockAllocatorWithGC::(void) throw(BadAlloc); Allocate a block. This method is very fast unless a garbage collection needs to be invoked.

Returns
Allocated block.
Exceptions
BadAllocIf no more memory is available.

Mark the given block as living.

Parameters
bBlock to mark as living.
Returns
True if the block is already collected, false else. };

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