home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / INAME.C < prev    next >
C/C++ Source or Header  |  1992-06-16  |  8KB  |  260 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* iname.c */
  21. /* Name lookup for Ghostscript interpreter */
  22. #include "memory_.h"
  23. #include "ghost.h"
  24. #include "alloc.h"
  25. #include "errors.h"
  26. #include "name.h"
  27. #include "store.h"
  28.  
  29. /* Definitions and structure for the name table. */
  30. /* The first entry is left unused. */
  31. /* 1-character names are the next nt_1char_size entries. */
  32. #define nt_log2_sub_size 7
  33. #define nt_sub_size (1 << nt_log2_sub_size)
  34. #define nt_sub_index_mask (nt_sub_size - 1)
  35. #define nt_hash_size 256        /* must be a power of 2 */
  36. #define nt_1char_size 128
  37. typedef name name_sub_table[nt_sub_size];
  38. typedef struct {
  39.     ushort hash[nt_hash_size];
  40.     name *table[1 << (16 - nt_log2_sub_size)];    /* name_sub_table */
  41.     ref count;            /* t_integer */
  42. #define nt_count(nt) (uint)((nt)->count.value.intval)
  43. #define set_nt_count(nt,cnt) ((nt)->count.value.intval = (cnt))
  44. } name_table;
  45. #define name_index_ptr(nt, index)\
  46.   ((nt)->table[(index) >> nt_log2_sub_size] + ((index) & nt_sub_index_mask))
  47.  
  48. /*
  49.  * Scramble the assignment order within a sub-table, so that
  50.  * dictionary lookup doesn't have to scramble the index.
  51.  * The algorithm must have three properties:
  52.  *    - It must map 0 to 0;
  53.  *    - It must only scramble the sub-table index;
  54.  *    - It must be a permutation on the sub-table index.
  55.  * We do something very simple for now.
  56.  */
  57. #define name_count_to_index(cnt)\
  58.   (((cnt) & (-nt_sub_size)) + (((cnt) * 59) & nt_sub_index_mask))
  59. /* We also store the reverse permutation, for age checking in restore. */
  60. #define name_index_to_count(idx)\
  61.   ((idx) ^ name_sub_index_to_count[(idx) & nt_sub_index_mask])
  62. #if nt_sub_size <= 256
  63. private byte name_sub_index_to_count[nt_sub_size];
  64. #else
  65. private ushort name_sub_index_to_count[nt_sub_size];
  66. #endif
  67.  
  68. /* The one and only name table (for now). */
  69. private name_table *the_nt;
  70.  
  71. /* Forward references */
  72. private int name_alloc_sub(P1(name_table *));
  73.  
  74. /* Make a t_name ref out of a name * */
  75. #define make_name(pref, idx, pnm)\
  76.   make_tasv(pref, t_name, 0, idx, pname, pnm)
  77.  
  78. /* Initialize the name table */
  79. void
  80. name_init()
  81. {    register uint i;
  82.     static byte one_char_names[nt_1char_size];
  83.     /* Initialize the index_to_count map. */
  84.     for ( i = 0; i < nt_sub_size; i++ )
  85.        {    uint idx = name_count_to_index(i);
  86.         name_sub_index_to_count[idx] = idx ^ i;
  87.        }
  88.     the_nt = (name_table *)alloc(1, sizeof(name_table), "name_init");
  89.     memset(the_nt, 0, sizeof(name_table));
  90.     make_int(&the_nt->count, 1);
  91.     /* Initialize the one-character names. */
  92.     for ( i = 0; i < nt_1char_size; i++ )
  93.        {    uint ncnt = i + 1;
  94.         uint nidx = name_count_to_index(ncnt);
  95.         register name *pname;
  96.         if ( the_nt->table[nidx >> nt_log2_sub_size] == 0 )
  97.             name_alloc_sub(the_nt);
  98.         pname = name_index_ptr(the_nt, nidx);
  99.         set_nt_count(the_nt, ncnt + 1);
  100.         one_char_names[i] = i;
  101.         pname->string_size = 1;
  102.         pname->string_bytes = &one_char_names[i];
  103.         pname->pvalue = pv_no_defn;
  104.        }
  105. }
  106.  
  107. /* Look up or enter a name in the table. */
  108. /* Return 0 or an error code. */
  109. /* The return may overlap the characters of the string! */
  110. /* See name.h for the meaning of enterflag. */
  111. int
  112. name_ref(const byte *ptr, uint isize, ref *pref, int enterflag)
  113. {    register name *pname;
  114.     const byte *cptr;
  115.     ushort size = (ushort)isize;    /* see name.h */
  116.     uint nidx;
  117.     if ( size == 1 && *ptr < nt_1char_size )
  118.        {    uint ccnt = *ptr + 1;
  119.         nidx = name_count_to_index(ccnt);
  120.         pname = name_index_ptr(the_nt, nidx);
  121.         make_name(pref, nidx, pname);
  122.         return 0;
  123.        }
  124.     else
  125.        {    ushort *phash =
  126.           the_nt->hash +
  127.             ((ushort)string_hash(ptr, size) & (nt_hash_size - 1));
  128.         uint ncnt;
  129.         nidx = *phash;
  130.         while ( nidx != 0 )
  131.            {    pname = name_index_ptr(the_nt, nidx);
  132.             if ( pname->string_size == size &&
  133.                  !memcmp(ptr, pname->string_bytes, size)
  134.                )
  135.                {    make_name(pref, nidx, pname);
  136.                 return 0;
  137.                }
  138.             nidx = pname->next_index;
  139.            }
  140.         /* Not in table, allocate a new entry. */
  141.         if ( enterflag < 0 ) return e_undefined;
  142.         if ( !(nt_count(the_nt) & (nt_sub_size - 1)) )
  143.            {    int code = name_alloc_sub(the_nt);
  144.             if ( code < 0 ) return code;
  145.            }
  146.         ncnt = nt_count(the_nt);
  147.         nidx = name_count_to_index(ncnt);
  148.         pname = name_index_ptr(the_nt, nidx);
  149.         pname->next_index = *phash;
  150.         *phash = nidx;
  151.         if_debug4('n', "[n]new name 0x%lx#%u, length=%u, count=%u\n",
  152.               (ulong)pname, nidx, isize, ncnt);
  153.         ref_save(&the_nt->count, "name_ref(count)");
  154.         set_nt_count(the_nt, ncnt + 1);
  155.        }
  156.     /* Name was not in the table.  Make a new entry. */
  157.     if ( enterflag )
  158.        {    cptr = (const byte *)alloc(size, 1, "name_ref(string)");
  159.         if ( cptr == 0 ) return e_VMerror;
  160.         memcpy((byte *)cptr, ptr, size);
  161.        }
  162.     else
  163.         cptr = ptr;
  164.     pname->string_size = size;
  165.     pname->string_bytes = cptr;
  166.     pname->pvalue = pv_no_defn;
  167.     make_name(pref, nidx, pname);
  168.     return 0;
  169. }
  170.  
  171. /* Get the string for a name. */
  172. void
  173. name_string_ref(const ref *pnref /* t_name */,
  174.   ref *psref /* result, t_string */)
  175. {    name *pname = pnref->value.pname;
  176.     make_tasv(psref, t_string, a_readonly, pname->string_size,
  177.           bytes, (byte *)pname->string_bytes);
  178. }
  179.  
  180. /* Convert a t_string object to a name. */
  181. /* Copy the executable attribute. */
  182. int
  183. name_from_string(const ref *psref, ref *pnref)
  184. {    int exec = r_has_attr(psref, a_executable);
  185.     int code = name_ref(psref->value.bytes, r_size(psref), pnref, 1);
  186.     if ( code < 0 ) return code;
  187.     if ( exec ) r_set_attrs(pnref, a_executable);
  188.     return code;
  189. }
  190.  
  191. /* Enter a name during initialization. */
  192. /* Fatal error if the entry fails. */
  193. void
  194. name_enter(const char *str, ref *pref)
  195. {    if ( name_ref((const byte *)str, strlen(str), pref, 0) )
  196.         lprintf1("name_enter failed - %s", str),
  197.         gs_exit(1);
  198. }
  199.  
  200. /* Get the name with a given index. */
  201. void
  202. name_index_ref(uint index, ref *pnref)
  203. {    make_name(pnref, index, name_index_ptr(the_nt, index));
  204. }
  205.  
  206. /* Get the current name count. */
  207. uint
  208. name_count()
  209. {    return nt_count(the_nt);
  210. }
  211.  
  212. /* Check whether a name was created since a given count. */
  213. int
  214. name_is_since_count(const ref *pnref, uint cnt)
  215. {    return name_index_to_count(name_index(pnref)) >= cnt;
  216. }
  217.  
  218. /* Clean up the name table before a restore. */
  219. /* The count will be reset, and added subtables will be freed. */
  220. /* All we have to do is remove initial entries from the hash chains, */
  221. /* since we know they are linked in decreasing index order */
  222. /* (by sub-table, but not within each sub-table.) */
  223. /* (There will be some spurious non-zero entries in the subtable table, */
  224. /* but this doesn't matter since they will never be accessed.) */
  225. void
  226. name_restore(uint old_count)
  227. {    ushort *phash = &the_nt->hash[0];
  228.     uint old_sub = old_count & -nt_sub_size;
  229.     register uint i;
  230.     for ( i = 0; i < nt_hash_size; phash++, i++ )
  231.        {    register ushort *pnh = phash;
  232.         while ( *pnh >= old_sub )
  233.            {    if ( name_index_to_count(*pnh) < old_count )
  234.               pnh = &name_index_ptr(the_nt, *pnh)->next_index;
  235.             else
  236.                {
  237. #ifdef DEBUG
  238.                 if ( debug_c('n') | debug_c('U') )
  239.                   dprintf2("[n]restore remove name 0x%lx#%u\n",
  240.                        (ulong)name_index_ptr(the_nt, *pnh),
  241.                        (uint)*pnh);
  242. #endif
  243.                 *pnh = name_index_ptr(the_nt, *pnh)->next_index;
  244.                }
  245.            }
  246.        }
  247. }
  248.  
  249. /* ------ Internal procedures ------ */
  250.  
  251. /* Allocate the next sub-table. */
  252. private int
  253. name_alloc_sub(name_table *nt)
  254. {    name *sub = (name *)alloc(1, sizeof(name_sub_table), "name_alloc_sub");
  255.     if ( sub == 0 ) return e_VMerror;
  256.     memset(sub, 0, sizeof(name_sub_table));
  257.     nt->table[nt_count(nt) >> nt_log2_sub_size] = sub;
  258.     return 0;
  259. }
  260.