hash.c revision 1.23 1 /* $NetBSD: hash.c,v 1.23 2020/07/18 21:37:38 sjg 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 #ifndef MAKE_NATIVE
73 static char rcsid[] = "$NetBSD: hash.c,v 1.23 2020/07/18 21:37:38 sjg Exp $";
74 #else
75 #include <sys/cdefs.h>
76 #ifndef lint
77 #if 0
78 static char sccsid[] = "@(#)hash.c 8.1 (Berkeley) 6/6/93";
79 #else
80 __RCSID("$NetBSD: hash.c,v 1.23 2020/07/18 21:37:38 sjg Exp $");
81 #endif
82 #endif /* not lint */
83 #endif
84
85 /* hash.c --
86 *
87 * This module contains routines to manipulate a hash table.
88 * See hash.h for a definition of the structure of the hash
89 * table. Hash tables grow automatically as the amount of
90 * information increases.
91 */
92 #include "sprite.h"
93 #include "make.h"
94 #include "hash.h"
95
96 /*
97 * Forward references to local procedures that are used before they're
98 * defined:
99 */
100
101 static void RebuildTable(Hash_Table *);
102
103 /*
104 * The following defines the ratio of # entries to # buckets
105 * at which we rebuild the table to make it larger.
106 */
107
108 #define rebuildLimit 3
109
110 /* The hash function(s) */
111 /* This one matches Gosling's emacs */
112 #define HASH(h, key, p) do { \
113 for (h = 0, p = key; *p;) \
114 h = (h << 5) - h + *p++; \
115 } while (0)
116
117
118 /*
119 *---------------------------------------------------------
120 *
121 * Hash_InitTable --
122 *
123 * This routine just sets up the hash table.
124 *
125 * Input:
126 * t Structure to to hold table.
127 * numBuckets How many buckets to create for starters. This
128 * number is rounded up to a power of two. If
129 * <= 0, a reasonable default is chosen. The
130 * table will grow in size later as needed.
131 *
132 * Results:
133 * None.
134 *
135 * Side Effects:
136 * Memory is allocated for the initial bucket area.
137 *
138 *---------------------------------------------------------
139 */
140
141 void
142 Hash_InitTable(Hash_Table *t, int numBuckets)
143 {
144 int i;
145 struct Hash_Entry **hp;
146
147 /*
148 * Round up the size to a power of two.
149 */
150 if (numBuckets <= 0)
151 i = 16;
152 else {
153 for (i = 2; i < numBuckets; i <<= 1)
154 continue;
155 }
156 t->numEntries = 0;
157 t->maxlen = 0;
158 t->size = i;
159 t->mask = i - 1;
160 t->bucketPtr = hp = bmake_malloc(sizeof(*hp) * i);
161 while (--i >= 0)
162 *hp++ = NULL;
163 }
164
165 /*
166 *---------------------------------------------------------
167 *
168 * Hash_DeleteTable --
169 *
170 * This routine removes everything from a hash table
171 * and frees up the memory space it occupied (except for
172 * the space in the Hash_Table structure).
173 *
174 * Results:
175 * None.
176 *
177 * Side Effects:
178 * Lots of memory is freed up.
179 *
180 *---------------------------------------------------------
181 */
182
183 void
184 Hash_DeleteTable(Hash_Table *t)
185 {
186 struct Hash_Entry **hp, *h, *nexth = NULL;
187 int i;
188
189 for (hp = t->bucketPtr, i = t->size; --i >= 0;) {
190 for (h = *hp++; h != NULL; h = nexth) {
191 nexth = h->next;
192 free(h);
193 }
194 }
195 free(t->bucketPtr);
196
197 /*
198 * Set up the hash table to cause memory faults on any future access
199 * attempts until re-initialization.
200 */
201 t->bucketPtr = NULL;
202 }
203
204 /*
205 *---------------------------------------------------------
206 *
207 * Hash_FindEntry --
208 *
209 * Searches a hash table for an entry corresponding to key.
210 *
211 * Input:
212 * t Hash table to search.
213 * key A hash key.
214 *
215 * Results:
216 * The return value is a pointer to the entry for key,
217 * if key was present in the table. If key was not
218 * present, NULL is returned.
219 *
220 * Side Effects:
221 * None.
222 *
223 *---------------------------------------------------------
224 */
225
226 Hash_Entry *
227 Hash_FindEntry(Hash_Table *t, const char *key)
228 {
229 Hash_Entry *e;
230 unsigned h;
231 const char *p;
232 int chainlen;
233
234 if (t == NULL || t->bucketPtr == NULL) {
235 return NULL;
236 }
237 HASH(h, key, p);
238 p = key;
239 if (DEBUG(HASH))
240 fprintf(debug_file, "%s: %p h=%x key=%s\n", __func__,
241 t, h, key);
242 chainlen = 0;
243 for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next) {
244 chainlen++;
245 if (e->namehash == h && strcmp(e->name, p) == 0)
246 break;
247 }
248 if (chainlen > t->maxlen)
249 t->maxlen = chainlen;
250 return e;
251 }
252
253 /*
254 *---------------------------------------------------------
255 *
256 * Hash_CreateEntry --
257 *
258 * Searches a hash table for an entry corresponding to
259 * key. If no entry is found, then one is created.
260 *
261 * Input:
262 * t Hash table to search.
263 * key A hash key.
264 * newPtr Filled in with TRUE if new entry created,
265 * FALSE otherwise.
266 *
267 * Results:
268 * The return value is a pointer to the entry. If *newPtr
269 * isn't NULL, then *newPtr is filled in with TRUE if a
270 * new entry was created, and FALSE if an entry already existed
271 * with the given key.
272 *
273 * Side Effects:
274 * Memory may be allocated, and the hash buckets may be modified.
275 *---------------------------------------------------------
276 */
277
278 Hash_Entry *
279 Hash_CreateEntry(Hash_Table *t, const char *key, Boolean *newPtr)
280 {
281 Hash_Entry *e;
282 unsigned h;
283 const char *p;
284 int keylen;
285 int chainlen;
286 struct Hash_Entry **hp;
287
288 /*
289 * Hash the key. As a side effect, save the length (strlen) of the
290 * key in case we need to create the entry.
291 */
292 HASH(h, key, p);
293 keylen = p - key;
294 p = key;
295 if (DEBUG(HASH))
296 fprintf(debug_file, "%s: %p h=%x key=%s\n", __func__,
297 t, h, key);
298 chainlen = 0;
299 for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next) {
300 chainlen++;
301 if (e->namehash == h && strcmp(e->name, p) == 0) {
302 if (newPtr != NULL)
303 *newPtr = FALSE;
304 break;
305 }
306 }
307 if (chainlen > t->maxlen)
308 t->maxlen = chainlen;
309 if (e)
310 return e;
311
312 /*
313 * The desired entry isn't there. Before allocating a new entry,
314 * expand the table if necessary (and this changes the resulting
315 * bucket chain).
316 */
317 if (t->numEntries >= rebuildLimit * t->size)
318 RebuildTable(t);
319 e = bmake_malloc(sizeof(*e) + keylen);
320 hp = &t->bucketPtr[h & t->mask];
321 e->next = *hp;
322 *hp = e;
323 Hash_SetValue(e, NULL);
324 e->namehash = h;
325 (void)strcpy(e->name, p);
326 t->numEntries++;
327
328 if (newPtr != NULL)
329 *newPtr = TRUE;
330 return e;
331 }
332
333 /*
334 *---------------------------------------------------------
335 *
336 * Hash_DeleteEntry --
337 *
338 * Delete the given hash table entry and free memory associated with
339 * it.
340 *
341 * Results:
342 * None.
343 *
344 * Side Effects:
345 * Hash chain that entry lives in is modified and memory is freed.
346 *
347 *---------------------------------------------------------
348 */
349
350 void
351 Hash_DeleteEntry(Hash_Table *t, Hash_Entry *e)
352 {
353 Hash_Entry **hp, *p;
354
355 if (e == NULL)
356 return;
357 for (hp = &t->bucketPtr[e->namehash & t->mask];
358 (p = *hp) != NULL; hp = &p->next) {
359 if (p == e) {
360 *hp = p->next;
361 free(p);
362 t->numEntries--;
363 return;
364 }
365 }
366 (void)write(2, "bad call to Hash_DeleteEntry\n", 29);
367 abort();
368 }
369
370 /*
371 *---------------------------------------------------------
372 *
373 * Hash_EnumFirst --
374 * This procedure sets things up for a complete search
375 * of all entries recorded in the hash table.
376 *
377 * Input:
378 * t Table to be searched.
379 * searchPtr Area in which to keep state about search.
380 *
381 * Results:
382 * The return value is the address of the first entry in
383 * the hash table, or NULL if the table is empty.
384 *
385 * Side Effects:
386 * The information in searchPtr is initialized so that successive
387 * calls to Hash_Next will return successive HashEntry's
388 * from the table.
389 *
390 *---------------------------------------------------------
391 */
392
393 Hash_Entry *
394 Hash_EnumFirst(Hash_Table *t, Hash_Search *searchPtr)
395 {
396 searchPtr->tablePtr = t;
397 searchPtr->nextIndex = 0;
398 searchPtr->hashEntryPtr = NULL;
399 return Hash_EnumNext(searchPtr);
400 }
401
402 /*
403 *---------------------------------------------------------
404 *
405 * Hash_EnumNext --
406 * This procedure returns successive entries in the hash table.
407 *
408 * Input:
409 * searchPtr Area used to keep state about search.
410 *
411 * Results:
412 * The return value is a pointer to the next HashEntry
413 * in the table, or NULL when the end of the table is
414 * reached.
415 *
416 * Side Effects:
417 * The information in searchPtr is modified to advance to the
418 * next entry.
419 *
420 *---------------------------------------------------------
421 */
422
423 Hash_Entry *
424 Hash_EnumNext(Hash_Search *searchPtr)
425 {
426 Hash_Entry *e;
427 Hash_Table *t = searchPtr->tablePtr;
428
429 /*
430 * The hashEntryPtr field points to the most recently returned
431 * entry, or is nil if we are starting up. If not nil, we have
432 * to start at the next one in the chain.
433 */
434 e = searchPtr->hashEntryPtr;
435 if (e != NULL)
436 e = e->next;
437 /*
438 * If the chain ran out, or if we are starting up, we need to
439 * find the next nonempty chain.
440 */
441 while (e == NULL) {
442 if (searchPtr->nextIndex >= t->size)
443 return NULL;
444 e = t->bucketPtr[searchPtr->nextIndex++];
445 }
446 searchPtr->hashEntryPtr = e;
447 return e;
448 }
449
450 /*
451 *---------------------------------------------------------
452 *
453 * RebuildTable --
454 * This local routine makes a new hash table that
455 * is larger than the old one.
456 *
457 * Results:
458 * None.
459 *
460 * Side Effects:
461 * The entire hash table is moved, so any bucket numbers
462 * from the old table are invalid.
463 *
464 *---------------------------------------------------------
465 */
466
467 static void
468 RebuildTable(Hash_Table *t)
469 {
470 Hash_Entry *e, *next = NULL, **hp, **xp;
471 int i, mask;
472 Hash_Entry **oldhp;
473 int oldsize;
474
475 oldhp = t->bucketPtr;
476 oldsize = i = t->size;
477 i <<= 1;
478 t->size = i;
479 t->mask = mask = i - 1;
480 t->bucketPtr = hp = bmake_malloc(sizeof(*hp) * i);
481 while (--i >= 0)
482 *hp++ = NULL;
483 for (hp = oldhp, i = oldsize; --i >= 0;) {
484 for (e = *hp++; e != NULL; e = next) {
485 next = e->next;
486 xp = &t->bucketPtr[e->namehash & mask];
487 e->next = *xp;
488 *xp = e;
489 }
490 }
491 free(oldhp);
492 if (DEBUG(HASH))
493 fprintf(debug_file, "%s: %p size=%d entries=%d maxlen=%d\n",
494 __func__, t, t->size, t->numEntries, t->maxlen);
495 t->maxlen = 0;
496 }
497
498 void Hash_ForEach(Hash_Table *t, void (*action)(void *, void *), void *data)
499 {
500 Hash_Search search;
501 Hash_Entry *e;
502
503 for (e = Hash_EnumFirst(t, &search);
504 e != NULL;
505 e = Hash_EnumNext(&search))
506 action(Hash_GetValue(e), data);
507 }
508