Coverage Report

Created: 2026-10-28 08:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/psi/isave.c
Line
Count
Source
1
/* Copyright (C) 2001-2026 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
17
/* Save/restore manager for Ghostscript interpreter */
18
#include "ghost.h"
19
#include "memory_.h"
20
#include "ierrors.h"
21
#include "gsexit.h"
22
#include "gsstruct.h"
23
#include "iastate.h"
24
#include "inamedef.h"
25
#include "iname.h"
26
#include "ipacked.h"
27
#include "isave.h"
28
#include "isstate.h"
29
#include "gsstate.h"
30
#include "store.h"    /* for ref_assign */
31
#include "ivmspace.h"
32
#include "igc.h"
33
#include "gsutil.h"   /* gs_next_ids prototype */
34
#include "icstate.h"
35
#include "assert.h"
36
37
/* Structure descriptor */
38
private_st_alloc_save();
39
40
/* Define the maximum amount of data we are willing to scan repeatedly -- */
41
/* see below for details. */
42
static const long max_repeated_scan = 100000;
43
44
/* Define the minimum space for creating an inner clump. */
45
/* Must be at least sizeof(clump_head_t). */
46
static const long min_inner_clump_space = sizeof(clump_head_t) + 500;
47
48
/*
49
 * The logic for saving and restoring the state is complex.
50
 * Both the changes to individual objects, and the overall state
51
 * of the memory manager, must be saved and restored.
52
 */
53
54
/*
55
 * To save the state of the memory manager:
56
 *      Save the state of the current clump in which we are allocating.
57
 *      Shrink all clumps to their inner unallocated region.
58
 *      Save and reset the free block chains.
59
 * By doing this, we guarantee that no object older than the save
60
 * can be freed.
61
 *
62
 * To restore the state of the memory manager:
63
 *      Free all clumps newer than the save, and the descriptors for
64
 *        the inner clumps created by the save.
65
 *      Make current the clump that was current at the time of the save.
66
 *      Restore the state of the current clump.
67
 *
68
 * In addition to save ("start transaction") and restore ("abort transaction"),
69
 * we support forgetting a save ("commit transation").  To forget a save:
70
 *      Reassign to the next outer save all clumps newer than the save.
71
 *      Free the descriptors for the inners clump, updating their outer
72
 *        clumps to reflect additional allocations in the inner clumps.
73
 *      Concatenate the free block chains with those of the outer save.
74
 */
75
76
/*
77
 * For saving changes to individual objects, we add an "attribute" bit
78
 * (l_new) that logically belongs to the slot where the ref is stored,
79
 * not to the ref itself.  The bit means "the contents of this slot
80
 * have been changed, or the slot was allocated, since the last save."
81
 * To keep track of changes since the save, we associate a chain of
82
 * <slot, old_contents> pairs that remembers the old contents of slots.
83
 *
84
 * When creating an object, if the save level is non-zero:
85
 *      Set l_new in all slots.
86
 *
87
 * When storing into a slot, if the save level is non-zero:
88
 *      If l_new isn't set, save the address and contents of the slot
89
 *        on the current contents chain.
90
 *      Set l_new after storing the new value.
91
 *
92
 * To do a save:
93
 *      If the save level is non-zero:
94
 *              Reset l_new in all slots on the contents chain, and in all
95
 *                objects created since the previous save.
96
 *      Push the head of the contents chain, and reset the chain to empty.
97
 *
98
 * To do a restore:
99
 *      Check all the stacks to make sure they don't contain references
100
 *        to objects created since the save.
101
 *      Restore all the slots on the contents chain.
102
 *      Pop the contents chain head.
103
 *      If the save level is now non-zero:
104
 *              Scan the newly restored contents chain, and set l_new in all
105
 *                the slots it references.
106
 *              Scan all objects created since the previous save, and set
107
 *                l_new in all the slots of each object.
108
 *
109
 * To forget a save:
110
 *      If the save level is greater than 1:
111
 *              Set l_new as for a restore, per the next outer save.
112
 *              Concatenate the next outer contents chain to the end of
113
 *                the current one.
114
 *      If the save level is 1:
115
 *              Reset l_new as for a save.
116
 *              Free the contents chain.
117
 */
118
119
/*
120
 * A consequence of the foregoing algorithms is that the cost of a save is
121
 * proportional to the total amount of data allocated since the previous
122
 * save.  If a PostScript program reads in a large amount of setup code and
123
 * then uses save/restore heavily, each save/restore will be expensive.  To
124
 * mitigate this, we check to see how much data we have scanned at this save
125
 * level: if it is large, we do a second, invisible save.  This greatly
126
 * reduces the cost of inner saves, at the expense of possibly saving some
127
 * changes twice that otherwise would only have to be saved once.
128
 */
129
130
/*
131
 * The presence of global and local VM complicates the situation further.
132
 * There is a separate save chain and contents chain for each VM space.
133
 * When multiple contexts are fully implemented, save and restore will have
134
 * the following effects, according to the privacy status of the current
135
 * context's global and local VM:
136
 *      Private global, private local:
137
 *              The outermost save saves both global and local VM;
138
 *                otherwise, save only saves local VM.
139
 *      Shared global, private local:
140
 *              Save only saves local VM.
141
 *      Shared global, shared local:
142
 *              Save only saves local VM, and suspends all other contexts
143
 *                sharing the same local VM until the matching restore.
144
 * Since we do not currently implement multiple contexts, only the first
145
 * case is relevant.
146
 *
147
 * Note that when saving the contents of a slot, the choice of chain
148
 * is determined by the VM space in which the slot is allocated,
149
 * not by the current allocation mode.
150
 */
151
152
/* Tracing printout */
153
static void
154
print_save(const char *str, uint spacen, const alloc_save_t *sav)
155
2.34M
{
156
2.34M
  if_debug5('u', "[u]%s space %u "PRI_INTPTR": cdata = "PRI_INTPTR", id = %lu\n",\
157
2.34M
            str, spacen, (intptr_t)sav, (intptr_t)sav->client_data, (ulong)sav->id);
158
2.34M
}
159
160
/* A link to igcref.c . */
161
ptr_proc_reloc(igc_reloc_ref_ptr_nocheck, ref_packed);
162
163
static
164
CLEAR_MARKS_PROC(change_clear_marks)
165
43.0M
{
166
43.0M
    alloc_change_t *const ptr = (alloc_change_t *)vptr;
167
168
43.0M
    if (r_is_packed(&ptr->contents))
169
376k
        r_clear_pmark((ref_packed *) & ptr->contents);
170
42.7M
    else
171
42.7M
        r_clear_attrs(&ptr->contents, l_mark);
172
43.0M
}
173
static
174
171M
ENUM_PTRS_WITH(change_enum_ptrs, alloc_change_t *ptr) return 0;
175
42.7M
ENUM_PTR(0, alloc_change_t, next);
176
42.7M
case 1:
177
42.7M
    if (ptr->offset >= 0)
178
2
        ENUM_RETURN((byte *) ptr->where - ptr->offset);
179
42.7M
    else
180
42.7M
        if (ptr->offset != AC_OFFSET_ALLOCATED)
