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