hash.c revision 1.49 1 1.49 rillig /* $NetBSD: hash.c,v 1.49 2020/10/25 17:58:53 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.49 rillig MAKE_RCSID("$NetBSD: hash.c,v 1.49 2020/10/25 17:58:53 rillig Exp $");
83 1.32 rillig
84 1.1 cgd /*
85 1.43 rillig * The ratio of # entries to # buckets at which we rebuild the table to
86 1.43 rillig * make it larger.
87 1.1 cgd */
88 1.43 rillig #define rebuildLimit 3
89 1.1 cgd
90 1.43 rillig /* This hash function matches Gosling's emacs. */
91 1.43 rillig static unsigned int
92 1.43 rillig hash(const char *key, size_t *out_keylen)
93 1.43 rillig {
94 1.43 rillig unsigned h = 0;
95 1.43 rillig const char *p = key;
96 1.43 rillig while (*p != '\0')
97 1.43 rillig h = (h << 5) - h + (unsigned char)*p++;
98 1.43 rillig if (out_keylen != NULL)
99 1.43 rillig *out_keylen = (size_t)(p - key);
100 1.43 rillig return h;
101 1.43 rillig }
102 1.23 sjg
103 1.48 rillig unsigned int
104 1.48 rillig Hash_Hash(const char *key)
105 1.48 rillig {
106 1.48 rillig return hash(key, NULL);
107 1.48 rillig }
108 1.48 rillig
109 1.46 rillig static HashEntry *
110 1.46 rillig HashTable_Find(HashTable *t, unsigned int h, const char *key)
111 1.43 rillig {
112 1.46 rillig HashEntry *e;
113 1.44 rillig unsigned int chainlen = 0;
114 1.41 rillig
115 1.43 rillig #ifdef DEBUG_HASH_LOOKUP
116 1.43 rillig DEBUG4(HASH, "%s: %p h=%x key=%s\n", __func__, t, h, key);
117 1.43 rillig #endif
118 1.41 rillig
119 1.43 rillig for (e = t->buckets[h & t->bucketsMask]; e != NULL; e = e->next) {
120 1.43 rillig chainlen++;
121 1.47 rillig if (e->key_hash == h && strcmp(e->key, key) == 0)
122 1.43 rillig break;
123 1.43 rillig }
124 1.41 rillig
125 1.43 rillig if (chainlen > t->maxchain)
126 1.43 rillig t->maxchain = chainlen;
127 1.41 rillig
128 1.43 rillig return e;
129 1.43 rillig }
130 1.41 rillig
131 1.43 rillig /* Sets up the hash table. */
132 1.1 cgd void
133 1.46 rillig Hash_InitTable(HashTable *t)
134 1.1 cgd {
135 1.44 rillig unsigned int n = 16, i;
136 1.46 rillig HashEntry **hp;
137 1.1 cgd
138 1.1 cgd t->numEntries = 0;
139 1.25 sjg t->maxchain = 0;
140 1.31 rillig t->bucketsSize = n;
141 1.31 rillig t->bucketsMask = n - 1;
142 1.31 rillig t->buckets = hp = bmake_malloc(sizeof(*hp) * n);
143 1.31 rillig for (i = 0; i < n; i++)
144 1.31 rillig hp[i] = NULL;
145 1.1 cgd }
146 1.1 cgd
147 1.28 rillig /* Removes everything from the hash table and frees up the memory space it
148 1.46 rillig * occupied (except for the space in the HashTable structure). */
149 1.1 cgd void
150 1.46 rillig Hash_DeleteTable(HashTable *t)
151 1.1 cgd {
152 1.46 rillig HashEntry **hp, *h, *nexth = NULL;
153 1.10 wiz int i;
154 1.1 cgd
155 1.44 rillig for (hp = t->buckets, i = (int)t->bucketsSize; --i >= 0;) {
156 1.1 cgd for (h = *hp++; h != NULL; h = nexth) {
157 1.1 cgd nexth = h->next;
158 1.16 christos free(h);
159 1.1 cgd }
160 1.1 cgd }
161 1.29 rillig free(t->buckets);
162 1.1 cgd
163 1.1 cgd /*
164 1.1 cgd * Set up the hash table to cause memory faults on any future access
165 1.6 christos * attempts until re-initialization.
166 1.1 cgd */
167 1.29 rillig t->buckets = NULL;
168 1.1 cgd }
169 1.1 cgd
170 1.28 rillig /* Searches the hash table for an entry corresponding to the key.
171 1.1 cgd *
172 1.10 wiz * Input:
173 1.10 wiz * t Hash table to search.
174 1.10 wiz * key A hash key.
175 1.10 wiz *
176 1.1 cgd * Results:
177 1.28 rillig * Returns a pointer to the entry for key, or NULL if the table contains
178 1.28 rillig * no entry for the key.
179 1.1 cgd */
180 1.46 rillig HashEntry *
181 1.46 rillig Hash_FindEntry(HashTable *t, const char *key)
182 1.1 cgd {
183 1.43 rillig unsigned int h = hash(key, NULL);
184 1.43 rillig return HashTable_Find(t, h, key);
185 1.1 cgd }
186 1.1 cgd
187 1.33 rillig void *
188 1.46 rillig Hash_FindValue(HashTable *t, const char *key)
189 1.33 rillig {
190 1.46 rillig HashEntry *he = Hash_FindEntry(t, key);
191 1.43 rillig return he != NULL ? he->value : NULL;
192 1.43 rillig }
193 1.43 rillig
194 1.48 rillig void *
195 1.48 rillig Hash_FindValueHash(HashTable *t, const char *key, unsigned int h)
196 1.48 rillig {
197 1.48 rillig HashEntry *he = HashTable_Find(t, h, key);
198 1.48 rillig return he != NULL ? he->value : NULL;
199 1.48 rillig }
200 1.48 rillig
201 1.43 rillig /* Makes a new hash table that is larger than the old one. The entire hash
202 1.43 rillig * table is moved, so any bucket numbers from the old table become invalid. */
203 1.43 rillig static void
204 1.46 rillig RebuildTable(HashTable *t)
205 1.43 rillig {
206 1.49 rillig unsigned int oldSize = t->bucketsSize;
207 1.49 rillig HashEntry **oldBuckets = t->buckets;
208 1.49 rillig unsigned int newSize = 2 * oldSize;
209 1.49 rillig unsigned int newMask = newSize - 1;
210 1.49 rillig HashEntry **newBuckets = bmake_malloc(sizeof(*newBuckets) * newSize);
211 1.49 rillig size_t i;
212 1.49 rillig
213 1.49 rillig for (i = 0; i < newSize; i++)
214 1.49 rillig newBuckets[i] = NULL;
215 1.49 rillig
216 1.49 rillig for (i = 0; i < oldSize; i++) {
217 1.49 rillig HashEntry *he = oldBuckets[i];
218 1.49 rillig while (he != NULL) {
219 1.49 rillig HashEntry *next = he->next;
220 1.49 rillig he->next = newBuckets[he->key_hash & newMask];
221 1.49 rillig newBuckets[he->key_hash & newMask] = he;
222 1.49 rillig he = next;
223 1.43 rillig }
224 1.43 rillig }
225 1.49 rillig
226 1.49 rillig free(oldBuckets);
227 1.49 rillig
228 1.49 rillig t->bucketsSize = newSize;
229 1.49 rillig t->bucketsMask = newMask;
230 1.49 rillig t->buckets = newBuckets;
231 1.43 rillig DEBUG5(HASH, "%s: %p size=%d entries=%d maxchain=%d\n",
232 1.43 rillig __func__, t, t->bucketsSize, t->numEntries, t->maxchain);
233 1.43 rillig t->maxchain = 0;
234 1.33 rillig }
235 1.33 rillig
236 1.28 rillig /* Searches the hash table for an entry corresponding to the key.
237 1.28 rillig * If no entry is found, then one is created.
238 1.1 cgd *
239 1.10 wiz * Input:
240 1.10 wiz * t Hash table to search.
241 1.10 wiz * key A hash key.
242 1.28 rillig * newPtr Filled with TRUE if new entry created,
243 1.10 wiz * FALSE otherwise.
244 1.1 cgd */
245 1.46 rillig HashEntry *
246 1.46 rillig Hash_CreateEntry(HashTable *t, const char *key, Boolean *newPtr)
247 1.1 cgd {
248 1.46 rillig HashEntry *e;
249 1.10 wiz unsigned h;
250 1.43 rillig size_t keylen;
251 1.46 rillig HashEntry **hp;
252 1.1 cgd
253 1.43 rillig h = hash(key, &keylen);
254 1.43 rillig e = HashTable_Find(t, h, key);
255 1.43 rillig if (e) {
256 1.43 rillig if (newPtr != NULL)
257 1.43 rillig *newPtr = FALSE;
258 1.43 rillig return e;
259 1.42 rillig }
260 1.1 cgd
261 1.1 cgd /*
262 1.1 cgd * The desired entry isn't there. Before allocating a new entry,
263 1.1 cgd * expand the table if necessary (and this changes the resulting
264 1.6 christos * bucket chain).
265 1.1 cgd */
266 1.29 rillig if (t->numEntries >= rebuildLimit * t->bucketsSize)
267 1.1 cgd RebuildTable(t);
268 1.43 rillig
269 1.17 joerg e = bmake_malloc(sizeof(*e) + keylen);
270 1.29 rillig hp = &t->buckets[h & t->bucketsMask];
271 1.1 cgd e->next = *hp;
272 1.1 cgd *hp = e;
273 1.19 dsl Hash_SetValue(e, NULL);
274 1.47 rillig e->key_hash = h;
275 1.47 rillig memcpy(e->key, key, keylen + 1);
276 1.1 cgd t->numEntries++;
277 1.1 cgd
278 1.1 cgd if (newPtr != NULL)
279 1.1 cgd *newPtr = TRUE;
280 1.21 rillig return e;
281 1.1 cgd }
282 1.1 cgd
283 1.28 rillig /* Delete the given hash table entry and free memory associated with it. */
284 1.1 cgd void
285 1.46 rillig Hash_DeleteEntry(HashTable *t, HashEntry *e)
286 1.1 cgd {
287 1.46 rillig HashEntry **hp, *p;
288 1.1 cgd
289 1.47 rillig for (hp = &t->buckets[e->key_hash & t->bucketsMask];
290 1.1 cgd (p = *hp) != NULL; hp = &p->next) {
291 1.1 cgd if (p == e) {
292 1.1 cgd *hp = p->next;
293 1.16 christos free(p);
294 1.1 cgd t->numEntries--;
295 1.1 cgd return;
296 1.1 cgd }
297 1.1 cgd }
298 1.1 cgd abort();
299 1.1 cgd }
300 1.1 cgd
301 1.45 rillig /* Set things up for iterating over all entries in the hash table. */
302 1.45 rillig void
303 1.46 rillig HashIter_Init(HashIter *hi, HashTable *t)
304 1.1 cgd {
305 1.45 rillig hi->table = t;
306 1.45 rillig hi->nextBucket = 0;
307 1.45 rillig hi->entry = NULL;
308 1.1 cgd }
309 1.1 cgd
310 1.45 rillig /* Return the next entry in the hash table, or NULL if the end of the table
311 1.45 rillig * is reached. */
312 1.46 rillig HashEntry *
313 1.45 rillig HashIter_Next(HashIter *hi)
314 1.1 cgd {
315 1.46 rillig HashEntry *e;
316 1.46 rillig HashTable *t = hi->table;
317 1.1 cgd
318 1.1 cgd /*
319 1.29 rillig * The entry field points to the most recently returned
320 1.29 rillig * entry, or is NULL if we are starting up. If not NULL, we have
321 1.1 cgd * to start at the next one in the chain.
322 1.1 cgd */
323 1.45 rillig e = hi->entry;
324 1.1 cgd if (e != NULL)
325 1.1 cgd e = e->next;
326 1.1 cgd /*
327 1.1 cgd * If the chain ran out, or if we are starting up, we need to
328 1.1 cgd * find the next nonempty chain.
329 1.1 cgd */
330 1.1 cgd while (e == NULL) {
331 1.45 rillig if (hi->nextBucket >= t->bucketsSize)
332 1.18 dsl return NULL;
333 1.45 rillig e = t->buckets[hi->nextBucket++];
334 1.1 cgd }
335 1.45 rillig hi->entry = e;
336 1.21 rillig return e;
337 1.1 cgd }
338 1.1 cgd
339 1.28 rillig void
340 1.46 rillig Hash_DebugStats(HashTable *t, const char *name)
341 1.25 sjg {
342 1.46 rillig DEBUG4(HASH, "HashTable %s: size=%u numEntries=%u maxchain=%u\n",
343 1.43 rillig name, t->bucketsSize, t->numEntries, t->maxchain);
344 1.25 sjg }
345