isave.c 46.4 KB
Newer Older
1
/* Copyright (C) 2001-2019 Artifex Software, Inc.
2
   All Rights Reserved.
3

4 5 6
   This software is provided AS-IS with no warranty, either express or
   implied.

7 8 9 10 11
   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
12 13
   Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
   CA 94945, U.S.A., +1(415)492-9861, for further information.
14 15
*/

16

17 18 19 20 21 22 23 24 25 26 27 28 29
/* Save/restore manager for Ghostscript interpreter */
#include "ghost.h"
#include "memory_.h"
#include "ierrors.h"
#include "gsexit.h"
#include "gsstruct.h"
#include "stream.h"		/* for linking for forgetsave */
#include "iastate.h"
#include "inamedef.h"
#include "iname.h"
#include "ipacked.h"
#include "isave.h"
#include "isstate.h"
30
#include "gsstate.h"
31 32 33 34
#include "store.h"		/* for ref_assign */
#include "ivmspace.h"
#include "igc.h"
#include "gsutil.h"		/* gs_next_ids prototype */
35
#include "icstate.h"
36 37 38 39 40 41 42 43

/* Structure descriptor */
private_st_alloc_save();

/* Define the maximum amount of data we are willing to scan repeatedly -- */
/* see below for details. */
static const long max_repeated_scan = 100000;

44 45 46
/* Define the minimum space for creating an inner clump. */
/* Must be at least sizeof(clump_head_t). */
static const long min_inner_clump_space = sizeof(clump_head_t) + 500;
47 48 49 50 51 52 53 54 55

/*
 * The logic for saving and restoring the state is complex.
 * Both the changes to individual objects, and the overall state
 * of the memory manager, must be saved and restored.
 */

/*
 * To save the state of the memory manager:
56 57
 *      Save the state of the current clump in which we are allocating.
 *      Shrink all clumps to their inner unallocated region.
58 59 60 61 62
 *      Save and reset the free block chains.
 * By doing this, we guarantee that no object older than the save
 * can be freed.
 *
 * To restore the state of the memory manager:
63 64 65 66
 *      Free all clumps newer than the save, and the descriptors for
 *        the inner clumps created by the save.
 *      Make current the clump that was current at the time of the save.
 *      Restore the state of the current clump.
67 68 69
 *
 * In addition to save ("start transaction") and restore ("abort transaction"),
 * we support forgetting a save ("commit transation").  To forget a save:
70 71 72
 *      Reassign to the next outer save all clumps newer than the save.
 *      Free the descriptors for the inners clump, updating their outer
 *        clumps to reflect additional allocations in the inner clumps.
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
 *      Concatenate the free block chains with those of the outer save.
 */

/*
 * For saving changes to individual objects, we add an "attribute" bit
 * (l_new) that logically belongs to the slot where the ref is stored,
 * not to the ref itself.  The bit means "the contents of this slot
 * have been changed, or the slot was allocated, since the last save."
 * To keep track of changes since the save, we associate a chain of
 * <slot, old_contents> pairs that remembers the old contents of slots.
 *
 * When creating an object, if the save level is non-zero:
 *      Set l_new in all slots.
 *
 * When storing into a slot, if the save level is non-zero:
 *      If l_new isn't set, save the address and contents of the slot
 *        on the current contents chain.
 *      Set l_new after storing the new value.
 *
 * To do a save:
 *      If the save level is non-zero:
 *              Reset l_new in all slots on the contents chain, and in all
 *                objects created since the previous save.
 *      Push the head of the contents chain, and reset the chain to empty.
 *
 * To do a restore:
 *      Check all the stacks to make sure they don't contain references
 *        to objects created since the save.
 *      Restore all the slots on the contents chain.
 *      Pop the contents chain head.
 *      If the save level is now non-zero:
 *              Scan the newly restored contents chain, and set l_new in all
 *                the slots it references.
 *              Scan all objects created since the previous save, and set
 *                l_new in all the slots of each object.
 *
 * To forget a save:
 *      If the save level is greater than 1:
 *              Set l_new as for a restore, per the next outer save.
 *              Concatenate the next outer contents chain to the end of
 *                the current one.
 *      If the save level is 1:
 *              Reset l_new as for a save.
 *              Free the contents chain.
 */

/*
 * A consequence of the foregoing algorithms is that the cost of a save is
 * proportional to the total amount of data allocated since the previous
 * save.  If a PostScript program reads in a large amount of setup code and
 * then uses save/restore heavily, each save/restore will be expensive.  To
 * mitigate this, we check to see how much data we have scanned at this save
 * level: if it is large, we do a second, invisible save.  This greatly
 * reduces the cost of inner saves, at the expense of possibly saving some
 * changes twice that otherwise would only have to be saved once.
 */

/*
 * The presence of global and local VM complicates the situation further.
 * There is a separate save chain and contents chain for each VM space.
 * When multiple contexts are fully implemented, save and restore will have
 * the following effects, according to the privacy status of the current
 * context's global and local VM:
 *      Private global, private local:
 *              The outermost save saves both global and local VM;
 *                otherwise, save only saves local VM.
 *      Shared global, private local:
 *              Save only saves local VM.
 *      Shared global, shared local:
 *              Save only saves local VM, and suspends all other contexts
 *                sharing the same local VM until the matching restore.
 * Since we do not currently implement multiple contexts, only the first
 * case is relevant.
 *
 * Note that when saving the contents of a slot, the choice of chain
 * is determined by the VM space in which the slot is allocated,
 * not by the current allocation mode.
 */

/* Tracing printout */
static void
print_save(const char *str, uint spacen, const alloc_save_t *sav)
{
  if_debug5('u', "[u]%s space %u 0x%lx: cdata = 0x%lx, id = %lu\n",\
157
            str, spacen, (ulong)sav, (ulong)sav->client_data, (ulong)sav->id);
158 159 160 161 162
}

/* A link to igcref.c . */
ptr_proc_reloc(igc_reloc_ref_ptr_nocheck, ref_packed);

163
static
164 165 166 167 168
CLEAR_MARKS_PROC(change_clear_marks)
{
    alloc_change_t *const ptr = (alloc_change_t *)vptr;

    if (r_is_packed(&ptr->contents))
169
        r_clear_pmark((ref_packed *) & ptr->contents);
170
    else
171
        r_clear_attrs(&ptr->contents, l_mark);
172
}
173
static
174 175 176 177
ENUM_PTRS_WITH(change_enum_ptrs, alloc_change_t *ptr) return 0;
ENUM_PTR(0, alloc_change_t, next);
case 1:
    if (ptr->offset >= 0)
178
        ENUM_RETURN((byte *) ptr->where - ptr->offset);
179
    else
