igcref.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /* Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999 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: igcref.c,v 1.6 2004/08/04 19:36:12 stefan Exp $ */
  14. /* ref garbage collector for Ghostscript */
  15. #include "memory_.h"
  16. #include "ghost.h"
  17. #include "gsexit.h"
  18. #include "gsstruct.h" /* for gxalloc.h included by iastate.h */
  19. #include "iname.h"
  20. #include "iastate.h"
  21. #include "idebug.h"
  22. #include "igc.h"
  23. #include "ipacked.h"
  24. #include "store.h" /* for ref_assign_inline */
  25. /* Define whether to trace every step of relocating ref pointers. */
  26. #if 0
  27. # define rputc(c) dputc(c)
  28. #else
  29. # define rputc(c) DO_NOTHING
  30. #endif
  31. /* Forward references */
  32. ptr_proc_reloc(igc_reloc_ref_ptr, ref_packed);
  33. refs_proc_reloc(igc_reloc_refs);
  34. /*
  35. * Define the 'structure' type descriptor for refs.
  36. * This is special because it has different shared procs.
  37. */
  38. private gc_proc_clear_reloc(refs_clear_reloc);
  39. private gc_proc_set_reloc(refs_set_reloc);
  40. private gc_proc_compact(refs_compact);
  41. private const struct_shared_procs_t refs_shared_procs =
  42. {refs_clear_reloc, refs_set_reloc, refs_compact};
  43. private struct_proc_clear_marks(refs_clear_marks);
  44. private struct_proc_reloc_ptrs(refs_do_reloc);
  45. const gs_memory_struct_type_t st_refs =
  46. {sizeof(ref), "refs", &refs_shared_procs, refs_clear_marks, 0, refs_do_reloc};
  47. /*
  48. * Define the GC procedures for structs that actually contain refs.
  49. * These are special because the shared refs_* procedures
  50. * are never called. Instead, we unmark the individual refs in clear_marks,
  51. * disregard refs_*_reloc (because we will never relocate a ptr_ref_type
  52. * pointer pointing into the structure), disregard refs_compact (because
  53. * compaction is never required), and remove the marks in reloc_ptrs.
  54. * See also the comment about ptr_ref_type in imemory.h.
  55. */
  56. CLEAR_MARKS_PROC(ref_struct_clear_marks)
  57. {
  58. ref *pref = (ref *) vptr;
  59. ref *end = (ref *) ((char *)vptr + size);
  60. for (; pref < end; pref++)
  61. r_clear_attrs(pref, l_mark);
  62. }
  63. ENUM_PTRS_BEGIN_PROC(ref_struct_enum_ptrs)
  64. {
  65. if (index >= size / sizeof(ref))
  66. return 0;
  67. pep->ptr = (const ref *)vptr + index;
  68. return ptr_ref_type;
  69. ENUM_PTRS_END_PROC
  70. }
  71. RELOC_PTRS_BEGIN(ref_struct_reloc_ptrs)
  72. {
  73. vm_spaces spaces = gcst->spaces;
  74. const gs_memory_t *cmem = space_system->stable_memory;
  75. ref *beg = vptr;
  76. ref *end = (ref *) ((char *)vptr + size);
  77. igc_reloc_refs((ref_packed *) beg, (ref_packed *) end, gcst);
  78. ref_struct_clear_marks(cmem, vptr, size, pstype);
  79. } RELOC_PTRS_END
  80. /* ------ Unmarking phase ------ */
  81. /* Unmark a single ref. */
  82. void
  83. ptr_ref_unmark(enum_ptr_t *pep, gc_state_t * ignored)
  84. {
  85. ref_packed *rpp = (ref_packed *)pep->ptr;
  86. if (r_is_packed(rpp))
  87. r_clear_pmark(rpp);
  88. else
  89. r_clear_attrs((ref *)rpp, l_mark);
  90. }
  91. /* Unmarking routine for ref objects. */
  92. private void
  93. refs_clear_marks(const gs_memory_t *cmem,
  94. void /*obj_header_t */ *vptr, uint size,
  95. const gs_memory_struct_type_t * pstype)
  96. {
  97. ref_packed *rp = (ref_packed *) vptr;
  98. ref_packed *end = (ref_packed *) ((byte *) vptr + size);
  99. /* Since the last ref is full-size, we only need to check for */
  100. /* the end of the block when we see one of those. */
  101. for (;;) {
  102. if (r_is_packed(rp)) {
  103. #ifdef DEBUG
  104. if (gs_debug_c('8')) {
  105. dlprintf1(" [8]unmark packed 0x%lx ", (ulong) rp);
  106. debug_print_ref(cmem, (const ref *)rp);
  107. dputs("\n");
  108. }
  109. #endif
  110. r_clear_pmark(rp);
  111. rp++;
  112. } else { /* full-size ref */
  113. ref *const pref = (ref *)rp;
  114. #ifdef DEBUG
  115. if (gs_debug_c('8')) {
  116. dlprintf1(" [8]unmark ref 0x%lx ", (ulong) rp);
  117. debug_print_ref(cmem, pref);
  118. dputs("\n");
  119. }
  120. #endif
  121. r_clear_attrs(pref, l_mark);
  122. rp += packed_per_ref;
  123. if (rp >= (ref_packed *) end)
  124. break;
  125. }
  126. }
  127. }
  128. /* ------ Marking phase ------ */
  129. /* Mark a ref. Return true if new mark. */
  130. bool
  131. ptr_ref_mark(enum_ptr_t *pep, gc_state_t * ignored)
  132. {
  133. ref_packed *rpp = (void *)pep->ptr;
  134. if (r_is_packed(rpp)) {
  135. if (r_has_pmark(rpp))
  136. return false;
  137. r_set_pmark(rpp);
  138. } else {
  139. ref *const pref = (ref *)rpp;
  140. if (r_has_attr(pref, l_mark))
  141. return false;
  142. r_set_attrs(pref, l_mark);
  143. }
  144. return true;
  145. }
  146. /* ------ Relocation planning phase ------ */
  147. /*
  148. * We store relocation in the size field of refs that don't use it,
  149. * so that we don't have to scan all the way to an unmarked object.
  150. * We must avoid nulls, which sometimes have useful information
  151. * in their size fields, and the types above t_next_index, which are
  152. * actually operators in disguise and also use the size field.
  153. */
  154. /* Clear the relocation for a ref object. */
  155. private void
  156. refs_clear_reloc(obj_header_t *hdr, uint size)
  157. {
  158. ref_packed *rp = (ref_packed *) (hdr + 1);
  159. ref_packed *end = (ref_packed *) ((byte *) rp + size);
  160. while (rp < end) {
  161. if (r_is_packed(rp))
  162. rp++;
  163. else {
  164. /* Full-size ref. Store the relocation here if possible. */
  165. ref *const pref = (ref *)rp;
  166. if (!ref_type_uses_size_or_null(r_type(pref))) {
  167. if_debug1('8', " [8]clearing reloc at 0x%lx\n", (ulong) rp);
  168. r_set_size(pref, 0);
  169. }
  170. rp += packed_per_ref;
  171. }
  172. }
  173. }
  174. /* Set the relocation for a ref object. */
  175. private bool
  176. refs_set_reloc(obj_header_t * hdr, uint reloc, uint size)
  177. {
  178. ref_packed *rp = (ref_packed *) (hdr + 1);
  179. ref_packed *end = (ref_packed *) ((byte *) rp + size);
  180. uint freed = 0;
  181. /*
  182. * We have to be careful to keep refs aligned properly.
  183. * For the moment, we do this by either keeping or discarding
  184. * an entire (aligned) block of align_packed_per_ref packed elements
  185. * as a unit. We know that align_packed_per_ref <= packed_per_ref,
  186. * and we also know that packed refs are always allocated in blocks
  187. * of align_packed_per_ref, so this makes things relatively easy.
  188. */
  189. while (rp < end) {
  190. if (r_is_packed(rp)) {
  191. #if align_packed_per_ref == 1
  192. if (r_has_pmark(rp)) {
  193. if_debug1('8',
  194. " [8]packed ref 0x%lx is marked\n",
  195. (ulong) rp);
  196. rp++;
  197. } else {
  198. #else
  199. int i;
  200. /*
  201. * Note: align_packed_per_ref is typically
  202. * 2 or 4 for 32-bit processors.
  203. */
  204. #define all_marked (align_packed_per_ref * lp_mark)
  205. # if align_packed_per_ref == 2
  206. # if arch_sizeof_int == arch_sizeof_short * 2
  207. # undef all_marked
  208. # define all_marked ( (lp_mark << (sizeof(short) * 8)) + lp_mark )
  209. # define marked (*(int *)rp & all_marked)
  210. # else
  211. # define marked ((*rp & lp_mark) + (rp[1] & lp_mark))
  212. # endif
  213. # else
  214. # if align_packed_per_ref == 4
  215. # define marked ((*rp & lp_mark) + (rp[1] & lp_mark) +\
  216. (rp[2] & lp_mark) + (rp[3] & lp_mark))
  217. # else
  218. /*
  219. * The value of marked is logically a uint, not an int:
  220. * we declare it as int only to avoid a compiler warning
  221. * message about using a non-int value in a switch statement.
  222. */
  223. int marked = *rp & lp_mark;
  224. for (i = 1; i < align_packed_per_ref; i++)
  225. marked += rp[i] & lp_mark;
  226. # endif
  227. # endif
  228. /*
  229. * Now marked is lp_mark * the number of marked
  230. * packed refs in the aligned block, except for
  231. * a couple of special cases above.
  232. */
  233. switch (marked) {
  234. case all_marked:
  235. if_debug2('8',
  236. " [8]packed refs 0x%lx..0x%lx are marked\n",
  237. (ulong) rp,
  238. (ulong) (rp + (align_packed_per_ref - 1)));
  239. rp += align_packed_per_ref;
  240. break;
  241. default:
  242. /* At least one packed ref in the block */
  243. /* is marked: Keep the whole block. */
  244. for (i = align_packed_per_ref; i--; rp++) {
  245. r_set_pmark(rp);
  246. if_debug1('8',
  247. " [8]packed ref 0x%lx is marked\n",
  248. (ulong) rp);
  249. }
  250. break;
  251. case 0:
  252. #endif
  253. if_debug2('8', " [8]%d packed ref(s) at 0x%lx are unmarked\n",
  254. align_packed_per_ref, (ulong) rp);
  255. {
  256. uint rel = reloc + freed;
  257. /* Change this to an integer so we can */
  258. /* store the relocation here. */
  259. *rp = pt_tag(pt_integer) +
  260. min(rel, packed_max_value);
  261. }
  262. rp += align_packed_per_ref;
  263. freed += sizeof(ref_packed) * align_packed_per_ref;
  264. }
  265. } else { /* full-size ref */
  266. uint rel = reloc + freed;
  267. /* The following assignment is logically */
  268. /* unnecessary; we do it only for convenience */
  269. /* in debugging. */
  270. ref *pref = (ref *) rp;
  271. if (!r_has_attr(pref, l_mark)) {
  272. if_debug1('8', " [8]ref 0x%lx is unmarked\n",
  273. (ulong) pref);
  274. /* Change this to a mark so we can */
  275. /* store the relocation. */
  276. r_set_type(pref, t_mark);
  277. r_set_size(pref, rel);
  278. freed += sizeof(ref);
  279. } else {
  280. if_debug1('8', " [8]ref 0x%lx is marked\n",
  281. (ulong) pref);
  282. /* Store the relocation here if possible. */
  283. if (!ref_type_uses_size_or_null(r_type(pref))) {
  284. if_debug2('8', " [8]storing reloc %u at 0x%lx\n",
  285. rel, (ulong) pref);
  286. r_set_size(pref, rel);
  287. }
  288. }
  289. rp += packed_per_ref;
  290. }
  291. }
  292. if_debug3('7', " [7]at end of refs 0x%lx, size = %u, freed = %u\n",
  293. (ulong) (hdr + 1), size, freed);
  294. if (freed == size)
  295. return false;
  296. #if arch_sizeof_int > arch_sizeof_short
  297. /*
  298. * If the final relocation can't fit in the r_size field
  299. * (which can't happen if the object shares a chunk with
  300. * any other objects, so we know reloc = 0 in this case),
  301. * we have to keep the entire object unless there are no
  302. * references to any ref in it.
  303. */
  304. if (freed <= max_ushort)
  305. return true;
  306. /*
  307. * We have to mark all surviving refs, but we also must
  308. * overwrite any non-surviving refs with something that
  309. * doesn't contain any pointers.
  310. */
  311. rp = (ref_packed *) (hdr + 1);
  312. while (rp < end) {
  313. if (r_is_packed(rp)) {
  314. if (!r_has_pmark(rp))
  315. *rp = pt_tag(pt_integer) | lp_mark;
  316. ++rp;
  317. } else { /* The following assignment is logically */
  318. /* unnecessary; we do it only for convenience */
  319. /* in debugging. */
  320. ref *pref = (ref *) rp;
  321. if (!r_has_attr(pref, l_mark)) {
  322. r_set_type_attrs(pref, t_mark, l_mark);
  323. r_set_size(pref, reloc);
  324. } else {
  325. if (!ref_type_uses_size_or_null(r_type(pref)))
  326. r_set_size(pref, reloc);
  327. }
  328. rp += packed_per_ref;
  329. }
  330. }
  331. /* The last ref has to remain unmarked. */
  332. r_clear_attrs((ref *) rp - 1, l_mark);
  333. #endif
  334. return true;
  335. }
  336. /* ------ Relocation phase ------ */
  337. /* Relocate all the pointers in a block of refs. */
  338. private void
  339. refs_do_reloc(void /*obj_header_t */ *vptr, uint size,
  340. const gs_memory_struct_type_t * pstype, gc_state_t * gcst)
  341. {
  342. igc_reloc_refs((ref_packed *) vptr,
  343. (ref_packed *) ((char *)vptr + size),
  344. gcst);
  345. }
  346. /* Relocate the contents of a block of refs. */
  347. /* If gcst->relocating_untraced is true, we are relocating pointers from an */
  348. /* untraced space, so relocate all refs, not just marked ones. */
  349. void
  350. igc_reloc_refs(ref_packed * from, ref_packed * to, gc_state_t * gcst)
  351. {
  352. int min_trace = gcst->min_collect;
  353. ref_packed *rp = from;
  354. bool do_all = gcst->relocating_untraced;
  355. vm_spaces spaces = gcst->spaces;
  356. const gs_memory_t *cmem = space_system->stable_memory;
  357. while (rp < to) {
  358. ref *pref;
  359. #ifdef DEBUG
  360. const void *before = 0;
  361. const void *after = 0;
  362. # define DO_RELOC(var, stat)\
  363. BEGIN before = (var); stat; after = (var); END
  364. # define SET_RELOC(var, expr)\
  365. BEGIN before = (var); after = (var) = (expr); END
  366. #else
  367. # define DO_RELOC(var, stat) stat
  368. # define SET_RELOC(var, expr) var = expr
  369. #endif
  370. if (r_is_packed(rp)) {
  371. rp++;
  372. continue;
  373. }
  374. /* The following assignment is logically unnecessary; */
  375. /* we do it only for convenience in debugging. */
  376. pref = (ref *) rp;
  377. if_debug3('8', " [8]relocating %s %d ref at 0x%lx",
  378. (r_has_attr(pref, l_mark) ? "marked" : "unmarked"),
  379. r_btype(pref), (ulong) pref);
  380. if ((r_has_attr(pref, l_mark) || do_all) &&
  381. r_space(pref) >= min_trace
  382. ) {
  383. switch (r_type(pref)) {
  384. /* Struct cases */
  385. case t_file:
  386. DO_RELOC(pref->value.pfile, RELOC_VAR(pref->value.pfile));
  387. break;
  388. case t_device:
  389. DO_RELOC(pref->value.pdevice,
  390. RELOC_VAR(pref->value.pdevice));
  391. break;
  392. case t_fontID:
  393. case t_struct:
  394. case t_astruct:
  395. DO_RELOC(pref->value.pstruct,
  396. RELOC_VAR(pref->value.pstruct));
  397. break;
  398. /* Non-trivial non-struct cases */
  399. case t_dictionary:
  400. rputc('d');
  401. SET_RELOC(pref->value.pdict,
  402. (dict *)igc_reloc_ref_ptr((ref_packed *)pref->value.pdict, gcst));
  403. break;
  404. case t_array:
  405. {
  406. uint size = r_size(pref);
  407. if (size != 0) { /* value.refs might be NULL */
  408. /*
  409. * If the array is large, we allocated it in its
  410. * own object (at least originally -- this might
  411. * be a pointer to a subarray.) In this case,
  412. * we know it is the only object in its
  413. * containing st_refs object, so we know that
  414. * the mark containing the relocation appears
  415. * just after it.
  416. */
  417. if (size < max_size_st_refs / sizeof(ref)) {
  418. rputc('a');
  419. SET_RELOC(pref->value.refs,
  420. (ref *) igc_reloc_ref_ptr(
  421. (ref_packed *) pref->value.refs, gcst));
  422. } else {
  423. rputc('A');
  424. /*
  425. * See the t_shortarray case below for why we
  426. * decrement size.
  427. */
  428. --size;
  429. SET_RELOC(pref->value.refs,
  430. (ref *) igc_reloc_ref_ptr(
  431. (ref_packed *) (pref->value.refs + size),
  432. gcst) - size);
  433. }
  434. }
  435. }
  436. break;
  437. case t_mixedarray:
  438. if (r_size(pref) != 0) { /* value.refs might be NULL */
  439. rputc('m');
  440. SET_RELOC(pref->value.packed,
  441. igc_reloc_ref_ptr(pref->value.packed, gcst));
  442. }
  443. break;
  444. case t_shortarray:
  445. {
  446. uint size = r_size(pref);
  447. /*
  448. * Since we know that igc_reloc_ref_ptr works by
  449. * scanning forward, and we know that all the
  450. * elements of this array itself are marked, we can
  451. * save some scanning time by relocating the pointer
  452. * to the end of the array rather than the
  453. * beginning.
  454. */
  455. if (size != 0) { /* value.refs might be NULL */
  456. rputc('s');
  457. /*
  458. * igc_reloc_ref_ptr has to be able to determine
  459. * whether the pointer points into a space that
  460. * isn't being collected. It does this by
  461. * checking whether the referent of the pointer
  462. * is marked. For this reason, we have to pass
  463. * a pointer to the last real element of the
  464. * array, rather than just beyond it.
  465. */
  466. --size;
  467. SET_RELOC(pref->value.packed,
  468. igc_reloc_ref_ptr(pref->value.packed + size,
  469. gcst) - size);
  470. }
  471. }
  472. break;
  473. case t_name:
  474. {
  475. void *psub = name_ref_sub_table(cmem, pref);
  476. void *rsub = RELOC_OBJ(psub); /* gcst implicit */
  477. SET_RELOC(pref->value.pname,
  478. (name *)
  479. ((char *)rsub + ((char *)pref->value.pname -
  480. (char *)psub)));
  481. } break;
  482. case t_string:
  483. {
  484. gs_string str;
  485. str.data = pref->value.bytes;
  486. str.size = r_size(pref);
  487. DO_RELOC(str.data, RELOC_STRING_VAR(str));
  488. pref->value.bytes = str.data;
  489. }
  490. break;
  491. case t_oparray:
  492. rputc('o');
  493. SET_RELOC(pref->value.const_refs,
  494. (const ref *)igc_reloc_ref_ptr((const ref_packed *)pref->value.const_refs, gcst));
  495. break;
  496. default:
  497. goto no_reloc; /* don't print trace message */
  498. }
  499. if_debug2('8', ", 0x%lx => 0x%lx", (ulong)before, (ulong)after);
  500. }
  501. no_reloc:
  502. if_debug0('8', "\n");
  503. rp += packed_per_ref;
  504. }
  505. }
  506. /* Relocate a pointer to a ref. */
  507. /* See gsmemory.h for why the argument is const and the result is not. */
  508. ref_packed *
  509. igc_reloc_ref_ptr(const ref_packed * prp, gc_state_t *gcst)
  510. {
  511. /*
  512. * Search forward for relocation. This algorithm is intrinsically very
  513. * inefficient; we hope eventually to replace it with a better one.
  514. */
  515. const ref_packed *rp = prp;
  516. uint dec = 0;
  517. #ifdef ALIGNMENT_ALIASING_BUG
  518. const ref *rpref;
  519. # define RP_REF(rp) (rpref = (const ref *)rp, rpref)
  520. #else
  521. # define RP_REF(rp) ((const ref *)rp)
  522. #endif
  523. /*
  524. * Iff this pointer points into a space that wasn't traced,
  525. * the referent won't be marked. In this case, we shouldn't
  526. * do any relocation. Check for this first.
  527. */
  528. if (r_is_packed(rp)) {
  529. if (!r_has_pmark(rp))
  530. goto ret_rp;
  531. } else {
  532. if (!r_has_attr(RP_REF(rp), l_mark))
  533. goto ret_rp;
  534. }
  535. for (;;) {
  536. if (r_is_packed(rp)) {
  537. /*
  538. * Normally, an unmarked packed ref will be an
  539. * integer whose value is the amount of relocation.
  540. * However, the relocation value might have been
  541. * too large to fit. If this is the case, for
  542. * each such unmarked packed ref we pass over,
  543. * we have to decrement the final relocation.
  544. */
  545. rputc((*rp & lp_mark ? '1' : '0'));
  546. if (!(*rp & lp_mark)) {
  547. if (*rp != pt_tag(pt_integer) + packed_max_value) {
  548. /* This is a stored relocation value. */
  549. rputc('\n');
  550. rp = print_reloc(prp, "ref",
  551. (const ref_packed *)
  552. ((const char *)prp -
  553. (*rp & packed_value_mask) + dec));
  554. break;
  555. }
  556. /*
  557. * We know this is the first of an aligned block
  558. * of packed refs. Skip over the entire block,
  559. * decrementing the final relocation.
  560. */
  561. dec += sizeof(ref_packed) * align_packed_per_ref;
  562. rp += align_packed_per_ref;
  563. } else
  564. rp++;
  565. continue;
  566. }
  567. if (!ref_type_uses_size_or_null(r_type(RP_REF(rp)))) {
  568. /* reloc is in r_size */
  569. rputc('\n');
  570. rp = print_reloc(prp, "ref",
  571. (const ref_packed *)
  572. (r_size(RP_REF(rp)) == 0 ? prp :
  573. (const ref_packed *)((const char *)prp -
  574. r_size(RP_REF(rp)) + dec)));
  575. break;
  576. }
  577. rputc('u');
  578. rp += packed_per_ref;
  579. }
  580. ret_rp:
  581. /* Use a severely deprecated pun to remove the const property. */
  582. {
  583. union { const ref_packed *r; ref_packed *w; } u;
  584. u.r = rp;
  585. return u.w;
  586. }
  587. }
  588. /* ------ Compaction phase ------ */
  589. /* Compact a ref object. */
  590. /* Remove the marks at the same time. */
  591. private void
  592. refs_compact(const gs_memory_t *mem, obj_header_t * pre, obj_header_t * dpre, uint size)
  593. {
  594. ref_packed *dest;
  595. ref_packed *src;
  596. ref_packed *end;
  597. uint new_size;
  598. src = (ref_packed *) (pre + 1);
  599. end = (ref_packed *) ((byte *) src + size);
  600. /*
  601. * We know that a block of refs always ends with an unmarked
  602. * full-size ref, so we only need to check for reaching the end
  603. * of the block when we see one of those.
  604. */
  605. if (dpre == pre) /* Loop while we don't need to copy. */
  606. for (;;) {
  607. if (r_is_packed(src)) {
  608. if (!r_has_pmark(src))
  609. break;
  610. if_debug1('8', " [8]packed ref 0x%lx \"copied\"\n",
  611. (ulong) src);
  612. *src &= ~lp_mark;
  613. src++;
  614. } else { /* full-size ref */
  615. ref *const pref = (ref *)src;
  616. if (!r_has_attr(pref, l_mark))
  617. break;
  618. if_debug1('8', " [8]ref 0x%lx \"copied\"\n", (ulong) src);
  619. r_clear_attrs(pref, l_mark);
  620. src += packed_per_ref;
  621. }
  622. } else
  623. *dpre = *pre;
  624. dest = (ref_packed *) ((char *)dpre + ((char *)src - (char *)pre));
  625. for (;;) {
  626. if (r_is_packed(src)) {
  627. if (r_has_pmark(src)) {
  628. if_debug2('8', " [8]packed ref 0x%lx copied to 0x%lx\n",
  629. (ulong) src, (ulong) dest);
  630. *dest++ = *src & ~lp_mark;
  631. }
  632. src++;
  633. } else { /* full-size ref */
  634. if (r_has_attr((ref *) src, l_mark)) {
  635. ref rtemp;
  636. if_debug2('8', " [8]ref 0x%lx copied to 0x%lx\n",
  637. (ulong) src, (ulong) dest);
  638. /* We can't just use ref_assign_inline, */
  639. /* because the source and destination */
  640. /* might overlap! */
  641. ref_assign_inline(&rtemp, (ref *) src);
  642. r_clear_attrs(&rtemp, l_mark);
  643. ref_assign_inline((ref *) dest, &rtemp);
  644. dest += packed_per_ref;
  645. src += packed_per_ref;
  646. } else { /* check for end of block */
  647. src += packed_per_ref;
  648. if (src >= end)
  649. break;
  650. }
  651. }
  652. }
  653. new_size = (byte *) dest - (byte *) (dpre + 1) + sizeof(ref);
  654. #ifdef DEBUG
  655. /* Check that the relocation came out OK. */
  656. /* NOTE: this check only works within a single chunk. */
  657. if ((byte *) src - (byte *) dest != r_size((ref *) src - 1) + sizeof(ref)) {
  658. lprintf3("Reloc error for refs 0x%lx: reloc = %lu, stored = %u\n",
  659. (ulong) dpre, (ulong) ((byte *) src - (byte *) dest),
  660. (uint) r_size((ref *) src - 1));
  661. gs_abort(mem);
  662. }
  663. #endif
  664. /* Pad to a multiple of sizeof(ref). */
  665. while (new_size & (sizeof(ref) - 1))
  666. *dest++ = pt_tag(pt_integer),
  667. new_size += sizeof(ref_packed);
  668. /* We want to make the newly freed space into a free block, */
  669. /* but we can only do this if we have enough room. */
  670. if (size - new_size < sizeof(obj_header_t)) { /* Not enough room. Pad to original size. */
  671. while (new_size < size)
  672. *dest++ = pt_tag(pt_integer),
  673. new_size += sizeof(ref_packed);
  674. } else {
  675. obj_header_t *pfree = (obj_header_t *) ((ref *) dest + 1);
  676. pfree->o_alone = 0;
  677. pfree->o_size = size - new_size - sizeof(obj_header_t);
  678. pfree->o_type = &st_bytes;
  679. }
  680. /* Re-create the final ref. */
  681. r_set_type((ref *) dest, t_integer);
  682. dpre->o_size = new_size;
  683. }