1 /* $NetBSD: symtab.c,v 1.8 2015/01/03 23:22:52 christos Exp $ */
3 /* Id: symtab.c,v 1.11 2014/03/26 00:17:09 Tom.Shields Exp */
8 __RCSID("$NetBSD: symtab.c,v 1.8 2015/01/03 23:22:52 christos Exp $");
10 /* TABLE_SIZE is the number of entries in the symbol table. */
11 /* TABLE_SIZE must be a power of two. */
13 #define TABLE_SIZE 1024
15 static bucket
**symbol_table
= 0;
20 hash(const char *name
)
25 assert(name
&& *name
);
28 while ((c
= *++s
) != 0)
29 k
= (31 * k
+ c
) & (TABLE_SIZE
- 1);
35 make_bucket(const char *name
)
41 bp
= TMALLOC(bucket
, 1);
47 bp
->name
= TMALLOC(char, strlen(name
) + 1);
51 bp
->value
= UNDEFINED
;
62 strcpy(bp
->name
, name
);
68 lookup(const char *name
)
72 bpp
= symbol_table
+ hash(name
);
77 if (strcmp(name
, bp
->name
) == 0)
83 *bpp
= bp
= make_bucket(name
);
84 last_symbol
->next
= bp
;
91 create_symbol_table(void)
96 symbol_table
= TMALLOC(bucket
*, TABLE_SIZE
);
97 NO_SPACE(symbol_table
);
99 for (i
= 0; i
< TABLE_SIZE
; i
++)
102 bp
= make_bucket("error");
108 symbol_table
[hash("error")] = bp
;
112 free_symbol_table(void)
123 for (p
= first_symbol
; p
; p
= q
)