1 /* $NetBSD: hash.c,v 1.1.1.1 2002/09/19 10:40:07 agc Exp $ */
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
9 * This code is derived from software contributed to Berkeley by
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
42 static char rcsid[] = "$NetBSD: hash.c,v 1.1.1.1 2002/09/19 10:40:07 agc Exp $";
44 #include <sys/cdefs.h>
47 static char sccsid[] = "@(#)hash.c 8.1 (Berkeley) 6/6/93";
49 __RCSID("$NetBSD: hash.c,v 1.1.1.1 2002/09/19 10:40:07 agc Exp $");
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.
66 * Forward references to local procedures that are used before they're
70 static void RebuildTable __P((Hash_Table *));
73 * The following defines the ratio of # entries to # buckets
74 * at which we rebuild the table to make it larger.
77 #define rebuildLimit 3
80 *---------------------------------------------------------
84 * This routine just sets up the hash table.
90 * Memory is allocated for the initial bucket area.
92 *---------------------------------------------------------
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
105 register struct Hash_Entry **hp;
108 * Round up the size to a power of two.
113 for (i = 2; i < numBuckets; i <<= 1)
119 t->bucketPtr = hp = (struct Hash_Entry **)emalloc(sizeof(*hp) * i);
125 *---------------------------------------------------------
127 * Hash_DeleteTable --
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).
137 * Lots of memory is freed up.
139 *---------------------------------------------------------
146 register struct Hash_Entry **hp, *h, *nexth = NULL;
149 for (hp = t->bucketPtr, i = t->size; --i >= 0;) {
150 for (h = *hp++; h != NULL; h = nexth) {
155 free((char *)t->bucketPtr);
158 * Set up the hash table to cause memory faults on any future access
159 * attempts until re-initialization.
165 *---------------------------------------------------------
169 * Searches a hash table for an entry corresponding to key.
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.
179 *---------------------------------------------------------
183 Hash_FindEntry(t, key)
184 Hash_Table *t; /* Hash table to search. */
185 char *key; /* A hash key. */
187 register Hash_Entry *e;
191 for (h = 0, p = key; *p;)
192 h = (h << 5) - h + *p++;
194 for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next)
195 if (e->namehash == h && strcmp(e->name, p) == 0)
201 *---------------------------------------------------------
203 * Hash_CreateEntry --
205 * Searches a hash table for an entry corresponding to
206 * key. If no entry is found, then one is created.
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.
215 * Memory may be allocated, and the hash buckets may be modified.
216 *---------------------------------------------------------
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. */
226 register Hash_Entry *e;
230 struct Hash_Entry **hp;
233 * Hash the key. As a side effect, save the length (strlen) of the
234 * key in case we need to create the entry.
236 for (h = 0, p = key; *p;)
237 h = (h << 5) - h + *p++;
240 for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next) {
241 if (e->namehash == h && strcmp(e->name, p) == 0) {
249 * The desired entry isn't there. Before allocating a new entry,
250 * expand the table if necessary (and this changes the resulting
253 if (t->numEntries >= rebuildLimit * t->size)
255 e = (Hash_Entry *) emalloc(sizeof(*e) + keylen);
256 hp = &t->bucketPtr[h & t->mask];
259 e->clientData = NULL;
261 (void) strcpy(e->name, p);
270 *---------------------------------------------------------
272 * Hash_DeleteEntry --
274 * Delete the given hash table entry and free memory associated with
281 * Hash chain that entry lives in is modified and memory is freed.
283 *---------------------------------------------------------
287 Hash_DeleteEntry(t, e)
291 register Hash_Entry **hp, *p;
295 for (hp = &t->bucketPtr[e->namehash & t->mask];
296 (p = *hp) != NULL; hp = &p->next) {
304 (void) write(2, "bad call to Hash_DeleteEntry\n", 29);
309 *---------------------------------------------------------
312 * This procedure sets things up for a complete search
313 * of all entries recorded in the hash table.
316 * The return value is the address of the first entry in
317 * the hash table, or NULL if the table is empty.
320 * The information in searchPtr is initialized so that successive
321 * calls to Hash_Next will return successive HashEntry's
324 *---------------------------------------------------------
328 Hash_EnumFirst(t, searchPtr)
329 Hash_Table *t; /* Table to be searched. */
330 register Hash_Search *searchPtr;/* Area in which to keep state
333 searchPtr->tablePtr = t;
334 searchPtr->nextIndex = 0;
335 searchPtr->hashEntryPtr = NULL;
336 return Hash_EnumNext(searchPtr);
340 *---------------------------------------------------------
343 * This procedure returns successive entries in the hash table.
346 * The return value is a pointer to the next HashEntry
347 * in the table, or NULL when the end of the table is
351 * The information in searchPtr is modified to advance to the
354 *---------------------------------------------------------
358 Hash_EnumNext(searchPtr)
359 register Hash_Search *searchPtr; /* Area used to keep state about
362 register Hash_Entry *e;
363 Hash_Table *t = searchPtr->tablePtr;
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.
370 e = searchPtr->hashEntryPtr;
374 * If the chain ran out, or if we are starting up, we need to
375 * find the next nonempty chain.
378 if (searchPtr->nextIndex >= t->size)
380 e = t->bucketPtr[searchPtr->nextIndex++];
382 searchPtr->hashEntryPtr = e;
387 *---------------------------------------------------------
390 * This local routine makes a new hash table that
391 * is larger than the old one.
397 * The entire hash table is moved, so any bucket numbers
398 * from the old table are invalid.
400 *---------------------------------------------------------
405 register Hash_Table *t;
407 register Hash_Entry *e, *next = NULL, **hp, **xp;
408 register int i, mask;
409 register Hash_Entry **oldhp;
412 oldhp = t->bucketPtr;
413 oldsize = i = t->size;
416 t->mask = mask = i - 1;
417 t->bucketPtr = hp = (struct Hash_Entry **) emalloc(sizeof(*hp) * i);
420 for (hp = oldhp, i = oldsize; --i >= 0;) {
421 for (e = *hp++; e != NULL; e = next) {
423 xp = &t->bucketPtr[e->namehash & mask];