hash.c revision 1.40 1 /* $NetBSD: hash.c,v 1.40 2020/10/04 17:50:41 rillig Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
21 *
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
32 * SUCH DAMAGE.
33 */
34
35 /*
36 * Copyright (c) 1988, 1989 by Adam de Boor
37 * Copyright (c) 1989 by Berkeley Softworks
38 * All rights reserved.
39 *
40 * This code is derived from software contributed to Berkeley by
41 * Adam de Boor.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 */
71
72 /*
73 * This module contains routines to manipulate a hash table.
74 * See hash.h for a definition of the structure of the hash
75 * table. Hash tables grow automatically as the amount of
76 * information increases.
77 */
78
79 #include "make.h"
80
81 /* "@(#)hash.c 8.1 (Berkeley) 6/6/93" */
82 MAKE_RCSID("$NetBSD: hash.c,v 1.40 2020/10/04 17:50:41 rillig Exp $");
83
84 /*
85 * The ratio of # entries to # buckets at which we rebuild the table to
86 * make it larger.
87 */
88 #define rebuildLimit 3
89
90 /* This hash function matches Gosling's emacs. */
91 static unsigned int
92 hash(const char *key, size_t *out_keylen)
93 {
94 unsigned h = 0;
95 const char *p = key;
96 while (*p != '\0')
97 h = (h << 5) - h + (unsigned char)*p++;
98 if (out_keylen != NULL)
99 *out_keylen = (size_t)(p - key);
100 return h;
101 }
102
103 /* Sets up the hash table. */
104 void
105 Hash_InitTable(Hash_Table *t)
106 {
107 size_t n = 16, i;
108 struct Hash_Entry **hp;
109
110 t->numEntries = 0;
111 t->maxchain = 0;
112 t->bucketsSize = n;
113 t->bucketsMask = n - 1;
114 t->buckets = hp = bmake_malloc(sizeof(*hp) * n);
115 for (i = 0; i < n; i++)
116 hp[i] = NULL;
117 }
118
119 /* Removes everything from the hash table and frees up the memory space it
120 * occupied (except for the space in the Hash_Table structure). */
121 void
122 Hash_DeleteTable(Hash_Table *t)
123 {
124 struct Hash_Entry **hp, *h, *nexth = NULL;
125 int i;
126
127 for (hp = t->buckets, i = t->bucketsSize; --i >= 0;) {
128 for (h = *hp++; h != NULL; h = nexth) {
129 nexth = h->next;
130 free(h);
131 }
132 }
133 free(t->buckets);
134
135 /*
136 * Set up the hash table to cause memory faults on any future access
137 * attempts until re-initialization.
138 */
139 t->buckets = NULL;
140 }
141
142 /* Searches the hash table for an entry corresponding to the key.
143 *
144 * Input:
145 * t Hash table to search.
146 * key A hash key.
147 *
148 * Results:
149 * Returns a pointer to the entry for key, or NULL if the table contains
150 * no entry for the key.
151 */
152 Hash_Entry *
153 Hash_FindEntry(Hash_Table *t, const char *key)
154 {
155 Hash_Entry *e;
156 unsigned h;
157 int chainlen;
158
159 h = hash(key, NULL);
160 chainlen = 0;
161 #ifdef DEBUG_HASH_LOOKUP
162 DEBUG4(HASH, "%s: %p h=%x key=%s\n", __func__, t, h, key);
163 #endif
164 for (e = t->buckets[h & t->bucketsMask]; e != NULL; e = e->next) {
165 chainlen++;
166 if (e->namehash == h && strcmp(e->name, key) == 0)
167 break;
168 }
169 if (chainlen > t->maxchain)
170 t->maxchain = chainlen;
171 return e;
172 }
173
174 void *
175 Hash_FindValue(Hash_Table *t, const char *key)
176 {
177 Hash_Entry *he = Hash_FindEntry(t, key);
178 return he != NULL ? he->value : NULL;
179 }
180
181 /* Makes a new hash table that is larger than the old one. The entire hash
182 * table is moved, so any bucket numbers from the old table become invalid. */
183 static void
184 RebuildTable(Hash_Table *t)
185 {
186 Hash_Entry *e, *next = NULL, **hp, **xp;
187 int i, mask;
188 Hash_Entry **oldhp;
189 int oldsize;
190
191 oldhp = t->buckets;
192 oldsize = i = t->bucketsSize;
193 i <<= 1;
194 t->bucketsSize = i;
195 t->bucketsMask = mask = i - 1;
196 t->buckets = hp = bmake_malloc(sizeof(*hp) * i);
197 while (--i >= 0)
198 *hp++ = NULL;
199 for (hp = oldhp, i = oldsize; --i >= 0;) {
200 for (e = *hp++; e != NULL; e = next) {
201 next = e->next;
202 xp = &t->buckets[e->namehash & mask];
203 e->next = *xp;
204 *xp = e;
205 }
206 }
207 free(oldhp);
208 DEBUG5(HASH, "%s: %p size=%d entries=%d maxchain=%d\n",
209 __func__, t, t->bucketsSize, t->numEntries, t->maxchain);
210 t->maxchain = 0;
211 }
212
213 /* Searches the hash table for an entry corresponding to the key.
214 * If no entry is found, then one is created.
215 *
216 * Input:
217 * t Hash table to search.
218 * key A hash key.
219 * newPtr Filled with TRUE if new entry created,
220 * FALSE otherwise.
221 */
222 Hash_Entry *
223 Hash_CreateEntry(Hash_Table *t, const char *key, Boolean *newPtr)
224 {
225 Hash_Entry *e;
226 unsigned h;
227 size_t keylen;
228 int chainlen;
229 struct Hash_Entry **hp;
230
231 /*
232 * Hash the key. As a side effect, save the length (strlen) of the
233 * key in case we need to create the entry.
234 */
235 h = hash(key, &keylen);
236 chainlen = 0;
237 #ifdef DEBUG_HASH_LOOKUP
238 DEBUG4(HASH, "%s: %p h=%x key=%s\n", __func__, t, h, key);
239 #endif
240 for (e = t->buckets[h & t->bucketsMask]; e != NULL; e = e->next) {
241 chainlen++;
242 if (e->namehash == h && strcmp(e->name, key) == 0) {
243 if (newPtr != NULL)
244 *newPtr = FALSE;
245 break;
246 }
247 }
248 if (chainlen > t->maxchain)
249 t->maxchain = chainlen;
250 if (e)
251 return e;
252
253 /*
254 * The desired entry isn't there. Before allocating a new entry,
255 * expand the table if necessary (and this changes the resulting
256 * bucket chain).
257 */
258 if (t->numEntries >= rebuildLimit * t->bucketsSize)
259 RebuildTable(t);
260
261 e = bmake_malloc(sizeof(*e) + keylen);
262 hp = &t->buckets[h & t->bucketsMask];
263 e->next = *hp;
264 *hp = e;
265 Hash_SetValue(e, NULL);
266 e->namehash = h;
267 memcpy(e->name, key, keylen + 1);
268 t->numEntries++;
269
270 if (newPtr != NULL)
271 *newPtr = TRUE;
272 return e;
273 }
274
275 /* Delete the given hash table entry and free memory associated with it. */
276 void
277 Hash_DeleteEntry(Hash_Table *t, Hash_Entry *e)
278 {
279 Hash_Entry **hp, *p;
280
281 for (hp = &t->buckets[e->namehash & t->bucketsMask];
282 (p = *hp) != NULL; hp = &p->next) {
283 if (p == e) {
284 *hp = p->next;
285 free(p);
286 t->numEntries--;
287 return;
288 }
289 }
290 abort();
291 }
292
293 /* Sets things up for enumerating all entries in the hash table.
294 *
295 * Input:
296 * t Table to be searched.
297 * searchPtr Area in which to keep state about search.
298 *
299 * Results:
300 * The return value is the address of the first entry in
301 * the hash table, or NULL if the table is empty.
302 */
303 Hash_Entry *
304 Hash_EnumFirst(Hash_Table *t, Hash_Search *searchPtr)
305 {
306 searchPtr->table = t;
307 searchPtr->nextBucket = 0;
308 searchPtr->entry = NULL;
309 return Hash_EnumNext(searchPtr);
310 }
311
312 /* Returns the next entry in the hash table, or NULL if the end of the table
313 * is reached.
314 *
315 * Input:
316 * searchPtr Area used to keep state about search.
317 */
318 Hash_Entry *
319 Hash_EnumNext(Hash_Search *searchPtr)
320 {
321 Hash_Entry *e;
322 Hash_Table *t = searchPtr->table;
323
324 /*
325 * The entry field points to the most recently returned
326 * entry, or is NULL if we are starting up. If not NULL, we have
327 * to start at the next one in the chain.
328 */
329 e = searchPtr->entry;
330 if (e != NULL)
331 e = e->next;
332 /*
333 * If the chain ran out, or if we are starting up, we need to
334 * find the next nonempty chain.
335 */
336 while (e == NULL) {
337 if (searchPtr->nextBucket >= t->bucketsSize)
338 return NULL;
339 e = t->buckets[searchPtr->nextBucket++];
340 }
341 searchPtr->entry = e;
342 return e;
343 }
344
345 void
346 Hash_ForEach(Hash_Table *t, void (*action)(void *, void *), void *data)
347 {
348 Hash_Search search;
349 Hash_Entry *e;
350
351 for (e = Hash_EnumFirst(t, &search);
352 e != NULL;
353 e = Hash_EnumNext(&search))
354 action(Hash_GetValue(e), data);
355 }
356
357 void
358 Hash_DebugStats(Hash_Table *t, const char *name)
359 {
360 DEBUG4(HASH, "Hash_Table %s: size=%d numEntries=%d maxchain=%d\n",
361 name, t->bucketsSize, t->numEntries, t->maxchain);
362 }
363