isave.c 34 KB

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