Coverage Report

Created: 2026-10-28 08:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/psi/zpacked.c
Line
Count
Source
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
658k
{
34
658k
    os_ptr op = osp;
35
36
658k
    push(1);
37
658k
    ref_assign(op, &ref_array_packing);
38
658k
    return 0;
39
658k
}
40
41
/* <obj_0> ... <obj_n-1> <n> packedarray <packedarray> */
42
int
43
zpackedarray(i_ctx_t *i_ctx_p)
44
8.92M
{
45
8.92M
    os_ptr op = osp;
46
8.92M
    int code;
47
8.92M
    ref parr;
48
49
8.92M
    check_type(*op, t_integer);
50
51
    /* This avoids a valgrind error */
52
8.92M
    parr.tas.type_attrs = 0;
53
54
8.92M
    if (op->value.intval < 0)
55
5
        return_error(gs_error_rangecheck);
56
8.92M
    if (op->value.intval > op - osbot &&
57
1
        op->value.intval >= ref_stack_count(&o_stack)
58
8.92M
        )
59
1
        return_error(gs_error_stackunderflow);
60
8.92M
    osp--;
61
8.92M
    code = make_packed_array(&parr, &o_stack, (uint) op->value.intval,
62
8.92M
                             idmemory, "packedarray");
63
8.92M
    osp++;
64
8.92M
    if (code >= 0)
65
8.92M
        *osp = parr;
66
8.92M
    return code;
67
8.92M
}
68
69
/* <bool> setpacking - */
70
static int
71
zsetpacking(i_ctx_t *i_ctx_p)
72
1.62M
{
73
1.62M
    os_ptr op = osp;
74
1.62M
    ref cont;
75
1.62M
    check_op(1);
76
1.62M
    check_type(*op, t_boolean);
77
1.62M
    make_struct(&cont, avm_local, ref_array_packing_container);
78
1.62M
    ref_assign_old(&cont, &ref_array_packing, op, "setpacking");
79
1.62M
    pop(1);
80
1.62M
    return 0;
81
1.62M
}
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
729M
{
92
729M
    uint i;
93
729M
    const ref *pref;
94
729M
    uint idest = 0, ishort = 0;
95
729M
    ref_packed *pbody;
96
729M
    ref_packed *pdest;
97
729M
    ref_packed *pshort;   /* points to start of */
98
                                /* last run of short elements */
99
729M
    gs_ref_memory_t *imem = idmemory->current;
100
729M
    uint space = imemory_space(imem);
101
729M
    int skip = 0, pad;
102
729M
    ref rtemp;
103
729M
    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
6.36G
    for (i = size; i != 0; i--) {
109
5.63G
        pref = ref_stack_index(pstack, i - 1);
110
5.63G
        if (pref == NULL)
111
0
            return_error(gs_error_stackunderflow);
112
113
5.63G
        switch (r_btype(pref)) { /* not r_type, opers are special */
114
4.06G
            case t_name:
115
4.06G
              if (name_index(imem, pref) >= packed_name_max_index)
116
368M
                    break;  /* can't pack */
117
3.69G
                idest++;
118
3.69G
                continue;
119
679M
            case t_integer:
120
679M
                if (pref->value.intval < packed_min_intval ||
121
679M
                    pref->value.intval > packed_max_intval
122
679M
                    )
123
14.5M
                    break;
124
665M
                idest++;
125
665M
                continue;
126
5.02M
            case t_oparray:
127
                /* Check for local-into-global store. */
128
5.02M
                store_check_space(space, pref);
129
                /* falls through */
130
18.8M
            case t_operator:
131
18.8M
                {
132
18.8M
                    uint oidx;
133
134
18.8M
                    if (!r_has_attr(pref, a_executable))
135
0
                        break;
136
18.8M
                    oidx = op_index(pref);
137
18.8M
                    if (oidx == 0 || oidx > packed_int_mask)
138
0
                        break;
139
18.8M
                }
140
18.8M
                idest++;
141
18.8M
                continue;
142
867M
            default:
143
                /* Check for local-into-global store. */
144
867M
                store_check_space(space, pref);
145
5.63G
        }
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
1.25G
        {
152
1.25G
            int i = (idest - ishort) & (align_packed_per_ref - 1);
153
154
1.25G
            if (ishort == 0)  /* first time */
155
481M
                idest += skip = -i & (align_packed_per_ref - 1);
156
769M
            else
157
769M
                idest += (packed_per_ref - 1) * i;
158
1.25G
        }
159
1.25G
        ishort = idest += packed_per_ref;
160
1.25G
    }
161
729M
    pad = (packed_per_ref - idest % packed_per_ref) % packed_per_ref; /* padding at end */
162
163
    /* Now we can allocate the array. */
164
165
729M
    code = gs_alloc_ref_array(imem, &rtemp, 0, (idest + pad) / packed_per_ref,
166
729M
                              cname);
167
729M
    if (code < 0)
168
0
        return code;
169
729M
    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
729M
    pshort = pbody;
175
1.29G
    for (; skip; skip--)
176
568M
        *pbody++ = pt_tag(pt_integer);
177
729M
    pdest = pbody;
178
179
6.36G
    for (i = size; i != 0; i--) {
180
5.63G
        pref = ref_stack_index(pstack, i - 1);
181
5.63G
        if (pref == NULL)
182
0
            return_error(gs_error_stackunderflow);
183
184
5.63G
        switch (r_btype(pref)) { /* not r_type, opers are special */
185
4.06G
            case t_name:
186
4.06G
                {
187
4.06G
                    uint nidx = name_index(imem, pref);
188
189
4.06G
                    if (nidx >= packed_name_max_index)
190
368M
                        break;  /* can't pack */
191
3.69G
                    *pdest++ = nidx +
192
3.69G
                        (r_has_attr(pref, a_executable) ?
193
3.01G
                         pt_tag(pt_executable_name) :
194
3.69G
                         pt_tag(pt_literal_name));
195
3.69G
                }
196
0
                continue;
197
679M
            case t_integer:
198
679M
                if (pref->value.intval < packed_min_intval ||
199
679M
                    pref->value.intval > packed_max_intval
200
679M
                    )
201
14.5M
                    break;
202
665M
                *pdest++ = pt_tag(pt_integer) +
203
665M
                    ((short)pref->value.intval - packed_min_intval);
204
665M
                continue;
205
5.02M
            case t_oparray:
206
18.8M
            case t_operator:
207
18.8M
                {
208
18.8M
                    uint oidx;
209
210
18.8M
                    if (!r_has_attr(pref, a_executable))
211
0
                        break;
212
18.8M
                    oidx = op_index(pref);
213
18.8M
                    if (oidx == 0 || oidx > packed_int_mask)
214
0
                        break;
215
18.8M
                    *pdest++ = pt_tag(pt_executable_operator) + oidx;
216
18.8M
                }
217
0
                continue;
218
5.63G
        }
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
1.25G
        {
225
1.25G
            int i = (pdest - pshort) & (align_packed_per_ref - 1);
226
1.25G
            const ref_packed *psrc = pdest;
227
1.25G
            ref *pmove =
228
1.25G
            (ref *) (pdest += (packed_per_ref - 1) * i);
229
230
1.25G
            ref_assign_new(pmove, pref);
231
1.96G
            while (--i >= 0) {
232
709M
                --psrc;
233
709M
                --pmove;
234
709M
                packed_get(imem->non_gc_memory, psrc, pmove);
235
709M
            }
236
1.25G
        }
237
1.25G
        pshort = pdest += packed_per_ref;
238
1.25G
    }
239
240
729M
    {
241
729M
        int atype =
242
729M
        (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
3.75G
        for (; pad; pad--)
248
3.02G
            *pdest++ = pt_tag(pt_integer);
249
250
        /* Finally, make the array. */
251
252
729M
        ref_stack_pop(pstack, size);
253
729M
        make_tasv_new(parr, atype, a_readonly | space, size,
254
729M
                      packed, pbody + skip);
255
729M
    }
256
729M
    return 0;
257
729M
}
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
};