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