1 /* $NetBSD: symtab.c,v 1.6 2011/09/10 21:29:04 christos Exp $ */
2 /* Id: symtab.c,v 1.9 2010/11/24 15:12:29 tom Exp */
7 __RCSID("$NetBSD: symtab.c,v 1.6 2011/09/10 21:29:04 christos Exp $");
9 /* TABLE_SIZE is the number of entries in the symbol table. */
10 /* TABLE_SIZE must be a power of two. */
12 #define TABLE_SIZE 1024
14 static bucket
**symbol_table
= 0;
19 hash(const char *name
)
24 assert(name
&& *name
);
27 while ((c
= *++s
) != 0)
28 k
= (31 * k
+ c
) & (TABLE_SIZE
- 1);
34 make_bucket(const char *name
)
40 bp
= (bucket
*)MALLOC(sizeof(bucket
));
46 bp
->name
= MALLOC(strlen(name
) + 1);
50 bp
->value
= UNDEFINED
;
55 strcpy(bp
->name
, name
);
61 lookup(const char *name
)
65 bpp
= symbol_table
+ hash(name
);
70 if (strcmp(name
, bp
->name
) == 0)
76 *bpp
= bp
= make_bucket(name
);
77 last_symbol
->next
= bp
;
84 create_symbol_table(void)
89 symbol_table
= (bucket
**)MALLOC(TABLE_SIZE
* sizeof(bucket
*));
90 NO_SPACE(symbol_table
);
92 for (i
= 0; i
< TABLE_SIZE
; i
++)
95 bp
= make_bucket("error");
101 symbol_table
[hash("error")] = bp
;
105 free_symbol_table(void)
116 for (p
= first_symbol
; p
; p
= q
)