181
24.2M
            ENUM_RETURN_REF(ptr->where);
182
18.5M
        else {
183
            /* Don't enumerate ptr->where, because it
184
               needs a special processing with
185
               alloc_save__filter_changes. */
186
18.5M
            ENUM_RETURN(0);
187
18.5M
        }
188
42.7M
case 2:
189
42.7M
    ENUM_RETURN_REF(&ptr->contents);
190
171M
ENUM_PTRS_END
191
26.8M
static RELOC_PTRS_WITH(change_reloc_ptrs, alloc_change_t *ptr)
192
26.8M
{
193
26.8M
    RELOC_VAR(ptr->next);
194
26.8M
    switch (ptr->offset) {
195
0
        case AC_OFFSET_STATIC:
196
0
            break;
197
24.2M
        case AC_OFFSET_REF:
198
24.2M
            RELOC_REF_PTR_VAR(ptr->where);
199
24.2M
            break;
200
2.59M
        case AC_OFFSET_ALLOCATED:
201
            /* We know that ptr->where may point to an unmarked object
202
               because change_enum_ptrs skipped it,
203
               and we know it always points to same space
204
               because we took a special care when calling alloc_save_change_alloc.
205
               Therefore we must skip the check for the mark,
206
               which would happen if we call the regular relocation function
207
               igc_reloc_ref_ptr from RELOC_REF_PTR_VAR.
208
               Calling igc_reloc_ref_ptr_nocheck instead. */
209
2.59M
            { /* A sanity check. */
210
2.59M
                obj_header_t *pre = (obj_header_t *)ptr->where - 1;
211
212
2.59M
                if (pre->o_type != &st_refs)
213
0
                    gs_abort(gcst->heap);
214
2.59M
            }
215
2.59M
            if (ptr->where != 0 && !gcst->relocating_untraced)
216
2.30M
                ptr->where = igc_reloc_ref_ptr_nocheck(ptr->where, gcst);
217
2.59M
            break;
218
2
        default:
219
2
            {
220
2
                byte *obj = (byte *) ptr->where - ptr->offset;
221
222
2
                RELOC_VAR(obj);
223
2
                ptr->where = (ref_packed *) (obj + ptr->offset);
224
2
            }
225
2
            break;
226
26.8M
    }
227
26.8M
    if (r_is_packed(&ptr->contents))
228
375k
        r_clear_pmark((ref_packed *) & ptr->contents);
229
26.4M
    else {
230
26.4M
        RELOC_REF_VAR(ptr->contents);
231
26.4M
        r_clear_attrs(&ptr->contents, l_mark);
232
26.4M
    }
233
26.8M
}
234
26.8M
RELOC_PTRS_END
235
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
/* Debugging printout */
239
#ifdef DEBUG
240
static void
241
alloc_save_print(const gs_memory_t *mem, alloc_change_t * cp, bool print_current)
242
{
243
    dmprintf2(mem, " "PRI_INTPTR"x: "PRI_INTPTR": ", (intptr_t) cp, (intptr_t) cp->where);
244
    if (r_is_packed(&cp->contents)) {
245
        if (print_current)
246
            dmprintf2(mem, "saved=%x cur=%x\n", *(ref_packed *) & cp->contents,
247
                      *cp->where);
248
        else
249
            dmprintf1(mem, "%x\n", *(ref_packed *) & cp->contents);
250
    } else {
251
        if (print_current)
252
            dmprintf6(mem, "saved=%x %x %lx cur=%x %x %lx\n",
253
                      r_type_attrs(&cp->contents), r_size(&cp->contents),
254
                      (ulong) cp->contents.value.intval,
255
                      r_type_attrs((ref *) cp->where),
256
                      r_size((ref *) cp->where),
257
                      (ulong) ((ref *) cp->where)->value.intval);
258
        else
259
            dmprintf3(mem, "%x %x %lx\n",
260
                      r_type_attrs(&cp->contents), r_size(&cp->contents),
261
                      (ulong) cp->contents.value.intval);
262
    }
263
}
264
#endif
265
266
/* Forward references */
267
static int  restore_resources(alloc_save_t *, gs_ref_memory_t *);
268
static void restore_free(gs_ref_memory_t *);
269
static int  save_set_new(gs_ref_memory_t * mem, bool to_new, bool set_limit, ulong *pscanned);
270
static int  save_set_new_changes(gs_ref_memory_t *, bool, bool);
271
static bool check_l_mark(void *obj);
272
273
/* Initialize the save/restore machinery. */
274
void
275
alloc_save_init(gs_dual_memory_t * dmem)
276
162k
{
277
162k
    alloc_set_not_in_save(dmem);
278
162k
}
279
280
/* Record that we are in a save. */
281
static void
282
alloc_set_masks(gs_dual_memory_t *dmem, uint new_mask, uint test_mask)
283
2.00M
{
284
2.00M
    int i;
285
2.00M
    gs_ref_memory_t *mem;
286
287
2.00M
    dmem->new_mask = new_mask;
288
2.00M
    dmem->test_mask = test_mask;
289
10.0M
    for (i = 0; i < countof(dmem->spaces.memories.indexed); ++i)
290
8.01M
        if ((mem = dmem->spaces.memories.indexed[i]) != 0) {
291
6.00M
            mem->new_mask = new_mask, mem->test_mask = test_mask;
292
6.00M
            if (mem->stable_memory != (gs_memory_t *)mem) {
293
4.00M
                mem = (gs_ref_memory_t *)mem->stable_memory;
294
4.00M
                mem->new_mask = new_mask, mem->test_mask = test_mask;
295
4.00M
            }
296
6.00M
        }
297
2.00M
}
298
void
299
alloc_set_in_save(gs_dual_memory_t *dmem)
300
1.18M
{
301
1.18M
    alloc_set_masks(dmem, l_new, l_new);
302
1.18M
}
303
304
/* Record that we are not in a save. */
305
void
306
alloc_set_not_in_save(gs_dual_memory_t *dmem)
307
822k
{
308
822k
    alloc_set_masks(dmem, 0, ~0);
309
822k
}
310
311
/* Save the state. */
312
static alloc_save_t *alloc_save_space(gs_ref_memory_t *mem,
313
                                       gs_dual_memory_t *dmem,
314
                                       ulong sid);
