| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header: d:/cvsroot/tads/tads3/VMUNDO.CPP,v 1.3 1999/07/11 00:46:58 MJRoberts Exp $"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 1998, 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 | vmundo.cpp - undo manager |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 10/29/98 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #include "t3std.h" |
| 24 | #include "vmtype.h" |
| 25 | #include "vmobj.h" |
| 26 | #include "vmundo.h" |
| 27 | |
| 28 | /* |
| 29 | * create the undo manager |
| 30 | */ |
| 31 | CVmUndo::CVmUndo(size_t undo_record_cnt, uint max_savepts) |
| 32 | { |
| 33 | /* remember the maximum number of savepoints */ |
| 34 | max_savepts_ = (vm_savept_t)max_savepts; |
| 35 | |
| 36 | /* |
| 37 | * initialize the savepoint to number 0 (this is arbitrary, but we |
| 38 | * might as well start here) |
| 39 | */ |
| 40 | cur_savept_ = 0; |
| 41 | |
| 42 | /* no savepoints have been created yet */ |
| 43 | savept_cnt_ = 0; |
| 44 | |
| 45 | /* create the undo record array */ |
| 46 | rec_arr_size_ = undo_record_cnt; |
| 47 | rec_arr_ = (CVmUndoMeta *)t3malloc(rec_arr_size_ |
| 48 | * sizeof(rec_arr_[0])); |
| 49 | |
| 50 | /* we have no records at all yet, so we have no "firsts" */ |
| 51 | cur_first_ = 0; |
| 52 | oldest_first_ = 0; |
| 53 | |
| 54 | /* start allocating from the first entry in the array */ |
| 55 | next_free_ = rec_arr_; |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | * delete the undo manager |
| 60 | */ |
| 61 | CVmUndo::~CVmUndo() |
| 62 | { |
| 63 | /* delete the array of undo records */ |
| 64 | t3free(rec_arr_); |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * create a savepoint |
| 69 | */ |
| 70 | void CVmUndo::create_savept(VMG0_) |
| 71 | { |
| 72 | CVmUndoMeta *rec; |
| 73 | |
| 74 | /* |
| 75 | * Allocate an undo record to serve as the link pointer for this |
| 76 | * savepoint. If this fails, we cannot create a savepoint; we won't |
| 77 | * need to do anything else in this case, since we will have deleted |
| 78 | * all existing savepoints if we ran out of records. |
| 79 | */ |
| 80 | rec = alloc_rec(vmg0_); |
| 81 | if (rec == 0) |
| 82 | return; |
| 83 | |
| 84 | /* if we have an old savepoint, the new record starts the next one */ |
| 85 | if (cur_first_ != 0) |
| 86 | cur_first_->link.next_first = rec; |
| 87 | |
| 88 | /* the old savepoint (if any) is our previous savepoint */ |
| 89 | rec->link.prev_first = cur_first_; |
| 90 | |
| 91 | /* there's nothing after us yet */ |
| 92 | rec->link.next_first = 0; |
| 93 | |
| 94 | /* this record is now the current savepoint's first record */ |
| 95 | cur_first_ = rec; |
| 96 | |
| 97 | /* |
| 98 | * if we didn't have any savepoints recorded yet, this is now the |
| 99 | * first record in the oldest savepoint |
| 100 | */ |
| 101 | if (oldest_first_ == 0) |
| 102 | oldest_first_ = rec; |
| 103 | |
| 104 | /* |
| 105 | * increment the savepoint number, wrapping to zero if we reach the |
| 106 | * highest value we can store |
| 107 | */ |
| 108 | if (cur_savept_ == VM_SAVEPT_MAX) |
| 109 | cur_savept_ = 0; |
| 110 | else |
| 111 | ++cur_savept_; |
| 112 | |
| 113 | /* |
| 114 | * if we're already storing the maximum number of savepoints we're |
| 115 | * allowed to store simultaneously, drop the oldest savepoint; |
| 116 | * otherwise, simply increment the savepoint counter |
| 117 | */ |
| 118 | if (savept_cnt_ == max_savepts_) |
| 119 | { |
| 120 | /* we have the maximum number of savepoints; discard the oldest */ |
| 121 | drop_oldest_savept(vmg0_); |
| 122 | } |
| 123 | |
| 124 | /* count the new savepoint */ |
| 125 | ++savept_cnt_; |
| 126 | |
| 127 | /* notify the objects that we're starting a new savepoint */ |
| 128 | G_obj_table->notify_new_savept(); |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * Discard the oldest savepoint |
| 133 | */ |
| 134 | void CVmUndo::drop_oldest_savept(VMG0_) |
| 135 | { |
| 136 | /* if there are no savepoints, there's nothing to do */ |
| 137 | if (savept_cnt_ == 0) |
| 138 | return; |
| 139 | |
| 140 | /* decrement the savepoint count, now that this one is gone */ |
| 141 | --savept_cnt_; |
| 142 | |
| 143 | /* forget the oldest lead pointer */ |
| 144 | if (oldest_first_ != 0) |
| 145 | { |
| 146 | /* |
| 147 | * discard records from the oldest lead pointer to the next |
| 148 | * oldest lead pointer |
| 149 | */ |
| 150 | CVmUndoMeta *meta; |
| 151 | for (meta = oldest_first_, inc_rec_ptr(&meta) ; |
| 152 | meta != oldest_first_->link.next_first && meta != next_free_ ; ) |
| 153 | { |
| 154 | /* discard this entry, if it still exists */ |
| 155 | if (meta->rec.obj != VM_INVALID_OBJ) |
| 156 | vm_objp(vmg_ meta->rec.obj)->discard_undo(vmg_ &meta->rec); |
| 157 | |
| 158 | /* advance to the next record */ |
| 159 | inc_rec_ptr(&meta); |
| 160 | } |
| 161 | |
| 162 | /* advance the oldest pointer to the next savepoint's first record */ |
| 163 | oldest_first_ = oldest_first_->link.next_first; |
| 164 | |
| 165 | /* there's now nothing before this one */ |
| 166 | if (oldest_first_ != 0) |
| 167 | oldest_first_->link.prev_first = 0; |
| 168 | } |
| 169 | |
| 170 | /* if we don't have an oldest, we also don't have a current */ |
| 171 | if (oldest_first_ == 0) |
| 172 | cur_first_ = 0; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Drop all undo information |
| 177 | */ |
| 178 | void CVmUndo::drop_undo(VMG0_) |
| 179 | { |
| 180 | /* delete each savepoint until we have none left */ |
| 181 | while (savept_cnt_ != 0) |
| 182 | drop_oldest_savept(vmg0_); |
| 183 | |
| 184 | /* reset the savepoint number */ |
| 185 | cur_savept_ = 0; |
| 186 | |
| 187 | /* we have no "firsts" */ |
| 188 | cur_first_ = 0; |
| 189 | oldest_first_ = 0; |
| 190 | |
| 191 | /* start allocating from the start of the array */ |
| 192 | next_free_ = rec_arr_; |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * Allocate an undo record. If we run out of free records, delete |
| 197 | * savepoints, starting with the oldest savepoint, until we have a free |
| 198 | * record. |
| 199 | */ |
| 200 | CVmUndoMeta *CVmUndo::alloc_rec(VMG0_) |
| 201 | { |
| 202 | /* |
| 203 | * keep trying until we find an entry or run out of savepoints to |
| 204 | * delete |
| 205 | */ |
| 206 | for (;;) |
| 207 | { |
| 208 | /* |
| 209 | * If we have no savepoints at all, all records are free. |
| 210 | * Otherwise, if the next free pointer hasn't bumped up against |
| 211 | * the oldest allocated record yet, we can allocate another |
| 212 | * record. |
| 213 | */ |
| 214 | if (savept_cnt_ == 0 || next_free_ != oldest_first_) |
| 215 | { |
| 216 | CVmUndoMeta *ret; |
| 217 | |
| 218 | /* remember the current free record */ |
| 219 | ret = next_free_; |
| 220 | |
| 221 | /* |
| 222 | * advance to the next record, wrapping if we reach the end |
| 223 | * of the array (since we treat the array as circular) |
| 224 | */ |
| 225 | inc_rec_ptr(&next_free_); |
| 226 | |
| 227 | /* return the record */ |
| 228 | return ret; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * If we have at least one old savepoint, discard the oldest |
| 233 | * savepoint, which may free up some records, and try again; |
| 234 | * otherwise, give up. |
| 235 | */ |
| 236 | if (savept_cnt_ > 1) |
| 237 | { |
| 238 | /* |
| 239 | * we have at least one old savepoint we can discard - get |
| 240 | * rid of it and try again |
| 241 | */ |
| 242 | drop_oldest_savept(vmg0_); |
| 243 | } |
| 244 | else |
| 245 | { |
| 246 | /* |
| 247 | * we are down to our last savepoint (or none at all) - it's |
| 248 | * no longer valid (since we can't keep it complete due to |
| 249 | * the lack of available records), so delete it and return |
| 250 | * failure |
| 251 | */ |
| 252 | drop_oldest_savept(vmg0_); |
| 253 | return 0; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Add a new record with a property ID key |
| 260 | */ |
| 261 | void CVmUndo::add_new_record_prop_key(VMG_ vm_obj_id_t obj, vm_prop_id_t key, |
| 262 | const vm_val_t *val) |
| 263 | { |
| 264 | CVmUndoRecord *rec; |
| 265 | |
| 266 | /* allocate the new record */ |
| 267 | rec = add_new_record(vmg_ obj); |
| 268 | |
| 269 | /* if we successfully allocated a record, set it up */ |
| 270 | if (rec != 0) |
| 271 | { |
| 272 | /* set the object */ |
| 273 | rec->obj = obj; |
| 274 | |
| 275 | /* set the key */ |
| 276 | rec->id.prop = key; |
| 277 | |
| 278 | /* set the value */ |
| 279 | rec->oldval = *val; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * Add a new record with an integer key |
| 285 | */ |
| 286 | void CVmUndo::add_new_record_int_key(VMG_ vm_obj_id_t obj, uint32_t key, |
| 287 | const vm_val_t *val) |
| 288 | { |
| 289 | CVmUndoRecord *rec; |
| 290 | |
| 291 | /* allocate the new record */ |
| 292 | rec = add_new_record(vmg_ obj); |
| 293 | |
| 294 | /* if we successfully allocated a record, set it up */ |
| 295 | if (rec != 0) |
| 296 | { |
| 297 | /* set the object */ |
| 298 | rec->obj = obj; |
| 299 | |
| 300 | /* set the key */ |
| 301 | |
| 302 | rec->id.intval = key; |
| 303 | |
| 304 | /* set the value */ |
| 305 | rec->oldval = *val; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * Add a new record with a pointer key |
| 311 | */ |
| 312 | int CVmUndo::add_new_record_ptr_key(VMG_ vm_obj_id_t obj, void *key, |
| 313 | const vm_val_t *val) |
| 314 | { |
| 315 | CVmUndoRecord *rec; |
| 316 | |
| 317 | /* allocate the new record */ |
| 318 | rec = add_new_record(vmg_ obj); |
| 319 | |
| 320 | /* if we successfully allocated a record, set it up */ |
| 321 | if (rec != 0) |
| 322 | { |
| 323 | /* set the object */ |
| 324 | rec->obj = obj; |
| 325 | |
| 326 | /* set the key */ |
| 327 | rec->id.ptrval = key; |
| 328 | |
| 329 | /* set the value */ |
| 330 | rec->oldval = *val; |
| 331 | } |
| 332 | |
| 333 | /* return an indication as to whether the record was created */ |
| 334 | return (rec != 0); |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * Add a new undo record |
| 339 | */ |
| 340 | CVmUndoRecord *CVmUndo::add_new_record(VMG_ vm_obj_id_t controlling_obj) |
| 341 | { |
| 342 | CVmUndoMeta *meta; |
| 343 | |
| 344 | /* if there is not an active savepoint, do not keep undo */ |
| 345 | if (savept_cnt_ == 0) |
| 346 | return 0; |
| 347 | |
| 348 | /* |
| 349 | * if the object was created after the current savepoint was created, |
| 350 | * there is no need to keep undo for it during this savepoint, since |
| 351 | * rolling back to the savepoint will roll back to a point at which |
| 352 | * the object didn't even exist |
| 353 | */ |
| 354 | if (!G_obj_table->is_obj_in_undo(controlling_obj)) |
| 355 | return 0; |
| 356 | |
| 357 | /* allocate a new record */ |
| 358 | meta = alloc_rec(vmg0_); |
| 359 | |
| 360 | /* return the undo record, if we succeeded */ |
| 361 | return (meta == 0 ? 0 : &meta->rec); |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * Apply undo to the most recent savepoint. |
| 366 | */ |
| 367 | void CVmUndo::undo_to_savept(VMG0_) |
| 368 | { |
| 369 | CVmUndoMeta *meta; |
| 370 | |
| 371 | /* if we don't have any savepoints, there's nothing to do */ |
| 372 | if (savept_cnt_ == 0) |
| 373 | return; |
| 374 | |
| 375 | /* |
| 376 | * Starting with the most recently-added record, apply each record |
| 377 | * in sequence until we reach the first savepoint in the undo list. |
| 378 | */ |
| 379 | meta = next_free_; |
| 380 | for (;;) |
| 381 | { |
| 382 | /* |
| 383 | * move to the previous record, wrapping to the end of the array |
| 384 | * at the first record (since we treat the array as circular) |
| 385 | */ |
| 386 | if (meta == rec_arr_) |
| 387 | meta = rec_arr_ + rec_arr_size_; |
| 388 | --meta; |
| 389 | |
| 390 | /* |
| 391 | * if we're at the first record in the current savepoint, we're |
| 392 | * done |
| 393 | */ |
| 394 | if (meta == cur_first_) |
| 395 | break; |
| 396 | |
| 397 | /* apply this undo record */ |
| 398 | G_obj_table->apply_undo(vmg_ &meta->rec); |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * Unwind the undo stack -- get the first record in the previous |
| 403 | * savepoint from the link pointer. |
| 404 | */ |
| 405 | cur_first_ = cur_first_->link.prev_first; |
| 406 | |
| 407 | /* |
| 408 | * there's nothing following the current savepoint any more -- the |
| 409 | * savepoint we just applied is gone now |
| 410 | */ |
| 411 | if (cur_first_ != 0) |
| 412 | cur_first_->link.next_first = 0; |
| 413 | else |
| 414 | oldest_first_ = 0; |
| 415 | |
| 416 | /* that's one less savepoint */ |
| 417 | --savept_cnt_; |
| 418 | |
| 419 | /* make the previous savepoint current */ |
| 420 | --cur_savept_; |
| 421 | if (cur_savept_ == 0) |
| 422 | cur_savept_ = VM_SAVEPT_MAX; |
| 423 | |
| 424 | /* the savepoint link we just removed is now the next free record */ |
| 425 | next_free_ = meta; |
| 426 | |
| 427 | /* |
| 428 | * notify objects that a new savepoint is in effect - the savepoint |
| 429 | * isn't new in the sense of being newly created, but in the sense of |
| 430 | * being a different savepoint than was previously in effect |
| 431 | */ |
| 432 | G_obj_table->notify_new_savept(); |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | * Garbage collection: mark referenced objects. This goes through all |
| 437 | * of the undo records; for each undo record, we have the object that |
| 438 | * owns the undo record mark as referenced any object to which the |
| 439 | * record refers. |
| 440 | */ |
| 441 | void CVmUndo::gc_mark_refs(VMG0_) |
| 442 | { |
| 443 | CVmUndoMeta *cur; |
| 444 | CVmUndoMeta *next_link; |
| 445 | |
| 446 | /* if we don't have any records, there's nothing to do */ |
| 447 | if (oldest_first_ == 0) |
| 448 | return; |
| 449 | |
| 450 | /* the first record is a linking record */ |
| 451 | next_link = oldest_first_; |
| 452 | |
| 453 | /* start at the first record */ |
| 454 | cur = oldest_first_; |
| 455 | |
| 456 | /* |
| 457 | * Go through all undo records, from the oldest to the newest. Note |
| 458 | * that we must keep track of each "link" record, because these are |
| 459 | * not ordinary undo records and must be handled differently. |
| 460 | */ |
| 461 | for (;;) |
| 462 | { |
| 463 | /* check to see if this is a linking record or an ordinary record */ |
| 464 | if (cur == next_link) |
| 465 | { |
| 466 | /* |
| 467 | * this is a linking record - simply note the next linking |
| 468 | * record and otherwise ignore this record |
| 469 | */ |
| 470 | next_link = cur->link.next_first; |
| 471 | } |
| 472 | else if (cur->rec.obj != VM_INVALID_OBJ) |
| 473 | { |
| 474 | /* |
| 475 | * It's an ordinary undo record -- mark its references. |
| 476 | * Note that the fact that an object has undo records does |
| 477 | * not mean that the object is reachable -- this is |
| 478 | * effectively a weak link to the object, because if it's |
| 479 | * not reachable through other means, there's no need to |
| 480 | * keep any undo information for it; so, we don't mark the |
| 481 | * owner object itself as reachable at this point. |
| 482 | */ |
| 483 | G_obj_table->mark_obj_undo_rec(vmg_ cur->rec.obj, &cur->rec); |
| 484 | } |
| 485 | |
| 486 | /* advance to the next record, wrapping at the end of the array */ |
| 487 | ++cur; |
| 488 | if (cur == rec_arr_ + rec_arr_size_) |
| 489 | cur = rec_arr_; |
| 490 | |
| 491 | /* stop if we've reached the last record */ |
| 492 | if (cur == next_free_) |
| 493 | break; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | /* |
| 498 | * Garbage collection: delete obsolete weak references. This goes |
| 499 | * through all of the undo records; for each undo record, if the object |
| 500 | * that owns the record is unreachable, we'll delete the undo record; |
| 501 | * otherwise, we'll tell the object that owns the record to clean up the |
| 502 | * record if it contains an unreachable weak reference. |
| 503 | */ |
| 504 | void CVmUndo::gc_remove_stale_weak_refs(VMG0_) |
| 505 | { |
| 506 | CVmUndoMeta *cur; |
| 507 | CVmUndoMeta *next_link; |
| 508 | |
| 509 | /* if we don't have any records, there's nothing to do */ |
| 510 | if (oldest_first_ == 0) |
| 511 | return; |
| 512 | |
| 513 | /* the first record is a linking record */ |
| 514 | next_link = oldest_first_; |
| 515 | |
| 516 | /* start at the first record */ |
| 517 | cur = oldest_first_; |
| 518 | |
| 519 | /* |
| 520 | * Go through all undo records, from the oldest to the newest. Note |
| 521 | * that we must keep track of each "link" record, because these are |
| 522 | * not ordinary undo records and must be handled differently. |
| 523 | */ |
| 524 | for (;;) |
| 525 | { |
| 526 | /* check to see if this is a linking record or an ordinary record */ |
| 527 | if (cur == next_link) |
| 528 | { |
| 529 | /* |
| 530 | * this is a linking record - simply note the next linking |
| 531 | * record and otherwise ignore this record |
| 532 | */ |
| 533 | next_link = cur->link.next_first; |
| 534 | } |
| 535 | else if (cur->rec.obj != VM_INVALID_OBJ) |
| 536 | { |
| 537 | /* |
| 538 | * It's an ordinary undo record -- delete its stale |
| 539 | * references. If the record owner is itself about to be |
| 540 | * deleted, don't bother with the object, but rather delete |
| 541 | * the entire undo record -- undo records themselves only |
| 542 | * weakly reference their owning objects. |
| 543 | */ |
| 544 | if (!G_obj_table->is_obj_deletable(cur->rec.obj)) |
| 545 | { |
| 546 | /* it's not ready for deletion - clean up its weak refs */ |
| 547 | G_obj_table->remove_obj_stale_undo_weak_ref( |
| 548 | vmg_ cur->rec.obj, &cur->rec); |
| 549 | } |
| 550 | else |
| 551 | { |
| 552 | /* |
| 553 | * it's no longer reachable - delete the undo record by |
| 554 | * setting the owning object to 'invalid' |
| 555 | */ |
| 556 | cur->rec.obj = VM_INVALID_OBJ; |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | /* advance to the next record, wrapping at the end of the array */ |
| 561 | ++cur; |
| 562 | if (cur == rec_arr_ + rec_arr_size_) |
| 563 | cur = rec_arr_; |
| 564 | |
| 565 | /* stop if we've reached the last record */ |
| 566 | if (cur == next_free_) |
| 567 | break; |
| 568 | } |
| 569 | } |
| 570 |