/src/ghostpdl/psi/zpacked.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2001-2024 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 | | /* Packed array operators */ |
18 | | #include "ghost.h" |
19 | | #include "ialloc.h" |
20 | | #include "idict.h" |
21 | | #include "iname.h" |
22 | | #include "istack.h" /* for iparray.h */ |
23 | | #include "ipacked.h" |
24 | | #include "iparray.h" |
25 | | #include "ivmspace.h" |
26 | | #include "oper.h" |
27 | | #include "store.h" |
28 | | #include "gxalloc.h" |
29 | | |
30 | | /* - currentpacking <bool> */ |
31 | | static int |
32 | | zcurrentpacking(i_ctx_t *i_ctx_p) |
33 | 7.14k | { |
34 | 7.14k | os_ptr op = osp; |
35 | | |
36 | 7.14k | push(1); |
37 | 7.14k | ref_assign(op, &ref_array_packing); |
38 | 7.14k | return 0; |
39 | 7.14k | } |
40 | | |
41 | | /* <obj_0> ... <obj_n-1> <n> packedarray <packedarray> */ |
42 | | int |
43 | | zpackedarray(i_ctx_t *i_ctx_p) |
44 | 123k | { |
45 | 123k | os_ptr op = osp; |
46 | 123k | int code; |
47 | 123k | ref parr; |
48 | | |
49 | 123k | check_type(*op, t_integer); |
50 | | |
51 | | /* This avoids a valgrind error */ |
52 | 123k | parr.tas.type_attrs = 0; |
53 | | |
54 | 123k | if (op->value.intval < 0) |
55 | 0 | return_error(gs_error_rangecheck); |
56 | 123k | if (op->value.intval > op - osbot && |
57 | 123k | op->value.intval >= ref_stack_count(&o_stack) |
58 | 123k | ) |
59 | 0 | return_error(gs_error_stackunderflow); |
60 | 123k | osp--; |
61 | 123k | code = make_packed_array(&parr, &o_stack, (uint) op->value.intval, |
62 | 123k | idmemory, "packedarray"); |
63 | 123k | osp++; |
64 | 123k | if (code >= 0) |
65 | 123k | *osp = parr; |
66 | 123k | return code; |
67 | 123k | } |
68 | | |
69 | | /* <bool> setpacking - */ |
70 | | static int |
71 | | zsetpacking(i_ctx_t *i_ctx_p) |
72 | 17.8k | { |
73 | 17.8k | os_ptr op = osp; |
74 | 17.8k | ref cont; |
75 | 17.8k | check_op(1); |
76 | 17.8k | check_type(*op, t_boolean); |
77 | 17.8k | make_struct(&cont, avm_local, ref_array_packing_container); |
78 | 17.8k | ref_assign_old(&cont, &ref_array_packing, op, "setpacking"); |
79 | 17.8k | pop(1); |
80 | 17.8k | return 0; |
81 | 17.8k | } |
82 | | |
83 | | /* ------ Non-operator routines ------ */ |
84 | | |
85 | | /* Make a packed array. See the comment in packed.h about */ |
86 | | /* ensuring that refs in mixed arrays are properly aligned. */ |
87 | | #undef idmemory /****** NOTA BENE ******/ |
88 | | int |
89 | | make_packed_array(ref * parr, ref_stack_t * pstack, uint size, |
90 | | gs_dual_memory_t *idmemory, client_name_t cname) |
91 | 8.35M | { |
92 | 8.35M | uint i; |
93 | 8.35M | const ref *pref; |
94 | 8.35M | uint idest = 0, ishort = 0; |
95 | 8.35M | ref_packed *pbody; |
96 | 8.35M | ref_packed *pdest; |
97 | 8.35M | ref_packed *pshort; /* points to start of */ |
98 | | /* last run of short elements */ |
99 | 8.35M | gs_ref_memory_t *imem = idmemory->current; |
100 | 8.35M | uint space = imemory_space(imem); |
101 | 8.35M | int skip = 0, pad; |
102 | 8.35M | ref rtemp; |
103 | 8.35M | int code; |
104 | | |
105 | | /* Do a first pass to calculate the size of the array, */ |
106 | | /* and to detect local-into-global stores. */ |
107 | | |
108 | 73.0M | for (i = size; i != 0; i--) { |
109 | 64.7M | pref = ref_stack_index(pstack, i - 1); |
110 | 64.7M | if (pref == NULL) |
111 | 0 | return_error(gs_error_stackunderflow); |
112 | | |
113 | 64.7M | switch (r_btype(pref)) { /* not r_type, opers are special */ |
114 | 46.7M | case t_name: |
115 | 46.7M | if (name_index(imem, pref) >= packed_name_max_index) |
116 | 4.27M | break; /* can't pack */ |
117 | 42.4M | idest++; |
118 | 42.4M | continue; |
119 | 7.84M | case t_integer: |
120 | 7.84M | if (pref->value.intval < packed_min_intval || |
121 | 7.84M | pref->value.intval > packed_max_intval |
122 | 7.84M | ) |
123 | 165k | break; |
124 | 7.67M | idest++; |
125 | 7.67M | continue; |
126 | 74.9k | case t_oparray: |
127 | | /* Check for local-into-global store. */ |
128 | 74.9k | store_check_space(space, pref); |
129 | | /* falls through */ |
130 | 247k | case t_operator: |
131 | 247k | { |
132 | 247k | uint oidx; |
133 | | |
134 | 247k | if (!r_has_attr(pref, a_executable)) |
135 | 0 | break; |
136 | 247k | oidx = op_index(pref); |
137 | 247k | if (oidx == 0 || oidx > packed_int_mask) |
138 | 0 | break; |
139 | 247k | } |
140 | 247k | idest++; |
141 | 247k | continue; |
142 | 9.92M | default: |
143 | | /* Check for local-into-global store. */ |
144 | 9.92M | store_check_space(space, pref); |
145 | 64.7M | } |
146 | | /* Can't pack this element, use a full ref. */ |
147 | | /* We may have to unpack up to align_packed_per_ref - 1 */ |
148 | | /* preceding short elements. */ |
149 | | /* If we are at the beginning of the array, however, */ |
150 | | /* we can just move the elements up. */ |
151 | 14.3M | { |
152 | 14.3M | int i = (idest - ishort) & (align_packed_per_ref - 1); |
153 | | |
154 | 14.3M | if (ishort == 0) /* first time */ |
155 | 5.53M | idest += skip = -i & (align_packed_per_ref - 1); |
156 | 8.82M | else |
157 | 8.82M | idest += (packed_per_ref - 1) * i; |
158 | 14.3M | } |
159 | 14.3M | ishort = idest += packed_per_ref; |
160 | 14.3M | } |
161 | 8.35M | pad = (packed_per_ref - idest % packed_per_ref) % packed_per_ref; /* padding at end */ |
162 | | |
163 | | /* Now we can allocate the array. */ |
164 | | |
165 | 8.35M | code = gs_alloc_ref_array(imem, &rtemp, 0, (idest + pad) / packed_per_ref, |
166 | 8.35M | cname); |
167 | 8.35M | if (code < 0) |
168 | 0 | return code; |
169 | 8.35M | pbody = (ref_packed *) rtemp.value.refs; |
170 | | |
171 | | /* Make sure any initial skipped elements contain legal packed */ |
172 | | /* refs, so that the garbage collector can scan storage. */ |
173 | | |
174 | 8.35M | pshort = pbody; |
175 | 14.8M | for (; skip; skip--) |
176 | 6.51M | *pbody++ = pt_tag(pt_integer); |
177 | 8.35M | pdest = pbody; |
178 | | |
179 | 73.0M | for (i = size; i != 0; i--) { |
180 | 64.7M | pref = ref_stack_index(pstack, i - 1); |
181 | 64.7M | if (pref == NULL) |
182 | 0 | return_error(gs_error_stackunderflow); |
183 | | |
184 | 64.7M | switch (r_btype(pref)) { /* not r_type, opers are special */ |
185 | 46.7M | case t_name: |
186 | 46.7M | { |
187 | 46.7M | uint nidx = name_index(imem, pref); |
188 | | |
189 | 46.7M | if (nidx >= packed_name_max_index) |
190 | 4.27M | break; /* can't pack */ |
191 | 42.4M | *pdest++ = nidx + |
192 | 42.4M | (r_has_attr(pref, a_executable) ? |
193 | 34.5M | pt_tag(pt_executable_name) : |
194 | 42.4M | pt_tag(pt_literal_name)); |
195 | 42.4M | } |
196 | 0 | continue; |
197 | 7.84M | case t_integer: |
198 | 7.84M | if (pref->value.intval < packed_min_intval || |
199 | 7.84M | pref->value.intval > packed_max_intval |
200 | 7.84M | ) |
201 | 165k | break; |
202 | 7.67M | *pdest++ = pt_tag(pt_integer) + |
203 | 7.67M | ((short)pref->value.intval - packed_min_intval); |
204 | 7.67M | continue; |
205 | 74.9k | case t_oparray: |
206 | 247k | case t_operator: |
207 | 247k | { |
208 | 247k | uint oidx; |
209 | | |
210 | 247k | if (!r_has_attr(pref, a_executable)) |
211 | 0 | break; |
212 | 247k | oidx = op_index(pref); |
213 | 247k | if (oidx == 0 || oidx > packed_int_mask) |
214 | 0 | break; |
215 | 247k | *pdest++ = pt_tag(pt_executable_operator) + oidx; |
216 | 247k | } |
217 | 0 | continue; |
218 | 64.7M | } |
219 | | /* Can't pack this element, use a full ref. */ |
220 | | /* We may have to unpack up to align_packed_per_ref - 1 */ |
221 | | /* preceding short elements. */ |
222 | | /* Note that if we are at the beginning of the array, */ |
223 | | /* 'skip' already ensures that we don't need to do this. */ |
224 | 14.3M | { |
225 | 14.3M | int i = (pdest - pshort) & (align_packed_per_ref - 1); |
226 | 14.3M | const ref_packed *psrc = pdest; |
227 | 14.3M | ref *pmove = |
228 | 14.3M | (ref *) (pdest += (packed_per_ref - 1) * i); |
229 | | |
230 | 14.3M | ref_assign_new(pmove, pref); |
231 | 22.6M | while (--i >= 0) { |
232 | 8.28M | --psrc; |
233 | 8.28M | --pmove; |
234 | 8.28M | packed_get(imem->non_gc_memory, psrc, pmove); |
235 | 8.28M | } |
236 | 14.3M | } |
237 | 14.3M | pshort = pdest += packed_per_ref; |
238 | 14.3M | } |
239 | | |
240 | 8.35M | { |
241 | 8.35M | int atype = |
242 | 8.35M | (pdest == pbody + size ? t_shortarray : t_mixedarray); |
243 | | |
244 | | /* Pad with legal packed refs so that the garbage collector */ |
245 | | /* can scan storage. */ |
246 | | |
247 | 42.8M | for (; pad; pad--) |
248 | 34.5M | *pdest++ = pt_tag(pt_integer); |
249 | | |
250 | | /* Finally, make the array. */ |
251 | | |
252 | 8.35M | ref_stack_pop(pstack, size); |
253 | 8.35M | make_tasv_new(parr, atype, a_readonly | space, size, |
254 | 8.35M | packed, pbody + skip); |
255 | 8.35M | } |
256 | 8.35M | return 0; |
257 | 8.35M | } |
258 | | |
259 | | /* ------ Initialization procedure ------ */ |
260 | | |
261 | | const op_def zpacked_op_defs[] = |
262 | | { |
263 | | {"0currentpacking", zcurrentpacking}, |
264 | | {"1packedarray", zpackedarray}, |
265 | | {"1setpacking", zsetpacking}, |
266 | | op_def_end(0) |
267 | | }; |