180 181 182 183 184 185 186 187
        if (ptr->offset != AC_OFFSET_ALLOCATED)
            ENUM_RETURN_REF(ptr->where);
        else {
            /* Don't enumerate ptr->where, because it
               needs a special processing with
               alloc_save__filter_changes. */
            ENUM_RETURN(0);
        }
188 189 190 191 192 193 194
case 2:
    ENUM_RETURN_REF(&ptr->contents);
ENUM_PTRS_END
static RELOC_PTRS_WITH(change_reloc_ptrs, alloc_change_t *ptr)
{
    RELOC_VAR(ptr->next);
    switch (ptr->offset) {
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
        case AC_OFFSET_STATIC:
            break;
        case AC_OFFSET_REF:
            RELOC_REF_PTR_VAR(ptr->where);
            break;
        case AC_OFFSET_ALLOCATED:
            /* We know that ptr->where may point to an unmarked object
               because change_enum_ptrs skipped it,
               and we know it always points to same space
               because we took a special care when calling alloc_save_change_alloc.
               Therefore we must skip the check for the mark,
               which would happen if we call the regular relocation function
               igc_reloc_ref_ptr from RELOC_REF_PTR_VAR.
               Calling igc_reloc_ref_ptr_nocheck instead. */
            {	/* A sanity check. */
210
                obj_header_t *pre = (obj_header_t *)ptr->where - 1;
211 212

                if (pre->o_type != &st_refs)
213
                    gs_abort(gcst->heap);
214 215 216 217 218 219 220 221 222 223 224 225
            }
            if (ptr->where != 0 && !gcst->relocating_untraced)
                ptr->where = igc_reloc_ref_ptr_nocheck(ptr->where, gcst);
            break;
        default:
            {
                byte *obj = (byte *) ptr->where - ptr->offset;

                RELOC_VAR(obj);
                ptr->where = (ref_packed *) (obj + ptr->offset);
            }
            break;
226 227
    }
    if (r_is_packed(&ptr->contents))
228
        r_clear_pmark((ref_packed *) & ptr->contents);
229
    else {
230 231
        RELOC_REF_VAR(ptr->contents);
        r_clear_attrs(&ptr->contents, l_mark);
232 233 234 235
    }
}
RELOC_PTRS_END
gs_private_st_complex_only(st_alloc_change, alloc_change_t, "alloc_change",
236
                change_clear_marks, change_enum_ptrs, change_reloc_ptrs, 0);
237 238 239 240

/* Debugging printout */
#ifdef DEBUG
static void
241
alloc_save_print(const gs_memory_t *mem, alloc_change_t * cp, bool print_current)
242
{
243
    dmprintf2(mem, " 0x%lx: 0x%lx: ", (ulong) cp, (ulong) cp->where);
244
    if (r_is_packed(&cp->contents)) {
245
        if (print_current)
246 247
            dmprintf2(mem, "saved=%x cur=%x\n", *(ref_packed *) & cp->contents,
                      *cp->where);
248
        else
249
            dmprintf1(mem, "%x\n", *(ref_packed *) & cp->contents);
250
    } else {
251
        if (print_current)
252 253 254 255 256 257
            dmprintf6(mem, "saved=%x %x %lx cur=%x %x %lx\n",
                      r_type_attrs(&cp->contents), r_size(&cp->contents),
                      (ulong) cp->contents.value.intval,
                      r_type_attrs((ref *) cp->where),
                      r_size((ref *) cp->where),
                      (ulong) ((ref *) cp->where)->value.intval);
258
        else
259 260 261
            dmprintf3(mem, "%x %x %lx\n",
                      r_type_attrs(&cp->contents), r_size(&cp->contents),
                      (ulong) cp->contents.value.intval);
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
    }
}
#endif

/* Forward references */
static int  restore_resources(alloc_save_t *, gs_ref_memory_t *);
static void restore_free(gs_ref_memory_t *);
static int  save_set_new(gs_ref_memory_t * mem, bool to_new, bool set_limit, ulong *pscanned);
static int  save_set_new_changes(gs_ref_memory_t *, bool, bool);
static bool check_l_mark(void *obj);

/* Initialize the save/restore machinery. */
void
alloc_save_init(gs_dual_memory_t * dmem)
{
    alloc_set_not_in_save(dmem);
}

/* Record that we are in a save. */
static void
alloc_set_masks(gs_dual_memory_t *dmem, uint new_mask, uint test_mask)
{
    int i;
    gs_ref_memory_t *mem;

    dmem->new_mask = new_mask;
    dmem->test_mask = test_mask;
    for (i = 0; i < countof(dmem->spaces.memories.indexed); ++i)
290 291 292 293 294 295 296
        if ((mem = dmem->spaces.memories.indexed[i]) != 0) {
            mem->new_mask = new_mask, mem->test_mask = test_mask;
            if (mem->stable_memory != (gs_memory_t *)mem) {
                mem = (gs_ref_memory_t *)mem->stable_memory;
                mem->new_mask = new_mask, mem->test_mask = test_mask;
            }
        }
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
}
void
alloc_set_in_save(gs_dual_memory_t *dmem)
{
    alloc_set_masks(dmem, l_new, l_new);
}

/* Record that we are not in a save. */
void
alloc_set_not_in_save(gs_dual_memory_t *dmem)
{
    alloc_set_masks(dmem, 0, ~0);
}

/* Save the state. */
static alloc_save_t *alloc_save_space(gs_ref_memory_t *mem,
313 314
                                       gs_dual_memory_t *dmem,
                                       ulong sid);
