hash.c revision 1.38 1 /* $NetBSD: hash.c,v 1.38 2020/10/03 23:16:28 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.38 2020/10/03 23:16:28 rillig Exp $");
83
84 /*
85 * Forward references to local procedures that are used before they're
86 * defined:
87 */
88
89 static void RebuildTable(Hash_Table *);
90
91 /*
92 * The following defines the ratio of # entries to # buckets
93 * at which we rebuild the table to make it larger.
94 */
95
96 #define rebuildLimit 3
97
98 /* This hash function matches Gosling's emacs. */
99 static unsigned int
100 hash(const char *key, size_t *out_keylen)
101 {
102 unsigned h = 0;
103 const char *p = key;
104 while (*p != '\0')
105 h = (h << 5) - h + (unsigned char)*p++;
106 if (out_keylen != NULL)
107 *out_keylen = (size_t)(p - key);
108 return h;
109 }
110
111 /* Sets up the hash table. */
112 void
113 Hash_InitTable(Hash_Table *t)
114 {
115 size_t n = 16, i;
116 struct Hash_Entry **hp;
117
118 t->numEntries = 0;
119 t->maxchain = 0;
120 t->bucketsSize = n;
121 t->bucketsMask = n - 1;
122 t->buckets = hp = bmake_malloc(sizeof(*hp) * n);
123 for (i = 0; i < n; i++)
124 hp[i] = NULL;
125 }
126
127 /* Removes everything from the hash table and frees up the memory space it
128 * occupied (except for the space in the Hash_Table structure). */
129 void
130 Hash_DeleteTable(Hash_Table *t)
131 {
132 struct Hash_Entry **hp, *h, *nexth = NULL;
133 int i;
134
135 for (hp = t->buckets, i = t->bucketsSize; --i >= 0;) {
136 for (h = *hp++; h != NULL; h = nexth) {
137 nexth = h->next;
138 free(h);
139 }
140 }
141 free(t->buckets);
142
143 /*
144 * Set up the hash table to cause memory faults on any future access
145 * attempts until re-initialization.
146 */
147 t->buckets = NULL;
148 }
149
150 /* Searches the hash table for an entry corresponding to the key.
151 *
152 * Input:
153 * t Hash table to search.
154 * key A hash key.
155 *
156 * Results:
157 * Returns a pointer to the entry for key, or NULL if the table contains
158 * no entry for the key.
159 */
160 Hash_Entry *
161 Hash_FindEntry(Hash_Table *t, const char *key)
162 {
163 Hash_Entry *e;
164 unsigned h;
165 int chainlen;
166
167 if (t == NULL || t->buckets == NULL)
168 return NULL;
169
170 h = hash(key, NULL);
171 chainlen = 0;
172 #ifdef DEBUG_HASH_LOOKUP
173 DEBUG4(HASH, "%s: %p h=%x key=%s\n", __func__, t, h, key);
174 #endif
175 for (e = t->buckets[h & t->bucketsMask]; e != NULL; e = e->next) {
176 chainlen++;
177 if (e->namehash == h && strcmp(e->name, key) == 0)
178 break;
179 }
180 if (chainlen > t->maxchain)
181 t->maxchain = chainlen;
182 return e;
183 }
184
185 void *
186 Hash_FindValue(Hash_Table *t, const char *key)
187 {
188 Hash_Entry *he = Hash_FindEntry(t, key);
189 return he != NULL ? he->value : NULL;
190 }
191
192 /* Searches the hash table for an entry corresponding to the key.
193 * If no entry is found, then one is created.
194 *
195 * Input:
196 * t Hash table to search.
197 * key A hash key.
198 * newPtr Filled with TRUE if new entry created,
199 * FALSE otherwise.
200 */
201 Hash_Entry *
202 Hash_CreateEntry(Hash_Table *t, const char *key, Boolean *newPtr)
203 {
204 Hash_Entry *e;
205 unsigned h;
206 size_t keylen;
207 int chainlen;
208 struct Hash_Entry **hp;
209
210 /*
211 * Hash the key. As a side effect, save the length (strlen) of the
212 * key in case we need to create the entry.
213 */
214 h = hash(key, &keylen);
215 chainlen = 0;
216 #ifdef DEBUG_HASH_LOOKUP
217 DEBUG4(HASH, "%s: %p h=%x key=%s\n", __func__, t, h, key);
218 #endif
219 for (e = t->buckets[h & t->bucketsMask]; e != NULL; e = e->next) {
220 chainlen++;
221 if (e->namehash == h && strcmp(e->name, key) == 0) {
222 if (newPtr != NULL)
223 *newPtr = FALSE;
224 break;
225 }
226 }
227 if (chainlen > t->maxchain)
228 t->maxchain = chainlen;
229 if (e)
230 return e;
231
232 /*
233 * The desired entry isn't there. Before allocating a new entry,
234 * expand the table if necessary (and this changes the resulting
235 * bucket chain).
236 */
237 if (t->numEntries >= rebuildLimit * t->bucketsSize)
238 RebuildTable(t);
239
240 e = bmake_malloc(sizeof(*e) + keylen);
241 hp = &t->buckets[h & t->bucketsMask];
242 e->next = *hp;
243 *hp = e;
244 Hash_SetValue(e, NULL);
245 e->namehash = h;
246 memcpy(e->name, key, keylen + 1);
247 t->numEntries++;
248
249 if (newPtr != NULL)
250 *newPtr = TRUE;
251 return e;
252 }
253
254 /* Delete the given hash table entry and free memory associated with it. */
255 void
256 Hash_DeleteEntry(Hash_Table *t, Hash_Entry *e)
257 {
258 Hash_Entry **hp, *p;
259
260 for (hp = &t->buckets[e->namehash & t->bucketsMask];
261 (p = *hp) != NULL; hp = &p->next) {
262 if (p == e) {
263 *hp = p->next;
264 free(p);
265 t->numEntries--;
266 return;
267 }
268 }
269 abort();
270 }
271
272 /* Sets things up for enumerating all entries in the hash table.
273 *
274 * Input:
275 * t Table to be searched.
276 * searchPtr Area in which to keep state about search.
277 *
278 * Results:
279 * The return value is the address of the first entry in
280 * the hash table, or NULL if the table is empty.
281 */
282 Hash_Entry *
283 Hash_EnumFirst(Hash_Table *t, Hash_Search *searchPtr)
284 {
285 searchPtr->table = t;
286 searchPtr->nextBucket = 0;
287 searchPtr->entry = NULL;
288 return Hash_EnumNext(searchPtr);
289 }
290
291 /* Returns the next entry in the hash table, or NULL if the end of the table
292 * is reached.
293 *
294 * Input:
295 * searchPtr Area used to keep state about search.
296 */
297 Hash_Entry *
298 Hash_EnumNext(Hash_Search *searchPtr)
299 {
300 Hash_Entry *e;
301 Hash_Table *t = searchPtr->table;
302
303 /*
304 * The entry field points to the most recently returned
305 * entry, or is NULL if we are starting up. If not NULL, we have
306 * to start at the next one in the chain.
307 */
308 e = searchPtr->entry;
309 if (e != NULL)
310 e = e->next;
311 /*
312 * If the chain ran out, or if we are starting up, we need to
313 * find the next nonempty chain.
314 */
315 while (e == NULL) {
316 if (searchPtr->nextBucket >= t->bucketsSize)
317 return NULL;
318 e = t->buckets[searchPtr->nextBucket++];
319 }
320 searchPtr->entry = e;
321 return e;
322 }
323
324 /* Makes a new hash table that is larger than the old one. The entire hash
325 * table is moved, so any bucket numbers from the old table become invalid. */
326 static void
327 RebuildTable(Hash_Table *t)
328 {
329 Hash_Entry *e, *next = NULL, **hp, **xp;
330 int i, mask;
331 Hash_Entry **oldhp;
332 int oldsize;
333
334 oldhp = t->buckets;
335 oldsize = i = t->bucketsSize;
336 i <<= 1;
337 t->bucketsSize = i;
338 t->bucketsMask = mask = i - 1;
339 t->buckets = hp = bmake_malloc(sizeof(*hp) * i);
340 while (--i >= 0)
341 *hp++ = NULL;
342 for (hp = oldhp, i = oldsize; --i >= 0;) {
343 for (e = *hp++; e != NULL; e = next) {
344 next = e->next;
345 xp = &t->buckets[e->namehash & mask];
346 e->next = *xp;
347 *xp = e;
348 }
349 }
350 free(oldhp);
351 DEBUG5(HASH, "%s: %p size=%d entries=%d maxchain=%d\n",
352 __func__, t, t->bucketsSize, t->numEntries, t->maxchain);
353 t->maxchain = 0;
354 }
355
356 void
357 Hash_ForEach(Hash_Table *t, void (*action)(void *, void *), void *data)
358 {
359 Hash_Search search;
360 Hash_Entry *e;
361
362 for (e = Hash_EnumFirst(t, &search);
363 e != NULL;
364 e = Hash_EnumNext(&search))
365 action(Hash_GetValue(e), data);
366 }
367
368 void
369 Hash_DebugStats(Hash_Table *t, const char *name)
370 {
371 DEBUG4(HASH, "Hash_Table %s: size=%d numEntries=%d maxchain=%d\n",
372 name, t->bucketsSize, t->numEntries, t->maxchain);
373 }
374