xref: /netbsd-src/usr.bin/xlint/lint2/hash.c (revision 8e6f7afb5bba2c07fd084c08249718961dfbc1d1)
1 /*	$NetBSD: hash.c,v 1.6 2001/05/28 12:40:38 lukem Exp $	*/
2 
3 /*
4  * Copyright (c) 1994, 1995 Jochen Pohl
5  * All Rights Reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Jochen Pohl for
18  *	The NetBSD Project.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 #ifndef lint
36 __RCSID("$NetBSD: hash.c,v 1.6 2001/05/28 12:40:38 lukem Exp $");
37 #endif
38 
39 /*
40  * XXX Really need a generalized hash table package
41  */
42 
43 #include <stddef.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <limits.h>
47 #include <err.h>
48 
49 #include "lint2.h"
50 
51 /* pointer to hash table, initialized in inithash() */
52 static	hte_t	**htab;
53 
54 static	int	hash(const char *);
55 
56 /*
57  * Initialize hash table.
58  */
59 void
60 _inithash(hte_t ***tablep)
61 {
62 
63 	if (tablep == NULL)
64 		tablep = &htab;
65 
66 	*tablep = xcalloc(HSHSIZ2, sizeof (hte_t *));
67 }
68 
69 /*
70  * Compute hash value from a string.
71  */
72 static int
73 hash(const char *s)
74 {
75 	u_int	v;
76 	const	u_char *us;
77 
78 	v = 0;
79 	for (us = (const u_char *)s; *us != '\0'; us++) {
80 		v = (v << sizeof (v)) + *us;
81 		v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v));
82 	}
83 	return (v % HSHSIZ2);
84 }
85 
86 /*
87  * Look for a hash table entry. If no hash table entry for the
88  * given name exists and mknew is set, create a new one.
89  */
90 hte_t *
91 _hsearch(hte_t **table, const char *s, int mknew)
92 {
93 	int	h;
94 	hte_t	*hte;
95 
96 	if (table == NULL)
97 		table = htab;
98 
99 	h = hash(s);
100 	for (hte = table[h]; hte != NULL; hte = hte->h_link) {
101 		if (strcmp(hte->h_name, s) == 0)
102 			break;
103 	}
104 
105 	if (hte != NULL || !mknew)
106 		return (hte);
107 
108 	/* create a new hte */
109 	hte = xmalloc(sizeof (hte_t));
110 	hte->h_name = xstrdup(s);
111 	hte->h_used = 0;
112 	hte->h_def = 0;
113 	hte->h_static = 0;
114 	hte->h_syms = NULL;
115 	hte->h_lsym = &hte->h_syms;
116 	hte->h_calls = NULL;
117 	hte->h_lcall = &hte->h_calls;
118 	hte->h_usyms = NULL;
119 	hte->h_lusym = &hte->h_usyms;
120 	hte->h_link = table[h];
121 	hte->h_hte = NULL;
122 	table[h] = hte;
123 
124 	return (hte);
125 }
126 
127 /*
128  * Call function f for each name in the hash table.
129  */
130 void
131 _forall(hte_t **table, void (*f)(hte_t *))
132 {
133 	int	i;
134 	hte_t	*hte;
135 
136 	if (table == NULL)
137 		table = htab;
138 
139 	for (i = 0; i < HSHSIZ2; i++) {
140 		for (hte = table[i]; hte != NULL; hte = hte->h_link)
141 			(*f)(hte);
142 	}
143 }
144 
145 /*
146  * Free all contents of the hash table that this module allocated.
147  */
148 void
149 _destroyhash(hte_t **table)
150 {
151 	int	i;
152 	hte_t	*hte, *nexthte;
153 
154 	if (table == NULL)
155 		err(1, "_destroyhash called on main hash table");
156 
157 	for (i = 0; i < HSHSIZ2; i++) {
158 		for (hte = table[i]; hte != NULL; hte = nexthte) {
159 			free((void *)hte->h_name);
160 			nexthte = hte->h_link;
161 			free(hte);
162 		}
163 	}
164 	free(table);
165 }
166