Merge from vendor branch OPENSSH:
[dragonfly.git] / sbin / rcorder / hash.c
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  * $DragonFly: src/sbin/rcorder/hash.c,v 1.3 2003/11/01 17:16:01 drhodus Exp $
40  */
41
42 #ifdef MAKE_BOOTSTRAP
43 static char rcsid[] = "$NetBSD: hash.c,v 1.1.1.1 1999/11/19 04:30:56 mrg Exp $";
44 #else
45 #include <sys/cdefs.h>
46 #ifndef lint
47 #if 0
48 static char sccsid[] = "@(#)hash.c      8.1 (Berkeley) 6/6/93";
49 #else
50 __RCSID("$NetBSD: hash.c,v 1.1.1.1 1999/11/19 04:30:56 mrg Exp $");
51 #endif
52 #endif /* not lint */
53 #endif
54
55 #include <sys/types.h>
56
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60
61 /* hash.c --
62  *
63  *      This module contains routines to manipulate a hash table.
64  *      See hash.h for a definition of the structure of the hash
65  *      table.  Hash tables grow automatically as the amount of
66  *      information increases.
67  */
68 #include "sprite.h"
69 #ifndef ORDER
70 #include "make.h"
71 #endif /* ORDER */
72 #include "hash.h"
73 #include "ealloc.h"
74
75 /*
76  * Forward references to local procedures that are used before they're
77  * defined:
78  */
79
80 static void RebuildTable(Hash_Table *);
81
82 /*
83  * The following defines the ratio of # entries to # buckets
84  * at which we rebuild the table to make it larger.
85  */
86
87 #define rebuildLimit 8
88
89 /*
90  *---------------------------------------------------------
91  *
92  * Hash_InitTable --
93  *
94  *      This routine just sets up the hash table.
95  *
96  * Results:
97  *      None.
98  *
99  * Side Effects:
100  *      Memory is allocated for the initial bucket area.
101  *
102  *---------------------------------------------------------
103  */
104
105 /*
106  * register Hash_Table *t;      Structure to use to hold table. 
107  * int numBuckets;                      How many buckets to create for starters.
108  *                                                              This number is rounded up to a power of
109  *                                                              two.   If <= 0, a reasonable default is
110  *                                                              chosen. The table will grow in size later
111  *                                                              as needed.
112  */
113 void
114 Hash_InitTable(register Hash_Table *t, int numBuckets)
115 {
116         register int i;
117         register struct Hash_Entry **hp;
118
119         /*
120          * Round up the size to a power of two.
121          */
122         if (numBuckets <= 0)
123                 i = 16;
124         else {
125                 for (i = 2; i < numBuckets; i <<= 1)
126                          continue;
127         }
128         t->numEntries = 0;
129         t->size = i;
130         t->mask = i - 1;
131         t->bucketPtr = hp = (struct Hash_Entry **)emalloc(sizeof(*hp) * i);
132         while (--i >= 0)
133                 *hp++ = NULL;
134 }
135
136 /*
137  *---------------------------------------------------------
138  *
139  * Hash_DeleteTable --
140  *
141  *      This routine removes everything from a hash table
142  *      and frees up the memory space it occupied (except for
143  *      the space in the Hash_Table structure).
144  *
145  * Results:
146  *      None.
147  *
148  * Side Effects:
149  *      Lots of memory is freed up.
150  *
151  *---------------------------------------------------------
152  */
153
154 void
155 Hash_DeleteTable(Hash_Table *t)
156 {
157         register struct Hash_Entry **hp, *h, *nexth = NULL;
158         register int i;
159
160         for (hp = t->bucketPtr, i = t->size; --i >= 0;) {
161                 for (h = *hp++; h != NULL; h = nexth) {
162                         nexth = h->next;
163                         free((char *)h);
164                 }
165         }
166         free((char *)t->bucketPtr);
167
168         /*
169          * Set up the hash table to cause memory faults on any future access
170          * attempts until re-initialization.
171          */
172         t->bucketPtr = NULL;
173 }
174
175 /*
176  *---------------------------------------------------------
177  *
178  * Hash_FindEntry --
179  *
180  *      Searches a hash table for an entry corresponding to key.
181  *
182  * Results:
183  *      The return value is a pointer to the entry for key,
184  *      if key was present in the table.  If key was not
185  *      present, NULL is returned.
186  *
187  * Side Effects:
188  *      None.
189  *
190  *---------------------------------------------------------
191  */
192
193 Hash_Entry *
194 Hash_FindEntry(Hash_Table *t, char *key)
195 {
196         register Hash_Entry *e;
197         register unsigned h;
198         register char *p;
199
200         for (h = 0, p = key; *p;)
201                 h = (h << 5) - h + *p++;
202         p = key;
203         for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next)
204                 if (e->namehash == h && strcmp(e->name, p) == 0)
205                         return (e);
206         return (NULL);
207 }
208
209 /*
210  *---------------------------------------------------------
211  *
212  * Hash_CreateEntry --
213  *
214  *      Searches a hash table for an entry corresponding to
215  *      key.  If no entry is found, then one is created.
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(register Hash_Table *t, char *key, Boolean *newPtr)
230 {
231         register Hash_Entry *e;
232         register unsigned h;
233         register 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         (void) 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
280  *      it.
281  *
282  * Results:
283  *      None.
284  *
285  * Side Effects:
286  *      Hash chain that entry lives in is modified and memory is freed.
287  *
288  *---------------------------------------------------------
289  */
290
291 void
292 Hash_DeleteEntry(Hash_Table *t, Hash_Entry *e)
293 {
294         register Hash_Entry **hp, *p;
295
296         if (e == NULL)
297                 return;
298         for (hp = &t->bucketPtr[e->namehash & t->mask];
299              (p = *hp) != NULL; hp = &p->next) {
300                 if (p == e) {
301                         *hp = p->next;
302                         free((char *)p);
303                         t->numEntries--;
304                         return;
305                 }
306         }
307         (void)write(2, "bad call to Hash_DeleteEntry\n", 29);
308         abort();
309 }
310
311 /*
312  *---------------------------------------------------------
313  *
314  * Hash_EnumFirst --
315  *      This procedure sets things up for a complete search
316  *      of all entries recorded in the hash table.
317  *
318  * Results:
319  *      The return value is the address of the first entry in
320  *      the hash table, or NULL if the table is empty.
321  *
322  * Side Effects:
323  *      The information in searchPtr is initialized so that successive
324  *      calls to Hash_Next will return successive HashEntry's
325  *      from the table.
326  *
327  *---------------------------------------------------------
328  */
329
330 Hash_Entry *
331 Hash_EnumFirst(Hash_Table *t, register Hash_Search *searchPtr)
332 {
333         searchPtr->tablePtr = t;
334         searchPtr->nextIndex = 0;
335         searchPtr->hashEntryPtr = NULL;
336         return Hash_EnumNext(searchPtr);
337 }
338
339 /*
340  *---------------------------------------------------------
341  *
342  * Hash_EnumNext --
343  *    This procedure returns successive entries in the hash table.
344  *
345  * Results:
346  *    The return value is a pointer to the next HashEntry
347  *    in the table, or NULL when the end of the table is
348  *    reached.
349  *
350  * Side Effects:
351  *    The information in searchPtr is modified to advance to the
352  *    next entry.
353  *
354  *---------------------------------------------------------
355  */
356
357 Hash_Entry *
358 Hash_EnumNext(register Hash_Search *searchPtr)
359 {
360         register Hash_Entry *e;
361         Hash_Table *t = searchPtr->tablePtr;
362
363         /*
364          * The hashEntryPtr field points to the most recently returned
365          * entry, or is nil if we are starting up.  If not nil, we have
366          * to start at the next one in the chain.
367          */
368         e = searchPtr->hashEntryPtr;
369         if (e != NULL)
370                 e = e->next;
371         /*
372          * If the chain ran out, or if we are starting up, we need to
373          * find the next nonempty chain.
374          */
375         while (e == NULL) {
376                 if (searchPtr->nextIndex >= t->size)
377                         return (NULL);
378                 e = t->bucketPtr[searchPtr->nextIndex++];
379         }
380         searchPtr->hashEntryPtr = e;
381         return (e);
382 }
383
384 /*
385  *---------------------------------------------------------
386  *
387  * RebuildTable --
388  *      This local routine makes a new hash table that
389  *      is larger than the old one.
390  *
391  * Results:
392  *      None.
393  *
394  * Side Effects:
395  *      The entire hash table is moved, so any bucket numbers
396  *      from the old table are invalid.
397  *
398  *---------------------------------------------------------
399  */
400
401 static void
402 RebuildTable(register Hash_Table *t)
403 {
404         register Hash_Entry *e, *next = NULL, **hp, **xp;
405         register int i, mask;
406         register Hash_Entry **oldhp;
407         int oldsize;
408
409         oldhp = t->bucketPtr;
410         oldsize = i = t->size;
411         i <<= 1;
412         t->size = i;
413         t->mask = mask = i - 1;
414         t->bucketPtr = hp = (struct Hash_Entry **) emalloc(sizeof(*hp) * i);
415         while (--i >= 0)
416                 *hp++ = NULL;
417         for (hp = oldhp, i = oldsize; --i >= 0;) {
418                 for (e = *hp++; e != NULL; e = next) {
419                         next = e->next;
420                         xp = &t->bucketPtr[e->namehash & mask];
421                         e->next = *xp;
422                         *xp = e;
423                 }
424         }
425         free((char *)oldhp);
426 }