File: vmundo.cpp

package info (click to toggle)
qtads 2.1.6-1.1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 16,156 kB
  • ctags: 18,767
  • sloc: cpp: 133,078; ansic: 26,048; xml: 18; makefile: 11
file content (570 lines) | stat: -rw-r--r-- 15,995 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#ifdef RCSID
static char RCSid[] =
"$Header: d:/cvsroot/tads/tads3/VMUNDO.CPP,v 1.3 1999/07/11 00:46:58 MJRoberts Exp $";
#endif

/* 
 *   Copyright (c) 1998, 2002 Michael J. Roberts.  All Rights Reserved.
 *   
 *   Please see the accompanying license file, LICENSE.TXT, for information
 *   on using and copying this software.  
 */
/*
Name
  vmundo.cpp - undo manager
Function
  
Notes
  
Modified
  10/29/98 MJRoberts  - Creation
*/

#include "t3std.h"
#include "vmtype.h"
#include "vmobj.h"
#include "vmundo.h"

/*
 *   create the undo manager 
 */
CVmUndo::CVmUndo(size_t undo_record_cnt, uint max_savepts)
{
    /* remember the maximum number of savepoints */
    max_savepts_ = (vm_savept_t)max_savepts;

    /* 
     *   initialize the savepoint to number 0 (this is arbitrary, but we
     *   might as well start here) 
     */
    cur_savept_ = 0;

    /* no savepoints have been created yet */
    savept_cnt_ = 0;

    /* create the undo record array */
    rec_arr_size_ = undo_record_cnt;
    rec_arr_ = (CVmUndoMeta *)t3malloc(rec_arr_size_
                                       * sizeof(rec_arr_[0]));

    /* we have no records at all yet, so we have no "firsts" */
    cur_first_ = 0;
    oldest_first_ = 0;

    /* start allocating from the first entry in the array */
    next_free_ = rec_arr_;
}

/*
 *   delete the undo manager 
 */
CVmUndo::~CVmUndo()
{
    /* delete the array of undo records */
    t3free(rec_arr_);
}

/*
 *   create a savepoint 
 */
void CVmUndo::create_savept(VMG0_)
{
    CVmUndoMeta *rec;

    /* 
     *   Allocate an undo record to serve as the link pointer for this
     *   savepoint.  If this fails, we cannot create a savepoint; we won't
     *   need to do anything else in this case, since we will have deleted
     *   all existing savepoints if we ran out of records.  
     */
    rec = alloc_rec(vmg0_);
    if (rec == 0)
        return;

    /* if we have an old savepoint, the new record starts the next one */
    if (cur_first_ != 0)
        cur_first_->link.next_first = rec;

    /* the old savepoint (if any) is our previous savepoint */
    rec->link.prev_first = cur_first_;

    /* there's nothing after us yet */
    rec->link.next_first = 0;

    /* this record is now the current savepoint's first record */
    cur_first_ = rec;

    /* 
     *   if we didn't have any savepoints recorded yet, this is now the
     *   first record in the oldest savepoint 
     */
    if (oldest_first_ == 0)
        oldest_first_ = rec;

    /* 
     *   increment the savepoint number, wrapping to zero if we reach the
     *   highest value we can store 
     */
    if (cur_savept_ == VM_SAVEPT_MAX)
        cur_savept_ = 0;
    else
        ++cur_savept_;

    /* 
     *   if we're already storing the maximum number of savepoints we're
     *   allowed to store simultaneously, drop the oldest savepoint;
     *   otherwise, simply increment the savepoint counter 
     */
    if (savept_cnt_ == max_savepts_)
    {
        /* we have the maximum number of savepoints; discard the oldest */
        drop_oldest_savept(vmg0_);
    }

    /* count the new savepoint */
    ++savept_cnt_;

    /* notify the objects that we're starting a new savepoint */
    G_obj_table->notify_new_savept();
}

/*
 *   Discard the oldest savepoint 
 */
