Like Java, and unlike C, C++ and most traditional languages, Lisp uses garbage collection. The Tom's Lisp interpreter uses reference counting, probably the simplest form of GC. Each object counted contains a count of the number of pointers to it. RefCtObj is a base class for all memory-managed objects and holds the reference count. Class RefPtr behaves as a pointer to objects derived from RefCtObj. RefPtr controls the reference counts in the RefCtObj object they point to.
To control the reference counts in a controlled object, the count must be incremented whenever a new RefPtr object starts to point to that object, and must be decremented when that pointer is changed or destroyed. To make this happen, the RefPtr class has constructors, a destructor, and an assignment operator overload which manipulate the count inside the referenced object. If the count reaches zero, the object is deleted.
The template class Ptr<T> makes objects which behave as pointers to type T. This could be done with RefPtr alone, but that would require much casting.
Note that classed derived from RefCtObj should implement a static alloc
method to create an object and return a Ptr
refct.h
[Download]