igc.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  1. /* Copyright (C) 1993, 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: igc.c,v 1.15 2005/09/05 13:58:55 leonardo Exp $ */
  14. /* Garbage collector for Ghostscript */
  15. #include "memory_.h"
  16. #include "ghost.h"
  17. #include "ierrors.h"
  18. #include "gsexit.h"
  19. #include "gsmdebug.h"
  20. #include "gsstruct.h"
  21. #include "gsutil.h"
  22. #include "iastate.h"
  23. #include "isave.h"
  24. #include "isstate.h"
  25. #include "idict.h"
  26. #include "ipacked.h"
  27. #include "istruct.h"
  28. #include "igc.h"
  29. #include "igcstr.h"
  30. #include "inamedef.h"
  31. #include "opdef.h" /* for marking oparray names */
  32. /* Define whether to force all garbage collections to be global. */
  33. private const bool I_FORCE_GLOBAL_GC = false;
  34. /* Define whether to bypass the collector entirely. */
  35. private const bool I_BYPASS_GC = false;
  36. /* Define an entry on the mark stack. */
  37. typedef struct {
  38. void *ptr;
  39. uint index;
  40. bool is_refs;
  41. } ms_entry;
  42. /* Define (a segment of) the mark stack. */
  43. /* entries[0] has ptr = 0 to indicate the bottom of the stack. */
  44. /* count additional entries follow this structure. */
  45. typedef struct gc_mark_stack_s gc_mark_stack;
  46. struct gc_mark_stack_s {
  47. gc_mark_stack *prev;
  48. gc_mark_stack *next;
  49. uint count;
  50. bool on_heap; /* if true, allocated during GC */
  51. ms_entry entries[1];
  52. };
  53. /* Define the mark stack sizing parameters. */
  54. #define ms_size_default 100 /* default, allocated on C stack */
  55. /* This should probably be defined as a parameter somewhere.... */
  56. #define ms_size_desired /* for additional allocation */\
  57. ((max_ushort - sizeof(gc_mark_stack)) / sizeof(ms_entry) - 10)
  58. #define ms_size_min 50 /* min size for segment in free block */
  59. /* Forward references */
  60. private void gc_init_mark_stack(gc_mark_stack *, uint);
  61. private void gc_objects_clear_marks(const gs_memory_t *mem, chunk_t *);
  62. private void gc_unmark_names(name_table *);
  63. private int gc_trace(gs_gc_root_t *, gc_state_t *, gc_mark_stack *);
  64. private int gc_rescan_chunk(chunk_t *, gc_state_t *, gc_mark_stack *);
  65. private int gc_trace_chunk(const gs_memory_t *mem, chunk_t *, gc_state_t *, gc_mark_stack *);
  66. private bool gc_trace_finish(gc_state_t *);
  67. private void gc_clear_reloc(chunk_t *);
  68. private void gc_objects_set_reloc(chunk_t *);
  69. private void gc_do_reloc(chunk_t *, gs_ref_memory_t *, gc_state_t *);
  70. private void gc_objects_compact(chunk_t *, gc_state_t *);
  71. private void gc_free_empty_chunks(gs_ref_memory_t *);
  72. /* Forward references for pointer types */
  73. private ptr_proc_unmark(ptr_struct_unmark);
  74. private ptr_proc_mark(ptr_struct_mark);
  75. private ptr_proc_unmark(ptr_string_unmark);
  76. private ptr_proc_mark(ptr_string_mark);
  77. /*ptr_proc_unmark(ptr_ref_unmark); *//* in igc.h */
  78. /*ptr_proc_mark(ptr_ref_mark); *//* in igc.h */
  79. private ptr_proc_reloc(igc_reloc_struct_ptr, void);
  80. ptr_proc_reloc(igc_reloc_ref_ptr, ref_packed); /* in igcref.c */
  81. refs_proc_reloc(igc_reloc_refs); /* in igcref.c */
  82. /* Define this GC's procedure vector. */
  83. private const gc_procs_with_refs_t igc_procs = {
  84. igc_reloc_struct_ptr, igc_reloc_string, igc_reloc_const_string,
  85. igc_reloc_param_string, igc_reloc_ref_ptr, igc_reloc_refs
  86. };
  87. /* Pointer type descriptors. */
  88. /* Note that the trace/mark routine has special knowledge of ptr_ref_type */
  89. /* and ptr_struct_type -- it assumes that no other types have embedded */
  90. /* pointers. Note also that the reloc procedures for string and ref */
  91. /* pointers are never called. */
  92. typedef ptr_proc_reloc((*ptr_proc_reloc_t), void);
  93. const gs_ptr_procs_t ptr_struct_procs =
  94. {ptr_struct_unmark, ptr_struct_mark, (ptr_proc_reloc_t) igc_reloc_struct_ptr};
  95. const gs_ptr_procs_t ptr_string_procs =
  96. {ptr_string_unmark, ptr_string_mark, NULL};
  97. const gs_ptr_procs_t ptr_const_string_procs =
  98. {ptr_string_unmark, ptr_string_mark, NULL};
  99. const gs_ptr_procs_t ptr_ref_procs =
  100. {ptr_ref_unmark, ptr_ref_mark, NULL};
  101. /* ------ Main program ------ */
  102. /* Top level of garbage collector. */
  103. #ifdef DEBUG
  104. private void
  105. end_phase(const char *str)
  106. {
  107. if (gs_debug_c('6')) {
  108. dlprintf1("[6]---------------- end %s ----------------\n",
  109. (const char *)str);
  110. dflush();
  111. }
  112. }
  113. static const char *const depth_dots_string = "..........";
  114. private const char *
  115. depth_dots(const ms_entry * sp, const gc_mark_stack * pms)
  116. {
  117. int depth = sp - pms->entries - 1;
  118. const gc_mark_stack *pss = pms;
  119. while ((pss = pss->prev) != 0)
  120. depth += pss->count - 1;
  121. return depth_dots_string + (depth >= 10 ? 0 : 10 - depth);
  122. }
  123. private void
  124. gc_validate_spaces(gs_ref_memory_t **spaces, int max_space, gc_state_t *gcst)
  125. {
  126. int i;
  127. gs_ref_memory_t *mem;
  128. for (i = 1; i <= max_space; ++i)
  129. if ((mem = spaces[i]) != 0)
  130. ialloc_validate_memory(mem, gcst);
  131. }
  132. #else /* !DEBUG */
  133. # define end_phase(str) DO_NOTHING
  134. #endif /* DEBUG */
  135. void
  136. gs_gc_reclaim(vm_spaces * pspaces, bool global)
  137. {
  138. #define nspaces ((i_vm_max + 1) * 2) /* * 2 for stable allocators */
  139. vm_spaces spaces;
  140. gs_ref_memory_t *space_memories[nspaces];
  141. gs_gc_root_t space_roots[nspaces];
  142. int max_trace; /* max space_ to trace */
  143. int min_collect; /* min space_ to collect */
  144. int min_collect_vm_space; /* min VM space to collect */
  145. int ispace;
  146. gs_ref_memory_t *mem;
  147. chunk_t *cp;
  148. gs_gc_root_t *rp;
  149. gc_state_t state;
  150. struct _msd {
  151. gc_mark_stack stack;
  152. ms_entry body[ms_size_default];
  153. } ms_default;
  154. gc_mark_stack *mark_stack = &ms_default.stack;
  155. const gs_memory_t *cmem;
  156. /* Optionally force global GC for debugging. */
  157. if (I_FORCE_GLOBAL_GC)
  158. global = true;
  159. /* Determine which spaces we are tracing and collecting. */
  160. spaces = *pspaces;
  161. cmem = space_system->stable_memory;
  162. space_memories[1] = space_system;
  163. space_memories[2] = space_global;
  164. min_collect = max_trace = 2;
  165. min_collect_vm_space = i_vm_global;
  166. if (space_global->stable_memory != (gs_memory_t *)space_global)
  167. space_memories[++max_trace] =
  168. (gs_ref_memory_t *)space_global->stable_memory;
  169. if (space_global != space_local) {
  170. space_memories[++max_trace] = space_local;
  171. min_collect = max_trace;
  172. min_collect_vm_space = i_vm_local;
  173. if (space_local->stable_memory != (gs_memory_t *)space_local)
  174. space_memories[++max_trace] =
  175. (gs_ref_memory_t *)space_local->stable_memory;
  176. }
  177. if (global)
  178. min_collect = min_collect_vm_space = 1;
  179. #define for_spaces(i, n)\
  180. for (i = 1; i <= n; ++i)
  181. #define for_collected_spaces(i)\
  182. for (i = min_collect; i <= max_trace; ++i)
  183. #define for_space_mems(i, mem)\
  184. for (mem = space_memories[i]; mem != 0; mem = &mem->saved->state)
  185. #define for_mem_chunks(mem, cp)\
  186. for (cp = (mem)->cfirst; cp != 0; cp = cp->cnext)
  187. #define for_space_chunks(i, mem, cp)\
  188. for_space_mems(i, mem) for_mem_chunks(mem, cp)
  189. #define for_chunks(n, mem, cp)\
  190. for_spaces(ispace, n) for_space_chunks(ispace, mem, cp)
  191. #define for_collected_chunks(mem, cp)\
  192. for_collected_spaces(ispace) for_space_chunks(ispace, mem, cp)
  193. #define for_roots(n, mem, rp)\
  194. for_spaces(ispace, n)\
  195. for (mem = space_memories[ispace], rp = mem->roots; rp != 0; rp = rp->next)
  196. /* Initialize the state. */
  197. state.procs = &igc_procs;
  198. state.loc.memory = space_global; /* any one will do */
  199. state.loc.cp = 0;
  200. state.spaces = spaces;
  201. state.min_collect = min_collect_vm_space << r_space_shift;
  202. state.relocating_untraced = false;
  203. state.heap = state.loc.memory->non_gc_memory;
  204. state.ntable = state.heap->gs_lib_ctx->gs_name_table;
  205. /* Register the allocators themselves as roots, */
  206. /* so we mark and relocate the change and save lists properly. */
  207. for_spaces(ispace, max_trace)
  208. gs_register_struct_root((gs_memory_t *)space_memories[ispace],
  209. &space_roots[ispace],
  210. (void **)&space_memories[ispace],
  211. "gc_top_level");
  212. end_phase("register space roots");
  213. #ifdef DEBUG
  214. /* Pre-validate the state. This shouldn't be necessary.... */
  215. gc_validate_spaces(space_memories, max_trace, &state);
  216. end_phase("pre-validate pointers");
  217. #endif
  218. if (I_BYPASS_GC) { /* Don't collect at all. */
  219. goto no_collect;
  220. }
  221. /* Clear marks in spaces to be collected. */
  222. for_collected_spaces(ispace)
  223. for_space_chunks(ispace, mem, cp) {
  224. gc_objects_clear_marks((const gs_memory_t *)mem, cp);
  225. gc_strings_set_marks(cp, false);
  226. }
  227. end_phase("clear chunk marks");
  228. /* Clear the marks of roots. We must do this explicitly, */
  229. /* since some roots are not in any chunk. */
  230. for_roots(max_trace, mem, rp) {
  231. enum_ptr_t eptr;
  232. eptr.ptr = *rp->p;
  233. if_debug_root('6', "[6]unmarking root", rp);
  234. (*rp->ptype->unmark)(&eptr, &state);
  235. }
  236. end_phase("clear root marks");
  237. if (global)
  238. gc_unmark_names(state.ntable);
  239. /* Initialize the (default) mark stack. */
  240. gc_init_mark_stack(&ms_default.stack, ms_size_default);
  241. ms_default.stack.prev = 0;
  242. ms_default.stack.on_heap = false;
  243. /* Add all large-enough free blocks to the mark stack. */
  244. /* Also initialize the rescan pointers. */
  245. {
  246. gc_mark_stack *end = mark_stack;
  247. for_chunks(max_trace, mem, cp) {
  248. uint avail = cp->ctop - cp->cbot;
  249. if (avail >= sizeof(gc_mark_stack) + sizeof(ms_entry) *
  250. ms_size_min &&
  251. !cp->inner_count
  252. ) {
  253. gc_mark_stack *pms = (gc_mark_stack *) cp->cbot;
  254. gc_init_mark_stack(pms, (avail - sizeof(gc_mark_stack)) /
  255. sizeof(ms_entry));
  256. end->next = pms;
  257. pms->prev = end;
  258. pms->on_heap = false;
  259. if_debug2('6', "[6]adding free 0x%lx(%u) to mark stack\n",
  260. (ulong) pms, pms->count);
  261. }
  262. cp->rescan_bot = cp->cend;
  263. cp->rescan_top = cp->cbase;
  264. }
  265. }
  266. /* Mark reachable objects. */
  267. {
  268. int more = 0;
  269. /* Mark from roots. */
  270. for_roots(max_trace, mem, rp) {
  271. if_debug_root('6', "[6]marking root", rp);
  272. more |= gc_trace(rp, &state, mark_stack);
  273. }
  274. end_phase("mark");
  275. /* If this is a local GC, mark from non-local chunks. */
  276. if (!global)
  277. for_chunks(min_collect - 1, mem, cp)
  278. more |= gc_trace_chunk((const gs_memory_t *)mem, cp, &state, mark_stack);
  279. /* Handle mark stack overflow. */
  280. while (more < 0) { /* stack overflowed */
  281. more = 0;
  282. for_chunks(max_trace, mem, cp)
  283. more |= gc_rescan_chunk(cp, &state, mark_stack);
  284. }
  285. end_phase("mark overflow");
  286. }
  287. /* Free the mark stack. */
  288. {
  289. gc_mark_stack *pms = mark_stack;
  290. while (pms->next)
  291. pms = pms->next;
  292. while (pms) {
  293. gc_mark_stack *prev = pms->prev;
  294. if (pms->on_heap)
  295. gs_free_object(state.heap, pms, "gc mark stack");
  296. else
  297. gs_alloc_fill(pms, gs_alloc_fill_free,
  298. sizeof(*pms) + sizeof(ms_entry) * pms->count);
  299. pms = prev;
  300. }
  301. }
  302. end_phase("free mark stack");
  303. if (global) {
  304. gc_trace_finish(&state);
  305. names_trace_finish(state.ntable, &state);
  306. end_phase("finish trace");
  307. }
  308. /* Clear marks and relocation in spaces that are only being traced. */
  309. /* We have to clear the marks first, because we want the */
  310. /* relocation to wind up as o_untraced, not o_unmarked. */
  311. for_chunks(min_collect - 1, mem, cp)
  312. gc_objects_clear_marks((const gs_memory_t *)mem, cp);
  313. end_phase("post-clear marks");
  314. for_chunks(min_collect - 1, mem, cp)
  315. gc_clear_reloc(cp);
  316. end_phase("clear reloc");
  317. /* Set the relocation of roots outside any chunk to o_untraced, */
  318. /* so we won't try to relocate pointers to them. */
  319. /* (Currently, there aren't any.) */
  320. /* Disable freeing in the allocators of the spaces we are */
  321. /* collecting, so finalization procedures won't cause problems. */
  322. {
  323. int i;
  324. for_collected_spaces(i)
  325. gs_enable_free((gs_memory_t *)space_memories[i], false);
  326. }
  327. /* Compute relocation based on marks, in the spaces */
  328. /* we are going to compact. Also finalize freed objects. */
  329. for_collected_chunks(mem, cp) {
  330. gc_objects_set_reloc(cp);
  331. gc_strings_set_reloc(cp);
  332. }
  333. /* Re-enable freeing. */
  334. {
  335. int i;
  336. for_collected_spaces(i)
  337. gs_enable_free((gs_memory_t *)space_memories[i], true);
  338. }
  339. end_phase("set reloc");
  340. /* Relocate pointers. */
  341. state.relocating_untraced = true;
  342. for_chunks(min_collect - 1, mem, cp)
  343. gc_do_reloc(cp, mem, &state);
  344. state.relocating_untraced = false;
  345. for_collected_chunks(mem, cp)
  346. gc_do_reloc(cp, mem, &state);
  347. end_phase("relocate chunks");
  348. for_roots(max_trace, mem, rp) {
  349. if_debug3('6', "[6]relocating root 0x%lx: 0x%lx -> 0x%lx\n",
  350. (ulong) rp, (ulong) rp->p, (ulong) * rp->p);
  351. if (rp->ptype == ptr_ref_type) {
  352. ref *pref = (ref *) * rp->p;
  353. igc_reloc_refs((ref_packed *) pref,
  354. (ref_packed *) (pref + 1),
  355. &state);
  356. } else
  357. *rp->p = (*rp->ptype->reloc) (*rp->p, &state);
  358. if_debug3('6', "[6]relocated root 0x%lx: 0x%lx -> 0x%lx\n",
  359. (ulong) rp, (ulong) rp->p, (ulong) * rp->p);
  360. }
  361. end_phase("relocate roots");
  362. /* Compact data. We only do this for spaces we are collecting. */
  363. for_collected_spaces(ispace) {
  364. for_space_mems(ispace, mem) {
  365. for_mem_chunks(mem, cp) {
  366. if_debug_chunk('6', "[6]compacting chunk", cp);
  367. gc_objects_compact(cp, &state);
  368. gc_strings_compact(cp);
  369. if_debug_chunk('6', "[6]after compaction:", cp);
  370. if (mem->pcc == cp)
  371. mem->cc = *cp;
  372. }
  373. mem->saved = mem->reloc_saved;
  374. ialloc_reset_free(mem);
  375. }
  376. }
  377. end_phase("compact");
  378. /* Free empty chunks. */
  379. for_collected_spaces(ispace) {
  380. for_space_mems(ispace, mem) {
  381. gc_free_empty_chunks(mem);
  382. }
  383. }
  384. end_phase("free empty chunks");
  385. /*
  386. * Update previous_status to reflect any freed chunks,
  387. * and set inherited to the negative of allocated,
  388. * so it has no effect. We must update previous_status by
  389. * working back-to-front along the save chain, using pointer reversal.
  390. * (We could update inherited in any order, since it only uses
  391. * information local to the individual save level.)
  392. */
  393. for_collected_spaces(ispace) { /* Reverse the pointers. */
  394. alloc_save_t *curr;
  395. alloc_save_t *prev = 0;
  396. alloc_save_t *next;
  397. gs_memory_status_t total;
  398. for (curr = space_memories[ispace]->saved; curr != 0;
  399. prev = curr, curr = next
  400. ) {
  401. next = curr->state.saved;
  402. curr->state.saved = prev;
  403. }
  404. /* Now work the other way, accumulating the values. */
  405. total.allocated = 0, total.used = 0;
  406. for (curr = prev, prev = 0; curr != 0;
  407. prev = curr, curr = next
  408. ) {
  409. mem = &curr->state;
  410. next = mem->saved;
  411. mem->saved = prev;
  412. mem->previous_status = total;
  413. if_debug3('6',
  414. "[6]0x%lx previous allocated=%lu, used=%lu\n",
  415. (ulong) mem, total.allocated, total.used);
  416. gs_memory_status((gs_memory_t *) mem, &total);
  417. mem->gc_allocated = mem->allocated + total.allocated;
  418. mem->inherited = -(int)mem->allocated;
  419. }
  420. mem = space_memories[ispace];
  421. mem->previous_status = total;
  422. mem->gc_allocated = mem->allocated + total.allocated;
  423. if_debug3('6', "[6]0x%lx previous allocated=%lu, used=%lu\n",
  424. (ulong) mem, total.allocated, total.used);
  425. }
  426. end_phase("update stats");
  427. no_collect:
  428. /* Unregister the allocator roots. */
  429. for_spaces(ispace, max_trace)
  430. gs_unregister_root((gs_memory_t *)space_memories[ispace],
  431. &space_roots[ispace], "gc_top_level");
  432. end_phase("unregister space roots");
  433. #ifdef DEBUG
  434. /* Validate the state. This shouldn't be necessary.... */
  435. gc_validate_spaces(space_memories, max_trace, &state);
  436. end_phase("validate pointers");
  437. #endif
  438. }
  439. /* ------ Debugging utilities ------ */
  440. /* Validate a pointer to an object header. */
  441. #ifdef DEBUG
  442. # define debug_check_object(pre, cp, gcst)\
  443. ialloc_validate_object((pre) + 1, cp, gcst)
  444. #else
  445. # define debug_check_object(pre, cp, gcst) DO_NOTHING
  446. #endif
  447. /* ------ Unmarking phase ------ */
  448. /* Unmark a single struct. */
  449. private void
  450. ptr_struct_unmark(enum_ptr_t *pep, gc_state_t * ignored)
  451. {
  452. void *const vptr = (void *)pep->ptr; /* break const */
  453. if (vptr != 0)
  454. o_set_unmarked(((obj_header_t *) vptr - 1));
  455. }
  456. /* Unmark a single string. */
  457. private void
  458. ptr_string_unmark(enum_ptr_t *pep, gc_state_t * gcst)
  459. {
  460. discard(gc_string_mark(pep->ptr, pep->size, false, gcst));
  461. }
  462. /* Unmark the objects in a chunk. */
  463. private void
  464. gc_objects_clear_marks(const gs_memory_t *mem, chunk_t * cp)
  465. {
  466. if_debug_chunk('6', "[6]unmarking chunk", cp);
  467. SCAN_CHUNK_OBJECTS(cp)
  468. DO_ALL
  469. struct_proc_clear_marks((*proc)) =
  470. pre->o_type->clear_marks;
  471. #ifdef DEBUG
  472. if (pre->o_type != &st_free)
  473. debug_check_object(pre, cp, NULL);
  474. #endif
  475. if_debug3('7', " [7](un)marking %s(%lu) 0x%lx\n",
  476. struct_type_name_string(pre->o_type),
  477. (ulong) size, (ulong) pre);
  478. o_set_unmarked(pre);
  479. if (proc != 0)
  480. (*proc) (mem, pre + 1, size, pre->o_type);
  481. END_OBJECTS_SCAN
  482. }
  483. /* Mark 0- and 1-character names, and those referenced from the */
  484. /* op_array_nx_table, and unmark all the rest. */
  485. private void
  486. gc_unmark_names(name_table * nt)
  487. {
  488. uint i;
  489. names_unmark_all(nt);
  490. for (i = 0; i < op_array_table_global.count; i++) {
  491. name_index_t nidx = op_array_table_global.nx_table[i];
  492. names_mark_index(nt, nidx);
  493. }
  494. for (i = 0; i < op_array_table_local.count; i++) {
  495. name_index_t nidx = op_array_table_local.nx_table[i];
  496. names_mark_index(nt, nidx);
  497. }
  498. }
  499. /* ------ Marking phase ------ */
  500. /* Initialize (a segment of) the mark stack. */
  501. private void
  502. gc_init_mark_stack(gc_mark_stack * pms, uint count)
  503. {
  504. pms->next = 0;
  505. pms->count = count;
  506. pms->entries[0].ptr = 0;
  507. pms->entries[0].index = 0;
  508. pms->entries[0].is_refs = false;
  509. }
  510. /* Mark starting from all marked objects in the interval of a chunk */
  511. /* needing rescanning. */
  512. private int
  513. gc_rescan_chunk(chunk_t * cp, gc_state_t * pstate, gc_mark_stack * pmstack)
  514. {
  515. byte *sbot = cp->rescan_bot;
  516. byte *stop = cp->rescan_top;
  517. gs_gc_root_t root;
  518. void *comp;
  519. int more = 0;
  520. const gs_memory_t *mem = gcst_get_memory_ptr( pstate );
  521. if (sbot > stop)
  522. return 0;
  523. root.p = &comp;
  524. if_debug_chunk('6', "[6]rescanning chunk", cp);
  525. cp->rescan_bot = cp->cend;
  526. cp->rescan_top = cp->cbase;
  527. SCAN_CHUNK_OBJECTS(cp)
  528. DO_ALL
  529. if ((byte *) (pre + 1) + size < sbot);
  530. else if ((byte *) (pre + 1) > stop)
  531. return more; /* 'break' won't work here */
  532. else {
  533. if_debug2('7', " [7]scanning/marking 0x%lx(%lu)\n",
  534. (ulong) pre, (ulong) size);
  535. if (pre->o_type == &st_refs) {
  536. ref_packed *rp = (ref_packed *) (pre + 1);
  537. char *end = (char *)rp + size;
  538. root.ptype = ptr_ref_type;
  539. while ((char *)rp < end) {
  540. comp = rp;
  541. if (r_is_packed(rp)) {
  542. if (r_has_pmark(rp)) {
  543. r_clear_pmark(rp);
  544. more |= gc_trace(&root, pstate,
  545. pmstack);
  546. }
  547. rp++;
  548. } else {
  549. ref *const pref = (ref *)rp;
  550. if (r_has_attr(pref, l_mark)) {
  551. r_clear_attrs(pref, l_mark);
  552. more |= gc_trace(&root, pstate, pmstack);
  553. }
  554. rp += packed_per_ref;
  555. }
  556. }
  557. } else if (!o_is_unmarked(pre)) {
  558. struct_proc_clear_marks((*proc)) =
  559. pre->o_type->clear_marks;
  560. root.ptype = ptr_struct_type;
  561. comp = pre + 1;
  562. if (!o_is_untraced(pre))
  563. o_set_unmarked(pre);
  564. if (proc != 0)
  565. (*proc) (mem, comp, size, pre->o_type);
  566. more |= gc_trace(&root, pstate, pmstack);
  567. }
  568. }
  569. END_OBJECTS_SCAN
  570. return more;
  571. }
  572. /* Mark starting from all the objects in a chunk. */
  573. /* We assume that pstate->min_collect > avm_system, */
  574. /* so we don't have to trace names. */
  575. private int
  576. gc_trace_chunk(const gs_memory_t *mem, chunk_t * cp, gc_state_t * pstate, gc_mark_stack * pmstack)
  577. {
  578. gs_gc_root_t root;
  579. void *comp;
  580. int more = 0;
  581. int min_trace = pstate->min_collect;
  582. root.p = &comp;
  583. if_debug_chunk('6', "[6]marking from chunk", cp);
  584. SCAN_CHUNK_OBJECTS(cp)
  585. DO_ALL
  586. {
  587. if_debug2('7', " [7]scanning/marking 0x%lx(%lu)\n",
  588. (ulong) pre, (ulong) size);
  589. if (pre->o_type == &st_refs) {
  590. ref_packed *rp = (ref_packed *) (pre + 1);
  591. char *end = (char *)rp + size;
  592. root.ptype = ptr_ref_type;
  593. while ((char *)rp < end) {
  594. comp = rp;
  595. if (r_is_packed(rp)) { /* No packed refs need tracing. */
  596. rp++;
  597. } else {
  598. ref *const pref = (ref *)rp;
  599. if (r_space(pref) >= min_trace) {
  600. r_clear_attrs(pref, l_mark);
  601. more |= gc_trace(&root, pstate, pmstack);
  602. }
  603. rp += packed_per_ref;
  604. }
  605. }
  606. } else if (!o_is_unmarked(pre)) {
  607. if (!o_is_untraced(pre))
  608. o_set_unmarked(pre);
  609. if (pre->o_type != &st_free) {
  610. struct_proc_clear_marks((*proc)) =
  611. pre->o_type->clear_marks;
  612. root.ptype = ptr_struct_type;
  613. comp = pre + 1;
  614. if (proc != 0)
  615. (*proc) (mem, comp, size, pre->o_type);
  616. more |= gc_trace(&root, pstate, pmstack);
  617. }
  618. }
  619. }
  620. END_OBJECTS_SCAN
  621. return more;
  622. }
  623. /* Recursively mark from a (root) pointer. */
  624. /* Return -1 if we overflowed the mark stack, */
  625. /* 0 if we completed successfully without marking any new objects, */
  626. /* 1 if we completed and marked some new objects. */
  627. private int gc_extend_stack(gc_mark_stack *, gc_state_t *);
  628. private int
  629. gc_trace(gs_gc_root_t * rp, gc_state_t * pstate, gc_mark_stack * pmstack)
  630. {
  631. int min_trace = pstate->min_collect;
  632. gc_mark_stack *pms = pmstack;
  633. ms_entry *sp = pms->entries + 1;
  634. /* We stop the mark stack 1 entry early, because we store into */
  635. /* the entry beyond the top. */
  636. ms_entry *stop = sp + pms->count - 2;
  637. int new = 0;
  638. enum_ptr_t nep;
  639. void *nptr;
  640. name_table *nt = pstate->ntable;
  641. #define mark_name(nidx)\
  642. BEGIN\
  643. if (names_mark_index(nt, nidx)) {\
  644. new |= 1;\
  645. if_debug2('8', " [8]marked name 0x%lx(%u)\n",\
  646. (ulong)names_index_ptr(nt, nidx), nidx);\
  647. }\
  648. END
  649. nptr = *rp->p;
  650. if (nptr == 0)
  651. return 0;
  652. /* Initialize the stack */
  653. sp->ptr = nptr;
  654. if (rp->ptype == ptr_ref_type)
  655. sp->index = 1, sp->is_refs = true;
  656. else {
  657. sp->index = 0, sp->is_refs = false;
  658. nep.ptr = nptr;
  659. if ((*rp->ptype->mark) (&nep, pstate))
  660. new |= 1;
  661. }
  662. for (;;) {
  663. gs_ptr_type_t ptp;
  664. /*
  665. * The following should really be an if..else, but that
  666. * would force unnecessary is_refs tests.
  667. */
  668. if (sp->is_refs)
  669. goto do_refs;
  670. /* ---------------- Structure ---------------- */
  671. do_struct:
  672. {
  673. obj_header_t *ptr = sp->ptr;
  674. struct_proc_enum_ptrs((*mproc));
  675. if (ptr == 0) { /* We've reached the bottom of a stack segment. */
  676. pms = pms->prev;
  677. if (pms == 0)
  678. break; /* all done */
  679. stop = pms->entries + pms->count - 1;
  680. sp = stop;
  681. continue;
  682. }
  683. debug_check_object(ptr - 1, NULL, NULL);
  684. ts:if_debug4('7', " [7]%smarking %s 0x%lx[%u]",
  685. depth_dots(sp, pms),
  686. struct_type_name_string(ptr[-1].o_type),
  687. (ulong) ptr, sp->index);
  688. mproc = ptr[-1].o_type->enum_ptrs;
  689. if (mproc == gs_no_struct_enum_ptrs ||
  690. (ptp = (*mproc)
  691. (gcst_get_memory_ptr(pstate), ptr, pre_obj_contents_size(ptr - 1),
  692. sp->index, &nep, ptr[-1].o_type, pstate)) == 0
  693. ) {
  694. if_debug0('7', " - done\n");
  695. sp--;
  696. continue;
  697. }
  698. /* The cast in the following statement is the one */
  699. /* place we need to break 'const' to make the */
  700. /* template for pointer enumeration work. */
  701. nptr = (void *)nep.ptr;
  702. sp->index++;
  703. if_debug1('7', " = 0x%lx\n", (ulong) nptr);
  704. /* Descend into nep.ptr, whose pointer type is ptp. */
  705. if (ptp == ptr_struct_type) {
  706. sp[1].index = 0;
  707. sp[1].is_refs = false;
  708. if (sp == stop)
  709. goto push;
  710. if (!ptr_struct_mark(&nep, pstate))
  711. goto ts;
  712. new |= 1;
  713. (++sp)->ptr = nptr;
  714. goto do_struct;
  715. } else if (ptp == ptr_ref_type) {
  716. sp[1].index = 1;
  717. sp[1].is_refs = true;
  718. if (sp == stop)
  719. goto push;
  720. new |= 1;
  721. (++sp)->ptr = nptr;
  722. goto do_refs;
  723. } else { /* We assume this is some non-pointer- */
  724. /* containing type. */
  725. if ((*ptp->mark) (&nep, pstate))
  726. new |= 1;
  727. goto ts;
  728. }
  729. }
  730. /* ---------------- Refs ---------------- */
  731. do_refs:
  732. {
  733. ref_packed *pptr = sp->ptr;
  734. ref *rptr;
  735. tr:if (!sp->index) {
  736. --sp;
  737. continue;
  738. }
  739. --(sp->index);
  740. if_debug3('8', " [8]%smarking refs 0x%lx[%u]\n",
  741. depth_dots(sp, pms), (ulong) pptr, sp->index);
  742. if (r_is_packed(pptr)) {
  743. if (!r_has_pmark(pptr)) {
  744. r_set_pmark(pptr);
  745. new |= 1;
  746. if (r_packed_is_name(pptr)) {
  747. name_index_t nidx = packed_name_index(pptr);
  748. mark_name(nidx);
  749. }
  750. }
  751. ++pptr;
  752. goto tr;
  753. }
  754. rptr = (ref *) pptr; /* * const beyond here */
  755. if (r_has_attr(rptr, l_mark)) {
  756. pptr = (ref_packed *)(rptr + 1);
  757. goto tr;
  758. }
  759. r_set_attrs(rptr, l_mark);
  760. new |= 1;
  761. if (r_space(rptr) < min_trace) { /* Note that this always picks up all scalars. */
  762. pptr = (ref_packed *) (rptr + 1);
  763. goto tr;
  764. }
  765. sp->ptr = rptr + 1;
  766. switch (r_type(rptr)) {
  767. /* Struct cases */
  768. case t_file:
  769. nptr = rptr->value.pfile;
  770. rs:sp[1].is_refs = false;
  771. sp[1].index = 0;
  772. if (sp == stop) {
  773. ptp = ptr_struct_type;
  774. break;
  775. }
  776. nep.ptr = nptr;
  777. if (!ptr_struct_mark(&nep, pstate))
  778. goto nr;
  779. new |= 1;
  780. (++sp)->ptr = nptr;
  781. goto do_struct;
  782. case t_device:
  783. nptr = rptr->value.pdevice;
  784. goto rs;
  785. case t_fontID:
  786. case t_struct:
  787. case t_astruct:
  788. nptr = rptr->value.pstruct;
  789. goto rs;
  790. /* Non-trivial non-struct cases */
  791. case t_dictionary:
  792. nptr = rptr->value.pdict;
  793. sp[1].index = sizeof(dict) / sizeof(ref);
  794. goto rrp;
  795. case t_array:
  796. nptr = rptr->value.refs;
  797. rr:if ((sp[1].index = r_size(rptr)) == 0) { /* Set the base pointer to 0, */
  798. /* so we never try to relocate it. */
  799. rptr->value.refs = 0;
  800. goto nr;
  801. }
  802. rrp:
  803. rrc:sp[1].is_refs = true;
  804. if (sp == stop) {
  805. /*
  806. * The following initialization is unnecessary:
  807. * ptp will not be used if sp[1].is_refs = true.
  808. * We put this here solely to get rid of bogus
  809. * "possibly uninitialized variable" warnings
  810. * from certain compilers.
  811. */
  812. ptp = ptr_ref_type;
  813. break;
  814. }
  815. new |= 1;
  816. (++sp)->ptr = nptr;
  817. goto do_refs;
  818. case t_mixedarray:
  819. case t_shortarray:
  820. nptr = rptr->value.writable_packed;
  821. goto rr;
  822. case t_name:
  823. mark_name(names_index(nt, rptr));
  824. nr:pptr = (ref_packed *) (rptr + 1);
  825. goto tr;
  826. case t_string:
  827. if (gc_string_mark(rptr->value.bytes, r_size(rptr), true, pstate))
  828. new |= 1;
  829. goto nr;
  830. case t_oparray:
  831. nptr = rptr->value.refs; /* discard const */
  832. sp[1].index = 1;
  833. goto rrc;
  834. default:
  835. goto nr;
  836. }
  837. }
  838. /* ---------------- Recursion ---------------- */
  839. push:
  840. if (sp == stop) { /* The current segment is full. */
  841. int new_added = gc_extend_stack(pms, pstate);
  842. if (new_added) {
  843. new |= new_added;
  844. continue;
  845. }
  846. pms = pms->next;
  847. stop = pms->entries + pms->count - 1;
  848. pms->entries[1] = sp[1];
  849. sp = pms->entries;
  850. }
  851. /* index and is_refs are already set */
  852. if (!sp[1].is_refs) {
  853. nep.ptr = nptr;
  854. if (!(*ptp->mark) (&nep, pstate))
  855. continue;
  856. new |= 1;
  857. }
  858. (++sp)->ptr = nptr;
  859. }
  860. return new;
  861. }
  862. /* Link to, attempting to allocate if necessary, */
  863. /* another chunk of mark stack. */
  864. private int
  865. gc_extend_stack(gc_mark_stack * pms, gc_state_t * pstate)
  866. {
  867. if (pms->next == 0) { /* Try to allocate another segment. */
  868. uint count;
  869. for (count = ms_size_desired; count >= ms_size_min; count >>= 1) {
  870. pms->next = (gc_mark_stack *)
  871. gs_alloc_bytes_immovable(pstate->heap,
  872. sizeof(gc_mark_stack) +
  873. sizeof(ms_entry) * count,
  874. "gc mark stack");
  875. if (pms->next != 0)
  876. break;
  877. }
  878. if (pms->next == 0) { /* The mark stack overflowed. */
  879. ms_entry *sp = pms->entries + pms->count - 1;
  880. byte *cptr = sp->ptr; /* container */
  881. chunk_t *cp = gc_locate(cptr, pstate);
  882. int new = 1;
  883. if (cp == 0) { /* We were tracing outside collectible */
  884. /* storage. This can't happen. */
  885. lprintf1("mark stack overflowed while outside collectible space at 0x%lx!\n",
  886. (ulong) cptr);
  887. gs_abort(pstate->heap);
  888. }
  889. if (cptr < cp->rescan_bot)
  890. cp->rescan_bot = cptr, new = -1;
  891. if (cptr > cp->rescan_top)
  892. cp->rescan_top = cptr, new = -1;
  893. return new;
  894. }
  895. gc_init_mark_stack(pms->next, count);
  896. pms->next->prev = pms;
  897. pms->next->on_heap = true;
  898. }
  899. return 0;
  900. }
  901. /* Mark a struct. Return true if new mark. */
  902. private bool
  903. ptr_struct_mark(enum_ptr_t *pep, gc_state_t * ignored)
  904. {
  905. obj_header_t *ptr = (obj_header_t *)pep->ptr;
  906. if (ptr == 0)
  907. return false;
  908. ptr--; /* point to header */
  909. if (!o_is_unmarked(ptr))
  910. return false;
  911. o_mark(ptr);
  912. return true;
  913. }
  914. /* Mark a string. Return true if new mark. */
  915. private bool
  916. ptr_string_mark(enum_ptr_t *pep, gc_state_t * gcst)
  917. {
  918. return gc_string_mark(pep->ptr, pep->size, true, gcst);
  919. }
  920. /* Finish tracing by marking names. */
  921. private bool
  922. gc_trace_finish(gc_state_t * pstate)
  923. {
  924. name_table *nt = pstate->ntable;
  925. name_index_t nidx = 0;
  926. bool marked = false;
  927. while ((nidx = names_next_valid_index(nt, nidx)) != 0) {
  928. name_string_t *pnstr = names_index_string_inline(nt, nidx);
  929. if (pnstr->mark) {
  930. enum_ptr_t enst, ensst;
  931. if (!pnstr->foreign_string &&
  932. gc_string_mark(pnstr->string_bytes, pnstr->string_size,
  933. true, pstate)
  934. )
  935. marked = true;
  936. enst.ptr = names_index_sub_table(nt, nidx);
  937. ensst.ptr = names_index_string_sub_table(nt, nidx);
  938. marked |=
  939. ptr_struct_mark(&enst, pstate) |
  940. ptr_struct_mark(&ensst, pstate);
  941. }
  942. }
  943. return marked;
  944. }
  945. /* ------ Relocation planning phase ------ */
  946. /* Initialize the relocation information in the chunk header. */
  947. private void
  948. gc_init_reloc(chunk_t * cp)
  949. {
  950. chunk_head_t *chead = cp->chead;
  951. chead->dest = cp->cbase;
  952. chead->free.o_back =
  953. offset_of(chunk_head_t, free) >> obj_back_shift;
  954. chead->free.o_size = sizeof(obj_header_t);
  955. chead->free.o_nreloc = 0;
  956. }
  957. /* Set marks and clear relocation for chunks that won't be compacted. */
  958. private void
  959. gc_clear_reloc(chunk_t * cp)
  960. {
  961. byte *pfree = (byte *) & cp->chead->free;
  962. gc_init_reloc(cp);
  963. SCAN_CHUNK_OBJECTS(cp)
  964. DO_ALL
  965. const struct_shared_procs_t *procs =
  966. pre->o_type->shared;
  967. if (procs != 0)
  968. (*procs->clear_reloc) (pre, size);
  969. o_set_untraced(pre);
  970. pre->o_back = ((byte *) pre - pfree) >> obj_back_shift;
  971. END_OBJECTS_SCAN
  972. gc_strings_set_marks(cp, true);
  973. gc_strings_clear_reloc(cp);
  974. }
  975. /* Set the relocation for the objects in a chunk. */
  976. /* This will never be called for a chunk with any o_untraced objects. */
  977. private void
  978. gc_objects_set_reloc(chunk_t * cp)
  979. {
  980. uint reloc = 0;
  981. chunk_head_t *chead = cp->chead;
  982. byte *pfree = (byte *) & chead->free; /* most recent free object */
  983. if_debug_chunk('6', "[6]setting reloc for chunk", cp);
  984. gc_init_reloc(cp);
  985. SCAN_CHUNK_OBJECTS(cp)
  986. DO_ALL
  987. struct_proc_finalize((*finalize));
  988. const struct_shared_procs_t *procs =
  989. pre->o_type->shared;
  990. if ((procs == 0 ? o_is_unmarked(pre) :
  991. !(*procs->set_reloc) (pre, reloc, size))
  992. ) { /* Free object */
  993. reloc += sizeof(obj_header_t) + obj_align_round(size);
  994. if ((finalize = pre->o_type->finalize) != 0) {
  995. if_debug2('u', "[u]GC finalizing %s 0x%lx\n",
  996. struct_type_name_string(pre->o_type),
  997. (ulong) (pre + 1));
  998. (*finalize) (pre + 1);
  999. }
  1000. pfree = (byte *) pre;
  1001. pre->o_back = (pfree - (byte *) chead) >> obj_back_shift;
  1002. pre->o_nreloc = reloc;
  1003. if_debug3('7', " [7]at 0x%lx, unmarked %lu, new reloc = %u\n",
  1004. (ulong) pre, (ulong) size, reloc);
  1005. } else { /* Useful object */
  1006. debug_check_object(pre, cp, NULL);
  1007. pre->o_back = ((byte *) pre - pfree) >> obj_back_shift;
  1008. }
  1009. END_OBJECTS_SCAN
  1010. #ifdef DEBUG
  1011. if (reloc != 0) {
  1012. if_debug1('6', "[6]freed %u", reloc);
  1013. if_debug_chunk('6', " in", cp);
  1014. }
  1015. #endif
  1016. }
  1017. /* ------ Relocation phase ------ */
  1018. /* Relocate the pointers in all the objects in a chunk. */
  1019. private void
  1020. gc_do_reloc(chunk_t * cp, gs_ref_memory_t * mem, gc_state_t * pstate)
  1021. {
  1022. chunk_head_t *chead = cp->chead;
  1023. if_debug_chunk('6', "[6]relocating in chunk", cp);
  1024. SCAN_CHUNK_OBJECTS(cp)
  1025. DO_ALL
  1026. #ifdef DEBUG
  1027. pstate->container = cp;
  1028. #endif
  1029. /* We need to relocate the pointers in an object iff */
  1030. /* it is o_untraced, or it is a useful object. */
  1031. /* An object is free iff its back pointer points to */
  1032. /* the chunk_head structure. */
  1033. if (o_is_untraced(pre) ||
  1034. pre->o_back << obj_back_shift != (byte *) pre - (byte *) chead
  1035. ) {
  1036. struct_proc_reloc_ptrs((*proc)) =
  1037. pre->o_type->reloc_ptrs;
  1038. if_debug3('7',
  1039. " [7]relocating ptrs in %s(%lu) 0x%lx\n",
  1040. struct_type_name_string(pre->o_type),
  1041. (ulong) size, (ulong) pre);
  1042. if (proc != 0)
  1043. (*proc) (pre + 1, size, pre->o_type, pstate);
  1044. }
  1045. #ifdef DEBUG
  1046. pstate->container = 0;
  1047. #endif
  1048. END_OBJECTS_SCAN
  1049. }
  1050. /* Print pointer relocation if debugging. */
  1051. /* We have to provide this procedure even if DEBUG is not defined, */
  1052. /* in case one of the other GC modules was compiled with DEBUG. */
  1053. const void *
  1054. print_reloc_proc(const void *obj, const char *cname, const void *robj)
  1055. {
  1056. if_debug3('9', " [9]relocate %s * 0x%lx to 0x%lx\n",
  1057. cname, (ulong)obj, (ulong)robj);
  1058. return robj;
  1059. }
  1060. /* Relocate a pointer to an (aligned) object. */
  1061. /* See gsmemory.h for why the argument is const and the result is not. */
  1062. private void /*obj_header_t */ *
  1063. igc_reloc_struct_ptr(const void /*obj_header_t */ *obj, gc_state_t * gcst)
  1064. {
  1065. const obj_header_t *const optr = (const obj_header_t *)obj;
  1066. const void *robj;
  1067. if (obj == 0) {
  1068. discard(print_reloc(obj, "NULL", 0));
  1069. return 0;
  1070. }
  1071. debug_check_object(optr - 1, NULL, gcst);
  1072. {
  1073. uint back = optr[-1].o_back;
  1074. if (back == o_untraced)
  1075. robj = obj;
  1076. else {
  1077. #ifdef DEBUG
  1078. /* Do some sanity checking. */
  1079. chunk_t *cp = gcst->container;
  1080. if (cp != 0 && cp->cbase <= (byte *)obj && (byte *)obj <cp->ctop) {
  1081. if (back > (cp->ctop - cp->cbase) >> obj_back_shift) {
  1082. lprintf2("Invalid back pointer %u at 0x%lx!\n",
  1083. back, (ulong) obj);
  1084. gs_abort(NULL);
  1085. }
  1086. } else {
  1087. /* Pointed to unknown chunk. Can't check it, sorry. */
  1088. }
  1089. #endif
  1090. {
  1091. const obj_header_t *pfree = (const obj_header_t *)
  1092. ((const char *)(optr - 1) -
  1093. (back << obj_back_shift));
  1094. const chunk_head_t *chead = (const chunk_head_t *)
  1095. ((const char *)pfree -
  1096. (pfree->o_back << obj_back_shift));
  1097. robj = chead->dest +
  1098. ((const char *)obj - (const char *)(chead + 1) -
  1099. pfree->o_nreloc);
  1100. }
  1101. }
  1102. }
  1103. /* Use a severely deprecated pun to remove the const property. */
  1104. {
  1105. union { const void *r; void *w; } u;
  1106. u.r = print_reloc(obj, struct_type_name_string(optr[-1].o_type), robj);
  1107. return u.w;
  1108. }
  1109. }
  1110. /* ------ Compaction phase ------ */
  1111. /* Compact the objects in a chunk. */
  1112. /* This will never be called for a chunk with any o_untraced objects. */
  1113. private void
  1114. gc_objects_compact(chunk_t * cp, gc_state_t * gcst)
  1115. {
  1116. chunk_head_t *chead = cp->chead;
  1117. obj_header_t *dpre = (obj_header_t *) chead->dest;
  1118. const gs_memory_t *cmem = gcst->spaces.memories.named.system->stable_memory;
  1119. SCAN_CHUNK_OBJECTS(cp)
  1120. DO_ALL
  1121. /* An object is free iff its back pointer points to */
  1122. /* the chunk_head structure. */
  1123. if (pre->o_back << obj_back_shift != (byte *) pre - (byte *) chead) {
  1124. const struct_shared_procs_t *procs = pre->o_type->shared;
  1125. debug_check_object(pre, cp, gcst);
  1126. if_debug4('7',
  1127. " [7]compacting %s 0x%lx(%lu) to 0x%lx\n",
  1128. struct_type_name_string(pre->o_type),
  1129. (ulong) pre, (ulong) size, (ulong) dpre);
  1130. if (procs == 0) {
  1131. if (dpre != pre)
  1132. memmove(dpre, pre,
  1133. sizeof(obj_header_t) + size);
  1134. } else
  1135. (*procs->compact) (cmem, pre, dpre, size);
  1136. dpre = (obj_header_t *)
  1137. ((byte *) dpre + obj_size_round(size));
  1138. }
  1139. END_OBJECTS_SCAN
  1140. if (cp->outer == 0 && chead->dest != cp->cbase)
  1141. dpre = (obj_header_t *) cp->cbase; /* compacted this chunk into another */
  1142. gs_alloc_fill(dpre, gs_alloc_fill_collected, cp->cbot - (byte *) dpre);
  1143. cp->cbot = (byte *) dpre;
  1144. cp->rcur = 0;
  1145. cp->rtop = 0; /* just to be sure */
  1146. }
  1147. /* ------ Cleanup ------ */
  1148. /* Free empty chunks. */
  1149. private void
  1150. gc_free_empty_chunks(gs_ref_memory_t * mem)
  1151. {
  1152. chunk_t *cp;
  1153. chunk_t *csucc;
  1154. /* Free the chunks in reverse order, */
  1155. /* to encourage LIFO behavior. */
  1156. for (cp = mem->clast; cp != 0; cp = csucc) { /* Make sure this isn't an inner chunk, */
  1157. /* or a chunk that has inner chunks. */
  1158. csucc = cp->cprev; /* save before freeing */
  1159. if (cp->cbot == cp->cbase && cp->ctop == cp->climit &&
  1160. cp->outer == 0 && cp->inner_count == 0
  1161. ) {
  1162. alloc_free_chunk(cp, mem);
  1163. if (mem->pcc == cp)
  1164. mem->pcc = 0;
  1165. }
  1166. }
  1167. }
  1168. const gs_memory_t * gcst_get_memory_ptr(gc_state_t *gcst)
  1169. {
  1170. vm_spaces spaces = gcst->spaces;
  1171. const gs_memory_t *cmem = space_system->stable_memory;
  1172. return cmem;
  1173. }