idstack.c 7.2 KB

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