void CVmUndo::drop_oldest_savept(VMG0_)
{
    /* if there are no savepoints, there's nothing to do */
    if (savept_cnt_ == 0)
        return;

    /* decrement the savepoint count, now that this one is gone */
    --savept_cnt_;

    /* forget the oldest lead pointer */
    if (oldest_first_ != 0)
    {
        /* 
         *   discard records from the oldest lead pointer to the next
         *   oldest lead pointer 
         */
        CVmUndoMeta *meta;
        for (meta = oldest_first_, inc_rec_ptr(&meta) ;
             meta != oldest_first_->link.next_first && meta != next_free_ ; )
        {
            /* discard this entry, if it still exists */
            if (meta->rec.obj != VM_INVALID_OBJ)
                vm_objp(vmg_ meta->rec.obj)->discard_undo(vmg_ &meta->rec);
            
            /* advance to the next record */
            inc_rec_ptr(&meta);
        }
        
        /* advance the oldest pointer to the next savepoint's first record */
        oldest_first_ = oldest_first_->link.next_first;

        /* there's now nothing before this one */
        if (oldest_first_ != 0)
            oldest_first_->link.prev_first = 0;
    }

    /* if we don't have an oldest, we also don't have a current */
    if (oldest_first_ == 0)
        cur_first_ = 0;
}

/*
 *   Drop all undo information
 */
void CVmUndo::drop_undo(VMG0_)
{
    /* delete each savepoint until we have none left */
    while (savept_cnt_ != 0)
        drop_oldest_savept(vmg0_);

    /* reset the savepoint number */
    cur_savept_ = 0;

    /* we have no "firsts" */
    cur_first_ = 0;
    oldest_first_ = 0;

    /* start allocating from the start of the array */
    next_free_ = rec_arr_;
}

/*
 *   Allocate an undo record.  If we run out of free records, delete
 *   savepoints, starting with the oldest savepoint, until we have a free
 *   record.  
 */
CVmUndoMeta *CVmUndo::alloc_rec(VMG0_)
{
    /* 
     *   keep trying until we find an entry or run out of savepoints to
     *   delete 
     */
    for (;;)
    {
        /*
         *   If we have no savepoints at all, all records are free.
         *   Otherwise, if the next free pointer hasn't bumped up against
         *   the oldest allocated record yet, we can allocate another
         *   record.  
         */
        if (savept_cnt_ == 0 || next_free_ != oldest_first_)
        {
            CVmUndoMeta *ret;
            
            /* remember the current free record */
            ret = next_free_;

            /* 
             *   advance to the next record, wrapping if we reach the end
             *   of the array (since we treat the array as circular) 
             */
            inc_rec_ptr(&next_free_);

            /* return the record */
            return ret;
        }
        
        /* 
         *   If we have at least one old savepoint, discard the oldest
         *   savepoint, which may free up some records, and try again;
         *   otherwise, give up.  
         */
        if (savept_cnt_ > 1)
        {
            /* 
             *   we have at least one old savepoint we can discard - get
             *   rid of it and try again 
             */
            drop_oldest_savept(vmg0_);
        }
        else
        {
            /* 
             *   we are down to our last savepoint (or none at all) - it's
             *   no longer valid (since we can't keep it complete due to
             *   the lack of available records), so delete it and return
             *   failure 
             */
            drop_oldest_savept(vmg0_);
            return 0;
        }
    }
}

/*
 *   Add a new record with a property ID key 
 */
void CVmUndo::add_new_record_prop_key(VMG_ vm_obj_id_t obj, vm_prop_id_t key,
                                      const vm_val_t *val)
{
    CVmUndoRecord *rec;

    /* allocate the new record */
    rec = add_new_record(vmg_ obj);

    /* if we successfully allocated a record, set it up */
    if (rec != 0)
    {
        /* set the object */
        rec->obj = obj;
        
        /* set the key */
        rec->id.prop = key;

        /* set the value */
        rec->oldval = *val;
    }
}

