2 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
3 * Copyright (c) 1988, 1989 by Adam de Boor
4 * Copyright (c) 1989 by Berkeley Softworks
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $NetBSD: hash.c,v 1.1.1.1 1999/11/19 04:30:56 mrg Exp $
37 #include <sys/types.h>
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.
58 * Forward references to local procedures that are used before they're
62 static void RebuildTable(Hash_Table
*);
65 * The following defines the ratio of # entries to # buckets
66 * at which we rebuild the table to make it larger.
69 #define rebuildLimit 8
72 *---------------------------------------------------------
76 * This routine just sets up the hash table.
82 * Memory is allocated for the initial bucket area.
84 *---------------------------------------------------------
88 * Hash_Table *t; Structure to use to hold table.
89 * int numBuckets; How many buckets to create for starters.
90 * This number is rounded up to a power of
91 * two. If <= 0, a reasonable default is
92 * chosen. The table will grow in size later
96 Hash_InitTable(Hash_Table
*t
, int numBuckets
)
99 struct Hash_Entry
**hp
;
102 * Round up the size to a power of two.
107 for (i
= 2; i
< numBuckets
; i
<<= 1)
113 t
->bucketPtr
= hp
= (struct Hash_Entry
**)emalloc(sizeof(*hp
) * i
);
119 *---------------------------------------------------------
121 * Hash_DeleteTable --
123 * This routine removes everything from a hash table
124 * and frees up the memory space it occupied (except for
125 * the space in the Hash_Table structure).
131 * Lots of memory is freed up.
133 *---------------------------------------------------------
137 Hash_DeleteTable(Hash_Table
*t
)
139 struct Hash_Entry
**hp
, *h
, *nexth
= NULL
;
142 for (hp
= t
->bucketPtr
, i
= t
->size
; --i
>= 0;) {
143 for (h
= *hp
++; h
!= NULL
; h
= nexth
) {
148 free((char *)t
->bucketPtr
);
151 * Set up the hash table to cause memory faults on any future access
152 * attempts until re-initialization.
158 *---------------------------------------------------------
162 * Searches a hash table for an entry corresponding to key.
165 * The return value is a pointer to the entry for key,
166 * if key was present in the table. If key was not
167 * present, NULL is returned.
172 *---------------------------------------------------------
176 Hash_FindEntry(Hash_Table
*t
, char *key
)
182 for (h
= 0, p
= key
; *p
;)
183 h
= (h
<< 5) - h
+ *p
++;
185 for (e
= t
->bucketPtr
[h
& t
->mask
]; e
!= NULL
; e
= e
->next
)
186 if (e
->namehash
== h
&& strcmp(e
->name
, p
) == 0)
192 *---------------------------------------------------------
194 * Hash_CreateEntry --
196 * Searches a hash table for an entry corresponding to
197 * key. If no entry is found, then one is created.
200 * The return value is a pointer to the entry. If *newPtr
201 * isn't NULL, then *newPtr is filled in with TRUE if a
202 * new entry was created, and FALSE if an entry already existed
203 * with the given key.
206 * Memory may be allocated, and the hash buckets may be modified.
207 *---------------------------------------------------------
211 Hash_CreateEntry(Hash_Table
*t
, char *key
, Boolean
*newPtr
)
217 struct Hash_Entry
**hp
;
220 * Hash the key. As a side effect, save the length (strlen) of the
221 * key in case we need to create the entry.
223 for (h
= 0, p
= key
; *p
;)
224 h
= (h
<< 5) - h
+ *p
++;
227 for (e
= t
->bucketPtr
[h
& t
->mask
]; e
!= NULL
; e
= e
->next
) {
228 if (e
->namehash
== h
&& strcmp(e
->name
, p
) == 0) {
236 * The desired entry isn't there. Before allocating a new entry,
237 * expand the table if necessary (and this changes the resulting
240 if (t
->numEntries
>= rebuildLimit
* t
->size
)
242 e
= (Hash_Entry
*) emalloc(sizeof(*e
) + keylen
);
243 hp
= &t
->bucketPtr
[h
& t
->mask
];
246 e
->clientData
= NULL
;
257 *---------------------------------------------------------
259 * Hash_DeleteEntry --
261 * Delete the given hash table entry and free memory associated with
268 * Hash chain that entry lives in is modified and memory is freed.
270 *---------------------------------------------------------
274 Hash_DeleteEntry(Hash_Table
*t
, Hash_Entry
*e
)
280 for (hp
= &t
->bucketPtr
[e
->namehash
& t
->mask
];
281 (p
= *hp
) != NULL
; hp
= &p
->next
) {
289 write(2, "bad call to Hash_DeleteEntry\n", 29);
294 *---------------------------------------------------------
297 * This procedure sets things up for a complete search
298 * of all entries recorded in the hash table.
301 * The return value is the address of the first entry in
302 * the hash table, or NULL if the table is empty.
305 * The information in searchPtr is initialized so that successive
306 * calls to Hash_Next will return successive HashEntry's
309 *---------------------------------------------------------
313 Hash_EnumFirst(Hash_Table
*t
, Hash_Search
*searchPtr
)
315 searchPtr
->tablePtr
= t
;
316 searchPtr
->nextIndex
= 0;
317 searchPtr
->hashEntryPtr
= NULL
;
318 return Hash_EnumNext(searchPtr
);
322 *---------------------------------------------------------
325 * This procedure returns successive entries in the hash table.
328 * The return value is a pointer to the next HashEntry
329 * in the table, or NULL when the end of the table is
333 * The information in searchPtr is modified to advance to the
336 *---------------------------------------------------------
340 Hash_EnumNext(Hash_Search
*searchPtr
)
343 Hash_Table
*t
= searchPtr
->tablePtr
;
346 * The hashEntryPtr field points to the most recently returned
347 * entry, or is nil if we are starting up. If not nil, we have
348 * to start at the next one in the chain.
350 e
= searchPtr
->hashEntryPtr
;
354 * If the chain ran out, or if we are starting up, we need to
355 * find the next nonempty chain.
358 if (searchPtr
->nextIndex
>= t
->size
)
360 e
= t
->bucketPtr
[searchPtr
->nextIndex
++];
362 searchPtr
->hashEntryPtr
= e
;
367 *---------------------------------------------------------
370 * This local routine makes a new hash table that
371 * is larger than the old one.
377 * The entire hash table is moved, so any bucket numbers
378 * from the old table are invalid.
380 *---------------------------------------------------------
384 RebuildTable(Hash_Table
*t
)
386 Hash_Entry
*e
, *next
= NULL
, **hp
, **xp
;
391 oldhp
= t
->bucketPtr
;
392 oldsize
= i
= t
->size
;
395 t
->mask
= mask
= i
- 1;
396 t
->bucketPtr
= hp
= (struct Hash_Entry
**) emalloc(sizeof(*hp
) * i
);
399 for (hp
= oldhp
, i
= oldsize
; --i
>= 0;) {
400 for (e
= *hp
++; e
!= NULL
; e
= next
) {
402 xp
= &t
->bucketPtr
[e
->namehash
& mask
];