1 /* $NetBSD: hash.c,v 1.26 2020/08/01 14:47:49 rillig Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 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. 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. 21 * 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. 33 */ 34 35 /* 36 * Copyright (c) 1988, 1989 by Adam de Boor 37 * Copyright (c) 1989 by Berkeley Softworks 38 * All rights reserved. 39 * 40 * This code is derived from software contributed to Berkeley by 41 * Adam de Boor. 42 * 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. 58 * 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. 70 */ 71 72 #ifndef MAKE_NATIVE 73 static char rcsid[] = "$NetBSD: hash.c,v 1.26 2020/08/01 14:47:49 rillig 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.26 2020/08/01 14:47:49 rillig Exp $"); 81 #endif 82 #endif /* not lint */ 83 #endif 84 85 /* hash.c -- 86 * 87 * This module contains routines to manipulate a hash table. 88 * See hash.h for a definition of the structure of the hash 89 * table. Hash tables grow automatically as the amount of 90 * information increases. 91 */ 92 #include "sprite.h" 93 #include "make.h" 94 #include "hash.h" 95 96 /* 97 * Forward references to local procedures that are used before they're 98 * defined: 99 */ 100 101 static void RebuildTable(Hash_Table *); 102 103 /* 104 * The following defines the ratio of # entries to # buckets 105 * at which we rebuild the table to make it larger. 106 */ 107 108 #define rebuildLimit 3 109 110 /* The hash function(s) */ 111 112 #ifndef HASH 113 /* The default: this one matches Gosling's emacs */ 114 #define HASH(h, key, p) do { \ 115 for (h = 0, p = key; *p;) \ 116 h = (h << 5) - h + *p++; \ 117 } while (0) 118 119 #endif 120 121 /* 122 *--------------------------------------------------------- 123 * 124 * Hash_InitTable -- 125 * 126 * This routine just sets up the hash table. 127 * 128 * Input: 129 * t Structure to to hold table. 130 * numBuckets How many buckets to create for starters. This 131 * number is rounded up to a power of two. If 132 * <= 0, a reasonable default is chosen. The 133 * table will grow in size later as needed. 134 * 135 * Results: 136 * None. 137 * 138 * Side Effects: 139 * Memory is allocated for the initial bucket area. 140 * 141 *--------------------------------------------------------- 142 */ 143 144 void 145 Hash_InitTable(Hash_Table *t, int numBuckets) 146 { 147 int i; 148 struct Hash_Entry **hp; 149 150 /* 151 * Round up the size to a power of two. 152 */ 153 if (numBuckets <= 0) 154 i = 16; 155 else { 156 for (i = 2; i < numBuckets; i <<= 1) 157 continue; 158 } 159 t->numEntries = 0; 160 t->maxchain = 0; 161 t->size = i; 162 t->mask = i - 1; 163 t->bucketPtr = hp = bmake_malloc(sizeof(*hp) * i); 164 while (--i >= 0) 165 *hp++ = NULL; 166 } 167 168 /* 169 *--------------------------------------------------------- 170 * 171 * Hash_DeleteTable -- 172 * 173 * This routine removes everything from a hash table 174 * and frees up the memory space it occupied (except for 175 * the space in the Hash_Table structure). 176 * 177 * Results: 178 * None. 179 * 180 * Side Effects: 181 * Lots of memory is freed up. 182 * 183 *--------------------------------------------------------- 184 */ 185 186 void 187 Hash_DeleteTable(Hash_Table *t) 188 { 189 struct Hash_Entry **hp, *h, *nexth = NULL; 190 int i; 191 192 for (hp = t->bucketPtr, i = t->size; --i >= 0;) { 193 for (h = *hp++; h != NULL; h = nexth) { 194 nexth = h->next; 195 free(h); 196 } 197 } 198 free(t->bucketPtr); 199 200 /* 201 * Set up the hash table to cause memory faults on any future access 202 * attempts until re-initialization. 203 */ 204 t->bucketPtr = NULL; 205 } 206 207 /* 208 *--------------------------------------------------------- 209 * 210 * Hash_FindEntry -- 211 * 212 * Searches a hash table for an entry corresponding to key. 213 * 214 * Input: 215 * t Hash table to search. 216 * key A hash key. 217 * 218 * Results: 219 * The return value is a pointer to the entry for key, 220 * if key was present in the table. If key was not 221 * present, NULL is returned. 222 * 223 * Side Effects: 224 * None. 225 * 226 *--------------------------------------------------------- 227 */ 228 229 Hash_Entry * 230 Hash_FindEntry(Hash_Table *t, const char *key) 231 { 232 Hash_Entry *e; 233 unsigned h; 234 const char *p; 235 int chainlen; 236 237 if (t == NULL || t->bucketPtr == NULL) { 238 return NULL; 239 } 240 HASH(h, key, p); 241 p = key; 242 chainlen = 0; 243 #ifdef DEBUG_HASH_LOOKUP 244 if (DEBUG(HASH)) 245 fprintf(debug_file, "%s: %p h=%x key=%s\n", __func__, 246 t, h, key); 247 #endif 248 for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next) { 249 chainlen++; 250 if (e->namehash == h && strcmp(e->name, p) == 0) 251 break; 252 } 253 if (chainlen > t->maxchain) 254 t->maxchain = chainlen; 255 return e; 256 } 257 258 /* 259 *--------------------------------------------------------- 260 * 261 * Hash_CreateEntry -- 262 * 263 * Searches a hash table for an entry corresponding to 264 * key. If no entry is found, then one is created. 265 * 266 * Input: 267 * t Hash table to search. 268 * key A hash key. 269 * newPtr Filled in with TRUE if new entry created, 270 * FALSE otherwise. 271 * 272 * Results: 273 * The return value is a pointer to the entry. If *newPtr 274 * isn't NULL, then *newPtr is filled in with TRUE if a 275 * new entry was created, and FALSE if an entry already existed 276 * with the given key. 277 * 278 * Side Effects: 279 * Memory may be allocated, and the hash buckets may be modified. 280 *--------------------------------------------------------- 281 */ 282 283 Hash_Entry * 284 Hash_CreateEntry(Hash_Table *t, const char *key, Boolean *newPtr) 285 { 286 Hash_Entry *e; 287 unsigned h; 288 const char *p; 289 int keylen; 290 int chainlen; 291 struct Hash_Entry **hp; 292 293 /* 294 * Hash the key. As a side effect, save the length (strlen) of the 295 * key in case we need to create the entry. 296 */ 297 HASH(h, key, p); 298 keylen = p - key; 299 p = key; 300 chainlen = 0; 301 #ifdef DEBUG_HASH_LOOKUP 302 if (DEBUG(HASH)) 303 fprintf(debug_file, "%s: %p h=%x key=%s\n", __func__, 304 t, h, key); 305 #endif 306 for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next) { 307 chainlen++; 308 if (e->namehash == h && strcmp(e->name, p) == 0) { 309 if (newPtr != NULL) 310 *newPtr = FALSE; 311 break; 312 } 313 } 314 if (chainlen > t->maxchain) 315 t->maxchain = chainlen; 316 if (e) 317 return e; 318 319 /* 320 * The desired entry isn't there. Before allocating a new entry, 321 * expand the table if necessary (and this changes the resulting 322 * bucket chain). 323 */ 324 if (t->numEntries >= rebuildLimit * t->size) 325 RebuildTable(t); 326 e = bmake_malloc(sizeof(*e) + keylen); 327 hp = &t->bucketPtr[h & t->mask]; 328 e->next = *hp; 329 *hp = e; 330 Hash_SetValue(e, NULL); 331 e->namehash = h; 332 (void)strcpy(e->name, p); 333 t->numEntries++; 334 335 if (newPtr != NULL) 336 *newPtr = TRUE; 337 return e; 338 } 339 340 /* 341 *--------------------------------------------------------- 342 * 343 * Hash_DeleteEntry -- 344 * 345 * Delete the given hash table entry and free memory associated with 346 * it. 347 * 348 * Results: 349 * None. 350 * 351 * Side Effects: 352 * Hash chain that entry lives in is modified and memory is freed. 353 * 354 *--------------------------------------------------------- 355 */ 356 357 void 358 Hash_DeleteEntry(Hash_Table *t, Hash_Entry *e) 359 { 360 Hash_Entry **hp, *p; 361 362 if (e == NULL) 363 return; 364 for (hp = &t->bucketPtr[e->namehash & t->mask]; 365 (p = *hp) != NULL; hp = &p->next) { 366 if (p == e) { 367 *hp = p->next; 368 free(p); 369 t->numEntries--; 370 return; 371 } 372 } 373 (void)write(2, "bad call to Hash_DeleteEntry\n", 29); 374 abort(); 375 } 376 377 /* 378 *--------------------------------------------------------- 379 * 380 * Hash_EnumFirst -- 381 * This procedure sets things up for a complete search 382 * of all entries recorded in the hash table. 383 * 384 * Input: 385 * t Table to be searched. 386 * searchPtr Area in which to keep state about search. 387 * 388 * Results: 389 * The return value is the address of the first entry in 390 * the hash table, or NULL if the table is empty. 391 * 392 * Side Effects: 393 * The information in searchPtr is initialized so that successive 394 * calls to Hash_Next will return successive HashEntry's 395 * from the table. 396 * 397 *--------------------------------------------------------- 398 */ 399 400 Hash_Entry * 401 Hash_EnumFirst(Hash_Table *t, Hash_Search *searchPtr) 402 { 403 searchPtr->tablePtr = t; 404 searchPtr->nextIndex = 0; 405 searchPtr->hashEntryPtr = NULL; 406 return Hash_EnumNext(searchPtr); 407 } 408 409 /* 410 *--------------------------------------------------------- 411 * 412 * Hash_EnumNext -- 413 * This procedure returns successive entries in the hash table. 414 * 415 * Input: 416 * searchPtr Area used to keep state about search. 417 * 418 * Results: 419 * The return value is a pointer to the next HashEntry 420 * in the table, or NULL when the end of the table is 421 * reached. 422 * 423 * Side Effects: 424 * The information in searchPtr is modified to advance to the 425 * next entry. 426 * 427 *--------------------------------------------------------- 428 */ 429 430 Hash_Entry * 431 Hash_EnumNext(Hash_Search *searchPtr) 432 { 433 Hash_Entry *e; 434 Hash_Table *t = searchPtr->tablePtr; 435 436 /* 437 * The hashEntryPtr field points to the most recently returned 438 * entry, or is nil if we are starting up. If not nil, we have 439 * to start at the next one in the chain. 440 */ 441 e = searchPtr->hashEntryPtr; 442 if (e != NULL) 443 e = e->next; 444 /* 445 * If the chain ran out, or if we are starting up, we need to 446 * find the next nonempty chain. 447 */ 448 while (e == NULL) { 449 if (searchPtr->nextIndex >= t->size) 450 return NULL; 451 e = t->bucketPtr[searchPtr->nextIndex++]; 452 } 453 searchPtr->hashEntryPtr = e; 454 return e; 455 } 456 457 /* 458 *--------------------------------------------------------- 459 * 460 * RebuildTable -- 461 * This local routine makes a new hash table that 462 * is larger than the old one. 463 * 464 * Results: 465 * None. 466 * 467 * Side Effects: 468 * The entire hash table is moved, so any bucket numbers 469 * from the old table are invalid. 470 * 471 *--------------------------------------------------------- 472 */ 473 474 static void 475 RebuildTable(Hash_Table *t) 476 { 477 Hash_Entry *e, *next = NULL, **hp, **xp; 478 int i, mask; 479 Hash_Entry **oldhp; 480 int oldsize; 481 482 oldhp = t->bucketPtr; 483 oldsize = i = t->size; 484 i <<= 1; 485 t->size = i; 486 t->mask = mask = i - 1; 487 t->bucketPtr = hp = bmake_malloc(sizeof(*hp) * i); 488 while (--i >= 0) 489 *hp++ = NULL; 490 for (hp = oldhp, i = oldsize; --i >= 0;) { 491 for (e = *hp++; e != NULL; e = next) { 492 next = e->next; 493 xp = &t->bucketPtr[e->namehash & mask]; 494 e->next = *xp; 495 *xp = e; 496 } 497 } 498 free(oldhp); 499 if (DEBUG(HASH)) 500 fprintf(debug_file, "%s: %p size=%d entries=%d maxchain=%d\n", 501 __func__, t, t->size, t->numEntries, t->maxchain); 502 t->maxchain = 0; 503 } 504 505 void Hash_ForEach(Hash_Table *t, void (*action)(void *, void *), void *data) 506 { 507 Hash_Search search; 508 Hash_Entry *e; 509 510 for (e = Hash_EnumFirst(t, &search); 511 e != NULL; 512 e = Hash_EnumNext(&search)) 513 action(Hash_GetValue(e), data); 514 } 515 516 void 517 Hash_DebugStats(Hash_Table *t, const char *name) 518 { 519 if (DEBUG(HASH)) 520 fprintf(debug_file, "Hash_Table %s: size=%d numEntries=%d maxchain=%d\n", 521 name, t->size, t->numEntries, t->maxchain); 522 } 523