source: trunk/src/kmk/hash.c@ 18

Last change on this file since 18 was 9, checked in by bird, 23 years ago

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.9 KB
Line 
1/*
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
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Adam de Boor.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
26 *
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
37 * SUCH DAMAGE.
38 *
39 * @(#)hash.c 8.1 (Berkeley) 6/6/93
40 */
41
42#include <sys/cdefs.h>
43__FBSDID("$FreeBSD: src/usr.bin/make/hash.c,v 1.18 2002/10/09 03:42:10 jmallett Exp $");
44
45/* hash.c --
46 *
47 * This module contains routines to manipulate a hash table.
48 * See hash.h for a definition of the structure of the hash
49 * table. Hash tables grow automatically as the amount of
50 * information increases.
51 */
52#include <unistd.h>
53#include "sprite.h"
54#include "make.h"
55#include "hash.h"
56
57/*
58 * Forward references to local procedures that are used before they're
59 * defined:
60 */
61
62static void RebuildTable(Hash_Table *);
63
64/*
65 * The following defines the ratio of # entries to # buckets
66 * at which we rebuild the table to make it larger.
67 */
68
69#define rebuildLimit 8
70
71/*
72 *---------------------------------------------------------
73 *
74 * Hash_InitTable --
75 *
76 * Set up the hash table t with a given number of buckets, or a
77 * reasonable default if the number requested is less than or
78 * equal to zero. Hash tables will grow in size as needed.
79 *
80 *
81 * Results:
82 * None.
83 *
84 * Side Effects:
85 * Memory is allocated for the initial bucket area.
86 *
87 *---------------------------------------------------------
88 */
89
90void
91Hash_InitTable(Hash_Table *t, int numBuckets)
92{
93 int i;
94 struct Hash_Entry **hp;
95
96 /*
97 * Round up the size to a power of two.
98 */
99 if (numBuckets <= 0)
100 i = 16;
101 else {
102 for (i = 2; i < numBuckets; i <<= 1)
103 continue;
104 }
105 t->numEntries = 0;
106 t->size = i;
107 t->mask = i - 1;
108 t->bucketPtr = hp = (struct Hash_Entry **)emalloc(sizeof(*hp) * i);
109 while (--i >= 0)
110 *hp++ = NULL;
111}
112
113/*
114 *---------------------------------------------------------
115 *
116 * Hash_DeleteTable --
117 *
118 * This routine removes everything from a hash table
119 * and frees up the memory space it occupied (except for
120 * the space in the Hash_Table structure).
121 *
122 * Results:
123 * None.
124 *
125 * Side Effects:
126 * Lots of memory is freed up.
127 *
128 *---------------------------------------------------------
129 */
130
131void
132Hash_DeleteTable(Hash_Table *t)
133{
134 struct Hash_Entry **hp, *h, *nexth = NULL;
135 int i;
136
137 for (hp = t->bucketPtr, i = t->size; --i >= 0;) {
138 for (h = *hp++; h != NULL; h = nexth) {
139 nexth = h->next;
140 free((char *)h);
141 }
142 }
143 free((char *)t->bucketPtr);
144
145 /*
146 * Set up the hash table to cause memory faults on any future access
147 * attempts until re-initialization.
148 */
149 t->bucketPtr = NULL;
150}
151
152/*
153 *---------------------------------------------------------
154 *
155 * Hash_FindEntry --
156 *
157 * Searches a hash table for an entry corresponding to key.
158 *
159 * Results:
160 * The return value is a pointer to the entry for key,
161 * if key was present in the table. If key was not
162 * present, NULL is returned.
163 *
164 * Side Effects:
165 * None.
166 *
167 *---------------------------------------------------------
168 */
169
170Hash_Entry *
171Hash_FindEntry(Hash_Table *t, char *key)
172{
173 Hash_Entry *e;
174 unsigned h;
175 char *p;
176
177 for (h = 0, p = key; *p;)
178 h = (h << 5) - h + *p++;
179 p = key;
180 for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next)
181 if (e->namehash == h && strcmp(e->name, p) == 0)
182 return (e);
183 return (NULL);
184}
185
186/*
187 *---------------------------------------------------------
188 *
189 * Hash_CreateEntry --
190 *
191 * Searches a hash table for an entry corresponding to
192 * key. If no entry is found, then one is created.
193 *
194 * Results:
195 * The return value is a pointer to the entry. If *newPtr
196 * isn't NULL, then *newPtr is filled in with TRUE if a
197 * new entry was created, and FALSE if an entry already existed
198 * with the given key.
199 *
200 * Side Effects:
201 * Memory may be allocated, and the hash buckets may be modified.
202 *---------------------------------------------------------
203 */
204
205Hash_Entry *
206Hash_CreateEntry(Hash_Table *t, char *key, Boolean *newPtr)
207{
208 Hash_Entry *e;
209 unsigned int h;
210 char *p;
211 int keylen;
212 struct Hash_Entry **hp;
213
214 /*
215 * Hash the key. As a side effect, save the length (strlen) of the
216 * key in case we need to create the entry.
217 */
218 for (h = 0, p = key; *p;)
219 h = (h << 5) - h + *p++;
220 keylen = p - key;
221 p = key;
222 for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next) {
223 if (e->namehash == h && strcmp(e->name, p) == 0) {
224 if (newPtr != NULL)
225 *newPtr = FALSE;
226 return (e);
227 }
228 }
229
230 /*
231 * The desired entry isn't there. Before allocating a new entry,
232 * expand the table if necessary (and this changes the resulting
233 * bucket chain).
234 */
235 if (t->numEntries >= rebuildLimit * t->size)
236 RebuildTable(t);
237 e = (Hash_Entry *) emalloc(sizeof(*e) + keylen);
238 hp = &t->bucketPtr[h & t->mask];
239 e->next = *hp;
240 *hp = e;
241 e->clientData = NULL;
242 e->namehash = h;
243 (void) strcpy(e->name, p);
244 t->numEntries++;
245
246 if (newPtr != NULL)
247 *newPtr = TRUE;
248 return (e);
249}
250
251/*
252 *---------------------------------------------------------
253 *
254 * Hash_DeleteEntry --
255 *
256 * Delete the given hash table entry and free memory associated with
257 * it.
258 *
259 * Results:
260 * None.
261 *
262 * Side Effects:
263 * Hash chain that entry lives in is modified and memory is freed.
264 *
265 *---------------------------------------------------------
266 */
267
268void
269Hash_DeleteEntry(Hash_Table *t, Hash_Entry *e)
270{
271 Hash_Entry **hp, *p;
272
273 if (e == NULL)
274 return;
275 for (hp = &t->bucketPtr[e->namehash & t->mask];
276 (p = *hp) != NULL; hp = &p->next) {
277 if (p == e) {
278 *hp = p->next;
279 free((char *)p);
280 t->numEntries--;
281 return;
282 }
283 }
284 (void) write(STDERR_FILENO, "bad call to Hash_DeleteEntry\n", 29);
285 abort();
286}
287
288/*
289 *---------------------------------------------------------
290 *
291 * Hash_EnumFirst --
292 * This procedure sets things up for a complete search
293 * of all entries recorded in the hash table.
294 *
295 * Results:
296 * The return value is the address of the first entry in
297 * the hash table, or NULL if the table is empty.
298 *
299 * Side Effects:
300 * The information in searchPtr is initialized so that successive
301 * calls to Hash_Next will return successive HashEntry's
302 * from the table.
303 *
304 *---------------------------------------------------------
305 */
306
307Hash_Entry *
308Hash_EnumFirst(Hash_Table *t, Hash_Search *searchPtr)
309{
310 searchPtr->tablePtr = t;
311 searchPtr->nextIndex = 0;
312 searchPtr->hashEntryPtr = NULL;
313 return Hash_EnumNext(searchPtr);
314}
315
316/*
317 *---------------------------------------------------------
318 *
319 * Hash_EnumNext --
320 * This procedure returns successive entries in the hash table.
321 *
322 * Results:
323 * The return value is a pointer to the next HashEntry
324 * in the table, or NULL when the end of the table is
325 * reached.
326 *
327 * Side Effects:
328 * The information in searchPtr is modified to advance to the
329 * next entry.
330 *
331 *---------------------------------------------------------
332 */
333
334Hash_Entry *
335Hash_EnumNext(Hash_Search *searchPtr)
336{
337 Hash_Entry *e;
338 Hash_Table *t = searchPtr->tablePtr;
339
340 /*
341 * The hashEntryPtr field points to the most recently returned
342 * entry, or is NULL if we are starting up. If not NULL, we have
343 * to start at the next one in the chain.
344 */
345 e = searchPtr->hashEntryPtr;
346 if (e != NULL)
347 e = e->next;
348 /*
349 * If the chain ran out, or if we are starting up, we need to
350 * find the next nonempty chain.
351 */
352 while (e == NULL) {
353 if (searchPtr->nextIndex >= t->size)
354 return (NULL);
355 e = t->bucketPtr[searchPtr->nextIndex++];
356 }
357 searchPtr->hashEntryPtr = e;
358 return (e);
359}
360
361/*
362 *---------------------------------------------------------
363 *
364 * RebuildTable --
365 * This local routine makes a new hash table that
366 * is larger than the old one.
367 *
368 * Results:
369 * None.
370 *
371 * Side Effects:
372 * The entire hash table is moved, so any bucket numbers
373 * from the old table are invalid.
374 *
375 *---------------------------------------------------------
376 */
377
378static void
379RebuildTable(Hash_Table *t)
380{
381 Hash_Entry *e, *next = NULL, **hp, **xp;
382 int i, mask;
383 Hash_Entry **oldhp;
384 int oldsize;
385
386 oldhp = t->bucketPtr;
387 oldsize = i = t->size;
388 i <<= 1;
389 t->size = i;
390 t->mask = mask = i - 1;
391 t->bucketPtr = hp = (struct Hash_Entry **) emalloc(sizeof(*hp) * i);
392 while (--i >= 0)
393 *hp++ = NULL;
394 for (hp = oldhp, i = oldsize; --i >= 0;) {
395 for (e = *hp++; e != NULL; e = next) {
396 next = e->next;
397 xp = &t->bucketPtr[e->namehash & mask];
398 e->next = *xp;
399 *xp = e;
400 }
401 }
402 free((char *)oldhp);
403}
Note: See TracBrowser for help on using the repository browser.