toka.nbk: Home | Index | Next Page: gc.c | Previous Page: FloatsLibrary


 garbage collector

The Toka implementation makes heavy use of dynamically allocated memory. To help avoid memory leaks, the memory allocation provides a very simple form of garbage collection.

The model is very simple. A list containing pointers to each allocation, and the corresponding size (in bytes) is kept. Each time memory is requested, the code adds the request to the list. When the list is filled, or if the allocation fails, the 16 oldest items are freed and all later entries are shifted down.

Obviously there is also a way to mark entries as permanent. This is done by keep (the gc_keep() function). This routine skims through the list of allocations (from most recent to oldest) looking for a specific pointer. When found, that entry (and any subsequent allocations -- in Toka these will almost always be subquotes, strings, etc) are removed from the list.

In actual practice things are just a little more complex. Toka actually maintains two lists; one for general allocations and a second one for internal use. The second one is only used for allocations that are known to be temporary.

In addition, the code will always leave the 16 newest allocations in each list alone to prevent them from falling out of scope.

Information regarding the current status of the garbage collection subsystem can be obtained via :gc (the gc_info() function).


toka.nbk: Home | Index | Next Page: gc.c | Previous Page: FloatsLibrary


Notebook exported on Sunday, 1 July 2007, 19:54:43 PM EDT