1 /* $NetBSD: hash.c,v 1.29 2020/09/01 21:11:31 rillig Exp $ */
4 * Copyright (c) 1988, 1989, 1990 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
36 * Copyright (c) 1988, 1989 by Adam de Boor
37 * Copyright (c) 1989 by Berkeley Softworks
38 * All rights reserved.
40 * This code is derived from software contributed to Berkeley by
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
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.
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
73 static char rcsid[] = "$NetBSD: hash.c,v 1.29 2020/09/01 21:11:31 rillig Exp $";
75 #include <sys/cdefs.h>
78 static char sccsid[] = "@(#)hash.c 8.1 (Berkeley) 6/6/93";
80 __RCSID("$NetBSD: hash.c,v 1.29 2020/09/01 21:11:31 rillig Exp $");
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.
95 * Forward references to local procedures that are used before they're
99 static void RebuildTable(Hash_Table *);
102 * The following defines the ratio of # entries to # buckets
103 * at which we rebuild the table to make it larger.
106 #define rebuildLimit 3
108 /* The hash function(s) */
111 /* The default: 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++; \
119 /* Sets up the hash table.
122 * t Structure to to hold the table.
123 * numBuckets How many buckets to create for starters. This
124 * number is rounded up to a power of two. If
125 * <= 0, a reasonable default is chosen. The
126 * table will grow in size later as needed.
129 Hash_InitTable(Hash_Table *t, int numBuckets)
132 struct Hash_Entry **hp;
135 * Round up the size to a power of two.
140 for (i = 2; i < numBuckets; i <<= 1)
146 t->bucketsMask = i - 1;
147 t->buckets = hp = bmake_malloc(sizeof(*hp) * i);
152 /* Removes everything from the hash table and frees up the memory space it
153 * occupied (except for the space in the Hash_Table structure). */
155 Hash_DeleteTable(Hash_Table *t)
157 struct Hash_Entry **hp, *h, *nexth = NULL;
160 for (hp = t->buckets, i = t->bucketsSize; --i >= 0;) {
161 for (h = *hp++; h != NULL; h = nexth) {
169 * Set up the hash table to cause memory faults on any future access
170 * attempts until re-initialization.
175 /* Searches the hash table for an entry corresponding to the key.
178 * t Hash table to search.
182 * Returns a pointer to the entry for key, or NULL if the table contains
183 * no entry for the key.
186 Hash_FindEntry(Hash_Table *t, const char *key)
193 if (t == NULL || t->buckets == NULL) {
199 #ifdef DEBUG_HASH_LOOKUP
201 fprintf(debug_file, "%s: %p h=%x key=%s\n", __func__,
204 for (e = t->buckets[h & t->bucketsMask]; e != NULL; e = e->next) {
206 if (e->namehash == h && strcmp(e->name, p) == 0)
209 if (chainlen > t->maxchain)
210 t->maxchain = chainlen;
214 /* Searches the hash table for an entry corresponding to the key.
215 * If no entry is found, then one is created.
218 * t Hash table to search.
220 * newPtr Filled with TRUE if new entry created,
224 Hash_CreateEntry(Hash_Table *t, const char *key, Boolean *newPtr)
231 struct Hash_Entry **hp;
234 * Hash the key. As a side effect, save the length (strlen) of the
235 * key in case we need to create the entry.
241 #ifdef DEBUG_HASH_LOOKUP
243 fprintf(debug_file, "%s: %p h=%x key=%s\n", __func__,
246 for (e = t->buckets[h & t->bucketsMask]; e != NULL; e = e->next) {
248 if (e->namehash == h && strcmp(e->name, p) == 0) {
254 if (chainlen > t->maxchain)
255 t->maxchain = chainlen;
260 * The desired entry isn't there. Before allocating a new entry,
261 * expand the table if necessary (and this changes the resulting
264 if (t->numEntries >= rebuildLimit * t->bucketsSize)
266 e = bmake_malloc(sizeof(*e) + keylen);
267 hp = &t->buckets[h & t->bucketsMask];
270 Hash_SetValue(e, NULL);
272 (void)strcpy(e->name, p);
280 /* Delete the given hash table entry and free memory associated with it. */
282 Hash_DeleteEntry(Hash_Table *t, Hash_Entry *e)
288 for (hp = &t->buckets[e->namehash & t->bucketsMask];
289 (p = *hp) != NULL; hp = &p->next) {
297 (void)write(2, "bad call to Hash_DeleteEntry\n", 29);
301 /* Sets things up for enumerating all entries in the hash table.
304 * t Table to be searched.
305 * searchPtr Area in which to keep state about search.
308 * The return value is the address of the first entry in
309 * the hash table, or NULL if the table is empty.
312 Hash_EnumFirst(Hash_Table *t, Hash_Search *searchPtr)
314 searchPtr->table = t;
315 searchPtr->nextBucket = 0;
316 searchPtr->entry = NULL;
317 return Hash_EnumNext(searchPtr);
320 /* Returns the next entry in the hash table, or NULL if the end of the table
324 * searchPtr Area used to keep state about search.
327 Hash_EnumNext(Hash_Search *searchPtr)
330 Hash_Table *t = searchPtr->table;
333 * The entry field points to the most recently returned
334 * entry, or is NULL if we are starting up. If not NULL, we have
335 * to start at the next one in the chain.
337 e = searchPtr->entry;
341 * If the chain ran out, or if we are starting up, we need to
342 * find the next nonempty chain.
345 if (searchPtr->nextBucket >= t->bucketsSize)
347 e = t->buckets[searchPtr->nextBucket++];
349 searchPtr->entry = e;
353 /* Makes a new hash table that is larger than the old one. The entire hash
354 * table is moved, so any bucket numbers from the old table become invalid. */
356 RebuildTable(Hash_Table *t)
358 Hash_Entry *e, *next = NULL, **hp, **xp;
364 oldsize = i = t->bucketsSize;
367 t->bucketsMask = mask = i - 1;
368 t->buckets = hp = bmake_malloc(sizeof(*hp) * i);
371 for (hp = oldhp, i = oldsize; --i >= 0;) {
372 for (e = *hp++; e != NULL; e = next) {
374 xp = &t->buckets[e->namehash & mask];
381 fprintf(debug_file, "%s: %p size=%d entries=%d maxchain=%d\n",
382 __func__, t, t->bucketsSize, t->numEntries, t->maxchain);
387 Hash_ForEach(Hash_Table *t, void (*action)(void *, void *), void *data)
392 for (e = Hash_EnumFirst(t, &search);
394 e = Hash_EnumNext(&search))
395 action(Hash_GetValue(e), data);
399 Hash_DebugStats(Hash_Table *t, const char *name)
402 fprintf(debug_file, "Hash_Table %s: size=%d numEntries=%d maxchain=%d\n",
403 name, t->bucketsSize, t->numEntries, t->maxchain);