Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
BlockAllocatorWithGC< T > Class Template Reference

#include <elm/alloc/BlockAllocatorWithGC.h>

+ Inheritance diagram for BlockAllocatorWithGC< T >:

Public Member Functions

 BlockAllocatorWithGC (t::size chunk_size=1<< 20)
 
T * allocate (void)
 
virtual void destroy (T *p)
 
void destroy (void *p) override
 
- Public Member Functions inherited from AbstractBlockAllocatorWithGC
 AbstractBlockAllocatorWithGC (t::size block_size, t::size chunk_size=1<< 20)
 
virtual ~AbstractBlockAllocatorWithGC (void)
 
voidallocate (void)
 
bool needsCollect () const
 
bool isSync ()
 
void setSync ()
 
void setAsync ()
 
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
 
voidallocate (t::size size)
 
void free (void *block)
 

Protected Member Functions

bool mark (T *b)
 
- Protected Member Functions inherited from 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 AbstractBlockAllocatorWithGC
typedef struct elm::AbstractBlockAllocatorWithGC::free_t free_t
 
- Protected Attributes inherited from 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 cooperative 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).

The garbage collection can performed according 2 model:

  • in synchronous model, no automatic garbage collection is performed and the user has to call BlockAllocatorWithGC::collectGarbage() to start garbage collection at safe point in its code;
  • in asynchronous model, garbage collection is automatically launched as soon as new memory is required, possibly in middle of an operation that may make the marking of alive block tricky but discharge the user from managing garbage collection load.

To help a bit in synchronous mode, this class provides a function, needsCollect(), indicating if a garbage collection is needed or not. As a default, and for backward compatibility, this class starts with asynchronous model but setting it synchronous may prevent a lot of bugs.

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

◆ BlockAllocatorWithGC()

BlockAllocatorWithGC ( t::size  chunk_size = 1 << 20)
inline

Build a block allocator.

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

Member Function Documentation

◆ allocate()

T* allocate ( void  )
inline

◆ destroy() [1/2]

virtual void destroy ( T *  p)
inlinevirtual

◆ destroy() [2/2]

void destroy ( void p)
inlineoverridevirtual

This function is called each time a block is released to the free list to let user perform some cleanup.

Parameters
pPointer to the block.

Reimplemented from AbstractBlockAllocatorWithGC.

Referenced by BlockAllocatorWithGC< node_t >::destroy().

◆ mark()

bool mark ( T *  b)
inlineprotected

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:
elm::BlockAllocatorWithGC::BlockAllocatorWithGC
BlockAllocatorWithGC(t::size chunk_size=1<< 20)
Definition: BlockAllocatorWithGC.h:80
elm::BlockAllocatorWithGC::mark
bool mark(T *b)
Definition: BlockAllocatorWithGC.h:86
elm::imm::cons
list< T > cons(const T &h, list< T > t)
Definition: list.h:103
elm::AbstractBlockAllocatorWithGC::collect
virtual void collect(void)=0