xref: /netbsd-src/sbin/rcorder/hash.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: hash.c,v 1.3 2003/08/07 10:04:37 agc Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Adam de Boor.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
21  *
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
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * Copyright (c) 1988, 1989 by Adam de Boor
37  * Copyright (c) 1989 by Berkeley Softworks
38  * All rights reserved.
39  *
40  * This code is derived from software contributed to Berkeley by
41  * Adam de Boor.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *	This product includes software developed by the University of
54  *	California, Berkeley and its contributors.
55  * 4. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  */
71 
72 #ifdef MAKE_BOOTSTRAP
73 static char rcsid[] = "$NetBSD: hash.c,v 1.3 2003/08/07 10:04:37 agc Exp $";
74 #else
75 #include <sys/cdefs.h>
76 #ifndef lint
77 #if 0
78 static char sccsid[] = "@(#)hash.c	8.1 (Berkeley) 6/6/93";
79 #else
80 __RCSID("$NetBSD: hash.c,v 1.3 2003/08/07 10:04:37 agc Exp $");
81 #endif
82 #endif /* not lint */
83 #endif
84 
85 #include <sys/types.h>
86 
87 #include <stdlib.h>
88 #include <string.h>
89 #include <unistd.h>
90 
91 /* hash.c --
92  *
93  * 	This module contains routines to manipulate a hash table.
94  * 	See hash.h for a definition of the structure of the hash
95  * 	table.  Hash tables grow automatically as the amount of
96  * 	information increases.
97  */
98 #include "hash.h"
99 #include "ealloc.h"
100 
101 /*
102  * Forward references to local procedures that are used before they're
103  * defined:
104  */
105 
106 static void RebuildTable(Hash_Table *);
107 
108 /*
109  * The following defines the ratio of # entries to # buckets
110  * at which we rebuild the table to make it larger.
111  */
112 
113 #define rebuildLimit 8
114 
115 /*
116  *---------------------------------------------------------
117  *
118  * Hash_InitTable --
119  *
120  *	This routine just sets up the hash table.
121  *
122  * Input:
123  *	t		Structure to use to hold table.
124  *	numBuckets	How many buckets to create for starters.  This number
125  *			is rounded up to a power of two.  If <= 0, a reasonable
126  *			default is chosen. The table will grow in size later
127  *			as needed.
128  *
129  * Results:
130  *	None.
131  *
132  * Side Effects:
133  *	Memory is allocated for the initial bucket area.
134  *
135  *---------------------------------------------------------
136  */
137 
138 void
139 Hash_InitTable(Hash_Table *t, int numBuckets)
140 {
141 	int i;
142 	struct Hash_Entry **hp;
143 
144 	/*
145 	 * Round up the size to a power of two.
146 	 */
147 	if (numBuckets <= 0)
148 		i = 16;
149 	else {
150 		for (i = 2; i < numBuckets; i <<= 1)
151 			 continue;
152 	}
153 	t->numEntries = 0;
154 	t->size = i;
155 	t->mask = i - 1;
156 	t->bucketPtr = hp = (struct Hash_Entry **)emalloc(sizeof(*hp) * i);
157 	while (--i >= 0)
158 		*hp++ = NULL;
159 }
160 
161 /*
162  *---------------------------------------------------------
163  *
164  * Hash_DeleteTable --
165  *
166  *	This routine removes everything from a hash table
167  *	and frees up the memory space it occupied (except for
168  *	the space in the Hash_Table structure).
169  *
170  * Results:
171  *	None.
172  *
173  * Side Effects:
174  *	Lots of memory is freed up.
175  *
176  *---------------------------------------------------------
177  */
178 
179 void
180 Hash_DeleteTable(Hash_Table *t)
181 {
182 	struct Hash_Entry **hp, *h, *nexth;
183 	int i;
184 
185 	nexth = NULL;
186 	for (hp = t->bucketPtr, i = t->size; --i >= 0;) {
187 		for (h = *hp++; h != NULL; h = nexth) {
188 			nexth = h->next;
189 			free((char *)h);
190 		}
191 	}
192 	free((char *)t->bucketPtr);
193 
194 	/*
195 	 * Set up the hash table to cause memory faults on any future access
196 	 * attempts until re-initialization.
197 	 */
198 	t->bucketPtr = NULL;
199 }
200 
201 /*
202  *---------------------------------------------------------
203  *
204  * Hash_FindEntry --
205  *
206  * 	Searches a hash table for an entry corresponding to key.
207  *
208  * Input:
209  *	t	Hash table to search.
210  *	key	A hash key.
211  *
212  * Results:
213  *	The return value is a pointer to the entry for key,
214  *	if key was present in the table.  If key was not
215  *	present, NULL is returned.
216  *
217  * Side Effects:
218  *	None.
219  *
220  *---------------------------------------------------------
221  */
222 
223 Hash_Entry *
224 Hash_FindEntry(Hash_Table *t, char *key)
225 {
226 	Hash_Entry *e;
227 	unsigned h;
228 	char *p;
229 
230 	for (h = 0, p = key; *p;)
231 		h = (h << 5) - h + *p++;
232 	p = key;
233 	for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next)
234 		if (e->namehash == h && strcmp(e->name, p) == 0)
235 			return (e);
236 	return (NULL);
237 }
238 
239 /*
240  *---------------------------------------------------------
241  *
242  * Hash_CreateEntry --
243  *
244  *	Searches a hash table for an entry corresponding to
245  *	key.  If no entry is found, then one is created.
246  *
247  * Input:
248  * 	t	Hash table to search.
249  *	key	A hash key.
250  *	newPtr	Filled in with 1 if new entry created, 0 otherwise.
251  *
252  * Results:
253  *	The return value is a pointer to the entry.  If *newPtr
254  *	isn't NULL, then *newPtr is filled in with TRUE if a
255  *	new entry was created, and FALSE if an entry already existed
256  *	with the given key.
257  *
258  * Side Effects:
259  *	Memory may be allocated, and the hash buckets may be modified.
260  *---------------------------------------------------------
261  */
262 
263 Hash_Entry *
264 Hash_CreateEntry(Hash_Table *t, char *key, int *newPtr)
265 {
266 	Hash_Entry *e;
267 	unsigned h;
268 	char *p;
269 	int keylen;
270 	struct Hash_Entry **hp;
271 
272 	/*
273 	 * Hash the key.  As a side effect, save the length (strlen) of the
274 	 * key in case we need to create the entry.
275 	 */
276 	for (h = 0, p = key; *p;)
277 		h = (h << 5) - h + *p++;
278 	keylen = p - key;
279 	p = key;
280 	for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next) {
281 		if (e->namehash == h && strcmp(e->name, p) == 0) {
282 			if (newPtr != NULL)
283 				*newPtr = 0;
284 			return (e);
285 		}
286 	}
287 
288 	/*
289 	 * The desired entry isn't there.  Before allocating a new entry,
290 	 * expand the table if necessary (and this changes the resulting
291 	 * bucket chain).
292 	 */
293 	if (t->numEntries >= rebuildLimit * t->size)
294 		RebuildTable(t);
295 	e = (Hash_Entry *) emalloc(sizeof(*e) + keylen);
296 	hp = &t->bucketPtr[h & t->mask];
297 	e->next = *hp;
298 	*hp = e;
299 	e->clientData = NULL;
300 	e->namehash = h;
301 	(void) strcpy(e->name, p);
302 	t->numEntries++;
303 
304 	if (newPtr != NULL)
305 		*newPtr = 1;
306 	return (e);
307 }
308 
309 /*
310  *---------------------------------------------------------
311  *
312  * Hash_DeleteEntry --
313  *
314  * 	Delete the given hash table entry and free memory associated with
315  *	it.
316  *
317  * Results:
318  *	None.
319  *
320  * Side Effects:
321  *	Hash chain that entry lives in is modified and memory is freed.
322  *
323  *---------------------------------------------------------
324  */
325 
326 void
327 Hash_DeleteEntry(Hash_Table *t, Hash_Entry *e)
328 {
329 	Hash_Entry **hp, *p;
330 
331 	if (e == NULL)
332 		return;
333 	for (hp = &t->bucketPtr[e->namehash & t->mask];
334 	     (p = *hp) != NULL; hp = &p->next) {
335 		if (p == e) {
336 			*hp = p->next;
337 			free((char *)p);
338 			t->numEntries--;
339 			return;
340 		}
341 	}
342 	(void)write(2, "bad call to Hash_DeleteEntry\n", 29);
343 	abort();
344 }
345 
346 /*
347  *---------------------------------------------------------
348  *
349  * Hash_EnumFirst --
350  *	This procedure sets things up for a complete search
351  *	of all entries recorded in the hash table.
352  *
353  * Input:
354  *	t		Table to be searched.
355  *	searchPtr	Area in which to keep state about search.
356  *
357  * Results:
358  *	The return value is the address of the first entry in
359  *	the hash table, or NULL if the table is empty.
360  *
361  * Side Effects:
362  *	The information in searchPtr is initialized so that successive
363  *	calls to Hash_Next will return successive HashEntry's
364  *	from the table.
365  *
366  *---------------------------------------------------------
367  */
368 
369 Hash_Entry *
370 Hash_EnumFirst(Hash_Table *t, Hash_Search *searchPtr)
371 {
372 
373 	searchPtr->tablePtr = t;
374 	searchPtr->nextIndex = 0;
375 	searchPtr->hashEntryPtr = NULL;
376 	return Hash_EnumNext(searchPtr);
377 }
378 
379 /*
380  *---------------------------------------------------------
381  *
382  * Hash_EnumNext --
383  *    This procedure returns successive entries in the hash table.
384  *
385  * Results:
386  *    The return value is a pointer to the next HashEntry
387  *    in the table, or NULL when the end of the table is
388  *    reached.
389  *
390  * Side Effects:
391  *    The information in searchPtr is modified to advance to the
392  *    next entry.
393  *
394  *---------------------------------------------------------
395  */
396 
397 Hash_Entry *
398 Hash_EnumNext(Hash_Search *searchPtr)
399 {
400 	Hash_Entry *e;
401 	Hash_Table *t = searchPtr->tablePtr;
402 
403 	/*
404 	 * The hashEntryPtr field points to the most recently returned
405 	 * entry, or is nil if we are starting up.  If not nil, we have
406 	 * to start at the next one in the chain.
407 	 */
408 	e = searchPtr->hashEntryPtr;
409 	if (e != NULL)
410 		e = e->next;
411 	/*
412 	 * If the chain ran out, or if we are starting up, we need to
413 	 * find the next nonempty chain.
414 	 */
415 	while (e == NULL) {
416 		if (searchPtr->nextIndex >= t->size)
417 			return (NULL);
418 		e = t->bucketPtr[searchPtr->nextIndex++];
419 	}
420 	searchPtr->hashEntryPtr = e;
421 	return (e);
422 }
423 
424 /*
425  *---------------------------------------------------------
426  *
427  * RebuildTable --
428  *	This local routine makes a new hash table that
429  *	is larger than the old one.
430  *
431  * Results:
432  * 	None.
433  *
434  * Side Effects:
435  *	The entire hash table is moved, so any bucket numbers
436  *	from the old table are invalid.
437  *
438  *---------------------------------------------------------
439  */
440 
441 static void
442 RebuildTable(Hash_Table *t)
443 {
444 	Hash_Entry *e, *next, **hp, **xp;
445 	int i, mask;
446         Hash_Entry **oldhp;
447 	int oldsize;
448 
449 	next = NULL;
450 	oldhp = t->bucketPtr;
451 	oldsize = i = t->size;
452 	i <<= 1;
453 	t->size = i;
454 	t->mask = mask = i - 1;
455 	t->bucketPtr = hp = (struct Hash_Entry **) emalloc(sizeof(*hp) * i);
456 	while (--i >= 0)
457 		*hp++ = NULL;
458 	for (hp = oldhp, i = oldsize; --i >= 0;) {
459 		for (e = *hp++; e != NULL; e = next) {
460 			next = e->next;
461 			xp = &t->bucketPtr[e->namehash & mask];
462 			e->next = *xp;
463 			*xp = e;
464 		}
465 	}
466 	free((char *)oldhp);
467 }
468