Coverage Report

Created: 2025-06-10 07:26

/src/ghostpdl/psi/zvmem2.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2023 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
/* Level 2 "Virtual memory" operators */
18
#include "ghost.h"
19
#include "oper.h"
20
#include "estack.h"
21
#include "ialloc.h"   /* for ivmspace.h */
22
#include "ivmspace.h"
23
#include "ivmem2.h"
24
#include "store.h"
25
26
/* Garbage collector control parameters. */
27
0
#define DEFAULT_VM_THRESHOLD_SMALL 100000
28
18.4k
#define DEFAULT_VM_THRESHOLD_LARGE 8000000
29
30
/* ------ Local/global VM control ------ */
31
32
/* <bool> .setglobal - */
33
static int
34
zsetglobal(i_ctx_t *i_ctx_p)
35
27.8M
{
36
27.8M
    os_ptr op = osp;
37
27.8M
    check_op(1);
38
27.8M
    check_type(*op, t_boolean);
39
27.8M
    ialloc_set_space(idmemory,
40
27.8M
                     (op->value.boolval ? avm_global : avm_local));
41
27.8M
    pop(1);
42
27.8M
    return 0;
43
27.8M
}
44
45
/* - .currentglobal <bool> */
46
static int
47
zcurrentglobal(i_ctx_t *i_ctx_p)
48
18.8M
{
49
18.8M
    os_ptr op = osp;
50
51
18.8M
    push(1);
52
18.8M
    make_bool(op, ialloc_space(idmemory) != avm_local);
53
18.8M
    return 0;
54
18.8M
}
55
56
/* <any> gcheck/scheck <bool> */
57
static int
58
zgcheck(i_ctx_t *i_ctx_p)
59
6.67M
{
60
6.67M
    os_ptr op = osp;
61
62
6.67M
    check_op(1);
63
6.67M
    make_bool(op, (r_is_local(op) ? false : true));
64
6.67M
    return 0;
65
6.67M
}
66
67
/* ------ Garbage collector control ------ */
68
69
/* These routines are exported for setuserparams. */
70
71
/*
72
 * <int> setvmthreshold -
73
 *
74
 * This is implemented as a PostScript procedure that calls setuserparams.
75
 */
76
int
77
set_vm_threshold(i_ctx_t *i_ctx_p, int64_t val)
78
53.5k
{
79
53.5k
    if (val < -1)
80
0
        return_error(gs_error_rangecheck);
81
53.5k
    else if (val == -1)
82
18.4k
        val = (gs_debug_c('.') ? DEFAULT_VM_THRESHOLD_SMALL :
83
18.4k
               DEFAULT_VM_THRESHOLD_LARGE);
84
#if PSINT32BIT==1
85
    else if (val > max_int)
86
        val = max_int
87
#endif
88
53.5k
    gs_memory_set_vm_threshold(idmemory->space_system, val);
89
53.5k
    gs_memory_set_vm_threshold(idmemory->space_global, val);
90
53.5k
    gs_memory_set_vm_threshold(idmemory->space_local, val);
91
53.5k
    return 0;
92
53.5k
}
93
94
int
95
set_vm_reclaim(i_ctx_t *i_ctx_p, long val)
96
53.5k
{
97
53.5k
    if (val >= -2 && val <= 0) {
98
53.5k
        gs_memory_set_vm_reclaim(idmemory->space_system, (val >= -1));
99
53.5k
        gs_memory_set_vm_reclaim(idmemory->space_global, (val >= -1));
100
53.5k
        gs_memory_set_vm_reclaim(idmemory->space_local, (val == 0));
101
53.5k
        return 0;
102
53.5k
    } else
103
0
        return_error(gs_error_rangecheck);
104
53.5k
}
105
106
/*
107
 * <int> .vmreclaim -
108
 *
109
 * This implements only immediate garbage collection: enabling and
110
 * disabling GC is implemented by calling setuserparams.
111
 */
112
static int
113
zvmreclaim(i_ctx_t *i_ctx_p)
114
9.22k
{
115
9.22k
    os_ptr op = osp;
116
117
9.22k
    check_op(1);
118
9.22k
    check_type(*op, t_integer);
119
9.22k
    if (op->value.intval == 1 || op->value.intval == 2) {
120
        /* Force the interpreter to store its state and exit. */
121
        /* The interpreter's caller will do the actual GC. */
122
9.22k
        return_error(gs_error_VMreclaim);
123
9.22k
    }
124
0
    return_error(gs_error_rangecheck);
125
9.22k
}
126
127
/* ------ Initialization procedure ------ */
128
129
/* The VM operators are defined even if the initial language level is 1, */
130
/* because we need them during initialization. */
131
const op_def zvmem2_op_defs[] =
132
{
133
    {"0.currentglobal", zcurrentglobal},
134
    {"1.gcheck", zgcheck},
135
    {"1.setglobal", zsetglobal},
136
                /* The rest of the operators are defined only in Level 2. */
137
    op_def_begin_level2(),
138
    {"1.vmreclaim", zvmreclaim},
139
    op_def_end(0)
140
};