315 316 317
static void
alloc_free_save(gs_ref_memory_t *mem, alloc_save_t *save, const char *scn)
{
318 319
    gs_ref_memory_t save_mem;
    save_mem = mem->saved->state;
320
    gs_free_object((gs_memory_t *)mem, save, scn);
321
    /* Free any inner clump structures.  This is the easiest way to do it. */
322
    restore_free(mem);
323 324 325
    /* Restore the 'saved' state - this pulls our object off the linked
     * list of states. Without this we hit a SEGV in the gc later. */
    *mem = save_mem;
326 327 328 329 330 331 332 333
}
int
alloc_save_state(gs_dual_memory_t * dmem, void *cdata, ulong *psid)
{
    gs_ref_memory_t *lmem = dmem->space_local;
    gs_ref_memory_t *gmem = dmem->space_global;
    ulong sid = gs_next_ids((const gs_memory_t *)lmem->stable_memory, 2);
    bool global =
334 335
        lmem->save_level == 0 && gmem != lmem &&
        gmem->num_contexts == 1;
336
    alloc_save_t *gsave =
337
        (global ? alloc_save_space(gmem, dmem, sid + 1) : (alloc_save_t *) 0);
338 339 340
    alloc_save_t *lsave = alloc_save_space(lmem, dmem, sid);

    if (lsave == 0 || (global && gsave == 0)) {
341 342 343 344
        /* Only 1 of lsave or gsave will have been allocated, but
         * nevertheless (in case things change in future), we free
         * lsave, then gsave, so they 'pop' correctly when restoring
         * the mem->saved states. */
345 346 347 348
        if (lsave != 0)
            alloc_free_save(lmem, lsave, "alloc_save_state(local save)");
        if (gsave != 0)
            alloc_free_save(gmem, gsave, "alloc_save_state(global save)");
349
        return_error(gs_error_VMerror);
350 351
    }
    if (gsave != 0) {
352 353 354 355 356
        gsave->client_data = 0;
        print_save("save", gmem->space, gsave);
        /* Restore names when we do the local restore. */
        lsave->restore_names = gsave->restore_names;
        gsave->restore_names = false;
357 358 359 360 361 362 363 364
    }
    lsave->id = sid;
    lsave->client_data = cdata;
    print_save("save", lmem->space, lsave);
    /* Reset the l_new attribute in all slots.  The only slots that */
    /* can have the attribute set are the ones on the changes chain, */
    /* and ones in objects allocated since the last save. */
    if (lmem->save_level > 1) {
365 366
        ulong scanned;
        int code = save_set_new(&lsave->state, false, true, &scanned);
367

368 369
        if (code < 0)
            return code;
370
#if 0 /* Disable invisible save levels. */
371 372 373
        if ((lsave->state.total_scanned += scanned) > max_repeated_scan) {
            /* Do a second, invisible save. */
            alloc_save_t *rsave;
374

375 376 377
            rsave = alloc_save_space(lmem, dmem, 0L);
            if (rsave != 0) {
                rsave->client_data = cdata;
378
#if 0 /* Bug 688153 */
379 380 381 382 383
                rsave->id = lsave->id;
                print_save("save", lmem->space, rsave);
                lsave->id = 0;	/* mark as invisible */
                rsave->state.save_level--; /* ditto */
                lsave->client_data = 0;
384
#else
385 386 387 388
                rsave->id = 0;  /* mark as invisible */
                print_save("save", lmem->space, rsave);
                rsave->state.save_level--; /* ditto */
                rsave->client_data = 0;
389
#endif
390 391 392 393 394
                /* Inherit the allocated space count -- */
                /* we need this for triggering a GC. */
                print_save("save", lmem->space, lsave);
            }
        }
395 396
#endif
    }
397

398 399 400 401 402 403 404 405 406 407
    alloc_set_in_save(dmem);
    *psid = sid;
    return 0;
}
/* Save the state of one space (global or local). */
static alloc_save_t *
alloc_save_space(gs_ref_memory_t * mem, gs_dual_memory_t * dmem, ulong sid)
{
    gs_ref_memory_t save_mem;
    alloc_save_t *save;
408
    clump_t *cp;
409
    clump_t *new_cc = NULL;
410
    clump_splay_walker sw;
411 412

    save_mem = *mem;
413
    alloc_close_clump(mem);
414
    mem->cc = NULL;
415 416 417
    gs_memory_status((gs_memory_t *) mem, &mem->previous_status);
    ialloc_reset(mem);

418
    /* Create inner clumps wherever it's worthwhile. */
419

420 421 422 423 424
    for (cp = clump_splay_walk_init(&sw, &save_mem); cp != 0; cp = clump_splay_walk_fwd(&sw)) {
        if (cp->ctop - cp->cbot > min_inner_clump_space) {
            /* Create an inner clump to cover only the unallocated part. */
            clump_t *inner =
                gs_raw_alloc_struct_immovable(mem->non_gc_memory, &st_clump,
425 426 427 428
                                              "alloc_save_space(inner)");

            if (inner == 0)
                break;		/* maybe should fail */
429 430 431
            alloc_init_clump(inner, cp->cbot, cp->ctop, cp->sreloc != 0, cp);
            alloc_link_clump(inner, mem);
            if_debug2m('u', (gs_memory_t *)mem, "[u]inner clump: cbot=0x%lx ctop=0x%lx\n",
432
                       (ulong) inner->cbot, (ulong) inner->ctop);
433 434
            if (cp == save_mem.cc)
                new_cc = inner;
435
        }
436
    }
437
    mem->cc = new_cc;
438
    alloc_open_clump(mem);
439 440

    save = gs_alloc_struct((gs_memory_t *) mem, alloc_save_t,
441
                           &st_alloc_save, "alloc_save_space(save)");
442 443
    if_debug2m('u', (gs_memory_t *)mem, "[u]save space %u at 0x%lx\n",
               mem->space, (ulong) save);
444
    if (save == 0) {
445
        /* Free the inner clump structures.  This is the easiest way. */
446 447 448
        restore_free(mem);
        *mem = save_mem;
        return 0;
449
    }
450
    save->client_data = NULL;
451 452 453 454 455 456
    save->state = save_mem;
    save->spaces = dmem->spaces;
    save->restore_names = (name_memory(mem) == (gs_memory_t *) mem);
    save->is_current = (dmem->current == mem);
    save->id = sid;
    mem->saved = save;
457 458
    if_debug2m('u', (gs_memory_t *)mem, "[u%u]file_save 0x%lx\n",
               mem->space, (ulong) mem->streams);
459 460 461 462
    mem->streams = 0;
    mem->total_scanned = 0;
    mem->total_scanned_after_compacting = 0;
    if (sid)
463
        mem->save_level++;
464 465 466 467 468 469 470
    return save;
}

/* Record a state change that must be undone for restore, */
/* and mark it as having been saved. */
int
alloc_save_change_in(gs_ref_memory_t *mem, const ref * pcont,
471
                  ref_packed * where, client_name_t cname)
472 473 474 475
{
    register alloc_change_t *cp;

    if (mem->new_mask == 0)
476
        return 0;		/* no saving */
477
    cp = gs_alloc_struct((gs_memory_t *)mem, alloc_change_t,
478
                         &st_alloc_change, "alloc_save_change");
479
    if (cp == 0)
480
        return -1;
481 482 483
    cp->next = mem->changes;
    cp->where = where;
    if (pcont == NULL)
484
        cp->offset = AC_OFFSET_STATIC;
485
    else if (r_is_array(pcont) || r_has_type(pcont, t_dictionary))
486
        cp->offset = AC_OFFSET_REF;
487
    else if (r_is_struct(pcont))
488
        cp->offset = (byte *) where - (byte *) pcont->value.pstruct;
489
    else {
490 491 492
        lprintf3("Bad type %u for save!  pcont = 0x%lx, where = 0x%lx\n",
                 r_type(pcont), (ulong) pcont, (ulong) where);
        gs_abort((const gs_memory_t *)mem);
493 494
    }
    if (r_is_packed(where))
495
        *(ref_packed *)&cp->contents = *where;
496
    else {
497 498
        ref_assign_inline(&cp->contents, (ref *) where);
        r_set_attrs((ref *) where, l_new);
499 500 501 502
    }
    mem->changes = cp;
#ifdef DEBUG
    if (gs_debug_c('U')) {
503 504
        dmlprintf1((const gs_memory_t *)mem, "[U]save(%s)", client_name_string(cname));
        alloc_save_print((const gs_memory_t *)mem, cp, false);
505 506 507 508 509 510
    }
#endif
    return 0;
}
int
alloc_save_change(gs_dual_memory_t * dmem, const ref * pcont,
511
                  ref_packed * where, client_name_t cname)
