Debian Codegraph @

terps/tads/tads3/vmlookup.h

raw symbols SCIP
1 /* $Header$ */
2
3 /*
4 * Copyright (c) 2001, 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 vmlookup.h - LookupTable metaclass
12 Function
13
14 Notes
15
16 Modified
17 02/06/01 MJRoberts - Creation
18 */
19
20 #ifndef VMLOOKUP_H
21 #define VMLOOKUP_H
22
23 #include "t3std.h"
24 #include "vmtype.h"
25 #include "vmglob.h"
26 #include "vmobj.h"
27 #include "vmundo.h"
28 #include "vmcoll.h"
29 #include "vmiter.h"
30
31
32 /* ------------------------------------------------------------------------ */
33 /*
34 * The image file data block is arranged as follows:
35 *
36 *. UINT2 bucket_count
37 *. UINT2 value_count
38 *. UINT2 first_free_index
39 *. UINT2 bucket_index[1]
40 *. UINT2 bucket_index[2]
41 *. ...
42 *. UINT2 bucket_index[N]
43 *. value[1]
44 *. value[2]
45 *. ...
46 *. value[N]
47 *. --- the following are only used in version 030003 and later ---
48 *. DATAHOLDER default_value
49 *
50 * value_count gives the number of value slots allocated. Free value slots
51 * are kept in a linked list, the head of which is at the 1-based index
52 * given by first_free_index. If first_free_index is zero, there are no
53 * free value slots.
54 *
55 * Each bucket_index[i] is the 1-based index of the first value in the
56 * chain for that hash bucket. If the value in a bucket_index[i] is zero,
57 * there are values for that bucket.
58 *
59 * Each free entry has a VM_EMPTY value stored in its key to indicate that
60 * it's empty.
61 *
62 * Each value[i] looks like this:
63 *
64 *. DATAHOLDER key
65 *. DATAHOLDER value
66 *. UINT2 next_index
67 *
68 * next_index gives the 1-based index of the next value in the chain for
69 * that bucket; a value of zero indicates that this is the last value in
70 * the chain.
71 *
72 * For version 030003 and later, additional data MAY be supplied. This can
73 * be detected by checking the data block size: if more bytes follow the
74 * last value slot, the extended version 030003 elements are present.
75 *
76 * default_value is the default value to return when the table is indexed
77 * by a key that doesn't exist in the table. This is nil by default.
78 */
79
80 /* value entry size */
81 #define VMLOOKUP_VALUE_SIZE (VMB_DATAHOLDER + VMB_DATAHOLDER + VMB_UINT2)
82
83 /* ------------------------------------------------------------------------ */
84 /*
85 * in-memory value entry structure
86 */
87 struct vm_lookup_val
88 {
89 /* the key */
90 vm_val_t key;
91
92 /* the value */
93 vm_val_t val;
94
95 /* next entry in same hash bucket */
96 vm_lookup_val *nxt;
97 };
98
99 /*
100 * Our in-memory extension data structure, which mimics the image file
101 * structure but uses native types.
102 */
103 struct vm_lookup_ext
104 {
105 /* allocate the structure, given the number of buckets and values */
106 static vm_lookup_ext *alloc_ext(VMG_ class CVmObjLookupTable *self,
107 uint bucket_cnt, uint value_cnt);
108
109 /*
110 * Initialize the extension - puts all values into the free list and
111 * clears all buckets. We don't do this automatically as part of
112 * allocation, because some types of allocation set up the buckets and
113 * free list from a known data set and thus are more efficient if they
114 * skip the initialization step.
115 */
116 void init_ext();
117
118 /*
119 * Reallocate the structure with a larger number of values. Copies
120 * all of the data from the original hash table into the new hash
121 * table, and deletes the old structure.
122 */
123 static vm_lookup_ext *expand_ext(VMG_ class CVmObjLookupTable *self,
124 vm_lookup_ext *old_ext,
125 uint new_value_cnt);
126
127 /*
128 * Copy the given extension's data into myself. This can only be used
129 * when we have the same bucket count as the original (the entry count
130 * need not be the same, but it must be large enough to hold all of
131 * the data from the original).
132 *
133 * This loses any data previously in the table.
134 */
135 void copy_ext_from(vm_lookup_ext *old_ext);
136
137 /* allocate a value entry out of my free list */
138 vm_lookup_val *alloc_val_entry()
139 {
140 /* if the free list is empty, return failure */
141 if (first_free == 0)
142 return 0;
143
144 /* take the first item off the free list */
145 vm_lookup_val *entry = first_free;
146
147 /* unlink it from the free list */
148 first_free = first_free->nxt;
149
150 /* return the allocated item */
151 return entry;
152 }
153
154 /*
155 * Add a value into the given hash bucket. The caller is responsible
156 * for ensuring there's enough room.
157 */
158 void add_val(uint hash, const vm_val_t *key, const vm_val_t *val)
159 {
160 /* allocate a new entry */
161 vm_lookup_val *entry = alloc_val_entry();
162
163 /* set it up with the new data */
164 entry->key = *key;
165 entry->val = *val;
166
167 /* link it into the given bucket */
168 entry->nxt = buckets[hash];
169 buckets[hash] = entry;
170 }
171
172 /*
173 * Given a pool index, retrieve a value entry from our pool of value
174 * entries. This has nothing to do with the hash bucket lists or the
175 * free list - this is simply the nth entry in the master pool of all
176 * values.
177 */
178 vm_lookup_val *idx_to_val(uint idx) const
179 {
180 vm_lookup_val *pool;
181
182 /* the pool of values starts immediately after the buckets */
183 pool = (vm_lookup_val *)(void *)&buckets[bucket_cnt];
184
185 /* return the nth element of the pool */
186 return &pool[idx];
187 }
188
189 /* given a value entry, get the pool index */
190 uint val_to_idx(vm_lookup_val *val) const
191 {
192 return (val - idx_to_val(0));
193 }
194
195 /*
196 * Convert an image-file or save-file index to a value pointer. These
197 * are given as 1-based pointers, with the special value zero used to
198 * indicate a null pointer.
199 */
200 vm_lookup_val *img_idx_to_val(uint idx) const
201 {
202 if (idx == 0)
203 return 0;
204 else
205 return idx_to_val(idx - 1);
206 }
207
208 /* convert a value pointer to an image file index */
209 uint val_to_img_idx(vm_lookup_val *val)
210 {
211 /*
212 * use zero for a null pointer; otherwise, use a 1-based index in
213 * our master value pool
214 */
215 if (val == 0)
216 return 0;
217 else
218 return (val - idx_to_val(0)) + 1;
219 }
220
221 /* number of buckets and number of allocated value entries */
222 uint bucket_cnt;
223 uint value_cnt;
224
225 /* pointer to the first free value */
226 vm_lookup_val *first_free;
227
228 /*
229 * default value - this is returned when we index the table by a key
230 * that doesn't exist in the table
231 */
232 vm_val_t default_value;
233
234 /*
235 * buckets (we overallocate the structure to make room): each bucket
236 * points to the first entry in the list of entries at this hash value
237 */
238 vm_lookup_val *buckets[1];
239 };
240
241
242 /* ------------------------------------------------------------------------ */
243 /*
244 * undo action codes
245 */
246 enum lookuptab_undo_action
247 {
248 /*
249 * null record - we use this to mark a record that has become
250 * irrelevant because of a stale weak reference
251 */
252 LOOKUPTAB_UNDO_NULL,
253
254 /* we added this word to the dictionary (undo by deleting it) */
255 LOOKUPTAB_UNDO_ADD,
256
257 /* we deleted this word from the dictionary (undo by adding it back) */
258 LOOKUPTAB_UNDO_DEL,
259
260 /* we modified the value for a given key */
261 LOOKUPTAB_UNDO_MOD,
262
263 /* we changed the default value for the table */
264 LOOKUPTAB_UNDO_DEFVAL
265 };
266
267
268 /* ------------------------------------------------------------------------ */
269 /*
270 * LookupTable metaclass
271 */
272
273 class CVmObjLookupTable: public CVmObjCollection
274 {
275 friend class CVmObjIterLookupTable;
276 friend class CVmMetaclassLookupTable;
277 friend class CVmObjWeakRefLookupTable;
278
279 public:
280 /* metaclass registration object */
281 static class CVmMetaclass *metaclass_reg_;
282 class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; }
283
284 /* am I of the given metaclass? */
285 virtual int is_of_metaclass(class CVmMetaclass *meta) const
286 {
287 /* try my own metaclass and my base class */
288 return (meta == metaclass_reg_
289 || CVmObjCollection::is_of_metaclass(meta));
290 }
291
292 /* is this a lookup table object? */
293 static int is_lookup_table_obj(VMG_ vm_obj_id_t obj)
294 { return vm_objp(vmg_ obj)->is_of_metaclass(metaclass_reg_); }
295
296 /* create */
297 static vm_obj_id_t create(VMG_ int in_root_set,
298 uint bucket_count, uint init_capacity);
299
300 /* create dynamically using stack arguments */
301 static vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr,
302 uint argc);
303
304 /*
305 * call a static property - we don't have any of our own, so simply
306 * "inherit" the base class handling
307 */
308 static int call_stat_prop(VMG_ vm_val_t *result,
309 const uchar **pc_ptr, uint *argc,
310 vm_prop_id_t prop)
311 {
312 return CVmObjCollection::
313 call_stat_prop(vmg_ result, pc_ptr, argc, prop);
314 }
315
316 /* reserve constant data */
317 virtual void reserve_const_data(VMG_ class CVmConstMapper *mapper,
318 vm_obj_id_t self)
319 {
320 /* we cannot be converted to constant data */
321 }
322
323 /* convert to constant data */
324 virtual void convert_to_const_data(VMG_ class CVmConstMapper *mapper,
325 vm_obj_id_t self);
326
327 /* notify of deletion */
328 void notify_delete(VMG_ int in_root_set);
329
330 /* set a property */
331 void set_prop(VMG_ class CVmUndo *undo,
332 vm_obj_id_t self, vm_prop_id_t prop, const vm_val_t *val);
333
334 /* get a property */
335 int get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val,
336 vm_obj_id_t self, vm_obj_id_t *source_obj, uint *argc);
337
338 /*
339 * receive savepoint notification - we don't keep any
340 * savepoint-relative records, so we don't need to do anything here
341 */
342 void notify_new_savept() { }
343
344 /* apply an undo record */
345 void apply_undo(VMG_ struct CVmUndoRecord *rec);
346
347 /* discard an undo record */
348 void discard_undo(VMG_ struct CVmUndoRecord *);
349
350 /* mark undo references */
351 void mark_undo_ref(VMG_ struct CVmUndoRecord *rec);
352
353 /* mark references */
354 void mark_refs(VMG_ uint);
355
356 /* we keep only strong references */
357 void remove_stale_weak_refs(VMG0_) { }
358 void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *) { }
359
360 /* load from an image file */
361 void load_from_image(VMG_ vm_obj_id_t self, const char *ptr, size_t siz);
362
363 /* reload from an image file */
364 void reload_from_image(VMG_ vm_obj_id_t self,
365 const char *ptr, size_t siz);
366
367 /* rebuild for image file */
368 virtual ulong rebuild_image(VMG_ char *buf, ulong buflen);
369
370 /* save to a file */
371 void save_to_file(VMG_ class CVmFile *fp);
372
373 /* restore from a file */
374 void restore_from_file(VMG_ vm_obj_id_t self,
375 class CVmFile *fp, class CVmObjFixup *fixups);
376
377 /*
378 * determine if we've been changed since loading - assume we have (if
379 * we haven't, the only harm is the cost of unnecessarily reloading or
380 * saving)
381 */
382 int is_changed_since_load() const { return TRUE; }
383
384 /* get an entry; returns true if the entry exists, false if not */
385 int index_check(VMG_ vm_val_t *result, const vm_val_t *index_val);
386
387 /* get a value by index */
388 int index_val_q(VMG_ vm_val_t *result,
389 vm_obj_id_t self,
390 const vm_val_t *index_val);
391
392 /* set a value by index */
393 int set_index_val_q(VMG_ vm_val_t *new_container,
394 vm_obj_id_t self,
395 const vm_val_t *index_val,
396 const vm_val_t *new_val);
397
398 /* add an entry - does not generate undo */
399 void add_entry(VMG_ const vm_val_t *key, const vm_val_t *val);
400
401 /* set or add an entry - does not generate undo */
402 void set_or_add_entry(VMG_ const vm_val_t *key, const vm_val_t *val);
403
404 /* make a list of keys/values in the table */
405 void keys_to_list(VMG_ vm_val_t *lst,
406 int (*filter)(VMG_ const vm_val_t *, const vm_val_t *)
407 = 0)
408 { make_list(vmg_ lst, TRUE, filter); }
409 void vals_to_list(VMG_ vm_val_t *lst,
410 int (*filter)(VMG_ const vm_val_t *, const vm_val_t *)
411 = 0)
412 { make_list(vmg_ lst, FALSE, filter); }
413
414 /* iterate over the table's contents through a callback */
415 void for_each(VMG_ void (*cb)(VMG_ const vm_val_t *key,
416 const vm_val_t *val, void *ctx), void *ctx);
417
418 /* get the default value */
419 void get_default_val(vm_val_t *val);
420
421 protected:
422 /* get and range-check the constructor arguments */
423 static void get_constructor_args(VMG_ uint argc,
424 size_t *bucket_count,
425 size_t *init_capacity,
426 vm_val_t *src_obj);
427
428 /* populate the table from a Key->Value list or vector */
429 void populate_from_list(VMG_ const vm_val_t *src_obj);
430
431 /* load or reload image data */
432 void load_image_data(VMG_ const char *ptr, size_t siz);
433
434 /* create a new object as a copy of this object */
435 vm_obj_id_t create_copy(VMG0_);
436
437 /* add an entry, generating undo */
438 void add_entry_undo(VMG_ vm_obj_id_t self,
439 const vm_val_t *key, const vm_val_t *val);
440
441 /* delete an entry - does not generate undo */
442 void del_entry(VMG_ const vm_val_t *key);
443
444 /*
445 * Unlink an entry - does not generate undo. 'prv_entry' is the
446 * previous entry in the hash chain containing this entry, and
447 * 'hashval' is the bucket containing the entry. Pass null for
448 * 'prv_entry' when the entry to unlink is the first entry in its hash
449 * chain.
450 */
451 void unlink_entry(VMG_ vm_lookup_val *entry, uint hashval,
452 vm_lookup_val *prv_entry);
453
454 /*
455 * modify an entry - changes the value associated with the given key;
456 * does not generate undo
457 */
458 void mod_entry(VMG_ const vm_val_t *key, const vm_val_t *val);
459
460 /* find an entry */
461 vm_lookup_val *find_entry(VMG_ const vm_val_t *key,
462 uint *hashval_p, vm_lookup_val **prv_entry_p);
463
464 /*
465 * Check the table to make sure there's enough free space to add one
466 * new item, and expand the table if necessary.
467 */
468 void expand_if_needed(VMG0_);
469
470 /* allocate a new entry, expanding the table if necessary */
471 vm_lookup_val *alloc_new_entry(VMG0_);
472
473 /* calculate a value's hash code */
474 uint calc_key_hash(VMG_ const vm_val_t *key);
475
476 /* get my extension data */
477 vm_lookup_ext *get_ext() const { return (vm_lookup_ext *)ext_; }
478
479 /* get the hash bucket count */
480 uint get_bucket_count() const { return get_ext()->bucket_cnt; }
481
482 /* get the value entry count */
483 uint get_entry_count() const { return get_ext()->value_cnt; }
484
485 /* get/set the first-free item */
486 vm_lookup_val *get_first_free() const { return get_ext()->first_free; }
487 void set_first_free(vm_lookup_val *p) { get_ext()->first_free = p; }
488
489 /* get a bucket's first entry given a hash code */
490 vm_lookup_val **get_bucket(uint hash) const
491 { return &get_ext()->buckets[hash]; }
492
493 /* set a bucket's contents given a hash code */
494 void set_bucket(uint hash, vm_lookup_val *p)
495 { get_ext()->buckets[hash] = p; }
496
497 /* set an entry, keeping undo for the change */
498 void set_entry_val_undo(VMG_ vm_obj_id_t self,
499 vm_lookup_val *entry, const vm_val_t *val);
500
501 /* create an iterator */
502 virtual void new_iterator(VMG_ vm_val_t *retval,
503 const vm_val_t *self_val);
504
505 /* create a live iterator */
506 virtual void new_live_iterator(VMG_ vm_val_t *retval,
507 const vm_val_t *self_val);
508
509 /* create a lookup table with no initial contents */
510 CVmObjLookupTable() { ext_ = 0; }
511
512 /*
513 * Create a lookup table with a given number of hash table buckets,
514 * and the given number of entry slots. The hash table bucket count
515 * is fixed for the life of the object. The entry slot count is
516 * merely advisory: the table size will be increased as necessary to
517 * accommodate new elements.
518 */
519 CVmObjLookupTable(VMG_ size_t hash_count, size_t entry_count);
520
521 /* property evaluator - undefined function */
522 int getp_undef(VMG_ vm_obj_id_t, vm_val_t *, uint *) { return FALSE; }
523
524 /* property evaluator - remove an entry given the key */
525 int getp_remove_entry(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
526
527 /* property evaluator - determine if a given key is in the table */
528 int getp_key_present(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
529
530 /* property evaluator - apply a callback to each element */
531 int getp_apply_all(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
532
533 /* property evaluator - call a callback on each element */
534 int getp_for_each(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
535
536 /* property evaluator - call a callback on each element */
537 int getp_for_each_assoc(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
538
539 /* general forEach/forEachAssoc processor */
540 int for_each_gen(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc,
541 int pass_key_to_cb, struct vm_rcdesc *rc);
542
543 /* get the number of buckets in the table */
544 int getp_count_buckets(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
545
546 /* get the number of entries in the table */
547 int getp_count_entries(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
548
549 /* make a list of all of the keys in the table */
550 int getp_keys_to_list(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
551
552 /* make a list of all of the values in the table */
553 int getp_vals_to_list(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
554
555 /* get the default value */
556 int getp_get_def_val(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
557
558 /* set the default value */
559 int getp_set_def_val(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
560
561 /* general handler for making a list of keys or values */
562 int getp_make_list(VMG_ vm_obj_id_t self,
563 vm_val_t *retval, uint *argc, int store_keys);
564
565 /* get the nth key */
566 int getp_nthKey(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
567
568 /* get the nth value */
569 int getp_nthVal(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc);
570
571 /*
572 * Get the nth element's key and/or value. This only counts in-use
573 * buckets. If idx is zero, we return the default value. If idx is
574 * out of range, we throw an error.
575 */
576 void get_nth_ele(VMG_ vm_val_t *key, vm_val_t *val,
577 vm_obj_id_t self, long idx);
578
579 /* make a list of the keys or values in the table */
580 void make_list(VMG_ vm_val_t *retval, int store_keys,
581 int (*filter)(VMG_ const vm_val_t *, const vm_val_t *));
582
583 /* add a record to the global undo stream */
584 void add_undo_rec(VMG_ vm_obj_id_t self,
585 enum lookuptab_undo_action action,
586 const vm_val_t *key,
587 const vm_val_t *old_entry_val);
588
589 /* property evaluation function table */
590 static int (CVmObjLookupTable::*func_table_[])(VMG_ vm_obj_id_t self,
591 vm_val_t *retval, uint *argc);
592 };
593
594 /* ------------------------------------------------------------------------ */
595 /*
596 * WeakRefLookupTable - a subclass of LookupTable that places weak
597 * references on its values. The keys are still strong references.
598 */
599 class CVmObjWeakRefLookupTable: public CVmObjLookupTable
600 {
601 friend class CVmMetaclassWeakRefLookupTable;
602
603 public:
604 /* metaclass registration object */
605 static class CVmMetaclass *metaclass_reg_;
606 class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; }
607
608 /* am I of the given metaclass? */
609 virtual int is_of_metaclass(class CVmMetaclass *meta) const
610 {
611 /* try my own metaclass and my base class */
612 return (meta == metaclass_reg_
613 || CVmObjLookupTable::is_of_metaclass(meta));
614 }
615
616 /* create */
617 static vm_obj_id_t create(VMG_ int in_root_set,
618 uint bucket_count, uint init_capacity);
619
620 /* create dynamically using stack arguments */
621 static vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr,
622 uint argc);
623
624 /*
625 * call a static property - we don't have any of our own, so simply
626 * "inherit" the base class handling
627 */
628 static int call_stat_prop(VMG_ vm_val_t *result,
629 const uchar **pc_ptr, uint *argc,
630 vm_prop_id_t prop)
631 {
632 return CVmObjLookupTable::
633 call_stat_prop(vmg_ result, pc_ptr, argc, prop);
634 }
635
636 /* mark references */
637 void mark_refs(VMG_ uint);
638
639 /* mark undo references */
640 void mark_undo_ref(VMG_ struct CVmUndoRecord *rec);
641
642 /* remove stale weak references */
643 void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *rec);
644
645 /* remove stale weak references */
646 void remove_stale_weak_refs(VMG0_);
647
648 protected:
649 /* create a lookup table with no initial contents */
650 CVmObjWeakRefLookupTable() { ext_ = 0; }
651
652 /* construct */
653 CVmObjWeakRefLookupTable(VMG_ size_t hash_count, size_t entry_count)
654 : CVmObjLookupTable(vmg_ hash_count, entry_count) { }
655 };
656
657 /* ------------------------------------------------------------------------ */
658 /*
659 * LookupTable Registration table object
660 */
661 class CVmMetaclassLookupTable: public CVmMetaclass
662 {
663 public:
664 /* get the global name */
665 const char *get_meta_name() const { return "lookuptable/030003"; }
666
667 /* create from image file */
668 void create_for_image_load(VMG_ vm_obj_id_t id)
669 {
670 new (vmg_ id) CVmObjLookupTable();
671 G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE);
672 }
673
674 /* create from restoring from saved state */
675 void create_for_restore(VMG_ vm_obj_id_t id)
676 {
677 new (vmg_ id) CVmObjLookupTable();
678 G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE);
679 }
680
681 /* create dynamically using stack arguments */
682 vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc)
683 { return CVmObjLookupTable::create_from_stack(vmg_ pc_ptr, argc); }
684
685 /* call a static property */
686 int call_stat_prop(VMG_ vm_val_t *result,
687 const uchar **pc_ptr, uint *argc,
688 vm_prop_id_t prop)
689 {
690 return CVmObjLookupTable::
691 call_stat_prop(vmg_ result, pc_ptr, argc, prop);
692 }
693
694 /* I'm a Collection object */
695 CVmMetaclass *get_supermeta_reg() const
696 { return CVmObjCollection::metaclass_reg_; }
697 };
698
699 /*
700 * WeakRefLookupTable registration object
701 */
702 class CVmMetaclassWeakRefLookupTable: public CVmMetaclass
703 {
704 public:
705 /* get the global name */
706 const char *get_meta_name() const { return "weakreflookuptable/030001"; }
707
708 /* create from image file */
709 void create_for_image_load(VMG_ vm_obj_id_t id)
710 { new (vmg_ id) CVmObjWeakRefLookupTable(); }
711
712 /* create from restoring from saved state */
713 void create_for_restore(VMG_ vm_obj_id_t id)
714 { new (vmg_ id) CVmObjWeakRefLookupTable(); }
715
716 /* create dynamically using stack arguments */
717 vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc)
718 { return CVmObjWeakRefLookupTable::
719 create_from_stack(vmg_ pc_ptr, argc); }
720
721 /* call a static property */
722 int call_stat_prop(VMG_ vm_val_t *result,
723 const uchar **pc_ptr, uint *argc,
724 vm_prop_id_t prop)
725 {
726 return CVmObjWeakRefLookupTable::
727 call_stat_prop(vmg_ result, pc_ptr, argc, prop);
728 }
729
730 /* I'm a LookupTable object */
731 CVmMetaclass *get_supermeta_reg() const
732 { return CVmObjLookupTable::metaclass_reg_; }
733 };
734
735 /* ------------------------------------------------------------------------ */
736 /*
737 * LookupTable Iterator subclass. This iterator is tightly coupled with
738 * the LookupTable class, since we must look into the internal
739 * implementation of the LookupTable to implement an iterator on it.
740 */
741
742 /*
743 * The extension data for an lookup table iterator consists of a reference
744 * to the associated LookupTable object, the current bucket index, and the
745 * index of the current entry.
746 *
747 * DATAHOLDER lookuptable_value
748 *. UINT2 cur_entry_index (1-based; zero is invalid)
749 *. UINT2 flags
750 *
751 * The flag values are:
752 *
753 * VMOBJITERLOOKUPTABLE_UNDO - we've saved undo for this savepoint. If
754 * this is set, we won't save additional undo for the same savepoint.
755 */
756
757 /* total extension size */
758 #define VMOBJITERLOOKUPTABLE_EXT_SIZE (VMB_DATAHOLDER + 2 + 2)
759
760 /*
761 * flag bits
762 */
763
764 /* we've saved undo for the current savepoint */
765 #define VMOBJITERLOOKUPTABLE_UNDO 0x0001
766
767 /*
768 * LookupTable iterator class
769 */
770 class CVmObjIterLookupTable: public CVmObjIter
771 {
772 friend class CVmMetaclassIterLookupTable;
773
774 public:
775 /* metaclass registration object */
776 static class CVmMetaclass *metaclass_reg_;
777 class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; }
778
779 /* am I of the given metaclass? */
780 virtual int is_of_metaclass(class CVmMetaclass *meta) const
781 {
782 /* try my own metaclass and my base class */
783 return (meta == metaclass_reg_
784 || CVmObjIter::is_of_metaclass(meta));
785 }
786
787 /*
788 * call a static property - we don't have any of our own, so simply
789 * "inherit" the base class handling
790 */
791 static int call_stat_prop(VMG_ vm_val_t *result,
792 const uchar **pc_ptr, uint *argc,
793 vm_prop_id_t prop)
794 {
795 return CVmObjIter::call_stat_prop(vmg_ result, pc_ptr, argc, prop);
796 }
797
798 /*
799 * Create a lookup table iterator. This method is to be called by the
800 * lookup table to create an iterator for its value.
801 */
802 static vm_obj_id_t create_for_coll(VMG_ const vm_val_t *coll);
803
804 /* notify of deletion */
805 void notify_delete(VMG_ int in_root_set);
806
807 /*
808 * notify of a new savepoint - clear the 'undo' flag, since we cannot
809 * have created any undo information yet for the new savepoint
810 */
811 void notify_new_savept()
812 { set_flags(get_flags() & ~VMOBJITERLOOKUPTABLE_UNDO); }
813
814 /* apply undo */
815 void apply_undo(VMG_ struct CVmUndoRecord *rec);
816
817 /* mark references */
818 void mark_refs(VMG_ uint state);
819
820 /* there are no references in our undo stream */
821 void mark_undo_ref(VMG_ struct CVmUndoRecord *) { }
822
823 /* we keep only strong references */
824 void remove_stale_weak_refs(VMG0_) { }
825 void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *) { }
826
827 /* load from an image file */
828 void load_from_image(VMG_ vm_obj_id_t self, const char *ptr, size_t siz);
829
830 /* reload from an image file */
831 void reload_from_image(VMG_ vm_obj_id_t self,
832 const char *ptr, size_t siz);
833
834 /*
835 * determine if the object has been changed since it was loaded -
836 * assume we have
837 */
838 int is_changed_since_load() const { return TRUE; }
839
840 /* save to a file */
841 void save_to_file(VMG_ class CVmFile *fp);
842
843 /* restore from a file */
844 void restore_from_file(VMG_ vm_obj_id_t self,
845 class CVmFile *fp, class CVmObjFixup *fixups);
846
847 /* rebuild for image file */
848 virtual ulong rebuild_image(VMG_ char *buf, ulong buflen);
849
850 /* convert to constant data */
851 virtual void convert_to_const_data(VMG_ class CVmConstMapper *mapper,
852 vm_obj_id_t self);
853
854 protected:
855 /* create */
856 CVmObjIterLookupTable() { ext_ = 0; }
857
858 /* create */
859 CVmObjIterLookupTable(VMG_ const vm_val_t *coll);
860
861 /* find the first valid entry, starting at the given index */
862 uint find_first_valid_entry(VMG_ uint entry) const;
863
864 /* property evaluator - get next value */
865 virtual int getp_get_next(VMG_ vm_obj_id_t self, vm_val_t *retval,
866 uint *argc);
867
868 /* property evaluator - is next value available? */
869 virtual int getp_is_next_avail(VMG_ vm_obj_id_t self, vm_val_t *retval,
870 uint *argc);
871
872 /* property evaluator - reset to first item */
873 virtual int getp_reset_iter(VMG_ vm_obj_id_t self, vm_val_t *retval,
874 uint *argc);
875
876 /* property evaluator - get current key */
877 virtual int getp_get_cur_key(VMG_ vm_obj_id_t self, vm_val_t *retval,
878 uint *argc);
879
880 /* property evaluator - get current value */
881 virtual int getp_get_cur_val(VMG_ vm_obj_id_t self, vm_val_t *retval,
882 uint *argc);
883
884 /* get information on the current entry */
885 void get_cur_entry(VMG_ vm_val_t *valp, vm_val_t *keyp) const;
886
887 /* get my collection value */
888 void get_coll_val(vm_val_t *val) const { vmb_get_dh(ext_, val); }
889
890 /* get/set the current entry index (without saving undo) */
891 long get_entry_index() const
892 { return osrp2(ext_ + VMB_DATAHOLDER); }
893 void set_entry_index(uint idx)
894 { oswp2(ext_ + VMB_DATAHOLDER, idx); }
895
896 /* update the entry index value, saving undo if necessary */
897 void set_entry_index_undo(VMG_ vm_obj_id_t self, uint entry);
898
899 /* get/set the flags */
900 uint get_flags() const
901 { return osrp2(ext_ + VMB_DATAHOLDER + 2); }
902 void set_flags(uint flags) const
903 { oswp2(ext_ + VMB_DATAHOLDER + 2, flags); }
904 };
905
906 /*
907 * Registration table object for lookup table iterators
908 */
909 class CVmMetaclassIterLookupTable: public CVmMetaclass
910 {
911 public:
912 /* get the global name */
913 const char *get_meta_name() const
914 { return "lookuptable-iterator/030000"; }
915
916 /* create from image file */
917 void create_for_image_load(VMG_ vm_obj_id_t id)
918 {
919 new (vmg_ id) CVmObjIterLookupTable();
920 G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE);
921 }
922
923 /* create from restoring from saved state */
924 void create_for_restore(VMG_ vm_obj_id_t id)
925 {
926 new (vmg_ id) CVmObjIterLookupTable();
927 G_obj_table->set_obj_gc_characteristics(id, TRUE, FALSE);
928 }
929
930 /* create dynamically using stack arguments */
931 vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc)
932 {
933 err_throw(VMERR_BAD_DYNAMIC_NEW);
934 AFTER_ERR_THROW(return VM_INVALID_OBJ;)
935 }
936
937 /* call a static property */
938 int call_stat_prop(VMG_ vm_val_t *result,
939 const uchar **pc_ptr, uint *argc,
940 vm_prop_id_t prop)
941 {
942 return CVmObjIterLookupTable::
943 call_stat_prop(vmg_ result, pc_ptr, argc, prop);
944 }
945 };
946
947
948 #endif /* VMLOOKUP_H */
949
950 /*
951 * Register the classes
952 */
953 VM_REGISTER_METACLASS(CVmObjLookupTable)
954 VM_REGISTER_METACLASS(CVmObjWeakRefLookupTable)
955 VM_REGISTER_METACLASS(CVmObjIterLookupTable)
956