| 1 | /* $Header: d:/cvsroot/tads/tads3/VMUNDO.H,v 1.2 1999/05/17 02:52:29 MJRoberts Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1998, 2002 Michael J. Roberts. All Rights Reserved. |
| 5 | * |
| 6 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 7 | * on using and copying this software. |
| 8 | */ |
| 9 | /* |
| 10 | Name |
| 11 | vmundo.h - VM undo manager |
| 12 | Function |
| 13 | |
| 14 | Notes |
| 15 | |
| 16 | Modified |
| 17 | 10/29/98 MJRoberts - Creation |
| 18 | */ |
| 19 | |
| 20 | #ifndef VMUNDO_H |
| 21 | #define VMUNDO_H |
| 22 | |
| 23 | #include "t3std.h" |
| 24 | #include "vmtype.h" |
| 25 | |
| 26 | |
| 27 | /* ------------------------------------------------------------------------ */ |
| 28 | /* |
| 29 | * Undo record. The VM undo manager maintains a list of these records; |
| 30 | * each record contains the information necessary to undo one particular |
| 31 | * data change in an object. Each object class (TADS object, array, |
| 32 | * etc) keeps a linked list of undo records for its own actions. |
| 33 | */ |
| 34 | struct CVmUndoRecord |
| 35 | { |
| 36 | /* |
| 37 | * Object ID. All undoable state is stored in objects, hence each |
| 38 | * undo record is associated with an object. |
| 39 | */ |
| 40 | vm_obj_id_t obj; |
| 41 | |
| 42 | /* |
| 43 | * Identifier - the meaning of this member is defined by the object |
| 44 | * that created the record. For TADS objects, this is a property ID |
| 45 | * specifying the property that was changed; for arrays, it's the |
| 46 | * index of the array element that changed. Other object |
| 47 | * metaclasses may assign different meanings. |
| 48 | */ |
| 49 | union |
| 50 | { |
| 51 | /* property value key */ |
| 52 | vm_prop_id_t prop; |
| 53 | |
| 54 | /* integer value key */ |
| 55 | uint32_t intval; |
| 56 | |
| 57 | /* pointer value key */ |
| 58 | void *ptrval; |
| 59 | } id; |
| 60 | |
| 61 | /* |
| 62 | * Data value. This is the data value prior to the change. In some |
| 63 | * cases, the type may be 'empty' to indicate that the original |
| 64 | * value did not exist; for example, when a property is added to a |
| 65 | * TADS object and the property was not present previously, we |
| 66 | * create an undo record with type 'empty' to indicate that the |
| 67 | * property must be deleted on undo. |
| 68 | */ |
| 69 | vm_val_t oldval; |
| 70 | }; |
| 71 | |
| 72 | |
| 73 | /* |
| 74 | * Undo meta-record. Each slot in the undo array is one of these |
| 75 | * meta-records, which is a union of the normal undo record and an undo |
| 76 | * link pointer. The first undo record in any savepoint is always a |
| 77 | * link pointer; all of the other records are normal undo records. |
| 78 | */ |
| 79 | union CVmUndoMeta |
| 80 | { |
| 81 | /* the first entry in each savepoint is a link entry */ |
| 82 | struct |
| 83 | { |
| 84 | /* pointer to the first record in the previous savepoint */ |
| 85 | CVmUndoMeta *prev_first; |
| 86 | |
| 87 | /* pointer to the first record in the next savepoint */ |
| 88 | CVmUndoMeta *next_first; |
| 89 | } link; |
| 90 | |
| 91 | /* every entry but the first in a savepoint is an ordinary undo record */ |
| 92 | CVmUndoRecord rec; |
| 93 | }; |
| 94 | |
| 95 | |
| 96 | /* ------------------------------------------------------------------------ */ |
| 97 | /* |
| 98 | * Undo manager |
| 99 | */ |
| 100 | class CVmUndo |
| 101 | { |
| 102 | public: |
| 103 | /* |
| 104 | * create the undo manager, specifying the upper limit for memory |
| 105 | * usage and retained savepoints |
| 106 | */ |
| 107 | CVmUndo(size_t undo_record_cnt, uint max_savepts); |
| 108 | |
| 109 | /* delete the undo manager */ |
| 110 | ~CVmUndo(); |
| 111 | |
| 112 | /* |
| 113 | * Create a savepoint. If doing so would exceed the maximum number |
| 114 | * of savepoints, we'll discard the oldest savepoint. |
| 115 | */ |
| 116 | void create_savept(VMG0_); |
| 117 | |
| 118 | /* get the current savepoint ID */ |
| 119 | vm_savept_t get_cur_savept() const { return cur_savept_; } |
| 120 | |
| 121 | /* determine how many savepoints exist */ |
| 122 | uint get_savept_cnt() const { return savept_cnt_; } |
| 123 | |
| 124 | /* drop all undo information */ |
| 125 | void drop_undo(VMG0_); |
| 126 | |
| 127 | /* |
| 128 | * Allocate and initialize an undo record with a property key or |
| 129 | * with an integer key. |
| 130 | * |
| 131 | * We check that the undo manager has an active savepoint. It may |
| 132 | * not have an active savepoint if no savepoint has been created, |
| 133 | * all undo records have been applied, or we've run out of space in |
| 134 | * the current savepoint; in any of these cases, since there's no |
| 135 | * savepoint, we don't need to keep any undo records. |
| 136 | */ |
| 137 | void add_new_record_prop_key(VMG_ vm_obj_id_t obj, vm_prop_id_t key, |
| 138 | const vm_val_t *val); |
| 139 | void add_new_record_int_key(VMG_ vm_obj_id_t obj, uint32_t key, |
| 140 | const vm_val_t *val); |
| 141 | |
| 142 | /* |
| 143 | * Add a new undo record with a pointer key; returns true if the |
| 144 | * record was created, false if not. (This version of |
| 145 | * add_new_record_xxx_key returns an indication of success because |
| 146 | * the pointer value might have been allocated, in which case the |
| 147 | * caller must deallocate the value if we didn't add an undo |
| 148 | * record.) |
| 149 | */ |
| 150 | int add_new_record_ptr_key(VMG_ vm_obj_id_t obj, void *key, |
| 151 | const vm_val_t *val); |
| 152 | |
| 153 | /* add a new record with a pointer key and no separate value data */ |
| 154 | int add_new_record_ptr_key(VMG_ vm_obj_id_t obj, void *key) |
| 155 | { |
| 156 | /* set up a nil value to fill the value slot in the record */ |
| 157 | vm_val_t nilval; |
| 158 | nilval.set_nil(); |
| 159 | |
| 160 | /* save the record */ |
| 161 | return add_new_record_ptr_key(vmg_ obj, key, &nilval); |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * Apply undo to the latest savepoint. After applying the undo |
| 166 | * records, we delete all undo records to the savepoint; this leaves |
| 167 | * the previous savepoint as the new most recent savepoint, so |
| 168 | * repeating this will undo to the previous savepoint. |
| 169 | */ |
| 170 | void undo_to_savept(VMG0_); |
| 171 | |
| 172 | /* |
| 173 | * Garbage collection: mark referenced objects. This goes through |
| 174 | * all of the undo records; for each undo record, we have the object |
| 175 | * that owns the undo record mark as referenced any object to which |
| 176 | * the record refers. |
| 177 | */ |
| 178 | void gc_mark_refs(VMG0_); |
| 179 | |
| 180 | /* |
| 181 | * Garbage collection: delete obsolete weak references. This goes |
| 182 | * through all of the undo records; for each undo record, if the |
| 183 | * object that owns the record is unreachable, we'll delete the undo |
| 184 | * record; otherwise, we'll tell the object that owns the record to |
| 185 | * clean up the record if it contains an unreachable weak reference. |
| 186 | */ |
| 187 | void gc_remove_stale_weak_refs(VMG0_); |
| 188 | |
| 189 | private: |
| 190 | /* |
| 191 | * Allocate an undo record. Returns null if no records are |
| 192 | * available, in which case the caller should simply skip saving |
| 193 | * undo. |
| 194 | */ |
| 195 | CVmUndoMeta *alloc_rec(VMG0_); |
| 196 | |
| 197 | /* |
| 198 | * increment a record pointer, wrapping back at the end of the array |
| 199 | * to the first record |
| 200 | */ |
| 201 | void inc_rec_ptr(CVmUndoMeta **rec) |
| 202 | { |
| 203 | /* increment the record pointer */ |
| 204 | ++(*rec); |
| 205 | |
| 206 | /* if it's at the end of the array, wrap back to the start */ |
| 207 | if (*rec == rec_arr_ + rec_arr_size_) |
| 208 | *rec = rec_arr_; |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Add a new record and return the new record. If we don't have an |
| 213 | * active savepoint, this will return null, since there's no need to |
| 214 | * keep undo. |
| 215 | */ |
| 216 | CVmUndoRecord *add_new_record(VMG_ vm_obj_id_t controlling_obj); |
| 217 | |
| 218 | /* discard the oldest savepoint */ |
| 219 | void drop_oldest_savept(VMG0_); |
| 220 | |
| 221 | /* current savepoint ID */ |
| 222 | vm_savept_t cur_savept_; |
| 223 | |
| 224 | /* |
| 225 | * Number of active savepoint ID's. If this is zero, it means that |
| 226 | * we have no undo information and we're not keeping any undo |
| 227 | * information. If this is one, it means that we only have the |
| 228 | * current savepoint stored. |
| 229 | */ |
| 230 | uint savept_cnt_; |
| 231 | |
| 232 | /* |
| 233 | * The nominal maximum number of savepoints to keep. Any time that |
| 234 | * we attempt to create a new savepoint, and doing so would exceed |
| 235 | * this number of stored savepoints, we will discard the oldest |
| 236 | * savepoint. Note that we may not always store this many |
| 237 | * savepoints, since we will also discard old savepoints when we run |
| 238 | * out of available undo records attempting to create a new |
| 239 | * savepoint. |
| 240 | */ |
| 241 | vm_savept_t max_savepts_; |
| 242 | |
| 243 | /* |
| 244 | * Pointer to the first record in the current savepoint. This isn't |
| 245 | * a real undo record -- it's a link record. |
| 246 | */ |
| 247 | CVmUndoMeta *cur_first_; |
| 248 | |
| 249 | /* |
| 250 | * Pointer to the first undo record in the oldest savepoint. This |
| 251 | * is a link record. |
| 252 | */ |
| 253 | CVmUndoMeta *oldest_first_; |
| 254 | |
| 255 | /* pointer to the next free undo record */ |
| 256 | CVmUndoMeta *next_free_; |
| 257 | |
| 258 | /* |
| 259 | * Master array of undo records, and the number of records in this |
| 260 | * list. We pre-allocate a fixed array of these records. If we run |
| 261 | * out, we start discarding old undo. |
| 262 | */ |
| 263 | CVmUndoMeta *rec_arr_; |
| 264 | size_t rec_arr_size_; |
| 265 | }; |
| 266 | |
| 267 | |
| 268 | #endif /* VMUNDO_H */ |
| 269 |