Coverage Report

Created: 2022-10-31 07:00

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