hash.c revision 1.29 1 1.29 rillig /* $NetBSD: hash.c,v 1.29 2020/09/01 21:11:31 rillig Exp $ */
2 1.5 christos
3 1.1 cgd /*
4 1.1 cgd * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5 1.12 agc * All rights reserved.
6 1.12 agc *
7 1.12 agc * This code is derived from software contributed to Berkeley by
8 1.12 agc * Adam de Boor.
9 1.12 agc *
10 1.12 agc * Redistribution and use in source and binary forms, with or without
11 1.12 agc * modification, are permitted provided that the following conditions
12 1.12 agc * are met:
13 1.12 agc * 1. Redistributions of source code must retain the above copyright
14 1.12 agc * notice, this list of conditions and the following disclaimer.
15 1.12 agc * 2. Redistributions in binary form must reproduce the above copyright
16 1.12 agc * notice, this list of conditions and the following disclaimer in the
17 1.12 agc * documentation and/or other materials provided with the distribution.
18 1.12 agc * 3. Neither the name of the University nor the names of its contributors
19 1.12 agc * may be used to endorse or promote products derived from this software
20 1.12 agc * without specific prior written permission.
21 1.12 agc *
22 1.12 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.12 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.12 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.12 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.12 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.12 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.12 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.12 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.12 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.12 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.12 agc * SUCH DAMAGE.
33 1.12 agc */
34 1.12 agc
35 1.12 agc /*
36 1.1 cgd * Copyright (c) 1988, 1989 by Adam de Boor
37 1.1 cgd * Copyright (c) 1989 by Berkeley Softworks
38 1.1 cgd * All rights reserved.
39 1.1 cgd *
40 1.1 cgd * This code is derived from software contributed to Berkeley by
41 1.1 cgd * Adam de Boor.
42 1.1 cgd *
43 1.1 cgd * Redistribution and use in source and binary forms, with or without
44 1.1 cgd * modification, are permitted provided that the following conditions
45 1.1 cgd * are met:
46 1.1 cgd * 1. Redistributions of source code must retain the above copyright
47 1.1 cgd * notice, this list of conditions and the following disclaimer.
48 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
49 1.1 cgd * notice, this list of conditions and the following disclaimer in the
50 1.1 cgd * documentation and/or other materials provided with the distribution.
51 1.1 cgd * 3. All advertising materials mentioning features or use of this software
52 1.1 cgd * must display the following acknowledgement:
53 1.1 cgd * This product includes software developed by the University of
54 1.1 cgd * California, Berkeley and its contributors.
55 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
56 1.1 cgd * may be used to endorse or promote products derived from this software
57 1.1 cgd * without specific prior written permission.
58 1.1 cgd *
59 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 1.1 cgd * SUCH DAMAGE.
70 1.1 cgd */
71 1.1 cgd
72 1.13 ross #ifndef MAKE_NATIVE
73 1.29 rillig static char rcsid[] = "$NetBSD: hash.c,v 1.29 2020/09/01 21:11:31 rillig Exp $";
74 1.8 lukem #else
75 1.7 christos #include <sys/cdefs.h>
76 1.1 cgd #ifndef lint
77 1.5 christos #if 0
78 1.6 christos static char sccsid[] = "@(#)hash.c 8.1 (Berkeley) 6/6/93";
79 1.5 christos #else
80 1.29 rillig __RCSID("$NetBSD: hash.c,v 1.29 2020/09/01 21:11:31 rillig Exp $");
81 1.5 christos #endif
82 1.1 cgd #endif /* not lint */
83 1.8 lukem #endif
84 1.1 cgd
85 1.1 cgd /* hash.c --
86 1.1 cgd *
87 1.1 cgd * This module contains routines to manipulate a hash table.
88 1.1 cgd * See hash.h for a definition of the structure of the hash
89 1.1 cgd * table. Hash tables grow automatically as the amount of
90 1.1 cgd * information increases.
91 1.1 cgd */
92 1.4 cgd #include "make.h"
93 1.1 cgd
94 1.1 cgd /*
95 1.1 cgd * Forward references to local procedures that are used before they're
96 1.1 cgd * defined:
97 1.1 cgd */
98 1.1 cgd
99 1.10 wiz static void RebuildTable(Hash_Table *);
100 1.1 cgd
101 1.6 christos /*
102 1.1 cgd * The following defines the ratio of # entries to # buckets
103 1.1 cgd * at which we rebuild the table to make it larger.
104 1.1 cgd */
105 1.1 cgd
106 1.9 mycroft #define rebuildLimit 3
107 1.1 cgd
108 1.23 sjg /* The hash function(s) */
109 1.25 sjg
110 1.25 sjg #ifndef HASH
111 1.25 sjg /* The default: this one matches Gosling's emacs */
112 1.23 sjg #define HASH(h, key, p) do { \
113 1.23 sjg for (h = 0, p = key; *p;) \
114 1.23 sjg h = (h << 5) - h + *p++; \
115 1.23 sjg } while (0)
116 1.23 sjg
117 1.25 sjg #endif
118 1.23 sjg
119 1.28 rillig /* Sets up the hash table.
120 1.1 cgd *
121 1.10 wiz * Input:
122 1.28 rillig * t Structure to to hold the table.
123 1.10 wiz * numBuckets How many buckets to create for starters. This
124 1.10 wiz * number is rounded up to a power of two. If
125 1.10 wiz * <= 0, a reasonable default is chosen. The
126 1.10 wiz * table will grow in size later as needed.
127 1.1 cgd */
128 1.1 cgd void
129 1.10 wiz Hash_InitTable(Hash_Table *t, int numBuckets)
130 1.1 cgd {
131 1.10 wiz int i;
132 1.10 wiz struct Hash_Entry **hp;
133 1.1 cgd
134 1.1 cgd /*
135 1.6 christos * Round up the size to a power of two.
136 1.1 cgd */
137 1.1 cgd if (numBuckets <= 0)
138 1.1 cgd i = 16;
139 1.1 cgd else {
140 1.1 cgd for (i = 2; i < numBuckets; i <<= 1)
141 1.4 cgd continue;
142 1.1 cgd }
143 1.1 cgd t->numEntries = 0;
144 1.25 sjg t->maxchain = 0;
145 1.29 rillig t->bucketsSize = i;
146 1.29 rillig t->bucketsMask = i - 1;
147 1.29 rillig t->buckets = hp = bmake_malloc(sizeof(*hp) * i);
148 1.1 cgd while (--i >= 0)
149 1.1 cgd *hp++ = NULL;
150 1.1 cgd }
151 1.1 cgd
152 1.28 rillig /* Removes everything from the hash table and frees up the memory space it
153 1.28 rillig * occupied (except for the space in the Hash_Table structure). */
154 1.1 cgd void
155 1.10 wiz Hash_DeleteTable(Hash_Table *t)
156 1.1 cgd {
157 1.10 wiz struct Hash_Entry **hp, *h, *nexth = NULL;
158 1.10 wiz int i;
159 1.1 cgd
160 1.29 rillig for (hp = t->buckets, i = t->bucketsSize; --i >= 0;) {
161 1.1 cgd for (h = *hp++; h != NULL; h = nexth) {
162 1.1 cgd nexth = h->next;
163 1.16 christos free(h);
164 1.1 cgd }
165 1.1 cgd }
166 1.29 rillig free(t->buckets);
167 1.1 cgd
168 1.1 cgd /*
169 1.1 cgd * Set up the hash table to cause memory faults on any future access
170 1.6 christos * attempts until re-initialization.
171 1.1 cgd */
172 1.29 rillig t->buckets = NULL;
173 1.1 cgd }
174 1.1 cgd
175 1.28 rillig /* Searches the hash table for an entry corresponding to the key.
176 1.1 cgd *
177 1.10 wiz * Input:
178 1.10 wiz * t Hash table to search.
179 1.10 wiz * key A hash key.
180 1.10 wiz *
181 1.1 cgd * Results:
182 1.28 rillig * Returns a pointer to the entry for key, or NULL if the table contains
183 1.28 rillig * no entry for the key.
184 1.1 cgd */
185 1.1 cgd Hash_Entry *
186 1.11 christos Hash_FindEntry(Hash_Table *t, const char *key)
187 1.1 cgd {
188 1.10 wiz Hash_Entry *e;
189 1.10 wiz unsigned h;
190 1.11 christos const char *p;
191 1.23 sjg int chainlen;
192 1.1 cgd
193 1.29 rillig if (t == NULL || t->buckets == NULL) {
194 1.20 sjg return NULL;
195 1.20 sjg }
196 1.23 sjg HASH(h, key, p);
197 1.1 cgd p = key;
198 1.25 sjg chainlen = 0;
199 1.25 sjg #ifdef DEBUG_HASH_LOOKUP
200 1.23 sjg if (DEBUG(HASH))
201 1.23 sjg fprintf(debug_file, "%s: %p h=%x key=%s\n", __func__,
202 1.23 sjg t, h, key);
203 1.25 sjg #endif
204 1.29 rillig for (e = t->buckets[h & t->bucketsMask]; e != NULL; e = e->next) {
205 1.23 sjg chainlen++;
206 1.24 riastrad if (e->namehash == h && strcmp(e->name, p) == 0)
207 1.23 sjg break;
208 1.23 sjg }
209 1.25 sjg if (chainlen > t->maxchain)
210 1.25 sjg t->maxchain = chainlen;
211 1.23 sjg return e;
212 1.1 cgd }
213 1.1 cgd
214 1.28 rillig /* Searches the hash table for an entry corresponding to the key.
215 1.28 rillig * If no entry is found, then one is created.
216 1.1 cgd *
217 1.10 wiz * Input:
218 1.10 wiz * t Hash table to search.
219 1.10 wiz * key A hash key.
220 1.28 rillig * newPtr Filled with TRUE if new entry created,
221 1.10 wiz * FALSE otherwise.
222 1.1 cgd */
223 1.1 cgd Hash_Entry *
224 1.11 christos Hash_CreateEntry(Hash_Table *t, const char *key, Boolean *newPtr)
225 1.1 cgd {
226 1.10 wiz Hash_Entry *e;
227 1.10 wiz unsigned h;
228 1.11 christos const char *p;
229 1.1 cgd int keylen;
230 1.23 sjg int chainlen;
231 1.1 cgd struct Hash_Entry **hp;
232 1.1 cgd
233 1.1 cgd /*
234 1.1 cgd * Hash the key. As a side effect, save the length (strlen) of the
235 1.1 cgd * key in case we need to create the entry.
236 1.1 cgd */
237 1.23 sjg HASH(h, key, p);
238 1.1 cgd keylen = p - key;
239 1.1 cgd p = key;
240 1.25 sjg chainlen = 0;
241 1.25 sjg #ifdef DEBUG_HASH_LOOKUP
242 1.23 sjg if (DEBUG(HASH))
243 1.23 sjg fprintf(debug_file, "%s: %p h=%x key=%s\n", __func__,
244 1.23 sjg t, h, key);
245 1.25 sjg #endif
246 1.29 rillig for (e = t->buckets[h & t->bucketsMask]; e != NULL; e = e->next) {
247 1.23 sjg chainlen++;
248 1.1 cgd if (e->namehash == h && strcmp(e->name, p) == 0) {
249 1.1 cgd if (newPtr != NULL)
250 1.1 cgd *newPtr = FALSE;
251 1.23 sjg break;
252 1.1 cgd }
253 1.1 cgd }
254 1.25 sjg if (chainlen > t->maxchain)
255 1.25 sjg t->maxchain = chainlen;
256 1.23 sjg if (e)
257 1.23 sjg return e;
258 1.1 cgd
259 1.1 cgd /*
260 1.1 cgd * The desired entry isn't there. Before allocating a new entry,
261 1.1 cgd * expand the table if necessary (and this changes the resulting
262 1.6 christos * bucket chain).
263 1.1 cgd */
264 1.29 rillig if (t->numEntries >= rebuildLimit * t->bucketsSize)
265 1.1 cgd RebuildTable(t);
266 1.17 joerg e = bmake_malloc(sizeof(*e) + keylen);
267 1.29 rillig hp = &t->buckets[h & t->bucketsMask];
268 1.1 cgd e->next = *hp;
269 1.1 cgd *hp = e;
270 1.19 dsl Hash_SetValue(e, NULL);
271 1.1 cgd e->namehash = h;
272 1.15 christos (void)strcpy(e->name, p);
273 1.1 cgd t->numEntries++;
274 1.1 cgd
275 1.1 cgd if (newPtr != NULL)
276 1.1 cgd *newPtr = TRUE;
277 1.21 rillig return e;
278 1.1 cgd }
279 1.1 cgd
280 1.28 rillig /* Delete the given hash table entry and free memory associated with it. */
281 1.1 cgd void
282 1.10 wiz Hash_DeleteEntry(Hash_Table *t, Hash_Entry *e)
283 1.1 cgd {
284 1.10 wiz Hash_Entry **hp, *p;
285 1.1 cgd
286 1.1 cgd if (e == NULL)
287 1.1 cgd return;
288 1.29 rillig for (hp = &t->buckets[e->namehash & t->bucketsMask];
289 1.1 cgd (p = *hp) != NULL; hp = &p->next) {
290 1.1 cgd if (p == e) {
291 1.1 cgd *hp = p->next;
292 1.16 christos free(p);
293 1.1 cgd t->numEntries--;
294 1.1 cgd return;
295 1.1 cgd }
296 1.1 cgd }
297 1.15 christos (void)write(2, "bad call to Hash_DeleteEntry\n", 29);
298 1.1 cgd abort();
299 1.1 cgd }
300 1.1 cgd
301 1.28 rillig /* Sets things up for enumerating all entries in the hash table.
302 1.1 cgd *
303 1.10 wiz * Input:
304 1.10 wiz * t Table to be searched.
305 1.10 wiz * searchPtr Area in which to keep state about search.
306 1.10 wiz *
307 1.6 christos * Results:
308 1.1 cgd * The return value is the address of the first entry in
309 1.1 cgd * the hash table, or NULL if the table is empty.
310 1.1 cgd */
311 1.1 cgd Hash_Entry *
312 1.10 wiz Hash_EnumFirst(Hash_Table *t, Hash_Search *searchPtr)
313 1.1 cgd {
314 1.29 rillig searchPtr->table = t;
315 1.29 rillig searchPtr->nextBucket = 0;
316 1.29 rillig searchPtr->entry = NULL;
317 1.1 cgd return Hash_EnumNext(searchPtr);
318 1.1 cgd }
319 1.1 cgd
320 1.28 rillig /* Returns the next entry in the hash table, or NULL if the end of the table
321 1.28 rillig * is reached.
322 1.1 cgd *
323 1.10 wiz * Input:
324 1.10 wiz * searchPtr Area used to keep state about search.
325 1.1 cgd */
326 1.1 cgd Hash_Entry *
327 1.10 wiz Hash_EnumNext(Hash_Search *searchPtr)
328 1.1 cgd {
329 1.10 wiz Hash_Entry *e;
330 1.29 rillig Hash_Table *t = searchPtr->table;
331 1.1 cgd
332 1.1 cgd /*
333 1.29 rillig * The entry field points to the most recently returned
334 1.29 rillig * entry, or is NULL if we are starting up. If not NULL, we have
335 1.1 cgd * to start at the next one in the chain.
336 1.1 cgd */
337 1.29 rillig e = searchPtr->entry;
338 1.1 cgd if (e != NULL)
339 1.1 cgd e = e->next;
340 1.1 cgd /*
341 1.1 cgd * If the chain ran out, or if we are starting up, we need to
342 1.1 cgd * find the next nonempty chain.
343 1.1 cgd */
344 1.1 cgd while (e == NULL) {
345 1.29 rillig if (searchPtr->nextBucket >= t->bucketsSize)
346 1.18 dsl return NULL;
347 1.29 rillig e = t->buckets[searchPtr->nextBucket++];
348 1.1 cgd }
349 1.29 rillig searchPtr->entry = e;
350 1.21 rillig return e;
351 1.1 cgd }
352 1.1 cgd
353 1.28 rillig /* Makes a new hash table that is larger than the old one. The entire hash
354 1.28 rillig * table is moved, so any bucket numbers from the old table become invalid. */
355 1.1 cgd static void
356 1.10 wiz RebuildTable(Hash_Table *t)
357 1.1 cgd {
358 1.10 wiz Hash_Entry *e, *next = NULL, **hp, **xp;
359 1.10 wiz int i, mask;
360 1.26 rillig Hash_Entry **oldhp;
361 1.1 cgd int oldsize;
362 1.1 cgd
363 1.29 rillig oldhp = t->buckets;
364 1.29 rillig oldsize = i = t->bucketsSize;
365 1.1 cgd i <<= 1;
366 1.29 rillig t->bucketsSize = i;
367 1.29 rillig t->bucketsMask = mask = i - 1;
368 1.29 rillig t->buckets = hp = bmake_malloc(sizeof(*hp) * i);
369 1.1 cgd while (--i >= 0)
370 1.1 cgd *hp++ = NULL;
371 1.1 cgd for (hp = oldhp, i = oldsize; --i >= 0;) {
372 1.1 cgd for (e = *hp++; e != NULL; e = next) {
373 1.1 cgd next = e->next;
374 1.29 rillig xp = &t->buckets[e->namehash & mask];
375 1.1 cgd e->next = *xp;
376 1.1 cgd *xp = e;
377 1.1 cgd }
378 1.1 cgd }
379 1.16 christos free(oldhp);
380 1.23 sjg if (DEBUG(HASH))
381 1.25 sjg fprintf(debug_file, "%s: %p size=%d entries=%d maxchain=%d\n",
382 1.29 rillig __func__, t, t->bucketsSize, t->numEntries, t->maxchain);
383 1.25 sjg t->maxchain = 0;
384 1.1 cgd }
385 1.22 rillig
386 1.28 rillig void
387 1.28 rillig Hash_ForEach(Hash_Table *t, void (*action)(void *, void *), void *data)
388 1.22 rillig {
389 1.22 rillig Hash_Search search;
390 1.22 rillig Hash_Entry *e;
391 1.22 rillig
392 1.22 rillig for (e = Hash_EnumFirst(t, &search);
393 1.22 rillig e != NULL;
394 1.22 rillig e = Hash_EnumNext(&search))
395 1.22 rillig action(Hash_GetValue(e), data);
396 1.22 rillig }
397 1.25 sjg
398 1.25 sjg void
399 1.25 sjg Hash_DebugStats(Hash_Table *t, const char *name)
400 1.25 sjg {
401 1.25 sjg if (DEBUG(HASH))
402 1.25 sjg fprintf(debug_file, "Hash_Table %s: size=%d numEntries=%d maxchain=%d\n",
403 1.29 rillig name, t->bucketsSize, t->numEntries, t->maxchain);
404 1.25 sjg }
405