/*
 *   Add a new record with an integer key 
 */
void CVmUndo::add_new_record_int_key(VMG_ vm_obj_id_t obj, uint32_t key,
                                     const vm_val_t *val)
{
    CVmUndoRecord *rec;

    /* allocate the new record */
    rec = add_new_record(vmg_ obj);

    /* if we successfully allocated a record, set it up */
    if (rec != 0)
    {
        /* set the object */
        rec->obj = obj;
        
        /* set the key */

        rec->id.intval = key;

        /* set the value */
        rec->oldval = *val;
    }
}

/*
 *   Add a new record with a pointer key
 */
int CVmUndo::add_new_record_ptr_key(VMG_ vm_obj_id_t obj, void *key,
                                    const vm_val_t *val)
{
    CVmUndoRecord *rec;

    /* allocate the new record */
    rec = add_new_record(vmg_ obj);

    /* if we successfully allocated a record, set it up */
    if (rec != 0)
    {
        /* set the object */
        rec->obj = obj;

        /* set the key */
        rec->id.ptrval = key;

        /* set the value */
        rec->oldval = *val;
    }

    /* return an indication as to whether the record was created */
    return (rec != 0);
}

/*
 *   Add a new undo record 
 */
CVmUndoRecord *CVmUndo::add_new_record(VMG_ vm_obj_id_t controlling_obj)
{
    CVmUndoMeta *meta;
    
    /* if there is not an active savepoint, do not keep undo */
    if (savept_cnt_ == 0)
        return 0;

    /* 
     *   if the object was created after the current savepoint was created,
     *   there is no need to keep undo for it during this savepoint, since
     *   rolling back to the savepoint will roll back to a point at which
     *   the object didn't even exist 
     */
    if (!G_obj_table->is_obj_in_undo(controlling_obj))
        return 0;
    
    /* allocate a new record */
    meta = alloc_rec(vmg0_);
    
    /* return the undo record, if we succeeded */
    return (meta == 0 ? 0 : &meta->rec);
}

/*
 *   Apply undo to the most recent savepoint.
 */
void CVmUndo::undo_to_savept(VMG0_)
{
    CVmUndoMeta *meta;
    
    /* if we don't have any savepoints, there's nothing to do */
    if (savept_cnt_ == 0)
        return;

    /* 
     *   Starting with the most recently-added record, apply each record
     *   in sequence until we reach the first savepoint in the undo list.  
     */
    meta = next_free_;
    for (;;)
    {
        /* 
         *   move to the previous record, wrapping to the end of the array
         *   at the first record (since we treat the array as circular) 
         */
        if (meta == rec_arr_)
            meta = rec_arr_ + rec_arr_size_;
        --meta;

        /* 
         *   if we're at the first record in the current savepoint, we're
         *   done 
         */
        if (meta == cur_first_)
            break;

        /* apply this undo record */
        G_obj_table->apply_undo(vmg_ &meta->rec);
    }

    /*
     *   Unwind the undo stack -- get the first record in the previous
     *   savepoint from the link pointer. 
     */
    cur_first_ = cur_first_->link.prev_first;

    /* 
     *   there's nothing following the current savepoint any more -- the
     *   savepoint we just applied is gone now 
     */
    if (cur_first_ != 0)
        cur_first_->link.next_first = 0;
    else
        oldest_first_ = 0;

    /* that's one less savepoint */
    --savept_cnt_;

    /* make the previous savepoint current */
    --cur_savept_;
    if (cur_savept_ == 0)
        cur_savept_ = VM_SAVEPT_MAX;

    /* the savepoint link we just removed is now the next free record */
    next_free_ = meta;

    /* 
     *   notify objects that a new savepoint is in effect - the savepoint
     *   isn't new in the sense of being newly created, but in the sense of
     *   being a different savepoint than was previously in effect 
     */
    G_obj_table->notify_new_savept();
}