512 513
{
    gs_ref_memory_t *mem =
514 515
        (pcont == NULL ? dmem->space_local :
         dmem->spaces_indexed[r_space(pcont) >> r_space_shift]);
516 517 518 519 520 521

    return alloc_save_change_in(mem, pcont, where, cname);
}

/* Allocate a structure for recording an allocation event. */
int
522
alloc_save_change_alloc(gs_ref_memory_t *mem, client_name_t cname, alloc_change_t **pcp)
523 524 525 526
{
    register alloc_change_t *cp;

    if (mem->new_mask == 0)
527
        return 0;		/* no saving */
528
    cp = gs_alloc_struct((gs_memory_t *)mem, alloc_change_t,
529
                         &st_alloc_change, "alloc_save_change");
530
    if (cp == 0)
531
        return_error(gs_error_VMerror);
532 533 534 535
    cp->next = mem->changes;
    cp->where = 0;
    cp->offset = AC_OFFSET_ALLOCATED;
    make_null(&cp->contents);
536
    *pcp = cp;
537 538 539 540 541 542 543 544
    return 1;
}

/* Remove an AC_OFFSET_ALLOCATED element. */
void
alloc_save_remove(gs_ref_memory_t *mem, ref_packed *obj, client_name_t cname)
{
    alloc_change_t **cpp = &mem->changes;
545

546
    for (; *cpp != NULL;) {
547 548 549 550 551 552 553 554 555
        alloc_change_t *cp = *cpp;

        if (cp->offset == AC_OFFSET_ALLOCATED && cp->where == obj) {
            if (mem->scan_limit == cp)
                mem->scan_limit = cp->next;
            *cpp = cp->next;
            gs_free_object((gs_memory_t *)mem, cp, "alloc_save_remove");
        } else
            cpp = &(*cpp)->next;
556 557 558 559 560 561 562 563 564 565 566 567
    }
}

/* Filter save change lists. */
static inline void
alloc_save__filter_changes_in_space(gs_ref_memory_t *mem)
{
    /* This is a special function, which is called
       from the garbager after setting marks and before collecting
       unused space. Therefore it just resets marks for
       elements being released instead releasing them really. */
    alloc_change_t **cpp = &mem->changes;
568

569
    for (; *cpp != NULL; ) {
570 571 572 573 574 575 576 577 578 579 580 581
        alloc_change_t *cp = *cpp;

        if (cp->offset == AC_OFFSET_ALLOCATED && !check_l_mark(cp->where)) {
            obj_header_t *pre = (obj_header_t *)cp - 1;

            *cpp = cp->next;
            cp->where = 0;
            if (mem->scan_limit == cp)
                mem->scan_limit = cp->next;
            o_set_unmarked(pre);
        } else
            cpp = &(*cpp)->next;
582 583 584 585 586 587 588 589 590 591
    }
}

/* Filter save change lists. */
void
alloc_save__filter_changes(gs_ref_memory_t *memory)
{
    gs_ref_memory_t *mem = memory;

    for  (; mem; mem = &mem->saved->state)
592
        alloc_save__filter_changes_in_space(mem);
593 594 595 596 597 598 599 600 601 602
}

/* Return (the id of) the innermost externally visible save object, */
/* i.e., the innermost save with a non-zero ID. */
ulong
alloc_save_current_id(const gs_dual_memory_t * dmem)
{
    const alloc_save_t *save = dmem->space_local->saved;

    while (save != 0 && save->id == 0)
603
        save = save->state.saved;
604 605 606 607 608 609 610
    if (save)
        return save->id;

    /* This should never happen, if it does, return a totally
     * impossible value.
     */
    return (ulong)-1;
611 612 613 614 615 616 617 618 619 620 621
}
alloc_save_t *
alloc_save_current(const gs_dual_memory_t * dmem)
{
    return alloc_find_save(dmem, alloc_save_current_id(dmem));
}

/* Test whether a reference would be invalidated by a restore. */
bool
alloc_is_since_save(const void *vptr, const alloc_save_t * save)
{
622 623
    /* A reference postdates a save iff it is in a clump allocated */
    /* since the save (including any carried-over inner clumps). */
624 625

    const char *const ptr = (const char *)vptr;
626
    register gs_ref_memory_t *mem = save->space_local;
627

628 629
    if_debug2m('U', (gs_memory_t *)mem, "[U]is_since_save 0x%lx, 0x%lx:\n",
               (ulong) ptr, (ulong) save);
630
    if (mem->saved == 0) {	/* This is a special case, the final 'restore' from */
631 632
        /* alloc_restore_all. */
        return true;
633
    }
634
    /* Check against clumps allocated since the save. */
635 636
    /* (There may have been intermediate saves as well.) */
    for (;; mem = &mem->saved->state) {
637
        if_debug1m('U', (gs_memory_t *)mem, "[U]checking mem=0x%lx\n", (ulong) mem);
638 639 640
        if (ptr_is_within_mem_clumps(ptr, mem)) {
            if_debug0m('U', (gs_memory_t *)mem, "[U+]found\n");
            return true;
641
        }
642
        if_debug1m('U', (gs_memory_t *)mem, "[U-]not in any chunks belonging to 0x%lx\n", (ulong) mem);
643 644 645 646
        if (mem->saved == save) {	/* We've checked all the more recent saves, */
            /* must be OK. */
            break;
        }
647 648 649 650 651 652 653 654 655 656
    }

    /*
     * If we're about to do a global restore (a restore to the level 0),
     * and there is only one context using this global VM
     * (the normal case, in which global VM is saved by the
     * outermost save), we also have to check the global save.
     * Global saves can't be nested, which makes things easy.
     */
    if (save->state.save_level == 0 /* Restoring to save level 0 - see bug 688157, 688161 */ &&
657 658 659
        (mem = save->space_global) != save->space_local &&
        save->space_global->num_contexts == 1
        ) {
660
        if_debug1m('U', (gs_memory_t *)mem, "[U]checking global mem=0x%lx\n", (ulong) mem);
661 662 663 664
        if (ptr_is_within_mem_clumps(ptr, mem)) {
            if_debug0m('U', (gs_memory_t *)mem, "[U+]  found\n");
            return true;
        }
665 666 667 668 669 670 671 672 673
    }
    return false;

#undef ptr
}

