The base class GC is slightly more complex, as it has to provide
a few different functionalities. The first service that class GC
must
provide is of course memory allocation and deallocation. As a time saving
device we can specify what the default collector is using the methods
get_default_gc and set_default_gc. Method GCObject::new
will use this collector by default, unless placement syntax is used.
Method GCObject::delete, on the other hand, can correctly infer the
proper collector to use by the address of the object.
The low level methods to allocate and deallocate memory are m_alloc
and free respectively. The programmers usually do not have to
use these methods directly.
The method to invoke a garbage collection of a specific level is
collect(int level). This forces an explicit collection.
Method grow_heap(size_t) can also be used to explicitly increase
the heap size of a collector. Depending of the actual behavior
of the subclasses, these methods may have different effects.
class GC {
protected:
static GC * default_gc;
public:
static GC& get_default_gc() { return *default_gc; }
static void set_default_gc(GC& gc) { default_gc = &gc; }
virtual void * m_alloc (size_t) = 0;
virtual void free (void *);
virtual void collect (int level = 0) = 0;
virtual void grow_heap (size_t) = 0;
static void garbage_collect() { default_gc->collect(); }
virtual void set_gc_ratio(int);
virtual void set_initial_heap_size (size_t);
virtual void set_min_heap_growth (size_t);
};