home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / IGCSTR.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  10KB  |  333 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. /* igcstr.c */
  20. /* String GC routines for Ghostscript */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "gsstruct.h"
  24. #include "iastate.h"
  25. #include "igc.h"
  26. #include "isave.h"            /* for gc_locate */
  27. #include "isstate.h"            /* ditto */
  28.  
  29. /* Import the debugging variables from gsmemory.c. */
  30. extern byte gs_alloc_fill_collected;
  31.  
  32. /* Forward references */
  33. private bool gc_locate(P2(const byte *, gc_state_t *));
  34.  
  35. /* (Un)mark the strings in a chunk. */
  36. void
  37. gc_strings_set_marks(chunk_t *cp, bool mark)
  38. {    if ( cp->smark != 0 )
  39.     { if_debug3('6', "[6]clearing string marks 0x%lx[%u] to %d\n",
  40.             (ulong)cp->smark, cp->smark_size, (int)mark);
  41.       memset(cp->smark, (mark ? 0xff : 0), cp->smark_size);
  42.     }
  43. }
  44.  
  45. /* Mark a string.  Return true if any new marks. */
  46. bool
  47. gc_string_mark(const byte *ptr, uint size, bool set, gc_state_t *gcst)
  48. {    uint left = size;
  49.     byte *bp;
  50.     uint bn;
  51.     byte m;
  52.     const chunk_t *cp;
  53.     bool marks = false;
  54.     if ( size == 0 )
  55.       return false;
  56. #define dprintstr()\
  57.   dputc('('); fwrite(ptr, 1, min(size, 20), dstderr);\
  58.   dputs((size <= 20 ? ")" : "...)"))
  59.     if ( !gc_locate(ptr, gcst) )    /* not in a chunk */
  60.       {
  61. #ifdef DEBUG
  62.         if ( gs_debug_c('5') )
  63.           {    dprintf2("[5]0x%lx[%u]", (ulong)ptr, size);
  64.             dprintstr();
  65.             dputs(" not in a chunk\n");
  66.           }
  67. #endif        
  68.         return false;
  69.       }
  70.     cp = gcst->loc.cp;
  71.     if ( cp->smark == 0 )        /* not marking strings */
  72.       return false;
  73. #ifdef DEBUG
  74.     if ( ptr < cp->ctop )
  75.       {    lprintf4("String pointer 0x%lx[%u] outside [0x%lx..0x%lx)\n",
  76.              (ulong)ptr, size, (ulong)cp->ctop, (ulong)cp->climit);
  77.         return false;
  78.       }
  79.     else if ( ptr + size > cp->climit )
  80.       {    /*
  81.          * If this is the bottommost string in a chunk that has
  82.          * an inner chunk, the string's starting address is both
  83.          * cp->ctop of the outer chunk and cp->climit of the inner;
  84.          * gc_locate may incorrectly attribute the string to the
  85.          * inner chunk because of this.  This doesn't affect
  86.          * marking or relocation, since the machinery for these
  87.          * is all associated with the outermost chunk,
  88.          * but it can cause the validity check to fail.
  89.          * Check for this case now.
  90.          */
  91.         const chunk_t *scp = cp;
  92.         while ( ptr == scp->climit && scp->outer != 0 )
  93.           scp = scp->outer;
  94.         if ( ptr + size > scp->climit )
  95.           {    lprintf4("String pointer 0x%lx[%u] outside [0x%lx..0x%lx)\n",
  96.                  (ulong)ptr, size,
  97.                  (ulong)scp->ctop, (ulong)scp->climit);
  98.             return false;
  99.           }
  100.       }
  101. #endif
  102.     bp = cp->smark + ((ptr - cp->sbase) >> 3);
  103.     bn = (ptr - cp->sbase) & 7;
  104.     m = 0xff << bn;
  105.     if ( set )
  106.       {    while ( left + bn >= 8 )
  107.           {    if ( ~*bp & m )
  108.               *bp |= m, marks = true;
  109.             m = 0xff, left -= 8 - bn, bn = 0, bp++;
  110.           }
  111.         if ( left )
  112.           {    m -= m << left;
  113.             if ( ~*bp & m )
  114.               *bp |= m, marks = true;
  115.           }
  116.       }
  117.     else
  118.       {    while ( left + bn >= 8 )
  119.           {    *bp &= ~m;
  120.             m = 0xff, left -= 8 - bn, bn = 0, bp++;
  121.           }
  122.         if ( left )
  123.           {    m -= m << left;
  124.             *bp &= ~m;
  125.           }
  126.       }
  127. #ifdef DEBUG
  128.     if ( gs_debug_c('5') )
  129.       {    dprintf4("[5]%s%smarked 0x%lx[%u]",
  130.              (marks ? "" : "already "), (set ? "" : "un"),
  131.              (ulong)ptr, size);
  132.         dprintstr();
  133.         dputc('\n');
  134.       }
  135. #endif
  136.     return marks;
  137. }
  138.  
  139. /* Clear the relocation for strings. */
  140. void
  141. gc_strings_clear_reloc(chunk_t *cp)
  142. {    if ( cp->sreloc != 0 )
  143.     { uint sreloc_size = ((cp->smark_size + 3) >> 2) * sizeof(ushort);
  144.       if_debug2('6', "[6]clearing string reloc 0x%lx[%u]\n",
  145.             (ulong)cp->sreloc, sreloc_size);
  146.       memset(cp->sreloc, 0, sreloc_size);
  147.     }
  148. }
  149.  
  150. /* Count the 0-bits in a byte. */
  151. private const byte count_zero_bits_table[256] = {
  152. #define o4(n) n,n-1,n-1,n-2
  153. #define o16(n) o4(n),o4(n-1),o4(n-1),o4(n-2)
  154. #define o64(n) o16(n),o16(n-1),o16(n-1),o16(n-2)
  155.     o64(8), o64(7), o64(7), o64(6)
  156. };
  157. #define byte_count_zero_bits(byt)\
  158.   (uint)(count_zero_bits_table[byt])
  159. #define byte_count_one_bits(byt)\
  160.   (uint)(8 - count_zero_bits_table[byt])
  161.  
  162. /* Set the relocation for the strings in a chunk. */
  163. /* The sreloc table stores the relocated offset from climit for */
  164. /* the beginning of each block of 32 characters. */
  165. void
  166. gc_strings_set_reloc(chunk_t *cp)
  167. {    if ( cp->sreloc != 0 && cp->smark != 0 )
  168.     {    byte *bot = cp->ctop;
  169.         byte *top = cp->climit;
  170.         uint count = (top - bot + 31) >> 5;
  171.         ushort *relp = cp->sreloc + (cp->smark_size >> 2);
  172.         register byte *bitp = cp->smark + cp->smark_size;
  173.         register ushort reloc = 0;
  174.         while ( count-- )
  175.         {    bitp -= 4;
  176.             reloc += 32 - byte_count_zero_bits(bitp[0]);
  177.             reloc -= byte_count_zero_bits(bitp[1]);
  178.             reloc -= byte_count_zero_bits(bitp[2]);
  179.             reloc -= byte_count_zero_bits(bitp[3]);
  180.             *--relp = reloc;
  181.         }
  182.     }
  183.     cp->sdest = cp->climit;
  184. }
  185.  
  186. /* Relocate a pointer to a string. */
  187. byte *
  188. gs_reloc_string_ptr(byte *ptr, gc_state_t *gcst)
  189. {    const chunk_t *cp;
  190.     uint offset;
  191.     uint reloc;
  192.     const byte *bitp;
  193.     byte byt;
  194.     if ( !gc_locate(ptr, gcst) )    /* not in a chunk */
  195.       return ptr;
  196.     cp = gcst->loc.cp;
  197.     if ( cp->sreloc == 0 || cp->smark == 0 ) /* not marking strings */
  198.       return ptr;
  199.     offset = ptr - cp->sbase;
  200.     reloc = cp->sreloc[offset >> 5];
  201.     bitp = &cp->smark[offset >> 3];
  202.     switch ( offset & 24 )
  203.     {
  204.     case 24: reloc -= byte_count_one_bits(bitp[-3]);
  205.     case 16: reloc -= byte_count_one_bits(bitp[-2]);
  206.     case 8: reloc -= byte_count_one_bits(bitp[-1]);
  207.     }
  208.     byt = *bitp & (0xff >> (8 - (offset & 7)));
  209.     reloc -= byte_count_one_bits(byt);
  210.     return print_reloc(ptr, "char", cp->sdest - reloc);
  211. }
  212. const byte *
  213. gs_reloc_const_string_ptr(const byte *ptr, gc_state_t *gcst)
  214. {    return (const byte *)gs_reloc_string_ptr((byte *)ptr, gcst);
  215. }
  216.  
  217. /* Compact the strings in a chunk. */
  218. void
  219. gc_strings_compact(chunk_t *cp)
  220. {    if ( cp->smark != 0 )
  221.     {    byte *hi = cp->climit;
  222.         byte *lo = cp->ctop;
  223.         register const byte *from = hi;
  224.         register byte *to = hi;
  225.         register const byte *bp = cp->smark + cp->smark_size;
  226. #ifdef DEBUG
  227.         if ( gs_debug_c('4') || gs_debug_c('5') )
  228.           { const byte *base = cp->sbase;
  229.             uint i = (lo - base) & ~31;
  230.             uint n = (hi - base + 31) & ~31;
  231. #define R 16
  232.             for ( ; i < n; i += R )
  233.               { uint j;
  234.             dprintf1("[4]0x%lx: ", (ulong)(base + i));
  235.             for ( j = i; j < i + R; j++ )
  236.               { byte ch = base[j];
  237.                 if ( ch <= 31 )
  238.                   { dputc('^'); dputc(ch + 0100); }
  239.                 else
  240.                   dputc(ch);
  241.               }
  242.             dputc(' ');
  243.             for ( j = i; j < i + R; j++ )
  244.               dputc((cp->smark[j >> 3] & (1 << (j & 7)) ?
  245.                  '+' : '.'));
  246. #undef R
  247.             if ( !(i & 31) )
  248.               dprintf1(" %u", cp->sreloc[i >> 5]);
  249.             dputc('\n');
  250.               }
  251.           }
  252. #endif
  253.         while ( from > lo )
  254.           { register byte b = *--bp;
  255.             from -= 8;
  256.             switch ( b )
  257.               {
  258.               case 0xff:
  259.             to -= 8;
  260.             if ( to != from )
  261.               { to[7] = from[7]; to[6] = from[6];
  262.                 to[5] = from[5]; to[4] = from[4];
  263.                 to[3] = from[3]; to[2] = from[2];
  264.                 to[1] = from[1]; to[0] = from[0];
  265.               }
  266.             break;
  267.               default:
  268.             if ( b & 0x80 ) *--to = from[7];
  269.             if ( b & 0x40 ) *--to = from[6];
  270.             if ( b & 0x20 ) *--to = from[5];
  271.             if ( b & 0x10 ) *--to = from[4];
  272.             if ( b & 8 ) *--to = from[3];
  273.             if ( b & 4 ) *--to = from[2];
  274.             if ( b & 2 ) *--to = from[1];
  275.             if ( b & 1 ) *--to = from[0];
  276.             /* falls through */
  277.               case 0:
  278.             ;
  279.               }
  280.           }
  281.         if ( gs_alloc_debug )
  282.           memset(cp->ctop, gs_alloc_fill_collected, to - cp->ctop);
  283.         cp->ctop = to;
  284.     }
  285. }
  286.  
  287. /* ---------------- Private routines ---------------- */
  288.  
  289. /* Locate a pointer in the chunks of either space being collected. */
  290. private bool
  291. gc_locate(const byte *ptr, gc_state_t *gcst)
  292. {    const gs_ref_memory_t *mem;
  293.     if ( chunk_locate(ptr, &gcst->loc) )
  294.       return true;
  295.     mem = gcst->loc.memory;
  296.     /* Try the other space, if there is one. */
  297.     if ( gcst->local != gcst->global )
  298.       { gcst->loc.memory = (mem->local_attr ? gcst->global : gcst->local);
  299.         gcst->loc.cp = 0;
  300.         if ( chunk_locate(ptr, &gcst->loc) )
  301.           return true;
  302.         /* Try other save levels of this space. */
  303.         while ( (gcst->loc.memory = &gcst->loc.memory->saved->state) != 0 )
  304.           { gcst->loc.cp = 0;
  305.         if ( chunk_locate(ptr, &gcst->loc) )
  306.           return true;
  307.           }
  308.       }
  309.     /* Try the system space.  This is simpler because it isn't */
  310.     /* subject to save/restore. */
  311.     if ( mem != gcst->system )
  312.     {    gcst->loc.memory = gcst->system;
  313.         gcst->loc.cp = 0;
  314.         if ( chunk_locate(ptr, &gcst->loc) )
  315.           return true;
  316.     }
  317.     /* Try other save levels of the initial space, */
  318.     /* or of global space if the original space was system space. */
  319.     /* In the latter case, try all levels. */
  320.     gcst->loc.memory = (mem->local_attr ? gcst->local : gcst->global);
  321.     do
  322.       { if ( gcst->loc.memory != mem )    /* don't do twice */
  323.           if ( chunk_locate(ptr, &gcst->loc) )
  324.         return true;
  325.         gcst->loc.memory = &gcst->loc.memory->saved->state;
  326.       }
  327.     while ( gcst->loc.memory != 0 );
  328.     /* Restore locator to a legal state. */
  329.     gcst->loc.memory = mem;
  330.     gcst->loc.cp = 0;
  331.     return false;
  332. }
  333.