The programmatic interface to the garbage collector involves two base classes, GC and GCObject . The base class GC provides an uniform interface to all types of collectors and memory managers while class GCObject provides the interface to all collectable classes. Table 1 contains a listing of the classes in our hierarchy.
Class | Purpose |
GCObject | Collectable object base class |
GC | Garbage collector base class |
CGC | Conservative garbage collector base class |
BGC | Bartlett style mostly copying collector |
MarkSweepGC | A mark-sweep collector |
UserHeap | A heap for pointerless object |
GCVerifier | A heap walker that verifies the integrity of a structure |
Class GCObject is extremely simple: it redefines the operators
new
and delete
to allocate memory from the default collector,
and declares a virtual method ``trace
'' to be defined by subclasses
(more on this later.)
Memory for a GCObject is allocated and freed using the usually
new
and delete
operators. Of course, freeing memory explicitly
using delete
is optional for many subclasses of GC .
class GCObject { public: inline void * operator new(size_t n, GC& gc = *GC::default_gc) { return gc.m_alloc(n); } inline void * operator new(size_t n, size_t N, GC& gc = *GC::default_gc) { return gc.m_alloc(n > N ? n : N); } inline void operator delete(void *); virtual void trace(GC *) = 0; };