/* Test whether a name would be invalidated by a restore. */
bool
alloc_name_is_since_save(const gs_memory_t *mem,
674
                         const ref * pnref, const alloc_save_t * save)
675 676 677 678
{
    const name_string_t *pnstr;

    if (!save->restore_names)
679
        return false;
680 681
    pnstr = names_string_inline(mem->gs_lib_ctx->gs_name_table, pnref);
    if (pnstr->foreign_string)
682
        return false;
683 684 685 686
    return alloc_is_since_save(pnstr->string_bytes, save);
}
bool
alloc_name_index_is_since_save(const gs_memory_t *mem,
687
                               uint nidx, const alloc_save_t *save)
688 689 690 691
{
    const name_string_t *pnstr;

    if (!save->restore_names)
692
        return false;
693 694
    pnstr = names_index_string_inline(mem->gs_lib_ctx->gs_name_table, nidx);
    if (pnstr->foreign_string)
695
        return false;
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
    return alloc_is_since_save(pnstr->string_bytes, save);
}

/* Check whether any names have been created since a given save */
/* that might be released by the restore. */
bool
alloc_any_names_since_save(const alloc_save_t * save)
{
    return save->restore_names;
}

/* Get the saved state with a given ID. */
alloc_save_t *
alloc_find_save(const gs_dual_memory_t * dmem, ulong sid)
{
    alloc_save_t *sprev = dmem->space_local->saved;

    if (sid == 0)
714
        return 0;		/* invalid id */
715
    while (sprev != 0) {
716 717 718
        if (sprev->id == sid)
            return sprev;
        sprev = sprev->state.saved;
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
    }
    return 0;
}

/* Get the client data from a saved state. */
void *
alloc_save_client_data(const alloc_save_t * save)
{
    return save->client_data;
}

/*
 * Do one step of restoring the state.  The client is responsible for
 * calling alloc_find_save to get the save object, and for ensuring that
 * there are no surviving pointers for which alloc_is_since_save is true.
 * Return true if the argument was the innermost save, in which case
 * this is the last (or only) step.
 * Note that "one step" may involve multiple internal steps,
 * if this is the outermost restore (which requires restoring both local
 * and global VM) or if we created extra save levels to reduce scanning.
 */
static void restore_finalize(gs_ref_memory_t *);
static void restore_space(gs_ref_memory_t *, gs_dual_memory_t *);

int
alloc_restore_step_in(gs_dual_memory_t *dmem, alloc_save_t * save)
{
    /* Get save->space_* now, because the save object will be freed. */
    gs_ref_memory_t *lmem = save->space_local;
    gs_ref_memory_t *gmem = save->space_global;
    gs_ref_memory_t *mem = lmem;
    alloc_save_t *sprev;
    int code;

    /* Finalize all objects before releasing resources or undoing changes. */
    do {
755 756 757 758 759 760 761 762
        ulong sid;

        sprev = mem->saved;
        sid = sprev->id;
        restore_finalize(mem);	/* finalize objects */
        mem = &sprev->state;
        if (sid != 0)
            break;
763 764 765
    }
    while (sprev != save);
    if (mem->save_level == 0) {
766 767 768
        /* This is the outermost save, which might also */
        /* need to restore global VM. */
        mem = gmem;
769
        if (mem != lmem && mem->saved != 0) {
770
            restore_finalize(mem);
771
        }
772 773 774 775 776
    }

    /* Do one (externally visible) step of restoring the state. */
    mem = lmem;
    do {
777 778 779 780 781 782 783 784 785 786
        ulong sid;

        sprev = mem->saved;
        sid = sprev->id;
        code = restore_resources(sprev, mem);	/* release other resources */
        if (code < 0)
            return code;
        restore_space(mem, dmem);	/* release memory */
        if (sid != 0)
            break;
787 788 789 790
    }
    while (sprev != save);

    if (mem->save_level == 0) {
791 792 793 794 795 796 797 798 799 800
        /* This is the outermost save, which might also */
        /* need to restore global VM. */
        mem = gmem;
        if (mem != lmem && mem->saved != 0) {
            code = restore_resources(mem->saved, mem);
            if (code < 0)
                return code;
            restore_space(mem, dmem);
        }
        alloc_set_not_in_save(dmem);
801
    } else {			/* Set the l_new attribute in all slots that are now new. */
802
        ulong scanned;
803

804 805 806
        code = save_set_new(mem, true, false, &scanned);
        if (code < 0)
            return code;
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
    }

    return sprev == save;
}
/* Restore the memory of one space, by undoing changes and freeing */
/* memory allocated since the save. */
static void
restore_space(gs_ref_memory_t * mem, gs_dual_memory_t *dmem)
{
    alloc_save_t *save = mem->saved;
    alloc_save_t saved;

    print_save("restore", mem->space, save);

    /* Undo changes since the save. */
    {
823
        register alloc_change_t *cp = mem->changes;
824

825
        while (cp) {
826
#ifdef DEBUG
827
            if (gs_debug_c('U')) {
828 829
                dmlputs((const gs_memory_t *)mem, "[U]restore");
                alloc_save_print((const gs_memory_t *)mem, cp, true);
830
            }
831
#endif
832 833 834 835 836 837 838 839 840
            if (cp->offset == AC_OFFSET_ALLOCATED)
                DO_NOTHING;
            else
            if (r_is_packed(&cp->contents))
                *cp->where = *(ref_packed *) & cp->contents;
            else
                ref_assign_inline((ref *) cp->where, &cp->contents);
            cp = cp->next;
        }
841 842 843
    }

    /* Free memory allocated since the save. */
844
    /* Note that this frees all clumps except the inner ones */
845 846 847 848 849 850
    /* belonging to this level. */
    saved = *save;
    restore_free(mem);

    /* Restore the allocator state. */
    {
851
        int num_contexts = mem->num_contexts;	/* don't restore */
852

853 854
        *mem = saved.state;
        mem->num_contexts = num_contexts;
855
    }
856
    alloc_open_clump(mem);
857 858 859

    /* Make the allocator current if it was current before the save. */
    if (saved.is_current) {
860 861
        dmem->current = mem;
        dmem->current_space = mem->space;
862 863 864 865 866 867
    }
}

