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