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