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