315
static void
316
alloc_free_save(gs_ref_memory_t *mem, alloc_save_t *save, const char *scn)
317
0
{
318
0
    gs_ref_memory_t save_mem;
319
0
    save_mem = mem->saved->state;
320
0
    gs_free_object((gs_memory_t *)mem, save, scn);
321
    /* Free any inner clump structures.  This is the easiest way to do it. */
322
0
    restore_free(mem);
323
    /* Restore the 'saved' state - this pulls our object off the linked
324
     * list of states. Without this we hit a SEGV in the gc later. */
325
0
    *mem = save_mem;
326
0
}
327
int
328
alloc_save_state(gs_dual_memory_t * dmem, void *cdata, ulong *psid)
329
1.00M
{
330
1.00M
    gs_ref_memory_t *lmem = dmem->space_local;
331
1.00M
    gs_ref_memory_t *gmem = dmem->space_global;
332
1.00M
    ulong sid = gs_next_ids((const gs_memory_t *)lmem->stable_memory, 2);
333
1.00M
    bool global =
334
1.00M
        lmem->save_level == 0 && gmem != lmem &&
335
164k
        gmem->num_contexts == 1;
336
1.00M
    alloc_save_t *gsave =
337
1.00M
        (global ? alloc_save_space(gmem, dmem, sid + 1) : (alloc_save_t *) 0);
338
1.00M
    alloc_save_t *lsave = alloc_save_space(lmem, dmem, sid);
339
340
1.00M
    if (lsave == 0 || (global && gsave == 0)) {
341
        /* Only 1 of lsave or gsave will have been allocated, but
342
         * nevertheless (in case things change in future), we free
343
         * lsave, then gsave, so they 'pop' correctly when restoring
344
         * the mem->saved states. */
345
1
        if (lsave != 0)
346
0
            alloc_free_save(lmem, lsave, "alloc_save_state(local save)");
347
1
        if (gsave != 0)
348
0
            alloc_free_save(gmem, gsave, "alloc_save_state(global save)");
349
1
        return_error(gs_error_VMerror);
350
1
    }
351
1.00M
    if (gsave != 0) {
352
164k
        gsave->client_data = 0;
353
164k
        print_save("save", gmem->space, gsave);
354
        /* Restore names when we do the local restore. */
355
164k
        lsave->restore_names = gsave->restore_names;
356
164k
        gsave->restore_names = false;
357
164k
    }
358
1.00M
    lsave->id = sid;
359
1.00M
    lsave->client_data = cdata;
360
1.00M
    print_save("save", lmem->space, lsave);
361
    /* Reset the l_new attribute in all slots.  The only slots that */
362
    /* can have the attribute set are the ones on the changes chain, */
363
    /* and ones in objects allocated since the last save. */
364
1.00M
    if (lmem->save_level > 1) {
365
845k
        ulong scanned;
366
845k
        int code = save_set_new(&lsave->state, false, true, &scanned);
367
368
845k
        if (code < 0)
369
0
            return code;
370
#if 0 /* Disable invisible save levels. */
371
        if ((lsave->state.total_scanned += scanned) > max_repeated_scan) {
372
            /* Do a second, invisible save. */
373
            alloc_save_t *rsave;
374
375
            rsave = alloc_save_space(lmem, dmem, 0L);
376
            if (rsave != 0) {
377
                rsave->client_data = cdata;
378
#if 0 /* Bug 688153 */
379
                rsave->id = lsave->id;
380
                print_save("save", lmem->space, rsave);
381
                lsave->id = 0;  /* mark as invisible */
382
                rsave->state.save_level--; /* ditto */
383
                lsave->client_data = 0;
384
#else
385
                rsave->id = 0;  /* mark as invisible */
386
                print_save("save", lmem->space, rsave);
387
                rsave->state.save_level--; /* ditto */
388
                rsave->client_data = 0;
389
#endif
390
                /* Inherit the allocated space count -- */
391
                /* we need this for triggering a GC. */
392
                print_save("save", lmem->space, lsave);
393
            }
394
        }
395
#endif
396
845k
    }
397
398
1.00M
    alloc_set_in_save(dmem);
399
1.00M
    *psid = sid;
400
1.00M
    return 0;
401
1.00M
}
402
/* Save the state of one space (global or local). */
403
static alloc_save_t *
404
alloc_save_space(gs_ref_memory_t * mem, gs_dual_memory_t * dmem, ulong sid)
405
1.17M
{
406
1.17M
    gs_ref_memory_t save_mem;
407
1.17M
    alloc_save_t *save;
408
1.17M
    clump_t *cp;
409
1.17M
    clump_t *new_cc = NULL;
410
1.17M
    clump_splay_walker sw;
411
412
1.17M
    save_mem = *mem;
413
1.17M
    alloc_close_clump(mem);
414
1.17M
    mem->cc = NULL;
415
1.17M
    gs_memory_status((gs_memory_t *) mem, &mem->previous_status);
416
1.17M
    ialloc_reset(mem);
417
418
    /* Create inner clumps wherever it's worthwhile. */
419
420
18.5M
    for (cp = clump_splay_walk_init(&sw, &save_mem); cp != 0; cp = clump_splay_walk_fwd(&sw)) {
421
17.3M
        if (cp->ctop - cp->cbot > min_inner_clump_space) {
422
            /* Create an inner clump to cover only the unallocated part. */
423
7.84M
            clump_t *inner =
424
7.84M
                gs_raw_alloc_struct_immovable(mem->non_gc_memory, &st_clump,
425
7.84M
                                              "alloc_save_space(inner)");
426
427
7.84M
            if (inner == 0)
428
0
                break;   /* maybe should fail */
429
7.84M
            alloc_init_clump(inner, cp->cbot, cp->ctop, cp->sreloc != 0, cp);
430
7.84M
            alloc_link_clump(inner, mem);
431
7.84M
            if_debug2m('u', (gs_memory_t *)mem, "[u]inner clump: cbot="PRI_INTPTR" ctop="PRI_INTPTR"\n",
432
7.84M
                       (intptr_t) inner->cbot, (intptr_t) inner->ctop);
433
7.84M
            if (cp == save_mem.cc)
434
1.02M
                new_cc = inner;
435
7.84M
        }
436
17.3M
    }
437
1.17M
    mem->cc = new_cc;
438
1.17M
    alloc_open_clump(mem);
439
440
1.17M
    save = gs_alloc_struct((gs_memory_t *) mem, alloc_save_t,
441
1.17M
                           &st_alloc_save, "alloc_save_space(save)");
442
1.17M
    if_debug2m('u', (gs_memory_t *)mem, "[u]save space %u at "PRI_INTPTR"\n",
443
1.17M
               mem->space, (intptr_t) save);
444
1.17M
    if (save == 0) {
445
        /* Free the inner clump structures.  This is the easiest way. */
446
1
        restore_free(mem);
447
1
        *mem = save_mem;
448
1
        return 0;
449
1
    }
450
1.17M
    save->client_data = NULL;
451
1.17M
    save->state = save_mem;
452
1.17M
    save->spaces = dmem->spaces;
453
1.17M
    save->restore_names = (name_memory(mem) == (gs_memory_t *) mem);
454
1.17M
    save->is_current = (dmem->current == mem);
455
1.17M
    save->id = sid;
456
1.17M
    mem->saved = save;
457
1.17M
    if_debug2m('u', (gs_memory_t *)mem, "[u%u]file_save "PRI_INTPTR"\n",
458
1.17M
               mem->space, (intptr_t) mem->streams);
459
1.17M
    mem->streams = 0;
460
1.17M
    mem->total_scanned = 0;
461
1.17M
    mem->total_scanned_after_compacting = 0;
462
1.17M
    if (sid)
463
1.17M
        mem->save_level++;
464
1.17M
    return save;
465
1.17M
}
466
467
/* Record a state change that must be undone for restore, */
468
/* and mark it as having been saved. */
469
int
470
alloc_save_change_in(gs_ref_memory_t *mem, const ref * pcont,
471
                  ref_packed * where, client_name_t cname)
