kernel - Add support for the CPU_AMD64X2_INTR_SPAM option to x86_64
[dragonfly.git] / sbin / rcorder / hash.c
blob357920812ed4d99835f37f1b5303229ef21343d7
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.
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. 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.
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.
38 * $NetBSD: hash.c,v 1.1.1.1 1999/11/19 04:30:56 mrg Exp $
41 #include <sys/types.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
47 /* hash.c --
49 * This module contains routines to manipulate a hash table.
50 * See hash.h for a definition of the structure of the hash
51 * table. Hash tables grow automatically as the amount of
52 * information increases.
54 #include "sprite.h"
55 #ifndef ORDER
56 #include "make.h"
57 #endif /* ORDER */
58 #include "hash.h"
59 #include "ealloc.h"
62 * Forward references to local procedures that are used before they're
63 * defined:
66 static void RebuildTable(Hash_Table *);
69 * The following defines the ratio of # entries to # buckets
70 * at which we rebuild the table to make it larger.
73 #define rebuildLimit 8
76 *---------------------------------------------------------
78 * Hash_InitTable --
80 * This routine just sets up the hash table.
82 * Results:
83 * None.
85 * Side Effects:
86 * Memory is allocated for the initial bucket area.
88 *---------------------------------------------------------
92 * Hash_Table *t; Structure to use to hold table.
93 * int numBuckets; How many buckets to create for starters.
94 * This number is rounded up to a power of
95 * two. If <= 0, a reasonable default is
96 * chosen. The table will grow in size later
97 * as needed.
99 void
100 Hash_InitTable(Hash_Table *t, int numBuckets)
102 int i;
103 struct Hash_Entry **hp;
106 * Round up the size to a power of two.
108 if (numBuckets <= 0)
109 i = 16;
110 else {
111 for (i = 2; i < numBuckets; i <<= 1)
112 continue;
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;
123 *---------------------------------------------------------
125 * Hash_DeleteTable --
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).
131 * Results:
132 * None.
134 * Side Effects:
135 * Lots of memory is freed up.
137 *---------------------------------------------------------
140 void
141 Hash_DeleteTable(Hash_Table *t)
143 struct Hash_Entry **hp, *h, *nexth = NULL;
144 int i;
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);
152 free((char *)t->bucketPtr);
155 * Set up the hash table to cause memory faults on any future access
156 * attempts until re-initialization.
158 t->bucketPtr = NULL;
162 *---------------------------------------------------------
164 * Hash_FindEntry --
166 * Searches a hash table for an entry corresponding to key.
168 * Results:
169 * The return value is a pointer to the entry for key,
170 * if key was present in the table. If key was not
171 * present, NULL is returned.
173 * Side Effects:
174 * None.
176 *---------------------------------------------------------
179 Hash_Entry *
180 Hash_FindEntry(Hash_Table *t, char *key)
182 Hash_Entry *e;
183 unsigned h;
184 char *p;
186 for (h = 0, p = key; *p;)
187 h = (h << 5) - h + *p++;
188 p = key;
189 for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next)
190 if (e->namehash == h && strcmp(e->name, p) == 0)
191 return (e);
192 return (NULL);
196 *---------------------------------------------------------
198 * Hash_CreateEntry --
200 * Searches a hash table for an entry corresponding to
201 * key. If no entry is found, then one is created.
203 * Results:
204 * The return value is a pointer to the entry. If *newPtr
205 * isn't NULL, then *newPtr is filled in with TRUE if a
206 * new entry was created, and FALSE if an entry already existed
207 * with the given key.
209 * Side Effects:
210 * Memory may be allocated, and the hash buckets may be modified.
211 *---------------------------------------------------------
214 Hash_Entry *
215 Hash_CreateEntry(Hash_Table *t, char *key, Boolean *newPtr)
217 Hash_Entry *e;
218 unsigned h;
219 char *p;
220 int keylen;
221 struct Hash_Entry **hp;
224 * Hash the key. As a side effect, save the length (strlen) of the
225 * key in case we need to create the entry.
227 for (h = 0, p = key; *p;)
228 h = (h << 5) - h + *p++;
229 keylen = p - key;
230 p = key;
231 for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next) {
232 if (e->namehash == h && strcmp(e->name, p) == 0) {
233 if (newPtr != NULL)
234 *newPtr = FALSE;
235 return (e);
240 * The desired entry isn't there. Before allocating a new entry,
241 * expand the table if necessary (and this changes the resulting
242 * bucket chain).
244 if (t->numEntries >= rebuildLimit * t->size)
245 RebuildTable(t);
246 e = (Hash_Entry *) emalloc(sizeof(*e) + keylen);
247 hp = &t->bucketPtr[h & t->mask];
248 e->next = *hp;
249 *hp = e;
250 e->clientData = NULL;
251 e->namehash = h;
252 strcpy(e->name, p);
253 t->numEntries++;
255 if (newPtr != NULL)
256 *newPtr = TRUE;
257 return (e);
261 *---------------------------------------------------------
263 * Hash_DeleteEntry --
265 * Delete the given hash table entry and free memory associated with
266 * it.
268 * Results:
269 * None.
271 * Side Effects:
272 * Hash chain that entry lives in is modified and memory is freed.
274 *---------------------------------------------------------
277 void
278 Hash_DeleteEntry(Hash_Table *t, Hash_Entry *e)
280 Hash_Entry **hp, *p;
282 if (e == NULL)
283 return;
284 for (hp = &t->bucketPtr[e->namehash & t->mask];
285 (p = *hp) != NULL; hp = &p->next) {
286 if (p == e) {
287 *hp = p->next;
288 free((char *)p);
289 t->numEntries--;
290 return;
293 write(2, "bad call to Hash_DeleteEntry\n", 29);
294 abort();
298 *---------------------------------------------------------
300 * Hash_EnumFirst --
301 * This procedure sets things up for a complete search
302 * of all entries recorded in the hash table.
304 * Results:
305 * The return value is the address of the first entry in
306 * the hash table, or NULL if the table is empty.
308 * Side Effects:
309 * The information in searchPtr is initialized so that successive
310 * calls to Hash_Next will return successive HashEntry's
311 * from the table.
313 *---------------------------------------------------------
316 Hash_Entry *
317 Hash_EnumFirst(Hash_Table *t, Hash_Search *searchPtr)
319 searchPtr->tablePtr = t;
320 searchPtr->nextIndex = 0;
321 searchPtr->hashEntryPtr = NULL;
322 return Hash_EnumNext(searchPtr);
326 *---------------------------------------------------------
328 * Hash_EnumNext --
329 * This procedure returns successive entries in the hash table.
331 * Results:
332 * The return value is a pointer to the next HashEntry
333 * in the table, or NULL when the end of the table is
334 * reached.
336 * Side Effects:
337 * The information in searchPtr is modified to advance to the
338 * next entry.
340 *---------------------------------------------------------
343 Hash_Entry *
344 Hash_EnumNext(Hash_Search *searchPtr)
346 Hash_Entry *e;
347 Hash_Table *t = searchPtr->tablePtr;
350 * The hashEntryPtr field points to the most recently returned
351 * entry, or is nil if we are starting up. If not nil, we have
352 * to start at the next one in the chain.
354 e = searchPtr->hashEntryPtr;
355 if (e != NULL)
356 e = e->next;
358 * If the chain ran out, or if we are starting up, we need to
359 * find the next nonempty chain.
361 while (e == NULL) {
362 if (searchPtr->nextIndex >= t->size)
363 return (NULL);
364 e = t->bucketPtr[searchPtr->nextIndex++];
366 searchPtr->hashEntryPtr = e;
367 return (e);
371 *---------------------------------------------------------
373 * RebuildTable --
374 * This local routine makes a new hash table that
375 * is larger than the old one.
377 * Results:
378 * None.
380 * Side Effects:
381 * The entire hash table is moved, so any bucket numbers
382 * from the old table are invalid.
384 *---------------------------------------------------------
387 static void
388 RebuildTable(Hash_Table *t)
390 Hash_Entry *e, *next = NULL, **hp, **xp;
391 int i, mask;
392 Hash_Entry **oldhp;
393 int oldsize;
395 oldhp = t->bucketPtr;
396 oldsize = i = t->size;
397 i <<= 1;
398 t->size = i;
399 t->mask = mask = i - 1;
400 t->bucketPtr = hp = (struct Hash_Entry **) emalloc(sizeof(*hp) * i);
401 while (--i >= 0)
402 *hp++ = NULL;
403 for (hp = oldhp, i = oldsize; --i >= 0;) {
404 for (e = *hp++; e != NULL; e = next) {
405 next = e->next;
406 xp = &t->bucketPtr[e->namehash & mask];
407 e->next = *xp;
408 *xp = e;
411 free((char *)oldhp);