Skip to content
Staon edited this page Apr 11, 2013 · 1 revision

The library implements an incremental mark and sweep algorithm for tracking of reachability of objects. The collector is a layer above the standard system new and delete operators and the collector can be combined with manual memory allocation or with another allocation schemes (like counted smart pointers).

The collector is implemented in the class ::OndraGC::Manager. There can be several collectors in one process but don't mixe objects between them (don't append a child object from one collector to a parent object of another collector).

The library defines smart pointers for tracking relations between objects. That's it. There is neither preprocessing of classes nor a need to write scanning functions manually. There are two smart pointers:

  1. root pointers - they are used to mark a reference to an object out of domain of the garbage collector,
  2. member pointers - they are used to mark a reference from one object to another object.

Programmer is responsible to select appropriate type of smart pointer. Objects kept by a root pointer are used as roots when the mark phase of collecting starts.

Each class, which instances want to be kept by the collector, must inherits from class ::OndraGC::Object. Multiple inheritance is supported. However, virtual inheritance not. The class implements several methods (i.e. gcManager() which returns pointer to the collector which the object belongs to) and redefines the new operator.

Job of the collector is not run automatically, but must be triggered by programmer (he must invoke a method of the collector to do one incremental "job quantum"). Thus it's possible to use bare C pointers instead of the smart pointers for temporary variables and function arguments (with all benefits like automatic conversions to parent type, using of forward declarations in headers and etc.). Usage of bare pointers is still exception safe because the memory block is tracked by the collector since the end of the new operator (former than the object is constructed). However, each object, which shall survive a job quantum, must be stored in a smart pointer.

Appropriate time, when run the quantum, depends on character of application. In example the garbage collector can be run in a periodical timer.

Clone this wiki locally