472
2.07G
{
473
2.07G
    register alloc_change_t *cp;
474
475
2.07G
    if (mem->new_mask == 0)
476
2.06G
        return 0;    /* no saving */
477
9.64M
    cp = gs_alloc_struct((gs_memory_t *)mem, alloc_change_t,
478
9.64M
                         &st_alloc_change, "alloc_save_change");
479
9.64M
    if (cp == 0)
480
6
        return -1;
481
9.64M
    cp->next = mem->changes;
482
9.64M
    cp->where = where;
483
9.64M
    if (pcont == NULL)
484
0
        cp->offset = AC_OFFSET_STATIC;
485
9.64M
    else if (r_is_array(pcont) || r_has_type(pcont, t_dictionary))
486
9.64M
        cp->offset = AC_OFFSET_REF;
487
2
    else if (r_is_struct(pcont)) {
488
2
        assert ((byte *) where - (byte *) pcont->value.pstruct <= max_short && (byte *) where - (byte *) pcont->value.pstruct >= min_short);
489
2
        cp->offset = (byte *) where - (byte *) pcont->value.pstruct;
490
2
    }
491
0
    else {
492
0
        if_debug3('u', "Bad type %u for save!  pcont = "PRI_INTPTR", where = "PRI_INTPTR"\n",
493
0
                 r_type(pcont), (intptr_t) pcont, (intptr_t) where);
494
0
        gs_abort((const gs_memory_t *)mem);
495
0
    }
496
9.64M
    if (r_is_packed(where))
497
498k
        *(ref_packed *)&cp->contents = *where;
498
9.15M
    else {
499
9.15M
        ref_assign_inline(&cp->contents, (ref *) where);
500
9.15M
        r_set_attrs((ref *) where, l_new);
501
9.15M
    }
502
9.64M
    mem->changes = cp;
503
#ifdef DEBUG
504
    if (gs_debug_c('U')) {
505
        dmlprintf1((const gs_memory_t *)mem, "[U]save(%s)", client_name_string(cname));
506
        alloc_save_print((const gs_memory_t *)mem, cp, false);
507
    }
508
#endif
509
9.64M
    return 0;
510
9.64M
}
511
int
512
alloc_save_change(gs_dual_memory_t * dmem, const ref * pcont,
513
                  ref_packed * where, client_name_t cname)
514
2.06G
{
515
2.06G
    gs_ref_memory_t *mem =
516
2.06G
        (pcont == NULL ? dmem->space_local :
517
2.06G
         dmem->spaces_indexed[r_space(pcont) >> r_space_shift]);
518
519
2.06G
    return alloc_save_change_in(mem, pcont, where, cname);
520
2.06G
}
521
522
/* Allocate a structure for recording an allocation event. */
523
int
524
alloc_save_change_alloc(gs_ref_memory_t *mem, client_name_t cname, alloc_change_t **pcp)
525
219M
{
526
219M
    register alloc_change_t *cp;
527
528
219M
    if (mem->new_mask == 0)
529
193M
        return 0;    /* no saving */
530
25.8M
    cp = gs_alloc_struct((gs_memory_t *)mem, alloc_change_t,
531
25.8M
                         &st_alloc_change, "alloc_save_change");
532
25.8M
    if (cp == 0)
533
0
        return_error(gs_error_VMerror);
534
25.8M
    cp->next = mem->changes;
535
25.8M
    cp->where = 0;
536
25.8M
    cp->offset = AC_OFFSET_ALLOCATED;
537
25.8M
    make_null(&cp->contents);
538
25.8M
    *pcp = cp;
539
25.8M
    return 1;
540
25.8M
}
541
542
/* Remove an AC_OFFSET_ALLOCATED element. */
543
void
544
alloc_save_remove(gs_ref_memory_t *mem, ref_packed *obj, client_name_t cname)
545
38.8k
{
546
38.8k
    alloc_change_t **cpp = &mem->changes;
547
548
988k
    for (; *cpp != NULL;) {
549
949k
        alloc_change_t *cp = *cpp;
550
551
949k
        if (cp->offset == AC_OFFSET_ALLOCATED && cp->where == obj) {
552
20.1k
            if (mem->scan_limit == cp)
553
0
                mem->scan_limit = cp->next;
554
20.1k
            *cpp = cp->next;
555
20.1k
            gs_free_object((gs_memory_t *)mem, cp, "alloc_save_remove");
556
20.1k
        } else
557
929k
            cpp = &(*cpp)->next;
558
949k
    }
559
38.8k
}
560
561
/* Remove a change list element that references into a ref array. */
562
/* Used when freeing a ref array from the current save level */
563
void
564
alloc_save_remove_change(gs_ref_memory_t *mem, ref_packed *arr, unsigned int num_refs, client_name_t cname)
565
38.8k
{
566
38.8k
    alloc_change_t **cpp = &mem->changes;
567
38.8k
    ref *arr1 = (ref *)arr;
568
569
988k
    for (; *cpp != NULL;) {
570
949k
        alloc_change_t *cp = *cpp;
571
572
949k
        if (cp->offset == AC_OFFSET_REF && (ref *)cp->where > arr1 && (ref *)cp->where < arr1 + num_refs) {
573
0
            *cpp = cp->next;
574
0
            gs_free_object((gs_memory_t *)mem, cp, "alloc_save_remove");
575
0
        } else
576
949k
            cpp = &(*cpp)->next;
577
949k
    }
578
38.8k
}
579
580
/* Filter save change lists. */
581
static inline void
582
alloc_save__filter_changes_in_space(gs_ref_memory_t *mem)
583
16.8M
{
584
    /* This is a special function, which is called
585
       from the garbager after setting marks and before collecting
586
       unused space. Therefore it just resets marks for
587
       elements being released instead releasing them really. */
588
16.8M
    alloc_change_t **cpp = &mem->changes;
589
590
59.2M
    for (; *cpp != NULL; ) {
591
42.4M
        alloc_change_t *cp = *cpp;
592
593
42.4M
        if (cp->offset == AC_OFFSET_ALLOCATED && !check_l_mark(cp->where)) {
594
15.9M
            obj_header_t *pre = (obj_header_t *)cp - 1;
595
596
15.9M
            *cpp = cp->next;
597
15.9M
            cp->where = 0;
598
15.9M
            if (mem->scan_limit == cp)
599
113k
                mem->scan_limit = cp->next;
600
15.9M
            o_set_unmarked(pre);
601
15.9M
        } else
602
26.5M
            cpp = &(*cpp)->next;
603
42.4M
    }
604
16.8M
}
605
606
/* Filter save change lists. */
607
void
608
alloc_save__filter_changes(gs_ref_memory_t *memory)
609
1.63M
{
610
1.63M
    gs_ref_memory_t *mem = memory;
611
612
18.4M
    for  (; mem; mem = mem->saved == NULL ? NULL : &mem->saved->state)
613
16.8M
        alloc_save__filter_changes_in_space(mem);
614
1.63M
}
615
616
/* Return (the id of) the innermost externally visible save object, */
617
/* i.e., the innermost save with a non-zero ID. */
618
ulong
619
alloc_save_current_id(const gs_dual_memory_t * dmem)
620
1.00M
{
621
1.00M
    const alloc_save_t *save = dmem->space_local->saved;
622
623
1.00M
    while (save != 0 && save->id == 0)
624
0
        save = save->state.saved;
625
1.00M
    if (save)
626
1.00M
        return save->id;
627
628
    /* This should never happen, if it does, return a totally
629
     * impossible value.
630
     */
631
0
    return (ulong)-1;
632
1.00M
}
633
alloc_save_t *
634
alloc_save_current(const gs_dual_memory_t * dmem)
635
1.00M
{
636
1.00M
    return alloc_find_save(dmem, alloc_save_current_id(dmem));
637
1.00M
}
638
639
/* Test whether a reference would be invalidated by a restore. */
640
bool
641
alloc_is_since_save(const void *vptr, const alloc_save_t * save)
642
6.56M
{
643
    /* A reference postdates a save iff it is in a clump allocated */
644
    /* since the save (including any carried-over inner clumps). */
645
646
6.56M
    const char *const ptr = (const char *)vptr;
647
6.56M
    register gs_ref_memory_t *mem = save->space_local;
648
649
6.56M
    if_debug2m('U', (gs_memory_t *)mem, "[U]is_since_save "PRI_INTPTR", "PRI_INTPTR":\n",
650
6.56M
               (intptr_t) ptr, (intptr_t) save);
651
6.56M
    if (mem->saved == 0) { /* This is a special case, the final 'restore' from */
652
        /* alloc_restore_all. */
653
161k
        return true;
654
161k
    }
655
    /* Check against clumps allocated since the save. */
656
    /* (There may have been intermediate saves as well.) */
657
6.40M
    for (;; mem = &mem->saved->state) {
658
6.40M
        if_debug1m('U', (gs_memory_t *)mem, "[U]checking mem="PRI_INTPTR"\n", (intptr_t) mem);
659
6.40M
        if (ptr_is_within_mem_clumps(ptr, mem)) {
660
31
            if_debug0m('U', (gs_memory_t *)mem, "[U+]found\n");
661
31
            return true;
662
31
        }
663
6.40M
        if_debug1m('U', (gs_memory_t *)mem, "[U-]not in any chunks belonging to "PRI_INTPTR"\n", (intptr_t) mem);
664
6.40M
        if (mem->saved == save) { /* We've checked all the more recent saves, */
665
            /* must be OK. */
666
6.40M
            break;
667
6.40M
        }
668
6.40M
    }
669
670
    /*
671
     * If we're about to do a global restore (a restore to the level 0),
672
     * and there is only one context using this global VM
673
     * (the normal case, in which global VM is saved by the
674
     * outermost save), we also have to check the global save.
675
     * Global saves can't be nested, which makes things easy.
676
     */
677
6.40M
    if (save->state.save_level == 0 /* Restoring to save level 0 - see bug 688157, 688161 */ &&
678
201k
        (mem = save->space_global) != save->space_local &&
679
201k
        save->space_global->num_contexts == 1
680
6.40M
        ) {
681
201k
        if_debug1m('U', (gs_memory_t *)mem, "[U]checking global mem="PRI_INTPTR"\n", (intptr_t) mem);
682
201k
        if (ptr_is_within_mem_clumps(ptr, mem)) {
683
0
            if_debug0m('U', (gs_memory_t *)mem, "[U+]  found\n");
684
0
            return true;
685
0
        }
686
201k
    }
687
6.40M
    return false;
688
689
6.40M
#undef ptr
690
6.40M
}
691
692
/* Test whether a name would be invalidated by a restore. */
693
bool
694
alloc_name_is_since_save(const gs_memory_t *mem,
695
                         const ref * pnref, const alloc_save_t * save)
