|
|
|
cobl_util
- Collectable Object ListsThe COBL_UTIL package implements a "poor man's" form of garbage collection; i.e., it provides a simple means of keeping track of allocated objects and of collecting or destroying those objects.
Keeping track of allocated objects is done by maintaining a list of collectable objects. Associated with each object in a list is an object destruction function that is called when the object is collected. Collectable object lists can be homogeneous (all objects in the list are of the same type) or heterogenous (objects in the list can be of different types). A homogeneous list is created by specifying an object destruction function when the list is created; a heterogeneous list is created by not specifying the destroy function:
#include "cobl_util.h" -- Collectable object lists. extern int objDestroy (void *object) ; ColObjList listOfAny, listOfOnly ; ... coblCreate (objDestroy, &listOfOnly) ; -- Only one type of object. coblCreate (NULL, &listOfAny) ; -- Any type of object.
Registering an object as collectable is simply a matter of adding it to the homogeneous list:
SomeType *object ; ... coblAdd (listOfOnly, (void *) object, NULL) ;
or to the heterogeneous list:
extern int destroySomeType (void *object) ; ... coblAdd (listOfAny, (void *) object, destroySomeType) ;
(As you might guess, the COBL_UTIL package knows nothing about
homogeneous and heterogeneous lists. The destroy function specified
when a list is created is simply a default that can be superseded on
a per-object basis in the coblAdd()
call.)
If necessary, you can remove the registration of a collectable object without destroying it:
coblDelete (listOfAny, object) ;
Destroying all of the objects in a list can be accomplished by collecting them:
coblCollect (listOfOnly) ;
leaving an empty list, or by destroying the list itself:
coblDestroy (listOfOnly) ;
In either case, the order in which objects are destroyed is the reverse of the order in which they were registered; in other words, last-registered, first-destroyed.
coblAdd()
- register an object as collectable.
coblCollect()
- destroy all members of a list of collectable objects.
coblCreate()
- create an empty list of collectable objects.
coblDelete()
- remove an object from a list of collectable objects.
coblDestroy()
- destroy a list of collectable objects.
cobl_util.c
cobl_util.h