idstack.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* Copyright (C) 1998 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: idstack.c,v 1.2 2000/09/19 19:00:43 lpd Exp $ */
  16. /* Implementation of dictionary stacks */
  17. #include "ghost.h"
  18. #include "idict.h"
  19. #include "idictdef.h"
  20. #include "idstack.h"
  21. #include "inamedef.h"
  22. #include "iname.h"
  23. #include "ipacked.h"
  24. #include "iutil.h"
  25. #include "ivmspace.h"
  26. /* Debugging statistics */
  27. #ifdef DEBUG
  28. #include "idebug.h"
  29. #define MAX_STATS_DEPTH 6
  30. struct stats_dstack_s {
  31. long lookups; /* total lookups */
  32. long probes[2]; /* successful lookups on 1 or 2 probes */
  33. long depth[MAX_STATS_DEPTH + 1]; /* stack depth of lookups requiring search */
  34. } stats_dstack;
  35. # define INCR(v) (++stats_dstack.v)
  36. #else
  37. # define INCR(v) DO_NOTHING
  38. #endif
  39. #ifdef DEBUG
  40. /* Wrapper for dstack_find_name_by_index */
  41. ref *real_dstack_find_name_by_index(P2(dict_stack_t * pds, uint nidx));
  42. ref *
  43. dstack_find_name_by_index(dict_stack_t * pds, uint nidx)
  44. {
  45. ref *pvalue = real_dstack_find_name_by_index(pds, nidx);
  46. dict *pdict = pds->stack.p->value.pdict;
  47. INCR(lookups);
  48. if (dict_is_packed(pdict)) {
  49. uint hash =
  50. dict_hash_mod(dict_name_index_hash(nidx), npairs(pdict)) + 1;
  51. if (pdict->keys.value.packed[hash] ==
  52. pt_tag(pt_literal_name) + nidx
  53. )
  54. INCR(probes[0]);
  55. else if (pdict->keys.value.packed[hash - 1] ==
  56. pt_tag(pt_literal_name) + nidx
  57. )
  58. INCR(probes[1]);
  59. }
  60. if (gs_debug_c('d') && !(stats_dstack.lookups % 1000))
  61. dlprintf3("[d]lookups=%ld probe1=%ld probe2=%ld\n",
  62. stats_dstack.lookups, stats_dstack.probes[0],
  63. stats_dstack.probes[1]);
  64. return pvalue;
  65. }
  66. #define dstack_find_name_by_index real_dstack_find_name_by_index
  67. #endif
  68. /* Check whether a dictionary is one of the permanent ones on the d-stack. */
  69. bool
  70. dstack_dict_is_permanent(const dict_stack_t * pds, const ref * pdref)
  71. {
  72. dict *pdict = pdref->value.pdict;
  73. int i;
  74. if (pds->stack.extension_size == 0) { /* Only one block of d-stack. */
  75. for (i = 0; i < pds->min_size; ++i)
  76. if (pds->stack.bot[i].value.pdict == pdict)
  77. return true;
  78. } else { /* More than one block of d-stack. */
  79. uint count = ref_stack_count(&pds->stack);
  80. for (i = count - pds->min_size; i < count; ++i)
  81. if (ref_stack_index(&pds->stack, i)->value.pdict == pdict)
  82. return true;
  83. }
  84. return false;
  85. }
  86. /*
  87. * Look up a name on the dictionary stack.
  88. * Return the pointer to the value if found, 0 if not.
  89. */
  90. ref *
  91. dstack_find_name_by_index(dict_stack_t * pds, uint nidx)
  92. {
  93. ds_ptr pdref = pds->stack.p;
  94. /* Since we know the hash function is the identity function, */
  95. /* there's no point in allocating a separate variable for it. */
  96. #define hash dict_name_index_hash(nidx)
  97. ref_packed kpack = packed_name_key(nidx);
  98. do {
  99. dict *pdict = pdref->value.pdict;
  100. uint size = npairs(pdict);
  101. #ifdef DEBUG
  102. if (gs_debug_c('D')) {
  103. ref dnref;
  104. name_index_ref(nidx, &dnref);
  105. dlputs("[D]lookup ");
  106. debug_print_name(&dnref);
  107. dprintf3(" in 0x%lx(%u/%u)\n",
  108. (ulong) pdict, dict_length(pdref),
  109. dict_maxlength(pdref));
  110. }
  111. #endif
  112. #define INCR_DEPTH(pdref)\
  113. INCR(depth[min(MAX_STATS_DEPTH, pds->stack.p - pdref)])
  114. if (dict_is_packed(pdict)) {
  115. packed_search_1(INCR_DEPTH(pdref),
  116. return packed_search_value_pointer,
  117. DO_NOTHING, goto miss);
  118. packed_search_2(INCR_DEPTH(pdref),
  119. return packed_search_value_pointer,
  120. DO_NOTHING, break);
  121. miss:;
  122. } else {
  123. ref *kbot = pdict->keys.value.refs;
  124. register ref *kp;
  125. int wrap = 0;
  126. /* Search the dictionary */
  127. for (kp = kbot + dict_hash_mod(hash, size) + 2;;) {
  128. --kp;
  129. if (r_has_type(kp, t_name)) {
  130. if (name_index(kp) == nidx) {
  131. INCR_DEPTH(pdref);
  132. return pdict->values.value.refs + (kp - kbot);
  133. }
  134. } else if (r_has_type(kp, t_null)) { /* Empty, deleted, or wraparound. */
  135. /* Figure out which. */
  136. if (!r_has_attr(kp, a_executable))
  137. break;
  138. if (kp == kbot) { /* wrap */
  139. if (wrap++)
  140. break; /* 2 wraps */
  141. kp += size + 1;
  142. }
  143. }
  144. }
  145. }
  146. #undef INCR_DEPTH
  147. }
  148. while (pdref-- > pds->stack.bot);
  149. /* The name isn't in the top dictionary block. */
  150. /* If there are other blocks, search them now (more slowly). */
  151. if (!pds->stack.extension_size) /* no more blocks */
  152. return (ref *) 0;
  153. { /* We could use the STACK_LOOP macros, but for now, */
  154. /* we'll do things the simplest way. */
  155. ref key;
  156. uint i = pds->stack.p + 1 - pds->stack.bot;
  157. uint size = ref_stack_count(&pds->stack);
  158. ref *pvalue;
  159. name_index_ref(nidx, &key);
  160. for (; i < size; i++) {
  161. if (dict_find(ref_stack_index(&pds->stack, i),
  162. &key, &pvalue) > 0
  163. ) {
  164. INCR(depth[min(MAX_STATS_DEPTH, i)]);
  165. return pvalue;
  166. }
  167. }
  168. }
  169. return (ref *) 0;
  170. #undef hash
  171. }
  172. /* Set the cached values computed from the top entry on the dstack. */
  173. /* See idstack.h for details. */
  174. private const ref_packed no_packed_keys[2] =
  175. {packed_key_deleted, packed_key_empty};
  176. void
  177. dstack_set_top(dict_stack_t * pds)
  178. {
  179. ds_ptr dsp = pds->stack.p;
  180. dict *pdict = dsp->value.pdict;
  181. if_debug3('d', "[d]dsp = 0x%lx -> 0x%lx, key array type = %d\n",
  182. (ulong) dsp, (ulong) pdict, r_type(&pdict->keys));
  183. if (dict_is_packed(pdict) &&
  184. r_has_attr(dict_access_ref(dsp), a_read)
  185. ) {
  186. pds->top_keys = pdict->keys.value.packed;
  187. pds->top_npairs = npairs(pdict);
  188. pds->top_values = pdict->values.value.refs;
  189. } else {
  190. pds->top_keys = no_packed_keys;
  191. pds->top_npairs = 1;
  192. }
  193. if (!r_has_attr(dict_access_ref(dsp), a_write))
  194. pds->def_space = -1;
  195. else
  196. pds->def_space = r_space(dsp);
  197. }
  198. /* After a garbage collection, scan the permanent dictionaries and */
  199. /* update the cached value pointers in names. */
  200. void
  201. dstack_gc_cleanup(dict_stack_t * pds)
  202. {
  203. uint count = ref_stack_count(&pds->stack);
  204. uint dsi;
  205. for (dsi = pds->min_size; dsi > 0; --dsi) {
  206. const dict *pdict =
  207. ref_stack_index(&pds->stack, count - dsi)->value.pdict;
  208. uint size = nslots(pdict);
  209. ref *pvalue = pdict->values.value.refs;
  210. uint i;
  211. for (i = 0; i < size; ++i, ++pvalue) {
  212. ref key;
  213. ref *old_pvalue;
  214. array_get(&pdict->keys, (long)i, &key);
  215. if (r_has_type(&key, t_name) &&
  216. pv_valid(old_pvalue = key.value.pname->pvalue)
  217. ) { /*
  218. * The name only has a single definition,
  219. * so it must be this one. Check to see if
  220. * no relocation is actually needed; if so,
  221. * we can skip the entire dictionary.
  222. */
  223. if (old_pvalue == pvalue) {
  224. if_debug1('d', "[d]skipping dstack entry %d\n",
  225. dsi - 1);
  226. break;
  227. }
  228. /* Update the value pointer. */
  229. key.value.pname->pvalue = pvalue;
  230. }
  231. }
  232. }
  233. }