/*
 *   Garbage collection: mark referenced objects.  This goes through all
 *   of the undo records; for each undo record, we have the object that
 *   owns the undo record mark as referenced any object to which the
 *   record refers.  
 */
void CVmUndo::gc_mark_refs(VMG0_)
{
    CVmUndoMeta *cur;
    CVmUndoMeta *next_link;

    /* if we don't have any records, there's nothing to do */
    if (oldest_first_ == 0)
        return;

    /* the first record is a linking record */
    next_link = oldest_first_;

    /* start at the first record */
    cur = oldest_first_;

    /* 
     *   Go through all undo records, from the oldest to the newest.  Note
     *   that we must keep track of each "link" record, because these are
     *   not ordinary undo records and must be handled differently.  
     */
    for (;;)
    {
        /* check to see if this is a linking record or an ordinary record */
        if (cur == next_link)
        {
            /* 
             *   this is a linking record - simply note the next linking
             *   record and otherwise ignore this record 
             */
            next_link = cur->link.next_first;
        }
        else if (cur->rec.obj != VM_INVALID_OBJ)
        {
            /* 
             *   It's an ordinary undo record -- mark its references.
             *   Note that the fact that an object has undo records does
             *   not mean that the object is reachable -- this is
             *   effectively a weak link to the object, because if it's
             *   not reachable through other means, there's no need to
             *   keep any undo information for it; so, we don't mark the
             *   owner object itself as reachable at this point.  
             */
            G_obj_table->mark_obj_undo_rec(vmg_ cur->rec.obj, &cur->rec);
        }
        
        /* advance to the next record, wrapping at the end of the array */
        ++cur;
        if (cur == rec_arr_ + rec_arr_size_)
            cur = rec_arr_;

        /* stop if we've reached the last record */
        if (cur == next_free_)
            break;
    }
}

/*
 *   Garbage collection: delete obsolete weak references.  This goes
 *   through all of the undo records; for each undo record, if the object
 *   that owns the record is unreachable, we'll delete the undo record;
 *   otherwise, we'll tell the object that owns the record to clean up the
 *   record if it contains an unreachable weak reference.
 */
void CVmUndo::gc_remove_stale_weak_refs(VMG0_)
{
    CVmUndoMeta *cur;
    CVmUndoMeta *next_link;

    /* if we don't have any records, there's nothing to do */
    if (oldest_first_ == 0)
        return;

    /* the first record is a linking record */
    next_link = oldest_first_;

    /* start at the first record */
    cur = oldest_first_;

    /* 
     *   Go through all undo records, from the oldest to the newest.  Note
     *   that we must keep track of each "link" record, because these are
     *   not ordinary undo records and must be handled differently.  
     */
    for (;;)
    {
        /* check to see if this is a linking record or an ordinary record */
        if (cur == next_link)
        {
            /* 
             *   this is a linking record - simply note the next linking
             *   record and otherwise ignore this record 
             */
            next_link = cur->link.next_first;
        }
        else if (cur->rec.obj != VM_INVALID_OBJ)
        {
            /* 
             *   It's an ordinary undo record -- delete its stale
             *   references.  If the record owner is itself about to be
             *   deleted, don't bother with the object, but rather delete
             *   the entire undo record -- undo records themselves only
             *   weakly reference their owning objects.  
             */
            if (!G_obj_table->is_obj_deletable(cur->rec.obj))
            {
                /* it's not ready for deletion - clean up its weak refs */
                G_obj_table->remove_obj_stale_undo_weak_ref(
                    vmg_ cur->rec.obj, &cur->rec);
            }
            else
            {
                /* 
                 *   it's no longer reachable - delete the undo record by
                 *   setting the owning object to 'invalid' 
                 */
                cur->rec.obj = VM_INVALID_OBJ;
            }
        }

        /* advance to the next record, wrapping at the end of the array */
        ++cur;
        if (cur == rec_arr_ + rec_arr_size_)
            cur = rec_arr_;

        /* stop if we've reached the last record */
        if (cur == next_free_)
            break;
    }
}