696
261k
{
697
261k
    const name_string_t *pnstr;
698
699
261k
    if (!save->restore_names)
700
261k
        return false;
701
0
    pnstr = names_string_inline(mem->gs_lib_ctx->gs_name_table, pnref);
702
0
    if (pnstr->foreign_string)
703
0
        return false;
704
0
    return alloc_is_since_save(pnstr->string_bytes, save);
705
0
}
706
bool
707
alloc_name_index_is_since_save(const gs_memory_t *mem,
708
                               uint nidx, const alloc_save_t *save)
709
0
{
710
0
    const name_string_t *pnstr;
711
712
0
    if (!save->restore_names)
713
0
        return false;
714
0
    pnstr = names_index_string_inline(mem->gs_lib_ctx->gs_name_table, nidx);
715
0
    if (pnstr->foreign_string)
716
0
        return false;
717
0
    return alloc_is_since_save(pnstr->string_bytes, save);
718
0
}
719
720
/* Check whether any names have been created since a given save */
721
/* that might be released by the restore. */
722
bool
723
alloc_any_names_since_save(const alloc_save_t * save)
724
1.17M
{
725
1.17M
    return save->restore_names;
726
1.17M
}
727
728
/* Get the saved state with a given ID. */
729
alloc_save_t *
730
alloc_find_save(const gs_dual_memory_t * dmem, ulong sid)
731
2.33M
{
732
2.33M
    alloc_save_t *sprev = dmem->space_local->saved;
733
734
2.33M
    if (sid == 0)
735
0
        return 0;   /* invalid id */
736
76.7M
    while (sprev != 0) {
737
76.7M
        if (sprev->id == sid)
738
2.33M
            return sprev;
739
74.3M
        sprev = sprev->state.saved;
740
74.3M
    }
741
0
    return 0;
742
2.33M
}
743
744
/* Get the client data from a saved state. */
745
void *
746
alloc_save_client_data(const alloc_save_t * save)
747
1.00M
{
748
1.00M
    return save->client_data;
749
1.00M
}
750
751
/*
752
 * Do one step of restoring the state.  The client is responsible for
753
 * calling alloc_find_save to get the save object, and for ensuring that
754
 * there are no surviving pointers for which alloc_is_since_save is true.
755
 * Return true if the argument was the innermost save, in which case
756
 * this is the last (or only) step.
757
 * Note that "one step" may involve multiple internal steps,
758
 * if this is the outermost restore (which requires restoring both local
759
 * and global VM) or if we created extra save levels to reduce scanning.
760
 */