/* Restore to the initial state, releasing all resources. */
/* The allocator is no longer usable after calling this routine! */
int
868
alloc_restore_all(i_ctx_t *i_ctx_p)
869 870 871 872 873
{
    /*
     * Save the memory pointers, since freeing space_local will also
     * free dmem itself.
     */
874 875 876 877
    gs_ref_memory_t *lmem = idmemory->space_local;
    gs_ref_memory_t *gmem = idmemory->space_global;
    gs_ref_memory_t *smem = idmemory->space_system;

878 879 880 881 882
    gs_ref_memory_t *mem;
    int code;

    /* Restore to a state outside any saves. */
    while (lmem->save_level != 0) {
883 884 885 886 887
        vm_save_t *vmsave = alloc_save_client_data(alloc_save_current(idmemory));
        gs_grestoreall_for_restore(i_ctx_p->pgs, vmsave->gsave);
        vmsave->gsave = 0;
        code = alloc_restore_step_in(idmemory, lmem->saved);

888 889
        if (code < 0)
            return code;
890 891 892 893 894
    }

    /* Finalize memory. */
    restore_finalize(lmem);
    if ((mem = (gs_ref_memory_t *)lmem->stable_memory) != lmem)
895
        restore_finalize(mem);
896
    if (gmem != lmem && gmem->num_contexts == 1) {
897 898 899
        restore_finalize(gmem);
        if ((mem = (gs_ref_memory_t *)gmem->stable_memory) != gmem)
            restore_finalize(mem);
900 901 902 903 904 905
    }
    restore_finalize(smem);

    /* Release resources other than memory, using fake */
    /* save and memory objects. */
    {
906
        alloc_save_t empty_save;
907

908
        empty_save.spaces = idmemory->spaces;
909 910 911 912
        empty_save.restore_names = false;	/* don't bother to release */
        code = restore_resources(&empty_save, NULL);
        if (code < 0)
            return code;
913 914 915 916 917
    }

    /* Finally, release memory. */
    restore_free(lmem);
    if ((mem = (gs_ref_memory_t *)lmem->stable_memory) != lmem)
918
        restore_free(mem);
919
    if (gmem != lmem) {
920 921 922 923 924
        if (!--(gmem->num_contexts)) {
            restore_free(gmem);
            if ((mem = (gs_ref_memory_t *)gmem->stable_memory) != gmem)
                restore_free(mem);
        }
925 926 927 928 929 930 931 932 933 934 935 936 937
    }
    restore_free(smem);
    return 0;
}

/*
 * Finalize objects that will be freed by a restore.
 * Note that we must temporarily disable the freeing operations
 * of the allocator while doing this.
 */
static void
restore_finalize(gs_ref_memory_t * mem)
{
938 939
    clump_t *cp;
    clump_splay_walker sw;
940

941
    alloc_close_clump(mem);
942
    gs_enable_free((gs_memory_t *) mem, false);
943 944
    for (cp = clump_splay_walk_bwd_init(&sw, mem); cp != 0; cp = clump_splay_walk_bwd(&sw)) {
        SCAN_CLUMP_OBJECTS(cp)
945 946 947 948
            DO_ALL
            struct_proc_finalize((*finalize)) =
            pre->o_type->finalize;
        if (finalize != 0) {
949 950 951
            if_debug2m('u', (gs_memory_t *)mem, "[u]restore finalizing %s 0x%lx\n",
                       struct_type_name_string(pre->o_type),
                       (ulong) (pre + 1));
952
            (*finalize) ((gs_memory_t *) mem, pre + 1);
953 954
        }
        END_OBJECTS_SCAN
955 956 957 958 959 960 961 962 963 964 965
    }
    gs_enable_free((gs_memory_t *) mem, true);
}

/* Release resources for a restore */
static int
restore_resources(alloc_save_t * sprev, gs_ref_memory_t * mem)
{
    int code;
#ifdef DEBUG
    if (mem) {
966
        /* Note restoring of the file list. */
967 968 969
        if_debug4m('u', (gs_memory_t *)mem, "[u%u]file_restore 0x%lx => 0x%lx for 0x%lx\n",
                   mem->space, (ulong)mem->streams,
                   (ulong)sprev->state.streams, (ulong) sprev);
970 971 972 973 974 975
    }
#endif

    /* Remove entries from font and character caches. */
    code = font_restore(sprev);
    if (code < 0)
976
        return code;
977 978 979

    /* Adjust the name table. */
    if (sprev->restore_names)
980
        names_restore(mem->gs_lib_ctx->gs_name_table, sprev);
981 982 983 984 985 986 987
    return 0;
}

/* Release memory for a restore. */
static void
restore_free(gs_ref_memory_t * mem)
{
988
    /* Free clumps allocated since the save. */
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
    gs_free_all((gs_memory_t *) mem);
}

