isave.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /* Copyright (C) 1993, 2000 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: isave.c,v 1.3 2000/09/19 19:00:46 lpd Exp $ */
  16. /* Save/restore manager for Ghostscript interpreter */
  17. #include "ghost.h"
  18. #include "memory_.h"
  19. #include "errors.h"
  20. #include "gsexit.h"
  21. #include "gsstruct.h"
  22. #include "stream.h" /* for linking for forgetsave */
  23. #include "iastate.h"
  24. #include "inamedef.h"
  25. #include "iname.h"
  26. #include "ipacked.h"
  27. #include "isave.h"
  28. #include "isstate.h"
  29. #include "store.h" /* for ref_assign */
  30. #include "ivmspace.h"
  31. #include "gsutil.h" /* gs_next_ids prototype */
  32. /* Imported save/restore routines */
  33. extern void font_restore(P1(const alloc_save_t *));
  34. /* Structure descriptor */
  35. private_st_alloc_save();
  36. /* Define the maximum amount of data we are willing to scan repeatedly -- */
  37. /* see below for details. */
  38. private const long max_repeated_scan = 100000;
  39. /*
  40. * The logic for saving and restoring the state is complex.
  41. * Both the changes to individual objects, and the overall state
  42. * of the memory manager, must be saved and restored.
  43. */
  44. /*
  45. * To save the state of the memory manager:
  46. * Save the state of the current chunk in which we are allocating.
  47. * Shrink the current chunk to its inner unallocated region.
  48. * Save and reset the free block chains.
  49. * By doing this, we guarantee that no object older than the save
  50. * can be freed.
  51. *
  52. * To restore the state of the memory manager:
  53. * Free all chunks newer than the save, and the descriptor for
  54. * the inner chunk created by the save.
  55. * Make current the chunk that was current at the time of the save.
  56. * Restore the state of the current chunk.
  57. *
  58. * In addition to save ("start transaction") and restore ("abort transaction"),
  59. * we support forgetting a save ("commit transation"). To forget a save:
  60. * Reassign to the next outer save all chunks newer than the save.
  61. * Free the descriptor for the inner chunk, updating its outer chunk
  62. * to reflect additional allocations in the inner chunk.
  63. * Concatenate the free block chains with those of the outer save.
  64. */
  65. /*
  66. * For saving changes to individual objects, we add an "attribute" bit
  67. * (l_new) that logically belongs to the slot where the ref is stored,
  68. * not to the ref itself. The bit means "the contents of this slot
  69. * have been changed, or the slot was allocated, since the last save."
  70. * To keep track of changes since the save, we associate a chain of
  71. * <slot, old_contents> pairs that remembers the old contents of slots.
  72. *
  73. * When creating an object, if the save level is non-zero:
  74. * Set l_new in all slots.
  75. *
  76. * When storing into a slot, if the save level is non-zero:
  77. * If l_new isn't set, save the address and contents of the slot
  78. * on the current contents chain.
  79. * Set l_new after storing the new value.
  80. *
  81. * To do a save:
  82. * If the save level is non-zero:
  83. * Reset l_new in all slots on the contents chain, and in all
  84. * objects created since the previous save.
  85. * Push the head of the contents chain, and reset the chain to empty.
  86. *
  87. * To do a restore:
  88. * Check all the stacks to make sure they don't contain references
  89. * to objects created since the save.
  90. * Restore all the slots on the contents chain.
  91. * Pop the contents chain head.
  92. * If the save level is now non-zero:
  93. * Scan the newly restored contents chain, and set l_new in all
  94. * the slots it references.
  95. * Scan all objects created since the previous save, and set
  96. * l_new in all the slots of each object.
  97. *
  98. * To forget a save:
  99. * If the save level is greater than 1:
  100. * Set l_new as for a restore, per the next outer save.
  101. * Concatenate the next outer contents chain to the end of
  102. * the current one.
  103. * If the save level is 1:
  104. * Reset l_new as for a save.
  105. * Free the contents chain.
  106. */
  107. /*
  108. * A consequence of the foregoing algorithms is that the cost of a save is
  109. * proportional to the total amount of data allocated since the previous
  110. * save. If a PostScript program reads in a large amount of setup code and
  111. * then uses save/restore heavily, each save/restore will be expensive. To
  112. * mitigate this, we check to see how much data we have scanned at this save
  113. * level: if it is large, we do a second, invisible save. This greatly
  114. * reduces the cost of inner saves, at the expense of possibly saving some
  115. * changes twice that otherwise would only have to be saved once.
  116. */
  117. /*
  118. * The presence of global and local VM complicates the situation further.
  119. * There is a separate save chain and contents chain for each VM space.
  120. * When multiple contexts are fully implemented, save and restore will have
  121. * the following effects, according to the privacy status of the current
  122. * context's global and local VM:
  123. * Private global, private local:
  124. * The outermost save saves both global and local VM;
  125. * otherwise, save only saves local VM.
  126. * Shared global, private local:
  127. * Save only saves local VM.
  128. * Shared global, shared local:
  129. * Save only saves local VM, and suspends all other contexts
  130. * sharing the same local VM until the matching restore.
  131. * Since we do not currently implement multiple contexts, only the first
  132. * case is relevant.
  133. *
  134. * Note that when saving the contents of a slot, the choice of chain
  135. * is determined by the VM space in which the slot is allocated,
  136. * not by the current allocation mode.
  137. */
  138. /* Tracing printout */
  139. private void
  140. print_save(const char *str, uint spacen, const alloc_save_t *sav)
  141. {
  142. if_debug5('u', "[u]%s space %u 0x%lx: cdata = 0x%lx, id = %lu\n",\
  143. str, spacen, (ulong)sav, (ulong)sav->client_data, (ulong)sav->id);
  144. }
  145. /*
  146. * Structure for saved change chain for save/restore. Because of the
  147. * garbage collector, we need to distinguish the cases where the change
  148. * is in a static object, a dynamic ref, or a dynamic struct.
  149. */
  150. typedef struct alloc_change_s alloc_change_t;
  151. struct alloc_change_s {
  152. alloc_change_t *next;
  153. ref_packed *where;
  154. ref contents;
  155. #define AC_OFFSET_STATIC (-2) /* static object */
  156. #define AC_OFFSET_REF (-1) /* dynamic ref */
  157. short offset; /* if >= 0, offset within struct */
  158. };
  159. private
  160. CLEAR_MARKS_PROC(change_clear_marks)
  161. {
  162. alloc_change_t *const ptr = (alloc_change_t *)vptr;
  163. if (r_is_packed(&ptr->contents))
  164. r_clear_pmark((ref_packed *) & ptr->contents);
  165. else
  166. r_clear_attrs(&ptr->contents, l_mark);
  167. }
  168. private
  169. ENUM_PTRS_WITH(change_enum_ptrs, alloc_change_t *ptr) return 0;
  170. ENUM_PTR(0, alloc_change_t, next);
  171. case 1:
  172. if (ptr->offset >= 0)
  173. ENUM_RETURN((byte *) ptr->where - ptr->offset);
  174. else
  175. ENUM_RETURN_REF(ptr->where);
  176. case 2:
  177. ENUM_RETURN_REF(&ptr->contents);
  178. ENUM_PTRS_END
  179. private RELOC_PTRS_WITH(change_reloc_ptrs, alloc_change_t *ptr)
  180. {
  181. RELOC_VAR(ptr->next);
  182. switch (ptr->offset) {
  183. case AC_OFFSET_STATIC:
  184. break;
  185. case AC_OFFSET_REF:
  186. RELOC_REF_PTR_VAR(ptr->where);
  187. break;
  188. default:
  189. {
  190. byte *obj = (byte *) ptr->where - ptr->offset;
  191. RELOC_VAR(obj);
  192. ptr->where = (ref_packed *) (obj + ptr->offset);
  193. }
  194. break;
  195. }
  196. if (r_is_packed(&ptr->contents))
  197. r_clear_pmark((ref_packed *) & ptr->contents);
  198. else {
  199. RELOC_REF_VAR(ptr->contents);
  200. r_clear_attrs(&ptr->contents, l_mark);
  201. }
  202. }
  203. RELOC_PTRS_END
  204. gs_private_st_complex_only(st_alloc_change, alloc_change_t, "alloc_change",
  205. change_clear_marks, change_enum_ptrs, change_reloc_ptrs, 0);
  206. /* Debugging printout */
  207. #ifdef DEBUG
  208. private void
  209. alloc_save_print(alloc_change_t * cp, bool print_current)
  210. {
  211. dprintf2(" 0x%lx: 0x%lx: ", (ulong) cp, (ulong) cp->where);
  212. if (r_is_packed(&cp->contents)) {
  213. if (print_current)
  214. dprintf2("saved=%x cur=%x\n", *(ref_packed *) & cp->contents,
  215. *cp->where);
  216. else
  217. dprintf1("%x\n", *(ref_packed *) & cp->contents);
  218. } else {
  219. if (print_current)
  220. dprintf6("saved=%x %x %lx cur=%x %x %lx\n",
  221. r_type_attrs(&cp->contents), r_size(&cp->contents),
  222. (ulong) cp->contents.value.intval,
  223. r_type_attrs((ref *) cp->where),
  224. r_size((ref *) cp->where),
  225. (ulong) ((ref *) cp->where)->value.intval);
  226. else
  227. dprintf3("%x %x %lx\n",
  228. r_type_attrs(&cp->contents), r_size(&cp->contents),
  229. (ulong) cp->contents.value.intval);
  230. }
  231. }
  232. #endif
  233. /* Forward references */
  234. private void restore_resources(P2(alloc_save_t *, gs_ref_memory_t *));
  235. private void restore_free(P1(gs_ref_memory_t *));
  236. private long save_set_new(P2(gs_ref_memory_t *, bool));
  237. private void save_set_new_changes(P2(gs_ref_memory_t *, bool));
  238. /* Initialize the save/restore machinery. */
  239. void
  240. alloc_save_init(gs_dual_memory_t * dmem)
  241. {
  242. alloc_set_not_in_save(dmem);
  243. }
  244. /* Record that we are in a save. */
  245. private void
  246. alloc_set_masks(gs_dual_memory_t *dmem, uint new_mask, uint test_mask)
  247. {
  248. int i;
  249. gs_ref_memory_t *mem;
  250. dmem->new_mask = new_mask;
  251. dmem->test_mask = test_mask;
  252. for (i = 0; i < countof(dmem->spaces.memories.indexed); ++i)
  253. if ((mem = dmem->spaces.memories.indexed[i]) != 0) {
  254. mem->new_mask = new_mask, mem->test_mask = test_mask;
  255. if (mem->stable_memory != (gs_memory_t *)mem) {
  256. mem = (gs_ref_memory_t *)mem->stable_memory;
  257. mem->new_mask = new_mask, mem->test_mask = test_mask;
  258. }
  259. }
  260. }
  261. void
  262. alloc_set_in_save(gs_dual_memory_t *dmem)
  263. {
  264. alloc_set_masks(dmem, l_new, l_new);
  265. }
  266. /* Record that we are not in a save. */
  267. void
  268. alloc_set_not_in_save(gs_dual_memory_t *dmem)
  269. {
  270. alloc_set_masks(dmem, 0, ~0);
  271. }
  272. /* Save the state. */
  273. private alloc_save_t *alloc_save_space(P3(gs_ref_memory_t *mem,
  274. gs_dual_memory_t *dmem,
  275. ulong sid));
  276. private void
  277. alloc_free_save(gs_ref_memory_t *mem, alloc_save_t *save, const char *scn,
  278. const char *icn)
  279. {
  280. chunk_t *inner = mem->pcc;
  281. gs_free_object((gs_memory_t *)mem, save, scn);
  282. gs_free_object(mem->parent, inner, icn);
  283. }
  284. ulong
  285. alloc_save_state(gs_dual_memory_t * dmem, void *cdata)
  286. {
  287. gs_ref_memory_t *lmem = dmem->space_local;
  288. gs_ref_memory_t *gmem = dmem->space_global;
  289. ulong sid = gs_next_ids(2);
  290. bool global =
  291. lmem->save_level == 0 && gmem != lmem &&
  292. gmem->num_contexts == 1;
  293. alloc_save_t *gsave =
  294. (global ? alloc_save_space(gmem, dmem, sid + 1) : (alloc_save_t *) 0);
  295. alloc_save_t *lsave = alloc_save_space(lmem, dmem, sid);
  296. if (lsave == 0 || (global &&gsave == 0)) {
  297. if (lsave != 0)
  298. alloc_free_save(lmem, lsave, "alloc_save_state(local save)",
  299. "alloc_save_state(local inner)");
  300. if (gsave != 0)
  301. alloc_free_save(gmem, gsave, "alloc_save_state(global save)",
  302. "alloc_save_state(global inner)");
  303. return 0;
  304. }
  305. if (gsave != 0) {
  306. gsave->client_data = 0;
  307. print_save("save", gmem->space, gsave);
  308. /* Restore names when we do the local restore. */
  309. lsave->restore_names = gsave->restore_names;
  310. gsave->restore_names = false;
  311. }
  312. lsave->id = sid;
  313. lsave->client_data = cdata;
  314. print_save("save", lmem->space, lsave);
  315. /* Reset the l_new attribute in all slots. The only slots that */
  316. /* can have the attribute set are the ones on the changes chain, */
  317. /* and ones in objects allocated since the last save. */
  318. if (lmem->save_level > 1) {
  319. long scanned = save_set_new(&lsave->state, false);
  320. if ((lsave->state.total_scanned += scanned) > max_repeated_scan) {
  321. /* Do a second, invisible save. */
  322. alloc_save_t *rsave;
  323. rsave = alloc_save_space(lmem, dmem, 0L);
  324. if (rsave != 0) {
  325. rsave->client_data = cdata;
  326. rsave->id = lsave->id;
  327. print_save("save", lmem->space, rsave);
  328. lsave->id = 0; /* mark as invisible */
  329. rsave->state.save_level--; /* ditto */
  330. lsave->client_data = 0;
  331. /* Inherit the allocated space count -- */
  332. /* we need this for triggering a GC. */
  333. rsave->state.inherited =
  334. lsave->state.allocated + lsave->state.inherited;
  335. lmem->inherited = rsave->state.inherited;
  336. print_save("save", lmem->space, lsave);
  337. }
  338. }
  339. }
  340. alloc_set_in_save(dmem);
  341. return sid;
  342. }
  343. /* Save the state of one space (global or local). */
  344. private alloc_save_t *
  345. alloc_save_space(gs_ref_memory_t * mem, gs_dual_memory_t * dmem, ulong sid)
  346. {
  347. gs_ref_memory_t save_mem;
  348. alloc_save_t *save;
  349. chunk_t *inner = 0;
  350. if (mem->cc.ctop - mem->cc.cbot > sizeof(chunk_head_t)) {
  351. inner = gs_raw_alloc_struct_immovable(mem->parent, &st_chunk,
  352. "alloc_save_space(inner)");
  353. if (inner == 0)
  354. return 0;
  355. }
  356. save_mem = *mem;
  357. alloc_close_chunk(mem);
  358. gs_memory_status((gs_memory_t *) mem, &mem->previous_status);
  359. ialloc_reset(mem);
  360. mem->cc.cnext = mem->cc.cprev = 0;
  361. if (inner != 0) { /* Create an inner chunk to cover only the unallocated part. */
  362. alloc_init_chunk(&mem->cc, mem->cc.cbot, mem->cc.ctop,
  363. true, mem->pcc);
  364. *inner = mem->cc;
  365. mem->pcc = inner;
  366. mem->cfirst = mem->clast = inner;
  367. } else { /* Not enough room to create an inner chunk. */
  368. mem->pcc = 0;
  369. mem->cfirst = mem->clast = 0;
  370. mem->cc.cbot = mem->cc.ctop = 0;
  371. }
  372. save = gs_alloc_struct((gs_memory_t *) mem, alloc_save_t,
  373. &st_alloc_save, "alloc_save_space(save)");
  374. #ifdef DEBUG
  375. if (inner != 0) {
  376. if_debug4('u',
  377. "[u]save space %u at 0x%lx: cbot=0x%lx ctop=0x%lx\n",
  378. mem->space, (ulong) save,
  379. (ulong) inner->cbot, (ulong) inner->ctop);
  380. } else {
  381. if_debug2('u',
  382. "[u]save space %u at 0x%lx (no inner)\n",
  383. mem->space, (ulong) save);
  384. }
  385. #endif
  386. if (save == 0) {
  387. gs_free_object(mem->parent, inner, "alloc_save_space(inner)");
  388. *mem = save_mem;
  389. return 0;
  390. }
  391. save->state = save_mem;
  392. save->spaces = dmem->spaces;
  393. save->restore_names = (name_memory() == (gs_memory_t *) mem);
  394. save->is_current = (dmem->current == mem);
  395. save->id = sid;
  396. mem->saved = save;
  397. if_debug2('u', "[u%u]file_save 0x%lx\n",
  398. mem->space, (ulong) mem->streams);
  399. mem->streams = 0;
  400. mem->total_scanned = 0;
  401. if (sid)
  402. mem->save_level++;
  403. return save;
  404. }
  405. /* Record a state change that must be undone for restore, */
  406. /* and mark it as having been saved. */
  407. int
  408. alloc_save_change_in(gs_ref_memory_t *mem, const ref * pcont,
  409. ref_packed * where, client_name_t cname)
  410. {
  411. register alloc_change_t *cp;
  412. if (mem->new_mask == 0)
  413. return 0; /* no saving */
  414. cp = gs_alloc_struct((gs_memory_t *)mem, alloc_change_t,
  415. &st_alloc_change, "alloc_save_change");
  416. if (cp == 0)
  417. return -1;
  418. cp->next = mem->changes;
  419. cp->where = where;
  420. if (pcont == NULL)
  421. cp->offset = AC_OFFSET_STATIC;
  422. else if (r_is_array(pcont) || r_has_type(pcont, t_dictionary))
  423. cp->offset = AC_OFFSET_REF;
  424. else if (r_is_struct(pcont))
  425. cp->offset = (byte *) where - (byte *) pcont->value.pstruct;
  426. else {
  427. lprintf3("Bad type %u for save! pcont = 0x%lx, where = 0x%lx\n",
  428. r_type(pcont), (ulong) pcont, (ulong) where);
  429. gs_abort();
  430. }
  431. if (r_is_packed(where))
  432. *(ref_packed *)&cp->contents = *where;
  433. else {
  434. ref_assign_inline(&cp->contents, (ref *) where);
  435. r_set_attrs((ref *) where, l_new);
  436. }
  437. mem->changes = cp;
  438. #ifdef DEBUG
  439. if (gs_debug_c('U')) {
  440. dlprintf1("[U]save(%s)", client_name_string(cname));
  441. alloc_save_print(cp, false);
  442. }
  443. #endif
  444. return 0;
  445. }
  446. int
  447. alloc_save_change(gs_dual_memory_t * dmem, const ref * pcont,
  448. ref_packed * where, client_name_t cname)
  449. {
  450. gs_ref_memory_t *mem =
  451. (pcont == NULL ? dmem->space_local :
  452. dmem->spaces_indexed[r_space(pcont) >> r_space_shift]);
  453. return alloc_save_change_in(mem, pcont, where, cname);
  454. }
  455. /* Return (the id of) the innermost externally visible save object, */
  456. /* i.e., the innermost save with a non-zero ID. */
  457. ulong
  458. alloc_save_current_id(const gs_dual_memory_t * dmem)
  459. {
  460. const alloc_save_t *save = dmem->space_local->saved;
  461. while (save != 0 && save->id == 0)
  462. save = save->state.saved;
  463. return save->id;
  464. }
  465. alloc_save_t *
  466. alloc_save_current(const gs_dual_memory_t * dmem)
  467. {
  468. return alloc_find_save(dmem, alloc_save_current_id(dmem));
  469. }
  470. /* Test whether a reference would be invalidated by a restore. */
  471. bool
  472. alloc_is_since_save(const void *vptr, const alloc_save_t * save)
  473. {
  474. /* A reference postdates a save iff it is in a chunk allocated */
  475. /* since the save (including the carried-over inner chunk). */
  476. const char *const ptr = (const char *)vptr;
  477. register const gs_ref_memory_t *mem = save->space_local;
  478. if_debug2('U', "[U]is_since_save 0x%lx, 0x%lx:\n",
  479. (ulong) ptr, (ulong) save);
  480. if (mem->saved == 0) { /* This is a special case, the final 'restore' from */
  481. /* alloc_restore_all. */
  482. return true;
  483. }
  484. /* Check against chunks allocated since the save. */
  485. /* (There may have been intermediate saves as well.) */
  486. for (;; mem = &mem->saved->state) {
  487. const chunk_t *cp;
  488. if_debug1('U', "[U]checking mem=0x%lx\n", (ulong) mem);
  489. for (cp = mem->cfirst; cp != 0; cp = cp->cnext) {
  490. if (ptr_is_within_chunk(ptr, cp)) {
  491. if_debug3('U', "[U+]in new chunk 0x%lx: 0x%lx, 0x%lx\n",
  492. (ulong) cp,
  493. (ulong) cp->cbase, (ulong) cp->cend);
  494. return true;
  495. }
  496. if_debug1('U', "[U-]not in 0x%lx\n", (ulong) cp);
  497. }
  498. if (mem->saved == save) { /* We've checked all the more recent saves, */
  499. /* must be OK. */
  500. break;
  501. }
  502. }
  503. /*
  504. * If we're about to do a global restore (save level = 1),
  505. * and there is only one context using this global VM
  506. * (the normal case, in which global VM is saved by the
  507. * outermost save), we also have to check the global save.
  508. * Global saves can't be nested, which makes things easy.
  509. */
  510. if (mem->save_level == 1 &&
  511. (mem = save->space_global) != save->space_local &&
  512. save->space_global->num_contexts == 1
  513. ) {
  514. const chunk_t *cp;
  515. if_debug1('U', "[U]checking global mem=0x%lx\n", (ulong) mem);
  516. for (cp = mem->cfirst; cp != 0; cp = cp->cnext)
  517. if (ptr_is_within_chunk(ptr, cp)) {
  518. if_debug3('U', "[U+] new chunk 0x%lx: 0x%lx, 0x%lx\n",
  519. (ulong) cp, (ulong) cp->cbase, (ulong) cp->cend);
  520. return true;
  521. }
  522. }
  523. return false;
  524. #undef ptr
  525. }
  526. /* Test whether a name would be invalidated by a restore. */
  527. bool
  528. alloc_name_is_since_save(const ref * pnref, const alloc_save_t * save)
  529. {
  530. const name_string_t *pnstr;
  531. if (!save->restore_names)
  532. return false;
  533. pnstr = names_string_inline(the_gs_name_table, pnref);
  534. if (pnstr->foreign_string)
  535. return false;
  536. return alloc_is_since_save(pnstr->string_bytes, save);
  537. }
  538. bool
  539. alloc_name_index_is_since_save(uint nidx, const alloc_save_t * save)
  540. {
  541. ref nref;
  542. nref.value.pname = name_index_ptr(nidx);
  543. return alloc_name_is_since_save(&nref, save);
  544. }
  545. /* Check whether any names have been created since a given save */
  546. /* that might be released by the restore. */
  547. bool
  548. alloc_any_names_since_save(const alloc_save_t * save)
  549. {
  550. return save->restore_names;
  551. }
  552. /* Get the saved state with a given ID. */
  553. alloc_save_t *
  554. alloc_find_save(const gs_dual_memory_t * dmem, ulong sid)
  555. {
  556. alloc_save_t *sprev = dmem->space_local->saved;
  557. if (sid == 0)
  558. return 0; /* invalid id */
  559. while (sprev != 0) {
  560. if (sprev->id == sid)
  561. return sprev;
  562. sprev = sprev->state.saved;
  563. }
  564. return 0;
  565. }
  566. /* Get the client data from a saved state. */
  567. void *
  568. alloc_save_client_data(const alloc_save_t * save)
  569. {
  570. return save->client_data;
  571. }
  572. /*
  573. * Do one step of restoring the state. The client is responsible for
  574. * calling alloc_find_save to get the save object, and for ensuring that
  575. * there are no surviving pointers for which alloc_is_since_save is true.
  576. * Return true if the argument was the innermost save, in which case
  577. * this is the last (or only) step.
  578. * Note that "one step" may involve multiple internal steps,
  579. * if this is the outermost restore (which requires restoring both local
  580. * and global VM) or if we created extra save levels to reduce scanning.
  581. */
  582. private void restore_finalize(P1(gs_ref_memory_t *));
  583. private void restore_space(P2(gs_ref_memory_t *, gs_dual_memory_t *));
  584. bool
  585. alloc_restore_step_in(gs_dual_memory_t *dmem, alloc_save_t * save)
  586. {
  587. /* Get save->space_* now, because the save object will be freed. */
  588. gs_ref_memory_t *lmem = save->space_local;
  589. gs_ref_memory_t *gmem = save->space_global;
  590. gs_ref_memory_t *mem = lmem;
  591. alloc_save_t *sprev;
  592. /* Finalize all objects before releasing resources or undoing changes. */
  593. do {
  594. ulong sid;
  595. sprev = mem->saved;
  596. sid = sprev->id;
  597. restore_finalize(mem); /* finalize objects */
  598. mem = &sprev->state;
  599. if (sid != 0)
  600. break;
  601. }
  602. while (sprev != save);
  603. if (mem->save_level == 0) {
  604. /* This is the outermost save, which might also */
  605. /* need to restore global VM. */
  606. mem = gmem;
  607. if (mem != lmem && mem->saved != 0)
  608. restore_finalize(mem);
  609. }
  610. /* Do one (externally visible) step of restoring the state. */
  611. mem = lmem;
  612. do {
  613. ulong sid;
  614. sprev = mem->saved;
  615. sid = sprev->id;
  616. restore_resources(sprev, mem); /* release other resources */
  617. restore_space(mem, dmem); /* release memory */
  618. if (sid != 0)
  619. break;
  620. }
  621. while (sprev != save);
  622. if (mem->save_level == 0) {
  623. /* This is the outermost save, which might also */
  624. /* need to restore global VM. */
  625. mem = gmem;
  626. if (mem != lmem && mem->saved != 0) {
  627. restore_resources(mem->saved, mem);
  628. restore_space(mem, dmem);
  629. }
  630. alloc_set_not_in_save(dmem);
  631. } else { /* Set the l_new attribute in all slots that are now new. */
  632. save_set_new(mem, true);
  633. }
  634. return sprev == save;
  635. }
  636. /* Restore the memory of one space, by undoing changes and freeing */
  637. /* memory allocated since the save. */
  638. private void
  639. restore_space(gs_ref_memory_t * mem, gs_dual_memory_t *dmem)
  640. {
  641. alloc_save_t *save = mem->saved;
  642. alloc_save_t saved;
  643. print_save("restore", mem->space, save);
  644. /* Undo changes since the save. */
  645. {
  646. register alloc_change_t *cp = mem->changes;
  647. while (cp) {
  648. #ifdef DEBUG
  649. if (gs_debug_c('U')) {
  650. dlputs("[U]restore");
  651. alloc_save_print(cp, true);
  652. }
  653. #endif
  654. if (r_is_packed(&cp->contents))
  655. *cp->where = *(ref_packed *) & cp->contents;
  656. else
  657. ref_assign_inline((ref *) cp->where, &cp->contents);
  658. cp = cp->next;
  659. }
  660. }
  661. /* Free memory allocated since the save. */
  662. /* Note that this frees all chunks except the inner one */
  663. /* belonging to this level. */
  664. saved = *save;
  665. restore_free(mem);
  666. /* Restore the allocator state. */
  667. {
  668. int num_contexts = mem->num_contexts; /* don't restore */
  669. *mem = saved.state;
  670. mem->num_contexts = num_contexts;
  671. }
  672. alloc_open_chunk(mem);
  673. /* Make the allocator current if it was current before the save. */
  674. if (saved.is_current) {
  675. dmem->current = mem;
  676. dmem->current_space = mem->space;
  677. }
  678. }
  679. /* Restore to the initial state, releasing all resources. */
  680. /* The allocator is no longer usable after calling this routine! */
  681. void
  682. alloc_restore_all(gs_dual_memory_t * dmem)
  683. {
  684. /*
  685. * Save the memory pointers, since freeing space_local will also
  686. * free dmem itself.
  687. */
  688. gs_ref_memory_t *lmem = dmem->space_local;
  689. gs_ref_memory_t *gmem = dmem->space_global;
  690. gs_ref_memory_t *smem = dmem->space_system;
  691. gs_ref_memory_t *mem;
  692. /* Restore to a state outside any saves. */
  693. while (lmem->save_level != 0)
  694. discard(alloc_restore_step_in(dmem, lmem->saved));
  695. /* Finalize memory. */
  696. restore_finalize(lmem);
  697. if ((mem = (gs_ref_memory_t *)lmem->stable_memory) != lmem)
  698. restore_finalize(mem);
  699. if (gmem != lmem && gmem->num_contexts == 1) {
  700. restore_finalize(gmem);
  701. if ((mem = (gs_ref_memory_t *)gmem->stable_memory) != gmem)
  702. restore_finalize(mem);
  703. }
  704. restore_finalize(smem);
  705. /* Release resources other than memory, using fake */
  706. /* save and memory objects. */
  707. {
  708. alloc_save_t empty_save;
  709. empty_save.spaces = dmem->spaces;
  710. empty_save.restore_names = false; /* don't bother to release */
  711. restore_resources(&empty_save, NULL);
  712. }
  713. /* Finally, release memory. */
  714. restore_free(lmem);
  715. if ((mem = (gs_ref_memory_t *)lmem->stable_memory) != lmem)
  716. restore_free(mem);
  717. if (gmem != lmem) {
  718. if (!--(gmem->num_contexts)) {
  719. restore_free(gmem);
  720. if ((mem = (gs_ref_memory_t *)gmem->stable_memory) != gmem)
  721. restore_free(mem);
  722. }
  723. }
  724. restore_free(smem);
  725. }
  726. /*
  727. * Finalize objects that will be freed by a restore.
  728. * Note that we must temporarily disable the freeing operations
  729. * of the allocator while doing this.
  730. */
  731. private void
  732. restore_finalize(gs_ref_memory_t * mem)
  733. {
  734. chunk_t *cp;
  735. alloc_close_chunk(mem);
  736. gs_enable_free((gs_memory_t *) mem, false);
  737. for (cp = mem->clast; cp != 0; cp = cp->cprev) {
  738. SCAN_CHUNK_OBJECTS(cp)
  739. DO_ALL
  740. struct_proc_finalize((*finalize)) =
  741. pre->o_type->finalize;
  742. if (finalize != 0) {
  743. if_debug2('u', "[u]restore finalizing %s 0x%lx\n",
  744. struct_type_name_string(pre->o_type),
  745. (ulong) (pre + 1));
  746. (*finalize) (pre + 1);
  747. }
  748. END_OBJECTS_SCAN
  749. }
  750. gs_enable_free((gs_memory_t *) mem, true);
  751. }
  752. /* Release resources for a restore */
  753. private void
  754. restore_resources(alloc_save_t * sprev, gs_ref_memory_t * mem)
  755. {
  756. #ifdef DEBUG
  757. if (mem) {
  758. /* Note restoring of the file list. */
  759. if_debug4('u', "[u%u]file_restore 0x%lx => 0x%lx for 0x%lx\n",
  760. mem->space, (ulong)mem->streams,
  761. (ulong)sprev->state.streams, (ulong) sprev);
  762. }
  763. #endif
  764. /* Remove entries from font and character caches. */
  765. font_restore(sprev);
  766. /* Adjust the name table. */
  767. if (sprev->restore_names)
  768. names_restore(the_gs_name_table, sprev);
  769. }
  770. /* Release memory for a restore. */
  771. private void
  772. restore_free(gs_ref_memory_t * mem)
  773. {
  774. /* Free chunks allocated since the save. */
  775. gs_free_all((gs_memory_t *) mem);
  776. }
  777. /* Forget a save, by merging this level with the next outer one. */
  778. private void file_forget_save(P1(gs_ref_memory_t *));
  779. private void combine_space(P1(gs_ref_memory_t *));
  780. private void forget_changes(P1(gs_ref_memory_t *));
  781. void
  782. alloc_forget_save_in(gs_dual_memory_t *dmem, alloc_save_t * save)
  783. {
  784. gs_ref_memory_t *mem = save->space_local;
  785. alloc_save_t *sprev;
  786. print_save("forget_save", mem->space, save);
  787. /* Iteratively combine the current level with the previous one. */
  788. do {
  789. sprev = mem->saved;
  790. if (sprev->id != 0)
  791. mem->save_level--;
  792. if (mem->save_level != 0) {
  793. alloc_change_t *chp = mem->changes;
  794. save_set_new(&sprev->state, true);
  795. /* Concatenate the changes chains. */
  796. if (chp == 0)
  797. mem->changes = sprev->state.changes;
  798. else {
  799. while (chp->next != 0)
  800. chp = chp->next;
  801. chp->next = sprev->state.changes;
  802. }
  803. file_forget_save(mem);
  804. combine_space(mem); /* combine memory */
  805. } else {
  806. forget_changes(mem);
  807. save_set_new(mem, false);
  808. file_forget_save(mem);
  809. combine_space(mem); /* combine memory */
  810. /* This is the outermost save, which might also */
  811. /* need to combine global VM. */
  812. mem = save->space_global;
  813. if (mem != save->space_local && mem->saved != 0) {
  814. forget_changes(mem);
  815. save_set_new(mem, false);
  816. file_forget_save(mem);
  817. combine_space(mem);
  818. }
  819. alloc_set_not_in_save(dmem);
  820. break; /* must be outermost */
  821. }
  822. }
  823. while (sprev != save);
  824. }
  825. /* Combine the chunks of the next outer level with those of the current one, */
  826. /* and free the bookkeeping structures. */
  827. private void
  828. combine_space(gs_ref_memory_t * mem)
  829. {
  830. alloc_save_t *saved = mem->saved;
  831. gs_ref_memory_t *omem = &saved->state;
  832. chunk_t *cp;
  833. chunk_t *csucc;
  834. alloc_close_chunk(mem);
  835. for (cp = mem->cfirst; cp != 0; cp = csucc) {
  836. csucc = cp->cnext; /* save before relinking */
  837. if (cp->outer == 0)
  838. alloc_link_chunk(cp, omem);
  839. else {
  840. chunk_t *outer = cp->outer;
  841. outer->inner_count--;
  842. if (mem->pcc == cp)
  843. mem->pcc = outer;
  844. if (mem->cfreed.cp == cp)
  845. mem->cfreed.cp = outer;
  846. /* "Free" the header of the inner chunk, */
  847. /* and any immediately preceding gap left by */
  848. /* the GC having compacted the outer chunk. */
  849. {
  850. obj_header_t *hp = (obj_header_t *) outer->cbot;
  851. hp->o_alone = 0;
  852. hp->o_size = (char *)(cp->chead + 1)
  853. - (char *)(hp + 1);
  854. hp->o_type = &st_bytes;
  855. /* The following call is probably not safe. */
  856. #if 0 /* **************** */
  857. gs_free_object((gs_memory_t *) mem,
  858. hp + 1, "combine_space(header)");
  859. #endif /* **************** */
  860. }
  861. /* Update the outer chunk's allocation pointers. */
  862. outer->cbot = cp->cbot;
  863. outer->rcur = cp->rcur;
  864. outer->rtop = cp->rtop;
  865. outer->ctop = cp->ctop;
  866. outer->has_refs |= cp->has_refs;
  867. gs_free_object(mem->parent, cp,
  868. "combine_space(inner)");
  869. }
  870. }
  871. /* Update relevant parts of allocator state. */
  872. mem->cfirst = omem->cfirst;
  873. mem->clast = omem->clast;
  874. mem->allocated += omem->allocated;
  875. mem->gc_allocated += omem->allocated;
  876. mem->lost.objects += omem->lost.objects;
  877. mem->lost.refs += omem->lost.refs;
  878. mem->lost.strings += omem->lost.strings;
  879. mem->saved = omem->saved;
  880. mem->previous_status = omem->previous_status;
  881. { /* Concatenate free lists. */
  882. int i;
  883. for (i = 0; i < num_freelists; i++) {
  884. obj_header_t *olist = omem->freelists[i];
  885. obj_header_t *list = mem->freelists[i];
  886. if (olist == 0);
  887. else if (list == 0)
  888. mem->freelists[i] = olist;
  889. else {
  890. while (*(obj_header_t **) list != 0)
  891. list = *(obj_header_t **) list;
  892. *(obj_header_t **) list = olist;
  893. }
  894. }
  895. if (omem->largest_free_size > mem->largest_free_size)
  896. mem->largest_free_size = omem->largest_free_size;
  897. }
  898. gs_free_object((gs_memory_t *) mem, saved, "combine_space(saved)");
  899. alloc_open_chunk(mem);
  900. }
  901. /* Free the changes chain for a level 0 .forgetsave, */
  902. /* resetting the l_new flag in the changed refs. */
  903. private void
  904. forget_changes(gs_ref_memory_t * mem)
  905. {
  906. register alloc_change_t *chp = mem->changes;
  907. alloc_change_t *next;
  908. for (; chp; chp = next) {
  909. ref_packed *prp = chp->where;
  910. if_debug1('U', "[U]forgetting change 0x%lx\n", (ulong) chp);
  911. if (!r_is_packed(prp))
  912. r_clear_attrs((ref *) prp, l_new);
  913. next = chp->next;
  914. gs_free_object((gs_memory_t *) mem, chp, "forget_changes");
  915. }
  916. mem->changes = 0;
  917. }
  918. /* Update the streams list when forgetting a save. */
  919. private void
  920. file_forget_save(gs_ref_memory_t * mem)
  921. {
  922. const alloc_save_t *save = mem->saved;
  923. stream *streams = mem->streams;
  924. stream *saved_streams = save->state.streams;
  925. if_debug4('u', "[u%d]file_forget_save 0x%lx + 0x%lx for 0x%lx\n",
  926. mem->space, (ulong) streams, (ulong) saved_streams,
  927. (ulong) save);
  928. if (streams == 0)
  929. mem->streams = saved_streams;
  930. else if (saved_streams != 0) {
  931. while (streams->next != 0)
  932. streams = streams->next;
  933. streams->next = saved_streams;
  934. saved_streams->prev = streams;
  935. }
  936. }
  937. /* ------ Internal routines ------ */
  938. /* Set or reset the l_new attribute in every relevant slot. */
  939. /* This includes every slot on the current change chain, */
  940. /* and every (ref) slot allocated at this save level. */
  941. /* Return the number of bytes of data scanned. */
  942. private long
  943. save_set_new(gs_ref_memory_t * mem, bool to_new)
  944. {
  945. long scanned = 0;
  946. /* Handle the change chain. */
  947. save_set_new_changes(mem, to_new);
  948. /* Handle newly allocated ref objects. */
  949. SCAN_MEM_CHUNKS(mem, cp) {
  950. if (cp->has_refs) {
  951. bool has_refs = false;
  952. SCAN_CHUNK_OBJECTS(cp)
  953. DO_ALL
  954. if_debug3('U', "[U]set_new scan(0x%lx(%u), %d)\n",
  955. (ulong) pre, size, to_new);
  956. if (pre->o_type == &st_refs) {
  957. /* These are refs, scan them. */
  958. ref_packed *prp = (ref_packed *) (pre + 1);
  959. ref_packed *next = (ref_packed *) ((char *)prp + size);
  960. #ifdef ALIGNMENT_ALIASING_BUG
  961. ref *rpref;
  962. # define RP_REF(rp) (rpref = (ref *)rp, rpref)
  963. #else
  964. # define RP_REF(rp) ((ref *)rp)
  965. #endif
  966. if_debug2('U', "[U]refs 0x%lx to 0x%lx\n",
  967. (ulong) prp, (ulong) next);
  968. has_refs = true;
  969. scanned += size;
  970. /* We know that every block of refs ends with */
  971. /* a full-size ref, so we only need the end check */
  972. /* when we encounter one of those. */
  973. if (to_new)
  974. while (1) {
  975. if (r_is_packed(prp))
  976. prp++;
  977. else {
  978. RP_REF(prp)->tas.type_attrs |= l_new;
  979. prp += packed_per_ref;
  980. if (prp >= next)
  981. break;
  982. }
  983. } else
  984. while (1) {
  985. if (r_is_packed(prp))
  986. prp++;
  987. else {
  988. RP_REF(prp)->tas.type_attrs &= ~l_new;
  989. prp += packed_per_ref;
  990. if (prp >= next)
  991. break;
  992. }
  993. }
  994. #undef RP_REF
  995. } else
  996. scanned += sizeof(obj_header_t);
  997. END_OBJECTS_SCAN
  998. cp->has_refs = has_refs;
  999. }
  1000. }
  1001. END_CHUNKS_SCAN
  1002. if_debug2('u', "[u]set_new (%s) scanned %ld\n",
  1003. (to_new ? "restore" : "save"), scanned);
  1004. return scanned;
  1005. }
  1006. /* Set or reset the l_new attribute on the changes chain. */
  1007. private void
  1008. save_set_new_changes(gs_ref_memory_t * mem, bool to_new)
  1009. {
  1010. register alloc_change_t *chp = mem->changes;
  1011. register uint new = (to_new ? l_new : 0);
  1012. for (; chp; chp = chp->next) {
  1013. ref_packed *prp = chp->where;
  1014. if_debug3('U', "[U]set_new 0x%lx: (0x%lx, %d)\n",
  1015. (ulong)chp, (ulong)prp, new);
  1016. if (!r_is_packed(prp)) {
  1017. ref *const rp = (ref *) prp;
  1018. rp->tas.type_attrs =
  1019. (rp->tas.type_attrs & ~l_new) + new;
  1020. }
  1021. }
  1022. }