1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
|
/* Copyright (C) 2001-2021 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied,
modified or distributed except as expressly authorized under the terms
of the license contained in the file LICENSE in this distribution.
Refer to licensing information at http://www.artifex.com or contact
Artifex Software, Inc., 1305 Grant Avenue - Suite 200, Novato,
CA 94945, U.S.A., +1(415)492-9861, for further information.
*/
/* Name lookup for Ghostscript interpreter */
#include "memory_.h"
#include "string_.h"
#include "ghost.h"
#include "gsstruct.h"
#include "gxobj.h" /* for o_set_unmarked */
#include "ierrors.h"
#include "inamedef.h"
#include "imemory.h" /* for isave.h */
#include "isave.h"
#include "store.h"
/* Public values */
const uint name_max_string = max_name_string;
/* Define the permutation table for name hashing. */
static const byte hash_permutation[256] = {
NAME_HASH_PERMUTATION_DATA
};
/* Define the data for the 1-character names. */
static const byte nt_1char_names[NT_1CHAR_SIZE] = {
NT_1CHAR_NAMES_DATA
};
static void gs_names_finalize(const gs_memory_t *, void *);
/* Structure descriptors */
gs_private_st_simple(st_name_sub_table, name_sub_table, "name_sub_table");
gs_private_st_composite(st_name_string_sub_table, name_string_sub_table_t,
"name_string_sub_table_t",
name_string_sub_enum_ptrs, name_string_sub_reloc_ptrs);
gs_private_st_composite_final(st_name_table, name_table, "name_table",
name_table_enum_ptrs, name_table_reloc_ptrs,gs_names_finalize);
/* Forward references */
static int name_alloc_sub(name_table *);
static void name_free_sub(name_table *, uint, bool);
static void name_scan_sub(name_table *, uint, bool, bool);
/* Debugging printout */
#ifdef DEBUG
static void
name_print(const char *msg, const name_table *nt, uint nidx, const int *pflag)
{
const name_string_t *pnstr = names_index_string_inline(nt, nidx);
const name *pname = names_index_ptr_inline(nt, nidx);
const byte *str = pnstr->string_bytes;
dmlprintf1(nt->memory, "[n]%s", msg);
if (pflag)
dmprintf1(nt->memory, "(%d)", *pflag);
dmprintf2(nt->memory, " ("PRI_INTPTR"#%u)", (intptr_t)pname, nidx);
debug_print_string(nt->memory, str, pnstr->string_size);
dmprintf2(nt->memory, "("PRI_INTPTR",%u)\n", (intptr_t)str, pnstr->string_size);
}
# define if_debug_name(msg, nt, nidx, pflag)\
if ( gs_debug_c('n') ) name_print(msg, nt, nidx, pflag)
#else
# define if_debug_name(msg, nt, nidx, pflag) DO_NOTHING
#endif
/* Initialize a name table */
name_table *
names_init(ulong count, gs_ref_memory_t *imem)
{
gs_memory_t *mem = (gs_memory_t *)imem;
name_table *nt;
int i;
if (count == 0)
count = max_name_count + 1L;
else if (count - 1 > max_name_count)
return 0;
nt = gs_alloc_struct(mem, name_table, &st_name_table, "name_init(nt)");
if (nt == 0)
return 0;
memset(nt, 0, sizeof(name_table));
nt->max_sub_count =
((count - 1) | nt_sub_index_mask) >> nt_log2_sub_size;
nt->name_string_attrs = imemory_space(imem) | a_readonly;
nt->memory = mem;
/* Initialize the one-character names. */
/* Start by creating the necessary sub-tables. */
for (i = 0; i < NT_1CHAR_FIRST + NT_1CHAR_SIZE; i += nt_sub_size) {
int code = name_alloc_sub(nt);
if (code < 0) {
names_free(nt);
return 0;
}
}
for (i = -1; i < NT_1CHAR_SIZE; i++) {
uint ncnt = NT_1CHAR_FIRST + i;
uint nidx = name_count_to_index(ncnt);
name *pname = names_index_ptr_inline(nt, nidx);
name_string_t *pnstr = names_index_string_inline(nt, nidx);
if (i < 0)
pnstr->string_bytes = nt_1char_names,
pnstr->string_size = 0;
else
pnstr->string_bytes = nt_1char_names + i,
pnstr->string_size = 1;
pnstr->foreign_string = 1;
pnstr->mark = 1;
pname->pvalue = pv_no_defn;
}
nt->perm_count = NT_1CHAR_FIRST + NT_1CHAR_SIZE;
/* Reconstruct the free list. */
nt->free = 0;
names_trace_finish(nt, NULL);
return nt;
}
/* Free a name table */
void
names_free(name_table *nt)
{
if (nt == NULL) return;
while (nt->sub_count > 0)
name_free_sub(nt, --(nt->sub_count), false);
gs_free_object(nt->memory, nt, "name_init(nt)");
}
static void
gs_names_finalize(const gs_memory_t *cmem, void *vptr)
{
(void)vptr; /* unused */
cmem->gs_lib_ctx->gs_name_table = NULL;
}
/* Get the allocator for the name table. */
gs_memory_t *
names_memory(const name_table * nt)
{
return nt->memory;
}
/* Look up or enter a name in the table. */
/* Return 0 or an error code. */
/* The return may overlap the characters of the string! */
/* See iname.h for the meaning of enterflag. */
int
names_ref(name_table *nt, const byte *ptr, uint size, ref *pref, int enterflag)
{
name *pname;
name_string_t *pnstr;
uint nidx;
uint *phash;
/* Compute a hash for the string. */
/* Make a special check for 1-character names. */
switch (size) {
case 0:
nidx = name_count_to_index(1);
pname = names_index_ptr_inline(nt, nidx);
goto mkn;
case 1:
if (*ptr < NT_1CHAR_SIZE) {
uint hash = *ptr + NT_1CHAR_FIRST;
nidx = name_count_to_index(hash);
pname = names_index_ptr_inline(nt, nidx);
goto mkn;
}
/* falls through */
default: {
uint hash;
NAME_HASH(hash, hash_permutation, ptr, size);
phash = nt->hash + (hash & (NT_HASH_SIZE - 1));
}
}
for (nidx = *phash; nidx != 0;
nidx = name_next_index(nidx, pnstr)
) {
pnstr = names_index_string_inline(nt, nidx);
if (pnstr->string_size == size &&
!memcmp_inline(ptr, pnstr->string_bytes, size)
) {
pname = name_index_ptr_inline(nt, nidx);
goto mkn;
}
}
/* Name was not in the table. Make a new entry. */
if (enterflag < 0)
return_error(gs_error_undefined);
if (size > max_name_string)
return_error(gs_error_limitcheck);
nidx = nt->free;
if (nidx == 0) {
int code = name_alloc_sub(nt);
if (code < 0)
return code;
nidx = nt->free;
}
pnstr = names_index_string_inline(nt, nidx);
if (enterflag == 1) {
byte *cptr = (byte *)gs_alloc_string(nt->memory, size,
"names_ref(string)");
if (cptr == 0)
return_error(gs_error_VMerror);
memcpy(cptr, ptr, size);
pnstr->string_bytes = cptr;
pnstr->foreign_string = 0;
} else {
pnstr->string_bytes = ptr;
pnstr->foreign_string = (enterflag == 0 ? 1 : 0);
}
pnstr->string_size = size;
pname = name_index_ptr_inline(nt, nidx);
pname->pvalue = pv_no_defn;
nt->free = name_next_index(nidx, pnstr);
set_name_next_index(nidx, pnstr, *phash);
*phash = nidx;
if_debug_name("new name", nt, nidx, &enterflag);
mkn:
make_name(pref, nidx, pname);
return 0;
}
/* Get the string for a name. */
void
names_string_ref(const name_table * nt, const ref * pnref /* t_name */ ,
ref * psref /* result, t_string */ )
{
const name_string_t *pnstr = names_string_inline(nt, pnref);
make_const_string(psref,
(pnstr->foreign_string ? avm_foreign | a_readonly :
nt->name_string_attrs),
pnstr->string_size,
(const byte *)pnstr->string_bytes);
}
/* Convert a t_string object to a name. */
/* Copy the executable attribute. */
int
names_from_string(name_table * nt, const ref * psref, ref * pnref)
{
int exec = r_has_attr(psref, a_executable);
int code = names_ref(nt, psref->value.bytes, r_size(psref), pnref, 1);
if (code < 0)
return code;
if (exec)
r_set_attrs(pnref, a_executable);
return code;
}
/* Enter a (permanently allocated) C string as a name. */
int
names_enter_string(name_table * nt, const char *str, ref * pref)
{
return names_ref(nt, (const byte *)str, strlen(str), pref, 0);
}
/* Invalidate the value cache for a name. */
void
names_invalidate_value_cache(name_table * nt, const ref * pnref)
{
pnref->value.pname->pvalue = pv_other;
}
/* Convert between names and indices. */
#undef names_index
name_index_t
names_index(const name_table * nt, const ref * pnref)
{
return names_index_inline(nt, pnref);
}
void
names_index_ref(const name_table * nt, name_index_t index, ref * pnref)
{
names_index_ref_inline(nt, index, pnref);
}
name *
names_index_ptr(const name_table * nt, name_index_t index)
{
return names_index_ptr_inline(nt, index);
}
/* Get the index of the next valid name. */
/* The argument is 0 or a valid index. */
/* Return 0 if there are no more. */
name_index_t
names_next_valid_index(name_table * nt, name_index_t nidx)
{
const name_string_sub_table_t *ssub =
nt->sub[nidx >> nt_log2_sub_size].strings;
const name_string_t *pnstr;
do {
++nidx;
if ((nidx & nt_sub_index_mask) == 0)
for (;; nidx += nt_sub_size) {
if ((nidx >> nt_log2_sub_size) >= nt->sub_count)
return 0;
ssub = nt->sub[nidx >> nt_log2_sub_size].strings;
if (ssub != 0)
break;
}
pnstr = &ssub->strings[nidx & nt_sub_index_mask];
}
while (pnstr->string_bytes == 0);
return nidx;
}
/* ------ Garbage collection ------ */
/* Unmark all non-permanent names before a garbage collection. */
void
names_unmark_all(name_table * nt)
{
uint si;
name_string_sub_table_t *ssub;
for (si = 0; si < nt->sub_count; ++si)
if ((ssub = nt->sub[si].strings) != 0) {
uint i;
/* We can make the test much more efficient if we want.... */
for (i = 0; i < nt_sub_size; ++i)
if (name_index_to_count((si << nt_log2_sub_size) + i) >=
nt->perm_count)
ssub->strings[i].mark = 0;
}
}
/* Mark a name. Return true if new mark. We export this so we can mark */
/* character names in the character cache. */
bool
names_mark_index(name_table * nt, name_index_t nidx)
{
name_string_t *pnstr = names_index_string_inline(nt, nidx);
if (pnstr->mark)
return false;
pnstr->mark = 1;
return true;
}
/* Get the object (sub-table) containing a name. */
/* The garbage collector needs this so it can relocate pointers to names. */
void /*obj_header_t */ *
names_ref_sub_table(name_table * nt, const ref * pnref)
{
/* When this procedure is called, the pointers from the name table */
/* to the sub-tables may or may not have been relocated already, */
/* so we can't use them. Instead, we have to work backwards from */
/* the name pointer itself. */
return pnref->value.pname - (r_size(pnref) & nt_sub_index_mask);
}
void /*obj_header_t */ *
names_index_sub_table(name_table * nt, name_index_t index)
{
return nt->sub[index >> nt_log2_sub_size].names;
}
void /*obj_header_t */ *
names_index_string_sub_table(name_table * nt, name_index_t index)
{
return nt->sub[index >> nt_log2_sub_size].strings;
}
/*
* Clean up the name table after the trace/mark phase of a garbage
* collection, by removing names that aren't marked. gcst == NULL indicates
* we're doing this for initialization or restore rather than for a GC.
*/
void
names_trace_finish(name_table * nt, gc_state_t * gcst)
{
uint *phash = &nt->hash[0];
int i;
for (i = 0; i < NT_HASH_SIZE; phash++, i++) {
name_index_t prev = 0;
/*
* The following initialization is only to pacify compilers:
* pnprev is only referenced if prev has been set in the loop,
* in which case pnprev is also set.
*/
name_string_t *pnprev = 0;
name_index_t nidx = *phash;
while (nidx != 0) {
name_string_t *pnstr = names_index_string_inline(nt, nidx);
name_index_t next = name_next_index(nidx, pnstr);
if (pnstr->mark) {
prev = nidx;
pnprev = pnstr;
} else {
if_debug_name("GC remove name", nt, nidx, NULL);
/* Zero out the string data for the GC. */
pnstr->string_bytes = 0;
pnstr->string_size = 0;
if (prev == 0)
*phash = next;
else
set_name_next_index(prev, pnprev, next);
}
nidx = next;
}
}
/* Reconstruct the free list. */
nt->free = 0;
for (i = nt->sub_count; --i >= 0;) {
name_sub_table *sub = nt->sub[i].names;
if (sub != 0) {
name_scan_sub(nt, i, true, true && (gcst != 0));
}
}
nt->sub_next = 0;
}
/* ------ Save/restore ------ */
/* Clean up the name table before a restore. */
/* Currently, this is never called, because the name table is allocated */
/* in system VM. However, for a Level 1 system, we might choose to */
/* allocate the name table in global VM; in this case, this routine */
/* would be called before doing the global part of a top-level restore. */
/* Currently we don't make any attempt to optimize this. */
void
names_restore(name_table * nt, alloc_save_t * save)
{
/* We simply mark all names older than the save, */
/* and let names_trace_finish sort everything out. */
uint si;
for (si = 0; si < nt->sub_count; ++si)
if (nt->sub[si].strings != 0) {
uint i;
for (i = 0; i < nt_sub_size; ++i) {
name_string_t *pnstr =
names_index_string_inline(nt, (si << nt_log2_sub_size) + i);
if (pnstr->string_bytes == 0)
pnstr->mark = 0;
else if (pnstr->foreign_string) {
/* Avoid storing into a read-only name string. */
if (!pnstr->mark)
pnstr->mark = 1;
} else
pnstr->mark =
!alloc_is_since_save(pnstr->string_bytes, save);
}
}
names_trace_finish(nt, NULL);
}
/* ------ Internal procedures ------ */
/* Allocate the next sub-table. */
static int
name_alloc_sub(name_table * nt)
{
gs_memory_t *mem = nt->memory;
uint sub_index = nt->sub_next;
name_sub_table *sub;
name_string_sub_table_t *ssub;
for (;; ++sub_index) {
if (sub_index > nt->max_sub_count)
return_error(gs_error_limitcheck);
if (nt->sub[sub_index].names == 0)
break;
}
nt->sub_next = sub_index + 1;
if (nt->sub_next > nt->sub_count)
nt->sub_count = nt->sub_next;
sub = gs_alloc_struct(mem, name_sub_table, &st_name_sub_table,
"name_alloc_sub(sub-table)");
ssub = gs_alloc_struct(mem, name_string_sub_table_t,
&st_name_string_sub_table,
"name_alloc_sub(string sub-table)");
if (sub == 0 || ssub == 0) {
gs_free_object(mem, ssub, "name_alloc_sub(string sub-table)");
gs_free_object(mem, sub, "name_alloc_sub(sub-table)");
return_error(gs_error_VMerror);
}
memset(sub, 0, sizeof(name_sub_table));
memset(ssub, 0, sizeof(name_string_sub_table_t));
/* The following code is only used if EXTEND_NAMES is non-zero. */
#if name_extension_bits > 0
sub->high_index = (sub_index >> (16 - nt_log2_sub_size)) << 16;
#endif
nt->sub[sub_index].names = sub;
nt->sub[sub_index].strings = ssub;
/* Add the newly allocated entries to the free list. */
/* Note that the free list will only be properly sorted if */
/* it was empty initially. */
name_scan_sub(nt, sub_index, false, false);
#ifdef DEBUG
if (gs_debug_c('n')) { /* Print the lengths of the hash chains. */
int i0;
for (i0 = 0; i0 < NT_HASH_SIZE; i0 += 16) {
int i;
dmlprintf1(mem, "[n]chain %d:", i0);
for (i = i0; i < i0 + 16; i++) {
int n = 0;
uint nidx;
for (nidx = nt->hash[i]; nidx != 0;
nidx = name_next_index(nidx,
names_index_string_inline(nt, nidx))
)
n++;
dmprintf1(mem, " %d", n);
}
dmputc(mem, '\n');
}
}
#endif
return 0;
}
/* Free a sub-table. */
static void
name_free_sub(name_table * nt, uint sub_index, bool unmark)
{
/* If the subtable is in a previous save level, gs_free_object()
* may not actually free the memory, in case that happens, we need
* to explicitly remove the gc mark if requested.
*/
if (unmark) {
name_sub_table *sub = nt->sub[sub_index].names;
name_string_sub_table_t *ssub = nt->sub[sub_index].strings;
o_set_unmarked((obj_header_t *)sub - 1);
o_set_unmarked((obj_header_t *)ssub - 1);
}
gs_free_object(nt->memory, nt->sub[sub_index].strings,
"name_free_sub(string sub-table)");
gs_free_object(nt->memory, nt->sub[sub_index].names,
"name_free_sub(sub-table)");
nt->sub[sub_index].names = 0;
nt->sub[sub_index].strings = 0;
}
/* Scan a sub-table and add unmarked entries to the free list. */
/* We add the entries in decreasing count order, so the free list */
/* will stay sorted. If all entries are unmarked and free_empty is true, */
/* free the sub-table. */
static void
name_scan_sub(name_table * nt, uint sub_index, bool free_empty, bool unmark)
{
name_string_sub_table_t *ssub = nt->sub[sub_index].strings;
uint free = nt->free;
uint nbase = sub_index << nt_log2_sub_size;
uint ncnt = nbase + (nt_sub_size - 1);
bool keep = !free_empty;
if (ssub == 0)
return;
if (nbase == 0)
nbase = 1, keep = true; /* don't free name 0 */
for (;; --ncnt) {
uint nidx = name_count_to_index(ncnt);
name_string_t *pnstr = &ssub->strings[nidx & nt_sub_index_mask];
if (pnstr->mark)
keep = true;
else {
set_name_next_index(nidx, pnstr, free);
free = nidx;
}
if (ncnt == nbase)
break;
}
if (keep)
nt->free = free;
else {
/* No marked entries, free the sub-table. */
name_free_sub(nt, sub_index, unmark);
if (sub_index == nt->sub_count - 1) {
/* Back up over a final run of deleted sub-tables. */
do {
--sub_index;
} while (nt->sub[sub_index].names == 0);
nt->sub_count = sub_index + 1;
if (nt->sub_next > sub_index)
nt->sub_next = sub_index;
} else if (nt->sub_next == sub_index)
nt->sub_next--;
}
}
/* Garbage collector enumeration and relocation procedures. */
static
ENUM_PTRS_BEGIN_PROC(name_table_enum_ptrs)
{
EV_CONST name_table *const nt = vptr;
uint i = index >> 1;
if (i >= nt->sub_count)
return 0;
if (index & 1)
ENUM_RETURN(nt->sub[i].strings);
else
ENUM_RETURN(nt->sub[i].names);
}
ENUM_PTRS_END_PROC
static RELOC_PTRS_WITH(name_table_reloc_ptrs, name_table *nt)
{
uint sub_count = nt->sub_count;
uint i;
/* Now we can relocate the sub-table pointers. */
for (i = 0; i < sub_count; i++) {
RELOC_VAR(nt->sub[i].names);
RELOC_VAR(nt->sub[i].strings);
}
/*
* We also need to relocate the cached value pointers.
* We don't do this here, but in a separate scan over the
* permanent dictionaries, at the very end of garbage collection.
*/
}
RELOC_PTRS_END
static ENUM_PTRS_BEGIN_PROC(name_string_sub_enum_ptrs)
{
return 0;
}
ENUM_PTRS_END_PROC
static RELOC_PTRS_BEGIN(name_string_sub_reloc_ptrs)
{
name_string_t *pnstr = ((name_string_sub_table_t *)vptr)->strings;
uint i;
for (i = 0; i < nt_sub_size; ++pnstr, ++i) {
if (pnstr->string_bytes != 0 && !pnstr->foreign_string) {
gs_const_string nstr;
nstr.data = pnstr->string_bytes;
nstr.size = pnstr->string_size;
RELOC_CONST_STRING_VAR(nstr);
pnstr->string_bytes = nstr.data;
}
}
}
RELOC_PTRS_END
|