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