2 * Copyright (c) 1988, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1988, 1989 by Adam de Boor
5 * Copyright (c) 1989 by Berkeley Softworks
8 * This code is derived from software contributed to Berkeley by
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * @(#)hash.c 8.1 (Berkeley) 6/6/93
40 * $FreeBSD: src/usr.bin/make/hash.c,v 1.9 1999/09/11 13:08:01 hoek Exp $
41 * $DragonFly: src/usr.bin/make/hash.c,v 1.13 2004/12/16 23:22:15 okumoto Exp $
46 * This module contains routines to manipulate a hash table.
47 * See hash.h for a definition of the structure of the hash
48 * table. Hash tables grow automatically as the amount of
49 * information increases.
57 * Forward references to local procedures that are used before they're
60 static void RebuildTable(Hash_Table *);
63 * The following defines the ratio of # entries to # buckets
64 * at which we rebuild the table to make it larger.
67 #define rebuildLimit 8
70 *---------------------------------------------------------
74 * Set up the hash table t with a given number of buckets, or a
75 * reasonable default if the number requested is less than or
76 * equal to zero. Hash tables will grow in size as needed.
83 * Memory is allocated for the initial bucket area.
85 *---------------------------------------------------------
88 Hash_InitTable(Hash_Table *t, int numBuckets)
91 struct Hash_Entry **hp;
94 * Round up the size to a power of two.
99 for (i = 2; i < numBuckets; i <<= 1)
105 t->bucketPtr = hp = emalloc(sizeof(*hp) * i);
111 *---------------------------------------------------------
113 * Hash_DeleteTable --
115 * This routine removes everything from a hash table
116 * and frees up the memory space it occupied (except for
117 * the space in the Hash_Table structure).
123 * Lots of memory is freed up.
125 *---------------------------------------------------------
128 Hash_DeleteTable(Hash_Table *t)
130 struct Hash_Entry **hp, *h, *nexth = NULL;
133 for (hp = t->bucketPtr, i = t->size; --i >= 0;) {
134 for (h = *hp++; h != NULL; h = nexth) {
142 * Set up the hash table to cause memory faults on any future access
143 * attempts until re-initialization.
149 *---------------------------------------------------------
153 * Searches a hash table for an entry corresponding to key.
156 * The return value is a pointer to the entry for key,
157 * if key was present in the table. If key was not
158 * present, NULL is returned.
163 *---------------------------------------------------------
166 Hash_FindEntry(const Hash_Table *t, const char *key)
172 for (h = 0, p = key; *p;)
173 h = (h << 5) - h + *p++;
175 for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next)
176 if (e->namehash == h && strcmp(e->name, p) == 0)
182 *---------------------------------------------------------
184 * Hash_CreateEntry --
186 * Searches a hash table for an entry corresponding to
187 * key. If no entry is found, then one is created.
190 * The return value is a pointer to the entry. If *newPtr
191 * isn't NULL, then *newPtr is filled in with TRUE if a
192 * new entry was created, and FALSE if an entry already existed
193 * with the given key.
196 * Memory may be allocated, and the hash buckets may be modified.
197 *---------------------------------------------------------
200 Hash_CreateEntry(Hash_Table *t, const char *key, Boolean *newPtr)
206 struct Hash_Entry **hp;
209 * Hash the key. As a side effect, save the length (strlen) of the
210 * key in case we need to create the entry.
212 for (h = 0, p = key; *p;)
213 h = (h << 5) - h + *p++;
216 for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next) {
217 if (e->namehash == h && strcmp(e->name, p) == 0) {
225 * The desired entry isn't there. Before allocating a new entry,
226 * expand the table if necessary (and this changes the resulting
229 if (t->numEntries >= rebuildLimit * t->size)
231 e = emalloc(sizeof(*e) + keylen);
232 hp = &t->bucketPtr[h & t->mask];
235 e->clientData = NULL;
246 *---------------------------------------------------------
248 * Hash_DeleteEntry --
250 * Delete the given hash table entry and free memory associated with
257 * Hash chain that entry lives in is modified and memory is freed.
259 *---------------------------------------------------------
262 Hash_DeleteEntry(Hash_Table *t, Hash_Entry *e)
268 for (hp = &t->bucketPtr[e->namehash & t->mask];
269 (p = *hp) != NULL; hp = &p->next) {
277 write(STDERR_FILENO, "bad call to Hash_DeleteEntry\n", 29);
282 *---------------------------------------------------------
285 * This procedure sets things up for a complete search
286 * of all entries recorded in the hash table.
289 * The return value is the address of the first entry in
290 * the hash table, or NULL if the table is empty.
293 * The information in searchPtr is initialized so that successive
294 * calls to Hash_Next will return successive HashEntry's
297 *---------------------------------------------------------
300 Hash_EnumFirst(const Hash_Table *t, Hash_Search *searchPtr)
303 searchPtr->tablePtr = t;
304 searchPtr->nextIndex = 0;
305 searchPtr->hashEntryPtr = NULL;
306 return (Hash_EnumNext(searchPtr));
310 *---------------------------------------------------------
313 * This procedure returns successive entries in the hash table.
316 * The return value is a pointer to the next HashEntry
317 * in the table, or NULL when the end of the table is
321 * The information in searchPtr is modified to advance to the
324 *---------------------------------------------------------
327 Hash_EnumNext(Hash_Search *searchPtr)
330 const Hash_Table *t = searchPtr->tablePtr;
333 * The hashEntryPtr field points to the most recently returned
334 * entry, or is NULL if we are starting up. If not NULL, we have
335 * to start at the next one in the chain.
337 e = searchPtr->hashEntryPtr;
341 * If the chain ran out, or if we are starting up, we need to
342 * find the next nonempty chain.
345 if (searchPtr->nextIndex >= t->size)
347 e = t->bucketPtr[searchPtr->nextIndex++];
349 searchPtr->hashEntryPtr = e;
354 *---------------------------------------------------------
357 * This local routine makes a new hash table that
358 * is larger than the old one.
364 * The entire hash table is moved, so any bucket numbers
365 * from the old table are invalid.
367 *---------------------------------------------------------
370 RebuildTable(Hash_Table *t)
372 Hash_Entry *e, *next = NULL, **hp, **xp;
377 oldhp = t->bucketPtr;
378 oldsize = i = t->size;
381 t->mask = mask = i - 1;
382 t->bucketPtr = hp = emalloc(sizeof(*hp) * i);
385 for (hp = oldhp, i = oldsize; --i >= 0;) {
386 for (e = *hp++; e != NULL; e = next) {
388 xp = &t->bucketPtr[e->namehash & mask];