| 1 | /* $Header: d:/cvsroot/tads/tads3/VMOBJ.H,v 1.3 1999/07/11 00:46:58 MJRoberts Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1998, 2002 Michael J. Roberts. All Rights Reserved. |
| 5 | * |
| 6 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 7 | * on using and copying this software. |
| 8 | */ |
| 9 | /* |
| 10 | Name |
| 11 | vmobj.h - VM object memory manager |
| 12 | Function |
| 13 | |
| 14 | Notes |
| 15 | |
| 16 | Modified |
| 17 | 10/20/98 MJRoberts - Creation |
| 18 | */ |
| 19 | |
| 20 | /* define this symbol to collect and display GC statistics, for tuning */ |
| 21 | // #define VMOBJ_GC_STATS |
| 22 | |
| 23 | #ifndef VMOBJ_H |
| 24 | #define VMOBJ_H |
| 25 | |
| 26 | #include <stdlib.h> |
| 27 | #include <memory.h> |
| 28 | |
| 29 | #include "vmglob.h" |
| 30 | #include "vmtype.h" |
| 31 | #include "vmerr.h" |
| 32 | #include "vmerrnum.h" |
| 33 | |
| 34 | |
| 35 | /* ------------------------------------------------------------------------ */ |
| 36 | /* |
| 37 | * GC statistics conditional compilation macro |
| 38 | */ |
| 39 | #ifdef VMOBJ_GC_STATS |
| 40 | # define IF_GC_STATS(x) x |
| 41 | #else |
| 42 | # define IF_GC_STATS(x) |
| 43 | #endif |
| 44 | |
| 45 | |
| 46 | /* ------------------------------------------------------------------------ */ |
| 47 | /* |
| 48 | * If the VM_REGISTER_METACLASS macro hasn't been defined, define it |
| 49 | * now. This macro is defined different ways on different inclusions, |
| 50 | * but when it's not defined, it should always be defined to do nothing. |
| 51 | */ |
| 52 | #ifndef VM_REGISTER_METACLASS |
| 53 | # define VM_REGISTER_METACLASS(metaclass) |
| 54 | #endif |
| 55 | |
| 56 | /* ------------------------------------------------------------------------ */ |
| 57 | /* |
| 58 | * NOTE TO METACLASS IMPLEMENTORS - each final concrete class derived |
| 59 | * from CVmObject that is to be available to VM programs for static |
| 60 | * image file loading and/or dynamic creation (via the "NEW" |
| 61 | * instructions) must specify a VM_REGISTER_METACLASS declaration to |
| 62 | * register the metaclass. This declaration must occur in the |
| 63 | * metaclass's header file, and must be OUTSIDE of the region protected |
| 64 | * against multiple inclusion. The definition should look like this: |
| 65 | * |
| 66 | * VM_REGISTER_METACLASS("metaclass-string-name", CVmClassName) |
| 67 | * |
| 68 | * The string name must be the universally unique metaclass identifier |
| 69 | * string registered with the T3 VM Specification Maintainer. The class |
| 70 | * name is the C++ class (derived from CVmObject). |
| 71 | * |
| 72 | * Each CVmObject subclass must define certain static construction |
| 73 | * methods in addition to the virtual methods defined as abstract in |
| 74 | * CVmObject. See the comments on CVmObject for details. |
| 75 | */ |
| 76 | |
| 77 | |
| 78 | /* ------------------------------------------------------------------------ */ |
| 79 | /* |
| 80 | * To get a pointer to an object (CVmObject *) given an object ID |
| 81 | * (vm_obj_id_t), use this: |
| 82 | * |
| 83 | * CVmObject *obj = vm_objp(vmg_ id); |
| 84 | * |
| 85 | * To allocate a new object: |
| 86 | * |
| 87 | * vm_obj_id_t id = vm_newid(vmg_ in_root_set); |
| 88 | *. CVmObject *obj = new (vmg_ id) CVmObjXxx(constructor params); |
| 89 | * |
| 90 | * The functions vm_objp() and vm_newid() are defined later in this file. |
| 91 | */ |
| 92 | |
| 93 | |
| 94 | /* ------------------------------------------------------------------------ */ |
| 95 | /* |
| 96 | * Garbage collector work increment. This is the number of objects that |
| 97 | * the GC will process on each call to gc_pass_queue(). |
| 98 | * |
| 99 | * The point of running the GC incrementally is to allow GC work to be |
| 100 | * interleaved with long-running user I/O operations (such as reading a |
| 101 | * line of text from the keyboard) in the foreground thread, so the work |
| 102 | * increment should be chosen so that each call to this routine |
| 103 | * completes quickly enough that the user will perceive no delay. |
| 104 | * However, making this number too small will introduce additional |
| 105 | * overhead by making an excessive number of function calls. |
| 106 | */ |
| 107 | const int VM_GC_WORK_INCREMENT = 500; |
| 108 | |
| 109 | |
| 110 | |
| 111 | /* ------------------------------------------------------------------------ */ |
| 112 | /* |
| 113 | * flag values for propDefined() |
| 114 | */ |
| 115 | #define VMOBJ_PROPDEF_ANY 1 |
| 116 | #define VMOBJ_PROPDEF_DIRECTLY 2 |
| 117 | #define VMOBJ_PROPDEF_INHERITS 3 |
| 118 | #define VMOBJ_PROPDEF_GET_CLASS 4 |
| 119 | |
| 120 | /* |
| 121 | * flag values for explicit_to_string() |
| 122 | */ |
| 123 | #define TOSTR_UNSIGNED 0x0001 /* treat VM_INT values as unsigned */ |
| 124 | #define TOSTR_ROUND 0x0002 /* round floating point values to integer */ |
| 125 | |
| 126 | |
| 127 | /* ------------------------------------------------------------------------ */ |
| 128 | /* |
| 129 | * Objects are composed of two parts. The first is a fixed-size |
| 130 | * portion, or header, which consists of an abstract interface (in terms |
| 131 | * of stored data, this is simply a vtable pointer) and an extension |
| 132 | * pointer. The extension pointer points to the variable-size second |
| 133 | * part of the object, which contains all of the additional instance |
| 134 | * data for the object. |
| 135 | */ |
| 136 | |
| 137 | /* ------------------------------------------------------------------------ */ |
| 138 | /* |
| 139 | * |
| 140 | * The fixed-size parts, or object headers, are allocated from a set of |
| 141 | * arrays. A master array has a pointer to the sub-arrays, and each |
| 142 | * sub-array has a fixed number of slots for the fixed-size parts. |
| 143 | * Thus, it's very fast to find an object header given an object ID. |
| 144 | * |
| 145 | * Because each fixed-size part has an abstract interface, we can have |
| 146 | * multiple implementations for the objects. This would allow us, for |
| 147 | * example, to include native objects (with an appropriate interface) as |
| 148 | * though they were normal VM objects. It also allows us to have |
| 149 | * different types of implementations for VM objects. |
| 150 | * |
| 151 | * The fixed-size object parts never move in memory, so we can refer to |
| 152 | * them with pointers. We can efficiently re-use space as object |
| 153 | * headers are allocated and deleted, because a new object will always |
| 154 | * take up exactly the same size as a previous object whose space was |
| 155 | * freed. |
| 156 | * |
| 157 | * The fixed-size objects are always allocated within the page table, |
| 158 | * thus operator new is overridden to allocate memory within the page |
| 159 | * table. |
| 160 | */ |
| 161 | /* |
| 162 | * In addition to the virtual methods listed, every object must define a |
| 163 | * data member as follows: |
| 164 | * |
| 165 | * public: static class CVmMetaclass *metaclass_reg_; |
| 166 | * |
| 167 | * This must be a static singleton instance of the CVmMetaclass subclass |
| 168 | * (see below) for the CVmObject subclass. Each CVmObject subclass must |
| 169 | * have a corresponding CVmMetaclass subclass; the singleton member |
| 170 | * variable metaclass_reg_ provides the registration table entry that |
| 171 | * allows instances of this object to be dynamically linked from the |
| 172 | * image file. |
| 173 | */ |
| 174 | class CVmObject |
| 175 | { |
| 176 | friend class CVmVarHeap; |
| 177 | |
| 178 | public: |
| 179 | /* metaclass registration object (for the root object implementation) */ |
| 180 | static class CVmMetaclass *metaclass_reg_; |
| 181 | |
| 182 | /* |
| 183 | * Default implementation for calling a static property. We don't |
| 184 | * have any static properties at this level, so we'll simply return |
| 185 | * false to indicate that the property wasn't evaluated. |
| 186 | */ |
| 187 | static int call_stat_prop(VMG_ vm_val_t *retval, const uchar **pc_ptr, |
| 188 | uint *argc, vm_prop_id_t); |
| 189 | |
| 190 | /* get the registration object for this metaclass */ |
| 191 | virtual class CVmMetaclass *get_metaclass_reg() const |
| 192 | { return metaclass_reg_; } |
| 193 | |
| 194 | /* |
| 195 | * Get the image file version string for my metaclass object. This is |
| 196 | * the version of the metaclass that the image file actually depends |
| 197 | * upon, which might be an earlier version than the one actually |
| 198 | * present in the implementation. This can be used for varying |
| 199 | * behavior to preserve compatibility with older versions. |
| 200 | */ |
| 201 | const char *get_image_file_version(VMG0_) const; |
| 202 | |
| 203 | /* |
| 204 | * Is the image file version of the metaclass AT LEAST the given |
| 205 | * version? This returns true if the image file is dependent upon a |
| 206 | * version of the metaclass equal to or later than (higher than) the |
| 207 | * given version string. The string should be given in the usual |
| 208 | * metaclass format - "030001", for example. |
| 209 | */ |
| 210 | int image_file_version_ge(VMG_ const char *ver) |
| 211 | { return strcmp(get_image_file_version(vmg0_), ver) >= 0; } |
| 212 | |
| 213 | /* |
| 214 | * Is this object of the given metaclass? Returns true if the |
| 215 | * object is an instance of 'meta' or inherits from the metaclass. |
| 216 | * Each object class must override this. |
| 217 | */ |
| 218 | virtual int is_of_metaclass(class CVmMetaclass *meta) const |
| 219 | { return (meta == metaclass_reg_); } |
| 220 | |
| 221 | /* |
| 222 | * Receive notification that this object is being deleted - the |
| 223 | * garbage collector calls this function when the object is |
| 224 | * unreachable. |
| 225 | * |
| 226 | * Note that we don't use the real destructor, since we use our own |
| 227 | * memory management; instead, we have this virtual finalizer that |
| 228 | * we explicitly call when it's time to delete the object. (This |
| 229 | * isn't entirely symmetrical with the overridden operator new, but |
| 230 | * the GC is the only code that can delete objects, and this saves |
| 231 | * us the trouble of overriding operator delete for the object.) |
| 232 | */ |
| 233 | virtual void notify_delete(VMG_ int in_root_set) = 0; |
| 234 | |
| 235 | /* |
| 236 | * Create an instance of this class. If this object does not |
| 237 | * represent a class, or cannot be instanced, throw an error. By |
| 238 | * default, objects cannot be instanced, so we'll simply throw an |
| 239 | * error. If successful, leaves the new object in register R0. |
| 240 | * |
| 241 | * Parameters to the constructor are passed on the VM stack; 'argc' |
| 242 | * gives the number of arguments on the stack. This routine will |
| 243 | * remove the arguments from the stack before returning. |
| 244 | */ |
| 245 | virtual void create_instance(VMG_ vm_obj_id_t self, |
| 246 | const uchar **pc_ptr, uint argc) |
| 247 | { |
| 248 | /* throw the error */ |
| 249 | err_throw(VMERR_CANNOT_CREATE_INST); |
| 250 | } |
| 251 | |
| 252 | /* |
| 253 | * Determine if the object has a non-trivial finalizer. Returns |
| 254 | * true if the object has a non-trivial finalizer, false if it has |
| 255 | * no finalizer or the finalizer is trivial and hence can be |
| 256 | * ignored. We'll return false by default. |
| 257 | */ |
| 258 | virtual int has_finalizer(VMG_ vm_obj_id_t /*self*/) |
| 259 | { return FALSE; } |
| 260 | |
| 261 | /* |
| 262 | * Invoke the object's finalizer. This need do nothing if the |
| 263 | * object does not define or inherit a finalizer method. Any |
| 264 | * exceptions thrown in the course of executing the finalizer should |
| 265 | * be caught and discarded. By default, we'll do nothing at all. |
| 266 | */ |
| 267 | virtual void invoke_finalizer(VMG_ vm_obj_id_t /*self*/) { } |
| 268 | |
| 269 | /* |
| 270 | * Determine if this is a class object. This returns true if this |
| 271 | * object is a class, false if it's an instance. |
| 272 | */ |
| 273 | virtual int is_class_object(VMG_ vm_obj_id_t /*self*/) const |
| 274 | { return FALSE; } |
| 275 | |
| 276 | /* |
| 277 | * Determine if this object is an instance of another object. |
| 278 | * Returns true if this object derives from the other object, |
| 279 | * directly or indirectly. If this object derives from an object |
| 280 | * which in turn derives from the given object, then this object |
| 281 | * derives (indirectly) from the given object. |
| 282 | */ |
| 283 | virtual int is_instance_of(VMG_ vm_obj_id_t obj); |
| 284 | |
| 285 | /* |
| 286 | * Get the number of superclasses of the object, and get the nth |
| 287 | * superclass. By default, we have one superclass, which is the |
| 288 | * IntrinsicClass object that represents this metaclass. |
| 289 | */ |
| 290 | virtual int get_superclass_count(VMG_ vm_obj_id_t /*self*/) const |
| 291 | { return 1; } |
| 292 | virtual vm_obj_id_t get_superclass(VMG_ vm_obj_id_t /*self*/, |
| 293 | int /*superclass_index*/) const; |
| 294 | |
| 295 | /* |
| 296 | * Determine if the object has properties that can be enumerated. |
| 297 | * Returns true if so, false if not. Note that this should return |
| 298 | * true for an object of a type that provides properties, even if |
| 299 | * the instance happens to have zero properties. |
| 300 | */ |
| 301 | virtual int provides_props(VMG0_) const { return FALSE; } |
| 302 | |
| 303 | /* |
| 304 | * Enumerate properties of the object. Invoke the callback for each |
| 305 | * property. The callback is not permitted to make any changes to |
| 306 | * the object or its properties and should not invoke garbage |
| 307 | * collection. |
| 308 | */ |
| 309 | virtual void enum_props(VMG_ vm_obj_id_t self, |
| 310 | void (*cb)(VMG_ void *ctx, |
| 311 | vm_obj_id_t self, vm_prop_id_t prop, |
| 312 | const vm_val_t *val), |
| 313 | void *cbctx) |
| 314 | { |
| 315 | /* by default, assume we have nothing to enumerate */ |
| 316 | } |
| 317 | |
| 318 | /* set a property value */ |
| 319 | virtual void set_prop(VMG_ class CVmUndo *undo, |
| 320 | vm_obj_id_t self, vm_prop_id_t prop, |
| 321 | const vm_val_t *val) = 0; |
| 322 | |
| 323 | /* |
| 324 | * Get a property value. We do not evaluate the property, but |
| 325 | * merely get the raw value; thus, if the property value is of type |
| 326 | * code, we simply retrieve the code offset pointer. Returns true |
| 327 | * if the property was found, false if not. |
| 328 | * |
| 329 | * If we find the object, we'll set *source_obj to the ID of the |
| 330 | * object in which we found the object. If the property was |
| 331 | * supplied by this object directly, we'll simply set source_id to |
| 332 | * self; if we inherited the property from a superclass, we'll set |
| 333 | * *source_id to the ID of the superclass object that actually |
| 334 | * supplied the value. |
| 335 | * |
| 336 | * If 'argc' is not null, this function can consume arguments from |
| 337 | * the run-time stack, and set *argc to zero to indicate that the |
| 338 | * arguments have been consumed. |
| 339 | * |
| 340 | * If 'argc' is null, and evaluating the property would involve |
| 341 | * running system code (with or without consuming arguments), this |
| 342 | * function should return TRUE, but should NOT run the system code - |
| 343 | * instead, set val->typ to VM_NATIVE_CODE to indicate that the |
| 344 | * value is a "native code" value (i.e., evaluating it requires |
| 345 | * executing system code). |
| 346 | */ |
| 347 | virtual int get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val, |
| 348 | vm_obj_id_t self, vm_obj_id_t *source_obj, |
| 349 | uint *argc); |
| 350 | |
| 351 | /* |
| 352 | * Get the invocation routine. This retrieves the bytecode location to |
| 353 | * invoke when calling the object as though it were a function. If the |
| 354 | * object has a function interface, set up a FUNCPTR or CODEPTR value |
| 355 | * to point to the code to invoke. Regular objects aren't invokable. |
| 356 | * |
| 357 | * Returns true if this is an invokable object, false if not. 'val' |
| 358 | * can be a null pointer if the caller merely wishes to determine |
| 359 | * whether or not this object is invokable. |
| 360 | */ |
| 361 | virtual int get_invoker(VMG_ vm_val_t *val) { return FALSE; } |
| 362 | |
| 363 | /* get the interface to the given property */ |
| 364 | int get_prop_interface(VMG_ vm_obj_id_t self, vm_prop_id_t prop, |
| 365 | int &min_args, int &opt_args, int &varargs); |
| 366 | |
| 367 | /* |
| 368 | * Inherit a property value. This works similarly to get_prop, but |
| 369 | * finds an inherited definition of the property, as though |
| 370 | * orig_target_obj.prop (and anything overriding orig_target_obj.prop) |
| 371 | * were undefined. |
| 372 | * |
| 373 | * In broad terms, the algorithm for this method is to do the same |
| 374 | * thing as get_prop(), but to ignore every definition of the property |
| 375 | * found in the class tree until after reaching and skipping |
| 376 | * orig_target_obj.prop. Once orig_target_obj.prop is found, this |
| 377 | * method simply continues searching in the same manner as get_prop() |
| 378 | * and returns the next definition it finds. |
| 379 | * |
| 380 | * 'defining_obj' is the object containing the method currently |
| 381 | * running. This is not necessarily 'self', because the method |
| 382 | * currently running might already have been inherited from a |
| 383 | * superclass of 'self'. |
| 384 | * |
| 385 | * 'orig_target_obj' is the object that was originally targeted for the |
| 386 | * get_prop() operation that invoked the calling method (or invoked the |
| 387 | * method that inherited the calling method, or so on). This gives us |
| 388 | * the starting point in the search, so that we can continue the |
| 389 | * original inheritance tree search that started with get_prop(). |
| 390 | * |
| 391 | * 'argc' has the same meaning as for get_prop(). |
| 392 | * |
| 393 | * Objects that cannot be subclassed via byte-code can ignore this |
| 394 | * method. The base class implementation follows the inheritance chain |
| 395 | * of "modifier" objects, which allow byte-code methods to be plugged |
| 396 | * in under the native code class tree. |
| 397 | */ |
| 398 | virtual int inh_prop(VMG_ vm_prop_id_t prop, vm_val_t *retval, |
| 399 | vm_obj_id_t self, vm_obj_id_t orig_target_obj, |
| 400 | vm_obj_id_t defining_obj, vm_obj_id_t *source_obj, |
| 401 | uint *argc); |
| 402 | |
| 403 | /* |
| 404 | * Build a list of the properties directly defined on this object |
| 405 | * instance. On return, retval must be filled in with the new list |
| 406 | * object. |
| 407 | * |
| 408 | * Note that a self-reference must be pushed by the caller to protect |
| 409 | * against garbage collection of self while this routine is running. |
| 410 | * |
| 411 | * Most object types do not define any properties in the instance, so |
| 412 | * this default implementation will simply return an empty list. |
| 413 | * Classes that can define properties in instances (such as |
| 414 | * TadsObjects), and the "intrinsic class" class that represents |
| 415 | * classes, must override this to build their lists. |
| 416 | */ |
| 417 | virtual void build_prop_list(VMG_ vm_obj_id_t self, vm_val_t *retval); |
| 418 | |
| 419 | /* |
| 420 | * Mark all strongly-referenced objects. Calls |
| 421 | * obj_table->mark_refs() for each referenced object. |
| 422 | */ |
| 423 | virtual void mark_refs(VMG_ uint state) = 0; |
| 424 | |
| 425 | /* |
| 426 | * Remove stale weak references. For each weakly-referenced object, |
| 427 | * check to see if the object is marked as reachable; if not, it's |
| 428 | * about to be deleted, so forget the weak reference. |
| 429 | */ |
| 430 | virtual void remove_stale_weak_refs(VMG0_) = 0; |
| 431 | |
| 432 | /* |
| 433 | * Receive notification that the undo manager is creating a new |
| 434 | * savepoint. |
| 435 | */ |
| 436 | virtual void notify_new_savept() = 0; |
| 437 | |
| 438 | /* |
| 439 | * apply an undo record created by this object; if the record has |
| 440 | * any additional data associated with it (allocated by the object |
| 441 | * when the undo record was created), this should also discard the |
| 442 | * additional data |
| 443 | */ |
| 444 | virtual void apply_undo(VMG_ struct CVmUndoRecord *rec) = 0; |
| 445 | |
| 446 | /* |
| 447 | * Discard any extra information associated with this undo record. |
| 448 | * Note that this will not be called if apply_undo() is called, |
| 449 | * since apply_undo() is expected to discard any extra information |
| 450 | * itself after applying the record. |
| 451 | */ |
| 452 | virtual void discard_undo(VMG_ struct CVmUndoRecord *) { } |
| 453 | |
| 454 | /* |
| 455 | * Mark an object object reference. If this object keeps strong |
| 456 | * references, this should mark any object contained in the undo |
| 457 | * record's saved value as referenced; if this object keeps only |
| 458 | * weak references, this doesn't need to do anything. |
| 459 | */ |
| 460 | virtual void mark_undo_ref(VMG_ struct CVmUndoRecord *rec) = 0; |
| 461 | |
| 462 | /* |
| 463 | * Remove any stale weak reference contained in an undo record. For |
| 464 | * objects with ordinary strong references, this doesn't need to do |
| 465 | * anything. For objects that keep weak references to other |
| 466 | * objects, this should check the object referenced in the undo |
| 467 | * record, if any, to determine if the object is about to be |
| 468 | * deleted, and if so clear the undo record (by setting the object |
| 469 | * in the old value to 'invalid'). |
| 470 | */ |
| 471 | virtual void remove_stale_undo_weak_ref(VMG_ |
| 472 | struct CVmUndoRecord *rec) = 0; |
| 473 | |
| 474 | /* |
| 475 | * Post-load initialization. This routine is called only if the object |
| 476 | * specifically requests this by calling request_post_load_init(). |
| 477 | * This routine is called exactly once for each initial program load, |
| 478 | * restart, or restore, and is called after ALL objects are loaded. |
| 479 | * |
| 480 | * The purpose of this routine is to allow an object to perform |
| 481 | * initializations that depend upon other objects. During the normal |
| 482 | * load/restore/reset methods, an object cannot assume that any other |
| 483 | * objects are already loaded, because the order of loading is |
| 484 | * arbitrary. When this routine is called, it is guaranteed that all |
| 485 | * objects are already loaded. |
| 486 | */ |
| 487 | virtual void post_load_init(VMG_ vm_obj_id_t /*self*/) |
| 488 | { |
| 489 | /* we do nothing by default */ |
| 490 | } |
| 491 | |
| 492 | /* |
| 493 | * Load the object from an image file. The object's data block is |
| 494 | * at the given address and has the given size. |
| 495 | * |
| 496 | * The underlying memory is owned by the image file loader. The |
| 497 | * object must not attempt to deallocate this memory. |
| 498 | */ |
| 499 | virtual void load_from_image(VMG_ vm_obj_id_t self, |
| 500 | const char *ptr, size_t siz) = 0; |
| 501 | |
| 502 | /* |
| 503 | * Reload the object from an image file. The object's data block is at |
| 504 | * the given address and has the given size. Discards any changes made |
| 505 | * since the object was loaded and restores its state as it was |
| 506 | * immediately after it was loaded from the image file. By default, we |
| 507 | * do nothing. |
| 508 | * |
| 509 | * NOTE 1: this routine can be implemented instead of reset_to_image(). |
| 510 | * If an object doesn't have any other need to store a pointer to its |
| 511 | * image file data in its own extension, but the image file data is |
| 512 | * necessary to effect a reset, then this routine should be used, to |
| 513 | * reduce the size of the object's extension for non-image instances. |
| 514 | * |
| 515 | * NOTE 2: in order to use this routine, the object MUST call the |
| 516 | * object table's save_image_pointer() routine during the initial |
| 517 | * loading (i.e., in the object's load_from_image()). During a reset, |
| 518 | * the object table will only call this routine on objects whose image |
| 519 | * pointers were previously saved with save_image_pointer(). |
| 520 | */ |
| 521 | virtual void reload_from_image(VMG_ vm_obj_id_t self, |
| 522 | const char *ptr, size_t siz) { } |
| 523 | |
| 524 | /* |
| 525 | * Reset to the image file state. Discards any changes made since the |
| 526 | * object was loaded and restores its state as it was immediately |
| 527 | * after it was loaded from the image file. By default, we do nothing. |
| 528 | * |
| 529 | * NOTE: this routine doesn't have to do anything if |
| 530 | * reload_from_image() is implemented for the object. |
| 531 | */ |
| 532 | virtual void reset_to_image(VMG_ vm_obj_id_t /*self*/) { } |
| 533 | |
| 534 | /* |
| 535 | * Determine if the object has been changed since it was loaded from |
| 536 | * the image file. This can only be called for objects that were |
| 537 | * originally loaded from the image file. Returns true if the object's |
| 538 | * internal state has been changed since loading, false if the object |
| 539 | * is in exactly the same state that's stored in the image file. |
| 540 | * |
| 541 | * If this returns false, then it's not necessary to save the object to |
| 542 | * a saved state file, because the object's state can be restored |
| 543 | * simply by resetting to the image file state. |
| 544 | */ |
| 545 | virtual int is_changed_since_load() const { return FALSE; } |
| 546 | |
| 547 | /* |
| 548 | * save this object to a file, so that it can be restored to its |
| 549 | * current state via restore_from_file |
| 550 | */ |
| 551 | virtual void save_to_file(VMG_ class CVmFile *fp) = 0; |
| 552 | |
| 553 | /* |
| 554 | * Restore the state of the object from a file into which state was |
| 555 | * previously stored via save_to_file. This routine may need |
| 556 | * access to the memory manager because it may need to allocate |
| 557 | * memory to make room for the variable data. |
| 558 | */ |
| 559 | virtual void restore_from_file(VMG_ vm_obj_id_t self, |
| 560 | class CVmFile *fp, |
| 561 | class CVmObjFixup *fixups) = 0; |
| 562 | |
| 563 | /* |
| 564 | * Compare to another value for equality. Returns true if the value is |
| 565 | * equal, false if not. By default, we return true only if the other |
| 566 | * value is an object with the same ID (i.e., a reference to exactly |
| 567 | * the same instance). However, subclasses can override this to |
| 568 | * provide different behavior; the string class, for example, may |
| 569 | * override this so that it compares equal to any string object or |
| 570 | * constant containing the same string text. |
| 571 | * |
| 572 | * 'depth' has the same meaning as in calc_hash(). |
| 573 | */ |
| 574 | virtual int equals(VMG_ vm_obj_id_t self, const vm_val_t *val, |
| 575 | int /*depth*/) const |
| 576 | { |
| 577 | /* return true if the other value is a reference to this object */ |
| 578 | return (val->typ == VM_OBJ && val->val.obj == self); |
| 579 | } |
| 580 | |
| 581 | /* |
| 582 | * Compare magnitude of this object and another object. Returns a |
| 583 | * positive value if this object is greater than the other object, a |
| 584 | * negative value if this object is less than the other object, or |
| 585 | * zero if this object equals the other object. Throws an error if |
| 586 | * a magnitude comparison is not meaningful between the two objects. |
| 587 | * |
| 588 | * By default, magnitude comparisons between objects are not |
| 589 | * meaningful, so we throw an error. |
| 590 | */ |
| 591 | virtual int compare_to(VMG_ vm_obj_id_t /*self*/, const vm_val_t *) const |
| 592 | { |
| 593 | /* by default, magnitude comparisons between objects are illegal */ |
| 594 | err_throw(VMERR_INVALID_COMPARISON); |
| 595 | |
| 596 | /* the compiler doesn't know that we'll never get here */ |
| 597 | AFTER_ERR_THROW(return 0;) |
| 598 | } |
| 599 | |
| 600 | /* |
| 601 | * Calculate a hash value for the object. |
| 602 | * |
| 603 | * 'depth' is the recursion depth of the hash calculation. Objects |
| 604 | * that can potentially contain cyclical references back to themselves, |
| 605 | * and which follow those references to calculate the hash value |
| 606 | * recursively, must check the depth counter to see if it exceeds |
| 607 | * VM_MAX_TREE_DEPTH_EQ, throwing VMERR_TREE_TOO_DEEP_EQ if so; and |
| 608 | * they must increment the depth counter in the recursive traversals. |
| 609 | * Objects that don't traverse into their contents can ignore the depth |
| 610 | * counter. |
| 611 | */ |
| 612 | virtual uint calc_hash(VMG_ vm_obj_id_t self, int /*depth*/) const |
| 613 | { |
| 614 | /* |
| 615 | * by default, use a 16-bit hash of our object ID as the hash |
| 616 | * value |
| 617 | */ |
| 618 | return (uint)(((ulong)self & 0xffff) |
| 619 | ^ (((ulong)self & 0xffff0000) >> 16)); |
| 620 | } |
| 621 | |
| 622 | |
| 623 | /* |
| 624 | * Add a value to this object, returning the result in *result. This |
| 625 | * may create a new object to hold the result, or may modify the |
| 626 | * existing object in place, depending on the subclass implementation. |
| 627 | * 'self' is the object ID of this object. |
| 628 | * |
| 629 | * Returns true if the add was implemented, false if not. If the |
| 630 | * operation isn't implemented as a native op, don't throw an error - |
| 631 | * simply return false to tell the caller to try an overloaded operator |
| 632 | * in byte code. |
| 633 | */ |
| 634 | virtual int add_val(VMG_ vm_val_t * /*result*/, |
| 635 | vm_obj_id_t /*self*/, const vm_val_t * /*val*/) |
| 636 | { return FALSE; } |
| 637 | |
| 638 | /* |
| 639 | * Subtract a value from this object, returning the result in |
| 640 | * *result. This may create a new object to hold the result, or may |
| 641 | * modify the existing object in place, depending upon the subclass |
| 642 | * implementation. 'self' is the object ID of this object. |
| 643 | */ |
| 644 | virtual int sub_val(VMG_ vm_val_t * /*result*/, |
| 645 | vm_obj_id_t /*self*/, const vm_val_t * /*val*/) |
| 646 | { return FALSE; } |
| 647 | |
| 648 | /* multiply this object by a value, returning the result in *result */ |
| 649 | virtual int mul_val(VMG_ vm_val_t * /* result*/, |
| 650 | vm_obj_id_t /*self*/, const vm_val_t * /*val*/) |
| 651 | { return FALSE; } |
| 652 | |
| 653 | /* divide a value into this object, returning the result in *result */ |
| 654 | virtual int div_val(VMG_ vm_val_t * /* result*/, |
| 655 | vm_obj_id_t /*self*/, const vm_val_t * /*val*/) |
| 656 | { return FALSE; } |
| 657 | |
| 658 | /* get the arithmetic negative of self, returning the result in *result */ |
| 659 | virtual int neg_val(VMG_ vm_val_t * /* result*/, vm_obj_id_t /*self*/) |
| 660 | { return FALSE; } |
| 661 | |
| 662 | /* |
| 663 | * Index with overloading. If the object implements native indexing, |
| 664 | * we'll invoke that; otherwise we'll check for operator[] and invoke |
| 665 | * that if present; otherwise we'll throw an error. |
| 666 | */ |
| 667 | void index_val_ov(VMG_ vm_val_t *result, vm_obj_id_t self, |
| 668 | const vm_val_t *index_val); |
| 669 | |
| 670 | |
| 671 | /* index the object, throwing an error if not implemented for the type */ |
| 672 | void index_val(VMG_ vm_val_t *result, vm_obj_id_t self, |
| 673 | const vm_val_t *index_val) |
| 674 | { |
| 675 | if (!index_val_q(vmg_ result, self, index_val)) |
| 676 | err_throw(VMERR_CANNOT_INDEX_TYPE); |
| 677 | } |
| 678 | |
| 679 | /* |
| 680 | * Index the object, query mode: retrieve the indexed value and return |
| 681 | * true if the object supports the operation, or simply return false |
| 682 | * (with no error) if not. Most object types cannot be indexed, so by |
| 683 | * default we'll return false to indicate this is not an implemented |
| 684 | * operator. Subclasses that can be indexed (such as lists) should |
| 685 | * look up the element given by the index value, and store the value at |
| 686 | * that index in *result. |
| 687 | */ |
| 688 | virtual int index_val_q(VMG_ vm_val_t * /*result*/, |
| 689 | vm_obj_id_t /*self*/, |
| 690 | const vm_val_t * /*index_val*/) |
| 691 | { return FALSE; } |
| 692 | |
| 693 | /* |
| 694 | * Assign to an indexed element of the object, with overloading. If |
| 695 | * the object implements native indexing, we'll invoke that; otherwise |
| 696 | * we'll check for operator[]= and invoke that if present; otherwise |
| 697 | * we'll throw an error. |
| 698 | */ |
| 699 | void set_index_val_ov(VMG_ vm_val_t *new_container, vm_obj_id_t self, |
| 700 | const vm_val_t *index_val, const vm_val_t *new_val); |
| 701 | |
| 702 | /* |
| 703 | * Set an indexed element of the object, and fill in *new_container |
| 704 | * with the new object that results if an entire new object is |
| 705 | * created to hold the modified contents, or with the original |
| 706 | * object if the contents of the original object are actually |
| 707 | * modified by this operation. Lists, for example, cannot be |
| 708 | * modified, so always create a new list object when an element is |
| 709 | * set; arrays, in contrast, can be directly modified, so will |
| 710 | * simply return 'self' in *new_container. |
| 711 | * |
| 712 | * By default, we'll throw an error, since default objects cannot be |
| 713 | * indexed. |
| 714 | */ |
| 715 | void set_index_val(VMG_ vm_val_t *new_container, vm_obj_id_t self, |
| 716 | const vm_val_t *index_val, const vm_val_t *new_val) |
| 717 | { |
| 718 | if (!set_index_val_q(vmg_ new_container, self, index_val, new_val)) |
| 719 | err_throw(VMERR_CANNOT_INDEX_TYPE); |
| 720 | } |
| 721 | |
| 722 | /* |
| 723 | * Set an index element of an object, query mode. Returns true (and |
| 724 | * performs the set-index operation) if this is a supported operator, |
| 725 | * false if not. |
| 726 | */ |
| 727 | virtual int set_index_val_q(VMG_ vm_val_t * /*new_container*/, |
| 728 | vm_obj_id_t /*self*/, |
| 729 | const vm_val_t * /*index_val*/, |
| 730 | const vm_val_t * /*new_val*/) |
| 731 | { return FALSE; } |
| 732 | |
| 733 | /* |
| 734 | * Get the next iterator value. If another value is available, fills |
| 735 | * in *val with the value and returns true. If not, returns false |
| 736 | * without changing *val. |
| 737 | * |
| 738 | * For an ordinary object, we call the method isNextAvailable(); if |
| 739 | * that returns true, we'll call getNext(), fill in *val with its |
| 740 | * return value, and return true, otherwise we'll return false. |
| 741 | * |
| 742 | * The built-in iterator classes override this for better performance. |
| 743 | */ |
| 744 | virtual int iter_next(VMG_ vm_obj_id_t self, vm_val_t *val); |
| 745 | |
| 746 | /* |
| 747 | * Cast the value to integer. If there's a suitable integer |
| 748 | * representation, return it; otherwise throw VMERR_NO_INT_CONV. |
| 749 | * |
| 750 | * For a string, parse the string and return the integer value. For a |
| 751 | * BigNumber, round to the nearest integer. Most other types do not |
| 752 | * have an integer representation. |
| 753 | */ |
| 754 | virtual long cast_to_int(VMG0_) const |
| 755 | { |
| 756 | /* by default, just throw a "cannot convert" error */ |
| 757 | err_throw(VMERR_NO_INT_CONV); |
| 758 | |
| 759 | /* we can't get here, but the compiler doesn't always know that */ |
| 760 | AFTER_ERR_THROW(return 0;) |
| 761 | } |
| 762 | |
| 763 | /* |
| 764 | * Cast the value to a numeric type. Fills in 'val' with the new |
| 765 | * value. If there's a suitable integer representation, return it; |
| 766 | * otherwise, if there's a BigNumber representation, return it; if |
| 767 | * there's some other numeric type similar to integer or BigNumber that |
| 768 | * we can convert to, return that conversion; otherwise throw |
| 769 | * VMERR_NO_NUM_CONV. |
| 770 | * |
| 771 | * Currently, the only types that count as numeric are integer (VM_INT) |
| 772 | * and BigNumber. This interface is designed to be extensible if new |
| 773 | * numeric types are added in the future. Basically, the idea is that |
| 774 | * we should return (a) the original value, if it's already numeric, or |
| 775 | * (b) the "simplest" numeric type that can precisely represent the |
| 776 | * value. By "simplest", we mean the type that uses the least memory |
| 777 | * to represent the given value and is most efficient for computations. |
| 778 | * VM_INT is much more efficient than BigNumber, since VM_INT types |
| 779 | * don't require memory allocations and perform their arithmetic with |
| 780 | * native hardware integer arithmetic. If we ever were to add a native |
| 781 | * "double" type, that would rank between VM_INT and BigNumber, since |
| 782 | * even native hardware doubles are generally slower than native |
| 783 | * integers for computations, but they're still much faster than |
| 784 | * BigNumber. |
| 785 | */ |
| 786 | virtual void cast_to_num(VMG_ vm_val_t *val, vm_obj_id_t self) const |
| 787 | { |
| 788 | /* by default, just throw a no-conversion error */ |
| 789 | err_throw(VMERR_NO_NUM_CONV); |
| 790 | } |
| 791 | |
| 792 | /* |
| 793 | * Get a string representation of the object. If necessary, this |
| 794 | * can create a new string object to represent the result. |
| 795 | * |
| 796 | * Returns the string representation in portable string format: the |
| 797 | * first two bytes give the byte length of the rest of the string in |
| 798 | * portable UINT2 format, and immediately following the length |
| 799 | * prefix are the bytes of the string's contents. The length prefix |
| 800 | * does not count the length prefix itself in the length of the |
| 801 | * string. |
| 802 | * |
| 803 | * If a new string object is created, new_str must be set to a |
| 804 | * reference to the new string object. If a pointer into our data |
| 805 | * is returned, we must set new_str to self. If no object data are |
| 806 | * involved, new_str can be set to nil. |
| 807 | * |
| 808 | * If it is not possible to create a string representation of the |
| 809 | * object, throw an error (VMERR_NO_STR_CONV). |
| 810 | * |
| 811 | * By default, we'll throw an error indicating that the object |
| 812 | * cannot be converted to a string. |
| 813 | */ |
| 814 | virtual const char *cast_to_string(VMG_ vm_obj_id_t self, |
| 815 | vm_val_t *new_str) const; |
| 816 | |
| 817 | /* |
| 818 | * Same as cast_to_string, but the conversion is explicitly due to a |
| 819 | * toString() call with a radix. By default, we'll simply use the |
| 820 | * basic cast_to_string() processing, but objects representing numbers |
| 821 | * (e.g., BigNumber) should override this to respect the radix. |
| 822 | * |
| 823 | * 'flags' is a combination of TOSTR_xxx bit flags: |
| 824 | * |
| 825 | *. TOSTR_UNSIGNED - interpret a VM_INT value as unsigned |
| 826 | *. TOSTR_ROUND - round a floating point value to the nearest integer |
| 827 | */ |
| 828 | virtual const char *explicit_to_string( |
| 829 | VMG_ vm_obj_id_t self, vm_val_t *new_str, |
| 830 | int /*radix*/, int /*flags*/) const |
| 831 | { return cast_to_string(vmg_ self, new_str); } |
| 832 | |
| 833 | /* |
| 834 | * Is this a numeric type? Returns true if the value represents some |
| 835 | * kind of number, false if not. For example, the BigNumber type |
| 836 | * returns true. Types that are merely convertible to numbers don't |
| 837 | * count as numeric types; for example, String isn't a numeric type |
| 838 | * even though some string values can be parsed as numbers. |
| 839 | */ |
| 840 | virtual int is_numeric() const { return FALSE; } |
| 841 | |
| 842 | /* |
| 843 | * Get the integer value of the object, if it has one. Returns true if |
| 844 | * there's an integer value, false if not. |
| 845 | * |
| 846 | * This differs from cast_to_int() in that we only convert values that |
| 847 | * are already scalar numbers. In particular, we don't attempt to |
| 848 | * convert string values. This is a weaker type of conversion than |
| 849 | * casting, for use in situations where different types might be |
| 850 | * allowed, but the only type of numeric value allowed is an integer |
| 851 | * value. |
| 852 | */ |
| 853 | virtual int get_as_int(long *val) const { return FALSE; } |
| 854 | |
| 855 | /* |
| 856 | * Get the 'double' value of the object, if it has one. Returns true |
| 857 | * if there's a double value, false if not. |
| 858 | * |
| 859 | * This only converts values that are already scalar numbers. We don't |
| 860 | * parse strings, for example. |
| 861 | */ |
| 862 | virtual int get_as_double(VMG_ double *val) const { return FALSE; } |
| 863 | |
| 864 | /* |
| 865 | * Promote an integer to the type of this object. For example, if |
| 866 | * 'this' is a BigNumber, this creates a BigNumber representation of |
| 867 | * the integer. This allows us to perform arithmetic where the left |
| 868 | * operand is an integer and the right operand is a different numeric |
| 869 | * type. '*val' contains the integer value on entry, and we replace it |
| 870 | * with the promoted value. |
| 871 | */ |
| 872 | virtual void promote_int(VMG_ vm_val_t * /*val*/) const |
| 873 | { err_throw(VMERR_NUM_VAL_REQD); } |
| 874 | |
| 875 | /* |
| 876 | * Get the list contained in the object, if possible, or null if |
| 877 | * not. If the value contained in the object is a list, this |
| 878 | * returns a pointer to the list value in portable format (the first |
| 879 | * two bytes are the length prefix as a portable UINT2, and the |
| 880 | * subsequent bytes form a packed array of data holders in portable |
| 881 | * format). |
| 882 | * |
| 883 | * Most object classes will return null for this, since they do not |
| 884 | * store lists. By default, we return null. |
| 885 | */ |
| 886 | virtual const char *get_as_list() const { return 0; } |
| 887 | |
| 888 | /* |
| 889 | * Is this a list-like object? Returns true if the object has a length |
| 890 | * and is indexable with index values 1..length. |
| 891 | */ |
| 892 | virtual int is_listlike(VMG_ vm_obj_id_t self); |
| 893 | |
| 894 | /* |
| 895 | * For a list-like object, get the number of elements. Returns -1 if |
| 896 | * this isn't a list-like object after all. |
| 897 | */ |
| 898 | virtual int ll_length(VMG_ vm_obj_id_t self); |
| 899 | |
| 900 | /* |
| 901 | * Get the string contained in the object, if possible, or null if |
| 902 | * not. If the value contained in the object is a string, this |
| 903 | * returns a pointer to the string value in portable format (the |
| 904 | * first two bytes are the string prefix as a portable UINT2, and |
| 905 | * the bytes of the string's text immediately follow in UTF8 format). |
| 906 | * |
| 907 | * Most object classes will return null for this, since they do not |
| 908 | * store strings. By default, we return null. |
| 909 | */ |
| 910 | virtual const char *get_as_string(VMG0_) const { return 0; } |
| 911 | |
| 912 | /* |
| 913 | * Rebuild the image data for the object. This is used to write the |
| 914 | * program state to a new image file after executing the program for |
| 915 | * a while, such as after a 'preinit' step after compilation. |
| 916 | * |
| 917 | * This should write the complete metaclass-specific record needed |
| 918 | * for an OBJS data block entry, not including the generic header. |
| 919 | * |
| 920 | * On success, return the size in bytes of the data stored to the |
| 921 | * buffer; these bytes must be copied directly to the new image |
| 922 | * file. If the given buffer isn't big enough, this should return |
| 923 | * the size needed and otherwise leave the buffer empty. If the |
| 924 | * object doesn't need to be written at all, this should return |
| 925 | * zero. |
| 926 | */ |
| 927 | virtual ulong rebuild_image(VMG_ char *, ulong) |
| 928 | { return 0; } |
| 929 | |
| 930 | /* |
| 931 | * Allocate space for conversion to constant data for storage in a |
| 932 | * rebuilt image file. If this object can be stored as constant |
| 933 | * data in a rebuilt image file, this should reserve space in the |
| 934 | * provided constant mapper object for the object's data in |
| 935 | * preparation for conversion. |
| 936 | * |
| 937 | * Most objects cannot be converted to constant data, so the default |
| 938 | * here does nothing. Those that can, such as strings and lists, |
| 939 | * should override this. |
| 940 | */ |
| 941 | virtual void reserve_const_data(VMG_ class CVmConstMapper *, |
| 942 | vm_obj_id_t /*self*/) |
| 943 | { /* default does nothing */ } |
| 944 | |
| 945 | /* |
| 946 | * Convert to constant data. If this object can be stored as |
| 947 | * constant data in a rebuilt image file, it should override this |
| 948 | * routine to store its data in the provided constant mapper object. |
| 949 | * If this object makes reference to other objects, it must check |
| 950 | * each object that it references to determine if the object has a |
| 951 | * non-zero address in the mapper, and if so must convert its |
| 952 | * reference to the object to a constant data item at the given |
| 953 | * address. There is no default implementation of this routine, |
| 954 | * because it must be overridden by essentially all objects (at |
| 955 | * least, it must be overridden by objects that can be converted or |
| 956 | * that can refer to objects that can be converted). |
| 957 | */ |
| 958 | virtual void convert_to_const_data(VMG_ class CVmConstMapper *, |
| 959 | vm_obj_id_t /*self*/) |
| 960 | { /* default does nothing */ } |
| 961 | |
| 962 | /* |
| 963 | * Get the type that this object uses when converted to constant |
| 964 | * data. Objects that can be converted to constant data via |
| 965 | * convert_to_const_data() should return the appropriate type |
| 966 | * (VM_SSTRING, VM_LIST, etc); others can return VM_NIL to indicate |
| 967 | * that they have no conversion. This will be called only when an |
| 968 | * object actually has been converted as indicated in a mapper |
| 969 | * (CVmConstMapper) object. We'll return NIL by default. |
| 970 | */ |
| 971 | virtual vm_datatype_t get_convert_to_const_data_type() const |
| 972 | { return VM_NIL; } |
| 973 | |
| 974 | |
| 975 | /* |
| 976 | * Allocate memory for the fixed part from a memory manager. We |
| 977 | * override operator new so that we can provide custom memory |
| 978 | * management for object headers. |
| 979 | * |
| 980 | * To create an object, use a sequence like this: |
| 981 | * |
| 982 | * obj_id = mem_mgr->get_obj_table()->alloc_obj(vmg_ in_root_set); |
| 983 | *. new (vmg_ obj_id) CVmObjString(subclass constructor params) |
| 984 | * |
| 985 | * The caller need not store the result of 'new'; the caller |
| 986 | * identifies the object by the obj_id value. |
| 987 | * |
| 988 | * Each CVmObject subclass should provide static methods that cover |
| 989 | * the above sequence, since it's a bit verbose to sprinkle |
| 990 | * throughout client code. |
| 991 | */ |
| 992 | void *operator new(size_t siz, VMG_ vm_obj_id_t obj_id); |
| 993 | |
| 994 | protected: |
| 995 | /* |
| 996 | * Find a modifier property. This is an internal service routine that |
| 997 | * we use to traverse the hierarchy of modifier objects associated with |
| 998 | * our intrinsic class hierarchy. |
| 999 | */ |
| 1000 | int find_modifier_prop(VMG_ vm_prop_id_t prop, vm_val_t *retval, |
| 1001 | vm_obj_id_t self, vm_obj_id_t orig_target_obj, |
| 1002 | vm_obj_id_t defining_obj, vm_obj_id_t *source_obj, |
| 1003 | uint *argc); |
| 1004 | |
| 1005 | /* |
| 1006 | * Service routine - check a get_prop() argument count for executing |
| 1007 | * a native code method implementation. If 'argc' is null, we'll |
| 1008 | * set the value to a VM_NATIVE_CODE indication and return true, |
| 1009 | * which the caller should do from get_prop() as well. If 'argc' is |
| 1010 | * non-null, we'll check it against the given required argument |
| 1011 | * count, and throw an error if it doesn't match. If 'argc' is |
| 1012 | * non-null and it matches the given argument count, we'll set *argc |
| 1013 | * to zero to indicate that we've consumed the arguments, and return |
| 1014 | * false - get_prop() should in this case execute the native code |
| 1015 | * and return the appropriate result value. |
| 1016 | */ |
| 1017 | static int get_prop_check_argc(vm_val_t *val, uint *argc, |
| 1018 | const CVmNativeCodeDesc *desc) |
| 1019 | { |
| 1020 | /* |
| 1021 | * if there's no 'argc', we can't execute the native code - we're |
| 1022 | * simply being asked for a description of the method, not to |
| 1023 | * evaluate its result |
| 1024 | */ |
| 1025 | if (argc == 0) |
| 1026 | { |
| 1027 | /* indicate a native code evaluation is required */ |
| 1028 | val->set_native(desc); |
| 1029 | |
| 1030 | /* tell get_prop() to return without further work */ |
| 1031 | return TRUE; |
| 1032 | } |
| 1033 | |
| 1034 | /* check the argument count - throw an error if out of range */ |
| 1035 | if (!desc->args_ok(*argc)) |
| 1036 | err_throw(VMERR_WRONG_NUM_OF_ARGS); |
| 1037 | |
| 1038 | /* everything's fine - consume the arguments */ |
| 1039 | *argc = 0; |
| 1040 | |
| 1041 | /* tell get_prop() to proceed with the native evaluation */ |
| 1042 | return FALSE; |
| 1043 | } |
| 1044 | |
| 1045 | #if 0 |
| 1046 | /* |
| 1047 | * Important note: |
| 1048 | * |
| 1049 | * This function has been removed because we no longer consider it |
| 1050 | * likely that we will ever wish to implement a relocating heap |
| 1051 | * manager. Given the large memory sizes of modern computers, and |
| 1052 | * recent academic research calling into question the conventional |
| 1053 | * wisdom that heap fragmentation is actually a problem in practical |
| 1054 | * applications, we no longer feel that maintaining the possibility of |
| 1055 | * a relocating heap manager is justified. |
| 1056 | * |
| 1057 | * Note that some code in the present implementation assumes that the |
| 1058 | * heap is non-relocating, so if a relocating heap manager is ever |
| 1059 | * implemented, those assumptions will have to be addressed. For |
| 1060 | * example, the CVmObjTads code stores direct object pointers inside |
| 1061 | * its objects, which is only possible with a non-relocating heap. |
| 1062 | */ |
| 1063 | |
| 1064 | /* |
| 1065 | * Adjust the extension pointer for a change - this must be called by |
| 1066 | * the variable heap page when compacting the heap or when the object |
| 1067 | * must be reallocated. |
| 1068 | * |
| 1069 | * This routine can be called ONLY during garbage collection. The |
| 1070 | * heap manager is not allowed to move variable-size blocks at any |
| 1071 | * other time. |
| 1072 | * |
| 1073 | * This method is virtual because a given object could have more than |
| 1074 | * one block allocated in the variable heap. By default, we'll fix up |
| 1075 | * the address of our extension if the block being moved (as |
| 1076 | * identified by its old address) matches our existing extension |
| 1077 | * pointer. |
| 1078 | */ |
| 1079 | virtual void move_var_part(void *old_pos, void *new_pos) |
| 1080 | { |
| 1081 | /* if the block being moved is our extension, note the new location */ |
| 1082 | if (ext_ == (char *)old_pos) |
| 1083 | ext_ = (char *)new_pos; |
| 1084 | } |
| 1085 | #endif |
| 1086 | |
| 1087 | /* |
| 1088 | * Pointer to extension. This pointer may be used in any way |
| 1089 | * desired by the subclassed implementation of the CVmObject |
| 1090 | * interface; normally, this pointer will contain the address of the |
| 1091 | * data associated with the object. |
| 1092 | */ |
| 1093 | char *ext_; |
| 1094 | |
| 1095 | /* property evaluator - undefined function */ |
| 1096 | int getp_undef(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc, |
| 1097 | vm_prop_id_t prop, vm_obj_id_t *source_obj); |
| 1098 | |
| 1099 | /* property evaluator - ofKind */ |
| 1100 | int getp_of_kind(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc, |
| 1101 | vm_prop_id_t prop, vm_obj_id_t *source_obj); |
| 1102 | |
| 1103 | /* property evaluator - getSuperclassList */ |
| 1104 | int getp_sclist(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc, |
| 1105 | vm_prop_id_t prop, vm_obj_id_t *source_obj); |
| 1106 | |
| 1107 | /* property evaluator - propDefined */ |
| 1108 | int getp_propdef(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc, |
| 1109 | vm_prop_id_t prop, vm_obj_id_t *source_obj); |
| 1110 | |
| 1111 | /* property evaluator - propType */ |
| 1112 | int getp_proptype(VMG_ vm_obj_id_t self, vm_val_t *retval, uint *argc, |
| 1113 | vm_prop_id_t prop, vm_obj_id_t *source_obj); |
| 1114 | |
| 1115 | /* property evaluator - getPropList */ |
| 1116 | int getp_get_prop_list(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 1117 | uint *argc, vm_prop_id_t prop, |
| 1118 | vm_obj_id_t *source_obj); |
| 1119 | |
| 1120 | /* property evaluator - getPropParams */ |
| 1121 | int getp_get_prop_params(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 1122 | uint *argc, vm_prop_id_t prop, |
| 1123 | vm_obj_id_t *source_obj); |
| 1124 | |
| 1125 | /* property evaluator - isClass */ |
| 1126 | int getp_is_class(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 1127 | uint *argc, vm_prop_id_t prop, |
| 1128 | vm_obj_id_t *source_obj); |
| 1129 | |
| 1130 | /* property evaluator - propInherited */ |
| 1131 | int getp_propinh(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 1132 | uint *argc, vm_prop_id_t prop, |
| 1133 | vm_obj_id_t *source_obj); |
| 1134 | |
| 1135 | /* property evaluator - isTransient */ |
| 1136 | int getp_is_transient(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 1137 | uint *argc, vm_prop_id_t prop, |
| 1138 | vm_obj_id_t *source_obj); |
| 1139 | |
| 1140 | /* find the intrinsic class for the given modifier object */ |
| 1141 | virtual vm_obj_id_t find_intcls_for_mod(VMG_ vm_obj_id_t self, |
| 1142 | vm_obj_id_t mod_obj); |
| 1143 | |
| 1144 | /* property evaluation function table */ |
| 1145 | static int (CVmObject::*func_table_[])(VMG_ vm_obj_id_t self, |
| 1146 | vm_val_t *retval, uint *argc, |
| 1147 | vm_prop_id_t prop, |
| 1148 | vm_obj_id_t *source_obj); |
| 1149 | }; |
| 1150 | |
| 1151 | /* |
| 1152 | * Function table indices for 'Object' intrinsic class methods. Each of |
| 1153 | * these gives the index in our function table for the property ID (as |
| 1154 | * defined in the image file) corresponding to that method. |
| 1155 | */ |
| 1156 | const int VMOBJ_IDX_OF_KIND = 1; |
| 1157 | const int VMOBJ_IDX_SCLIST = 2; |
| 1158 | const int VMOBJ_IDX_PROPDEF = 3; |
| 1159 | const int VMOBJ_IDX_PROPTYPE = 4; |
| 1160 | const int VMOBJ_IDX_GET_PROP_LIST = 5; |
| 1161 | const int VMOBJ_IDX_GET_PROP_PARAMS = 6; |
| 1162 | const int VMOBJ_IDX_IS_CLASS = 7; |
| 1163 | const int VMOBJ_IDX_PROPINH = 8; |
| 1164 | const int VMOBJ_IDX_IS_TRANSIENT = 9; |
| 1165 | |
| 1166 | /* ------------------------------------------------------------------------ */ |
| 1167 | /* |
| 1168 | * Each CVmObject subclass must define a singleton instance of |
| 1169 | * CVmMetaclass that describes the class and instantiates objects of the |
| 1170 | * class. |
| 1171 | */ |
| 1172 | |
| 1173 | /* |
| 1174 | * The "registration index" is set at startup, when we register the |
| 1175 | * metaclasses. This is static to the class - it isn't a per-instance |
| 1176 | * value. The purpose of setting this value is to allow |
| 1177 | * get_registration_index() in the instance to get the class value. |
| 1178 | * This information is used when saving our state to save information on |
| 1179 | * an instance's metaclass, so that the instance can be re-created when |
| 1180 | * the saved state is restored. |
| 1181 | * |
| 1182 | * The registration index is NOT persistent data. The registration |
| 1183 | * index of a particular metaclass can vary from execution to execution |
| 1184 | * (although it will remain fixed throughout the lifetime of one set of |
| 1185 | * VM globals, hence throughout an image file's execution). The |
| 1186 | * registration index allows us to find the registration table entry, |
| 1187 | * which in turn will give us the metaclass global ID, which is suitable |
| 1188 | * for persistent storage. |
| 1189 | */ |
| 1190 | |
| 1191 | class CVmMetaclass |
| 1192 | { |
| 1193 | public: |
| 1194 | virtual ~CVmMetaclass() { } |
| 1195 | |
| 1196 | /* |
| 1197 | * Get the metaclass registration table index. This gives the index |
| 1198 | * of the metaclass in the system registration table. This value is |
| 1199 | * fixed during execution; it is set during startup, when the |
| 1200 | * registration table is being built, and never changes after that. |
| 1201 | */ |
| 1202 | uint get_reg_idx() const { return meta_reg_idx_; } |
| 1203 | |
| 1204 | /* |
| 1205 | * Get the metaclass name. This is the universally unique |
| 1206 | * identifier assigned to the metaclass, and is the name used to |
| 1207 | * dynamically link the image file to the metaclass. |
| 1208 | */ |
| 1209 | virtual const char *get_meta_name() const = 0; |
| 1210 | |
| 1211 | /* |
| 1212 | * Create an instance of the metaclass using arguments from the VM |
| 1213 | * stack. Returns the ID of the newly-created object. If the class |
| 1214 | * has a byte-code constructor, invoke the constructor using the |
| 1215 | * normal call-procedure protocol. Leave the result in register R0. |
| 1216 | */ |
| 1217 | virtual vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, |
| 1218 | uint argc) = 0; |
| 1219 | |
| 1220 | /* |
| 1221 | * Create an instance of the metaclass with the given ID. The |
| 1222 | * object table entry will already be allocated by the caller; this |
| 1223 | * ro utine only needs to invoke the metaclass-specific constructor |
| 1224 | * to initialize the object. The object must be initialized in such |
| 1225 | * a way that it can subsequently be loaded from the image file with |
| 1226 | * load_from_image(). In general, this routine only needs to do |
| 1227 | * something like this: |
| 1228 | * |
| 1229 | * new (vmg_ id) CVmObjXxx(); |
| 1230 | */ |
| 1231 | virtual void create_for_image_load(VMG_ vm_obj_id_t id) = 0; |
| 1232 | |
| 1233 | /* |
| 1234 | * Create an instance of the metaclass with the given ID in |
| 1235 | * preparation for restoring the object from a saved state file. |
| 1236 | */ |
| 1237 | virtual void create_for_restore(VMG_ vm_obj_id_t id) = 0; |
| 1238 | |
| 1239 | /* |
| 1240 | * Call a static property of the metaclass |
| 1241 | */ |
| 1242 | virtual int call_stat_prop(VMG_ vm_val_t *result, |
| 1243 | const uchar **pc_ptr, uint *argc, |
| 1244 | vm_prop_id_t prop) = 0; |
| 1245 | |
| 1246 | /* |
| 1247 | * Set a static property (for setprop ops on the class itself). |
| 1248 | * Returns true if the setprop succeeded, false if not. This shouldn't |
| 1249 | * throw an error if the property isn't settable on the class; simply |
| 1250 | * return false instead. (It's okay to throw errors for other reasons, |
| 1251 | * such as an invalid type for a property that can be set. But if it's |
| 1252 | * not a supported property at all, we want to continue on to check for |
| 1253 | * setting a modifier property rather than throwing an error at this |
| 1254 | * phase.) |
| 1255 | * |
| 1256 | * 'class_state' is the class state holder. This is a direct pointer |
| 1257 | * to the state data in the CVmObjClass object. The callee can modify |
| 1258 | * this directly, in which case the updated value will be stored (and |
| 1259 | * will be handled properly for save/restore), but doing so won't save |
| 1260 | * undo. To save undo, use CVmObjClass::set_class_state_undo(). The |
| 1261 | * typical scenario for the state value is that it stores a reference |
| 1262 | * to a container object, such as a Vector or TadsObject, which is then |
| 1263 | * used to store the actual class state - we typically need a separate |
| 1264 | * container because we have more state than we could stuff into a |
| 1265 | * single vm_val_t. When using a separate container, that container |
| 1266 | * will usually handle the undo, in which case there's no need to worry |
| 1267 | * about undo at the class_state level. |
| 1268 | */ |
| 1269 | virtual int set_stat_prop(VMG_ class CVmUndo *, vm_obj_id_t /*self*/, |
| 1270 | vm_val_t * /* class_state */, |
| 1271 | vm_prop_id_t /*prop*/, const vm_val_t * /*val*/) |
| 1272 | { |
| 1273 | /* by default, class objects have no settable properties */ |
| 1274 | return FALSE; |
| 1275 | } |
| 1276 | |
| 1277 | /* |
| 1278 | * Get the number of superclasses of the metaclass, and get the |
| 1279 | * super-metaclass at the given index. All metaclasses have exactly |
| 1280 | * one super-metaclass, except for the root object metaclass, which |
| 1281 | * has no super-metaclass. |
| 1282 | */ |
| 1283 | virtual int get_supermeta_count(VMG0_) const { return 1; } |
| 1284 | virtual vm_obj_id_t get_supermeta(VMG_ int idx) const; |
| 1285 | |
| 1286 | /* determine if I'm an instance of the given object */ |
| 1287 | virtual int is_meta_instance_of(VMG_ vm_obj_id_t obj) const; |
| 1288 | |
| 1289 | /* |
| 1290 | * set the metaclass registration table index - this can only be |
| 1291 | * done by vm_register_metaclass() during initialization |
| 1292 | */ |
| 1293 | void set_metaclass_reg_index(uint idx) { meta_reg_idx_ = idx; } |
| 1294 | |
| 1295 | /* most metaclasses are simply derived from Object */ |
| 1296 | virtual CVmMetaclass *get_supermeta_reg() const |
| 1297 | { return CVmObject::metaclass_reg_; } |
| 1298 | |
| 1299 | /* |
| 1300 | * get my class object - we'll look up our class object in the |
| 1301 | * metaclass registration table |
| 1302 | */ |
| 1303 | vm_obj_id_t get_class_obj(VMG0_) const; |
| 1304 | |
| 1305 | private: |
| 1306 | /* system metaclass registration table index */ |
| 1307 | uint meta_reg_idx_; |
| 1308 | }; |
| 1309 | |
| 1310 | |
| 1311 | /* ------------------------------------------------------------------------ */ |
| 1312 | /* |
| 1313 | * Root Object definition. The root object can never be instantiated; |
| 1314 | * it is defined purely to provide an object to represent as the root in |
| 1315 | * the type system. |
| 1316 | */ |
| 1317 | class CVmMetaclassRoot: public CVmMetaclass |
| 1318 | { |
| 1319 | public: |
| 1320 | /* get the metaclass name */ |
| 1321 | const char *get_meta_name() const { return "root-object/030004"; } |
| 1322 | |
| 1323 | /* create an instance - this class cannot be instantiated */ |
| 1324 | vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc) |
| 1325 | { |
| 1326 | err_throw(VMERR_BAD_DYNAMIC_NEW); |
| 1327 | AFTER_ERR_THROW(return VM_INVALID_OBJ;) |
| 1328 | } |
| 1329 | |
| 1330 | /* create an object - this class cannot be instantiated */ |
| 1331 | void create_for_image_load(VMG_ vm_obj_id_t id) |
| 1332 | { err_throw(VMERR_BAD_STATIC_NEW); } |
| 1333 | |
| 1334 | /* create an instance for loading from a saved state */ |
| 1335 | void create_for_restore(VMG_ vm_obj_id_t id) |
| 1336 | { err_throw(VMERR_BAD_STATIC_NEW); } |
| 1337 | |
| 1338 | /* |
| 1339 | * call a static property (for getprop on the class itself, e.g. |
| 1340 | * TadsObject.createInstanceOf()) |
| 1341 | */ |
| 1342 | int call_stat_prop(VMG_ vm_val_t *result, |
| 1343 | const uchar **pc_ptr, uint *argc, |
| 1344 | vm_prop_id_t prop) |
| 1345 | { |
| 1346 | /* call the base object implementation */ |
| 1347 | return CVmObject::call_stat_prop(vmg_ result, pc_ptr, argc, prop); |
| 1348 | } |
| 1349 | |
| 1350 | /* the root object has no supermetaclasses */ |
| 1351 | int get_supermeta_count(VMG0_) const { return 0; } |
| 1352 | vm_obj_id_t get_supermeta(VMG_ int) const { return VM_INVALID_OBJ; } |
| 1353 | |
| 1354 | /* |
| 1355 | * determine if I'm an instance of the given object - the root |
| 1356 | * object is not a subclass of anything |
| 1357 | */ |
| 1358 | virtual int is_meta_instance_of(VMG_ vm_obj_id_t obj) const |
| 1359 | { return FALSE; } |
| 1360 | |
| 1361 | /* the base Object metaclass has no supermetaclass */ |
| 1362 | virtual CVmMetaclass *get_supermeta_reg() const { return 0; } |
| 1363 | }; |
| 1364 | |
| 1365 | /* ------------------------------------------------------------------------ */ |
| 1366 | /* |
| 1367 | * Object fixup table entry |
| 1368 | */ |
| 1369 | struct obj_fixup_entry |
| 1370 | { |
| 1371 | /* old ID */ |
| 1372 | vm_obj_id_t old_id; |
| 1373 | |
| 1374 | /* new ID */ |
| 1375 | vm_obj_id_t new_id; |
| 1376 | }; |
| 1377 | |
| 1378 | /* |
| 1379 | * Object ID Fixup Table. This is used during state restoration to map |
| 1380 | * from the saved state file's object numbering system to the new |
| 1381 | * in-memory numbering system. |
| 1382 | * |
| 1383 | * The objects must be added to the table IN ASCENDING ORDER OF OLD ID. |
| 1384 | * We assume this sorting order to perform a binary lookup when asked to |
| 1385 | * map an ID. |
| 1386 | */ |
| 1387 | |
| 1388 | /* fixup table subarray size */ |
| 1389 | #define VMOBJFIXUP_SUB_SIZE 2048 |
| 1390 | |
| 1391 | class CVmObjFixup |
| 1392 | { |
| 1393 | public: |
| 1394 | CVmObjFixup(ulong entry_cnt); |
| 1395 | ~CVmObjFixup(); |
| 1396 | |
| 1397 | /* add a fixup to the table */ |
| 1398 | void add_fixup(vm_obj_id_t old_id, vm_obj_id_t new_id); |
| 1399 | |
| 1400 | /* |
| 1401 | * Translate from the file numbering system to the new numbering |
| 1402 | * system. If the object isn't found, it must be a static object and |
| 1403 | * hence doesn't require translation, so we'll return the original ID |
| 1404 | * unchanged. |
| 1405 | */ |
| 1406 | vm_obj_id_t get_new_id(VMG_ vm_obj_id_t old_id); |
| 1407 | |
| 1408 | /* fix up a DATAHOLDER value */ |
| 1409 | void fix_dh(VMG_ char *dh); |
| 1410 | |
| 1411 | /* fix up an array of DATAHOLDER values */ |
| 1412 | void fix_dh_array(VMG_ char *arr, size_t cnt); |
| 1413 | |
| 1414 | /* fix a portable VMB_OBJECT_ID field */ |
| 1415 | void fix_vmb_obj(VMG_ char *p); |
| 1416 | |
| 1417 | /* fix an array of portable VMB_OBJECT_ID fields */ |
| 1418 | void fix_vmb_obj_array(VMG_ char *p, size_t cnt); |
| 1419 | |
| 1420 | private: |
| 1421 | /* find an entry given the old object ID */ |
| 1422 | struct obj_fixup_entry *find_entry(vm_obj_id_t old_entry); |
| 1423 | |
| 1424 | /* get an entry at the given array index */ |
| 1425 | struct obj_fixup_entry *get_entry(ulong idx) const |
| 1426 | { |
| 1427 | return &arr_[idx / VMOBJFIXUP_SUB_SIZE][idx % VMOBJFIXUP_SUB_SIZE]; |
| 1428 | } |
| 1429 | |
| 1430 | /* array of subarrays */ |
| 1431 | struct obj_fixup_entry **arr_; |
| 1432 | |
| 1433 | /* number of subarray pages */ |
| 1434 | ulong pages_; |
| 1435 | |
| 1436 | /* number of entries in the array */ |
| 1437 | ulong cnt_; |
| 1438 | |
| 1439 | /* number of entries used so far */ |
| 1440 | ulong used_; |
| 1441 | }; |
| 1442 | |
| 1443 | |
| 1444 | /* ------------------------------------------------------------------------ */ |
| 1445 | /* |
| 1446 | * Global variable structure. We maintain a linked list of these |
| 1447 | * structures for miscellaneous global variables required by other |
| 1448 | * subsystems. |
| 1449 | */ |
| 1450 | struct vm_globalvar_t |
| 1451 | { |
| 1452 | /* the variable's value */ |
| 1453 | vm_val_t val; |
| 1454 | |
| 1455 | /* next and previous pointer in linked list of global variables */ |
| 1456 | vm_globalvar_t *nxt; |
| 1457 | vm_globalvar_t *prv; |
| 1458 | }; |
| 1459 | |
| 1460 | |
| 1461 | /* ------------------------------------------------------------------------ */ |
| 1462 | /* |
| 1463 | * Global object page. We keep a linked list of pages of globals that |
| 1464 | * refer to objects that are always reachable but were not loaded from the |
| 1465 | * image file. |
| 1466 | */ |
| 1467 | class CVmObjGlobPage |
| 1468 | { |
| 1469 | public: |
| 1470 | CVmObjGlobPage() |
| 1471 | { |
| 1472 | /* we're not in a list yet */ |
| 1473 | nxt_ = 0; |
| 1474 | |
| 1475 | /* we have no allocated entries yet */ |
| 1476 | used_ = 0; |
| 1477 | } |
| 1478 | |
| 1479 | ~CVmObjGlobPage() |
| 1480 | { |
| 1481 | /* delete the next page */ |
| 1482 | delete nxt_; |
| 1483 | } |
| 1484 | |
| 1485 | /* |
| 1486 | * add an entry to this page; returns true on success, false if we're |
| 1487 | * too full to add another entry |
| 1488 | */ |
| 1489 | int add_entry(vm_obj_id_t obj) |
| 1490 | { |
| 1491 | /* if we're full, indicate failure */ |
| 1492 | if (used_ == sizeof(objs_)/sizeof(objs_[0])) |
| 1493 | return FALSE; |
| 1494 | |
| 1495 | /* store the entry and count it */ |
| 1496 | objs_[used_] = obj; |
| 1497 | ++used_; |
| 1498 | |
| 1499 | /* indicate success */ |
| 1500 | return TRUE; |
| 1501 | } |
| 1502 | |
| 1503 | /* next page in list */ |
| 1504 | CVmObjGlobPage *nxt_; |
| 1505 | |
| 1506 | /* number of entries on this page that are in use */ |
| 1507 | size_t used_; |
| 1508 | |
| 1509 | /* array of entries on this page */ |
| 1510 | vm_obj_id_t objs_[30]; |
| 1511 | }; |
| 1512 | |
| 1513 | /* ------------------------------------------------------------------------ */ |
| 1514 | /* |
| 1515 | * Object Header Manager |
| 1516 | */ |
| 1517 | |
| 1518 | /* |
| 1519 | * Number of objects in a page. We constrain this to be a power of two |
| 1520 | * to make certain calculations fast (in particular, so that division by |
| 1521 | * and modulo the page count can be done as bit shifts and masks). |
| 1522 | */ |
| 1523 | const unsigned int VM_OBJ_PAGE_CNT_LOG2 = 12; |
| 1524 | const unsigned int VM_OBJ_PAGE_CNT = (1 << VM_OBJ_PAGE_CNT_LOG2); |
| 1525 | |
| 1526 | /* |
| 1527 | * Reachability states. A Reachable object is in the root set, or can |
| 1528 | * be reached directly or indirectly from the root set. A |
| 1529 | * Finalizer-Reachable object can be reached directly or indirectly from |
| 1530 | * a finalizable object that is Finalizer-Reachable or Unreachable, but |
| 1531 | * not from any reachable object. An Unreachable object cannot be |
| 1532 | * reached from any Reachable or Finalizable object. |
| 1533 | * |
| 1534 | * We deliberately arrange the objects in a hierarchical order: |
| 1535 | * Finalizer-Reachable is "more reachable" than Unreachable, and |
| 1536 | * Reachable is more reachable than Finalizer-Reachable. The numeric |
| 1537 | * values of these states are arranged so that a higher number indicates |
| 1538 | * stronger reachability. |
| 1539 | */ |
| 1540 | #define VMOBJ_UNREACHABLE 0x00 |
| 1541 | #define VMOBJ_F_REACHABLE 0x01 |
| 1542 | #define VMOBJ_REACHABLE 0x02 |
| 1543 | |
| 1544 | /* |
| 1545 | * Finalization states. An Unfinalizable object is one which has not |
| 1546 | * ever been detected by the garbage collector to be less than fully |
| 1547 | * Reachable. A Finalizable object is one which has been found during a |
| 1548 | * garbage collection pass to be either F-Reachable or Unreachable, but |
| 1549 | * which has not yet been finalized. A Finalized object is one which |
| 1550 | * has had its finalizer method invoked. |
| 1551 | */ |
| 1552 | #define VMOBJ_UNFINALIZABLE 0x00 |
| 1553 | #define VMOBJ_FINALIZABLE 0x01 |
| 1554 | #define VMOBJ_FINALIZED 0x02 |
| 1555 | |
| 1556 | |
| 1557 | /* |
| 1558 | * Object table page entry. |
| 1559 | */ |
| 1560 | struct CVmObjPageEntry |
| 1561 | { |
| 1562 | /* |
| 1563 | * An entry is either a member of the free list, or it's a valid |
| 1564 | * object. |
| 1565 | */ |
| 1566 | union |
| 1567 | { |
| 1568 | /* |
| 1569 | * If it's not in the free list, then it's a VM object. We |
| 1570 | * can't actually embed a true CVmObject here - that's an |
| 1571 | * abstract type and we therefore can't directly instantiate it |
| 1572 | * through embedding. So, just allocate enough space for the |
| 1573 | * object; the memory manager will claim this space (via |
| 1574 | * operator new) and store the actual CVmObject here when the |
| 1575 | * slot is allocated to an object. |
| 1576 | */ |
| 1577 | char obj_[sizeof(CVmObject)]; |
| 1578 | |
| 1579 | /* |
| 1580 | * if it's in the free list, we just have a pointer to the |
| 1581 | * previous element of the free list |
| 1582 | */ |
| 1583 | vm_obj_id_t prev_free_; |
| 1584 | } ptr_; |
| 1585 | |
| 1586 | /* next object in list (either the GC work queue or the free list) */ |
| 1587 | vm_obj_id_t next_obj_; |
| 1588 | |
| 1589 | /* get my VM object pointer */ |
| 1590 | CVmObject *get_vm_obj() const { return (CVmObject *)ptr_.obj_; } |
| 1591 | |
| 1592 | /* flag: the object is in the free list */ |
| 1593 | unsigned int free_ : 1; |
| 1594 | |
| 1595 | /* |
| 1596 | * flag: the object is part of the root set (that is, there's a |
| 1597 | * reference to this object from some static location outside of the |
| 1598 | * root set, such as in p-code or in a constant list) |
| 1599 | */ |
| 1600 | unsigned int in_root_set_ : 1; |
| 1601 | |
| 1602 | /* |
| 1603 | * Reachability state. This indicates whether the object is |
| 1604 | * reachable, reachable from finalizable objects only, or |
| 1605 | * unreachable. This is set during garbage collection. |
| 1606 | */ |
| 1607 | uint reachable_ : 2; |
| 1608 | |
| 1609 | /* |
| 1610 | * Finalization state. This indicates whether an object is |
| 1611 | * unfinalizable, finalizable, or finalized. |
| 1612 | */ |
| 1613 | uint finalize_state_ : 2; |
| 1614 | |
| 1615 | /* |
| 1616 | * Flag: the object is part of an undo savepoint. This is cleared |
| 1617 | * when an object is initially created, and set for all existing |
| 1618 | * objects when an undo savepoint is created - this means that this |
| 1619 | * will be set for an object only if an undo savepoint has been |
| 1620 | * created since the object was created. |
| 1621 | * |
| 1622 | * When the undo mechanism is asked to create an undo record |
| 1623 | * associated with an object, it will do nothing if the object is not |
| 1624 | * part of the undo savepoint. This means that we won't save undo |
| 1625 | * records for objects created since the start of the most recent |
| 1626 | * savepoint - keeping undo for such objects is unnecessary, since if |
| 1627 | * we roll back to the savepoint, the object won't even be in |
| 1628 | * existence any more and hence has no need to restore any of its |
| 1629 | * state. |
| 1630 | */ |
| 1631 | uint in_undo_ : 1; |
| 1632 | |
| 1633 | /* |
| 1634 | * Flag: the object is "transient." A transient object does not |
| 1635 | * participate in undo, is not saved or restored to a saved state, and |
| 1636 | * is not affected by restarting. |
| 1637 | */ |
| 1638 | uint transient_ : 1; |
| 1639 | |
| 1640 | /* flag: the object has requested post-load initialization */ |
| 1641 | uint requested_post_load_init_ : 1; |
| 1642 | |
| 1643 | /* |
| 1644 | * Garbage collection hint flags. These flags provide hints on how the |
| 1645 | * object's metaclass interacts with the garbage collector. These do |
| 1646 | * NOT indicate the object's current status, but rather indicate the |
| 1647 | * metaclass's capabilities - so 'can_have_refs_' does not indicate |
| 1648 | * that the object current has or doesn't have any references to other |
| 1649 | * objects, but rather indicates if it's POSSIBLE for the object EVER |
| 1650 | * to have references to other objects. For example, all Strings would |
| 1651 | * set 'can_have_refs_' to false, and all TadsObjects would set it to |
| 1652 | * true. |
| 1653 | * |
| 1654 | * We set these flags to true by default, for the maximally |
| 1655 | * conservative settings. A metaclass can simply ignore these settings |
| 1656 | * and be assured of correct GC behavior. However, if a metaclass |
| 1657 | * knows that it can correctly set one of these flags to false, it |
| 1658 | * should do so after instances are created, because doing so allows |
| 1659 | * the garbage collector to reduce the amount of work it must do for |
| 1660 | * the object. |
| 1661 | * |
| 1662 | * 'can_have_refs_' indicates if the object can ever contain references |
| 1663 | * to other objects. By default, this is always set to true, but a |
| 1664 | * metaclass that is not capable of storing references to other objects |
| 1665 | * should set this to false. When this is set to false, the garbage |
| 1666 | * collector will avoid tracing into this object when tracing |
| 1667 | * references, because it will know in advance that tracing into the |
| 1668 | * object will have no effect. |
| 1669 | * |
| 1670 | * 'can_have_weak_refs_' indicates if the object can ever contain weak |
| 1671 | * references to other objects. By default, this is set to true, but a |
| 1672 | * metaclass that never uses weak references can set it to false. When |
| 1673 | * this is set to false, the garbage collector can avoid notifying this |
| 1674 | * object of the need to remove stale weak references. |
| 1675 | * |
| 1676 | * IMPORTANT: We assume that a metaclass that cannot have |
| 1677 | * references/weak references can ALSO never have the corresponding |
| 1678 | * thing in its undo information. It's hard to imagine a case where |
| 1679 | * we'd have no possibility of one kind of reference in an object but |
| 1680 | * then have the same kind of reference in the object's undo records. |
| 1681 | * But should such a case arise, the metaclass must indicate that it |
| 1682 | * does have the possibility - in other words, if an object can have a |
| 1683 | * given reference type in its undo, it must also say it can have that |
| 1684 | * type in its own data, by setting the appropriate flag here. |
| 1685 | */ |
| 1686 | uint can_have_refs_ : 1; |
| 1687 | uint can_have_weak_refs_ : 1; |
| 1688 | |
| 1689 | /* |
| 1690 | * An entry is deletable if it's unreachable and has been finalized. |
| 1691 | * If the entry is marked as free, it's already been deleted, hence |
| 1692 | * is certainly deletable. |
| 1693 | */ |
| 1694 | int is_deletable() const |
| 1695 | { |
| 1696 | return (free_ |
| 1697 | || (reachable_ == VMOBJ_UNREACHABLE |
| 1698 | && finalize_state_ == VMOBJ_FINALIZED)); |
| 1699 | } |
| 1700 | |
| 1701 | /* |
| 1702 | * Determine if the object should participate in undo. An object |
| 1703 | * participates in undo if it existed as of the most recent savepoint, |
| 1704 | * and the object is not transient. |
| 1705 | */ |
| 1706 | int is_in_undo() const { return in_undo_ && !transient_; } |
| 1707 | |
| 1708 | /* |
| 1709 | * A "saveable" object is one which must be written to a saved state |
| 1710 | * file. |
| 1711 | * |
| 1712 | * To be saveable, an object must not be free, must not be transient, |
| 1713 | * must be fully reachable, and must have been modified since loading |
| 1714 | * (or simply have been created dynamically, since all an object that |
| 1715 | * was created dynamically has inherently been modified since the |
| 1716 | * program was loaded, as it didn't even exist when the program was |
| 1717 | * loaded). |
| 1718 | * |
| 1719 | * Do not save objects that are only reachable through a finalizer; |
| 1720 | * assume that these objects do not figure into the persistent VM |
| 1721 | * state, but are still around merely because they have some external |
| 1722 | * resource deallocation to which they must yet tend. |
| 1723 | * |
| 1724 | * Do not save objects that are in the root set and which haven't been |
| 1725 | * modified since loading. We always reset before restoring to the |
| 1726 | * initial image file state, so there's no need to save data for any |
| 1727 | * object that's simply in its initial image file state. |
| 1728 | */ |
| 1729 | int is_saveable() const |
| 1730 | { |
| 1731 | return (!free_ |
| 1732 | && !transient_ |
| 1733 | && reachable_ == VMOBJ_REACHABLE |
| 1734 | && (!in_root_set_ |
| 1735 | || get_vm_obj()->is_changed_since_load())); |
| 1736 | } |
| 1737 | |
| 1738 | /* |
| 1739 | * Determine if the object is "persistent." A persistent object is |
| 1740 | * one which will survive saving and restoring machine state. An |
| 1741 | * object is persistent if it is not transient, and either it is |
| 1742 | * saveable (in which case it will be explicitly saved and restored), |
| 1743 | * or it is merely in the root set (in which case it is always |
| 1744 | * present). |
| 1745 | */ |
| 1746 | int is_persistent() const |
| 1747 | { |
| 1748 | return !transient_ && (in_root_set_ || is_saveable()); |
| 1749 | } |
| 1750 | }; |
| 1751 | |
| 1752 | /* ------------------------------------------------------------------------ */ |
| 1753 | /* |
| 1754 | * Object table. |
| 1755 | */ |
| 1756 | class CVmObjTable |
| 1757 | { |
| 1758 | public: |
| 1759 | /* create the table */ |
| 1760 | CVmObjTable(); |
| 1761 | |
| 1762 | /* initialize */ |
| 1763 | void init(VMG0_); |
| 1764 | |
| 1765 | /* |
| 1766 | * Clear the object table. This deletes the garbage collected objects, |
| 1767 | * but leaves the table itself intact. |
| 1768 | */ |
| 1769 | void clear_obj_table(VMG0_); |
| 1770 | |
| 1771 | /* |
| 1772 | * Destroy the table - call this rather BEFORE using operator delete |
| 1773 | * directly. After this routine is called, the object table can be |
| 1774 | * deleted. |
| 1775 | */ |
| 1776 | void delete_obj_table(VMG0_); |
| 1777 | |
| 1778 | /* clients must call delete_obj_table() before deleting the object */ |
| 1779 | ~CVmObjTable(); |
| 1780 | |
| 1781 | /* get an object given an object ID */ |
| 1782 | inline CVmObject *get_obj(vm_obj_id_t id) const |
| 1783 | { |
| 1784 | /* get the page entry, and get the object from the entry */ |
| 1785 | return (CVmObject *)&get_entry(id)->ptr_.obj_; |
| 1786 | } |
| 1787 | |
| 1788 | /* |
| 1789 | * Turn garbage collection on or off. When performing a series of |
| 1790 | * allocations of values that won't be stored on the stack, this can |
| 1791 | * be used to ensure that the intermediate allocations aren't |
| 1792 | * collected as unreferenced before the group of operations is |
| 1793 | * completed. Returns previous status for later restoration. |
| 1794 | */ |
| 1795 | int enable_gc(VMG_ int enable); |
| 1796 | |
| 1797 | /* allocate a new object ID */ |
| 1798 | vm_obj_id_t alloc_obj(VMG_ int in_root_set) |
| 1799 | { |
| 1800 | /* allocate, using maximally conservative GC characteristics */ |
| 1801 | return alloc_obj(vmg_ in_root_set, TRUE, TRUE); |
| 1802 | } |
| 1803 | |
| 1804 | /* allocate a new object ID */ |
| 1805 | vm_obj_id_t alloc_obj(VMG_ int in_root_set, int can_have_refs, |
| 1806 | int can_have_weak_refs); |
| 1807 | |
| 1808 | /* |
| 1809 | * Allocate an object at a particular object ID. This is used when |
| 1810 | * loading objects from an image file or restoring objects from a |
| 1811 | * saved state file, since objects must be loaded or restored with |
| 1812 | * the same object number which they were originally assigned. This |
| 1813 | * routine throws an error if the object is already allocated. |
| 1814 | */ |
| 1815 | void alloc_obj_with_id(vm_obj_id_t id, int in_root_set) |
| 1816 | { |
| 1817 | /* allocate with maximally conservative GC characteristics */ |
| 1818 | alloc_obj_with_id(id, in_root_set, TRUE, TRUE); |
| 1819 | } |
| 1820 | |
| 1821 | /* allocate an object with a given ID */ |
| 1822 | void alloc_obj_with_id(vm_obj_id_t id, int in_root_set, |
| 1823 | int can_have_refs, int can_have_weak_refs); |
| 1824 | |
| 1825 | /* |
| 1826 | * Collect all garbage. This runs an entire garbage collection pass |
| 1827 | * to completion with a single call. This can be used for |
| 1828 | * simplicity when the caller does not require incremental operation |
| 1829 | * of the garbage collector. |
| 1830 | * |
| 1831 | * This function actually performs two garbage collection passes to |
| 1832 | * ensure that all collectible objects are collected. We perform |
| 1833 | * one pass to detect finalizable objects, then finalize all objects |
| 1834 | * that we can, then make one more pass to sweep up all of the |
| 1835 | * finalized objects that can be deleted. |
| 1836 | */ |
| 1837 | void gc_full(VMG0_); |
| 1838 | |
| 1839 | /* |
| 1840 | * Incremental garbage collection. Call gc_pass_init() to |
| 1841 | * initialize the pass. Call gc_pass_continue() repeatedly to |
| 1842 | * perform incremental collection; this routine runs for a short |
| 1843 | * time and then returns. gc_pass_continue() returns true if |
| 1844 | * there's more work to do, false if not, so the caller can stop |
| 1845 | * invoking it as soon as it returns false. Call gc_pass_finish() |
| 1846 | * to complete the garbage collection. gc_pass_finish() will call |
| 1847 | * gc_pass_continue() if necessary to finish its work, so the caller |
| 1848 | * need not keep invoking gc_pass_continue() if it runs out of work |
| 1849 | * to interleave with the garbage collector. |
| 1850 | * |
| 1851 | * Once garbage collection is started, it must be finished before |
| 1852 | * any other VM activity occurs. So, after a call to |
| 1853 | * gc_pass_init(), the caller is not allowed to perform any other VM |
| 1854 | * operations until gc_pass_finish() returns (in particular, no new |
| 1855 | * objects may be created, and no references to existing objects may |
| 1856 | * be created or changed). |
| 1857 | */ |
| 1858 | void gc_pass_init(VMG0_); |
| 1859 | int gc_pass_continue(VMG0_) { return gc_pass_continue(vmg_ TRUE); } |
| 1860 | void gc_pass_finish(VMG0_); |
| 1861 | |
| 1862 | /* |
| 1863 | * Run pending finalizers. This can be run at any time other than |
| 1864 | * during garbage collection (i.e., between gc_pass_init() and |
| 1865 | * gc_pass_finish()). |
| 1866 | */ |
| 1867 | void run_finalizers(VMG0_); |
| 1868 | |
| 1869 | /* |
| 1870 | * Determine if a given object is subject to deletion. This only |
| 1871 | * gives meaningful results during the final garbage collector pass. |
| 1872 | * Returns true if the object is ready for deletion, which can only |
| 1873 | * happen when the object is both Unreachable and Finalized, or |
| 1874 | * false if it not. A true return means that the object can be |
| 1875 | * deleted at any time. This can be used by weak referencers to |
| 1876 | * determine if objects they are referencing are about to be |
| 1877 | * deleted, and thus that the weak reference must be forgotten. |
| 1878 | */ |
| 1879 | int is_obj_deletable(vm_obj_id_t obj) const |
| 1880 | { |
| 1881 | /* |
| 1882 | * If it's not a valid object, consider it deletable, since it |
| 1883 | * has indeed already been deleted; otherwise, it's deletable if |
| 1884 | * its object table entry is deletable. |
| 1885 | */ |
| 1886 | return (obj == VM_INVALID_OBJ |
| 1887 | || get_entry(obj)->is_deletable()); |
| 1888 | } |
| 1889 | |
| 1890 | /* |
| 1891 | * Mark object references (for GC tracing) made in an object's undo |
| 1892 | * record. If the object is marked as having no possibility of |
| 1893 | * containing references to other objects, we won't bother invoking |
| 1894 | * the object's tracing method, as we can be assured that the undo |
| 1895 | * records won't contain any references either. |
| 1896 | */ |
| 1897 | void mark_obj_undo_rec(VMG_ vm_obj_id_t obj, |
| 1898 | struct CVmUndoRecord *undo_rec) |
| 1899 | { |
| 1900 | /* get the object entry */ |
| 1901 | CVmObjPageEntry *entry = get_entry(obj); |
| 1902 | |
| 1903 | /* |
| 1904 | * if the object can have any references, mark any references the |
| 1905 | * object makes from the undo record; if the object can't have any |
| 1906 | * references, assume its undo records cannot either, in which |
| 1907 | * case there should be nothing to mark |
| 1908 | */ |
| 1909 | if (entry->can_have_refs_) |
| 1910 | entry->get_vm_obj()->mark_undo_ref(vmg_ undo_rec); |
| 1911 | } |
| 1912 | |
| 1913 | /* |
| 1914 | * Remove stale weak undo references for an object. If the object is |
| 1915 | * marked as having no possibility of weak references, we won't bother |
| 1916 | * invoking the object's weak undo reference remover method, since we |
| 1917 | * know it won't do anything. |
| 1918 | */ |
| 1919 | void remove_obj_stale_undo_weak_ref(VMG_ vm_obj_id_t obj, |
| 1920 | struct CVmUndoRecord *undo_rec) |
| 1921 | { |
| 1922 | /* get the object entry */ |
| 1923 | CVmObjPageEntry *entry = get_entry(obj); |
| 1924 | |
| 1925 | /* |
| 1926 | * if the object can have weak references, notify it; if not, |
| 1927 | * there's no need to do anything |
| 1928 | */ |
| 1929 | if (entry->can_have_weak_refs_) |
| 1930 | entry->get_vm_obj()->remove_stale_undo_weak_ref(vmg_ undo_rec); |
| 1931 | } |
| 1932 | |
| 1933 | /* |
| 1934 | * Determine if the given object is part of the latest undo savepoint. |
| 1935 | * Returns true if an undo savepoint has been created since the object |
| 1936 | * was created, false if not. |
| 1937 | */ |
| 1938 | int is_obj_in_undo(vm_obj_id_t obj) const |
| 1939 | { |
| 1940 | return (obj != VM_INVALID_OBJ |
| 1941 | && get_entry(obj)->is_in_undo()); |
| 1942 | } |
| 1943 | |
| 1944 | /* |
| 1945 | * Determine if a vm_val_t contains a reference to a deletable object. |
| 1946 | * This is a simple convenience routine. This returns true only if |
| 1947 | * the value contains a valid object reference, and the object is |
| 1948 | * currently deletable. |
| 1949 | */ |
| 1950 | int is_obj_deletable(const vm_val_t *val) const |
| 1951 | { |
| 1952 | return (val->typ == VM_OBJ |
| 1953 | && val->val.obj != VM_INVALID_OBJ |
| 1954 | && is_obj_deletable(val->val.obj)); |
| 1955 | } |
| 1956 | |
| 1957 | /* |
| 1958 | * Determine if the object is saveable. If this returns true, the |
| 1959 | * object should be saved to a saved state file. If not, the object |
| 1960 | * should not be included in the saved state file. Note that a |
| 1961 | * non-saveable object might still be a persistent object - if the |
| 1962 | * object is a root set object and hasn't been modified since |
| 1963 | * loading, it's still peristent even though it doesn't get written |
| 1964 | * to the saved state file. |
| 1965 | */ |
| 1966 | int is_obj_saveable(vm_obj_id_t obj) const |
| 1967 | { |
| 1968 | return (obj != VM_INVALID_OBJ |
| 1969 | && get_entry(obj)->is_saveable()); |
| 1970 | } |
| 1971 | |
| 1972 | /* |
| 1973 | * Determine if the object is persistent in a saved state. If this |
| 1974 | * returns true, the object will survive saving and restoring the |
| 1975 | * machine state; if not, the object will not be present after the |
| 1976 | * machine state is restored. This can be used to test if a weak |
| 1977 | * reference should be included in a saved state file. |
| 1978 | */ |
| 1979 | int is_obj_persistent(vm_obj_id_t obj) const |
| 1980 | { |
| 1981 | return (obj != VM_INVALID_OBJ |
| 1982 | && get_entry(obj)->is_persistent()); |
| 1983 | } |
| 1984 | |
| 1985 | /* determine if the given object is transient */ |
| 1986 | int is_obj_transient(vm_obj_id_t obj) const |
| 1987 | { |
| 1988 | return (obj != VM_INVALID_OBJ |
| 1989 | && get_entry(obj)->transient_); |
| 1990 | } |
| 1991 | |
| 1992 | /* mark an object as transient */ |
| 1993 | void set_obj_transient(vm_obj_id_t obj) const |
| 1994 | { |
| 1995 | /* set the 'transient' flag in the object */ |
| 1996 | get_entry(obj)->transient_ = TRUE; |
| 1997 | } |
| 1998 | |
| 1999 | /* set an object's garbage collection characteristics */ |
| 2000 | void set_obj_gc_characteristics( |
| 2001 | vm_obj_id_t obj, int can_have_refs, int can_have_weak_refs) const |
| 2002 | { |
| 2003 | CVmObjPageEntry *entry; |
| 2004 | |
| 2005 | /* get the object's entry */ |
| 2006 | entry = get_entry(obj); |
| 2007 | |
| 2008 | /* set the entry's GC flags as specified */ |
| 2009 | entry->can_have_refs_ = can_have_refs; |
| 2010 | entry->can_have_weak_refs_ = can_have_weak_refs; |
| 2011 | } |
| 2012 | |
| 2013 | /* determine if the given object is in the root set */ |
| 2014 | int is_obj_in_root_set(vm_obj_id_t obj) const |
| 2015 | { |
| 2016 | return (obj != VM_INVALID_OBJ |
| 2017 | && get_entry(obj)->in_root_set_); |
| 2018 | } |
| 2019 | |
| 2020 | /* |
| 2021 | * Mark the given object as referenced, and recursively mark all of |
| 2022 | * the objects to which it refers as referenced. |
| 2023 | */ |
| 2024 | void mark_all_refs(vm_obj_id_t obj, uint state) |
| 2025 | { add_to_gc_queue(obj, state); } |
| 2026 | |
| 2027 | /* |
| 2028 | * Receive notification from the undo manager that we're starting a |
| 2029 | * new savepoint. We'll simply notify all of the objects of this. |
| 2030 | */ |
| 2031 | void notify_new_savept(); |
| 2032 | |
| 2033 | /* |
| 2034 | * Apply an undo record |
| 2035 | */ |
| 2036 | void apply_undo(VMG_ struct CVmUndoRecord *rec); |
| 2037 | |
| 2038 | /* call a callback for each object in the table */ |
| 2039 | void for_each(VMG_ void (*func)(VMG_ vm_obj_id_t, void *), void *ctx); |
| 2040 | |
| 2041 | /* |
| 2042 | * Rebuild the image file's OBJS blocks for a particular metaclass. |
| 2043 | * We'll write all of the objects of the given metaclass to one or |
| 2044 | * more OBJS blocks in the given output file. This can be used to |
| 2045 | * dump the program state to a new image file after running |
| 2046 | * 'preinit' or a similar compile-time pre-initialization procedure. |
| 2047 | * |
| 2048 | * 'meta_dep_idx' is the index in the metaclass dependency table of |
| 2049 | * the metaclass to be written. |
| 2050 | */ |
| 2051 | void rebuild_image(VMG_ int meta_dep_idx, class CVmImageWriter *writer, |
| 2052 | class CVmConstMapper *mapper); |
| 2053 | |
| 2054 | /* |
| 2055 | * Scan all objects and add metaclass entries to the metaclass |
| 2056 | * dependency table for any metaclasses of which there are existing |
| 2057 | * instances. |
| 2058 | */ |
| 2059 | void add_metadeps_for_instances(VMG0_); |
| 2060 | |
| 2061 | /* |
| 2062 | * Scan all active objects and convert objects to constant data |
| 2063 | * where possible. Certain object metaclasses, such as strings and |
| 2064 | * lists, can be represented in a rebuilt image file as constant |
| 2065 | * data; this routine makes all of these conversions. |
| 2066 | */ |
| 2067 | void rebuild_image_convert_const_data(VMG_ |
| 2068 | class CVmConstMapper *const_mapper); |
| 2069 | |
| 2070 | /* |
| 2071 | * Get the maximum object ID that has ever been allocated. This |
| 2072 | * establishes an upper bound on the object ID's that can be found |
| 2073 | * among the active objects. |
| 2074 | */ |
| 2075 | vm_obj_id_t get_max_used_obj_id() const |
| 2076 | { return pages_used_ * VM_OBJ_PAGE_CNT; } |
| 2077 | |
| 2078 | /* determine if an object ID refers to a valid object */ |
| 2079 | int is_obj_id_valid(vm_obj_id_t obj) const |
| 2080 | { |
| 2081 | /* |
| 2082 | * the object is valid as long as it's not free, and the ID is |
| 2083 | * within the valid range |
| 2084 | */ |
| 2085 | return (obj != VM_INVALID_OBJ |
| 2086 | && obj < get_max_used_obj_id() |
| 2087 | && !get_entry(obj)->free_); |
| 2088 | } |
| 2089 | |
| 2090 | /* |
| 2091 | * Get the object state. This is intended primarily as a debugging |
| 2092 | * and testing aid for the VM itself; this value should be of no |
| 2093 | * interest to normal programs. Returns a value suitable for use |
| 2094 | * with CVmBifT3Test::get_obj_gc_state(). |
| 2095 | */ |
| 2096 | ulong get_obj_internal_state(vm_obj_id_t id) const |
| 2097 | { |
| 2098 | /* if the object ID is invalid, return 0xF000 to so indicate */ |
| 2099 | if (id >= get_max_used_obj_id()) |
| 2100 | return 0xF000; |
| 2101 | |
| 2102 | /* |
| 2103 | * return the state as a combination of these bits: |
| 2104 | * |
| 2105 | * free ? 0 : 1 |
| 2106 | *. Unreachable=0x00, F-Reachable=0x10, Reachable=0x20 |
| 2107 | *. Unfinalizable=0x000, Finalizable=0x100, Finalized=0x200 |
| 2108 | */ |
| 2109 | return ((get_entry(id)->free_ ? 0 : 1) |
| 2110 | + (((ulong)get_entry(id)->reachable_) << 4) |
| 2111 | + (((ulong)get_entry(id)->finalize_state_) << 8)); |
| 2112 | } |
| 2113 | |
| 2114 | /* |
| 2115 | * Reset to the initial image file state. Discards all objects not |
| 2116 | * in the root set, skipping finalizers, and resets all objects to |
| 2117 | * their initial image file state. |
| 2118 | */ |
| 2119 | void reset_to_image(VMG0_); |
| 2120 | |
| 2121 | /* |
| 2122 | * Save state to a file. We write out each object's state to the |
| 2123 | * file so that the state can be restored later. |
| 2124 | */ |
| 2125 | void save(VMG_ class CVmFile *fp); |
| 2126 | |
| 2127 | /* |
| 2128 | * Restore state from a previously saved file. Returns zero on |
| 2129 | * success, or a VMERR_xxx code on failure. |
| 2130 | * |
| 2131 | * This routine creates an object fixup table, and returns it in |
| 2132 | * *fixups. The caller is responsible for deleting this object if a |
| 2133 | * non-null pointer is returned in *fixups. |
| 2134 | */ |
| 2135 | int restore(VMG_ class CVmFile *fp, class CVmObjFixup **fixups); |
| 2136 | |
| 2137 | /* |
| 2138 | * Save an object's image data pointer. An object's load_from_image() |
| 2139 | * routine may call this routine (it cannot be called from anywhere |
| 2140 | * else) to save the loaded image location for the object. For each |
| 2141 | * object that calls this routine, we will call the object's |
| 2142 | * reload_from_image() method during a reset to initial image file |
| 2143 | * state. |
| 2144 | */ |
| 2145 | void save_image_pointer(vm_obj_id_t obj, const char *ptr, size_t siz); |
| 2146 | |
| 2147 | /* |
| 2148 | * Request post-load initialization. An object can call this to |
| 2149 | * request that its post_load_init() method be called after an initial |
| 2150 | * program load, restart, or restore operation. The post_load_init() |
| 2151 | * method will not be called until the load/restart/restore operation |
| 2152 | * has loaded every object, so the method can be used to perform any |
| 2153 | * initialization that depends upon other objects being loaded. |
| 2154 | */ |
| 2155 | void request_post_load_init(vm_obj_id_t obj); |
| 2156 | |
| 2157 | /* remove a post-load initialization request */ |
| 2158 | void remove_post_load_init(vm_obj_id_t obj); |
| 2159 | |
| 2160 | /* invoke all registered post-load initializations */ |
| 2161 | void do_all_post_load_init(VMG0_); |
| 2162 | |
| 2163 | /* |
| 2164 | * Ensure that the given object has had its post-load initialization |
| 2165 | * performed during the current load/restart/restore. If an object's |
| 2166 | * post-load initialization depends upon another object having already |
| 2167 | * been initialized, the first object should call this to ensure that |
| 2168 | * the other object it depends upon has been initialized. This allows |
| 2169 | * objects to ensure that initialization dependencies are handled in |
| 2170 | * the correct order, regardless of the order in which the objects were |
| 2171 | * loaded. |
| 2172 | * |
| 2173 | * Post-load initialization is guaranteed to be executed exactly once |
| 2174 | * per load/restart/restore cycle. When this routine is called, we |
| 2175 | * check to see if the target object has already been initialized |
| 2176 | * during this operation, and we do nothing if so. If we do invoke the |
| 2177 | * target object's post_load_init(), then this will be the only |
| 2178 | * invocation for this operation. |
| 2179 | * |
| 2180 | * Circular dependencies are prohibited. If object A's |
| 2181 | * post_load_init() method calls this to initialize object B, we will |
| 2182 | * invoke object B's post_load_init(). If object B in turn calls this |
| 2183 | * routine to initialize object A, we will observe that object A is |
| 2184 | * already in the process of being initialized and throw an error. |
| 2185 | */ |
| 2186 | void ensure_post_load_init(VMG_ vm_obj_id_t obj); |
| 2187 | |
| 2188 | /* |
| 2189 | * Add an object to the list of machine globals. An object added to |
| 2190 | * this list will never be deleted. If the object is in the root set |
| 2191 | * (which means it was loaded from the image file), this has no effect, |
| 2192 | * since a root object is inherently global. |
| 2193 | */ |
| 2194 | void add_to_globals(vm_obj_id_t obj); |
| 2195 | |
| 2196 | /* |
| 2197 | * Create a "global variable." A global variable is part of the root |
| 2198 | * set: any value in a variable allocated here will be traced during |
| 2199 | * garbage collection. Global variables are meant for use by other |
| 2200 | * subsystems, so that other subsystems can include their own static |
| 2201 | * and global variables in the root set. |
| 2202 | * |
| 2203 | * Global variables aren't affected by RESTORE, RESTART, or UNDO (like |
| 2204 | * the stack, global variables are transient). |
| 2205 | * |
| 2206 | * The caller can use delete_global_var() to delete the variable when |
| 2207 | * done with it. Any global variable that's never explicitly deleted |
| 2208 | * will be automatically deleted when the object table itself is |
| 2209 | * destroyed. |
| 2210 | */ |
| 2211 | vm_globalvar_t *create_global_var(); |
| 2212 | void delete_global_var(vm_globalvar_t *var); |
| 2213 | |
| 2214 | /* count an allocation */ |
| 2215 | void count_alloc(size_t siz) { bytes_since_gc_ += siz; } |
| 2216 | |
| 2217 | private: |
| 2218 | /* rebuild the image, writing only transient or only persistent objects */ |
| 2219 | void rebuild_image(VMG_ int meta_dep_idx, CVmImageWriter *writer, |
| 2220 | class CVmConstMapper *mapper, int trans); |
| 2221 | |
| 2222 | /* invoke a post-load initialization method */ |
| 2223 | static void call_post_load_init(VMG_ class CVmHashEntryPLI *entry); |
| 2224 | |
| 2225 | /* enumeration callbacks for post-load initialization */ |
| 2226 | static void pli_status_cb(void *ctx, class CVmHashEntry *entry); |
| 2227 | static void pli_invoke_cb(void *ctx, class CVmHashEntry *entry); |
| 2228 | |
| 2229 | /* get the page entry for a given ID */ |
| 2230 | inline CVmObjPageEntry *get_entry(vm_obj_id_t id) const |
| 2231 | { |
| 2232 | return &pages_[id >> VM_OBJ_PAGE_CNT_LOG2][id & (VM_OBJ_PAGE_CNT - 1)]; |
| 2233 | } |
| 2234 | |
| 2235 | /* delete an entry */ |
| 2236 | void delete_entry(VMG_ vm_obj_id_t id, CVmObjPageEntry *entry); |
| 2237 | |
| 2238 | /* allocate a new page of objects */ |
| 2239 | void alloc_new_page(); |
| 2240 | |
| 2241 | /* |
| 2242 | * initialize a newly-allocated object table entry -- removes the |
| 2243 | * entry from the free list, marks the entry as allocated, marks it |
| 2244 | * in or out of the root set as appropriate, and initializes its GC |
| 2245 | * status as appropriate |
| 2246 | */ |
| 2247 | void init_entry_for_alloc(vm_obj_id_t id, CVmObjPageEntry *entry, |
| 2248 | int in_root_set, int can_have_refs, |
| 2249 | int can_have_weak_refs); |
| 2250 | |
| 2251 | /* |
| 2252 | * Mark an object as referenced for the garbage collector and add it |
| 2253 | * to the garbage collector's work queue. If the object is already |
| 2254 | * marked as referenced, this does nothing. |
| 2255 | */ |
| 2256 | void add_to_gc_queue(vm_obj_id_t id, uint state) |
| 2257 | { |
| 2258 | /* get the object header and add it to the work queue */ |
| 2259 | add_to_gc_queue(id, get_entry(id), state); |
| 2260 | } |
| 2261 | |
| 2262 | /* add an object to the gc work queue given the object header entry */ |
| 2263 | void add_to_gc_queue(vm_obj_id_t id, CVmObjPageEntry *entry, uint state) |
| 2264 | { |
| 2265 | /* |
| 2266 | * If it's not already referenced somehow, add it to the queue. If |
| 2267 | * it's marked as referenced, it's already in the queue (or it's |
| 2268 | * already been in the queue and it's been processed). |
| 2269 | * |
| 2270 | * If the object can't have references to other objects, don't add |
| 2271 | * it to the queue - simply elevate its reachability state. We put |
| 2272 | * objects in the queue in order to trace into the objects they |
| 2273 | * reference, so an object that can't reference any other objects |
| 2274 | * doesn't need to go in the queue. |
| 2275 | */ |
| 2276 | if (entry->can_have_refs_ && entry->reachable_ == VMOBJ_UNREACHABLE) |
| 2277 | { |
| 2278 | /* add it to the work queue */ |
| 2279 | entry->next_obj_ = gc_queue_head_; |
| 2280 | gc_queue_head_ = id; |
| 2281 | |
| 2282 | /* |
| 2283 | * Since the entry is unreachable, and unreachable is the |
| 2284 | * lowest reachability state, we know that 'state' is at least |
| 2285 | * as reachable, so we can just set the new state without even |
| 2286 | * bothering with another comparison. |
| 2287 | */ |
| 2288 | entry->reachable_ = state; |
| 2289 | } |
| 2290 | else |
| 2291 | { |
| 2292 | /* |
| 2293 | * Elevate the reachability state. Never reduce an object's |
| 2294 | * reachability state: Finalizer-Reachable is higher than |
| 2295 | * Unreachable, and Reachable is higher than |
| 2296 | * Finalizer-Reachable. |
| 2297 | * |
| 2298 | * In other words, if an object is already marked Reachable, |
| 2299 | * never reduce its state to Finalizer-Reachable just because |
| 2300 | * we find that it can also be reached from a |
| 2301 | * Finalizer-Reachable object, when we already know that it |
| 2302 | * can be reached from a root-set object. |
| 2303 | */ |
| 2304 | if (state > entry->reachable_) |
| 2305 | entry->reachable_ = state; |
| 2306 | } |
| 2307 | } |
| 2308 | |
| 2309 | /* |
| 2310 | * Add an object to the finalizer work queue. An object can only be |
| 2311 | * in one queue - it can't be in both the finalizer queue and the |
| 2312 | * main gc work queue. |
| 2313 | */ |
| 2314 | void add_to_finalize_queue(vm_obj_id_t id, CVmObjPageEntry *entry) |
| 2315 | { |
| 2316 | /* mark the object as finalizer-reachable */ |
| 2317 | entry->reachable_ = VMOBJ_F_REACHABLE; |
| 2318 | |
| 2319 | /* link it into the finalize list */ |
| 2320 | entry->next_obj_ = finalize_queue_head_; |
| 2321 | finalize_queue_head_ = id; |
| 2322 | } |
| 2323 | |
| 2324 | /* |
| 2325 | * Count an allocation and check to see if we should run garbage |
| 2326 | * collection. We run gc after a certain number of consecutive |
| 2327 | * allocations to ensure that allocation-intensive operations don't |
| 2328 | * fill up memory if we can avoid it by removing unreferenced |
| 2329 | * objects. |
| 2330 | */ |
| 2331 | void alloc_check_gc(VMG_ int do_count) |
| 2332 | { |
| 2333 | /* count the allocation if desired */ |
| 2334 | if (do_count) |
| 2335 | ++allocs_since_gc_; |
| 2336 | |
| 2337 | /* if we've passed our threshhold for collecting garbage, do so now */ |
| 2338 | if (gc_enabled_ |
| 2339 | && (allocs_since_gc_ > max_allocs_between_gc_ |
| 2340 | || bytes_since_gc_ > max_bytes_between_gc_)) |
| 2341 | gc_before_alloc(vmg0_); |
| 2342 | } |
| 2343 | |
| 2344 | /* |
| 2345 | * Run a garbage collection in preparation to allocate memory. Runs |
| 2346 | * one garbage collection pass then one finalizer pass. |
| 2347 | */ |
| 2348 | void gc_before_alloc(VMG0_); |
| 2349 | |
| 2350 | /* garbage collection: trace objects reachable from the stack */ |
| 2351 | void gc_trace_stack(VMG0_); |
| 2352 | |
| 2353 | /* garbage collection: trace objects reachable from the imports */ |
| 2354 | void gc_trace_imports(VMG0_); |
| 2355 | |
| 2356 | /* garbage collection: trace objects reachable from machine globals */ |
| 2357 | void gc_trace_globals(VMG0_); |
| 2358 | |
| 2359 | /* garbage collection: trace all objects reachable from the work queue */ |
| 2360 | void gc_trace_work_queue(VMG_ int trace_transient); |
| 2361 | |
| 2362 | /* continue a GC pass */ |
| 2363 | int gc_pass_continue(VMG_ int trace_transient); |
| 2364 | |
| 2365 | /* |
| 2366 | * set the initial GC conditions for an object -- this puts the |
| 2367 | * object into the appropriate queue and sets the appropriate |
| 2368 | * reachability state in preparation for the start of the next GC |
| 2369 | * pass |
| 2370 | */ |
| 2371 | void gc_set_init_conditions(vm_obj_id_t id, |
| 2372 | struct CVmObjPageEntry *entry) |
| 2373 | { |
| 2374 | /* |
| 2375 | * Mark the object as unreachable -- at the start of each GC pass, |
| 2376 | * all non-root-set objects must be marked unreachable. (It |
| 2377 | * doesn't matter how we mark root set objects, so we simply mark |
| 2378 | * everything as reachable to avoid an unnecessary test.) |
| 2379 | */ |
| 2380 | entry->reachable_ = VMOBJ_UNREACHABLE; |
| 2381 | |
| 2382 | /* |
| 2383 | * If it's in the root set, add it to the GC work queue -- all |
| 2384 | * root-set objects must be in the work queue and marked as |
| 2385 | * reachable at the start of each GC pass. |
| 2386 | */ |
| 2387 | if (entry->in_root_set_) |
| 2388 | add_to_gc_queue(id, entry, VMOBJ_REACHABLE); |
| 2389 | } |
| 2390 | |
| 2391 | /* hash table of objects requested post_load_init() service */ |
| 2392 | class CVmHashTable *post_load_init_table_; |
| 2393 | |
| 2394 | /* |
| 2395 | * Head of global object page list. The global objects are objects |
| 2396 | * that are always reachable but which weren't necessarily loaded from |
| 2397 | * the image file; these objects must be treated as dynamically |
| 2398 | * created for purposes such as saving, but must never be deleted as |
| 2399 | * long as they are globally reachable. |
| 2400 | */ |
| 2401 | CVmObjGlobPage *globals_; |
| 2402 | |
| 2403 | /* head of global variable list */ |
| 2404 | struct vm_globalvar_t *global_var_head_; |
| 2405 | |
| 2406 | /* |
| 2407 | * Master page table. This is an array of pointers to pages. Each |
| 2408 | * page contains a fixed number of slots for fixed-size parts. |
| 2409 | */ |
| 2410 | CVmObjPageEntry **pages_; |
| 2411 | |
| 2412 | /* number of page slots allocated, and the number actually used */ |
| 2413 | size_t page_slots_; |
| 2414 | size_t pages_used_; |
| 2415 | |
| 2416 | /* first free object */ |
| 2417 | vm_obj_id_t first_free_; |
| 2418 | |
| 2419 | /* first page of saved image data pointers */ |
| 2420 | struct vm_image_ptr_page *image_ptr_head_; |
| 2421 | |
| 2422 | /* last page of saved image data pointers */ |
| 2423 | struct vm_image_ptr_page *image_ptr_tail_; |
| 2424 | |
| 2425 | /* number of image data pointers stored on last image pointer page */ |
| 2426 | size_t image_ptr_last_cnt_; |
| 2427 | |
| 2428 | /* head of garbage collection work queue */ |
| 2429 | vm_obj_id_t gc_queue_head_; |
| 2430 | |
| 2431 | /* head of finalizer queue */ |
| 2432 | vm_obj_id_t finalize_queue_head_; |
| 2433 | |
| 2434 | /* |
| 2435 | * Allocations since last garbage collection. We increment this on |
| 2436 | * each allocation, and reset it to zero each time we collect |
| 2437 | * garbage. When we've performed too many allocations since the |
| 2438 | * last garbage collection, we force a gc pass. |
| 2439 | */ |
| 2440 | uint allocs_since_gc_; |
| 2441 | |
| 2442 | /* |
| 2443 | * Bytes allocated since the last garbage collection pass. We add each |
| 2444 | * heap allocation request to this, and zero it each time we collect |
| 2445 | * garbage. This lets us trigger an early collection pass (before we'd |
| 2446 | * collect based on object count) if a lot of large objects are being |
| 2447 | * thrown around, to ensure that our OS-level working set doesn't grow |
| 2448 | * too fast due to rapid creation of large unreachable objects. |
| 2449 | */ |
| 2450 | ulong bytes_since_gc_; |
| 2451 | |
| 2452 | /* |
| 2453 | * Maximum number of objects and heap bytes allocated before we run the |
| 2454 | * garbage collector. |
| 2455 | */ |
| 2456 | uint max_allocs_between_gc_; |
| 2457 | ulong max_bytes_between_gc_; |
| 2458 | |
| 2459 | /* garbage collection enabled */ |
| 2460 | uint gc_enabled_ : 1; |
| 2461 | }; |
| 2462 | |
| 2463 | /* ------------------------------------------------------------------------ */ |
| 2464 | /* |
| 2465 | * An image data pointer. The object table uses this structure to save |
| 2466 | * the image data location for a given object when the object requests |
| 2467 | * that this information be saved. |
| 2468 | */ |
| 2469 | struct vm_image_ptr |
| 2470 | { |
| 2471 | /* object ID */ |
| 2472 | vm_obj_id_t obj_id_; |
| 2473 | |
| 2474 | /* pointer to image data and length of the data */ |
| 2475 | const char *image_data_ptr_; |
| 2476 | size_t image_data_len_; |
| 2477 | }; |
| 2478 | |
| 2479 | /* |
| 2480 | * Maximum number of image pointers stored per image pointer page |
| 2481 | */ |
| 2482 | const size_t VM_IMAGE_PTRS_PER_PAGE = 400; |
| 2483 | |
| 2484 | /* |
| 2485 | * A page of image pointers. |
| 2486 | */ |
| 2487 | struct vm_image_ptr_page |
| 2488 | { |
| 2489 | /* next page in the list of pages */ |
| 2490 | vm_image_ptr_page *next_; |
| 2491 | |
| 2492 | /* array of image pointers */ |
| 2493 | vm_image_ptr ptrs_[VM_IMAGE_PTRS_PER_PAGE]; |
| 2494 | }; |
| 2495 | |
| 2496 | |
| 2497 | /* ------------------------------------------------------------------------ */ |
| 2498 | /* |
| 2499 | * The variable-size parts are stored in a heap separately from the object |
| 2500 | * headers. Because the variable-size objects can expand or contract |
| 2501 | * dynamically, objects in the heap can move to new addresses. |
| 2502 | * |
| 2503 | * A particular block of memory in the heap is referenced by zero or one |
| 2504 | * object at any given time; a heap block is never shared among multiple |
| 2505 | * objects. Furthermore, the only thing that can directly reference a |
| 2506 | * heap block is an object's fixed portion (through its extension |
| 2507 | * pointer). Hence, we manage the variable heap directly through the |
| 2508 | * object headers: when an object becomes unreferenced, we explicitly free |
| 2509 | * the associated variable part. |
| 2510 | * |
| 2511 | * It is possible for a single object to allocate more than one heap |
| 2512 | * block. In practice, most objects will allocate only one heap block (if |
| 2513 | * they allocate a heap block at all), but there is no reason an object |
| 2514 | * can't allocate more than one block. |
| 2515 | * |
| 2516 | * Note that polymorphism in the variable parts is handled via the fixed |
| 2517 | * part. Because the fixed part is responsible for interactions with the |
| 2518 | * variable part, each fixed part implementation will be mated to a |
| 2519 | * particular variable part implementation. These implementations may |
| 2520 | * themselves be polymorphic, of course, but this isn't directly necessary |
| 2521 | * in the base class. |
| 2522 | */ |
| 2523 | |
| 2524 | /* |
| 2525 | * Variable-size object heap interface. |
| 2526 | */ |
| 2527 | class CVmVarHeap |
| 2528 | { |
| 2529 | public: |
| 2530 | virtual ~CVmVarHeap() { } |
| 2531 | |
| 2532 | /* |
| 2533 | * Initialize. The global object table is valid at this point, and |
| 2534 | * will remain valid until after terminate() is called. |
| 2535 | */ |
| 2536 | virtual void init(VMG0_) = 0; |
| 2537 | |
| 2538 | /* |
| 2539 | * Terminate. The global object table will remain valid until after |
| 2540 | * this function returns. |
| 2541 | * |
| 2542 | * The object table is expected to free each object's variable part |
| 2543 | * explicitly, so this function need not deallocate the memory used |
| 2544 | * by variable parts. |
| 2545 | */ |
| 2546 | virtual void terminate() = 0; |
| 2547 | |
| 2548 | /* |
| 2549 | * Allocate a variable-size part. 'siz' is the size requested in |
| 2550 | * bytes, and 'obj' is a pointer to the object header. The object |
| 2551 | * header will never move in memory, so this pointer is valid for as |
| 2552 | * long as the object remains allocated, hence the heap manager can |
| 2553 | * store the header pointer with the memory block if desired. |
| 2554 | */ |
| 2555 | virtual void *alloc_mem(size_t siz, CVmObject *obj) = 0; |
| 2556 | |
| 2557 | /* |
| 2558 | * Resize a variable-size part. The 'siz' is the new size requested |
| 2559 | * in bytes, and 'varpart' is the old variable-size memory block. |
| 2560 | * This should return a new variable-size memory block containing a |
| 2561 | * copy of the data in the original block, but the block should be |
| 2562 | * resized to at least 'siz' bytes. Returns a pointer to the new |
| 2563 | * block, which may move to a new memory location. |
| 2564 | * |
| 2565 | * If we move the memory to a new location, we are responsible for |
| 2566 | * freeing the old block of memory that the variable part occupied, |
| 2567 | * if necessary. |
| 2568 | * |
| 2569 | * We do not need to worry about informing the object header of any |
| 2570 | * change to the address of the variable part; the caller is |
| 2571 | * responsible for making any necessary changes in the object header |
| 2572 | * based on our return value. |
| 2573 | */ |
| 2574 | virtual void *realloc_mem(size_t siz, void *varpart, |
| 2575 | CVmObject *obj) = 0; |
| 2576 | |
| 2577 | /* |
| 2578 | * Free an object in the heap. The object header may no longer be |
| 2579 | * valid after this function returns, so the heap manager should not |
| 2580 | * store the object header pointer after this function returns. |
| 2581 | */ |
| 2582 | virtual void free_mem(void *varpart) = 0; |
| 2583 | |
| 2584 | #if 0 |
| 2585 | /* |
| 2586 | * This is not currently used by the heap implementation (and doesn't |
| 2587 | * even have any theoretical reason to exist at the moment, since the |
| 2588 | * adoption of a non-moveable heap policy and the removal of |
| 2589 | * move_var_part()), so it's been removed to avoid the unnecessary |
| 2590 | * overhead of calling an empty method. |
| 2591 | */ |
| 2592 | |
| 2593 | /* |
| 2594 | * Receive notification of the completion of a garbage collection |
| 2595 | * pass. If the heap manager is capable of closing gaps in memory |
| 2596 | * by moving objects around, this is a good time to perform this |
| 2597 | * work, since we have just deleted all unreachable objects. |
| 2598 | * |
| 2599 | * If any object moves during processing here, we must call the |
| 2600 | * associated CVmObject's move_var_part() routine to tell it about |
| 2601 | * its new location. |
| 2602 | * |
| 2603 | * This routine isn't required to do anything at all. It's simply |
| 2604 | * provided as a notification for heap managers that can take |
| 2605 | * advantage of the opportunity to compact the heap. |
| 2606 | */ |
| 2607 | virtual void finish_gc_pass() = 0; |
| 2608 | #endif |
| 2609 | }; |
| 2610 | |
| 2611 | |
| 2612 | /* ------------------------------------------------------------------------ */ |
| 2613 | /* |
| 2614 | * Simple variable-size object heap implementation. This implementation |
| 2615 | * uses the normal C heap manager (malloc and free) to manage the heap. |
| 2616 | */ |
| 2617 | |
| 2618 | /* |
| 2619 | * block header - we use the header to keep track of the size of the |
| 2620 | * object's data area |
| 2621 | */ |
| 2622 | struct CVmVarHeapMallocHdr |
| 2623 | { |
| 2624 | /* size of the object */ |
| 2625 | size_t siz_; |
| 2626 | }; |
| 2627 | |
| 2628 | /* |
| 2629 | * heap implementation |
| 2630 | */ |
| 2631 | class CVmVarHeapMalloc: public CVmVarHeap |
| 2632 | { |
| 2633 | public: |
| 2634 | CVmVarHeapMalloc() { } |
| 2635 | ~CVmVarHeapMalloc() { } |
| 2636 | |
| 2637 | /* initialize */ |
| 2638 | void init(VMG0_) { } |
| 2639 | |
| 2640 | /* terminate */ |
| 2641 | void terminate() { } |
| 2642 | |
| 2643 | /* allocate memory */ |
| 2644 | void *alloc_mem(size_t siz, CVmObject *) |
| 2645 | { |
| 2646 | /* allocate space for the block plus the header */ |
| 2647 | CVmVarHeapMallocHdr *hdr = (CVmVarHeapMallocHdr *) |
| 2648 | t3malloc(siz + sizeof(CVmVarHeapMallocHdr)); |
| 2649 | |
| 2650 | /* set up the header */ |
| 2651 | hdr->siz_ = siz; |
| 2652 | |
| 2653 | /* return the start of the part immediately after the header */ |
| 2654 | return (void *)(hdr + 1); |
| 2655 | } |
| 2656 | |
| 2657 | /* reallocate memory */ |
| 2658 | void *realloc_mem(size_t siz, void *varpart, CVmObject *) |
| 2659 | { |
| 2660 | CVmVarHeapMallocHdr *hdr; |
| 2661 | |
| 2662 | /* |
| 2663 | * get the original header, which immediately precedes the |
| 2664 | * original variable part in memory |
| 2665 | */ |
| 2666 | hdr = ((CVmVarHeapMallocHdr *)varpart) - 1; |
| 2667 | |
| 2668 | /* |
| 2669 | * Reallocate it - the header is the actual memory block as far |
| 2670 | * as malloc was concerned, so realloc that. Note that we must |
| 2671 | * add in the space needed for our header in the resized block. |
| 2672 | */ |
| 2673 | hdr = (CVmVarHeapMallocHdr *) |
| 2674 | t3realloc(hdr, siz + sizeof(CVmVarHeapMallocHdr)); |
| 2675 | |
| 2676 | /* adjust the size of the block in the header */ |
| 2677 | hdr->siz_ = siz; |
| 2678 | |
| 2679 | /* return the part immediately after the header */ |
| 2680 | return (void *)(hdr + 1); |
| 2681 | } |
| 2682 | |
| 2683 | /* free memory */ |
| 2684 | void free_mem(void *varpart) |
| 2685 | { |
| 2686 | CVmVarHeapMallocHdr *hdr; |
| 2687 | |
| 2688 | /* |
| 2689 | * get the original header, which immediately precedes the |
| 2690 | * original variable part in memory |
| 2691 | */ |
| 2692 | hdr = ((CVmVarHeapMallocHdr *)varpart) - 1; |
| 2693 | |
| 2694 | /* |
| 2695 | * free the header, which is the actual memory block as far as |
| 2696 | * malloc was concerned |
| 2697 | */ |
| 2698 | t3free(hdr); |
| 2699 | } |
| 2700 | |
| 2701 | #if 0 |
| 2702 | /* removed with the removal of move_var_part() */ |
| 2703 | |
| 2704 | /* |
| 2705 | * complete garbage collection pass - we don't have to do anything |
| 2706 | * here, since we can't move objects around to consolidate free |
| 2707 | * space |
| 2708 | */ |
| 2709 | void finish_gc_pass() { } |
| 2710 | #endif |
| 2711 | |
| 2712 | private: |
| 2713 | }; |
| 2714 | |
| 2715 | /* ------------------------------------------------------------------------ */ |
| 2716 | /* |
| 2717 | * Hybrid cell-based and malloc-based heap allocator. This heap manager |
| 2718 | * uses arrays of fixed-size blocks to allocate small objects, and falls |
| 2719 | * back on malloc for allocating large objects. Small-block allocations |
| 2720 | * and frees are fast, require very little memory overhead, and minimize |
| 2721 | * heap fragmentation by packing large blocks of fixed-size items into |
| 2722 | * arrays, then suballocating out of free lists built from the arrays. |
| 2723 | */ |
| 2724 | |
| 2725 | /* |
| 2726 | * Each item we allocate from a small-object array has a header that |
| 2727 | * points back to the array's master list. This is necessary so that we |
| 2728 | * can put the object back in the appropriate free list when it is |
| 2729 | * deleted. |
| 2730 | */ |
| 2731 | struct CVmVarHeapHybrid_hdr |
| 2732 | { |
| 2733 | /* |
| 2734 | * the block interface that allocated this object -- we use this |
| 2735 | * when we free the object so that we can call the free() routine in |
| 2736 | * the block manager that originally did the allocation |
| 2737 | */ |
| 2738 | class CVmVarHeapHybrid_block *block; |
| 2739 | |
| 2740 | /* requested allocation size of the block, if collecting statistics */ |
| 2741 | IF_GC_STATS(size_t siz;) |
| 2742 | }; |
| 2743 | |
| 2744 | /* |
| 2745 | * Hybrid heap allocator - sub-block interface. Each small-object cell |
| 2746 | * list is represented by one of these objects, as is the fallback |
| 2747 | * malloc allocator. |
| 2748 | */ |
| 2749 | class CVmVarHeapHybrid_block |
| 2750 | { |
| 2751 | public: |
| 2752 | virtual ~CVmVarHeapHybrid_block() {} |
| 2753 | |
| 2754 | /* allocate memory */ |
| 2755 | virtual struct CVmVarHeapHybrid_hdr *alloc(size_t siz) = 0; |
| 2756 | |
| 2757 | /* free memory */ |
| 2758 | virtual void free(struct CVmVarHeapHybrid_hdr *) = 0; |
| 2759 | |
| 2760 | /* |
| 2761 | * Reallocate memory. If necessary, allocate new memory, copy the |
| 2762 | * data to the new memory, and delete the old memory. We receive |
| 2763 | * the heap manager as an argument so that we can call it to |
| 2764 | * allocate new memory if necessary. |
| 2765 | */ |
| 2766 | virtual void *realloc(struct CVmVarHeapHybrid_hdr *mem, size_t siz, |
| 2767 | class CVmObject *obj) = 0; |
| 2768 | |
| 2769 | /* requested allocation size of the block, if collecting statistics */ |
| 2770 | IF_GC_STATS(size_t siz;) |
| 2771 | }; |
| 2772 | |
| 2773 | /* |
| 2774 | * Malloc suballocator |
| 2775 | */ |
| 2776 | class CVmVarHeapHybrid_malloc: public CVmVarHeapHybrid_block |
| 2777 | { |
| 2778 | public: |
| 2779 | /* allocate memory */ |
| 2780 | virtual struct CVmVarHeapHybrid_hdr *alloc(size_t siz) |
| 2781 | { |
| 2782 | CVmVarHeapHybrid_hdr *ptr; |
| 2783 | |
| 2784 | /* adjust the size to add in the required header */ |
| 2785 | siz = osrndsz(siz + sizeof(CVmVarHeapHybrid_hdr)); |
| 2786 | |
| 2787 | /* allocate directly via the default system heap manager */ |
| 2788 | ptr = (CVmVarHeapHybrid_hdr *)t3malloc(siz); |
| 2789 | |
| 2790 | /* fill in the header */ |
| 2791 | ptr->block = this; |
| 2792 | |
| 2793 | /* return the new block */ |
| 2794 | return ptr; |
| 2795 | } |
| 2796 | |
| 2797 | /* release memory */ |
| 2798 | virtual void free(CVmVarHeapHybrid_hdr *mem) |
| 2799 | { |
| 2800 | /* release the memory directly to the default system heap manager */ |
| 2801 | t3free(mem); |
| 2802 | } |
| 2803 | |
| 2804 | /* reallocate memory */ |
| 2805 | virtual void *realloc(struct CVmVarHeapHybrid_hdr *mem, size_t siz, |
| 2806 | CVmObject *) |
| 2807 | { |
| 2808 | CVmVarHeapHybrid_hdr *ptr; |
| 2809 | |
| 2810 | /* adjust the new size to add in the required header */ |
| 2811 | siz = osrndsz(siz + sizeof(CVmVarHeapHybrid_hdr)); |
| 2812 | |
| 2813 | /* reallocate the block */ |
| 2814 | ptr = (CVmVarHeapHybrid_hdr *)t3realloc(mem, siz); |
| 2815 | |
| 2816 | /* fill in the header in the new block */ |
| 2817 | ptr->block = this; |
| 2818 | |
| 2819 | /* return the caller-visible part of the new block */ |
| 2820 | return (void *)(ptr + 1); |
| 2821 | } |
| 2822 | }; |
| 2823 | |
| 2824 | /* |
| 2825 | * Small-object array list head. We suballocate small objects from |
| 2826 | * these arrays. We maintain one of these objects for each distinct |
| 2827 | * cell size; this object manages all of the storage for blocks of the |
| 2828 | * cell size. |
| 2829 | */ |
| 2830 | class CVmVarHeapHybrid_head: public CVmVarHeapHybrid_block |
| 2831 | { |
| 2832 | public: |
| 2833 | CVmVarHeapHybrid_head(class CVmVarHeapHybrid *mem_mgr, |
| 2834 | size_t cell_size, size_t page_count) |
| 2835 | { |
| 2836 | /* remember our memory manager */ |
| 2837 | mem_mgr_ = mem_mgr; |
| 2838 | |
| 2839 | /* remember our cell size and number of items per array */ |
| 2840 | cell_size_ = cell_size; |
| 2841 | page_count_ = page_count; |
| 2842 | |
| 2843 | /* we have nothing in our free list yet */ |
| 2844 | first_free_ = 0; |
| 2845 | } |
| 2846 | |
| 2847 | /* allocate an object from my pool, expanding the pool if necessary */ |
| 2848 | CVmVarHeapHybrid_hdr *alloc(size_t siz); |
| 2849 | |
| 2850 | /* free a cell */ |
| 2851 | void free(CVmVarHeapHybrid_hdr *mem); |
| 2852 | |
| 2853 | /* reallocate */ |
| 2854 | virtual void *realloc(struct CVmVarHeapHybrid_hdr *mem, size_t siz, |
| 2855 | class CVmObject *obj); |
| 2856 | |
| 2857 | /* get the cell size for this cell manager */ |
| 2858 | size_t get_cell_size() const { return cell_size_; } |
| 2859 | |
| 2860 | private: |
| 2861 | /* size of each cell in the array */ |
| 2862 | size_t cell_size_; |
| 2863 | |
| 2864 | /* number of items we allocate per array */ |
| 2865 | size_t page_count_; |
| 2866 | |
| 2867 | /* head of the free list of cells in this array */ |
| 2868 | void *first_free_; |
| 2869 | |
| 2870 | /* our memory manager */ |
| 2871 | CVmVarHeapHybrid *mem_mgr_; |
| 2872 | }; |
| 2873 | |
| 2874 | /* |
| 2875 | * Small-object array list block. We dynamically allocate these array |
| 2876 | * blocks as needed to hold blocks of a particular size. |
| 2877 | */ |
| 2878 | struct CVmVarHeapHybrid_array |
| 2879 | { |
| 2880 | /* next array in the master list */ |
| 2881 | CVmVarHeapHybrid_array *next_array; |
| 2882 | |
| 2883 | /* |
| 2884 | * memory for allocation (we over-allocate the structure to make |
| 2885 | * room for some number of our fixed-size cells) |
| 2886 | */ |
| 2887 | char mem[1]; |
| 2888 | }; |
| 2889 | |
| 2890 | /* |
| 2891 | * heap implementation |
| 2892 | */ |
| 2893 | class CVmVarHeapHybrid: public CVmVarHeap |
| 2894 | { |
| 2895 | friend class CVmVarHeapHybrid_head; |
| 2896 | |
| 2897 | public: |
| 2898 | CVmVarHeapHybrid(CVmObjTable *objtab); |
| 2899 | ~CVmVarHeapHybrid(); |
| 2900 | |
| 2901 | /* initialize */ |
| 2902 | void init(VMG0_) { } |
| 2903 | |
| 2904 | /* terminate */ |
| 2905 | void terminate() { } |
| 2906 | |
| 2907 | /* allocate memory */ |
| 2908 | void *alloc_mem(size_t siz, CVmObject *obj); |
| 2909 | |
| 2910 | /* reallocate memory */ |
| 2911 | void *realloc_mem(size_t siz, void *varpart, CVmObject *obj); |
| 2912 | |
| 2913 | /* free memory */ |
| 2914 | void free_mem(void *varpart); |
| 2915 | |
| 2916 | #if 0 |
| 2917 | /* removed with the removal of move_var_part() */ |
| 2918 | |
| 2919 | /* |
| 2920 | * complete garbage collection pass - we don't have to do anything |
| 2921 | * here, since we can't move objects around to consolidate free |
| 2922 | * space |
| 2923 | */ |
| 2924 | void finish_gc_pass() { } |
| 2925 | #endif |
| 2926 | |
| 2927 | private: |
| 2928 | /* |
| 2929 | * Head of list of arrays. We keep this list so that we can delete |
| 2930 | * all of the arrays when we delete this heap manager object itself. |
| 2931 | */ |
| 2932 | CVmVarHeapHybrid_array *first_array_; |
| 2933 | |
| 2934 | /* |
| 2935 | * Array of cell-based subheap managers. This array will be ordered |
| 2936 | * from smallest to largest, so we can search it for the best fit to |
| 2937 | * a requested size. |
| 2938 | */ |
| 2939 | CVmVarHeapHybrid_head **cell_heaps_; |
| 2940 | |
| 2941 | /* number of cell heap managers */ |
| 2942 | size_t cell_heap_cnt_; |
| 2943 | |
| 2944 | /* |
| 2945 | * Our fallback malloc heap manager. We'll use this allocator for |
| 2946 | * any blocks that we can't allocate from one of our cell-based |
| 2947 | * memory managers. |
| 2948 | */ |
| 2949 | CVmVarHeapHybrid_malloc *malloc_heap_; |
| 2950 | |
| 2951 | /* object table */ |
| 2952 | CVmObjTable *objtab_; |
| 2953 | }; |
| 2954 | |
| 2955 | /* ------------------------------------------------------------------------ */ |
| 2956 | /* |
| 2957 | * Memory Manager - this is the primary interface to the object memory |
| 2958 | * subsystem. |
| 2959 | */ |
| 2960 | class CVmMemory |
| 2961 | { |
| 2962 | public: |
| 2963 | /* create the memory manager, using a given variable-size heap */ |
| 2964 | CVmMemory(VMG_ CVmVarHeap *varheap); |
| 2965 | |
| 2966 | /* delete the memory manager */ |
| 2967 | ~CVmMemory() |
| 2968 | { |
| 2969 | /* tell the variable-size heap to disengage */ |
| 2970 | varheap_->terminate(); |
| 2971 | } |
| 2972 | |
| 2973 | /* get the variable heap manager */ |
| 2974 | CVmVarHeap *get_var_heap() const { return varheap_; } |
| 2975 | |
| 2976 | private: |
| 2977 | /* variable-size object heap */ |
| 2978 | CVmVarHeap *varheap_; |
| 2979 | |
| 2980 | /* our constant pool manager */ |
| 2981 | class CVmPool *constant_pool_; |
| 2982 | }; |
| 2983 | |
| 2984 | /* ------------------------------------------------------------------------ */ |
| 2985 | /* |
| 2986 | * Allocate a new object ID |
| 2987 | */ |
| 2988 | inline vm_obj_id_t vm_new_id(VMG_ int in_root_set) |
| 2989 | { |
| 2990 | /* ask the global object table to allocate a new ID */ |
| 2991 | return G_obj_table->alloc_obj(vmg_ in_root_set); |
| 2992 | } |
| 2993 | |
| 2994 | /* |
| 2995 | * Allocate a new object ID, setting GC characteristics |
| 2996 | */ |
| 2997 | inline vm_obj_id_t vm_new_id(VMG_ int in_root_set, int can_have_refs, |
| 2998 | int can_have_weak_refs) |
| 2999 | { |
| 3000 | /* ask the global object table to allocate a new ID */ |
| 3001 | return G_obj_table->alloc_obj(vmg_ in_root_set, can_have_refs, |
| 3002 | can_have_weak_refs); |
| 3003 | } |
| 3004 | |
| 3005 | /* |
| 3006 | * Given an object ID, get a pointer to the object |
| 3007 | */ |
| 3008 | inline CVmObject *vm_objp(VMG_ vm_obj_id_t id) |
| 3009 | { |
| 3010 | /* ask the global object table to translate the ID */ |
| 3011 | return G_obj_table->get_obj(id); |
| 3012 | } |
| 3013 | |
| 3014 | /* ------------------------------------------------------------------------ */ |
| 3015 | /* |
| 3016 | * Dynamic cast of a vm_val_t to a given object type. Returns null if the |
| 3017 | * vm_val_t isn't an object of the given type. |
| 3018 | */ |
| 3019 | #define vm_val_cast(cls, vv) \ |
| 3020 | (vm_val_cast_ok(cls, vv) ? (cls *)vm_objp(vmg_ (vv)->val.obj) : 0) |
| 3021 | |
| 3022 | /* |
| 3023 | * Will a dynamic cast of a vm_val_t to the given object type succeed? |
| 3024 | */ |
| 3025 | #define vm_val_cast_ok(cls, vv) \ |
| 3026 | ((vv)->typ == VM_OBJ && \ |
| 3027 | vm_objp(vmg_ ((vv)->val.obj))->is_of_metaclass(cls::metaclass_reg_)) |
| 3028 | |
| 3029 | /* |
| 3030 | * Cast a vm_obj_id_t to a given object type |
| 3031 | */ |
| 3032 | #define vm_objid_cast(cls, id) \ |
| 3033 | (id != VM_INVALID_OBJ \ |
| 3034 | && vm_objp(vmg_ id)->is_of_metaclass(cls::metaclass_reg_) \ |
| 3035 | ? (cls *)vm_objp(vmg_ id) : 0) |
| 3036 | |
| 3037 | /* |
| 3038 | * Get the class object for a given metaclass |
| 3039 | */ |
| 3040 | #define vm_classobj_for(cls) \ |
| 3041 | vm_objid_cast(CVmObjClass, cls::metaclass_reg_->get_class_obj(vmg0_)) |
| 3042 | |
| 3043 | #endif /* VMOBJ_H */ |
| 3044 | |
| 3045 | /* |
| 3046 | * Register the root object class |
| 3047 | */ |
| 3048 | VM_REGISTER_METACLASS(CVmObject) |