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