/* Forget a save, by merging this level with the next outer one. */
static void file_forget_save(gs_ref_memory_t *);
static void combine_space(gs_ref_memory_t *);
static void forget_changes(gs_ref_memory_t *);
int
alloc_forget_save_in(gs_dual_memory_t *dmem, alloc_save_t * save)
{
    gs_ref_memory_t *mem = save->space_local;
    alloc_save_t *sprev;
    ulong scanned;
    int code;

    print_save("forget_save", mem->space, save);

    /* Iteratively combine the current level with the previous one. */
    do {
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
        sprev = mem->saved;
        if (sprev->id != 0)
            mem->save_level--;
        if (mem->save_level != 0) {
            alloc_change_t *chp = mem->changes;

            code = save_set_new(&sprev->state, true, false, &scanned);
            if (code < 0)
                return code;
            /* Concatenate the changes chains. */
            if (chp == 0)
                mem->changes = sprev->state.changes;
            else {
                while (chp->next != 0)
                    chp = chp->next;
                chp->next = sprev->state.changes;
            }
            file_forget_save(mem);
            combine_space(mem);	/* combine memory */
        } else {
            forget_changes(mem);
            code = save_set_new(mem, false, false, &scanned);
            if (code < 0)
                return code;
            file_forget_save(mem);
            combine_space(mem);	/* combine memory */
            /* This is the outermost save, which might also */
            /* need to combine global VM. */
            mem = save->space_global;
            if (mem != save->space_local && mem->saved != 0) {
                forget_changes(mem);
                code = save_set_new(mem, false, false, &scanned);
                if (code < 0)
                    return code;
                file_forget_save(mem);
                combine_space(mem);
            }
            alloc_set_not_in_save(dmem);
            break;		/* must be outermost */
        }
1048 1049 1050 1051
    }
    while (sprev != save);
    return 0;
}
1052
/* Combine the clumps of the next outer level with those of the current one, */
1053 1054 1055 1056 1057 1058
/* and free the bookkeeping structures. */
static void
combine_space(gs_ref_memory_t * mem)
{
    alloc_save_t *saved = mem->saved;
    gs_ref_memory_t *omem = &saved->state;
1059 1060
    clump_t *cp;
    clump_splay_walker sw;
1061

1062 1063
    alloc_close_clump(mem);
    for (cp = clump_splay_walk_init(&sw, mem); cp != 0; cp = clump_splay_walk_fwd(&sw)) {
1064
        if (cp->outer == 0)
1065
            alloc_link_clump(cp, omem);
1066
        else {
1067
            clump_t *outer = cp->outer;
1068 1069

            outer->inner_count--;
1070 1071
            if (mem->cc == cp)
                mem->cc = outer;
1072 1073
            if (mem->cfreed.cp == cp)
                mem->cfreed.cp = outer;
1074
            /* "Free" the header of the inner clump, */
1075
            /* and any immediately preceding gap left by */
1076
            /* the GC having compacted the outer clump. */
1077 1078 1079
            {
                obj_header_t *hp = (obj_header_t *) outer->cbot;

1080
                hp->o_pad = 0;
1081 1082 1083 1084 1085
                hp->o_alone = 0;
                hp->o_size = (char *)(cp->chead + 1)
                    - (char *)(hp + 1);
                hp->o_type = &st_bytes;
                /* The following call is probably not safe. */
1086
#if 0				/* **************** */
1087 1088
                gs_free_object((gs_memory_t *) mem,
                               hp + 1, "combine_space(header)");
1089
#endif /* **************** */
1090
            }
1091
            /* Update the outer clump's allocation pointers. */
1092 1093 1094 1095 1096 1097 1098 1099
            outer->cbot = cp->cbot;
            outer->rcur = cp->rcur;
            outer->rtop = cp->rtop;
            outer->ctop = cp->ctop;
            outer->has_refs |= cp->has_refs;
            gs_free_object(mem->non_gc_memory, cp,
                           "combine_space(inner)");
        }
1100 1101
    }
    /* Update relevant parts of allocator state. */
1102
    mem->root = omem->root;
1103 1104 1105 1106 1107 1108 1109 1110
    mem->allocated += omem->allocated;
    mem->gc_allocated += omem->allocated;
    mem->lost.objects += omem->lost.objects;
    mem->lost.refs += omem->lost.refs;
    mem->lost.strings += omem->lost.strings;
    mem->saved = omem->saved;
    mem->previous_status = omem->previous_status;
    {				/* Concatenate free lists. */
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
        int i;

        for (i = 0; i < num_freelists; i++) {
            obj_header_t *olist = omem->freelists[i];
            obj_header_t *list = mem->freelists[i];

            if (olist == 0);
            else if (list == 0)
                mem->freelists[i] = olist;
            else {
                while (*(obj_header_t **) list != 0)
                    list = *(obj_header_t **) list;
                *(obj_header_t **) list = olist;
            }
        }
        if (omem->largest_free_size > mem->largest_free_size)
            mem->largest_free_size = omem->largest_free_size;
1128 1129
    }
    gs_free_object((gs_memory_t *) mem, saved, "combine_space(saved)");
1130
    alloc_open_clump(mem);
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
}
/* Free the changes chain for a level 0 .forgetsave, */
/* resetting the l_new flag in the changed refs. */
static void
forget_changes(gs_ref_memory_t * mem)
{
    register alloc_change_t *chp = mem->changes;
    alloc_change_t *next;

    for (; chp; chp = next) {
1141 1142
        ref_packed *prp = chp->where;

1143
        if_debug1m('U', (gs_memory_t *)mem, "[U]forgetting change 0x%lx\n", (ulong) chp);
1144 1145 1146 1147 1148 1149 1150
        if (chp->offset == AC_OFFSET_ALLOCATED)
            DO_NOTHING;
        else
        if (!r_is_packed(prp))
            r_clear_attrs((ref *) prp, l_new);
        next = chp->next;
        gs_free_object((gs_memory_t *) mem, chp, "forget_changes");
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
    }
    mem->changes = 0;
}
/* Update the streams list when forgetting a save. */
static void
file_forget_save(gs_ref_memory_t * mem)
{
    const alloc_save_t *save = mem->saved;
    stream *streams = mem->streams;
    stream *saved_streams = save->state.streams;

1162 1163 1164
    if_debug4m('u', (gs_memory_t *)mem, "[u%d]file_forget_save 0x%lx + 0x%lx for 0x%lx\n",
               mem->space, (ulong) streams, (ulong) saved_streams,
               (ulong) save);
1165
    if (streams == 0)
1166
        mem->streams = saved_streams;
1167
    else if (saved_streams != 0) {
1168 1169 1170 1171
        while (streams->next != 0)
            streams = streams->next;
        streams->next = saved_streams;
        saved_streams->prev = streams;
1172 1173 1174 1175 1176
    }
}

static inline int
mark_allocated(void *obj, bool to_new, uint *psize)
1177
{
1178 1179 1180 1181 1182
    obj_header_t *pre = (obj_header_t *)obj - 1;
    uint size = pre_obj_contents_size(pre);
    ref_packed *prp = (ref_packed *) (pre + 1);
    ref_packed *next = (ref_packed *) ((char *)prp + size);
#ifdef ALIGNMENT_ALIASING_BUG
1183
                ref *rpref;
1184 1185 1186 1187 1188 1189
# define RP_REF(rp) (rpref = (ref *)rp, rpref)
#else
# define RP_REF(rp) ((ref *)rp)
#endif

    if (pre->o_type != &st_refs) {
1190 1191
        /* Must not happen. */
        if_debug0('u', "Wrong object type when expected a ref.\n");
1192
        return_error(gs_error_Fatal);
1193 1194 1195 1196 1197
    }
    /* We know that every block of refs ends with */
    /* a full-size ref, so we only need the end check */
    /* when we encounter one of those. */
    if (to_new)
1198 1199 1200 1201 1202 1203 1204 1205 1206
        while (1) {
            if (r_is_packed(prp))
                prp++;
            else {
                RP_REF(prp)->tas.type_attrs |= l_new;
                prp += packed_per_ref;
                if (prp >= next)
                    break;
            }
1207
    } else
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
        while (1) {
            if (r_is_packed(prp))
                prp++;
            else {
                RP_REF(prp)->tas.type_attrs &= ~l_new;
                prp += packed_per_ref;
                if (prp >= next)
                    break;
            }
        }
1218 1219 1220 1221 1222 1223 1224 1225
#undef RP_REF
    *psize = size;
    return 0;
}

