xref: /plan9/sys/src/cmd/gs/src/zvmem2.c (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1992, 1993, 1994, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: zvmem2.c,v 1.7 2002/11/20 18:56:56 igor Exp $ */
18 /* Level 2 "Virtual memory" operators */
19 #include "ghost.h"
20 #include "oper.h"
21 #include "estack.h"
22 #include "ialloc.h"		/* for ivmspace.h */
23 #include "ivmspace.h"
24 #include "ivmem2.h"
25 #include "store.h"
26 
27 /* Garbage collector control parameters. */
28 #define DEFAULT_VM_THRESHOLD_SMALL 100000
29 #define DEFAULT_VM_THRESHOLD_LARGE 1000000
30 #define MIN_VM_THRESHOLD 1
31 #define MAX_VM_THRESHOLD max_long
32 
33 /* ------ Local/global VM control ------ */
34 
35 /* <bool> .setglobal - */
36 private int
zsetglobal(i_ctx_t * i_ctx_p)37 zsetglobal(i_ctx_t *i_ctx_p)
38 {
39     os_ptr op = osp;
40     check_type(*op, t_boolean);
41     ialloc_set_space(idmemory,
42 		     (op->value.boolval ? avm_global : avm_local));
43     pop(1);
44     return 0;
45 }
46 
47 /* <bool> .currentglobal - */
48 private int
zcurrentglobal(i_ctx_t * i_ctx_p)49 zcurrentglobal(i_ctx_t *i_ctx_p)
50 {
51     os_ptr op = osp;
52 
53     push(1);
54     make_bool(op, ialloc_space(idmemory) != avm_local);
55     return 0;
56 }
57 
58 /* <any> gcheck/scheck <bool> */
59 private int
zgcheck(i_ctx_t * i_ctx_p)60 zgcheck(i_ctx_t *i_ctx_p)
61 {
62     os_ptr op = osp;
63 
64     check_op(1);
65     make_bool(op, (r_is_local(op) ? false : true));
66     return 0;
67 }
68 
69 /* ------ Garbage collector control ------ */
70 
71 /* These routines are exported for setuserparams. */
72 
73 /*
74  * <int> setvmthreshold -
75  *
76  * This is implemented as a PostScript procedure that calls setuserparams.
77  */
78 int
set_vm_threshold(i_ctx_t * i_ctx_p,long val)79 set_vm_threshold(i_ctx_t *i_ctx_p, long val)
80 {
81     if (val < -1)
82 	return_error(e_rangecheck);
83     else if (val == -1)
84 	val = (gs_debug_c('.') ? DEFAULT_VM_THRESHOLD_SMALL :
85 	       DEFAULT_VM_THRESHOLD_LARGE);
86     else if (val < MIN_VM_THRESHOLD)
87 	val = MIN_VM_THRESHOLD;
88     else if (val > MAX_VM_THRESHOLD)
89 	val = MAX_VM_THRESHOLD;
90     gs_memory_set_vm_threshold(idmemory->space_global, val);
91     gs_memory_set_vm_threshold(idmemory->space_local, val);
92     return 0;
93 }
94 
95 int
set_vm_reclaim(i_ctx_t * i_ctx_p,long val)96 set_vm_reclaim(i_ctx_t *i_ctx_p, long val)
97 {
98     if (val >= -2 && val <= 0) {
99 	gs_memory_set_vm_reclaim(idmemory->space_system, (val >= -1));
100 	gs_memory_set_vm_reclaim(idmemory->space_global, (val >= -1));
101 	gs_memory_set_vm_reclaim(idmemory->space_local, (val == 0));
102 	return 0;
103     } else
104 	return_error(e_rangecheck);
105 }
106 
107 /*
108  * <int> .vmreclaim -
109  *
110  * This implements only immediate garbage collection: enabling and
111  * disabling GC is implemented by calling setuserparams.
112  */
113 private int
zvmreclaim(i_ctx_t * i_ctx_p)114 zvmreclaim(i_ctx_t *i_ctx_p)
115 {
116     os_ptr op = osp;
117 
118     check_type(*op, t_integer);
119     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 	return_error(e_VMreclaim);
123     }
124     return_error(e_rangecheck);
125 }
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 };
141