1 /* $NetBSD: symtab.c,v 1.11 2006/05/24 18:06:58 christos Exp $ */
4 * Copyright (c) 1989 The Regents of the University of California.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 #if defined(__RCSID) && !defined(lint)
38 static char sccsid
[] = "@(#)symtab.c 5.3 (Berkeley) 6/1/90";
40 __RCSID("$NetBSD: symtab.c,v 1.11 2006/05/24 18:06:58 christos Exp $");
46 /* TABLE_SIZE is the number of entries in the symbol table. */
47 /* TABLE_SIZE must be a power of two. */
49 #define TABLE_SIZE 1024
55 static bucket
**symbol_table
;
57 static int hash(const char *);
61 hash(const char *name
)
66 assert(name
&& *name
);
69 while ((c
= *++s
) != '\0')
70 k
= (31*k
+ c
) & (TABLE_SIZE
- 1);
77 make_bucket(const char *name
)
82 bp
= (bucket
*) MALLOC(sizeof(bucket
));
83 if (bp
== 0) no_space();
86 bp
->name
= strdup(name
);
87 if (bp
->name
== 0) no_space();
89 bp
->value
= UNDEFINED
;
104 bpp
= symbol_table
+ hash(name
);
109 if (strcmp(name
, bp
->name
) == 0) return (bp
);
114 *bpp
= bp
= make_bucket(name
);
115 last_symbol
->next
= bp
;
123 create_symbol_table(void)
128 symbol_table
= (bucket
**) MALLOC(TABLE_SIZE
*sizeof(bucket
*));
129 if (symbol_table
== 0) no_space();
130 for (i
= 0; i
< TABLE_SIZE
; i
++)
133 bp
= make_bucket("error");
139 symbol_table
[hash("error")] = bp
;
144 free_symbol_table(void)
156 for (p
= first_symbol
; p
; p
= q
)