| 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 |  | 
|---|
| 39 | #ifndef lint | 
|---|
| 40 | #if 0 | 
|---|
| 41 | static char sccsid[] = "@(#)hash.c      8.1 (Berkeley) 6/6/93"; | 
|---|
| 42 | #else | 
|---|
| 43 | static const char rcsid[] = | 
|---|
| 44 | "$FreeBSD: src/usr.bin/make/hash.c,v 1.9 1999/09/11 13:08:01 hoek Exp $"; | 
|---|
| 45 | #endif | 
|---|
| 46 | #define KLIBFILEDEF rcsid | 
|---|
| 47 | #endif /* not lint */ | 
|---|
| 48 |  | 
|---|
| 49 | /* hash.c -- | 
|---|
| 50 | * | 
|---|
| 51 | *      This module contains routines to manipulate a hash table. | 
|---|
| 52 | *      See hash.h for a definition of the structure of the hash | 
|---|
| 53 | *      table.  Hash tables grow automatically as the amount of | 
|---|
| 54 | *      information increases. | 
|---|
| 55 | */ | 
|---|
| 56 | #include "sprite.h" | 
|---|
| 57 | #include "make.h" | 
|---|
| 58 | #include "hash.h" | 
|---|
| 59 |  | 
|---|
| 60 | /* | 
|---|
| 61 | * Forward references to local procedures that are used before they're | 
|---|
| 62 | * defined: | 
|---|
| 63 | */ | 
|---|
| 64 |  | 
|---|
| 65 | static void RebuildTable __P((Hash_Table *)); | 
|---|
| 66 |  | 
|---|
| 67 | /* | 
|---|
| 68 | * The following defines the ratio of # entries to # buckets | 
|---|
| 69 | * at which we rebuild the table to make it larger. | 
|---|
| 70 | */ | 
|---|
| 71 |  | 
|---|
| 72 | #define rebuildLimit 8 | 
|---|
| 73 |  | 
|---|
| 74 | /* | 
|---|
| 75 | *--------------------------------------------------------- | 
|---|
| 76 | * | 
|---|
| 77 | * Hash_InitTable -- | 
|---|
| 78 | * | 
|---|
| 79 | *      This routine just sets up the hash table. | 
|---|
| 80 | * | 
|---|
| 81 | * Results: | 
|---|
| 82 | *      None. | 
|---|
| 83 | * | 
|---|
| 84 | * Side Effects: | 
|---|
| 85 | *      Memory is allocated for the initial bucket area. | 
|---|
| 86 | * | 
|---|
| 87 | *--------------------------------------------------------- | 
|---|
| 88 | */ | 
|---|
| 89 |  | 
|---|
| 90 | void | 
|---|
| 91 | Hash_InitTable(t, numBuckets) | 
|---|
| 92 | register 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. */ | 
|---|
| 98 | { | 
|---|
| 99 | register int i; | 
|---|
| 100 | register struct Hash_Entry **hp; | 
|---|
| 101 |  | 
|---|
| 102 | /* | 
|---|
| 103 | * Round up the size to a power of two. | 
|---|
| 104 | */ | 
|---|
| 105 | if (numBuckets <= 0) | 
|---|
| 106 | i = 16; | 
|---|
| 107 | else { | 
|---|
| 108 | for (i = 2; i < numBuckets; i <<= 1) | 
|---|
| 109 | continue; | 
|---|
| 110 | } | 
|---|
| 111 | t->numEntries = 0; | 
|---|
| 112 | t->size = i; | 
|---|
| 113 | t->mask = i - 1; | 
|---|
| 114 | t->bucketPtr = hp = (struct Hash_Entry **)emalloc(sizeof(*hp) * i); | 
|---|
| 115 | while (--i >= 0) | 
|---|
| 116 | *hp++ = NULL; | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | /* | 
|---|
| 120 | *--------------------------------------------------------- | 
|---|
| 121 | * | 
|---|
| 122 | * Hash_DeleteTable -- | 
|---|
| 123 | * | 
|---|
| 124 | *      This routine removes everything from a hash table | 
|---|
| 125 | *      and frees up the memory space it occupied (except for | 
|---|
| 126 | *      the space in the Hash_Table structure). | 
|---|
| 127 | * | 
|---|
| 128 | * Results: | 
|---|
| 129 | *      None. | 
|---|
| 130 | * | 
|---|
| 131 | * Side Effects: | 
|---|
| 132 | *      Lots of memory is freed up. | 
|---|
| 133 | * | 
|---|
| 134 | *--------------------------------------------------------- | 
|---|
| 135 | */ | 
|---|
| 136 |  | 
|---|
| 137 | void | 
|---|
| 138 | Hash_DeleteTable(t) | 
|---|
| 139 | Hash_Table *t; | 
|---|
| 140 | { | 
|---|
| 141 | register struct Hash_Entry **hp, *h, *nexth = NULL; | 
|---|
| 142 | register int i; | 
|---|
| 143 |  | 
|---|
| 144 | for (hp = t->bucketPtr, i = t->size; --i >= 0;) { | 
|---|
| 145 | for (h = *hp++; h != NULL; h = nexth) { | 
|---|
| 146 | nexth = h->next; | 
|---|
| 147 | efree((char *)h); | 
|---|
| 148 | } | 
|---|
| 149 | } | 
|---|
| 150 | efree((char *)t->bucketPtr); | 
|---|
| 151 |  | 
|---|
| 152 | /* | 
|---|
| 153 | * Set up the hash table to cause memory faults on any future access | 
|---|
| 154 | * attempts until re-initialization. | 
|---|
| 155 | */ | 
|---|
| 156 | t->bucketPtr = NULL; | 
|---|
| 157 | } | 
|---|
| 158 |  | 
|---|
| 159 | /* | 
|---|
| 160 | *--------------------------------------------------------- | 
|---|
| 161 | * | 
|---|
| 162 | * Hash_FindEntry -- | 
|---|
| 163 | * | 
|---|
| 164 | *      Searches a hash table for an entry corresponding to 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 |  | 
|---|
| 177 | Hash_Entry * | 
|---|
| 178 | Hash_FindEntry(t, key) | 
|---|
| 179 | Hash_Table *t;          /* Hash table to search. */ | 
|---|
| 180 | char *key;              /* A hash key. */ | 
|---|
| 181 | { | 
|---|
| 182 | register Hash_Entry *e; | 
|---|
| 183 | register unsigned h; | 
|---|
| 184 | register char *p; | 
|---|
| 185 |  | 
|---|
| 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); | 
|---|
| 193 | } | 
|---|
| 194 |  | 
|---|
| 195 | /* | 
|---|
| 196 | *--------------------------------------------------------- | 
|---|
| 197 | * | 
|---|
| 198 | * Hash_CreateEntry -- | 
|---|
| 199 | * | 
|---|
| 200 | *      Searches a hash table for an entry corresponding to | 
|---|
| 201 | *      key.  If no entry is found, then one is created. | 
|---|
| 202 | * | 
|---|
| 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. | 
|---|
| 208 | * | 
|---|
| 209 | * Side Effects: | 
|---|
| 210 | *      Memory may be allocated, and the hash buckets may be modified. | 
|---|
| 211 | *--------------------------------------------------------- | 
|---|
| 212 | */ | 
|---|
| 213 |  | 
|---|
| 214 | Hash_Entry * | 
|---|
| 215 | Hash_CreateEntry(t, key, newPtr) | 
|---|
| 216 | register Hash_Table *t; /* Hash table to search. */ | 
|---|
| 217 | char *key;              /* A hash key. */ | 
|---|
| 218 | Boolean *newPtr;        /* Filled in with TRUE if new entry created, | 
|---|
| 219 | * FALSE otherwise. */ | 
|---|
| 220 | { | 
|---|
| 221 | register Hash_Entry *e; | 
|---|
| 222 | register unsigned h; | 
|---|
| 223 | register char *p; | 
|---|
| 224 | int keylen; | 
|---|
| 225 | struct Hash_Entry **hp; | 
|---|
| 226 |  | 
|---|
| 227 | /* | 
|---|
| 228 | * Hash the key.  As a side effect, save the length (strlen) of the | 
|---|
| 229 | * key in case we need to create the entry. | 
|---|
| 230 | */ | 
|---|
| 231 | for (h = 0, p = key; *p;) | 
|---|
| 232 | h = (h << 5) - h + *p++; | 
|---|
| 233 | keylen = p - key; | 
|---|
| 234 | p = key; | 
|---|
| 235 | for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next) { | 
|---|
| 236 | if (e->namehash == h && strcmp(e->name, p) == 0) { | 
|---|
| 237 | if (newPtr != NULL) | 
|---|
| 238 | *newPtr = FALSE; | 
|---|
| 239 | return (e); | 
|---|
| 240 | } | 
|---|
| 241 | } | 
|---|
| 242 |  | 
|---|
| 243 | /* | 
|---|
| 244 | * The desired entry isn't there.  Before allocating a new entry, | 
|---|
| 245 | * expand the table if necessary (and this changes the resulting | 
|---|
| 246 | * bucket chain). | 
|---|
| 247 | */ | 
|---|
| 248 | if (t->numEntries >= rebuildLimit * t->size) | 
|---|
| 249 | RebuildTable(t); | 
|---|
| 250 | e = (Hash_Entry *) emalloc(sizeof(*e) + keylen); | 
|---|
| 251 | hp = &t->bucketPtr[h & t->mask]; | 
|---|
| 252 | e->next = *hp; | 
|---|
| 253 | *hp = e; | 
|---|
| 254 | e->clientData = NULL; | 
|---|
| 255 | e->namehash = h; | 
|---|
| 256 | (void) strcpy(e->name, p); | 
|---|
| 257 | t->numEntries++; | 
|---|
| 258 |  | 
|---|
| 259 | if (newPtr != NULL) | 
|---|
| 260 | *newPtr = TRUE; | 
|---|
| 261 | return (e); | 
|---|
| 262 | } | 
|---|
| 263 |  | 
|---|
| 264 | /* | 
|---|
| 265 | *--------------------------------------------------------- | 
|---|
| 266 | * | 
|---|
| 267 | * Hash_DeleteEntry -- | 
|---|
| 268 | * | 
|---|
| 269 | *      Delete the given hash table entry and efree memory associated with | 
|---|
| 270 | *      it. | 
|---|
| 271 | * | 
|---|
| 272 | * Results: | 
|---|
| 273 | *      None. | 
|---|
| 274 | * | 
|---|
| 275 | * Side Effects: | 
|---|
| 276 | *      Hash chain that entry lives in is modified and memory is freed. | 
|---|
| 277 | * | 
|---|
| 278 | *--------------------------------------------------------- | 
|---|
| 279 | */ | 
|---|
| 280 |  | 
|---|
| 281 | void | 
|---|
| 282 | Hash_DeleteEntry(t, e) | 
|---|
| 283 | Hash_Table *t; | 
|---|
| 284 | Hash_Entry *e; | 
|---|
| 285 | { | 
|---|
| 286 | register Hash_Entry **hp, *p; | 
|---|
| 287 |  | 
|---|
| 288 | if (e == NULL) | 
|---|
| 289 | return; | 
|---|
| 290 | for (hp = &t->bucketPtr[e->namehash & t->mask]; | 
|---|
| 291 | (p = *hp) != NULL; hp = &p->next) { | 
|---|
| 292 | if (p == e) { | 
|---|
| 293 | *hp = p->next; | 
|---|
| 294 | efree((char *)p); | 
|---|
| 295 | t->numEntries--; | 
|---|
| 296 | return; | 
|---|
| 297 | } | 
|---|
| 298 | } | 
|---|
| 299 | (void) write(STDERR_FILENO, "bad call to Hash_DeleteEntry\n", 29); | 
|---|
| 300 | abort(); | 
|---|
| 301 | } | 
|---|
| 302 |  | 
|---|
| 303 | /* | 
|---|
| 304 | *--------------------------------------------------------- | 
|---|
| 305 | * | 
|---|
| 306 | * Hash_EnumFirst -- | 
|---|
| 307 | *      This procedure sets things up for a complete search | 
|---|
| 308 | *      of all entries recorded in the hash table. | 
|---|
| 309 | * | 
|---|
| 310 | * Results: | 
|---|
| 311 | *      The return value is the address of the first entry in | 
|---|
| 312 | *      the hash table, or NULL if the table is empty. | 
|---|
| 313 | * | 
|---|
| 314 | * Side Effects: | 
|---|
| 315 | *      The information in searchPtr is initialized so that successive | 
|---|
| 316 | *      calls to Hash_Next will return successive HashEntry's | 
|---|
| 317 | *      from the table. | 
|---|
| 318 | * | 
|---|
| 319 | *--------------------------------------------------------- | 
|---|
| 320 | */ | 
|---|
| 321 |  | 
|---|
| 322 | Hash_Entry * | 
|---|
| 323 | Hash_EnumFirst(t, searchPtr) | 
|---|
| 324 | Hash_Table *t;                  /* Table to be searched. */ | 
|---|
| 325 | register Hash_Search *searchPtr;/* Area in which to keep state | 
|---|
| 326 | * about search.*/ | 
|---|
| 327 | { | 
|---|
| 328 | searchPtr->tablePtr = t; | 
|---|
| 329 | searchPtr->nextIndex = 0; | 
|---|
| 330 | searchPtr->hashEntryPtr = NULL; | 
|---|
| 331 | return Hash_EnumNext(searchPtr); | 
|---|
| 332 | } | 
|---|
| 333 |  | 
|---|
| 334 | /* | 
|---|
| 335 | *--------------------------------------------------------- | 
|---|
| 336 | * | 
|---|
| 337 | * Hash_EnumNext -- | 
|---|
| 338 | *    This procedure returns successive entries in the hash table. | 
|---|
| 339 | * | 
|---|
| 340 | * Results: | 
|---|
| 341 | *    The return value is a pointer to the next HashEntry | 
|---|
| 342 | *    in the table, or NULL when the end of the table is | 
|---|
| 343 | *    reached. | 
|---|
| 344 | * | 
|---|
| 345 | * Side Effects: | 
|---|
| 346 | *    The information in searchPtr is modified to advance to the | 
|---|
| 347 | *    next entry. | 
|---|
| 348 | * | 
|---|
| 349 | *--------------------------------------------------------- | 
|---|
| 350 | */ | 
|---|
| 351 |  | 
|---|
| 352 | Hash_Entry * | 
|---|
| 353 | Hash_EnumNext(searchPtr) | 
|---|
| 354 | register Hash_Search *searchPtr; /* Area used to keep state about | 
|---|
| 355 | search. */ | 
|---|
| 356 | { | 
|---|
| 357 | register Hash_Entry *e; | 
|---|
| 358 | Hash_Table *t = searchPtr->tablePtr; | 
|---|
| 359 |  | 
|---|
| 360 | /* | 
|---|
| 361 | * The hashEntryPtr field points to the most recently returned | 
|---|
| 362 | * entry, or is nil if we are starting up.  If not nil, we have | 
|---|
| 363 | * to start at the next one in the chain. | 
|---|
| 364 | */ | 
|---|
| 365 | e = searchPtr->hashEntryPtr; | 
|---|
| 366 | if (e != NULL) | 
|---|
| 367 | e = e->next; | 
|---|
| 368 | /* | 
|---|
| 369 | * If the chain ran out, or if we are starting up, we need to | 
|---|
| 370 | * find the next nonempty chain. | 
|---|
| 371 | */ | 
|---|
| 372 | while (e == NULL) { | 
|---|
| 373 | if (searchPtr->nextIndex >= t->size) | 
|---|
| 374 | return (NULL); | 
|---|
| 375 | e = t->bucketPtr[searchPtr->nextIndex++]; | 
|---|
| 376 | } | 
|---|
| 377 | searchPtr->hashEntryPtr = e; | 
|---|
| 378 | return (e); | 
|---|
| 379 | } | 
|---|
| 380 |  | 
|---|
| 381 | /* | 
|---|
| 382 | *--------------------------------------------------------- | 
|---|
| 383 | * | 
|---|
| 384 | * RebuildTable -- | 
|---|
| 385 | *      This local routine makes a new hash table that | 
|---|
| 386 | *      is larger than the old one. | 
|---|
| 387 | * | 
|---|
| 388 | * Results: | 
|---|
| 389 | *      None. | 
|---|
| 390 | * | 
|---|
| 391 | * Side Effects: | 
|---|
| 392 | *      The entire hash table is moved, so any bucket numbers | 
|---|
| 393 | *      from the old table are invalid. | 
|---|
| 394 | * | 
|---|
| 395 | *--------------------------------------------------------- | 
|---|
| 396 | */ | 
|---|
| 397 |  | 
|---|
| 398 | static void | 
|---|
| 399 | RebuildTable(t) | 
|---|
| 400 | register Hash_Table *t; | 
|---|
| 401 | { | 
|---|
| 402 | register Hash_Entry *e, *next = NULL, **hp, **xp; | 
|---|
| 403 | register int i, mask; | 
|---|
| 404 | register Hash_Entry **oldhp; | 
|---|
| 405 | int oldsize; | 
|---|
| 406 |  | 
|---|
| 407 | oldhp = t->bucketPtr; | 
|---|
| 408 | oldsize = i = t->size; | 
|---|
| 409 | i <<= 1; | 
|---|
| 410 | t->size = i; | 
|---|
| 411 | t->mask = mask = i - 1; | 
|---|
| 412 | t->bucketPtr = hp = (struct Hash_Entry **) emalloc(sizeof(*hp) * i); | 
|---|
| 413 | while (--i >= 0) | 
|---|
| 414 | *hp++ = NULL; | 
|---|
| 415 | for (hp = oldhp, i = oldsize; --i >= 0;) { | 
|---|
| 416 | for (e = *hp++; e != NULL; e = next) { | 
|---|
| 417 | next = e->next; | 
|---|
| 418 | xp = &t->bucketPtr[e->namehash & mask]; | 
|---|
| 419 | e->next = *xp; | 
|---|
| 420 | *xp = e; | 
|---|
| 421 | } | 
|---|
| 422 | } | 
|---|
| 423 | efree((char *)oldhp); | 
|---|
| 424 | } | 
|---|