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