// Object Type Class Definition // // File: obtype.h // Author: course // Version: 2 // This code is used only by modules that define new // types of LISP object. #ifndef OBTYPE_H #define OBTYPE_H #include "object.h" // Object Type Class Hierarchy: // // -------------------------------------- // Base | object_type | // Class: | | // | virtual functions: | // | initialize | // | get_free | // | put_used | // | clear_marked | // | mark | // | sweep | // | gc_print | // -------------------------------------- // | | | | | // Derived | | | | | // Classes: symbol_type | cons_type | ... // | | // fixnum_type function_type // // // // Each derived class of "object" has an associated // derived class of "object_type", and this later class // has ONLY ONE INSTANCE. Thus: // // symbol_type has the unique instance SYMBOL_TYPE // fixnum_type has the unique instance FIXNUM_TYPE // cons_type has the unique instance CONS_TYPE // function_type has the unique instance // FUNCTION_TYPE // // where all these names, SYMBOL_TYPE, FIXNUM_TYPE, etc // have the "type" "object_type *" and point at their // respective objects. // // The reason for all this is so we can define and // exercise functions, such as initialize, get_free, // etc., that can act on behalf of all objects of a // specific type, not just on one such object. E.g., // during initialization of lisp51 we execute: // // SYMBOL_TYPE->initialize() // FIXNUM_TYPE->initialize() // CONS_TYPE->initialize() // FUNCTION_TYPE->initialize() // ...->initialize() // (if someone defined additional LISP types) // // Similarly the garbage collector can call one sweep // function for each LISP type. Also, to do these // things we need not have any symbol, fixnum, cons, // or function object. // // All object_type class instances, SYMBOL_TYPE, // FIXNUM_TYPE, etc., are linked together in a list so // they can all be found. The constructor for any // class derived from object_type adds instances to this // list when they are constructed. Such instances must // never be destructed. // // Thus the virtual functions of a symbol object, such // as print and scavenge, work on that object; but the // virtual functions of SYMBOL_TYPE do things for all // symbol objects at once. class object_type { public: // Announce an error that an object does not have // a given type. Object may be NULL. // friend void type_error (object * ob, object_type * ty); public: // Should be protected but some compilers are // buggy about letting friends of a derived // class access protected members. object_type (char * name); // Construct object type with simple free and // used lists and the given name. The name // argument must be a never-freed character // string, as no copy is made of it. virtual void initialize() = 0; // Initialize the object type; e.g. allocate // symbols such as NIL and FUNCTION. virtual object * get_free(); // Return object from free list, or NULL if // none. Does no initialization. virtual void put_used (object * ob); // Put object on used list. virtual void clear_marked(); // Go through used lists clearing marked bits. virtual void mark(); // Go through used lists calling gc_mark on // objects if necessary. E.g. for symbols that // have bound value or function or non-NIL // plist. virtual void sweep(); // Sweep used lists to free list. Sets type // component of object to NULL to catch errors. virtual void gc_print(); // Print memory statistics for the type to *out; // typically one line containing type name and // number of elements on the free list. char * name; // LISP name of type, // e.g. "FIXNUM". object * free_list; // First free object; // or NULL if none. object * used_list; // First used object; or // NULL if none or this // used_list is unused. object_type * link; // Link for list of all // object_types. static object_type * list; // List of object_types // threaded by link. }; // Initialize all object types. First initializes NIL, // and then calls the object_type initializers that // allocate other symbols, etc. // void initialize_all_object_types (); #endif // OBTYPE_H