|
The previous section explained when and how to register with the garbage collector function. Similarly, global variables must usually be added to the set of reachable objects as well, if they are to survive garbage collections. In contrast to local variables, global variables are only made known to the garbage collector once after initialization, as their lifetime is that of the entire program. To add a global variable to the garbage collector's root set, the macro
|
|
|
Global_GC_Link(obj)
|
|
|
must be called with the properly initialized variable of type
Object. The macro takes the address of the specified object. An equivalent functional interface can also be used:
|
|
|
void Func_Global_GC_Link(Object *obj_ptr);
|
|
|
This function must be supplied the address of the global variable to be registered with the garbage collector. When writing extensions that maintain global
Object variables,
Global_GC_Link (or
Func_Global_GC_Link) is usually called from within the extension initialization function, immediately after each variable is assigned a value. For instance, the global Scheme vector
handlers that associate procedures with UNIX signals is initialized and GC-protected as follows:
|
|
|
void elk_init_unix_signal(void) {
|
|
handlers = Make_Vector(NSIG, False);
|
|
Global_GC_Link(handlers);
|
|
...
|
|
}
|
|
|
NSIG is the number of UNIX signal types as defined by the system include file. The signal handling Scheme procedures that are inserted into the vector later need not be registered with the garbage collector, because they are now reachable through another object which itself is reachable.
|