| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header$"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 2001, 2002 Michael J. Roberts. All Rights Reserved. |
| 8 | * |
| 9 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 10 | * on using and copying this software. |
| 11 | */ |
| 12 | /* |
| 13 | Name |
| 14 | vmlookup.cpp - LookupTable metaclass implementation |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 02/06/01 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <assert.h> |
| 26 | |
| 27 | #include "t3std.h" |
| 28 | #include "vmlookup.h" |
| 29 | #include "vmerr.h" |
| 30 | #include "vmerrnum.h" |
| 31 | #include "vmtype.h" |
| 32 | #include "vmglob.h" |
| 33 | #include "vmobj.h" |
| 34 | #include "vmundo.h" |
| 35 | #include "vmmcreg.h" |
| 36 | #include "vmfile.h" |
| 37 | #include "vmbif.h" |
| 38 | #include "vmmeta.h" |
| 39 | #include "vmstack.h" |
| 40 | #include "vmiter.h" |
| 41 | #include "vmrun.h" |
| 42 | #include "vmlst.h" |
| 43 | #include "vmvec.h" |
| 44 | |
| 45 | |
| 46 | |
| 47 | /* ------------------------------------------------------------------------ */ |
| 48 | /* |
| 49 | * LookupTable undo record. We attach this record to the standard undo |
| 50 | * record via the 'ptrval' field. |
| 51 | */ |
| 52 | struct lookuptab_undo_rec |
| 53 | { |
| 54 | /* action type */ |
| 55 | enum lookuptab_undo_action action; |
| 56 | |
| 57 | /* key value */ |
| 58 | vm_val_t key; |
| 59 | }; |
| 60 | |
| 61 | |
| 62 | /* ------------------------------------------------------------------------ */ |
| 63 | /* |
| 64 | * Allocate a new extension structure |
| 65 | */ |
| 66 | vm_lookup_ext *vm_lookup_ext::alloc_ext(VMG_ CVmObjLookupTable *self, |
| 67 | uint bucket_cnt, uint value_cnt) |
| 68 | { |
| 69 | size_t siz; |
| 70 | vm_lookup_ext *ext; |
| 71 | |
| 72 | /* calculate how much space we need */ |
| 73 | siz = sizeof(vm_lookup_ext) |
| 74 | + (bucket_cnt - 1) * sizeof(ext->buckets[0]) |
| 75 | + value_cnt * sizeof(vm_lookup_val); |
| 76 | |
| 77 | /* allocate the memory */ |
| 78 | ext = (vm_lookup_ext *)G_mem->get_var_heap()->alloc_mem(siz, self); |
| 79 | |
| 80 | /* remember the table sizes */ |
| 81 | ext->bucket_cnt = bucket_cnt; |
| 82 | ext->value_cnt = value_cnt; |
| 83 | |
| 84 | /* set the default value to nil initially */ |
| 85 | ext->default_value.set_nil(); |
| 86 | |
| 87 | /* return the new extension */ |
| 88 | return ext; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * initialize the buckets and free list |
| 93 | */ |
| 94 | void vm_lookup_ext::init_ext() |
| 95 | { |
| 96 | uint i; |
| 97 | vm_lookup_val **bucketp; |
| 98 | vm_lookup_val *valp; |
| 99 | |
| 100 | /* all of the buckets are initially empty */ |
| 101 | for (i = bucket_cnt, bucketp = buckets ; i != 0 ; --i, ++bucketp) |
| 102 | *bucketp = 0; |
| 103 | |
| 104 | /* |
| 105 | * Initialize the value free list. We suballocate the value entry |
| 106 | * structures out of our main allocation block; the available memory |
| 107 | * for the structures comes immediately after the last bucket, so |
| 108 | * bucketp points to the first value entry. |
| 109 | */ |
| 110 | valp = (vm_lookup_val *)(void *)bucketp; |
| 111 | first_free = valp; |
| 112 | for (first_free = valp, i = value_cnt ; i != 0 ; --i, ++valp) |
| 113 | { |
| 114 | /* link it into the free list */ |
| 115 | valp->nxt = valp + 1; |
| 116 | |
| 117 | /* mark it as empty */ |
| 118 | valp->key.set_empty(); |
| 119 | valp->val.set_empty(); |
| 120 | } |
| 121 | |
| 122 | /* terminate the free list at the last element */ |
| 123 | (valp - 1)->nxt = 0; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Reallocate an extension structure, adding more value entries. |
| 128 | */ |
| 129 | vm_lookup_ext *vm_lookup_ext::expand_ext(VMG_ CVmObjLookupTable *self, |
| 130 | vm_lookup_ext *old_ext, |
| 131 | uint new_value_cnt) |
| 132 | { |
| 133 | vm_lookup_ext *new_ext; |
| 134 | |
| 135 | /* allocate a new extension structure of the requested size */ |
| 136 | new_ext = alloc_ext(vmg_ self, old_ext->bucket_cnt, new_value_cnt); |
| 137 | |
| 138 | /* copy the old extension data into the new extension */ |
| 139 | new_ext->copy_ext_from(old_ext); |
| 140 | |
| 141 | /* delete the old memory */ |
| 142 | G_mem->get_var_heap()->free_mem(old_ext); |
| 143 | |
| 144 | /* return the new extension */ |
| 145 | return new_ext; |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Copy extension data |
| 150 | */ |
| 151 | void vm_lookup_ext::copy_ext_from(vm_lookup_ext *old_ext) |
| 152 | { |
| 153 | vm_lookup_val **oldbp; |
| 154 | vm_lookup_val **newbp; |
| 155 | vm_lookup_val *oldval; |
| 156 | vm_lookup_val *newval; |
| 157 | char *oldbase; |
| 158 | char *newbase; |
| 159 | uint i; |
| 160 | |
| 161 | /* the bucket counts must be the same in both extensions */ |
| 162 | assert(bucket_cnt == old_ext->bucket_cnt); |
| 163 | |
| 164 | /* we must have at least as many entries as the old one had */ |
| 165 | assert(value_cnt >= old_ext->value_cnt); |
| 166 | |
| 167 | /* copy the default value */ |
| 168 | default_value = old_ext->default_value; |
| 169 | |
| 170 | /* get the new and old base values for faster pointer arithmetic */ |
| 171 | oldbase = (char *)old_ext->idx_to_val(0); |
| 172 | newbase = (char *)idx_to_val(0); |
| 173 | |
| 174 | /* copy the hash buckets from the old extension to the new extension */ |
| 175 | for (i = old_ext->bucket_cnt, oldbp = old_ext->buckets, newbp = buckets ; |
| 176 | i != 0 ; --i, ++newbp, ++oldbp) |
| 177 | { |
| 178 | /* copy the bucket pointer, adjusting to our local pointer scheme */ |
| 179 | *newbp = (*oldbp == 0 |
| 180 | ? 0 |
| 181 | : (vm_lookup_val *)(((char *)*oldbp - oldbase) + newbase)); |
| 182 | } |
| 183 | |
| 184 | /* copy the values from the old extension to the new extension */ |
| 185 | for (i = old_ext->value_cnt, oldval = old_ext->idx_to_val(0), |
| 186 | newval = idx_to_val(0) ; |
| 187 | i != 0 ; |
| 188 | --i, ++oldval, ++newval) |
| 189 | { |
| 190 | /* copy this entry */ |
| 191 | newval->key = oldval->key; |
| 192 | newval->val = oldval->val; |
| 193 | newval->nxt = (oldval->nxt == 0 |
| 194 | ? 0 |
| 195 | : (vm_lookup_val *)(((char *)oldval->nxt - oldbase) |
| 196 | + newbase)); |
| 197 | } |
| 198 | |
| 199 | /* link all of the remaining free values into the free list */ |
| 200 | first_free = newval; |
| 201 | for (i = value_cnt - old_ext->value_cnt ; i != 0 ; --i, ++newval) |
| 202 | { |
| 203 | /* link this value into the free list */ |
| 204 | newval->nxt = newval + 1; |
| 205 | |
| 206 | /* mark this free value as empty */ |
| 207 | newval->key.set_empty(); |
| 208 | newval->val.set_empty(); |
| 209 | } |
| 210 | |
| 211 | /* terminate the free list */ |
| 212 | (newval - 1)->nxt = 0; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | /* ------------------------------------------------------------------------ */ |
| 217 | /* |
| 218 | * LookupTable object statics |
| 219 | */ |
| 220 | |
| 221 | /* metaclass registration object */ |
| 222 | static CVmMetaclassLookupTable metaclass_reg_obj; |
| 223 | CVmMetaclass *CVmObjLookupTable::metaclass_reg_ = &metaclass_reg_obj; |
| 224 | |
| 225 | /* function table */ |
| 226 | int (CVmObjLookupTable:: |
| 227 | *CVmObjLookupTable::func_table_[])(VMG_ vm_obj_id_t self, |
| 228 | vm_val_t *retval, uint *argc) = |
| 229 | { |
| 230 | &CVmObjLookupTable::getp_undef, /* 0 */ |
| 231 | &CVmObjLookupTable::getp_key_present, /* 1 */ |
| 232 | &CVmObjLookupTable::getp_remove_entry, /* 2 */ |
| 233 | &CVmObjLookupTable::getp_apply_all, /* 3 */ |
| 234 | &CVmObjLookupTable::getp_for_each, /* 4 */ |
| 235 | &CVmObjLookupTable::getp_count_buckets, /* 5 */ |
| 236 | &CVmObjLookupTable::getp_count_entries, /* 6 */ |
| 237 | &CVmObjLookupTable::getp_for_each_assoc, /* 7 */ |
| 238 | &CVmObjLookupTable::getp_keys_to_list, /* 8 */ |
| 239 | &CVmObjLookupTable::getp_vals_to_list, /* 9 */ |
| 240 | &CVmObjLookupTable::getp_get_def_val, /* 10 */ |
| 241 | &CVmObjLookupTable::getp_set_def_val, /* 11 */ |
| 242 | &CVmObjLookupTable::getp_nthKey, /* 12 */ |
| 243 | &CVmObjLookupTable::getp_nthVal /* 13 */ |
| 244 | }; |
| 245 | |
| 246 | |
| 247 | /* ------------------------------------------------------------------------ */ |
| 248 | /* |
| 249 | * Lookup Table metaclass implementation |
| 250 | */ |
| 251 | |
| 252 | /* |
| 253 | * create a lookup table with a given hash table size and the given |
| 254 | * initial entry table size |
| 255 | */ |
| 256 | CVmObjLookupTable::CVmObjLookupTable(VMG_ size_t bucket_cnt, size_t val_cnt) |
| 257 | { |
| 258 | vm_val_t empty; |
| 259 | |
| 260 | /* set up an empty value */ |
| 261 | empty.set_empty(); |
| 262 | |
| 263 | /* allocate our extension structure from the variable heap */ |
| 264 | ext_ = (char *)vm_lookup_ext::alloc_ext(vmg_ this, bucket_cnt, val_cnt); |
| 265 | |
| 266 | /* initialize the extension */ |
| 267 | get_ext()->init_ext(); |
| 268 | } |
| 269 | |
| 270 | |
| 271 | /* |
| 272 | * create |
| 273 | */ |
| 274 | vm_obj_id_t CVmObjLookupTable::create(VMG_ int in_root_set, |
| 275 | uint bucket_count, uint init_capacity) |
| 276 | { |
| 277 | vm_obj_id_t id; |
| 278 | |
| 279 | /* allocate the object ID */ |
| 280 | id = vm_new_id(vmg_ in_root_set, TRUE, FALSE); |
| 281 | |
| 282 | /* create the object */ |
| 283 | new (vmg_ id) CVmObjLookupTable(vmg_ bucket_count, init_capacity); |
| 284 | |
| 285 | /* return the new ID */ |
| 286 | return id; |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * create dynamically using stack arguments |
| 291 | */ |
| 292 | vm_obj_id_t CVmObjLookupTable::create_from_stack( |
| 293 | VMG_ const uchar **pc_ptr, uint argc) |
| 294 | { |
| 295 | vm_obj_id_t id; |
| 296 | size_t bucket_count; |
| 297 | size_t init_capacity; |
| 298 | vm_val_t src_obj; |
| 299 | |
| 300 | /* parse the arguments */ |
| 301 | get_constructor_args(vmg_ argc, &bucket_count, &init_capacity, &src_obj); |
| 302 | |
| 303 | /* |
| 304 | * allocate the object ID - this type of construction never creates a |
| 305 | * root object |
| 306 | */ |
| 307 | id = vm_new_id(vmg_ FALSE, TRUE, FALSE); |
| 308 | |
| 309 | /* create the object */ |
| 310 | new (vmg_ id) CVmObjLookupTable(vmg_ bucket_count, init_capacity); |
| 311 | |
| 312 | /* populate it with the source list if desired */ |
| 313 | ((CVmObjLookupTable *)vm_objp(vmg_ id))-> |
| 314 | populate_from_list(vmg_ &src_obj); |
| 315 | |
| 316 | /* discard the source object gc protection */ |
| 317 | G_stk->discard(); |
| 318 | |
| 319 | /* return the new ID */ |
| 320 | return id; |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * get and check constructor arguments |
| 325 | */ |
| 326 | void CVmObjLookupTable::get_constructor_args(VMG_ uint argc, |
| 327 | size_t *bucket_count, |
| 328 | size_t *init_capacity, |
| 329 | vm_val_t *src_obj) |
| 330 | { |
| 331 | /* presume no source object */ |
| 332 | src_obj->set_nil(); |
| 333 | |
| 334 | /* check arguments */ |
| 335 | if (argc == 0) |
| 336 | { |
| 337 | /* no arguments - they want default parameters */ |
| 338 | *bucket_count = 32; |
| 339 | *init_capacity = 64; |
| 340 | } |
| 341 | else if (argc == 1) |
| 342 | { |
| 343 | int cnt; |
| 344 | |
| 345 | /* |
| 346 | * One argument - they want to create from a list. Retrieve the |
| 347 | * list or vector object. |
| 348 | */ |
| 349 | G_stk->pop(src_obj); |
| 350 | |
| 351 | /* make sure it's a list or vector */ |
| 352 | if (!src_obj->is_listlike(vmg0_) |
| 353 | || (cnt = src_obj->ll_length(vmg0_)) < 0) |
| 354 | err_throw(VMERR_BAD_VAL_BIF); |
| 355 | |
| 356 | /* use 1.5x the pair count as the bucket count and capacity */ |
| 357 | *bucket_count = *init_capacity = cnt*3 / 2; |
| 358 | } |
| 359 | else if (argc == 2) |
| 360 | { |
| 361 | /* |
| 362 | * two arguments - they specified the bucket count and initial |
| 363 | * capacity; pop the parameters |
| 364 | */ |
| 365 | *bucket_count = (size_t)CVmBif::pop_long_val(vmg0_); |
| 366 | *init_capacity = (size_t)CVmBif::pop_long_val(vmg0_); |
| 367 | } |
| 368 | else |
| 369 | { |
| 370 | /* invalid arguments */ |
| 371 | err_throw(VMERR_WRONG_NUM_OF_ARGS); |
| 372 | } |
| 373 | |
| 374 | /* re-push the source object to protect it from gc */ |
| 375 | G_stk->push(src_obj); |
| 376 | |
| 377 | /* make sure both are positive */ |
| 378 | if (*bucket_count <= 0 || *init_capacity <= 0) |
| 379 | err_throw(VMERR_BAD_VAL_BIF); |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * Populate the table from a source list or vector. This should only be |
| 384 | * used during construction, because it doesn't save undo. (It's not |
| 385 | * necessary to save undo during construction because the whole object |
| 386 | * creation and construction is atomic for undo purposes: if we undo any of |
| 387 | * this, we undo the whole creation of the object, so there's no need to |
| 388 | * worry about saving old state internal to the object.) |
| 389 | */ |
| 390 | void CVmObjLookupTable::populate_from_list(VMG_ const vm_val_t *src) |
| 391 | { |
| 392 | /* copy elements from the source object */ |
| 393 | int cnt = src->ll_length(vmg0_); |
| 394 | int i; |
| 395 | for (i = 1 ; i + 1 <= cnt ; ) |
| 396 | { |
| 397 | vm_val_t key, val; |
| 398 | |
| 399 | /* get the next two elements - they're always key, value pairs */ |
| 400 | src->ll_index(vmg_ &key, i++); |
| 401 | src->ll_index(vmg_ &val, i++); |
| 402 | |
| 403 | /* set them in the table */ |
| 404 | set_or_add_entry(vmg_ &key, &val); |
| 405 | } |
| 406 | |
| 407 | /* if there's a single remaining entry, it's the default value */ |
| 408 | if (i <= cnt) |
| 409 | src->ll_index(vmg_ &get_ext()->default_value, i); |
| 410 | } |
| 411 | |
| 412 | |
| 413 | /* |
| 414 | * Create a copy of this object |
| 415 | */ |
| 416 | vm_obj_id_t CVmObjLookupTable::create_copy(VMG0_) |
| 417 | { |
| 418 | vm_obj_id_t id; |
| 419 | CVmObjLookupTable *new_obj; |
| 420 | |
| 421 | /* allocate the object ID */ |
| 422 | id = vm_new_id(vmg_ FALSE, TRUE, FALSE); |
| 423 | |
| 424 | /* allocate a new object */ |
| 425 | new_obj = new (vmg_ id) CVmObjLookupTable(); |
| 426 | |
| 427 | /* |
| 428 | * set up the new object with our same sizes, but don't initialize it |
| 429 | * - we're going to blast all of our data directly into the new |
| 430 | * extension, so we don't need to waste time setting up an empty |
| 431 | * initial state |
| 432 | */ |
| 433 | new_obj->ext_ = (char *)vm_lookup_ext::alloc_ext( |
| 434 | vmg_ new_obj, get_bucket_count(), get_entry_count()); |
| 435 | |
| 436 | /* copy my data to the next object */ |
| 437 | new_obj->get_ext()->copy_ext_from(get_ext()); |
| 438 | |
| 439 | /* return the new object's id */ |
| 440 | return id; |
| 441 | } |
| 442 | |
| 443 | /* |
| 444 | * notify of deletion |
| 445 | */ |
| 446 | void CVmObjLookupTable::notify_delete(VMG_ int /*in_root_set*/) |
| 447 | { |
| 448 | /* free our additional data, if we have any */ |
| 449 | if (ext_ != 0) |
| 450 | G_mem->get_var_heap()->free_mem(ext_); |
| 451 | } |
| 452 | |
| 453 | /* |
| 454 | * set a property |
| 455 | */ |
| 456 | void CVmObjLookupTable::set_prop(VMG_ class CVmUndo *undo, |
| 457 | vm_obj_id_t self, vm_prop_id_t prop, |
| 458 | const vm_val_t *val) |
| 459 | { |
| 460 | /* no settable properties - throw an error */ |
| 461 | err_throw(VMERR_INVALID_SETPROP); |
| 462 | } |
| 463 | |
| 464 | /* |
| 465 | * get a property |
| 466 | */ |
| 467 | int CVmObjLookupTable::get_prop(VMG_ vm_prop_id_t prop, vm_val_t *retval, |
| 468 | vm_obj_id_t self, vm_obj_id_t *source_obj, |
| 469 | uint *argc) |
| 470 | { |
| 471 | uint func_idx; |
| 472 | |
| 473 | /* translate the property into a function vector index */ |
| 474 | func_idx = G_meta_table |
| 475 | ->prop_to_vector_idx(metaclass_reg_->get_reg_idx(), prop); |
| 476 | |
| 477 | /* call the appropriate function */ |
| 478 | if ((this->*func_table_[func_idx])(vmg_ self, retval, argc)) |
| 479 | { |
| 480 | *source_obj = metaclass_reg_->get_class_obj(vmg0_); |
| 481 | return TRUE; |
| 482 | } |
| 483 | |
| 484 | /* inherit default handling from our base class */ |
| 485 | return CVmObjCollection:: |
| 486 | get_prop(vmg_ prop, retval, self, source_obj, argc); |
| 487 | } |
| 488 | |
| 489 | /* |
| 490 | * apply an undo record |
| 491 | */ |
| 492 | void CVmObjLookupTable::apply_undo(VMG_ struct CVmUndoRecord *undo_rec) |
| 493 | { |
| 494 | if (undo_rec->id.ptrval != 0) |
| 495 | { |
| 496 | lookuptab_undo_rec *rec; |
| 497 | |
| 498 | /* get our private record from the standard undo record */ |
| 499 | rec = (lookuptab_undo_rec *)undo_rec->id.ptrval; |
| 500 | |
| 501 | /* check the action in the record */ |
| 502 | switch(rec->action) |
| 503 | { |
| 504 | case LOOKUPTAB_UNDO_NULL: |
| 505 | /* |
| 506 | * null record, which means that it had a weak reference to an |
| 507 | * object that has been deleted - there's no way to reinstate |
| 508 | * such records, so ignore it |
| 509 | */ |
| 510 | break; |
| 511 | |
| 512 | case LOOKUPTAB_UNDO_ADD: |
| 513 | /* we added the entry, so we must now delete it */ |
| 514 | del_entry(vmg_ &rec->key); |
| 515 | break; |
| 516 | |
| 517 | case LOOKUPTAB_UNDO_DEL: |
| 518 | /* we deleted the entry, so we must now add it */ |
| 519 | add_entry(vmg_ &rec->key, &undo_rec->oldval); |
| 520 | break; |
| 521 | |
| 522 | case LOOKUPTAB_UNDO_MOD: |
| 523 | /* we modified the entry, so we must change it back */ |
| 524 | mod_entry(vmg_ &rec->key, &undo_rec->oldval); |
| 525 | break; |
| 526 | |
| 527 | case LOOKUPTAB_UNDO_DEFVAL: |
| 528 | /* we modified the default value; change it back */ |
| 529 | get_ext()->default_value = undo_rec->oldval; |
| 530 | break; |
| 531 | } |
| 532 | |
| 533 | /* discard the private record */ |
| 534 | t3free(rec); |
| 535 | |
| 536 | /* clear the pointer in the main record so we know it's gone */ |
| 537 | undo_rec->id.ptrval = 0; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | * discard extra undo information |
| 543 | */ |
| 544 | void CVmObjLookupTable::discard_undo(VMG_ CVmUndoRecord *rec) |
| 545 | { |
| 546 | /* delete our extra information record */ |
| 547 | if (rec->id.ptrval != 0) |
| 548 | { |
| 549 | /* free the record */ |
| 550 | t3free((lookuptab_undo_rec *)rec->id.ptrval); |
| 551 | |
| 552 | /* clear the pointer so we know it's gone */ |
| 553 | rec->id.ptrval = 0; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | /* |
| 558 | * Mark undo references. |
| 559 | */ |
| 560 | void CVmObjLookupTable:: |
| 561 | mark_undo_ref(VMG_ struct CVmUndoRecord *undo_rec) |
| 562 | { |
| 563 | lookuptab_undo_rec *rec; |
| 564 | |
| 565 | /* get our private record from the standard undo record */ |
| 566 | rec = (lookuptab_undo_rec *)undo_rec->id.ptrval; |
| 567 | |
| 568 | /* if the key in the record is an object, mark it referenced */ |
| 569 | if (rec->key.typ == VM_OBJ) |
| 570 | G_obj_table->mark_all_refs(rec->key.val.obj, VMOBJ_REACHABLE); |
| 571 | |
| 572 | /* if the value in the record is an object, mark it as well */ |
| 573 | if (undo_rec->oldval.typ == VM_OBJ) |
| 574 | G_obj_table->mark_all_refs(undo_rec->oldval.val.obj, |
| 575 | VMOBJ_REACHABLE); |
| 576 | } |
| 577 | |
| 578 | /* |
| 579 | * Mark references. Keys (but not values) are strongly referenced. |
| 580 | */ |
| 581 | void CVmObjLookupTable::mark_refs(VMG_ uint state) |
| 582 | { |
| 583 | /* get my extension */ |
| 584 | vm_lookup_ext *ext = get_ext(); |
| 585 | |
| 586 | /* mark the default value */ |
| 587 | const vm_val_t *val = &ext->default_value; |
| 588 | if (val->typ == VM_OBJ) |
| 589 | G_obj_table->mark_all_refs(val->val.obj, state); |
| 590 | |
| 591 | /* run through my buckets */ |
| 592 | vm_lookup_val **bp = ext->buckets; |
| 593 | size_t i = ext->bucket_cnt; |
| 594 | for ( ; i != 0 ; ++bp, --i) |
| 595 | { |
| 596 | /* run through all entries attached to this bucket */ |
| 597 | for (const vm_lookup_val *entry = *bp ; entry != 0 ; |
| 598 | entry = entry->nxt) |
| 599 | { |
| 600 | /* if the key is an object, mark it as referenced */ |
| 601 | val = &entry->key; |
| 602 | if (val->typ == VM_OBJ) |
| 603 | G_obj_table->mark_all_refs(val->val.obj, state); |
| 604 | |
| 605 | /* if the entry is an object, mark it as referenced */ |
| 606 | val = &entry->val; |
| 607 | if (val->typ == VM_OBJ) |
| 608 | G_obj_table->mark_all_refs(val->val.obj, state); |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | /* |
| 614 | * load from an image file |
| 615 | */ |
| 616 | void CVmObjLookupTable::load_from_image(VMG_ vm_obj_id_t self, |
| 617 | const char *ptr, size_t siz) |
| 618 | { |
| 619 | /* load our image data */ |
| 620 | load_image_data(vmg_ ptr, siz); |
| 621 | |
| 622 | /* |
| 623 | * save our image data pointer in the object table, so that we can |
| 624 | * access it (without storing it ourselves) during a reload |
| 625 | */ |
| 626 | G_obj_table->save_image_pointer(self, ptr, siz); |
| 627 | } |
| 628 | |
| 629 | /* |
| 630 | * reload from the image file |
| 631 | */ |
| 632 | void CVmObjLookupTable::reload_from_image(VMG_ vm_obj_id_t self, |
| 633 | const char *ptr, size_t siz) |
| 634 | { |
| 635 | /* load our image data */ |
| 636 | load_image_data(vmg_ ptr, siz); |
| 637 | } |
| 638 | |
| 639 | /* |
| 640 | * load or reload data from the image |
| 641 | */ |
| 642 | void CVmObjLookupTable::load_image_data(VMG_ const char *ptr, size_t siz) |
| 643 | { |
| 644 | uint bucket_cnt; |
| 645 | uint val_cnt; |
| 646 | uint i; |
| 647 | const char *ibp; |
| 648 | vm_lookup_val **ebp; |
| 649 | vm_lookup_val *eval; |
| 650 | vm_lookup_ext *ext; |
| 651 | |
| 652 | /* free our existing extension, if we have one */ |
| 653 | if (ext_ != 0) |
| 654 | G_mem->get_var_heap()->free_mem(ext_); |
| 655 | |
| 656 | /* read the bucket and value counts from the header */ |
| 657 | bucket_cnt = osrp2(ptr); |
| 658 | val_cnt = osrp2(ptr + 2); |
| 659 | |
| 660 | /* allocate space for a copy of the image data */ |
| 661 | ext = vm_lookup_ext::alloc_ext(vmg_ this, bucket_cnt, val_cnt); |
| 662 | ext_ = (char *)ext; |
| 663 | |
| 664 | /* initialize the free list pointer, given as a 1-based entry index */ |
| 665 | ext->first_free = ext->img_idx_to_val(osrp2(ptr + 4)); |
| 666 | |
| 667 | /* initialize the table from the load image data */ |
| 668 | for (i = bucket_cnt, ibp = ptr + 6, ebp = ext->buckets ; |
| 669 | i != 0 ; --i, ibp += 2, ++ebp) |
| 670 | { |
| 671 | /* translate the image file index to a value pointer */ |
| 672 | *ebp = ext->img_idx_to_val(osrp2(ibp)); |
| 673 | } |
| 674 | |
| 675 | /* initialize the value entries */ |
| 676 | for (i = val_cnt, eval = ext->idx_to_val(0) ; |
| 677 | i != 0 ; --i, ibp += VMLOOKUP_VALUE_SIZE, ++eval) |
| 678 | { |
| 679 | /* read the key and value */ |
| 680 | vmb_get_dh(ibp, &eval->key); |
| 681 | vmb_get_dh(ibp + VMB_DATAHOLDER, &eval->val); |
| 682 | |
| 683 | /* remember the next pointer, which is given as a 1-based index */ |
| 684 | eval->nxt = ext->img_idx_to_val(osrp2(ibp + VMB_DATAHOLDER*2)); |
| 685 | } |
| 686 | |
| 687 | /* if a default value is included, read it */ |
| 688 | ext->default_value.set_nil(); |
| 689 | if (ibp + VMB_DATAHOLDER <= ptr + siz) |
| 690 | { |
| 691 | /* read the default value and skip it in the source data */ |
| 692 | vmb_get_dh(ibp, &ext->default_value); |
| 693 | ibp += VMB_DATAHOLDER; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | /* |
| 698 | * save to a file |
| 699 | */ |
| 700 | void CVmObjLookupTable::save_to_file(VMG_ class CVmFile *fp) |
| 701 | { |
| 702 | vm_lookup_ext *ext = get_ext(); |
| 703 | uint i; |
| 704 | vm_lookup_val **bp; |
| 705 | vm_lookup_val *val; |
| 706 | char buf[VMLOOKUP_VALUE_SIZE]; |
| 707 | |
| 708 | /* write our bucket count, value count, and first-free index */ |
| 709 | fp->write_uint2(ext->bucket_cnt); |
| 710 | fp->write_uint2(ext->value_cnt); |
| 711 | fp->write_uint2(ext->val_to_img_idx(ext->first_free)); |
| 712 | |
| 713 | /* |
| 714 | * If the image file was compiled for version 030003 or later of the |
| 715 | * LookupTable object, save the default value. The default value |
| 716 | * wasn't added until 030003, so source code compiled against earlier |
| 717 | * versions of the metaclass spec can't change it from the 'nil' |
| 718 | * default. Furthermore, these image files could be loaded on VMs that |
| 719 | * have older versions of the metaclass that won't know to look for a |
| 720 | * default value in the loaded data, so to be compatible with those |
| 721 | * readers we must omit the value entirely. If the image file was |
| 722 | * compiled with 030003 or later, it can only be loaded by VMs with |
| 723 | * LookupTable implementations that know to read the value. |
| 724 | */ |
| 725 | if (image_file_version_ge(vmg_ "030003")) |
| 726 | { |
| 727 | vmb_put_dh(buf, &ext->default_value); |
| 728 | fp->write_bytes(buf, VMB_DATAHOLDER); |
| 729 | } |
| 730 | |
| 731 | /* write the buckets */ |
| 732 | for (i = ext->bucket_cnt, bp = ext->buckets ; i != 0 ; --i, ++bp) |
| 733 | fp->write_uint2(ext->val_to_img_idx(*bp)); |
| 734 | |
| 735 | /* write the values */ |
| 736 | for (i = ext->value_cnt, val = ext->idx_to_val(0) ; i != 0 ; --i, ++val) |
| 737 | { |
| 738 | uint idx; |
| 739 | |
| 740 | /* store the key, value, and index */ |
| 741 | vmb_put_dh(buf, &val->key); |
| 742 | vmb_put_dh(buf + VMB_DATAHOLDER, &val->val); |
| 743 | idx = ext->val_to_img_idx(val->nxt); |
| 744 | oswp2(buf + VMB_DATAHOLDER*2, idx); |
| 745 | |
| 746 | /* write the data */ |
| 747 | fp->write_bytes(buf, VMLOOKUP_VALUE_SIZE); |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | /* |
| 752 | * restore from a file |
| 753 | */ |
| 754 | void CVmObjLookupTable::restore_from_file(VMG_ vm_obj_id_t self, |
| 755 | CVmFile *fp, CVmObjFixup *fixups) |
| 756 | { |
| 757 | vm_lookup_ext *ext; |
| 758 | char buf[64]; |
| 759 | size_t bucket_cnt; |
| 760 | size_t val_cnt; |
| 761 | size_t i; |
| 762 | vm_lookup_val **bp; |
| 763 | vm_lookup_val *entry; |
| 764 | uint idx; |
| 765 | |
| 766 | /* free our existing extension, if we have one */ |
| 767 | if (ext_ != 0) |
| 768 | G_mem->get_var_heap()->free_mem(ext_); |
| 769 | |
| 770 | /* read the fixed fields */ |
| 771 | fp->read_bytes(buf, 6); |
| 772 | bucket_cnt = vmb_get_uint2(buf); |
| 773 | val_cnt = vmb_get_uint2(buf + 2); |
| 774 | |
| 775 | /* allocate the extension structure */ |
| 776 | ext = vm_lookup_ext::alloc_ext(vmg_ this, bucket_cnt, val_cnt); |
| 777 | ext_ = (char *)ext; |
| 778 | |
| 779 | /* store the free pointer */ |
| 780 | ext->first_free = ext->img_idx_to_val(vmb_get_uint2(buf + 4)); |
| 781 | |
| 782 | /* |
| 783 | * if the saver was using version 030003 or later, there's a default |
| 784 | * value; otherwise the default value is fixed at 'nil' |
| 785 | */ |
| 786 | ext->default_value.set_nil(); |
| 787 | if (image_file_version_ge(vmg_ "030003")) |
| 788 | { |
| 789 | /* read the default value */ |
| 790 | fp->read_bytes(buf, VMB_DATAHOLDER); |
| 791 | |
| 792 | /* apply fixups */ |
| 793 | fixups->fix_dh(vmg_ buf); |
| 794 | |
| 795 | /* decode it into the default value in the extension */ |
| 796 | vmb_get_dh(buf, &ext->default_value); |
| 797 | } |
| 798 | |
| 799 | /* read the buckets */ |
| 800 | for (i = bucket_cnt, bp = ext->buckets ; i != 0 ; ++bp, --i) |
| 801 | { |
| 802 | /* read this bucket pointer and store it */ |
| 803 | idx = fp->read_uint2(); |
| 804 | *bp = ext->img_idx_to_val(idx); |
| 805 | } |
| 806 | |
| 807 | /* read the value entries */ |
| 808 | for (i = val_cnt, entry = ext->idx_to_val(0) ; i != 0 ; --i, ++entry) |
| 809 | { |
| 810 | /* read this value entry */ |
| 811 | fp->read_bytes(buf, VMLOOKUP_VALUE_SIZE); |
| 812 | |
| 813 | /* fix up the key and value dataholders */ |
| 814 | fixups->fix_dh_array(vmg_ buf, 2); |
| 815 | |
| 816 | /* store the key and value */ |
| 817 | vmb_get_dh(buf, &entry->key); |
| 818 | vmb_get_dh(buf + VMB_DATAHOLDER, &entry->val); |
| 819 | |
| 820 | /* store the next pointer */ |
| 821 | idx = osrp2(buf + VMB_DATAHOLDER*2); |
| 822 | entry->nxt = ext->img_idx_to_val(idx); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | /* |
| 827 | * create an iterator |
| 828 | */ |
| 829 | void CVmObjLookupTable::new_iterator(VMG_ vm_val_t *retval, |
| 830 | const vm_val_t *self_val) |
| 831 | { |
| 832 | vm_val_t copy_val; |
| 833 | |
| 834 | /* save a copy of myself for protection against garbage collection */ |
| 835 | G_stk->push(self_val); |
| 836 | |
| 837 | /* |
| 838 | * create a copy of my object, so that we can iterate over a snapshot |
| 839 | * of our state even if we subsequently change our state |
| 840 | */ |
| 841 | copy_val.set_obj(create_copy(vmg0_)); |
| 842 | |
| 843 | /* set up a new lookup table iterator object on the copy */ |
| 844 | retval->set_obj(CVmObjIterLookupTable::create_for_coll(vmg_ ©_val)); |
| 845 | |
| 846 | /* done with the gc protection */ |
| 847 | G_stk->discard(); |
| 848 | } |
| 849 | |
| 850 | /* |
| 851 | * create a live iterator |
| 852 | */ |
| 853 | void CVmObjLookupTable::new_live_iterator(VMG_ vm_val_t *retval, |
| 854 | const vm_val_t *self_val) |
| 855 | { |
| 856 | /* set up a new lookup table iterator directly on myself */ |
| 857 | retval->set_obj(CVmObjIterLookupTable::create_for_coll(vmg_ self_val)); |
| 858 | } |
| 859 | |
| 860 | /* |
| 861 | * add an undo record |
| 862 | */ |
| 863 | void CVmObjLookupTable::add_undo_rec(VMG_ vm_obj_id_t self, |
| 864 | lookuptab_undo_action action, |
| 865 | const vm_val_t *key, |
| 866 | const vm_val_t *old_entry_val) |
| 867 | { |
| 868 | lookuptab_undo_rec *rec; |
| 869 | vm_val_t nil_val; |
| 870 | |
| 871 | /* allocate our record extension */ |
| 872 | rec = (lookuptab_undo_rec *)t3malloc(sizeof(lookuptab_undo_rec)); |
| 873 | |
| 874 | /* if either the key or old value are null pointers, use a nil value */ |
| 875 | nil_val.set_nil(); |
| 876 | if (key == 0) |
| 877 | key = &nil_val; |
| 878 | if (old_entry_val == 0) |
| 879 | old_entry_val = &nil_val; |
| 880 | |
| 881 | /* set up the record */ |
| 882 | rec->action = action; |
| 883 | rec->key = *key; |
| 884 | |
| 885 | /* add the record to the global undo stream */ |
| 886 | if (!G_undo->add_new_record_ptr_key(vmg_ self, rec, old_entry_val)) |
| 887 | { |
| 888 | /* |
| 889 | * we didn't add an undo record, so our extra undo information |
| 890 | * isn't actually going to be stored in the undo system - hence we |
| 891 | * must delete our extra information |
| 892 | */ |
| 893 | t3free(rec); |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | /* |
| 898 | * Add an entry; does not generate undo. |
| 899 | */ |
| 900 | void CVmObjLookupTable::add_entry(VMG_ |
| 901 | const vm_val_t *key, const vm_val_t *val) |
| 902 | { |
| 903 | uint hashval; |
| 904 | vm_lookup_val *entry; |
| 905 | |
| 906 | /* push the key and value as gc protection for a moment */ |
| 907 | G_stk->push(key); |
| 908 | G_stk->push(val); |
| 909 | |
| 910 | /* calculate the hash value for the key */ |
| 911 | hashval = calc_key_hash(vmg_ key); |
| 912 | |
| 913 | /* allocate an entry */ |
| 914 | entry = alloc_new_entry(vmg0_); |
| 915 | |
| 916 | /* link this entry into the chain at this hash value */ |
| 917 | entry->nxt = get_ext()->buckets[hashval]; |
| 918 | get_ext()->buckets[hashval] = entry; |
| 919 | |
| 920 | /* set the key and value for this entry */ |
| 921 | entry->key = *key; |
| 922 | entry->val = *val; |
| 923 | |
| 924 | /* discard the gc protection */ |
| 925 | G_stk->discard(2); |
| 926 | } |
| 927 | |
| 928 | /* |
| 929 | * Add an entry, generating undo |
| 930 | */ |
| 931 | void CVmObjLookupTable::add_entry_undo(VMG_ vm_obj_id_t self, |
| 932 | const vm_val_t *key, |
| 933 | const vm_val_t *val) |
| 934 | { |
| 935 | /* |
| 936 | * Add undo for the change. Since we're adding a new record, there's |
| 937 | * no old value for the record. |
| 938 | */ |
| 939 | add_undo_rec(vmg_ self, LOOKUPTAB_UNDO_ADD, key, 0); |
| 940 | |
| 941 | /* add the entry */ |
| 942 | add_entry(vmg_ key, val); |
| 943 | } |
| 944 | |
| 945 | |
| 946 | /* |
| 947 | * Delete an entry; does not generate undo. |
| 948 | */ |
| 949 | void CVmObjLookupTable::del_entry(VMG_ const vm_val_t *key) |
| 950 | { |
| 951 | uint hashval; |
| 952 | vm_lookup_val *entry; |
| 953 | vm_lookup_val *prv_entry; |
| 954 | |
| 955 | /* find the entry for the key */ |
| 956 | entry = find_entry(vmg_ key, &hashval, &prv_entry); |
| 957 | |
| 958 | /* if we found it, unlink it */ |
| 959 | if (entry != 0) |
| 960 | unlink_entry(vmg_ entry, hashval, prv_entry); |
| 961 | } |
| 962 | |
| 963 | /* |
| 964 | * Unlink an entry |
| 965 | */ |
| 966 | void CVmObjLookupTable::unlink_entry(VMG_ vm_lookup_val *entry, uint hashval, |
| 967 | vm_lookup_val *prv_entry) |
| 968 | { |
| 969 | vm_lookup_ext *ext = get_ext(); |
| 970 | |
| 971 | /* |
| 972 | * set the previous item's next pointer, or the head of the chain, as |
| 973 | * appropriate |
| 974 | */ |
| 975 | if (prv_entry == 0) |
| 976 | ext->buckets[hashval] = entry->nxt; |
| 977 | else |
| 978 | prv_entry->nxt = entry->nxt; |
| 979 | |
| 980 | /* link the entry into the free list */ |
| 981 | entry->nxt = ext->first_free; |
| 982 | ext->first_free = entry; |
| 983 | |
| 984 | /* set this entry's key and value to 'empty' to indicate that it's free */ |
| 985 | entry->key.set_empty(); |
| 986 | entry->val.set_empty(); |
| 987 | } |
| 988 | |
| 989 | /* |
| 990 | * Modify an entry; does not generate undo. |
| 991 | */ |
| 992 | void CVmObjLookupTable::mod_entry(VMG_ const vm_val_t *key, |
| 993 | const vm_val_t *val) |
| 994 | { |
| 995 | vm_lookup_val *entry; |
| 996 | |
| 997 | /* find the entry for the key */ |
| 998 | entry = find_entry(vmg_ key, 0, 0); |
| 999 | |
| 1000 | /* if we found it, change the value to the given value */ |
| 1001 | if (entry != 0) |
| 1002 | entry->val = *val; |
| 1003 | } |
| 1004 | |
| 1005 | /* |
| 1006 | * Set an entry's value, generating undo |
| 1007 | */ |
| 1008 | void CVmObjLookupTable::set_entry_val_undo(VMG_ vm_obj_id_t self, |
| 1009 | vm_lookup_val *entry, |
| 1010 | const vm_val_t *val) |
| 1011 | { |
| 1012 | /* generate undo for the change */ |
| 1013 | add_undo_rec(vmg_ self, LOOKUPTAB_UNDO_MOD, &entry->key, &entry->val); |
| 1014 | |
| 1015 | /* update the entry */ |
| 1016 | entry->val = *val; |
| 1017 | } |
| 1018 | |
| 1019 | /* |
| 1020 | * Find an entry for a given key. Returns the entry index, and fills in |
| 1021 | * the hash value and previous-entry output variables if provided. |
| 1022 | * Returns zero if there is no such key. |
| 1023 | */ |
| 1024 | vm_lookup_val *CVmObjLookupTable::find_entry(VMG_ const vm_val_t *key, |
| 1025 | uint *hashval_p, |
| 1026 | vm_lookup_val **prv_entry_p) |
| 1027 | { |
| 1028 | vm_lookup_ext *ext = get_ext(); |
| 1029 | uint hashval; |
| 1030 | vm_lookup_val *entry; |
| 1031 | vm_lookup_val *prv_entry; |
| 1032 | |
| 1033 | /* compute the hash value (and return it to the caller if desired) */ |
| 1034 | hashval = calc_key_hash(vmg_ key); |
| 1035 | if (hashval_p != 0) |
| 1036 | *hashval_p = hashval; |
| 1037 | |
| 1038 | /* scan the hash chain for this entry */ |
| 1039 | for (prv_entry = 0, entry = ext->buckets[hashval] ; entry != 0 ; |
| 1040 | prv_entry = entry, entry = entry->nxt) |
| 1041 | { |
| 1042 | /* if it matches the key we're looking for, this is the one */ |
| 1043 | if (entry->key.equals(vmg_ key)) |
| 1044 | break; |
| 1045 | } |
| 1046 | |
| 1047 | /* if the caller wanted to know the previous entry, tell them */ |
| 1048 | if (prv_entry_p != 0) |
| 1049 | *prv_entry_p = prv_entry; |
| 1050 | |
| 1051 | /* return the entry where we found the key, if indeed we found it */ |
| 1052 | return entry; |
| 1053 | } |
| 1054 | |
| 1055 | |
| 1056 | /* |
| 1057 | * Calculate a hash value for a key |
| 1058 | */ |
| 1059 | uint CVmObjLookupTable::calc_key_hash(VMG_ const vm_val_t *key) |
| 1060 | { |
| 1061 | uint hash; |
| 1062 | |
| 1063 | /* calculate the raw hash for the value */ |
| 1064 | hash = key->calc_hash(vmg0_); |
| 1065 | |
| 1066 | /* reduce the hash value modulo the hash table size */ |
| 1067 | hash %= get_bucket_count(); |
| 1068 | |
| 1069 | /* return the hash value */ |
| 1070 | return hash; |
| 1071 | } |
| 1072 | |
| 1073 | /* |
| 1074 | * Allocate a new entry |
| 1075 | */ |
| 1076 | vm_lookup_val *CVmObjLookupTable::alloc_new_entry(VMG0_) |
| 1077 | { |
| 1078 | /* if necessary, add more entry slots to the table */ |
| 1079 | expand_if_needed(vmg0_); |
| 1080 | |
| 1081 | /* get the first entry from the free list */ |
| 1082 | vm_lookup_val *entry = get_first_free(); |
| 1083 | |
| 1084 | /* unlink this entry from the free list */ |
| 1085 | set_first_free(entry->nxt); |
| 1086 | entry->nxt = 0; |
| 1087 | |
| 1088 | /* return the free entry */ |
| 1089 | return entry; |
| 1090 | } |
| 1091 | |
| 1092 | /* |
| 1093 | * Check for free space, and expand the table if necessary. |
| 1094 | */ |
| 1095 | void CVmObjLookupTable::expand_if_needed(VMG0_) |
| 1096 | { |
| 1097 | /* if we have any free entries left, we need do nothing */ |
| 1098 | if (get_first_free() != 0) |
| 1099 | return; |
| 1100 | |
| 1101 | /* increase the entry count by 50%, or 16 slots, whichever is more */ |
| 1102 | uint new_entry_cnt = get_entry_count() + (get_entry_count() >> 1); |
| 1103 | if (new_entry_cnt < get_entry_count() + 16) |
| 1104 | new_entry_cnt = get_entry_count() + 16; |
| 1105 | |
| 1106 | /* reallocate the extension at the new size */ |
| 1107 | ext_ = (char *)vm_lookup_ext::expand_ext( |
| 1108 | vmg_ this, get_ext(), new_entry_cnt); |
| 1109 | } |
| 1110 | |
| 1111 | /* ------------------------------------------------------------------------ */ |
| 1112 | /* |
| 1113 | * Get a value by index |
| 1114 | */ |
| 1115 | int CVmObjLookupTable::index_val_q(VMG_ vm_val_t *result, |
| 1116 | vm_obj_id_t self, |
| 1117 | const vm_val_t *index_val) |
| 1118 | { |
| 1119 | vm_lookup_val *entry; |
| 1120 | |
| 1121 | /* find the entry */ |
| 1122 | entry = find_entry(vmg_ index_val, 0, 0); |
| 1123 | |
| 1124 | /* if we found it, return it; otherwise, return nil */ |
| 1125 | if (entry != 0) |
| 1126 | { |
| 1127 | /* get the value from the entry and hand it back as the result */ |
| 1128 | *result = entry->val; |
| 1129 | } |
| 1130 | else |
| 1131 | { |
| 1132 | /* not found - return the default value */ |
| 1133 | *result = get_ext()->default_value; |
| 1134 | } |
| 1135 | |
| 1136 | /* handled */ |
| 1137 | return TRUE; |
| 1138 | } |
| 1139 | |
| 1140 | /* |
| 1141 | * Get a value by index, with an indication of whether or not the value |
| 1142 | * exists. |
| 1143 | */ |
| 1144 | int CVmObjLookupTable::index_check(VMG_ vm_val_t *result, const vm_val_t *idx) |
| 1145 | { |
| 1146 | /* find the entry */ |
| 1147 | vm_lookup_val *entry = find_entry(vmg_ idx, 0, 0); |
| 1148 | |
| 1149 | /* if we found it, return it; otherwise, return nil */ |
| 1150 | if (entry != 0) |
| 1151 | { |
| 1152 | /* get the indexed value */ |
| 1153 | *result = entry->val; |
| 1154 | |
| 1155 | /* indicate that the key is present is in the table */ |
| 1156 | return TRUE; |
| 1157 | } |
| 1158 | else |
| 1159 | { |
| 1160 | /* |
| 1161 | * not found - set a nil result value and indicate that the key |
| 1162 | * isn't in the table |
| 1163 | */ |
| 1164 | result->set_nil(); |
| 1165 | return FALSE; |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | |
| 1170 | /* |
| 1171 | * Set or add an entry, with no undo |
| 1172 | */ |
| 1173 | void CVmObjLookupTable::set_or_add_entry(VMG_ const vm_val_t *key, |
| 1174 | const vm_val_t *val) |
| 1175 | { |
| 1176 | vm_lookup_val *entry; |
| 1177 | |
| 1178 | /* look for an existing entry with this key */ |
| 1179 | entry = find_entry(vmg_ key, 0, 0); |
| 1180 | |
| 1181 | /* if we found an existing entry, modify it; otherwise, add a new one */ |
| 1182 | if (entry != 0) |
| 1183 | { |
| 1184 | /* set the new value for this entry */ |
| 1185 | entry->val = *val; |
| 1186 | } |
| 1187 | else |
| 1188 | { |
| 1189 | /* add a new entry with the given key */ |
| 1190 | add_entry(vmg_ key, val); |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | /* |
| 1195 | * Set a value by index |
| 1196 | */ |
| 1197 | int CVmObjLookupTable::set_index_val_q(VMG_ vm_val_t *new_container, |
| 1198 | vm_obj_id_t self, |
| 1199 | const vm_val_t *index_val, |
| 1200 | const vm_val_t *new_val) |
| 1201 | { |
| 1202 | vm_lookup_val *entry; |
| 1203 | |
| 1204 | /* look for an existing entry with this key */ |
| 1205 | entry = find_entry(vmg_ index_val, 0, 0); |
| 1206 | |
| 1207 | /* if we found an existing entry, modify it; otherwise, add a new one */ |
| 1208 | if (entry != 0) |
| 1209 | { |
| 1210 | /* set the new value for this entry */ |
| 1211 | set_entry_val_undo(vmg_ self, entry, new_val); |
| 1212 | } |
| 1213 | else |
| 1214 | { |
| 1215 | /* add a new entry with the given key */ |
| 1216 | add_entry_undo(vmg_ self, index_val, new_val); |
| 1217 | } |
| 1218 | |
| 1219 | /* we can be modified, hence the new container is the original one */ |
| 1220 | new_container->set_obj(self); |
| 1221 | |
| 1222 | /* handled */ |
| 1223 | return TRUE; |
| 1224 | } |
| 1225 | |
| 1226 | |
| 1227 | /* ------------------------------------------------------------------------ */ |
| 1228 | /* |
| 1229 | * Property evaluator - determine if the given key is in the table |
| 1230 | */ |
| 1231 | int CVmObjLookupTable::getp_key_present(VMG_ vm_obj_id_t self, |
| 1232 | vm_val_t *retval, uint *argc) |
| 1233 | { |
| 1234 | vm_lookup_val *entry; |
| 1235 | vm_val_t key; |
| 1236 | static CVmNativeCodeDesc desc(1); |
| 1237 | |
| 1238 | /* check arguments */ |
| 1239 | if (get_prop_check_argc(retval, argc, &desc)) |
| 1240 | return TRUE; |
| 1241 | |
| 1242 | /* get the key */ |
| 1243 | G_stk->pop(&key); |
| 1244 | |
| 1245 | /* find the entry for this key */ |
| 1246 | entry = find_entry(vmg_ &key, 0, 0); |
| 1247 | |
| 1248 | /* return true if we found it, nil if not */ |
| 1249 | retval->set_logical(entry != 0); |
| 1250 | |
| 1251 | /* handled */ |
| 1252 | return TRUE; |
| 1253 | } |
| 1254 | |
| 1255 | /* |
| 1256 | * Property evaluator - remove an entry given a key |
| 1257 | */ |
| 1258 | int CVmObjLookupTable::getp_remove_entry(VMG_ vm_obj_id_t self, |
| 1259 | vm_val_t *retval, uint *argc) |
| 1260 | { |
| 1261 | vm_lookup_val *entry; |
| 1262 | vm_val_t key; |
| 1263 | uint hashval; |
| 1264 | vm_lookup_val *prv_entry; |
| 1265 | static CVmNativeCodeDesc desc(1); |
| 1266 | |
| 1267 | /* check arguments */ |
| 1268 | if (get_prop_check_argc(retval, argc, &desc)) |
| 1269 | return TRUE; |
| 1270 | |
| 1271 | /* get the key */ |
| 1272 | G_stk->pop(&key); |
| 1273 | |
| 1274 | /* find the entry for this key */ |
| 1275 | entry = find_entry(vmg_ &key, &hashval, &prv_entry); |
| 1276 | |
| 1277 | /* if we found the entry, delete it */ |
| 1278 | if (entry != 0) |
| 1279 | { |
| 1280 | /* the return value is the value stored at this key */ |
| 1281 | *retval = entry->val; |
| 1282 | |
| 1283 | /* generate undo for the change */ |
| 1284 | add_undo_rec(vmg_ self, LOOKUPTAB_UNDO_DEL, &key, retval); |
| 1285 | |
| 1286 | /* unlink the entry */ |
| 1287 | unlink_entry(vmg_ entry, hashval, prv_entry); |
| 1288 | } |
| 1289 | else |
| 1290 | { |
| 1291 | /* not found - the return value is nil */ |
| 1292 | retval->set_nil(); |
| 1293 | } |
| 1294 | |
| 1295 | /* handled */ |
| 1296 | return TRUE; |
| 1297 | } |
| 1298 | |
| 1299 | /* |
| 1300 | * Property evaluator - apply a callback to each entry in the table |
| 1301 | */ |
| 1302 | int CVmObjLookupTable::getp_apply_all(VMG_ vm_obj_id_t self, |
| 1303 | vm_val_t *retval, uint *argc) |
| 1304 | { |
| 1305 | vm_val_t *fn; |
| 1306 | vm_lookup_val *entry; |
| 1307 | static CVmNativeCodeDesc desc(1); |
| 1308 | uint i; |
| 1309 | vm_rcdesc rc(vmg_ "LookupTable.applyAll", self, 3, G_stk->get(0), argc); |
| 1310 | |
| 1311 | /* check arguments */ |
| 1312 | if (get_prop_check_argc(retval, argc, &desc)) |
| 1313 | return TRUE; |
| 1314 | |
| 1315 | /* the function to invoke is the top argument */ |
| 1316 | fn = G_stk->get(0); |
| 1317 | |
| 1318 | /* push a self-reference for gc protection */ |
| 1319 | G_stk->push()->set_obj(self); |
| 1320 | |
| 1321 | /* iterate over each entry */ |
| 1322 | for (i = get_entry_count(), entry = get_ext()->idx_to_val(0) ; |
| 1323 | i != 0 ; ++entry, --i) |
| 1324 | { |
| 1325 | /* |
| 1326 | * if the entry is in use (it is if the key is non-empty), invoke |
| 1327 | * the callback on it |
| 1328 | */ |
| 1329 | if (entry->key.typ != VM_EMPTY) |
| 1330 | { |
| 1331 | /* push the arguments - (key, val) - push in reverse order */ |
| 1332 | G_stk->push(&entry->val); |
| 1333 | G_stk->push(&entry->key); |
| 1334 | |
| 1335 | /* invoke the callback */ |
| 1336 | G_interpreter->call_func_ptr(vmg_ fn, 2, &rc, 0); |
| 1337 | |
| 1338 | /* replace this element with the result */ |
| 1339 | set_entry_val_undo(vmg_ self, entry, G_interpreter->get_r0()); |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | /* discard the arguments and gc protection */ |
| 1344 | G_stk->discard(2); |
| 1345 | |
| 1346 | /* no return value */ |
| 1347 | retval->set_nil(); |
| 1348 | |
| 1349 | /* handled */ |
| 1350 | return TRUE; |
| 1351 | } |
| 1352 | |
| 1353 | /* |
| 1354 | * Property evaluator - invoke a callback for each entry in the table |
| 1355 | */ |
| 1356 | int CVmObjLookupTable::getp_for_each(VMG_ vm_obj_id_t self, |
| 1357 | vm_val_t *retval, uint *argc) |
| 1358 | { |
| 1359 | /* call the general processor */ |
| 1360 | vm_rcdesc rc(vmg_ "LookupTable.forEach", self, 4, G_stk->get(0), argc); |
| 1361 | return for_each_gen(vmg_ self, retval, argc, FALSE, &rc); |
| 1362 | } |
| 1363 | |
| 1364 | /* |
| 1365 | * Property evaluator - invoke a callback for each entry in the table |
| 1366 | */ |
| 1367 | int CVmObjLookupTable::getp_for_each_assoc(VMG_ vm_obj_id_t self, |
| 1368 | vm_val_t *retval, uint *argc) |
| 1369 | { |
| 1370 | /* call the general processor */ |
| 1371 | vm_rcdesc rc(vmg_ "List.forEachAssoc", self, 7, G_stk->get(0), argc); |
| 1372 | return for_each_gen(vmg_ self, retval, argc, TRUE, &rc); |
| 1373 | } |
| 1374 | |
| 1375 | /* |
| 1376 | * general processor for forEach/forEachAssoc |
| 1377 | */ |
| 1378 | int CVmObjLookupTable::for_each_gen(VMG_ vm_obj_id_t self, |
| 1379 | vm_val_t *retval, uint *argc, |
| 1380 | int pass_key_to_cb, vm_rcdesc *rc) |
| 1381 | { |
| 1382 | vm_val_t *fn; |
| 1383 | vm_lookup_val *entry; |
| 1384 | static CVmNativeCodeDesc desc(1); |
| 1385 | uint i; |
| 1386 | |
| 1387 | /* check arguments */ |
| 1388 | if (get_prop_check_argc(retval, argc, &desc)) |
| 1389 | return TRUE; |
| 1390 | |
| 1391 | /* the function to invoke is the top argument */ |
| 1392 | fn = G_stk->get(0); |
| 1393 | |
| 1394 | /* push a self-reference for gc protection */ |
| 1395 | G_stk->push()->set_obj(self); |
| 1396 | |
| 1397 | /* iterate over each bucket */ |
| 1398 | for (i = get_entry_count(), entry = get_ext()->idx_to_val(0) ; |
| 1399 | i != 0 ; --i, ++entry) |
| 1400 | { |
| 1401 | /* |
| 1402 | * if it's in use (i.e., if the key is non-empty), invoke the |
| 1403 | * callback for the entry |
| 1404 | */ |
| 1405 | if (entry->key.typ != VM_EMPTY) |
| 1406 | { |
| 1407 | /* push the current value argument */ |
| 1408 | G_stk->push(&entry->val); |
| 1409 | |
| 1410 | /* push the key if desired */ |
| 1411 | if (pass_key_to_cb) |
| 1412 | G_stk->push(&entry->key); |
| 1413 | |
| 1414 | /* invoke the callback */ |
| 1415 | G_interpreter->call_func_ptr(vmg_ fn, pass_key_to_cb ? 2 : 1, |
| 1416 | rc, 0); |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | /* discard the arguments and gc protection */ |
| 1421 | G_stk->discard(2); |
| 1422 | |
| 1423 | /* no return value */ |
| 1424 | retval->set_nil(); |
| 1425 | |
| 1426 | /* handled */ |
| 1427 | return TRUE; |
| 1428 | } |
| 1429 | |
| 1430 | /* |
| 1431 | * iterate over the table's contents through a callback |
| 1432 | */ |
| 1433 | void CVmObjLookupTable::for_each( |
| 1434 | VMG_ void (*cb)(VMG_ const vm_val_t *key, |
| 1435 | const vm_val_t *val, void *ctx), void *ctx) |
| 1436 | { |
| 1437 | vm_lookup_val *entry; |
| 1438 | uint i; |
| 1439 | |
| 1440 | /* iterate over each bucket */ |
| 1441 | for (i = get_entry_count(), entry = get_ext()->idx_to_val(0) ; |
| 1442 | i != 0 ; --i, ++entry) |
| 1443 | { |
| 1444 | /* |
| 1445 | * if it's in use (i.e., if the key is non-empty), invoke the |
| 1446 | * callback for the entry |
| 1447 | */ |
| 1448 | if (entry->key.typ != VM_EMPTY) |
| 1449 | (*cb)(vmg_ &entry->key, &entry->val, ctx); |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | /* |
| 1454 | * Property evaluator - get the number of buckets |
| 1455 | */ |
| 1456 | int CVmObjLookupTable::getp_count_buckets(VMG_ vm_obj_id_t self, |
| 1457 | vm_val_t *retval, uint *argc) |
| 1458 | { |
| 1459 | static CVmNativeCodeDesc desc(0); |
| 1460 | |
| 1461 | /* check arguments */ |
| 1462 | if (get_prop_check_argc(retval, argc, &desc)) |
| 1463 | return TRUE; |
| 1464 | |
| 1465 | /* return the number of buckets */ |
| 1466 | retval->set_int(get_bucket_count()); |
| 1467 | |
| 1468 | /* handled */ |
| 1469 | return TRUE; |
| 1470 | } |
| 1471 | |
| 1472 | /* |
| 1473 | * Property evaluator - get the number of entries in use |
| 1474 | */ |
| 1475 | int CVmObjLookupTable::getp_count_entries(VMG_ vm_obj_id_t self, |
| 1476 | vm_val_t *retval, uint *argc) |
| 1477 | { |
| 1478 | static CVmNativeCodeDesc desc(0); |
| 1479 | uint i; |
| 1480 | int cnt; |
| 1481 | vm_lookup_val *entry; |
| 1482 | |
| 1483 | /* check arguments */ |
| 1484 | if (get_prop_check_argc(retval, argc, &desc)) |
| 1485 | return TRUE; |
| 1486 | |
| 1487 | /* run through the table and count in-use entries */ |
| 1488 | for (cnt = 0, i = get_entry_count(), entry = get_ext()->idx_to_val(0) ; |
| 1489 | i != 0 ; --i, ++entry) |
| 1490 | { |
| 1491 | /* if the entry is not marked as free, count it as used */ |
| 1492 | if (entry->key.typ != VM_EMPTY) |
| 1493 | ++cnt; |
| 1494 | } |
| 1495 | |
| 1496 | /* return the number of in-use entries we counted */ |
| 1497 | retval->set_int(cnt); |
| 1498 | |
| 1499 | /* handled */ |
| 1500 | return TRUE; |
| 1501 | } |
| 1502 | |
| 1503 | |
| 1504 | /* |
| 1505 | * Property evaluator - make a list of all of the keys in the table |
| 1506 | */ |
| 1507 | int CVmObjLookupTable::getp_keys_to_list(VMG_ vm_obj_id_t self, |
| 1508 | vm_val_t *retval, uint *argc) |
| 1509 | { |
| 1510 | /* use the general list maker, listing only the keys */ |
| 1511 | return getp_make_list(vmg_ self, retval, argc, TRUE); |
| 1512 | } |
| 1513 | |
| 1514 | /* |
| 1515 | * Property evaluator - make a list of all of the values in the table |
| 1516 | */ |
| 1517 | int CVmObjLookupTable::getp_vals_to_list(VMG_ vm_obj_id_t self, |
| 1518 | vm_val_t *retval, uint *argc) |
| 1519 | { |
| 1520 | /* use the general list maker, listing only the values */ |
| 1521 | return getp_make_list(vmg_ self, retval, argc, FALSE); |
| 1522 | } |
| 1523 | |
| 1524 | /* |
| 1525 | * General handler for keys_to_list and vals_to_list |
| 1526 | */ |
| 1527 | int CVmObjLookupTable::getp_make_list(VMG_ vm_obj_id_t self, |
| 1528 | vm_val_t *retval, uint *argc, |
| 1529 | int store_keys) |
| 1530 | { |
| 1531 | static CVmNativeCodeDesc desc(0); |
| 1532 | |
| 1533 | /* check arguments */ |
| 1534 | if (get_prop_check_argc(retval, argc, &desc)) |
| 1535 | return TRUE; |
| 1536 | |
| 1537 | /* push self while we're working, for gc protection */ |
| 1538 | G_stk->push()->set_obj(self); |
| 1539 | |
| 1540 | /* make the list */ |
| 1541 | make_list(vmg_ retval, store_keys, 0); |
| 1542 | |
| 1543 | /* discard our gc protection */ |
| 1544 | G_stk->discard(); |
| 1545 | |
| 1546 | /* handled */ |
| 1547 | return TRUE; |
| 1548 | } |
| 1549 | |
| 1550 | /* |
| 1551 | * Low-level list maker. Creates a list of all of the keys or values in |
| 1552 | * the table. |
| 1553 | */ |
| 1554 | void CVmObjLookupTable::make_list(VMG_ vm_val_t *retval, int store_keys, |
| 1555 | int (*filter)(VMG_ const vm_val_t *, |
| 1556 | const vm_val_t *)) |
| 1557 | { |
| 1558 | vm_lookup_val *entry; |
| 1559 | uint i; |
| 1560 | int cnt; |
| 1561 | CVmObjList *lst; |
| 1562 | |
| 1563 | /* run through the table and count in-use entries */ |
| 1564 | for (cnt = 0, i = get_entry_count(), entry = get_ext()->idx_to_val(0) ; |
| 1565 | i != 0 ; ++entry, --i) |
| 1566 | { |
| 1567 | /* skip empties */ |
| 1568 | if (entry->key.typ == VM_EMPTY) |
| 1569 | continue; |
| 1570 | |
| 1571 | /* skip values that the filter function rejects */ |
| 1572 | if (filter != 0 && !filter(vmg_ &entry->key, &entry->val)) |
| 1573 | continue; |
| 1574 | |
| 1575 | /* count it */ |
| 1576 | ++cnt; |
| 1577 | } |
| 1578 | |
| 1579 | /* allocate a list to store the results */ |
| 1580 | retval->set_obj(CVmObjList::create(vmg_ FALSE, cnt)); |
| 1581 | |
| 1582 | /* get the list object */ |
| 1583 | lst = (CVmObjList *)vm_objp(vmg_ retval->val.obj); |
| 1584 | lst->cons_clear(); |
| 1585 | |
| 1586 | /* populate the list */ |
| 1587 | for (cnt = 0, i = get_entry_count(), entry = get_ext()->idx_to_val(0) ; |
| 1588 | i != 0 ; ++entry, --i) |
| 1589 | { |
| 1590 | /* if the entry is not marked as free, count it as used */ |
| 1591 | if (entry->key.typ != VM_EMPTY) |
| 1592 | { |
| 1593 | /* skip values that the filter function rejects */ |
| 1594 | if (filter != 0 && !filter(vmg_ &entry->key, &entry->val)) |
| 1595 | continue; |
| 1596 | |
| 1597 | /* store the key or value, as appropriate */ |
| 1598 | if (store_keys) |
| 1599 | { |
| 1600 | /* store the key */ |
| 1601 | lst->cons_set_element(cnt, &entry->key); |
| 1602 | } |
| 1603 | else |
| 1604 | { |
| 1605 | /* store the value */ |
| 1606 | lst->cons_set_element(cnt, &entry->val); |
| 1607 | } |
| 1608 | |
| 1609 | /* update the destination index */ |
| 1610 | ++cnt; |
| 1611 | } |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | |
| 1616 | /* |
| 1617 | * Property evaluator: get the default value |
| 1618 | */ |
| 1619 | int CVmObjLookupTable::getp_get_def_val(VMG_ vm_obj_id_t self, |
| 1620 | vm_val_t *retval, uint *argc) |
| 1621 | { |
| 1622 | /* check arguments */ |
| 1623 | static CVmNativeCodeDesc desc(0); |
| 1624 | if (get_prop_check_argc(retval, argc, &desc)) |
| 1625 | return TRUE; |
| 1626 | |
| 1627 | /* return the default value */ |
| 1628 | *retval = get_ext()->default_value; |
| 1629 | return TRUE; |
| 1630 | } |
| 1631 | |
| 1632 | /* get the default value - internal API version */ |
| 1633 | void CVmObjLookupTable::get_default_val(vm_val_t *val) |
| 1634 | { |
| 1635 | *val = get_ext()->default_value; |
| 1636 | } |
| 1637 | |
| 1638 | /* |
| 1639 | * Property evaluator: set the default value |
| 1640 | */ |
| 1641 | int CVmObjLookupTable::getp_set_def_val(VMG_ vm_obj_id_t self, |
| 1642 | vm_val_t *retval, uint *argc) |
| 1643 | { |
| 1644 | /* check arguments */ |
| 1645 | static CVmNativeCodeDesc desc(1); |
| 1646 | if (get_prop_check_argc(retval, argc, &desc)) |
| 1647 | return TRUE; |
| 1648 | |
| 1649 | /* save undo */ |
| 1650 | add_undo_rec(vmg_ self, LOOKUPTAB_UNDO_DEFVAL, |
| 1651 | 0, &get_ext()->default_value); |
| 1652 | |
| 1653 | /* set the default value to the argument value */ |
| 1654 | G_stk->pop(&get_ext()->default_value); |
| 1655 | |
| 1656 | /* return self */ |
| 1657 | retval->set_obj(self); |
| 1658 | return TRUE; |
| 1659 | } |
| 1660 | |
| 1661 | /* |
| 1662 | * Property evaluator: get the nth key |
| 1663 | */ |
| 1664 | int CVmObjLookupTable::getp_nthKey(VMG_ vm_obj_id_t self, |
| 1665 | vm_val_t *retval, uint *argc) |
| 1666 | { |
| 1667 | /* check arguments */ |
| 1668 | static CVmNativeCodeDesc desc(1); |
| 1669 | if (get_prop_check_argc(retval, argc, &desc)) |
| 1670 | return TRUE; |
| 1671 | |
| 1672 | /* get the nth key value */ |
| 1673 | get_nth_ele(vmg_ retval, 0, self, CVmBif::pop_long_val(vmg0_)); |
| 1674 | |
| 1675 | /* handled */ |
| 1676 | return TRUE; |
| 1677 | } |
| 1678 | |
| 1679 | /* |
| 1680 | * Property evaluator: get the nth value |
| 1681 | */ |
| 1682 | int CVmObjLookupTable::getp_nthVal(VMG_ vm_obj_id_t self, |
| 1683 | vm_val_t *retval, uint *argc) |
| 1684 | { |
| 1685 | /* check arguments */ |
| 1686 | static CVmNativeCodeDesc desc(1); |
| 1687 | if (get_prop_check_argc(retval, argc, &desc)) |
| 1688 | return TRUE; |
| 1689 | |
| 1690 | /* get the nth value */ |
| 1691 | get_nth_ele(vmg_ 0, retval, self, CVmBif::pop_long_val(vmg0_)); |
| 1692 | |
| 1693 | /* handled */ |
| 1694 | return TRUE; |
| 1695 | } |
| 1696 | |
| 1697 | /* |
| 1698 | * Get the nth element's key and/or value |
| 1699 | */ |
| 1700 | void CVmObjLookupTable::get_nth_ele(VMG_ vm_val_t *key, vm_val_t *val, |
| 1701 | vm_obj_id_t self, long idx) |
| 1702 | { |
| 1703 | uint i; |
| 1704 | vm_lookup_val *entry; |
| 1705 | |
| 1706 | /* |
| 1707 | * if the index is zero, and we're asking for the value, return the |
| 1708 | * default value |
| 1709 | */ |
| 1710 | if (idx == 0) |
| 1711 | { |
| 1712 | if (key != 0) |
| 1713 | key->set_nil(); |
| 1714 | if (val != 0) |
| 1715 | *val = get_ext()->default_value; |
| 1716 | return; |
| 1717 | } |
| 1718 | |
| 1719 | /* iterate over our buckets */ |
| 1720 | for (i = get_entry_count(), entry = get_ext()->idx_to_val(0) ; |
| 1721 | i != 0 ; --i, ++entry) |
| 1722 | { |
| 1723 | /* if it's in use (i.e., the key isn't empty), count it */ |
| 1724 | if (entry->key.typ != VM_EMPTY) |
| 1725 | { |
| 1726 | /* count it */ |
| 1727 | --idx; |
| 1728 | |
| 1729 | /* if this is the one we're looking for, return it */ |
| 1730 | if (idx == 0) |
| 1731 | { |
| 1732 | /* fill in the key and/or value elements */ |
| 1733 | if (key != 0) |
| 1734 | *key = entry->key; |
| 1735 | if (val != 0) |
| 1736 | *val = entry->val; |
| 1737 | |
| 1738 | /* done */ |
| 1739 | return; |
| 1740 | } |
| 1741 | } |
| 1742 | } |
| 1743 | |
| 1744 | /* we didn't find it - the index is out of range */ |
| 1745 | err_throw(VMERR_INDEX_OUT_OF_RANGE); |
| 1746 | } |
| 1747 | |
| 1748 | /* ------------------------------------------------------------------------ */ |
| 1749 | /* |
| 1750 | * WeakRefLookupTable |
| 1751 | */ |
| 1752 | |
| 1753 | |
| 1754 | /* |
| 1755 | * statics |
| 1756 | */ |
| 1757 | |
| 1758 | /* metaclass registration object */ |
| 1759 | static CVmMetaclassWeakRefLookupTable weak_metaclass_reg_obj; |
| 1760 | CVmMetaclass *CVmObjWeakRefLookupTable::metaclass_reg_ = |
| 1761 | &weak_metaclass_reg_obj; |
| 1762 | |
| 1763 | /* |
| 1764 | * create |
| 1765 | */ |
| 1766 | vm_obj_id_t CVmObjWeakRefLookupTable:: |
| 1767 | create(VMG_ int in_root_set, uint bucket_count, uint init_capacity) |
| 1768 | { |
| 1769 | vm_obj_id_t id; |
| 1770 | |
| 1771 | /* allocate the object ID */ |
| 1772 | id = vm_new_id(vmg_ in_root_set, TRUE, TRUE); |
| 1773 | |
| 1774 | /* create the object */ |
| 1775 | new (vmg_ id) CVmObjWeakRefLookupTable(vmg_ bucket_count, init_capacity); |
| 1776 | |
| 1777 | /* return the new ID */ |
| 1778 | return id; |
| 1779 | } |
| 1780 | |
| 1781 | /* |
| 1782 | * create dynamically using stack arguments |
| 1783 | */ |
| 1784 | vm_obj_id_t CVmObjWeakRefLookupTable::create_from_stack( |
| 1785 | VMG_ const uchar **pc_ptr, uint argc) |
| 1786 | { |
| 1787 | vm_obj_id_t id; |
| 1788 | size_t bucket_count; |
| 1789 | size_t init_capacity; |
| 1790 | vm_val_t src_obj; |
| 1791 | |
| 1792 | /* parse the arguments */ |
| 1793 | get_constructor_args(vmg_ argc, &bucket_count, &init_capacity, &src_obj); |
| 1794 | |
| 1795 | /* |
| 1796 | * allocate the object ID - this type of construction never creates a |
| 1797 | * root object |
| 1798 | */ |
| 1799 | id = vm_new_id(vmg_ FALSE, TRUE, TRUE); |
| 1800 | |
| 1801 | /* create the object */ |
| 1802 | new (vmg_ id) CVmObjWeakRefLookupTable(vmg_ bucket_count, init_capacity); |
| 1803 | |
| 1804 | /* populate it with the source list if desired */ |
| 1805 | ((CVmObjLookupTable *)vm_objp(vmg_ id))-> |
| 1806 | populate_from_list(vmg_ &src_obj); |
| 1807 | |
| 1808 | /* discard the source object gc protection */ |
| 1809 | G_stk->discard(); |
| 1810 | |
| 1811 | /* return the new ID */ |
| 1812 | return id; |
| 1813 | } |
| 1814 | |
| 1815 | |
| 1816 | /* |
| 1817 | * Mark undo references. Keys are strongly referenced, so mark the key in |
| 1818 | * the record, if present. |
| 1819 | */ |
| 1820 | void CVmObjWeakRefLookupTable:: |
| 1821 | mark_undo_ref(VMG_ struct CVmUndoRecord *undo_rec) |
| 1822 | { |
| 1823 | lookuptab_undo_rec *rec; |
| 1824 | |
| 1825 | /* get our private record from the standard undo record */ |
| 1826 | rec = (lookuptab_undo_rec *)undo_rec->id.ptrval; |
| 1827 | |
| 1828 | /* if the key in the record is an object, mark it referenced */ |
| 1829 | if (rec->key.typ == VM_OBJ) |
| 1830 | G_obj_table->mark_all_refs(rec->key.val.obj, VMOBJ_REACHABLE); |
| 1831 | } |
| 1832 | |
| 1833 | /* |
| 1834 | * Mark references. Keys (but not values) are strongly referenced. |
| 1835 | */ |
| 1836 | void CVmObjWeakRefLookupTable::mark_refs(VMG_ uint state) |
| 1837 | { |
| 1838 | vm_lookup_val **bp; |
| 1839 | uint i; |
| 1840 | |
| 1841 | /* note that the default value is weakly referenced, so don't mark it */ |
| 1842 | |
| 1843 | /* run through my buckets */ |
| 1844 | for (i = get_ext()->bucket_cnt, bp = get_ext()->buckets ; |
| 1845 | i != 0 ; --i, ++bp) |
| 1846 | { |
| 1847 | const vm_lookup_val *entry; |
| 1848 | |
| 1849 | /* run through all entries attached to this bucket */ |
| 1850 | for (entry = *bp ; entry != 0 ; entry = entry->nxt) |
| 1851 | { |
| 1852 | /* if the key is an object, mark it as referenced */ |
| 1853 | if (entry->key.typ == VM_OBJ) |
| 1854 | G_obj_table->mark_all_refs(entry->key.val.obj, state); |
| 1855 | |
| 1856 | /* |
| 1857 | * note that we only reference our value weakly, so do not |
| 1858 | * mark it here |
| 1859 | */ |
| 1860 | } |
| 1861 | } |
| 1862 | } |
| 1863 | |
| 1864 | /* |
| 1865 | * Remove stale weak references from an undo record. Value entries are |
| 1866 | * weakly referenced. |
| 1867 | */ |
| 1868 | void CVmObjWeakRefLookupTable:: |
| 1869 | remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *undo_rec) |
| 1870 | { |
| 1871 | int forget_record; |
| 1872 | |
| 1873 | /* presume we won't want to forget the record */ |
| 1874 | forget_record = FALSE; |
| 1875 | |
| 1876 | /* |
| 1877 | * if the old value in the undo record refers to an object, forget |
| 1878 | * about it |
| 1879 | */ |
| 1880 | if (G_obj_table->is_obj_deletable(&undo_rec->oldval)) |
| 1881 | { |
| 1882 | /* |
| 1883 | * the object is being deleted - since we only weakly reference |
| 1884 | * our objects, we must forget about this object now; this also |
| 1885 | * means we can forget about the record entirely, since there is |
| 1886 | * no need to apply it in the future now that it doesn't do |
| 1887 | * anything |
| 1888 | */ |
| 1889 | undo_rec->oldval.set_nil(); |
| 1890 | forget_record = TRUE; |
| 1891 | } |
| 1892 | |
| 1893 | /* delete our extended record if we're forgetting the record */ |
| 1894 | if (undo_rec->id.ptrval != 0 && forget_record) |
| 1895 | { |
| 1896 | lookuptab_undo_rec *rec; |
| 1897 | |
| 1898 | /* get our private record from the standard undo record */ |
| 1899 | rec = (lookuptab_undo_rec *)undo_rec->id.ptrval; |
| 1900 | |
| 1901 | /* we don't care about the key value in this record any more */ |
| 1902 | rec->key.set_nil(); |
| 1903 | |
| 1904 | /* mark the record as forgotten */ |
| 1905 | rec->action = LOOKUPTAB_UNDO_NULL; |
| 1906 | } |
| 1907 | } |
| 1908 | |
| 1909 | /* |
| 1910 | * Remove stale weak references. Our values are weak references, so |
| 1911 | * remove any entries that have values that are about to be deleted. |
| 1912 | */ |
| 1913 | void CVmObjWeakRefLookupTable::remove_stale_weak_refs(VMG0_) |
| 1914 | { |
| 1915 | vm_lookup_val **bucket; |
| 1916 | uint i; |
| 1917 | uint hashval; |
| 1918 | |
| 1919 | /* remove the default value if it's gone stale */ |
| 1920 | if (G_obj_table->is_obj_deletable(&get_ext()->default_value)) |
| 1921 | get_ext()->default_value.set_nil(); |
| 1922 | |
| 1923 | /* run through each bucket */ |
| 1924 | for (hashval = 0, bucket = get_ext()->buckets, i = get_bucket_count() ; |
| 1925 | i != 0 ; ++bucket, --i, ++hashval) |
| 1926 | { |
| 1927 | vm_lookup_val *entry; |
| 1928 | vm_lookup_val *prv; |
| 1929 | vm_lookup_val *nxt; |
| 1930 | |
| 1931 | /* run through all entries attached to this bucket */ |
| 1932 | for (prv = 0, entry = *bucket ; entry != 0 ; entry = nxt) |
| 1933 | { |
| 1934 | /* remember the next entry, in case we delete this one */ |
| 1935 | nxt = entry->nxt; |
| 1936 | |
| 1937 | /* |
| 1938 | * if the value has an object reference that has become stale, |
| 1939 | * delete the entry |
| 1940 | */ |
| 1941 | if (G_obj_table->is_obj_deletable(&entry->val)) |
| 1942 | { |
| 1943 | /* it's deletable, so delete the entire record */ |
| 1944 | unlink_entry(vmg_ entry, hashval, prv); |
| 1945 | |
| 1946 | /* |
| 1947 | * note that we do NOT advance the prv pointer - we've |
| 1948 | * deleted this entry, so the one preceding it is now the |
| 1949 | * previous for the one following this one |
| 1950 | */ |
| 1951 | } |
| 1952 | else |
| 1953 | { |
| 1954 | /* we're keeping this entry - make it the new previous */ |
| 1955 | prv = entry; |
| 1956 | } |
| 1957 | } |
| 1958 | } |
| 1959 | } |
| 1960 | |
| 1961 | |
| 1962 | /* ------------------------------------------------------------------------ */ |
| 1963 | /* |
| 1964 | * LookupTable Iterator metaclass |
| 1965 | */ |
| 1966 | |
| 1967 | /* |
| 1968 | * statics |
| 1969 | */ |
| 1970 | |
| 1971 | /* metaclass registration object */ |
| 1972 | static CVmMetaclassIterLookupTable iter_metaclass_reg_obj; |
| 1973 | CVmMetaclass *CVmObjIterLookupTable::metaclass_reg_ = &iter_metaclass_reg_obj; |
| 1974 | |
| 1975 | |
| 1976 | /* create a list with no initial contents */ |
| 1977 | vm_obj_id_t CVmObjIterLookupTable::create_for_coll(VMG_ const vm_val_t *coll) |
| 1978 | { |
| 1979 | vm_obj_id_t id; |
| 1980 | |
| 1981 | /* push the collection object reference for gc protection */ |
| 1982 | G_stk->push(coll); |
| 1983 | |
| 1984 | /* create a non-root-set object */ |
| 1985 | id = vm_new_id(vmg_ FALSE, TRUE, TRUE); |
| 1986 | |
| 1987 | /* instantiate the iterator */ |
| 1988 | new (vmg_ id) CVmObjIterLookupTable(vmg_ coll); |
| 1989 | |
| 1990 | /* done with the gc protection */ |
| 1991 | G_stk->discard(); |
| 1992 | |
| 1993 | /* return the new object's ID */ |
| 1994 | return id; |
| 1995 | } |
| 1996 | |
| 1997 | /* |
| 1998 | * constructor |
| 1999 | */ |
| 2000 | CVmObjIterLookupTable::CVmObjIterLookupTable(VMG_ const vm_val_t *coll) |
| 2001 | { |
| 2002 | /* allocate space for our extension data */ |
| 2003 | ext_ = (char *)G_mem->get_var_heap() |
| 2004 | ->alloc_mem(VMOBJITERLOOKUPTABLE_EXT_SIZE, this); |
| 2005 | |
| 2006 | /* save the collection value */ |
| 2007 | vmb_put_dh(ext_, coll); |
| 2008 | |
| 2009 | /* clear the flags */ |
| 2010 | set_flags(0); |
| 2011 | |
| 2012 | /* |
| 2013 | * set up at index zero - we're not at a valid entry until initialized |
| 2014 | * with getNext |
| 2015 | */ |
| 2016 | set_entry_index(0); |
| 2017 | } |
| 2018 | |
| 2019 | /* |
| 2020 | * Find the first valid entry, starting at the given entry index. If the |
| 2021 | * given index refers to a valid entry, we return it. If there is no |
| 2022 | * valid next entry, we return zero. |
| 2023 | */ |
| 2024 | uint CVmObjIterLookupTable::find_first_valid_entry(VMG_ uint entry) const |
| 2025 | { |
| 2026 | CVmObjLookupTable *ltab; |
| 2027 | vm_val_t coll; |
| 2028 | |
| 2029 | /* get my lookup table object */ |
| 2030 | get_coll_val(&coll); |
| 2031 | ltab = (CVmObjLookupTable *)vm_objp(vmg_ coll.val.obj); |
| 2032 | |
| 2033 | /* if we're already past the last item, we have nothing to find */ |
| 2034 | if (entry > ltab->get_entry_count()) |
| 2035 | return 0; |
| 2036 | |
| 2037 | /* |
| 2038 | * scan entries until we find one with a non-empty key (an empty key |
| 2039 | * value indicates that the entry is free) |
| 2040 | */ |
| 2041 | for ( ; entry <= ltab->get_entry_count() ; ++entry) |
| 2042 | { |
| 2043 | vm_lookup_val *entryp; |
| 2044 | |
| 2045 | /* translate the 1-based index to an entry pointer */ |
| 2046 | entryp = ltab->get_ext()->idx_to_val(entry - 1); |
| 2047 | |
| 2048 | /* if it's non-empty, this is the one we want */ |
| 2049 | if (entryp->key.typ != VM_EMPTY) |
| 2050 | return entry; |
| 2051 | } |
| 2052 | |
| 2053 | /* we didn't find a valid entry */ |
| 2054 | return 0; |
| 2055 | } |
| 2056 | |
| 2057 | /* |
| 2058 | * notify of deletion |
| 2059 | */ |
| 2060 | void CVmObjIterLookupTable::notify_delete(VMG_ int) |
| 2061 | { |
| 2062 | /* free our extension */ |
| 2063 | if (ext_ != 0) |
| 2064 | G_mem->get_var_heap()->free_mem(ext_); |
| 2065 | } |
| 2066 | |
| 2067 | /* |
| 2068 | * property evaluator - get the next item |
| 2069 | */ |
| 2070 | int CVmObjIterLookupTable::getp_get_next(VMG_ vm_obj_id_t self, |
| 2071 | vm_val_t *retval, uint *argc) |
| 2072 | { |
| 2073 | uint entry; |
| 2074 | static CVmNativeCodeDesc desc(0); |
| 2075 | CVmObjLookupTable *ltab; |
| 2076 | vm_val_t coll; |
| 2077 | |
| 2078 | /* check arguments */ |
| 2079 | if (get_prop_check_argc(retval, argc, &desc)) |
| 2080 | return TRUE; |
| 2081 | |
| 2082 | /* get my collection object */ |
| 2083 | get_coll_val(&coll); |
| 2084 | ltab = (CVmObjLookupTable *)vm_objp(vmg_ coll.val.obj); |
| 2085 | |
| 2086 | /* advance to the next valid index after the current one */ |
| 2087 | entry = find_first_valid_entry(vmg_ get_entry_index() + 1); |
| 2088 | |
| 2089 | /* |
| 2090 | * if we're past the end of the table, set the index to zero to |
| 2091 | * indicate that we're done |
| 2092 | */ |
| 2093 | if (entry == 0) |
| 2094 | err_throw(VMERR_OUT_OF_RANGE); |
| 2095 | |
| 2096 | /* retrieve the current entry */ |
| 2097 | *retval = ltab->get_ext()->idx_to_val(entry - 1)->val; |
| 2098 | |
| 2099 | /* update our internal state, saving undo */ |
| 2100 | set_entry_index_undo(vmg_ self, entry); |
| 2101 | |
| 2102 | /* handled */ |
| 2103 | return TRUE; |
| 2104 | } |
| 2105 | |
| 2106 | /* |
| 2107 | * property evaluator - get current value |
| 2108 | */ |
| 2109 | int CVmObjIterLookupTable::getp_get_cur_val(VMG_ vm_obj_id_t self, |
| 2110 | vm_val_t *retval, uint *argc) |
| 2111 | { |
| 2112 | static CVmNativeCodeDesc desc(0); |
| 2113 | |
| 2114 | /* check arguments */ |
| 2115 | if (get_prop_check_argc(retval, argc, &desc)) |
| 2116 | return TRUE; |
| 2117 | |
| 2118 | /* get the entry's value */ |
| 2119 | get_cur_entry(vmg_ retval, 0); |
| 2120 | |
| 2121 | /* handled */ |
| 2122 | return TRUE; |
| 2123 | } |
| 2124 | |
| 2125 | /* |
| 2126 | * property evaluator - get current key |
| 2127 | */ |
| 2128 | int CVmObjIterLookupTable::getp_get_cur_key(VMG_ vm_obj_id_t self, |
| 2129 | vm_val_t *retval, uint *argc) |
| 2130 | { |
| 2131 | static CVmNativeCodeDesc desc(0); |
| 2132 | |
| 2133 | /* check arguments */ |
| 2134 | if (get_prop_check_argc(retval, argc, &desc)) |
| 2135 | return TRUE; |
| 2136 | |
| 2137 | /* get the entry's key */ |
| 2138 | get_cur_entry(vmg_ 0, retval); |
| 2139 | |
| 2140 | /* handled */ |
| 2141 | return TRUE; |
| 2142 | } |
| 2143 | |
| 2144 | /* |
| 2145 | * Get the current entry index, value, and key for the current entry. Any |
| 2146 | * of the output pointers can be null if the corresponding value is not |
| 2147 | * needed by the caller. |
| 2148 | */ |
| 2149 | void CVmObjIterLookupTable::get_cur_entry(VMG_ vm_val_t *valp, |
| 2150 | vm_val_t *keyp) const |
| 2151 | { |
| 2152 | uint entry; |
| 2153 | vm_val_t coll; |
| 2154 | CVmObjLookupTable *ltab; |
| 2155 | vm_lookup_val *entryp; |
| 2156 | |
| 2157 | /* get my collection object */ |
| 2158 | get_coll_val(&coll); |
| 2159 | ltab = (CVmObjLookupTable *)vm_objp(vmg_ coll.val.obj); |
| 2160 | |
| 2161 | /* make sure the index is in range */ |
| 2162 | entry = get_entry_index(); |
| 2163 | if (entry < 1 || entry > ltab->get_entry_count()) |
| 2164 | err_throw(VMERR_OUT_OF_RANGE); |
| 2165 | |
| 2166 | /* translate our 1-based index to an entry pointer */ |
| 2167 | entryp = ltab->get_ext()->idx_to_val(entry - 1); |
| 2168 | |
| 2169 | /* if the current entry has been deleted, we have no valid current item */ |
| 2170 | if (entryp->key.typ == VM_EMPTY) |
| 2171 | err_throw(VMERR_OUT_OF_RANGE); |
| 2172 | |
| 2173 | /* retrieve this entry's value and key for the caller, as desired */ |
| 2174 | if (valp != 0) |
| 2175 | *valp = entryp->val; |
| 2176 | if (keyp != 0) |
| 2177 | *keyp = entryp->key; |
| 2178 | } |
| 2179 | |
| 2180 | /* |
| 2181 | * property evaluator - is next value available? |
| 2182 | */ |
| 2183 | int CVmObjIterLookupTable::getp_is_next_avail(VMG_ vm_obj_id_t self, |
| 2184 | vm_val_t *retval, uint *argc) |
| 2185 | { |
| 2186 | uint entry; |
| 2187 | static CVmNativeCodeDesc desc(0); |
| 2188 | |
| 2189 | /* check arguments */ |
| 2190 | if (get_prop_check_argc(retval, argc, &desc)) |
| 2191 | return TRUE; |
| 2192 | |
| 2193 | /* find the index of the next valid entry */ |
| 2194 | entry = find_first_valid_entry(vmg_ get_entry_index() + 1); |
| 2195 | |
| 2196 | /* return true if we have a valid index, false if not */ |
| 2197 | retval->set_logical(entry != 0); |
| 2198 | |
| 2199 | /* handled */ |
| 2200 | return TRUE; |
| 2201 | } |
| 2202 | |
| 2203 | /* |
| 2204 | * property evaluator - reset to first item |
| 2205 | */ |
| 2206 | int CVmObjIterLookupTable::getp_reset_iter(VMG_ vm_obj_id_t self, |
| 2207 | vm_val_t *retval, uint *argc) |
| 2208 | { |
| 2209 | static CVmNativeCodeDesc desc(0); |
| 2210 | |
| 2211 | /* check arguments */ |
| 2212 | if (get_prop_check_argc(retval, argc, &desc)) |
| 2213 | return TRUE; |
| 2214 | |
| 2215 | /* set our entry index back to zero, saving undo */ |
| 2216 | set_entry_index_undo(vmg_ self, 0); |
| 2217 | |
| 2218 | /* no return value */ |
| 2219 | retval->set_nil(); |
| 2220 | |
| 2221 | /* handled */ |
| 2222 | return TRUE; |
| 2223 | } |
| 2224 | |
| 2225 | /* |
| 2226 | * Set the next index value, saving undo if necessary |
| 2227 | */ |
| 2228 | void CVmObjIterLookupTable::set_entry_index_undo( |
| 2229 | VMG_ vm_obj_id_t self, uint entry_idx) |
| 2230 | |
| 2231 | { |
| 2232 | /* save undo if necessary */ |
| 2233 | if (G_undo != 0 && !(get_flags() & VMOBJITERLOOKUPTABLE_UNDO)) |
| 2234 | { |
| 2235 | vm_val_t val; |
| 2236 | |
| 2237 | /* |
| 2238 | * Add the undo record. Store the entry index as the key (it's |
| 2239 | * not really a key, but it'll do as a place to store the data). |
| 2240 | * We don't need anything in the payload, so store a dummy nil |
| 2241 | * value. |
| 2242 | */ |
| 2243 | val.set_nil(); |
| 2244 | G_undo->add_new_record_int_key(vmg_ self, get_entry_index(), &val); |
| 2245 | |
| 2246 | /* |
| 2247 | * set the undo bit so we don't save redundant undo for this |
| 2248 | * savepoint |
| 2249 | */ |
| 2250 | set_flags(get_flags() | VMOBJITERLOOKUPTABLE_UNDO); |
| 2251 | } |
| 2252 | |
| 2253 | /* set the index */ |
| 2254 | set_entry_index(entry_idx); |
| 2255 | } |
| 2256 | |
| 2257 | /* |
| 2258 | * apply undo |
| 2259 | */ |
| 2260 | void CVmObjIterLookupTable::apply_undo(VMG_ CVmUndoRecord *rec) |
| 2261 | { |
| 2262 | /* |
| 2263 | * the integer key in the undo record is my saved index value (and is |
| 2264 | * the only thing in our state that can ever change) |
| 2265 | */ |
| 2266 | set_entry_index((uint)rec->id.intval); |
| 2267 | } |
| 2268 | |
| 2269 | /* |
| 2270 | * mark references |
| 2271 | */ |
| 2272 | void CVmObjIterLookupTable::mark_refs(VMG_ uint state) |
| 2273 | { |
| 2274 | vm_val_t coll; |
| 2275 | |
| 2276 | /* my collection is always an object - mark it as referenced */ |
| 2277 | get_coll_val(&coll); |
| 2278 | G_obj_table->mark_all_refs(coll.val.obj, state); |
| 2279 | } |
| 2280 | |
| 2281 | /* |
| 2282 | * load from an image file |
| 2283 | */ |
| 2284 | void CVmObjIterLookupTable::load_from_image(VMG_ vm_obj_id_t self, |
| 2285 | const char *ptr, size_t siz) |
| 2286 | { |
| 2287 | /* if we already have memory allocated, free it */ |
| 2288 | if (ext_ != 0) |
| 2289 | { |
| 2290 | G_mem->get_var_heap()->free_mem(ext_); |
| 2291 | ext_ = 0; |
| 2292 | } |
| 2293 | |
| 2294 | /* |
| 2295 | * Allocate a new extension. Make sure it's at least as large as the |
| 2296 | * current standard extension size. |
| 2297 | */ |
| 2298 | ext_ = (char *)G_mem->get_var_heap() |
| 2299 | ->alloc_mem(siz < VMOBJITERLOOKUPTABLE_EXT_SIZE |
| 2300 | ? VMOBJITERLOOKUPTABLE_EXT_SIZE : siz, this); |
| 2301 | |
| 2302 | /* copy the image data to our extension */ |
| 2303 | memcpy(ext_, ptr, siz); |
| 2304 | |
| 2305 | /* |
| 2306 | * save our image data pointer in the object table, so that we can |
| 2307 | * access it (without storing it ourselves) during a reload |
| 2308 | */ |
| 2309 | G_obj_table->save_image_pointer(self, ptr, siz); |
| 2310 | |
| 2311 | /* clear the undo flag */ |
| 2312 | set_flags(get_flags() & ~VMOBJITERLOOKUPTABLE_UNDO); |
| 2313 | } |
| 2314 | |
| 2315 | /* |
| 2316 | * re-load from an image file |
| 2317 | */ |
| 2318 | void CVmObjIterLookupTable::reload_from_image(VMG_ vm_obj_id_t, |
| 2319 | const char *ptr, size_t siz) |
| 2320 | { |
| 2321 | /* re-copy the image data */ |
| 2322 | memcpy(ext_, ptr, siz); |
| 2323 | |
| 2324 | /* clear the undo flag */ |
| 2325 | set_flags(get_flags() & ~VMOBJITERLOOKUPTABLE_UNDO); |
| 2326 | } |
| 2327 | |
| 2328 | /* |
| 2329 | * save |
| 2330 | */ |
| 2331 | void CVmObjIterLookupTable::save_to_file(VMG_ CVmFile *fp) |
| 2332 | { |
| 2333 | /* write my extension */ |
| 2334 | fp->write_bytes(ext_, VMOBJITERLOOKUPTABLE_EXT_SIZE); |
| 2335 | } |
| 2336 | |
| 2337 | /* |
| 2338 | * restore |
| 2339 | */ |
| 2340 | void CVmObjIterLookupTable::restore_from_file(VMG_ vm_obj_id_t, |
| 2341 | CVmFile *fp, |
| 2342 | CVmObjFixup *fixups) |
| 2343 | { |
| 2344 | /* free any existing extension */ |
| 2345 | if (ext_ != 0) |
| 2346 | { |
| 2347 | G_mem->get_var_heap()->free_mem(ext_); |
| 2348 | ext_ = 0; |
| 2349 | } |
| 2350 | |
| 2351 | /* allocate a new extension */ |
| 2352 | ext_ = (char *)G_mem->get_var_heap() |
| 2353 | ->alloc_mem(VMOBJITERLOOKUPTABLE_EXT_SIZE, this); |
| 2354 | |
| 2355 | /* read my extension */ |
| 2356 | fp->read_bytes(ext_, VMOBJITERLOOKUPTABLE_EXT_SIZE); |
| 2357 | |
| 2358 | /* fix up my collection object reference */ |
| 2359 | fixups->fix_dh(vmg_ ext_); |
| 2360 | } |
| 2361 |