/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 | 324k | #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 | 495M | { |
36 | 495M | os_ptr op = osp; |
37 | 495M | check_op(1); |
38 | 495M | check_type(*op, t_boolean); |
39 | 495M | ialloc_set_space(idmemory, |
40 | 495M | (op->value.boolval ? avm_global : avm_local)); |
41 | 495M | pop(1); |
42 | 495M | return 0; |
43 | 495M | } |
44 | | |
45 | | /* - .currentglobal <bool> */ |
46 | | static int |
47 | | zcurrentglobal(i_ctx_t *i_ctx_p) |
48 | 339M | { |
49 | 339M | os_ptr op = osp; |
50 | | |
51 | 339M | push(1); |
52 | 339M | make_bool(op, ialloc_space(idmemory) != avm_local); |
53 | 339M | return 0; |
54 | 339M | } |
55 | | |
56 | | /* <any> gcheck/scheck <bool> */ |
57 | | static int |
58 | | zgcheck(i_ctx_t *i_ctx_p) |
59 | 121M | { |
60 | 121M | os_ptr op = osp; |
61 | | |
62 | 121M | check_op(1); |
63 | 121M | make_bool(op, (r_is_local(op) ? false : true)); |
64 | 121M | return 0; |
65 | 121M | } |
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 | 868k | { |
79 | 868k | if (val < -1) |
80 | 0 | return_error(gs_error_rangecheck); |
81 | 868k | else if (val == -1) |
82 | 324k | val = (gs_debug_c('.') ? DEFAULT_VM_THRESHOLD_SMALL : |
83 | 324k | DEFAULT_VM_THRESHOLD_LARGE); |
84 | | #if PSINT32BIT==1 |
85 | | else if (val > max_int) |
86 | | val = max_int |
87 | | #endif |
88 | 868k | gs_memory_set_vm_threshold(idmemory->space_system, val); |
89 | 868k | gs_memory_set_vm_threshold(idmemory->space_global, val); |
90 | 868k | gs_memory_set_vm_threshold(idmemory->space_local, val); |
91 | 868k | return 0; |
92 | 868k | } |
93 | | |
94 | | int |
95 | | set_vm_reclaim(i_ctx_t *i_ctx_p, long val) |
96 | 868k | { |
97 | 868k | if (val >= -2 && val <= 0) { |
98 | 868k | gs_memory_set_vm_reclaim(idmemory->space_system, (val >= -1)); |
99 | 868k | gs_memory_set_vm_reclaim(idmemory->space_global, (val >= -1)); |
100 | 868k | gs_memory_set_vm_reclaim(idmemory->space_local, (val == 0)); |
101 | 868k | return 0; |
102 | 868k | } else |
103 | 0 | return_error(gs_error_rangecheck); |
104 | 868k | } |
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 | 162k | { |
115 | 162k | os_ptr op = osp; |
116 | | |
117 | 162k | check_op(1); |
118 | 162k | check_type(*op, t_integer); |
119 | 162k | 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 | 162k | return_error(gs_error_VMreclaim); |
123 | 162k | } |
124 | 0 | return_error(gs_error_rangecheck); |
125 | 162k | } |
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 | | }; |