Remove building with NOCRYPTO option
[minix.git] / sbin / rcorder / hash.c
blobcaebde1afcef04307c2588ba7ba330646e1205d8
1 /* $NetBSD: hash.c,v 1.5 2007/03/03 00:09:30 simonb Exp $ */
3 /*
4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5 * All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor.
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.
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.
36 * Copyright (c) 1988, 1989 by Adam de Boor
37 * Copyright (c) 1989 by Berkeley Softworks
38 * All rights reserved.
40 * This code is derived from software contributed to Berkeley by
41 * Adam de Boor.
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.
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.
72 #ifdef MAKE_BOOTSTRAP
73 static char rcsid[] = "$NetBSD: hash.c,v 1.5 2007/03/03 00:09:30 simonb 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.5 2007/03/03 00:09:30 simonb Exp $");
81 #endif
82 #endif /* not lint */
83 #endif
85 #include <sys/types.h>
87 #include <stdlib.h>
88 #include <string.h>
89 #include <unistd.h>
90 #include <err.h>
91 #include <util.h>
93 /* hash.c --
95 * This module contains routines to manipulate a hash table.
96 * See hash.h for a definition of the structure of the hash
97 * table. Hash tables grow automatically as the amount of
98 * information increases.
100 #include "hash.h"
103 * Forward references to local procedures that are used before they're
104 * defined:
107 static void RebuildTable(Hash_Table *);
110 * The following defines the ratio of # entries to # buckets
111 * at which we rebuild the table to make it larger.
114 #define rebuildLimit 8
117 *---------------------------------------------------------
119 * Hash_InitTable --
121 * This routine just sets up the hash table.
123 * Input:
124 * t Structure to use to hold table.
125 * numBuckets How many buckets to create for starters. This number
126 * is rounded up to a power of two. If <= 0, a reasonable
127 * default is chosen. The table will grow in size later
128 * as needed.
130 * Results:
131 * None.
133 * Side Effects:
134 * Memory is allocated for the initial bucket area.
136 *---------------------------------------------------------
139 void
140 Hash_InitTable(Hash_Table *t, int numBuckets)
142 int i;
143 struct Hash_Entry **hp;
146 * Round up the size to a power of two.
148 if (numBuckets <= 0)
149 i = 16;
150 else {
151 for (i = 2; i < numBuckets; i <<= 1)
152 continue;
154 t->numEntries = 0;
155 t->size = i;
156 t->mask = i - 1;
157 t->bucketPtr = hp = (struct Hash_Entry **)emalloc(sizeof(*hp) * i);
158 while (--i >= 0)
159 *hp++ = NULL;
163 *---------------------------------------------------------
165 * Hash_DeleteTable --
167 * This routine removes everything from a hash table
168 * and frees up the memory space it occupied (except for
169 * the space in the Hash_Table structure).
171 * Results:
172 * None.
174 * Side Effects:
175 * Lots of memory is freed up.
177 *---------------------------------------------------------
180 void
181 Hash_DeleteTable(Hash_Table *t)
183 struct Hash_Entry **hp, *h, *nexth;
184 int i;
186 nexth = NULL;
187 for (hp = t->bucketPtr, i = t->size; --i >= 0;) {
188 for (h = *hp++; h != NULL; h = nexth) {
189 nexth = h->next;
190 free(h);
193 free(t->bucketPtr);
196 * Set up the hash table to cause memory faults on any future access
197 * attempts until re-initialization.
199 t->bucketPtr = NULL;
203 *---------------------------------------------------------
205 * Hash_FindEntry --
207 * Searches a hash table for an entry corresponding to key.
209 * Input:
210 * t Hash table to search.
211 * key A hash key.
213 * Results:
214 * The return value is a pointer to the entry for key,
215 * if key was present in the table. If key was not
216 * present, NULL is returned.
218 * Side Effects:
219 * None.
221 *---------------------------------------------------------
224 Hash_Entry *
225 Hash_FindEntry(Hash_Table *t, char *key)
227 Hash_Entry *e;
228 unsigned h;
229 char *p;
231 for (h = 0, p = key; *p;)
232 h = (h << 5) - h + *p++;
233 p = key;
234 for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next)
235 if (e->namehash == h && strcmp(e->name, p) == 0)
236 return (e);
237 return (NULL);
241 *---------------------------------------------------------
243 * Hash_CreateEntry --
245 * Searches a hash table for an entry corresponding to
246 * key. If no entry is found, then one is created.
248 * Input:
249 * t Hash table to search.
250 * key A hash key.
251 * newPtr Filled in with 1 if new entry created, 0 otherwise.
253 * Results:
254 * The return value is a pointer to the entry. If *newPtr
255 * isn't NULL, then *newPtr is filled in with TRUE if a
256 * new entry was created, and FALSE if an entry already existed
257 * with the given key.
259 * Side Effects:
260 * Memory may be allocated, and the hash buckets may be modified.
261 *---------------------------------------------------------
264 Hash_Entry *
265 Hash_CreateEntry(Hash_Table *t, char *key, int *newPtr)
267 Hash_Entry *e;
268 unsigned h;
269 char *p;
270 int keylen;
271 struct Hash_Entry **hp;
274 * Hash the key. As a side effect, save the length (strlen) of the
275 * key in case we need to create the entry.
277 for (h = 0, p = key; *p;)
278 h = (h << 5) - h + *p++;
279 keylen = p - key;
280 p = key;
281 for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next) {
282 if (e->namehash == h && strcmp(e->name, p) == 0) {
283 if (newPtr != NULL)
284 *newPtr = 0;
285 return (e);
290 * The desired entry isn't there. Before allocating a new entry,
291 * expand the table if necessary (and this changes the resulting
292 * bucket chain).
294 if (t->numEntries >= rebuildLimit * t->size)
295 RebuildTable(t);
296 e = (Hash_Entry *) emalloc(sizeof(*e) + keylen);
297 hp = &t->bucketPtr[h & t->mask];
298 e->next = *hp;
299 *hp = e;
300 e->clientData = NULL;
301 e->namehash = h;
302 (void) strcpy(e->name, p);
303 t->numEntries++;
305 if (newPtr != NULL)
306 *newPtr = 1;
307 return (e);
311 *---------------------------------------------------------
313 * Hash_DeleteEntry --
315 * Delete the given hash table entry and free memory associated with
316 * it.
318 * Results:
319 * None.
321 * Side Effects:
322 * Hash chain that entry lives in is modified and memory is freed.
324 *---------------------------------------------------------
327 void
328 Hash_DeleteEntry(Hash_Table *t, Hash_Entry *e)
330 Hash_Entry **hp, *p;
332 if (e == NULL)
333 return;
334 for (hp = &t->bucketPtr[e->namehash & t->mask];
335 (p = *hp) != NULL; hp = &p->next) {
336 if (p == e) {
337 *hp = p->next;
338 free(p);
339 t->numEntries--;
340 return;
343 (void)write(2, "bad call to Hash_DeleteEntry\n", 29);
344 abort();
348 *---------------------------------------------------------
350 * Hash_EnumFirst --
351 * This procedure sets things up for a complete search
352 * of all entries recorded in the hash table.
354 * Input:
355 * t Table to be searched.
356 * searchPtr Area in which to keep state about search.
358 * Results:
359 * The return value is the address of the first entry in
360 * the hash table, or NULL if the table is empty.
362 * Side Effects:
363 * The information in searchPtr is initialized so that successive
364 * calls to Hash_Next will return successive HashEntry's
365 * from the table.
367 *---------------------------------------------------------
370 Hash_Entry *
371 Hash_EnumFirst(Hash_Table *t, Hash_Search *searchPtr)
374 searchPtr->tablePtr = t;
375 searchPtr->nextIndex = 0;
376 searchPtr->hashEntryPtr = NULL;
377 return Hash_EnumNext(searchPtr);
381 *---------------------------------------------------------
383 * Hash_EnumNext --
384 * This procedure returns successive entries in the hash table.
386 * Results:
387 * The return value is a pointer to the next HashEntry
388 * in the table, or NULL when the end of the table is
389 * reached.
391 * Side Effects:
392 * The information in searchPtr is modified to advance to the
393 * next entry.
395 *---------------------------------------------------------
398 Hash_Entry *
399 Hash_EnumNext(Hash_Search *searchPtr)
401 Hash_Entry *e;
402 Hash_Table *t = searchPtr->tablePtr;
405 * The hashEntryPtr field points to the most recently returned
406 * entry, or is nil if we are starting up. If not nil, we have
407 * to start at the next one in the chain.
409 e = searchPtr->hashEntryPtr;
410 if (e != NULL)
411 e = e->next;
413 * If the chain ran out, or if we are starting up, we need to
414 * find the next nonempty chain.
416 while (e == NULL) {
417 if (searchPtr->nextIndex >= t->size)
418 return (NULL);
419 e = t->bucketPtr[searchPtr->nextIndex++];
421 searchPtr->hashEntryPtr = e;
422 return (e);
426 *---------------------------------------------------------
428 * RebuildTable --
429 * This local routine makes a new hash table that
430 * is larger than the old one.
432 * Results:
433 * None.
435 * Side Effects:
436 * The entire hash table is moved, so any bucket numbers
437 * from the old table are invalid.
439 *---------------------------------------------------------
442 static void
443 RebuildTable(Hash_Table *t)
445 Hash_Entry *e, *next, **hp, **xp;
446 int i, mask;
447 Hash_Entry **oldhp;
448 int oldsize;
450 next = NULL;
451 oldhp = t->bucketPtr;
452 oldsize = i = t->size;
453 i <<= 1;
454 t->size = i;
455 t->mask = mask = i - 1;
456 t->bucketPtr = hp = (struct Hash_Entry **) emalloc(sizeof(*hp) * i);
457 while (--i >= 0)
458 *hp++ = NULL;
459 for (hp = oldhp, i = oldsize; --i >= 0;) {
460 for (e = *hp++; e != NULL; e = next) {
461 next = e->next;
462 xp = &t->bucketPtr[e->namehash & mask];
463 e->next = *xp;
464 *xp = e;
467 free(oldhp);