// Object Type Class Code // // File: obtype.cc // Author: {your name} <{your e-mail address}> // Assignment: 9 #include "lisp.h" #include "obtype.h" #include // List of object types. // object_type * object_type::list = NULL; // Initialize all object types. First initializes NIL, // and then calls the object type initializers that // allocate other symbols, etc. // void initialize_all_object_types () { // Because NIL is use to initialize other things, // it must be initialized first as a special case. // NIL = make_symbol ("NIL"); set_symbol_value ((symbol *)NIL, NIL); set_symbol_plist ((symbol *)NIL, NIL); // Now call oll object_type initialize members. // for ( object_type * obty = object_type::list; obty != NULL; obty = obty->link ) obty->initialize(); } // Call error indicating object is not of this type. // void type_error (object * ob, object_type * ty) { char buffer [100]; ostrstream s (buffer, 100); s << "Object is not a " << ty->name << ": " << ends; if (ob) error (s.str(), ob); else error (s.str(), "NULL"); } // Construct object type with simple free and used lists // and the given name. Put the object type on the // object_type::list. // object_type::object_type (char * name) { this->name = name; free_list = NULL; used_list = NULL; link = object_type::list; object_type::list = this; } // Return object from free list, or NULL if none. Does // no initialization. // object * object_type::get_free() { object * ob; if (ob = free_list) { free_list = ob->link; return ob; } else return NULL; } // Put object on used list. // void object_type::put_used (object * ob) { ob->link = used_list; used_list = ob; } // Go through used lists clearing marked bits. // void object_type::clear_marked() { // ***** Put gc code here for assignment 9 ***** } // Go through used lists calling gc_mark in special // cases, as for symbols with bound value or function or // non-NIL plist. // void object_type::mark() {} // Do nothing normally. // Sweep used lists to free list. // void object_type::sweep() { // ***** Put gc code here for assignment 9 ***** } // Print memory statistics for this type. // void object_type::gc_print() { unsigned free = 0; unsigned used = 0; object * p; for (p = free_list; p; p = p->link) ++ free; for (p = used_list; p; p = p->link) ++ used; *out << " " << name << ": free " << free << ", used " << used << endl; }