1 /*
2  */
3 
4 #include "graph.h"
5 
6 /******************************************************************************
7 Symbol table management
8 ******************************************************************************/
9 
10 #define	MAXHASH		32749
11 
12 struct symtab **hashtable ;
13 
symtab_init(void)14 void symtab_init (void)
15 {
16     int i ;
17 
18     hashtable = mobj_alloc (hashmobj, MAXHASH) ;
19     for (i = 0 ; i < MAXHASH ; i++)
20 	hashtable [i] = NULL ;
21 }
22 
23 /*
24  * Computes the hash value for a name
25  *
26  * Input :
27  *   name : name
28  * Output :
29  *   return value : hash value
30  *
31  * Note : the hash value is the concatenation of the 2 lowest bits
32  *   of each byte.
33  *
34  * History :
35  *   2004/03/30 : pda/jean : design
36  */
37 
hash(char * name)38 static int hash (char *name)
39 {
40     unsigned int s ;
41 
42     s = 0 ;
43     while (*name != '\0')
44 	s = (s << 2) | (*name++ & 0x3) ;
45     return s % MAXHASH ;
46 }
47 
48 /*
49  * Lookup a name in the symbol table, and returns the name found, which
50  * is garanteed to be unique.
51  *
52  * Input :
53  *   name : name
54  * Output :
55  *   return value : name
56  *
57  * Note : the memory pointed to by the parameter "name" can be
58  *   deallocated by the caller.
59  *
60  * History :
61  *   2004/03/30 : pda/jean : design
62  */
63 
symtab_lookup(char * name)64 struct symtab *symtab_lookup (char *name)
65 {
66     int h ;
67     struct symtab *p ;
68 
69     h = hash (name) ;
70     p = hashtable [h] ;
71     while (p != NULL)
72     {
73 	if (strcmp (p->name, name) == 0)
74 	    break ;
75 	p = p->next ;
76     }
77     return p ;
78 }
79 
symtab_get(char * name)80 struct symtab *symtab_get (char *name)
81 {
82     int h ;
83     struct symtab *p ;
84 
85     p = symtab_lookup (name) ;
86     if (p == NULL)
87     {
88 	p = mobj_alloc (symmobj, 1) ;
89 	p->name = mobj_alloc (strmobj, strlen (name) + 1) ;
90 	strcpy (p->name, name) ;
91 
92 	h = hash (name) ;
93 	p->next = hashtable [h] ;
94 	hashtable [h] = p ;
95     }
96     return p ;
97 }
98