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