/* Check if a block contains refs marked by garbager. */
static bool
check_l_mark(void *obj)
1226
{
1227 1228 1229 1230 1231
    obj_header_t *pre = (obj_header_t *)obj - 1;
    uint size = pre_obj_contents_size(pre);
    ref_packed *prp = (ref_packed *) (pre + 1);
    ref_packed *next = (ref_packed *) ((char *)prp + size);
#ifdef ALIGNMENT_ALIASING_BUG
1232
                ref *rpref;
1233 1234 1235 1236 1237 1238 1239 1240 1241
# define RP_REF(rp) (rpref = (ref *)rp, rpref)
#else
# define RP_REF(rp) ((ref *)rp)
#endif

    /* We know that every block of refs ends with */
    /* a full-size ref, so we only need the end check */
    /* when we encounter one of those. */
    while (1) {
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
        if (r_is_packed(prp)) {
            if (r_has_pmark(prp))
                return true;
            prp++;
        } else {
            if (r_has_attr(RP_REF(prp), l_mark))
                return true;
            prp += packed_per_ref;
            if (prp >= next)
                return false;
        }
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
    }
#undef RP_REF
}

/* Set or reset the l_new attribute in every relevant slot. */
/* This includes every slot on the current change chain, */
/* and every (ref) slot allocated at this save level. */
/* Return the number of bytes of data scanned. */
static int
save_set_new(gs_ref_memory_t * mem, bool to_new, bool set_limit, ulong *pscanned)
{
    ulong scanned = 0;
    int code;

    /* Handle the change chain. */
    code = save_set_new_changes(mem, to_new, set_limit);
    if (code < 0)
1270
        return code;
1271 1272

    /* Handle newly allocated ref objects. */
1273
    SCAN_MEM_CLUMPS(mem, cp) {
1274 1275 1276
        if (cp->has_refs) {
            bool has_refs = false;

1277
            SCAN_CLUMP_OBJECTS(cp)
1278
                DO_ALL
1279 1280
                if_debug3m('U', (gs_memory_t *)mem, "[U]set_new scan(0x%lx(%u), %d)\n",
                           (ulong) pre, size, to_new);
1281 1282 1283 1284
            if (pre->o_type == &st_refs) {
                /* These are refs, scan them. */
                ref_packed *prp = (ref_packed *) (pre + 1);
                uint size;
1285
                has_refs = true && to_new;
1286 1287 1288 1289 1290 1291 1292 1293 1294
                code = mark_allocated(prp, to_new, &size);
                if (code < 0)
                    return code;
                scanned += size;
            } else
                scanned += sizeof(obj_header_t);
            END_OBJECTS_SCAN
                cp->has_refs = has_refs;
        }
1295
    }
1296
    END_CLUMPS_SCAN
1297 1298
    if_debug2m('u', (gs_memory_t *)mem, "[u]set_new (%s) scanned %ld\n",
               (to_new ? "restore" : "save"), scanned);
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
    *pscanned = scanned;
    return 0;
}

/* Drop redundant elements from the changes list and set l_new. */
static void
drop_redundant_changes(gs_ref_memory_t * mem)
{
    register alloc_change_t *chp = mem->changes, *chp_back = NULL, *chp_forth;

1309
    /* As we are trying to throw away redundant changes in an allocator instance
1310
       that has already been "saved", the active clump has already been "closed"
1311 1312
       by alloc_save_space(). Using such an allocator (for example, by calling
       gs_free_object() with it) can leave it in an unstable state, causing
1313 1314
       problems for the garbage collector (specifically, the clump validator code).
       So, before we might use it, open the current clump, and then close it again
1315 1316
       when we're done.
     */
1317
    alloc_open_clump(mem);
1318

1319 1320
    /* First reverse the list and set all. */
    for (; chp; chp = chp_forth) {
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
        chp_forth = chp->next;
        if (chp->offset != AC_OFFSET_ALLOCATED) {
            ref_packed *prp = chp->where;

            if (!r_is_packed(prp)) {
                ref *const rp = (ref *)prp;

                rp->tas.type_attrs |= l_new;
            }
        }
        chp->next = chp_back;
        chp_back = chp;
1333 1334 1335 1336 1337
    }
    mem->changes = chp_back;
    chp_back = NULL;
    /* Then filter, reset and reverse again. */
    for (chp = mem->changes; chp; chp = chp_forth) {
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
        chp_forth = chp->next;
        if (chp->offset != AC_OFFSET_ALLOCATED) {
            ref_packed *prp = chp->where;

            if (!r_is_packed(prp)) {
                ref *const rp = (ref *)prp;

                if ((rp->tas.type_attrs & l_new) == 0) {
                    if (mem->scan_limit == chp)
                        mem->scan_limit = chp_back;
                    if (mem->changes == chp)
                        mem->changes = chp_back;
                    gs_free_object((gs_memory_t *)mem, chp, "alloc_save_remove");
                    continue;
                } else
                    rp->tas.type_attrs &= ~l_new;
            }
        }
        chp->next = chp_back;
        chp_back = chp;
1358 1359
    }
    mem->changes = chp_back;
1360

1361
    alloc_close_clump(mem);
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
}

/* Set or reset the l_new attribute on the changes chain. */
static int
save_set_new_changes(gs_ref_memory_t * mem, bool to_new, bool set_limit)
{
    register alloc_change_t *chp;
    register uint new = (to_new ? l_new : 0);
    ulong scanned = 0;

    if (!to_new && mem->total_scanned_after_compacting > max_repeated_scan * 16) {
1373 1374 1375
        mem->total_scanned_after_compacting = 0;
        drop_redundant_changes(mem);
    }
1376
    for (chp = mem->changes; chp; chp = chp->next) {
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
        if (chp->offset == AC_OFFSET_ALLOCATED) {
            if (chp->where != 0) {
                uint size;
                int code = mark_allocated((void *)chp->where, to_new, &size);

                if (code < 0)
                    return code;
                scanned += size;
            }
        } else {
            ref_packed *prp = chp->where;

1389 1390
            if_debug3m('U', (gs_memory_t *)mem, "[U]set_new 0x%lx: (0x%lx, %d)\n",
                       (ulong)chp, (ulong)prp, new);
1391 1392 1393 1394 1395 1396 1397 1398 1399
            if (!r_is_packed(prp)) {
                ref *const rp = (ref *) prp;

                rp->tas.type_attrs =
                    (rp->tas.type_attrs & ~l_new) + new;
            }
        }
        if (mem->scan_limit == chp)
            break;
1400 1401 1402
    }
    if (set_limit) {
        mem->total_scanned_after_compacting += scanned;
1403 1404 1405 1406 1407
        if (scanned  + mem->total_scanned >= max_repeated_scan) {
            mem->scan_limit = mem->changes;
            mem->total_scanned = 0;
        } else
            mem->total_scanned += scanned;
1408 1409 1410
    }
    return 0;
}
1411 1412 1413 1414 1415 1416

gs_memory_t *
gs_save_any_memory(const alloc_save_t *save)
{
    return((gs_memory_t *)save->space_local);
}