igcstr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /* Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000 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: igcstr.c,v 1.7 2005/09/05 13:58:55 leonardo Exp $ */
  14. /* String GC routines for Ghostscript */
  15. #include "memory_.h"
  16. #include "ghost.h"
  17. #include "gsmdebug.h"
  18. #include "gsstruct.h"
  19. #include "iastate.h"
  20. #include "igcstr.h"
  21. /* Forward references */
  22. private bool gc_mark_string(const byte *, uint, bool, const chunk_t *);
  23. /* (Un)mark the strings in a chunk. */
  24. void
  25. gc_strings_set_marks(chunk_t * cp, bool mark)
  26. {
  27. if (cp->smark != 0) {
  28. if_debug3('6', "[6]clearing string marks 0x%lx[%u] to %d\n",
  29. (ulong) cp->smark, cp->smark_size, (int)mark);
  30. memset(cp->smark, 0, cp->smark_size);
  31. if (mark)
  32. gc_mark_string(cp->sbase, cp->climit - cp->sbase, true, cp);
  33. }
  34. }
  35. /* We mark strings a word at a time. */
  36. typedef string_mark_unit bword;
  37. #define bword_log2_bytes log2_sizeof_string_mark_unit
  38. #define bword_bytes (1 << bword_log2_bytes)
  39. #define bword_log2_bits (bword_log2_bytes + 3)
  40. #define bword_bits (1 << bword_log2_bits)
  41. #define bword_1s (~(bword)0)
  42. /* Compensate for byte order reversal if necessary. */
  43. #if arch_is_big_endian
  44. # if bword_bytes == 2
  45. # define bword_swap_bytes(m) m = (m << 8) | (m >> 8)
  46. # else /* bword_bytes == 4 */
  47. # define bword_swap_bytes(m)\
  48. m = (m << 24) | ((m & 0xff00) << 8) | ((m >> 8) & 0xff00) | (m >> 24)
  49. # endif
  50. #else
  51. # define bword_swap_bytes(m) DO_NOTHING
  52. #endif
  53. /* (Un)mark a string in a known chunk. Return true iff any new marks. */
  54. private bool
  55. gc_mark_string(const byte * ptr, uint size, bool set, const chunk_t * cp)
  56. {
  57. uint offset = ptr - cp->sbase;
  58. bword *bp = (bword *) (cp->smark + ((offset & -bword_bits) >> 3));
  59. uint bn = offset & (bword_bits - 1);
  60. bword m = bword_1s << bn;
  61. uint left = size;
  62. bword marks = 0;
  63. bword_swap_bytes(m);
  64. if (set) {
  65. if (left + bn >= bword_bits) {
  66. marks |= ~*bp & m;
  67. *bp |= m;
  68. m = bword_1s, left -= bword_bits - bn, bp++;
  69. while (left >= bword_bits) {
  70. marks |= ~*bp;
  71. *bp = bword_1s;
  72. left -= bword_bits, bp++;
  73. }
  74. }
  75. if (left) {
  76. bword_swap_bytes(m);
  77. m -= m << left;
  78. bword_swap_bytes(m);
  79. marks |= ~*bp & m;
  80. *bp |= m;
  81. }
  82. } else {
  83. if (left + bn >= bword_bits) {
  84. *bp &= ~m;
  85. m = bword_1s, left -= bword_bits - bn, bp++;
  86. if (left >= bword_bits * 5) {
  87. memset(bp, 0, (left & -bword_bits) >> 3);
  88. bp += left >> bword_log2_bits;
  89. left &= bword_bits - 1;
  90. } else
  91. while (left >= bword_bits) {
  92. *bp = 0;
  93. left -= bword_bits, bp++;
  94. }
  95. }
  96. if (left) {
  97. bword_swap_bytes(m);
  98. m -= m << left;
  99. bword_swap_bytes(m);
  100. *bp &= ~m;
  101. }
  102. }
  103. return marks != 0;
  104. }
  105. /* Mark a string. Return true if any new marks. */
  106. bool
  107. gc_string_mark(const byte * ptr, uint size, bool set, gc_state_t * gcst)
  108. {
  109. const chunk_t *cp;
  110. bool marks;
  111. if (size == 0)
  112. return false;
  113. #define dprintstr()\
  114. dputc('('); fwrite(ptr, 1, min(size, 20), dstderr);\
  115. dputs((size <= 20 ? ")" : "...)"))
  116. if (!(cp = gc_locate(ptr, gcst))) { /* not in a chunk */
  117. #ifdef DEBUG
  118. if (gs_debug_c('5')) {
  119. dlprintf2("[5]0x%lx[%u]", (ulong) ptr, size);
  120. dprintstr();
  121. dputs(" not in a chunk\n");
  122. }
  123. #endif
  124. return false;
  125. }
  126. if (cp->smark == 0) /* not marking strings */
  127. return false;
  128. #ifdef DEBUG
  129. if (ptr < cp->ctop) {
  130. lprintf4("String pointer 0x%lx[%u] outside [0x%lx..0x%lx)\n",
  131. (ulong) ptr, size, (ulong) cp->ctop, (ulong) cp->climit);
  132. return false;
  133. } else if (ptr + size > cp->climit) { /*
  134. * If this is the bottommost string in a chunk that has
  135. * an inner chunk, the string's starting address is both
  136. * cp->ctop of the outer chunk and cp->climit of the inner;
  137. * gc_locate may incorrectly attribute the string to the
  138. * inner chunk because of this. This doesn't affect
  139. * marking or relocation, since the machinery for these
  140. * is all associated with the outermost chunk,
  141. * but it can cause the validity check to fail.
  142. * Check for this case now.
  143. */
  144. const chunk_t *scp = cp;
  145. while (ptr == scp->climit && scp->outer != 0)
  146. scp = scp->outer;
  147. if (ptr + size > scp->climit) {
  148. lprintf4("String pointer 0x%lx[%u] outside [0x%lx..0x%lx)\n",
  149. (ulong) ptr, size,
  150. (ulong) scp->ctop, (ulong) scp->climit);
  151. return false;
  152. }
  153. }
  154. #endif
  155. marks = gc_mark_string(ptr, size, set, cp);
  156. #ifdef DEBUG
  157. if (gs_debug_c('5')) {
  158. dlprintf4("[5]%s%smarked 0x%lx[%u]",
  159. (marks ? "" : "already "), (set ? "" : "un"),
  160. (ulong) ptr, size);
  161. dprintstr();
  162. dputc('\n');
  163. }
  164. #endif
  165. return marks;
  166. }
  167. /* Clear the relocation for strings. */
  168. /* This requires setting the marks. */
  169. void
  170. gc_strings_clear_reloc(chunk_t * cp)
  171. {
  172. if (cp->sreloc != 0) {
  173. gc_strings_set_marks(cp, true);
  174. if_debug1('6', "[6]clearing string reloc 0x%lx\n",
  175. (ulong) cp->sreloc);
  176. gc_strings_set_reloc(cp);
  177. }
  178. }
  179. /* Count the 0-bits in a byte. */
  180. private const byte count_zero_bits_table[256] =
  181. {
  182. #define o4(n) n,n-1,n-1,n-2
  183. #define o16(n) o4(n),o4(n-1),o4(n-1),o4(n-2)
  184. #define o64(n) o16(n),o16(n-1),o16(n-1),o16(n-2)
  185. o64(8), o64(7), o64(7), o64(6)
  186. };
  187. #define byte_count_zero_bits(byt)\
  188. (uint)(count_zero_bits_table[byt])
  189. #define byte_count_one_bits(byt)\
  190. (uint)(8 - count_zero_bits_table[byt])
  191. /* Set the relocation for the strings in a chunk. */
  192. /* The sreloc table stores the relocated offset from climit for */
  193. /* the beginning of each block of string_data_quantum characters. */
  194. void
  195. gc_strings_set_reloc(chunk_t * cp)
  196. {
  197. if (cp->sreloc != 0 && cp->smark != 0) {
  198. byte *bot = cp->ctop;
  199. byte *top = cp->climit;
  200. uint count =
  201. (top - bot + (string_data_quantum - 1)) >>
  202. log2_string_data_quantum;
  203. string_reloc_offset *relp =
  204. cp->sreloc +
  205. (cp->smark_size >> (log2_string_data_quantum - 3));
  206. register const byte *bitp = cp->smark + cp->smark_size;
  207. register string_reloc_offset reloc = 0;
  208. /* Skip initial unrelocated strings quickly. */
  209. #if string_data_quantum == bword_bits || string_data_quantum == bword_bits * 2
  210. {
  211. /* Work around the alignment aliasing bug. */
  212. const bword *wp = (const bword *)bitp;
  213. #if string_data_quantum == bword_bits
  214. # define RELOC_TEST_1S(wp) (wp[-1])
  215. #else /* string_data_quantum == bword_bits * 2 */
  216. # define RELOC_TEST_1S(wp) (wp[-1] & wp[-2])
  217. #endif
  218. while (count && RELOC_TEST_1S(wp) == bword_1s) {
  219. wp -= string_data_quantum / bword_bits;
  220. *--relp = reloc += string_data_quantum;
  221. --count;
  222. }
  223. #undef RELOC_TEST_1S
  224. bitp = (const byte *)wp;
  225. }
  226. #endif
  227. while (count--) {
  228. bitp -= string_data_quantum / 8;
  229. reloc += string_data_quantum -
  230. byte_count_zero_bits(bitp[0]);
  231. reloc -= byte_count_zero_bits(bitp[1]);
  232. reloc -= byte_count_zero_bits(bitp[2]);
  233. reloc -= byte_count_zero_bits(bitp[3]);
  234. #if log2_string_data_quantum > 5
  235. reloc -= byte_count_zero_bits(bitp[4]);
  236. reloc -= byte_count_zero_bits(bitp[5]);
  237. reloc -= byte_count_zero_bits(bitp[6]);
  238. reloc -= byte_count_zero_bits(bitp[7]);
  239. #endif
  240. *--relp = reloc;
  241. }
  242. }
  243. cp->sdest = cp->climit;
  244. }
  245. /* Relocate a string pointer. */
  246. void
  247. igc_reloc_string(gs_string * sptr, gc_state_t * gcst)
  248. {
  249. byte *ptr;
  250. const chunk_t *cp;
  251. uint offset;
  252. uint reloc;
  253. const byte *bitp;
  254. byte byt;
  255. if (sptr->size == 0) {
  256. sptr->data = 0;
  257. return;
  258. }
  259. ptr = sptr->data;
  260. if (!(cp = gc_locate(ptr, gcst))) /* not in a chunk */
  261. return;
  262. if (cp->sreloc == 0 || cp->smark == 0) /* not marking strings */
  263. return;
  264. offset = ptr - cp->sbase;
  265. reloc = cp->sreloc[offset >> log2_string_data_quantum];
  266. bitp = &cp->smark[offset >> 3];
  267. switch (offset & (string_data_quantum - 8)) {
  268. #if log2_string_data_quantum > 5
  269. case 56:
  270. reloc -= byte_count_one_bits(bitp[-7]);
  271. case 48:
  272. reloc -= byte_count_one_bits(bitp[-6]);
  273. case 40:
  274. reloc -= byte_count_one_bits(bitp[-5]);
  275. case 32:
  276. reloc -= byte_count_one_bits(bitp[-4]);
  277. #endif
  278. case 24:
  279. reloc -= byte_count_one_bits(bitp[-3]);
  280. case 16:
  281. reloc -= byte_count_one_bits(bitp[-2]);
  282. case 8:
  283. reloc -= byte_count_one_bits(bitp[-1]);
  284. }
  285. byt = *bitp & (0xff >> (8 - (offset & 7)));
  286. reloc -= byte_count_one_bits(byt);
  287. if_debug2('5', "[5]relocate string 0x%lx to 0x%lx\n",
  288. (ulong) ptr, (ulong) (cp->sdest - reloc));
  289. sptr->data = cp->sdest - reloc;
  290. }
  291. void
  292. igc_reloc_const_string(gs_const_string * sptr, gc_state_t * gcst)
  293. { /* We assume the representation of byte * and const byte * is */
  294. /* the same.... */
  295. igc_reloc_string((gs_string *) sptr, gcst);
  296. }
  297. void
  298. igc_reloc_param_string(gs_param_string * sptr, gc_state_t * gcst)
  299. {
  300. if (!sptr->persistent) {
  301. /* We assume that gs_param_string is a subclass of gs_string. */
  302. igc_reloc_string((gs_string *)sptr, gcst);
  303. }
  304. }
  305. /* Compact the strings in a chunk. */
  306. void
  307. gc_strings_compact(chunk_t * cp)
  308. {
  309. if (cp->smark != 0) {
  310. byte *hi = cp->climit;
  311. byte *lo = cp->ctop;
  312. const byte *from = hi;
  313. byte *to = hi;
  314. const byte *bp = cp->smark + cp->smark_size;
  315. #ifdef DEBUG
  316. if (gs_debug_c('4') || gs_debug_c('5')) {
  317. byte *base = cp->sbase;
  318. uint i = (lo - base) & -string_data_quantum;
  319. uint n = ROUND_UP(hi - base, string_data_quantum);
  320. #define R 16
  321. for (; i < n; i += R) {
  322. uint j;
  323. dlprintf1("[4]0x%lx: ", (ulong) (base + i));
  324. for (j = i; j < i + R; j++) {
  325. byte ch = base[j];
  326. if (ch <= 31) {
  327. dputc('^');
  328. dputc(ch + 0100);
  329. } else
  330. dputc(ch);
  331. }
  332. dputc(' ');
  333. for (j = i; j < i + R; j++)
  334. dputc((cp->smark[j >> 3] & (1 << (j & 7)) ?
  335. '+' : '.'));
  336. #undef R
  337. if (!(i & (string_data_quantum - 1)))
  338. dprintf1(" %u", cp->sreloc[i >> log2_string_data_quantum]);
  339. dputc('\n');
  340. }
  341. }
  342. #endif
  343. /*
  344. * Skip unmodified strings quickly. We know that cp->smark is
  345. * aligned to a string_mark_unit.
  346. */
  347. {
  348. /* Work around the alignment aliasing bug. */
  349. const bword *wp = (const bword *)bp;
  350. while (to > lo && wp[-1] == bword_1s)
  351. to -= bword_bits, --wp;
  352. bp = (const byte *)wp;
  353. while (to > lo && bp[-1] == 0xff)
  354. to -= 8, --bp;
  355. }
  356. from = to;
  357. while (from > lo) {
  358. byte b = *--bp;
  359. from -= 8;
  360. switch (b) {
  361. case 0xff:
  362. to -= 8;
  363. /*
  364. * Since we've seen a byte other than 0xff, we know
  365. * to != from at this point.
  366. */
  367. to[7] = from[7];
  368. to[6] = from[6];
  369. to[5] = from[5];
  370. to[4] = from[4];
  371. to[3] = from[3];
  372. to[2] = from[2];
  373. to[1] = from[1];
  374. to[0] = from[0];
  375. break;
  376. default:
  377. if (b & 0x80)
  378. *--to = from[7];
  379. if (b & 0x40)
  380. *--to = from[6];
  381. if (b & 0x20)
  382. *--to = from[5];
  383. if (b & 0x10)
  384. *--to = from[4];
  385. if (b & 8)
  386. *--to = from[3];
  387. if (b & 4)
  388. *--to = from[2];
  389. if (b & 2)
  390. *--to = from[1];
  391. if (b & 1)
  392. *--to = from[0];
  393. /* falls through */
  394. case 0:
  395. ;
  396. }
  397. }
  398. gs_alloc_fill(cp->ctop, gs_alloc_fill_collected,
  399. to - cp->ctop);
  400. cp->ctop = to;
  401. }
  402. }