761
static void restore_finalize(gs_ref_memory_t *);
762
static void restore_space(gs_ref_memory_t *, gs_dual_memory_t *);
763
764
int
765
alloc_restore_step_in(gs_dual_memory_t *dmem, alloc_save_t * save)
766
1.00M
{
767
    /* Get save->space_* now, because the save object will be freed. */
768
1.00M
    gs_ref_memory_t *lmem = save->space_local;
769
1.00M
    gs_ref_memory_t *gmem = save->space_global;
770
1.00M
    gs_ref_memory_t *mem = lmem;
771
1.00M
    alloc_save_t *sprev;
772
1.00M
    int code;
773
774
    /* Finalize all objects before releasing resources or undoing changes. */
775
1.00M
    do {
776
1.00M
        ulong sid;
777
778
1.00M
        sprev = mem->saved;
779
1.00M
        sid = sprev->id;
780
1.00M
        restore_finalize(mem);  /* finalize objects */
781
1.00M
        mem = &sprev->state;
782
1.00M
        if (sid != 0)
783
1.00M
            break;
784
1.00M
    }
785
1.00M
    while (sprev != save);
786
1.00M
    if (mem->save_level == 0) {
787
        /* This is the outermost save, which might also */
788
        /* need to restore global VM. */
789
164k
        mem = gmem;
790
164k
        if (mem != lmem && mem->saved != 0) {
791
164k
            restore_finalize(mem);
792
164k
        }
793
164k
    }
794
795
    /* Do one (externally visible) step of restoring the state. */
796
1.00M
    mem = lmem;
797
1.00M
    do {
798
1.00M
        ulong sid;
799
800
1.00M
        sprev = mem->saved;
801
1.00M
        sid = sprev->id;
802
1.00M
        code = restore_resources(sprev, mem); /* release other resources */
803
1.00M
        if (code < 0)
804
0
            return code;
805
1.00M
        restore_space(mem, dmem); /* release memory */
806
1.00M
        if (sid != 0)
807
1.00M
            break;
808
1.00M
    }
809
1.00M
    while (sprev != save);
810
811
1.00M
    if (mem->save_level == 0) {
812
        /* This is the outermost save, which might also */
813
        /* need to restore global VM. */
814
164k
        mem = gmem;
815
164k
        if (mem != lmem && mem->saved != 0) {
816
164k
            code = restore_resources(mem->saved, mem);
817
164k
            if (code < 0)
818
0
                return code;
819
164k
            restore_space(mem, dmem);
820
164k
        }
821
164k
        alloc_set_not_in_save(dmem);
822
845k
    } else {     /* Set the l_new attribute in all slots that are now new. */
823
845k
        ulong scanned;
824
825
845k
        code = save_set_new(mem, true, false, &scanned);
826
845k
        if (code < 0)
827
0
            return code;
828
845k
    }
829
830
1.00M
    return sprev == save;
831
1.00M
}
832
/* Restore the memory of one space, by undoing changes and freeing */
833
/* memory allocated since the save. */
834
static void
835
restore_space(gs_ref_memory_t * mem, gs_dual_memory_t *dmem)
836
1.17M
{
837
1.17M
    alloc_save_t *save = mem->saved;
838
1.17M
    alloc_save_t saved;
839
840
1.17M
    print_save("restore", mem->space, save);
841
842
    /* Undo changes since the save. */
843
1.17M
    {
844
1.17M
        register alloc_change_t *cp = mem->changes;
845
846
20.7M
        while (cp) {
847
#ifdef DEBUG
848
            if (gs_debug_c('U')) {
849
                dmlputs((const gs_memory_t *)mem, "[U]restore");
850
                alloc_save_print((const gs_memory_t *)mem, cp, true);
851
            }
852
#endif
853
19.5M
            if (cp->offset == AC_OFFSET_ALLOCATED)
854
19.5M
                DO_NOTHING;
855
9.64M
            else
856
9.64M
            if (r_is_packed(&cp->contents))
857
498k
                *cp->where = *(ref_packed *) & cp->contents;
858
9.15M
            else
859
9.15M
                ref_assign_inline((ref *) cp->where, &cp->contents);
860
19.5M
            cp = cp->next;
861
19.5M
        }
862
1.17M
    }
863
864
    /* Free memory allocated since the save. */
865
    /* Note that this frees all clumps except the inner ones */
866
    /* belonging to this level. */
867
1.17M
    saved = *save;
868
1.17M
    restore_free(mem);
869
870
    /* Restore the allocator state. */
871
1.17M
    {
872
1.17M
        int num_contexts = mem->num_contexts; /* don't restore */
873
874
1.17M
        *mem = saved.state;
875
1.17M
        mem->num_contexts = num_contexts;
876
1.17M
    }
877
1.17M
    alloc_open_clump(mem);
878
879
    /* Make the allocator current if it was current before the save. */
880
1.17M
    if (saved.is_current) {
881
1.00M
        dmem->current = mem;
882
1.00M
        dmem->current_space = mem->space;
883
1.00M
    }
884
1.17M
}
885
886
/* Restore to the initial state, releasing all resources. */
887
/* The allocator is no longer usable after calling this routine! */
888
int
889
alloc_restore_all(i_ctx_t *i_ctx_p)
890
162k
{
891
    /*
892
     * Save the memory pointers, since freeing space_local will also
893
     * free dmem itself.
894
     */
895
162k
    gs_ref_memory_t *lmem = idmemory->space_local;
896
162k
    gs_ref_memory_t *gmem = idmemory->space_global;
897
162k
    gs_ref_memory_t *smem = idmemory->space_system;
898
899
162k
    gs_ref_memory_t *mem;
900
162k
    int code;
901
902
    /* Restore to a state outside any saves. */
903
1.06M
    while (lmem->save_level != 0) {
904
900k
        vm_save_t *vmsave = alloc_save_client_data(alloc_save_current(idmemory));
905
900k
        if (vmsave->gsave) {
906
900k
            gs_grestoreall_for_restore(i_ctx_p->pgs, vmsave->gsave);
907
900k
        }
908
900k
        vmsave->gsave = 0;
909
900k
        code = alloc_restore_step_in(idmemory, lmem->saved);
910
911
900k
        if (code < 0)
912
0
            return code;
913
900k
    }
914
915
    /* Finalize memory. */
916
162k
    restore_finalize(lmem);
917
162k
    if ((mem = (gs_ref_memory_t *)lmem->stable_memory) != lmem)
918
162k
        restore_finalize(mem);
919
162k
    if (gmem != lmem && gmem->num_contexts == 1) {
920
162k
        restore_finalize(gmem);
921
162k
        if ((mem = (gs_ref_memory_t *)gmem->stable_memory) != gmem)
922
162k
            restore_finalize(mem);
923
162k
    }
924
162k
    restore_finalize(smem);
925
926
    /* Release resources other than memory, using fake */
927
    /* save and memory objects. */
928
162k
    {
929
162k
        alloc_save_t empty_save;
930
931
162k
        empty_save.spaces = idmemory->spaces;
932
162k
        empty_save.restore_names = false; /* don't bother to release */
933
162k
        code = restore_resources(&empty_save, NULL);
934
162k
        if (code < 0)
935
0
            return code;
936
162k
    }
937
938
    /* Finally, release memory. */
939
162k
    restore_free(lmem);
940
162k
    if ((mem = (gs_ref_memory_t *)lmem->stable_memory) != lmem)
941
162k
        restore_free(mem);
942
162k
    if (gmem != lmem) {
943
162k
        if (!--(gmem->num_contexts)) {
944
162k
            restore_free(gmem);
945
162k
            if ((mem = (gs_ref_memory_t *)gmem->stable_memory) != gmem)
946
162k
                restore_free(mem);
947
162k
        }
948
162k
    }
949
162k
    restore_free(smem);
950
162k
    return 0;
951
162k
}
952
953
/*
954
 * Finalize objects that will be freed by a restore.
955
 * Note that we must temporarily disable the freeing operations
956
 * of the allocator while doing this.
957
 */
