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