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

  1. /* Copyright (C) 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. /* igcref.c */
  20. /* ref garbage collector for Ghostscript */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "iname.h"
  24. #include "iastate.h"
  25. #include "idebug.h"
  26. #include "igc.h"
  27. #include "ipacked.h"
  28. #include "store.h"            /* for ref_assign_inline */
  29.  
  30. /*
  31.  * Define the 'structure' type descriptor for refs.
  32.  * This is special because it has different shared procs.
  33.  */
  34. private gc_proc_clear_reloc(refs_clear_reloc);
  35. private gc_proc_set_reloc(refs_set_reloc);
  36. private gc_proc_compact(refs_compact);
  37. private const struct_shared_procs_t refs_shared_procs =
  38.  { refs_clear_reloc, refs_set_reloc, refs_compact };
  39. private struct_proc_clear_marks(refs_clear_marks);
  40. private struct_proc_reloc_ptrs(refs_do_reloc);
  41. const gs_memory_struct_type_t st_refs =
  42.  { sizeof(ref), "refs", &refs_shared_procs, refs_clear_marks, 0, refs_do_reloc };
  43.  
  44. /*
  45.  * Define the GC procedures for structs that actually contain refs.
  46.  * These are special because the shared refs_* procedures
  47.  * are never called.  Instead, we unmark the individual refs in clear_marks,
  48.  * disregard refs_*_reloc (because we will never relocate a ptr_ref_type
  49.  * pointer pointing into the structure), disregard refs_compact (because
  50.  * compaction is never required), and remove the marks in reloc_ptrs.
  51.  * See also the comment about ptr_ref_type in imemory.h.
  52.  */
  53. CLEAR_MARKS_PROC(ref_struct_clear_marks) {
  54.     ref *pref = (ref *)vptr;
  55.     ref *end = (ref *)((char *)vptr + size);
  56.     for ( ; pref < end; pref++ )
  57.         r_clear_attrs(pref, l_mark);
  58. }
  59. ENUM_PTRS_BEGIN_PROC(ref_struct_enum_ptrs) {
  60.     if ( index >= size / sizeof(ref) )
  61.       return 0;
  62.     *pep = (ref *)vptr + index;
  63.     return ptr_ref_type;
  64. ENUM_PTRS_END_PROC }
  65. RELOC_PTRS_BEGIN(ref_struct_reloc_ptrs) {
  66.     ref *beg = vptr;
  67.     ref *end = (ref *)((char *)vptr + size);
  68.     gs_reloc_refs((ref_packed *)beg, (ref_packed *)end, gcst);
  69.     ref_struct_clear_marks(vptr, size);
  70. } RELOC_PTRS_END
  71.  
  72. /* ------ Unmarking phase ------ */
  73.  
  74. /* Unmark a single ref. */
  75. void
  76. ptr_ref_unmark(void *vptr, gc_state_t *ignored)
  77. {
  78. #define ptr ((ref *)vptr)
  79. #define pptr ((ref_packed *)vptr)
  80.     if ( r_is_packed(ptr) )
  81.       r_clear_pmark(pptr);
  82.     else
  83.       r_clear_attrs(ptr, l_mark);
  84. #undef ptr
  85. #undef pptr
  86. }
  87.  
  88. /* Unmarking routine for ref objects. */
  89. private void
  90. refs_clear_marks(void /*obj_header_t*/ *vptr, uint size)
  91. {    gs_mark_refs((ref_packed *)vptr,
  92.              (ref *)((byte *)vptr + size),
  93.              false);
  94. }
  95. /* Mark or unmark a block of refs. */
  96. /* The last ref must be full-size, and is never marked. */
  97. void
  98. gs_mark_refs(ref_packed *from, ref *to, bool mark)
  99. {    ref_packed *rp = from;
  100.     ushort pmark = (mark ? lp_mark : 0);
  101.     ushort rmark = (mark ? l_mark : 0);
  102.     /* Since the last ref is full-size, we only need to check for */
  103.     /* the end of the block when we see one of those. */
  104.     for ( ; ; )
  105.     {    if ( r_is_packed(rp) )
  106.         {
  107. #ifdef DEBUG
  108. if ( gs_debug_c('8') )
  109. {            dprintf1("  [8]unmark packed 0x%lx ", (ulong)rp);
  110.             debug_print_ref((const ref *)rp);
  111.             dprintf("\n");
  112. }
  113. #endif
  114.             r_store_pmark(rp, pmark);
  115.             rp++;
  116.         }
  117.         else            /* full-size ref */
  118.         {
  119. #ifdef DEBUG
  120. if ( gs_debug_c('8') )
  121. {            dprintf1("  [8]unmark ref 0x%lx ", (ulong)rp);
  122.             debug_print_ref((ref *)rp);
  123.             dprintf("\n");
  124. }
  125. #endif
  126.             r_store_attrs((ref *)rp, l_mark, rmark);
  127.             rp += packed_per_ref;
  128.             if ( rp >= (ref_packed *)to )
  129.             {    /* Ensure the last ref is not marked. */
  130.                 r_clear_attrs((ref *)rp - 1, l_mark);
  131.                 break;
  132.             }
  133.         }
  134.     }
  135. }
  136.  
  137. /* ------ Marking phase ------ */
  138.  
  139. /* Mark a ref.  Return true if new mark. */
  140. bool
  141. ptr_ref_mark(void *vptr, gc_state_t *ignored)
  142. {
  143. #define ptr ((ref *)vptr)
  144. #define pptr ((ref_packed *)vptr)
  145.     if ( r_is_packed(ptr) )
  146.       { if ( r_has_pmark(pptr) ) return false;
  147.         r_set_pmark(pptr);
  148.       }
  149.     else
  150.       { if ( r_has_attr(ptr, l_mark) ) return false;
  151.         r_set_attrs(ptr, l_mark);
  152.       }
  153.     return true;
  154. #undef ptr
  155. #undef pptr
  156. }
  157.  
  158. /* ------ Relocation planning phase ------ */
  159.  
  160. /*
  161.  * We store relocation in the size field of refs that don't use it,
  162.  * so that we don't have to scan all the way to an unmarked object.
  163.  * We must avoid nulls, which sometimes have useful information
  164.  * in their size fields, and the types above t_next_index, which are
  165.  * actually operators in disguise and also use the size field.
  166.  */
  167. #define case_types_using_size\
  168.   case t_null: case_types_with_size
  169.  
  170. /* Clear the relocation for a ref object. */
  171. private void
  172. refs_clear_reloc(obj_header_t *hdr, uint size)
  173. {    register ref_packed *rp = (ref_packed *)(hdr + 1);
  174.     ref_packed *end = (ref_packed *)((byte *)rp + size);
  175.     while ( rp < end )
  176.     {    if ( r_is_packed(rp) )
  177.             rp++;
  178.         else            /* full-size ref */
  179.           {
  180. #define pref ((ref *)rp)
  181.             uint type;
  182.             /* Store the relocation here if possible. */
  183.             switch ( type = r_type(pref) )
  184.             {    case_types_using_size:
  185.                     break;
  186.                 default:
  187.                     if ( type >= t_next_index )
  188.                       break;
  189.                     if_debug1('8', "  [8]clearing reloc at 0x%lx\n",
  190.                           (ulong)pref);
  191.                     r_set_size(pref, 0);
  192.             }
  193.             rp += packed_per_ref;
  194. #undef pref
  195.         }
  196.     }
  197. }
  198.  
  199. /* Set the relocation for a ref object. */
  200. private bool
  201. refs_set_reloc(obj_header_t *hdr, uint reloc, uint size)
  202. {    ref_packed *rp = (ref_packed *)(hdr + 1);
  203.     ref_packed *end = (ref_packed *)((byte *)rp + size);
  204.     uint freed = 0;
  205.     /*
  206.      * We have to be careful to keep refs aligned properly.
  207.      * For the moment, we do this by either keeping or discarding
  208.      * an entire (aligned) block of align_packed_per_ref packed elements
  209.      * as a unit.  We know that align_packed_per_ref <= packed_per_ref,
  210.      * and we also know that packed refs are always allocated in blocks
  211.      * of align_packed_per_ref, so this makes things relatively easy.
  212.      */
  213.     while ( rp < end )
  214.     {    if ( r_is_packed(rp) )
  215.         {
  216. #if align_packed_per_ref == 1
  217.             if ( r_has_pmark(rp) )
  218.             {    if_debug1('8',
  219.                       "  [8]packed ref 0x%lx is marked\n",
  220.                       (ulong)rp);
  221.                 rp++;
  222.             }
  223. #else
  224.             uint marked = *rp;
  225.             int i;
  226.             for ( i = 1; i < align_packed_per_ref; i++ )
  227.                 marked |= rp[i];
  228.             if ( marked & lp_mark )
  229.             {    /* At least one packed_ref in the block */
  230.                 /* is marked: Keep the whole block. */
  231.                 for ( ; i; i--, rp++ )
  232.                 {    r_set_pmark(rp);
  233.                     if_debug1('8',
  234.                       "  [8]packed ref 0x%lx is marked\n",
  235.                       (ulong)rp);
  236.                 }
  237.             }
  238. #endif
  239.             else
  240.             {    if_debug2('8', "  [8]%d packed ref(s) at 0x%lx are unmarked\n",
  241.                       align_packed_per_ref, (ulong)rp);
  242.                 rp += align_packed_per_ref;
  243.                 freed += sizeof(ref_packed) * align_packed_per_ref;
  244.             }
  245.         }
  246.         else            /* full-size ref */
  247.         {    uint rel = reloc + freed;
  248. #define pref ((ref *)rp)
  249.             if ( !r_has_attr(pref, l_mark) )
  250.             {    if_debug1('8', "  [8]ref 0x%lx is unmarked\n",
  251.                       (ulong)pref);
  252.                 /* Change this to a mark so we can */
  253.                 /* store the relocation. */
  254.                 r_set_type(pref, t_mark);
  255.                 r_set_size(pref, rel);
  256.                 freed += sizeof(ref);
  257.             }
  258.             else
  259.             {    uint type;
  260.                 if_debug1('8', "  [8]ref 0x%lx is marked\n",
  261.                       (ulong)pref);
  262.                 /* Store the relocation here if possible. */
  263.                 switch ( type = r_type(pref) )
  264.                 {    case_types_using_size:
  265.                         break;
  266.                     default:
  267.                         if ( type >= t_next_index )
  268.                           break;
  269.                         if_debug2('8', "  [8]storing reloc %u at 0x%lx\n",
  270.                               rel, (ulong)pref);
  271.                         r_set_size(pref, rel);
  272.                 }
  273.             }
  274.             rp += packed_per_ref;
  275. #undef pref
  276.         }
  277.     }
  278.     if_debug3('7', " [7]at end of refs 0x%lx, size = %u, freed = %u\n",
  279.           (ulong)(hdr + 1), size, freed);
  280.     return freed != size;
  281. }
  282.  
  283. /* ------ Relocation phase ------ */
  284.  
  285. /* Relocate all the pointers in a block of refs. */
  286. private void
  287. refs_do_reloc(void /*obj_header_t*/ *vptr, uint size, gc_state_t *gcst)
  288. {    gs_reloc_refs((ref_packed *)vptr,
  289.               (ref_packed *)((char *)vptr + size),
  290.               gcst);
  291. }
  292. /* Relocate the contents of a block of refs. */
  293. void
  294. gs_reloc_refs(ref_packed *from, ref_packed *to, gc_state_t *gcst)
  295. {    ref_packed *rp = from;
  296.     while ( rp < to )
  297.     {    if ( r_is_packed(rp) )
  298.         {    rp++;
  299.             continue;
  300.         }
  301. #define pref ((ref *)rp)
  302.         if_debug3('8', "  [8]relocating %s %d ref at 0x%lx\n",
  303.               (r_has_attr(pref, l_mark) ? "marked" : "unmarked"),
  304.               r_btype(pref), (ulong)pref);
  305.         if ( r_has_attr(pref, l_mark) && !r_has_attr(pref, a_foreign) )
  306.           switch ( r_type(pref) )
  307.         {
  308.         /* Struct cases */
  309. #define ref_case(v, t)\
  310.   pref->value.v =\
  311.     (t *)gs_reloc_struct_ptr((obj_header_t *)pref->value.v, gcst)
  312.         case t_file:
  313.             ref_case(pfile, struct stream_s); break;
  314.         case t_device:
  315.             ref_case(pdevice, struct gx_device_s); break;
  316.         case t_fontID:
  317.         case t_struct:
  318.         case t_astruct:
  319.             ref_case(pstruct, void); break;
  320. #undef ref_case
  321.         /* Non-trivial non-struct cases */
  322.         case t_dictionary:
  323.             pref->value.pdict =
  324.               (dict *)gs_reloc_ref_ptr((ref_packed *)pref->value.pdict, gcst);
  325.             break;
  326.         case t_array:
  327.             if ( r_size(pref) != 0 ) /* value.refs might be NULL */
  328.               pref->value.refs =
  329.                 (ref *)gs_reloc_ref_ptr((ref_packed *)pref->value.refs, gcst);
  330.             break;
  331.         case t_mixedarray: case t_shortarray:
  332.             if ( r_size(pref) != 0 ) /* value.refs might be NULL */
  333.               pref->value.packed =
  334.                 (const ref_packed *)gs_reloc_ref_ptr((void *)pref->value.packed, gcst); /* discard const */
  335.             break;
  336.         case t_name:
  337.           {    void *psub = name_ref_sub_table(pref);
  338.             void *rsub = gs_reloc_struct_ptr(psub, gcst);
  339.             pref->value.pname = (name *)
  340.               ((char *)rsub + ((char *)pref->value.pname - (char *)psub));
  341.           }    break;
  342.         case t_string:
  343.             pref->value.bytes = gs_reloc_string_ptr(pref->value.bytes, gcst);
  344.             break;
  345.         }
  346. #undef pref
  347.         rp += packed_per_ref;
  348.     }
  349. }
  350.  
  351. /* Relocate a pointer to a ref. */
  352. ref_packed *
  353. gs_reloc_ref_ptr(ref_packed *prp, gc_state_t *ignored)
  354. {    /* Search forward for relocation. */
  355.     /* This algorithm is intrinsically very inefficient; */
  356.     /* we hope eventually to replace it with a better one. */
  357.     register ref_packed *rp = prp;
  358.     uint dec = 0;
  359.     for ( ; ; )
  360.     {    uint type;
  361.         if ( r_is_packed(rp) )
  362.         {    /* For each unmarked packed ref we pass over, */
  363.             /* we have to decrement the final relocation. */
  364.             if ( r_is_packed(rp + 1) )
  365.             {    /* Almost all packed refs are marked, */
  366.                 /* so test both at the same time. */
  367.                 if ( !(*rp & rp[1] & lp_mark) )
  368.                 {    if ( (*rp | rp[1]) & lp_mark )
  369.                         dec += sizeof(ref_packed);
  370.                     else
  371.                         dec += sizeof(ref_packed) * 2;
  372.                 }
  373.                 rp += 2;
  374.                 continue;
  375.             }
  376.             else if ( !r_has_pmark(rp) )
  377.                 dec += sizeof(ref_packed);
  378.             rp++;        /* fall through */
  379.         }
  380.         switch ( type = r_type((ref *)rp) )
  381.         {
  382.         default:        /* reloc is in r_size */
  383.             if ( type < t_next_index )
  384.               {    /* These refs might be in a space */
  385.                 /* that isn't being compacted.  If so, */
  386.                 /* the relocation value here will be zero. */
  387.                 return print_reloc(prp, "ref",
  388.                     (r_size((ref *)rp) == 0 ? prp :
  389.                      (ref_packed *)((char *)prp - r_size((ref *)rp) + dec)));
  390.               }
  391.             /* falls through */
  392.         case_types_using_size:
  393.             rp += packed_per_ref;
  394.         }
  395.     }
  396. }
  397.  
  398. /* ------ Compaction phase ------ */
  399.  
  400. /* Compact a ref object. */
  401. /* Remove the marks at the same time. */
  402. private void
  403. refs_compact(obj_header_t *pre, obj_header_t *dpre, uint size)
  404. {    ref_packed *dest;
  405.     ref_packed *src;
  406.     ref_packed *end;
  407.     uint new_size;
  408.     src = (ref_packed *)(pre + 1);
  409.     end = (ref_packed *)((byte *)src + size);
  410.     /*
  411.      * We know that a block of refs always ends with an unmarked
  412.      * full-size ref, so we only need to check for reaching the end
  413.      * of the block when we see one of those.
  414.      */
  415.     if ( dpre == pre )    /* Loop while we don't need to copy. */
  416.       for ( ; ; )
  417.     {    if ( r_is_packed(src) )
  418.         {    if ( !r_has_pmark(src) )
  419.                 break;
  420.             if_debug1('8', "  [8]packed ref 0x%lx \"copied\"\n",
  421.                       (ulong)src);
  422.             *src &= ~lp_mark;
  423.             src++;
  424.         }
  425.         else            /* full-size ref */
  426.         {    if ( !r_has_attr((ref *)src, l_mark) )
  427.                 break;
  428.             if_debug1('8', "  [8]ref 0x%lx \"copied\"\n",
  429.                       (ulong)src);
  430.             r_clear_attrs((ref *)src, l_mark);
  431.             src += packed_per_ref;
  432.         }
  433.     }
  434.     else
  435.         *dpre = *pre;
  436.     dest = (ref_packed *)((char *)dpre + ((char *)src - (char *)pre));
  437.     for ( ; ; )
  438.     {    if ( r_is_packed(src) )
  439.         {    if ( r_has_pmark(src) )
  440.             {    if_debug2('8', "  [8]packed ref 0x%lx copied to 0x%lx\n",
  441.                       (ulong)src, (ulong)dest);
  442.                 *dest++ = *src & ~lp_mark;
  443.             }
  444.             src++;
  445.         }
  446.         else            /* full-size ref */
  447.         {    if ( r_has_attr((ref *)src, l_mark) )
  448.             {    ref rtemp;
  449.                 if_debug2('8', "  [8]ref 0x%lx copied to 0x%lx\n",
  450.                       (ulong)src, (ulong)dest);
  451.                 /* We can't just use ref_assign_inline, */
  452.                 /* because the source and destination */
  453.                 /* might overlap! */
  454.                 ref_assign_inline(&rtemp, (ref *)src);
  455.                 r_clear_attrs(&rtemp, l_mark);
  456.                 ref_assign_inline((ref *)dest, &rtemp);
  457.                 dest += packed_per_ref;
  458.                 src += packed_per_ref;
  459.             }
  460.             else        /* check for end of block */
  461.             {    src += packed_per_ref;
  462.                 if ( src >= end )
  463.                     break;
  464.             }
  465.         }
  466.     }
  467.     new_size = (byte *)dest - (byte *)(dpre + 1) + sizeof(ref);
  468. #ifdef DEBUG
  469.     /* Check that the relocation came out OK. */
  470.     /* NOTE: this check only works within a single chunk. */
  471.     if ( (byte *)src - (byte *)dest != r_size((ref *)src - 1) + sizeof(ref) )
  472.     {    lprintf3("Reloc error for refs 0x%lx: reloc = %lu, stored = %u\n",
  473.              (ulong)dpre, (ulong)((byte *)src - (byte *)dest),
  474.              (uint)r_size((ref *)src - 1));
  475.         exit(1);
  476.     }
  477. #endif
  478.     /* Pad to a multiple of sizeof(ref). */
  479.     while ( new_size & (sizeof(ref) - 1) )
  480.         *dest++ = pt_tag(pt_integer),
  481.         new_size += sizeof(ref_packed);
  482.     /* We want to make the newly freed space into a free block, */
  483.     /* but we can only do this if we have enough room. */
  484.     if ( size - new_size < sizeof(obj_header_t) )
  485.       {    /* Not enough room.  Pad to original size. */
  486.         while ( new_size < size )
  487.           *dest++ = pt_tag(pt_integer),
  488.           new_size += sizeof(ref_packed);
  489.       }
  490.     else
  491.       {    obj_header_t *pfree = (obj_header_t *)((ref *)dest + 1);
  492.         pfree->o_large = 0;
  493.         pfree->o_size = size - new_size - sizeof(obj_header_t);
  494.         pfree->o_type = &st_bytes;
  495.       }
  496.     /* Re-create the final ref. */
  497.     r_set_type((ref *)dest, t_integer);
  498.     dpre->o_size = new_size;
  499. }
  500.