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;
};
public:
...
protected:
}
private:
}
};
- Parameters
-
T | Type of the allocated blocks. |