958
static void
959
restore_finalize(gs_ref_memory_t * mem)
960
1.98M
{
961
1.98M
    clump_t *cp;
962
1.98M
    clump_splay_walker sw;
963
964
1.98M
    alloc_close_clump(mem);
965
1.98M
    gs_enable_free((gs_memory_t *) mem, false);
966
32.3M
    for (cp = clump_splay_walk_bwd_init(&sw, mem); cp != 0; cp = clump_splay_walk_bwd(&sw)) {
967
264M
        SCAN_CLUMP_OBJECTS(cp)
968
264M
            DO_ALL
969
264M
            struct_proc_finalize((*finalize)) =
970
264M
            pre->o_type->finalize;
971
264M
        if (finalize != 0) {
972
9.62M
            if_debug2m('u', (gs_memory_t *)mem, "[u]restore finalizing %s "PRI_INTPTR"\n",
973
9.62M
                       struct_type_name_string(pre->o_type),
974
9.62M
                       (intptr_t) (pre + 1));
975
9.62M
            (*finalize) ((gs_memory_t *) mem, pre + 1);
976
9.62M
        }
977
264M
        END_OBJECTS_SCAN
978
30.3M
    }
979
1.98M
    gs_enable_free((gs_memory_t *) mem, true);
980
1.98M
}
981
982
/* Release resources for a restore */
983
static int
984
restore_resources(alloc_save_t * sprev, gs_ref_memory_t * mem)
985
1.33M
{
986
1.33M
    int code;
987
#ifdef DEBUG
988
    if (mem) {
989
        /* Note restoring of the file list. */
990
        if_debug4m('u', (gs_memory_t *)mem, "[u%u]file_restore "PRI_INTPTR" => "PRI_INTPTR" for "PRI_INTPTR"\n",
991
                   mem->space, (intptr_t)mem->streams,
992
                   (intptr_t)sprev->state.streams, (intptr_t)sprev);
993
    }
994
#endif
995
996
    /* Remove entries from font and character caches. */
997
1.33M
    code = font_restore(sprev);
998
1.33M
    if (code < 0)
999
0
        return code;
1000
1001
    /* Adjust the name table. */
1002
1.33M
    if (sprev->restore_names)
1003
0
        names_restore(mem->gs_lib_ctx->gs_name_table, sprev);
1004
1.33M
    return 0;
1005
1.33M
}
1006
1007
/* Release memory for a restore. */
1008
static void
1009
restore_free(gs_ref_memory_t * mem)
1010
1.98M
{
1011
    /* Free clumps allocated since the save. */
1012
1.98M
    gs_free_all((gs_memory_t *) mem);
1013
1.98M
}
1014
1015
static inline int
1016
mark_allocated(void *obj, bool to_new, uint *psize)
1017
8.31M
{
1018
8.31M
    obj_header_t *pre = (obj_header_t *)obj - 1;
1019
8.31M
    uint size = pre_obj_contents_size(pre);
1020
8.31M
    ref_packed *prp = (ref_packed *) (pre + 1);
1021
8.31M
    ref_packed *next = (ref_packed *) ((char *)prp + size);
1022
#ifdef ALIGNMENT_ALIASING_BUG
1023
                ref *rpref;
1024
# define RP_REF(rp) (rpref = (ref *)rp, rpref)
1025
#else
1026
1.67G
# define RP_REF(rp) ((ref *)rp)
1027
8.31M
#endif
1028
1029
8.31M
    if (pre->o_type != &st_refs) {
1030
        /* Must not happen. */
1031
0
        if_debug0('u', "Wrong object type when expected a ref.\n");
1032
0
        return_error(gs_error_Fatal);
1033
0
    }
1034
    /* We know that every block of refs ends with */
1035
    /* a full-size ref, so we only need the end check */
1036
    /* when we encounter one of those. */
1037
8.31M
    if (to_new)
1038
101M
        while (1) {
1039
101M
            if (r_is_packed(prp))
1040
16.2M
                prp++;
1041
84.7M
            else {
1042
84.7M
                RP_REF(prp)->tas.type_attrs |= l_new;
1043
84.7M
                prp += packed_per_ref;
1044
84.7M
                if (prp >= next)
1045
1.43M
                    break;
1046
84.7M
            }
1047
101M
    } else
1048
1.76G
        while (1) {
1049
1.76G
            if (r_is_packed(prp))
1050
179M
                prp++;
1051
1.58G
            else {
1052
1.58G
                RP_REF(prp)->tas.type_attrs &= ~l_new;
1053
1.58G
                prp += packed_per_ref;
1054
1.58G
                if (prp >= next)
1055
6.87M
                    break;
1056
1.58G
            }
1057
1.76G
        }
1058
8.31M
#undef RP_REF
1059
8.31M
    *psize = size;
1060
8.31M
    return 0;
1061
8.31M
}
1062
1063
/* Check if a block contains refs marked by garbager. */
1064
static bool
1065
check_l_mark(void *obj)
1066
18.2M
{
1067
18.2M
    obj_header_t *pre = (obj_header_t *)obj - 1;
1068
18.2M
    uint size = pre_obj_contents_size(pre);
1069
18.2M
    ref_packed *prp = (ref_packed *) (pre + 1);
1070
18.2M
    ref_packed *next = (ref_packed *) ((char *)prp + size);
1071
#ifdef ALIGNMENT_ALIASING_BUG
1072
                ref *rpref;
1073
# define RP_REF(rp) (rpref = (ref *)rp, rpref)
1074
#else
1075
18.2M
# define RP_REF(rp) ((ref *)rp)
1076
18.2M
#endif
1077
1078
    /* We know that every block of refs ends with */
1079
    /* a full-size ref, so we only need the end check */
1080
    /* when we encounter one of those. */
1081
23.2G
    while (1) {
1082
23.2G
        if (r_is_packed(prp)) {
1083
405M
            if (r_has_pmark(prp))
1084
12.5k
                return true;
1085
405M
            prp++;
1086
22.8G
        } else {
1087
22.8G
            if (r_has_attr(RP_REF(prp), l_mark))
1088
2.29M
                return true;
1089
22.8G
            prp += packed_per_ref;
1090
22.8G
            if (prp >= next)
1091
15.9M
                return false;
1092
22.8G
        }
1093
23.2G
    }
1094
18.2M
#undef RP_REF
1095
18.2M
}
1096
1097
/* Set or reset the l_new attribute in every relevant slot. */
1098
/* This includes every slot on the current change chain, */
1099
/* and every (ref) slot allocated at this save level. */
1100
/* Return the number of bytes of data scanned. */
1101
static int
1102
save_set_new(gs_ref_memory_t * mem, bool to_new, bool set_limit, ulong *pscanned)
1103
1.69M
{
1104
1.69M
    ulong scanned = 0;
1105
1.69M
    int code;
1106
1107
    /* Handle the change chain. */
1108
1.69M
    code = save_set_new_changes(mem, to_new, set_limit);
1109
1.69M
    if (code < 0)
1110
0
        return code;
1111
1112
    /* Handle newly allocated ref objects. */
1113
6.07M
    SCAN_MEM_CLUMPS(mem, cp) {
1114
6.07M
        if (cp->has_refs) {
1115
525k
            bool has_refs = false;
1116
1117
11.2M
            SCAN_CLUMP_OBJECTS(cp)
1118
11.2M
                DO_ALL
1119
11.2M
                if_debug3m('U', (gs_memory_t *)mem, "[U]set_new scan("PRI_INTPTR"(%u), %d)\n",
1120
11.2M
                           (intptr_t) pre, size, to_new);
1121
11.2M
            if (pre->o_type == &st_refs) {
1122
                /* These are refs, scan them. */
1123
3.29M
                ref_packed *prp = (ref_packed *) (pre + 1);
1124
3.29M
                uint size;
1125
3.29M
                has_refs = true && to_new;
1126
3.29M
                code = mark_allocated(prp, to_new, &size);
1127
3.29M
                if (code < 0)
1128
0
                    return code;
1129
3.29M
                scanned += size;
1130
3.29M
            } else
1131
7.92M
                scanned += sizeof(obj_header_t);
1132
11.2M
            END_OBJECTS_SCAN
1133
525k
                cp->has_refs = has_refs;
1134
525k
        }
1135
6.07M
    }
1136
6.07M
    END_CLUMPS_SCAN
1137
1.69M
    if_debug2m('u', (gs_memory_t *)mem, "[u]set_new (%s) scanned %ld\n",
1138
1.69M
               (to_new ? "restore" : "save"), scanned);
1139
1.69M
    *pscanned = scanned;
1140
1.69M
    return 0;
1141
1.69M
}
1142
1143
/* Drop redundant elements from the changes list and set l_new. */
1144
static void
1145
drop_redundant_changes(gs_ref_memory_t * mem)
1146
9
{
1147
9
    register alloc_change_t *chp = mem->changes, *chp_back = NULL, *chp_forth;
1148
1149
    /* As we are trying to throw away redundant changes in an allocator instance
1150
       that has already been "saved", the active clump has already been "closed"
1151
       by alloc_save_space(). Using such an allocator (for example, by calling
1152
       gs_free_object() with it) can leave it in an unstable state, causing
1153
       problems for the garbage collector (specifically, the clump validator code).
1154
       So, before we might use it, open the current clump, and then close it again
1155
       when we're done.
1156
     */
1157
9
    alloc_open_clump(mem);
1158
1159
    /* First reverse the list and set all. */
1160
737
    for (; chp; chp = chp_forth) {
1161
728
        chp_forth = chp->next;
1162
728
        if (chp->offset != AC_OFFSET_ALLOCATED) {
1163
665
            ref_packed *prp = chp->where;
1164
1165
665
            if (!r_is_packed(prp)) {
1166
665
                ref *const rp = (ref *)prp;
1167
1168
665
                rp->tas.type_attrs |= l_new;
1169
665
            }
1170
665
        }
1171
728
        chp->next = chp_back;
1172
728
        chp_back = chp;
1173
728
    }
1174
9
    mem->changes = chp_back;
1175
9
    chp_back = NULL;
1176
    /* Then filter, reset and reverse again. */
1177
737
    for (chp = mem->changes; chp; chp = chp_forth) {
1178
728
        chp_forth = chp->next;
1179
728
        if (chp->offset != AC_OFFSET_ALLOCATED) {
1180
665
            ref_packed *prp = chp->where;
1181
1182
665
            if (!r_is_packed(prp)) {
1183
665
                ref *const rp = (ref *)prp;
1184
1185
665
                if ((rp->tas.type_attrs & l_new) == 0) {
1186
344
                    if (mem->scan_limit == chp)
1187
1
                        mem->scan_limit = chp_back;
1188
344
                    if (mem->changes == chp)
1189
0
                        mem->changes = chp_back;
1190
344
                    gs_free_object((gs_memory_t *)mem, chp, "alloc_save_remove");
1191
344
                    continue;
1192
344
                } else
1193
321
                    rp->tas.type_attrs &= ~l_new;
1194
665
            }
1195
665
        }
1196
384
        chp->next = chp_back;
1197
384
        chp_back = chp;
1198
384
    }
1199
9
    mem->changes = chp_back;
1200
1201
9
    alloc_close_clump(mem);
1202
9
}
1203
1204
/* Set or reset the l_new attribute on the changes chain. */
1205
static int
1206
save_set_new_changes(gs_ref_memory_t * mem, bool to_new, bool set_limit)
1207
1.69M
{
1208
1.69M
    register alloc_change_t *chp;
1209
1.69M
    register uint new = (to_new ? l_new : 0);
1210
1.69M
    ulong scanned = 0;
1211
1212
1.69M
    if (!to_new && mem->total_scanned_after_compacting > max_repeated_scan * 16) {
1213
9
        mem->total_scanned_after_compacting = 0;
1214
9
        drop_redundant_changes(mem);
1215
9
    }
1216
11.9M
    for (chp = mem->changes; chp; chp = chp->next) {
1217
10.2M
        if (chp->offset == AC_OFFSET_ALLOCATED) {
1218
5.02M
            if (chp->where != 0) {
1219
5.02M
                uint size;
1220
5.02M
                int code = mark_allocated((void *)chp->where, to_new, &size);
1221
1222
5.02M
                if (code < 0)
1223
0
                    return code;
1224
5.02M
                scanned += size;
1225
5.02M
            }
1226
5.26M
        } else {
1227
5.26M
            ref_packed *prp = chp->where;
1228
1229
5.26M
            if_debug3m('U', (gs_memory_t *)mem, "[U]set_new "PRI_INTPTR": ("PRI_INTPTR", %d)\n",
1230
5.26M
                       (intptr_t)chp, (intptr_t)prp, new);
1231
5.26M
            if (!r_is_packed(prp)) {
1232
5.25M
                ref *const rp = (ref *) prp;
1233
1234
5.25M
                rp->tas.type_attrs =
1235
5.25M
                    (rp->tas.type_attrs & ~l_new) + new;
1236
5.25M
            }
1237
5.26M
        }
1238
10.2M
        if (mem->scan_limit == chp)
1239
11.5k
            break;
1240
10.2M
    }
1241
1.69M
    if (set_limit) {
1242
845k
        mem->total_scanned_after_compacting += scanned;
1243
845k
        if (scanned  + mem->total_scanned >= max_repeated_scan) {
1244
6.91k
            mem->scan_limit = mem->changes;
1245
6.91k
            mem->total_scanned = 0;
1246
6.91k
        } else
1247
838k
            mem->total_scanned += scanned;
1248
845k
    }
1249
1.69M
    return 0;
1250
1.69M
}
1251
1252
gs_memory_t *
1253
gs_save_any_memory(const alloc_save_t *save)
1254
1.33M
{
1255
1.33M
    return((gs_memory_t *)save->space_local);
1256
1.33M
}