home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / ISAVE.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  22KB  |  682 lines

  1. /* Copyright (C) 1993, 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* isave.c */
  20. /* Save/restore manager for Ghostscript interpreter */
  21. #include "ghost.h"
  22. #include "memory_.h"
  23. #include "errors.h"
  24. #include "gsstruct.h"
  25.  
  26. #include "iastate.h"
  27. #include "iname.h"
  28. #include "ipacked.h"
  29. #include "isave.h"
  30. #include "isstate.h"
  31. #include "store.h"            /* for ref_assign */
  32. #include "ivmspace.h"
  33. #include "gsutil.h"            /* gs_next_ids prototype */
  34.  
  35. /* Imported restore routines */
  36. extern void file_save(P0());
  37. extern void file_restore(P1(alloc_save_t *));
  38. extern void font_restore(P1(alloc_save_t *));
  39.  
  40. /* Structure descriptor */
  41. private_st_alloc_save();
  42.  
  43. /* Define the maximum amount of data we are willing to scan repeatedly -- */
  44. /* see below for details. */
  45. private const long max_repeated_scan = 5000;
  46.  
  47. /*
  48.  * The logic for saving and restoring the state is complex.
  49.  * Both the changes to individual objects, and the overall state
  50.  * of the memory manager, must be saved and restored.
  51.  */
  52.  
  53. /*
  54.  * To save the state of the memory manager:
  55.  *    Save the state of the current chunk in which we are allocating.
  56.  *    Shrink the current chunk to its inner unallocated region.
  57.  *    Save and reset the free block chains.
  58.  * By doing this, we guarantee that no object older than the save
  59.  * can be freed.
  60.  *
  61.  * To restore the state of the memory manager:
  62.  *    Free all chunks newer than the save.
  63.  *    Make current the chunk that was current at the time of the save.
  64.  *    Restore the state of the current chunk.
  65.  */
  66.  
  67. /*
  68.  * For saving changes to individual objects, we add an "attribute" bit
  69.  * (l_new) that logically belongs to the slot where the ref is stored,
  70.  * not to the ref itself.  The bit means "the contents of this slot
  71.  * have been changed, or the slot was allocated, since the last save."
  72.  * To keep track of changes since the save, we associate a chain of
  73.  * <slot, old_contents> pairs that remembers the old contents of slots.
  74.  *
  75.  * When creating an object, if the save level is non-zero:
  76.  *    Set the bit in all slots.
  77.  *
  78.  * When storing into a slot, if the save level is non-zero:
  79.  *    If the bit isn't set, save the address and contents of the slot
  80.  *      on the current contents chain.
  81.  *    Set the bit after storing the new value.
  82.  *
  83.  * To do a save:
  84.  *    If the save level is non-zero:
  85.  *        Reset the bit in all slots on the contents chain, and in all
  86.  *          objects created since the previous save.
  87.  *    Push the head of contents chain, and reset the chain to empty.
  88.  *
  89.  * To do a restore:
  90.  *    Check all the stacks to make sure they don't contain references
  91.  *      to objects created since the save.
  92.  *    Restore all the slots on the contents chain.
  93.  *    Pop the contents chain head.
  94.  *    If the save level is now non-zero:
  95.  *        Scan the newly restored contents chain, and set the bit in all
  96.  *          the slots it references.
  97.  *        Scan all objects created since the previous save, and set the
  98.  *          bit in all the slots of each object.
  99.  */
  100.  
  101. /*
  102.  * A consequence of the foregoing algorithms is that the cost of a save
  103.  * is proportional to the total amount of data allocated since the previous
  104.  * save.  If a PostScript program reads in a large amount of setup code
  105.  * and then uses save/restore heavily, each save/restore will be expensive.
  106.  * To mitigate this, we check to see how much data we are scanning at a save.
  107.  * If it is large, we do a second, invisible save.  This greatly reduces
  108.  * the cost of inner saves, at the expense of possibly saving some changes
  109.  * twice that otherwise would only have to be saved once.
  110.  */
  111.  
  112. /*
  113.  * The presence of global and local VM complicates the situation further.
  114.  * There is a separate save chain and contents chain for each VM space.
  115.  * When multiple contexts are fully implemented, save and restore will have
  116.  * the following effects, according to the privacy status of the current
  117.  * context's global and local VM:
  118.  *    Private global, private local:
  119.  *        The outermost save saves both global and local VM;
  120.  *          otherwise, save only saves local VM.
  121.  *    Shared global, private local:
  122.  *        Save only saves local VM.
  123.  *    Shared global, shared local:
  124.  *        Save only saves local VM, and suspends all other contexts
  125.  *          sharing the same local VM until the matching restore.
  126.  * Since we do not currently implement multiple contexts, only the first
  127.  * case is relevant.
  128.  *
  129.  * Note that when saving the contents of a slot, the choice of chain
  130.  * is determined by the VM space in which the slot is allocated,
  131.  * not by the current allocation mode.
  132.  */
  133.  
  134. #define set_in_save(dmem)\
  135.   ((dmem)->test_mask = (dmem)->new_mask = l_new)
  136. #define set_not_in_save(dmem)\
  137.   ((dmem)->test_mask = ~0, (dmem)->new_mask = 0)
  138.  
  139. /* Structure for saved change chain for save/restore. */
  140. typedef struct alloc_change_s alloc_change_t;
  141. struct alloc_change_s {
  142.     alloc_change_t *next;
  143.     ref_packed *where;
  144.     ref contents;
  145.     bool is_static;
  146. };
  147. #define ptr ((alloc_change_t *)vptr)
  148. private CLEAR_MARKS_PROC(change_clear_marks) {
  149.     if ( r_is_packed(&ptr->contents) )
  150.       r_clear_pmark((ref_packed *)&ptr->contents);
  151.     else
  152.       r_clear_attrs(&ptr->contents, l_mark);
  153. }
  154. private ENUM_PTRS_BEGIN(change_enum_ptrs) return 0;
  155.     ENUM_PTR(0, alloc_change_t, next);
  156.     case 1:
  157.         *pep = ptr->where;
  158.         return ptr_ref_type;
  159.     case 2:
  160.         *pep = &ptr->contents;
  161.         return ptr_ref_type;
  162. ENUM_PTRS_END
  163. private RELOC_PTRS_BEGIN(change_reloc_ptrs) {
  164.     RELOC_PTR(alloc_change_t, next);
  165.     if ( !ptr->is_static )
  166.       ptr->where = gs_reloc_ref_ptr(ptr->where, gcst);
  167.     if ( r_is_packed(&ptr->contents) )
  168.       r_clear_pmark((ref_packed *)&ptr->contents);
  169.     else
  170.       { gs_reloc_refs((ref_packed *)&ptr->contents,
  171.               (ref_packed *)(&ptr->contents + 1),
  172.               gcst);
  173.         r_clear_attrs(&ptr->contents, l_mark);
  174.       }
  175. } RELOC_PTRS_END
  176. #undef ptr
  177. gs_private_st_complex_only(st_alloc_change, alloc_change_t, "alloc_change",
  178.   change_clear_marks, change_enum_ptrs, change_reloc_ptrs, 0);
  179.  
  180. /* Debugging printout */
  181. #ifdef DEBUG
  182. private void
  183. alloc_save_print(alloc_change_t *cp, bool print_current)
  184. {    dprintf2(" 0x%lx: 0x%lx: ", (ulong)cp, (ulong)cp->where);
  185.     if ( r_is_packed(&cp->contents) )
  186.       { if ( print_current )
  187.           dprintf2("saved=%x cur=%x\n", *(ref_packed *)&cp->contents,
  188.                *cp->where);
  189.         else
  190.           dprintf1("%x\n", *(ref_packed *)&cp->contents);
  191.       }
  192.     else
  193.       { if ( print_current )
  194.           dprintf6("saved=%x %x %lx cur=%x %x %lx\n",
  195.                r_type_attrs(&cp->contents), r_size(&cp->contents),
  196.                (ulong)cp->contents.value.intval,
  197.                r_type_attrs((ref *)cp->where),
  198.                r_size((ref *)cp->where),
  199.                (ulong)((ref *)cp->where)->value.intval);
  200.         else
  201.           dprintf3("%x %x %lx\n",
  202.                r_type_attrs(&cp->contents), r_size(&cp->contents),
  203.                (ulong)cp->contents.value.intval);
  204.       }
  205. }
  206. #endif
  207.  
  208. /* Forward references */
  209. private void restore_resources(P1(alloc_save_t *));
  210. private void restore_free(P1(gs_ref_memory_t *));
  211. private long save_set_new(P2(gs_ref_memory_t *, bool));
  212. private void save_set_new_changes(P2(gs_ref_memory_t *, bool));
  213.  
  214. /* Initialize the save/restore machinery. */
  215. void
  216. alloc_save_init(gs_dual_memory_t *dmem)
  217. {    dmem->save_level = 0;
  218.     set_not_in_save(dmem);
  219. }
  220.  
  221. /* Save the state. */
  222. private alloc_save_t *alloc_save_space(P1(gs_ref_memory_t *));
  223. ulong
  224. alloc_save_state(gs_dual_memory_t *dmem, void *cdata)
  225. {    gs_ref_memory_t *lmem = dmem->local;
  226.     gs_ref_memory_t *gmem = dmem->global;
  227.     ulong sid = gs_next_ids(2);
  228. #define alloc_free_save(mem, s, scn, icn)\
  229.   { chunk_t *inner = (mem)->pcc;\
  230.     gs_free_object((gs_memory_t *)(mem), s, scn);\
  231.     gs_free_object((mem)->parent, inner, icn);\
  232.   }
  233.     bool global =
  234.         dmem->save_level == 0 && gmem != lmem &&
  235.         gmem->num_contexts == 1;
  236.     alloc_save_t *gsave =
  237.       (global ? alloc_save_space(gmem) : (alloc_save_t *)0);
  238.     alloc_save_t *lsave = alloc_save_space(lmem);
  239.  
  240.     if ( lsave == 0 || (global && gsave == 0) )
  241.     {    if ( lsave != 0 )
  242.           alloc_free_save(lmem, lsave, "alloc_save_state(local save)",
  243.                   "alloc_save_state(local inner)");
  244.         if ( gsave != 0 )
  245.           alloc_free_save(gmem, gsave, "alloc_save_state(global save)",
  246.                   "alloc_save_state(global inner)");
  247.         return 0;
  248.     }
  249. #undef alloc_free_save
  250.     if ( gsave != 0 )
  251.       { gsave->dmem = dmem;
  252.         gsave->id = sid + 1;
  253.         gsave->client_data = 0;
  254.         /* Do the right thing about names. */
  255.         if ( gsave->name_cnt != max_name_count )
  256.           lsave->name_cnt = gsave->name_cnt,
  257.           gsave->name_cnt = max_name_count;
  258.       }
  259.     lsave->dmem = dmem;
  260.     lsave->id = sid;
  261.     lsave->client_data = cdata;
  262.     /* Reset the l_new attribute in all slots.  The only slots that */
  263.     /* can have the attribute set are the ones on the changes chain, */
  264.     /* and ones in objects allocated since the last save. */
  265.     if ( dmem->save_level != 0 )
  266.       {    long scanned = save_set_new(&lsave->state, false);
  267.         if ( scanned > max_repeated_scan )
  268.           {    /* Do a second, invisible save. */
  269.             alloc_save_t *rsave;
  270.             /* Notify the file machinery of the first save. */
  271.             file_save();
  272.             rsave = alloc_save_space(lmem);
  273.             if ( rsave != 0 )
  274.               {    rsave->dmem = dmem;
  275.                 rsave->id = sid;
  276.                 rsave->client_data = cdata;
  277.                 lsave->id = 0;    /* mark as invisible */
  278.                 lsave->client_data = 0;
  279.               }
  280.           }
  281.       }
  282.     dmem->save_level++;
  283.     set_in_save(dmem);
  284.     /* Notify the file machinery we just did a save. */
  285.     file_save();
  286.     return sid;
  287. }
  288. /* Save the state of one space (global or local). */
  289. private alloc_save_t *
  290. alloc_save_space(gs_ref_memory_t *mem)
  291. {    gs_ref_memory_t save_mem;
  292.     alloc_save_t *save;
  293.     chunk_t *inner = 0;
  294.     if ( mem->cc.ctop - mem->cc.cbot > sizeof(chunk_head_t) )
  295.       {    inner = gs_alloc_struct(mem->parent, chunk_t, &st_chunk,
  296.                     "alloc_save_space(inner)");
  297.         if ( inner == 0 )
  298.           return 0;
  299.       }
  300.     save_mem = *mem;
  301.     alloc_close_chunk(mem);
  302.     gs_memory_status((gs_memory_t *)mem, &mem->previous_status);
  303.     ialloc_reset(mem);
  304.     mem->cc.cnext = mem->cc.cprev = 0;
  305.     if ( inner != 0 )
  306.       {    /* Create an inner chunk to cover only the unallocated part. */
  307.         alloc_init_chunk(&mem->cc, mem->cc.cbot, mem->cc.ctop,
  308.                  true, mem->pcc);
  309.         *inner = mem->cc;
  310.         mem->pcc = inner;
  311.         mem->cfirst = mem->clast = inner;
  312.       }
  313.     else
  314.       {    /* Not enough room to create an inner chunk. */
  315.         mem->pcc = 0;
  316.         mem->cfirst = mem->clast = 0;
  317.         mem->cc.cbot = mem->cc.ctop = 0;
  318.       }
  319.     save = gs_alloc_struct((gs_memory_t *)mem, alloc_save_t,
  320.                    &st_alloc_save, "alloc_save_space(save)");
  321.     if_debug3('u', "[u]save at 0x%lx: cbot=0x%lx ctop=0x%lx\n",
  322.           (ulong)save, (ulong)inner->cbot, (ulong)inner->ctop);
  323.     if ( save == 0 )
  324.       { gs_free_object(mem->parent, inner, "alloc_save_space(inner)");
  325.         *mem = save_mem;
  326.         return 0;
  327.       }
  328.     save->state = save_mem;
  329.     save->name_cnt =
  330.       (name_memory() == (gs_memory_t *)mem ? name_count() :
  331.        max_name_count);
  332.     mem->saved = save;
  333.     return save;
  334. }
  335.  
  336. /* Record a state change that must be undone for restore, */
  337. /* and mark it as having been saved. */
  338. int
  339. alloc_save_change(gs_dual_memory_t *dmem, const ref *pcont,
  340.   ref_packed *where, client_name_t cname)
  341. {    gs_ref_memory_t *mem;
  342.     register alloc_change_t *cp;
  343.     if ( dmem->save_level == 0 )
  344.       return 0;    /* no saving */
  345.     mem = (pcont == NULL || r_is_local(pcont) ?
  346.            dmem->local : dmem->global);
  347.     cp = gs_alloc_struct((gs_memory_t *)mem, alloc_change_t,
  348.                  &st_alloc_change, "alloc_save_change");
  349.     if ( cp == 0 )
  350.       return -1;
  351.     cp->next = mem->changes;
  352.     cp->where = where;
  353.     cp->is_static = pcont == NULL;
  354.     if ( r_is_packed(where) )
  355.       *(ref_packed *)&cp->contents = *where;
  356.     else
  357.       { ref_assign_inline(&cp->contents, (ref *)where);
  358.         r_set_attrs((ref *)where, l_new);
  359.       }
  360.     mem->changes = cp;
  361. #ifdef DEBUG
  362. if ( gs_debug_c('U') )
  363.     {    dprintf1("[u]save(%s)", client_name_string(cname));
  364.         alloc_save_print(cp, false);
  365.     }
  366. #endif
  367.     return 0;
  368. }
  369.  
  370. /* Return the current save level */
  371. int
  372. alloc_save_level(const gs_dual_memory_t *dmem)
  373. {    return dmem->save_level;
  374. }
  375.  
  376. /* Test whether a reference would be invalidated by a restore. */
  377. bool
  378. alloc_is_since_save(const char *ptr, const alloc_save_t *save)
  379. {
  380.     /* A reference postdates a save iff it is in a chunk allocated */
  381.     /* since the save (including the carried-over inner chunk). */
  382.  
  383.     const gs_dual_memory_t *dmem = save->dmem;
  384.     register const gs_ref_memory_t *mem = dmem->local;
  385.  
  386.     if_debug2('U', "[U]is_since_save 0x%lx, 0x%lx:\n",
  387.           (ulong)ptr, (ulong)save);
  388.     if ( mem->saved == 0 )
  389.     {    /* This is a special case, the final 'restore' from */
  390.         /* alloc_restore_all. */
  391.         return true;
  392.     }
  393.  
  394.     /* Check against chunks allocated since the save. */
  395.     /* (There may have been intermediate saves as well.) */
  396.     for ( ; ; mem = &mem->saved->state )
  397.     {    const chunk_t *cp;
  398.         if_debug1('U', "[U]checking mem=0x%lx\n", (ulong)mem);
  399.         for ( cp = mem->cfirst; cp != 0; cp = cp->cnext )
  400.         { if ( ptr_is_within_chunk(ptr, cp) )
  401.             {    if_debug3('U', "[U+]in new chunk 0x%lx: 0x%lx, 0x%lx\n",
  402.                   (ulong)cp,
  403.                   (ulong)cp->cbase, (ulong)cp->cend);
  404.             return true;
  405.             }
  406.           if_debug1('U', "[U-]not in 0x%lx\n", (ulong)cp);
  407.         }
  408.         if ( mem->saved == save )
  409.         {    /* We've checked all the more recent saves, */
  410.             /* must be OK. */
  411.             break;
  412.         }
  413.     }
  414.  
  415.     /* If we're about to do a global restore (save level = 1), */
  416.     /* we also have to check the global save. */
  417.     /* Global saves can't be nested, which makes things easy. */
  418.     if ( dmem->save_level == 1 &&
  419.          (mem = dmem->global) != dmem->local
  420.        )
  421.     {    const chunk_t *cp;
  422.         if_debug1('U', "[U]checking global mem=0x%lx\n", (ulong)mem);
  423.         for ( cp = mem->cfirst; cp != 0; cp = cp->cnext )
  424.           if ( ptr_is_within_chunk(ptr, cp) )
  425.             {    if_debug3('U', "[U+]  new chunk 0x%lx: 0x%lx, 0x%lx\n",
  426.                   (ulong)cp, (ulong)cp->cbase, (ulong)cp->cend);
  427.             return true;
  428.             }
  429.     }
  430.  
  431.     return false;
  432. }
  433.  
  434. /* Test whether a name would be invalidated by a restore. */
  435. bool
  436. alloc_name_is_since_save(const ref *pnref, const alloc_save_t *save)
  437. {    return name_is_since_count(pnref, save->name_cnt);
  438. }
  439. bool
  440. alloc_name_index_is_since_save(uint nidx, const alloc_save_t *save)
  441. {    return name_index_is_since_count(nidx, save->name_cnt);
  442. }
  443.  
  444. /* Get the saved state with a given ID. */
  445. alloc_save_t *
  446. alloc_find_save(const gs_dual_memory_t *dmem, ulong sid)
  447. {    alloc_save_t *sprev = dmem->local->saved;
  448.     if ( sid == 0 )
  449.       return 0;        /* invalid id */
  450.     while ( sprev != 0 )
  451.     {    if ( sprev->id == sid )
  452.             return sprev;
  453.         sprev = sprev->state.saved;
  454.     }
  455.     return 0;
  456. }
  457.  
  458. /* Get the client data from a saved state. */
  459. void *
  460. alloc_save_client_data(const alloc_save_t *save)
  461. {    return save->client_data;
  462. }
  463.  
  464. /* Restore the state.  The client is responsible for calling */
  465. /* alloc_find_save to get the save object, and for ensuring that */
  466. /* there are no surviving pointers for which alloc_is_since_save is true. */
  467. private void restore_space(P1(gs_ref_memory_t *));
  468. void
  469. alloc_restore_state(alloc_save_t *save)
  470. {    gs_dual_memory_t *dmem = save->dmem;
  471.     gs_ref_memory_t *mem = dmem->local;
  472.     alloc_save_t *sprev;
  473.  
  474.     /* Iteratively restore the state. */
  475.     do
  476.     {    ulong sid;
  477.         sprev = mem->saved;
  478.         sid = sprev->id;
  479.         restore_resources(sprev);    /* release other resources */
  480.         restore_space(mem);    /* release memory */
  481.         if ( sid != 0 )
  482.           dmem->save_level--;
  483.     }
  484.     while ( sprev != save );
  485.  
  486.     if ( dmem->save_level == 0 )
  487.     {    /* This is the outermost save, which might also */
  488.         /* need to restore global VM. */
  489.         mem = dmem->global;
  490.         if ( mem != dmem->local && mem->saved != 0 )
  491.             restore_space(mem);
  492.         set_not_in_save(dmem);
  493.     }
  494.     else
  495.     {    /* Set the l_new attribute in all slots that are now new. */
  496.         save_set_new(mem, true);
  497.     }
  498. }
  499. /* Restore the memory of one space, by undoing changes and freeing */
  500. /* memory allocated since the save. */
  501. private void
  502. restore_space(gs_ref_memory_t *mem)
  503. {    alloc_save_t *save = mem->saved;
  504.     gs_ref_memory_t saved_mem;
  505.  
  506.     if_debug2('u', "[u]restore from 0x%lx, id = %lu\n",
  507.           (ulong)save, (ulong)save->id);
  508.  
  509.     /* Undo changes since the save. */
  510.     {    register alloc_change_t *cp = mem->changes;
  511.         while ( cp )
  512.           {
  513. #ifdef DEBUG
  514. if ( gs_debug_c('U') )
  515.             {    dprintf("[U]restore");
  516.             alloc_save_print(cp, true);
  517.             }
  518. #endif
  519.             if ( r_is_packed(&cp->contents) )
  520.               *cp->where = *(ref_packed *)&cp->contents;
  521.             else
  522.               ref_assign_inline((ref *)cp->where, &cp->contents);
  523.             cp = cp->next;
  524.           }
  525.     }
  526.  
  527.     /* Free memory allocated since the save. */
  528.     /* Note that this frees all chunks except the inner one */
  529.     /* belonging to this level. */
  530.     saved_mem = save->state;
  531.     restore_free(mem);
  532.  
  533.     /* Restore the allocator state. */
  534.     {    int num_contexts = mem->num_contexts;    /* don't restore */
  535.         *mem = saved_mem;
  536.         mem->num_contexts = num_contexts;
  537.     }
  538.     alloc_open_chunk(mem);
  539. }
  540.  
  541. /* Restore to the initial state, releasing all resources. */
  542. /* The allocator is no longer usable after calling this routine! */
  543. void
  544. alloc_restore_all(gs_dual_memory_t *dmem)
  545. {
  546.     /* Restore to a state outside any saves. */
  547.     while ( dmem->save_level != 0 )
  548.         alloc_restore_state(dmem->local->saved);
  549.  
  550.     /* Release resources other than memory, using a fake save object. */
  551.     {    alloc_save_t empty_save;
  552.         empty_save.dmem = dmem;
  553.         empty_save.name_cnt = max_name_count;    /* don't bother to release */
  554.         restore_resources(&empty_save);
  555.     }
  556.  
  557.     /* Finally, release memory. */
  558.     restore_free(dmem->local);
  559.     {    gs_ref_memory_t *mem = dmem->global;
  560.         if ( mem != dmem->local )
  561.         {    if ( !--(mem->num_contexts) )
  562.                 restore_free(mem);
  563.         }
  564.     }
  565.  
  566. }
  567.  
  568. /* Release resources for a restore */
  569. private void
  570. restore_resources(alloc_save_t *sprev)
  571. {
  572.     /* Close inaccessible files. */
  573.     file_restore(sprev);
  574.  
  575.     /* Remove entries from font and character caches. */
  576.     font_restore(sprev);
  577.  
  578.     /* Adjust the name table. */
  579.     name_restore(sprev->name_cnt);
  580. }
  581.  
  582. /* Release memory for a restore. */
  583. private void
  584. restore_free(gs_ref_memory_t *mem)
  585. {    /* Free chunks allocated since the save. */
  586.     chunk_t *cp;
  587.     chunk_t *csucc;
  588.  
  589.     /* Free the chunks in reverse order, to encourage LIFO behavior. */
  590.     for ( cp = mem->clast; cp != 0; cp = csucc )
  591.     {    csucc = cp->cprev;     /* save before freeing */
  592.         alloc_free_chunk(cp, mem);
  593.     }
  594. }
  595.  
  596. /* ------ Internal routines ------ */
  597.  
  598. /* Set or reset the l_new attribute in every relevant slot. */
  599. /* This includes every slot on the current change chain, */
  600. /* and every (ref) slot allocated at this save level. */
  601. /* Return the number of bytes of data scanned. */
  602. private long
  603. save_set_new(gs_ref_memory_t *mem, bool to_new)
  604. {
  605.     long scanned = 0;
  606.  
  607.     /* Handle the change chain. */
  608.     save_set_new_changes(mem, to_new);
  609.  
  610.     /* Handle newly allocated ref objects. */
  611.     SCAN_MEM_CHUNKS(mem, cp)
  612.     { if ( cp->has_refs )
  613.       { bool has_refs = false;
  614.         SCAN_CHUNK_OBJECTS(cp)
  615.         DO_ALL
  616.           if_debug3('U', "[U]set_new scan(0x%lx(%lu), %d)\n",
  617.                 (ulong)pre, size, to_new);
  618.           if ( pre->o_type == &st_refs )
  619.            {    /* These are refs, scan them. */
  620.             ref_packed *prp = (ref_packed *)(pre + 1);
  621.             ref_packed *next = (ref_packed *)((char *)prp + size);
  622.             if_debug2('U', "[U]refs 0x%lx to 0x%lx\n",
  623.                   (ulong)prp, (ulong)next);
  624.             has_refs = true;
  625.             scanned += size;
  626.             /* We know that every block of refs ends with */
  627.             /* a full-size ref, so we only need the end check */
  628.             /* when we encounter one of those. */
  629. #define rp ((ref *)prp)
  630.             if ( to_new )
  631.               while ( 1 )
  632.                 {    if ( r_is_packed(prp) )
  633.                     prp++;
  634.                 else
  635.                 {    rp->tas.type_attrs |= l_new;
  636.                     prp += packed_per_ref;
  637.                     if ( prp >= next )
  638.                       break;
  639.                 }
  640.                 }
  641.             else
  642.               while ( 1 )
  643.                 {    if ( r_is_packed(prp) )
  644.                     prp++;
  645.                 else
  646.                 {    rp->tas.type_attrs &= ~l_new;
  647.                     prp += packed_per_ref;
  648.                     if ( prp >= next )
  649.                       break;
  650.                 }
  651.                 }
  652. #undef rp
  653.            }
  654.           else
  655.             scanned += sizeof(obj_header_t);
  656.         END_OBJECTS_SCAN
  657.         cp->has_refs = has_refs;
  658.       }
  659.     }
  660.     END_CHUNKS_SCAN
  661.     if_debug2('u', "[u]set_new (%s) scanned %ld\n",
  662.           (to_new ? "restore" : "save"), scanned);
  663.     return scanned;
  664. }
  665.  
  666. /* Set or reset the l_new attribute on the changes chain. */
  667. private void
  668. save_set_new_changes(gs_ref_memory_t *mem, bool to_new)
  669. {    register alloc_change_t *chp = mem->changes;
  670.     register uint new = (to_new ? l_new : 0);
  671.     for ( ; chp; chp = chp->next )
  672.     {    ref_packed *prp = chp->where;
  673.         if_debug2('U', "[U]set_new(0x%lx, %d)\n",
  674.               (ulong)prp, new);
  675.         if ( !r_is_packed(prp) )
  676. #define rp ((ref *)prp)
  677.          rp->tas.type_attrs =
  678.            (rp->tas.type_attrs & ~l_new) + new;
  679. #undef rp
  680.     }
  681. }
  682.