Home | History | Annotate | Line # | Download | only in make
hash.c revision 1.26
      1  1.26    rillig /*	$NetBSD: hash.c,v 1.26 2020/08/01 14:47:49 rillig Exp $	*/
      2   1.5  christos 
      3   1.1       cgd /*
      4   1.1       cgd  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
      5  1.12       agc  * All rights reserved.
      6  1.12       agc  *
      7  1.12       agc  * This code is derived from software contributed to Berkeley by
      8  1.12       agc  * Adam de Boor.
      9  1.12       agc  *
     10  1.12       agc  * Redistribution and use in source and binary forms, with or without
     11  1.12       agc  * modification, are permitted provided that the following conditions
     12  1.12       agc  * are met:
     13  1.12       agc  * 1. Redistributions of source code must retain the above copyright
     14  1.12       agc  *    notice, this list of conditions and the following disclaimer.
     15  1.12       agc  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.12       agc  *    notice, this list of conditions and the following disclaimer in the
     17  1.12       agc  *    documentation and/or other materials provided with the distribution.
     18  1.12       agc  * 3. Neither the name of the University nor the names of its contributors
     19  1.12       agc  *    may be used to endorse or promote products derived from this software
     20  1.12       agc  *    without specific prior written permission.
     21  1.12       agc  *
     22  1.12       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.12       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.12       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.12       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.12       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.12       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.12       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.12       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.12       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.12       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.12       agc  * SUCH DAMAGE.
     33  1.12       agc  */
     34  1.12       agc 
     35  1.12       agc /*
     36   1.1       cgd  * Copyright (c) 1988, 1989 by Adam de Boor
     37   1.1       cgd  * Copyright (c) 1989 by Berkeley Softworks
     38   1.1       cgd  * All rights reserved.
     39   1.1       cgd  *
     40   1.1       cgd  * This code is derived from software contributed to Berkeley by
     41   1.1       cgd  * Adam de Boor.
     42   1.1       cgd  *
     43   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     44   1.1       cgd  * modification, are permitted provided that the following conditions
     45   1.1       cgd  * are met:
     46   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     47   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     48   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     49   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     50   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     51   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     52   1.1       cgd  *    must display the following acknowledgement:
     53   1.1       cgd  *	This product includes software developed by the University of
     54   1.1       cgd  *	California, Berkeley and its contributors.
     55   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     56   1.1       cgd  *    may be used to endorse or promote products derived from this software
     57   1.1       cgd  *    without specific prior written permission.
     58   1.1       cgd  *
     59   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69   1.1       cgd  * SUCH DAMAGE.
     70   1.1       cgd  */
     71   1.1       cgd 
     72  1.13      ross #ifndef MAKE_NATIVE
     73  1.26    rillig static char rcsid[] = "$NetBSD: hash.c,v 1.26 2020/08/01 14:47:49 rillig Exp $";
     74   1.8     lukem #else
     75   1.7  christos #include <sys/cdefs.h>
     76   1.1       cgd #ifndef lint
     77   1.5  christos #if 0
     78   1.6  christos static char sccsid[] = "@(#)hash.c	8.1 (Berkeley) 6/6/93";
     79   1.5  christos #else
     80  1.26    rillig __RCSID("$NetBSD: hash.c,v 1.26 2020/08/01 14:47:49 rillig Exp $");
     81   1.5  christos #endif
     82   1.1       cgd #endif /* not lint */
     83   1.8     lukem #endif
     84   1.1       cgd 
     85   1.1       cgd /* hash.c --
     86   1.1       cgd  *
     87   1.1       cgd  * 	This module contains routines to manipulate a hash table.
     88   1.1       cgd  * 	See hash.h for a definition of the structure of the hash
     89   1.1       cgd  * 	table.  Hash tables grow automatically as the amount of
     90   1.1       cgd  * 	information increases.
     91   1.1       cgd  */
     92   1.1       cgd #include "sprite.h"
     93   1.4       cgd #include "make.h"
     94   1.1       cgd #include "hash.h"
     95   1.1       cgd 
     96   1.1       cgd /*
     97   1.1       cgd  * Forward references to local procedures that are used before they're
     98   1.1       cgd  * defined:
     99   1.1       cgd  */
    100   1.1       cgd 
    101  1.10       wiz static void RebuildTable(Hash_Table *);
    102   1.1       cgd 
    103   1.6  christos /*
    104   1.1       cgd  * The following defines the ratio of # entries to # buckets
    105   1.1       cgd  * at which we rebuild the table to make it larger.
    106   1.1       cgd  */
    107   1.1       cgd 
    108   1.9   mycroft #define rebuildLimit 3
    109   1.1       cgd 
    110  1.23       sjg /* The hash function(s) */
    111  1.25       sjg 
    112  1.25       sjg #ifndef HASH
    113  1.25       sjg /* The default: this one matches Gosling's emacs */
    114  1.23       sjg #define HASH(h, key, p) do { \
    115  1.23       sjg 	for (h = 0, p = key; *p;) \
    116  1.23       sjg 		h = (h << 5) - h + *p++; \
    117  1.23       sjg 	} while (0)
    118  1.23       sjg 
    119  1.25       sjg #endif
    120  1.23       sjg 
    121   1.1       cgd /*
    122   1.1       cgd  *---------------------------------------------------------
    123   1.6  christos  *
    124   1.1       cgd  * Hash_InitTable --
    125   1.1       cgd  *
    126   1.1       cgd  *	This routine just sets up the hash table.
    127   1.1       cgd  *
    128  1.10       wiz  * Input:
    129  1.10       wiz  *	t		Structure to to hold table.
    130  1.10       wiz  *	numBuckets	How many buckets to create for starters. This
    131  1.10       wiz  *			number is rounded up to a power of two.   If
    132  1.10       wiz  *			<= 0, a reasonable default is chosen. The
    133  1.10       wiz  *			table will grow in size later as needed.
    134  1.10       wiz  *
    135   1.6  christos  * Results:
    136   1.1       cgd  *	None.
    137   1.1       cgd  *
    138   1.1       cgd  * Side Effects:
    139   1.1       cgd  *	Memory is allocated for the initial bucket area.
    140   1.1       cgd  *
    141   1.1       cgd  *---------------------------------------------------------
    142   1.1       cgd  */
    143   1.1       cgd 
    144   1.1       cgd void
    145  1.10       wiz Hash_InitTable(Hash_Table *t, int numBuckets)
    146   1.1       cgd {
    147  1.10       wiz 	int i;
    148  1.10       wiz 	struct Hash_Entry **hp;
    149   1.1       cgd 
    150   1.1       cgd 	/*
    151   1.6  christos 	 * Round up the size to a power of two.
    152   1.1       cgd 	 */
    153   1.1       cgd 	if (numBuckets <= 0)
    154   1.1       cgd 		i = 16;
    155   1.1       cgd 	else {
    156   1.1       cgd 		for (i = 2; i < numBuckets; i <<= 1)
    157   1.4       cgd 			 continue;
    158   1.1       cgd 	}
    159   1.1       cgd 	t->numEntries = 0;
    160  1.25       sjg 	t->maxchain = 0;
    161   1.1       cgd 	t->size = i;
    162   1.1       cgd 	t->mask = i - 1;
    163  1.17     joerg 	t->bucketPtr = hp = bmake_malloc(sizeof(*hp) * i);
    164   1.1       cgd 	while (--i >= 0)
    165   1.1       cgd 		*hp++ = NULL;
    166   1.1       cgd }
    167   1.1       cgd 
    168   1.1       cgd /*
    169   1.1       cgd  *---------------------------------------------------------
    170   1.1       cgd  *
    171   1.1       cgd  * Hash_DeleteTable --
    172   1.1       cgd  *
    173   1.1       cgd  *	This routine removes everything from a hash table
    174   1.1       cgd  *	and frees up the memory space it occupied (except for
    175   1.1       cgd  *	the space in the Hash_Table structure).
    176   1.1       cgd  *
    177   1.6  christos  * Results:
    178   1.1       cgd  *	None.
    179   1.1       cgd  *
    180   1.1       cgd  * Side Effects:
    181   1.1       cgd  *	Lots of memory is freed up.
    182   1.1       cgd  *
    183   1.1       cgd  *---------------------------------------------------------
    184   1.1       cgd  */
    185   1.1       cgd 
    186   1.1       cgd void
    187  1.10       wiz Hash_DeleteTable(Hash_Table *t)
    188   1.1       cgd {
    189  1.10       wiz 	struct Hash_Entry **hp, *h, *nexth = NULL;
    190  1.10       wiz 	int i;
    191   1.1       cgd 
    192   1.1       cgd 	for (hp = t->bucketPtr, i = t->size; --i >= 0;) {
    193   1.1       cgd 		for (h = *hp++; h != NULL; h = nexth) {
    194   1.1       cgd 			nexth = h->next;
    195  1.16  christos 			free(h);
    196   1.1       cgd 		}
    197   1.1       cgd 	}
    198  1.16  christos 	free(t->bucketPtr);
    199   1.1       cgd 
    200   1.1       cgd 	/*
    201   1.1       cgd 	 * Set up the hash table to cause memory faults on any future access
    202   1.6  christos 	 * attempts until re-initialization.
    203   1.1       cgd 	 */
    204   1.1       cgd 	t->bucketPtr = NULL;
    205   1.1       cgd }
    206   1.1       cgd 
    207   1.1       cgd /*
    208   1.1       cgd  *---------------------------------------------------------
    209   1.1       cgd  *
    210   1.1       cgd  * Hash_FindEntry --
    211   1.1       cgd  *
    212   1.1       cgd  * 	Searches a hash table for an entry corresponding to key.
    213   1.1       cgd  *
    214  1.10       wiz  * Input:
    215  1.10       wiz  *	t		Hash table to search.
    216  1.10       wiz  *	key		A hash key.
    217  1.10       wiz  *
    218   1.1       cgd  * Results:
    219   1.1       cgd  *	The return value is a pointer to the entry for key,
    220   1.1       cgd  *	if key was present in the table.  If key was not
    221   1.1       cgd  *	present, NULL is returned.
    222   1.1       cgd  *
    223   1.1       cgd  * Side Effects:
    224   1.1       cgd  *	None.
    225   1.1       cgd  *
    226   1.1       cgd  *---------------------------------------------------------
    227   1.1       cgd  */
    228   1.1       cgd 
    229   1.1       cgd Hash_Entry *
    230  1.11  christos Hash_FindEntry(Hash_Table *t, const char *key)
    231   1.1       cgd {
    232  1.10       wiz 	Hash_Entry *e;
    233  1.10       wiz 	unsigned h;
    234  1.11  christos 	const char *p;
    235  1.23       sjg 	int chainlen;
    236   1.1       cgd 
    237  1.20       sjg 	if (t == NULL || t->bucketPtr == NULL) {
    238  1.20       sjg 	    return NULL;
    239  1.20       sjg 	}
    240  1.23       sjg 	HASH(h, key, p);
    241   1.1       cgd 	p = key;
    242  1.25       sjg 	chainlen = 0;
    243  1.25       sjg #ifdef DEBUG_HASH_LOOKUP
    244  1.23       sjg 	if (DEBUG(HASH))
    245  1.23       sjg 		fprintf(debug_file, "%s: %p h=%x key=%s\n", __func__,
    246  1.23       sjg 		    t, h, key);
    247  1.25       sjg #endif
    248  1.23       sjg 	for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next) {
    249  1.23       sjg 		chainlen++;
    250  1.24  riastrad 		if (e->namehash == h && strcmp(e->name, p) == 0)
    251  1.23       sjg 			break;
    252  1.23       sjg 	}
    253  1.25       sjg 	if (chainlen > t->maxchain)
    254  1.25       sjg 		t->maxchain = chainlen;
    255  1.23       sjg 	return e;
    256   1.1       cgd }
    257   1.1       cgd 
    258   1.1       cgd /*
    259   1.1       cgd  *---------------------------------------------------------
    260   1.1       cgd  *
    261   1.1       cgd  * Hash_CreateEntry --
    262   1.1       cgd  *
    263   1.1       cgd  *	Searches a hash table for an entry corresponding to
    264   1.1       cgd  *	key.  If no entry is found, then one is created.
    265   1.1       cgd  *
    266  1.10       wiz  * Input:
    267  1.10       wiz  *	t		Hash table to search.
    268  1.10       wiz  *	key		A hash key.
    269  1.10       wiz  *	newPtr		Filled in with TRUE if new entry created,
    270  1.10       wiz  *			FALSE otherwise.
    271  1.10       wiz  *
    272   1.1       cgd  * Results:
    273   1.1       cgd  *	The return value is a pointer to the entry.  If *newPtr
    274   1.1       cgd  *	isn't NULL, then *newPtr is filled in with TRUE if a
    275   1.1       cgd  *	new entry was created, and FALSE if an entry already existed
    276   1.1       cgd  *	with the given key.
    277   1.1       cgd  *
    278   1.1       cgd  * Side Effects:
    279   1.1       cgd  *	Memory may be allocated, and the hash buckets may be modified.
    280   1.1       cgd  *---------------------------------------------------------
    281   1.1       cgd  */
    282   1.1       cgd 
    283   1.1       cgd Hash_Entry *
    284  1.11  christos Hash_CreateEntry(Hash_Table *t, const char *key, Boolean *newPtr)
    285   1.1       cgd {
    286  1.10       wiz 	Hash_Entry *e;
    287  1.10       wiz 	unsigned h;
    288  1.11  christos 	const char *p;
    289   1.1       cgd 	int keylen;
    290  1.23       sjg 	int chainlen;
    291   1.1       cgd 	struct Hash_Entry **hp;
    292   1.1       cgd 
    293   1.1       cgd 	/*
    294   1.1       cgd 	 * Hash the key.  As a side effect, save the length (strlen) of the
    295   1.1       cgd 	 * key in case we need to create the entry.
    296   1.1       cgd 	 */
    297  1.23       sjg 	HASH(h, key, p);
    298   1.1       cgd 	keylen = p - key;
    299   1.1       cgd 	p = key;
    300  1.25       sjg 	chainlen = 0;
    301  1.25       sjg #ifdef DEBUG_HASH_LOOKUP
    302  1.23       sjg 	if (DEBUG(HASH))
    303  1.23       sjg 		fprintf(debug_file, "%s: %p h=%x key=%s\n", __func__,
    304  1.23       sjg 		    t, h, key);
    305  1.25       sjg #endif
    306   1.1       cgd 	for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next) {
    307  1.23       sjg 		chainlen++;
    308   1.1       cgd 		if (e->namehash == h && strcmp(e->name, p) == 0) {
    309   1.1       cgd 			if (newPtr != NULL)
    310   1.1       cgd 				*newPtr = FALSE;
    311  1.23       sjg 			break;
    312   1.1       cgd 		}
    313   1.1       cgd 	}
    314  1.25       sjg 	if (chainlen > t->maxchain)
    315  1.25       sjg 		t->maxchain = chainlen;
    316  1.23       sjg 	if (e)
    317  1.23       sjg 		return e;
    318   1.1       cgd 
    319   1.1       cgd 	/*
    320   1.1       cgd 	 * The desired entry isn't there.  Before allocating a new entry,
    321   1.1       cgd 	 * expand the table if necessary (and this changes the resulting
    322   1.6  christos 	 * bucket chain).
    323   1.1       cgd 	 */
    324   1.1       cgd 	if (t->numEntries >= rebuildLimit * t->size)
    325   1.1       cgd 		RebuildTable(t);
    326  1.17     joerg 	e = bmake_malloc(sizeof(*e) + keylen);
    327   1.1       cgd 	hp = &t->bucketPtr[h & t->mask];
    328   1.1       cgd 	e->next = *hp;
    329   1.1       cgd 	*hp = e;
    330  1.19       dsl 	Hash_SetValue(e, NULL);
    331   1.1       cgd 	e->namehash = h;
    332  1.15  christos 	(void)strcpy(e->name, p);
    333   1.1       cgd 	t->numEntries++;
    334   1.1       cgd 
    335   1.1       cgd 	if (newPtr != NULL)
    336   1.1       cgd 		*newPtr = TRUE;
    337  1.21    rillig 	return e;
    338   1.1       cgd }
    339   1.1       cgd 
    340   1.1       cgd /*
    341   1.1       cgd  *---------------------------------------------------------
    342   1.1       cgd  *
    343   1.1       cgd  * Hash_DeleteEntry --
    344   1.1       cgd  *
    345   1.1       cgd  * 	Delete the given hash table entry and free memory associated with
    346   1.1       cgd  *	it.
    347   1.1       cgd  *
    348   1.1       cgd  * Results:
    349   1.1       cgd  *	None.
    350   1.1       cgd  *
    351   1.1       cgd  * Side Effects:
    352   1.1       cgd  *	Hash chain that entry lives in is modified and memory is freed.
    353   1.1       cgd  *
    354   1.1       cgd  *---------------------------------------------------------
    355   1.1       cgd  */
    356   1.1       cgd 
    357   1.1       cgd void
    358  1.10       wiz Hash_DeleteEntry(Hash_Table *t, Hash_Entry *e)
    359   1.1       cgd {
    360  1.10       wiz 	Hash_Entry **hp, *p;
    361   1.1       cgd 
    362   1.1       cgd 	if (e == NULL)
    363   1.1       cgd 		return;
    364   1.1       cgd 	for (hp = &t->bucketPtr[e->namehash & t->mask];
    365   1.1       cgd 	     (p = *hp) != NULL; hp = &p->next) {
    366   1.1       cgd 		if (p == e) {
    367   1.1       cgd 			*hp = p->next;
    368  1.16  christos 			free(p);
    369   1.1       cgd 			t->numEntries--;
    370   1.1       cgd 			return;
    371   1.1       cgd 		}
    372   1.1       cgd 	}
    373  1.15  christos 	(void)write(2, "bad call to Hash_DeleteEntry\n", 29);
    374   1.1       cgd 	abort();
    375   1.1       cgd }
    376   1.1       cgd 
    377   1.1       cgd /*
    378   1.1       cgd  *---------------------------------------------------------
    379   1.1       cgd  *
    380   1.1       cgd  * Hash_EnumFirst --
    381   1.1       cgd  *	This procedure sets things up for a complete search
    382   1.1       cgd  *	of all entries recorded in the hash table.
    383   1.1       cgd  *
    384  1.10       wiz  * Input:
    385  1.10       wiz  *	t		Table to be searched.
    386  1.10       wiz  *	searchPtr	Area in which to keep state about search.
    387  1.10       wiz  *
    388   1.6  christos  * Results:
    389   1.1       cgd  *	The return value is the address of the first entry in
    390   1.1       cgd  *	the hash table, or NULL if the table is empty.
    391   1.1       cgd  *
    392   1.1       cgd  * Side Effects:
    393   1.1       cgd  *	The information in searchPtr is initialized so that successive
    394   1.1       cgd  *	calls to Hash_Next will return successive HashEntry's
    395   1.1       cgd  *	from the table.
    396   1.1       cgd  *
    397   1.1       cgd  *---------------------------------------------------------
    398   1.1       cgd  */
    399   1.1       cgd 
    400   1.1       cgd Hash_Entry *
    401  1.10       wiz Hash_EnumFirst(Hash_Table *t, Hash_Search *searchPtr)
    402   1.1       cgd {
    403   1.1       cgd 	searchPtr->tablePtr = t;
    404   1.1       cgd 	searchPtr->nextIndex = 0;
    405   1.1       cgd 	searchPtr->hashEntryPtr = NULL;
    406   1.1       cgd 	return Hash_EnumNext(searchPtr);
    407   1.1       cgd }
    408   1.1       cgd 
    409   1.1       cgd /*
    410   1.1       cgd  *---------------------------------------------------------
    411   1.1       cgd  *
    412   1.1       cgd  * Hash_EnumNext --
    413   1.1       cgd  *    This procedure returns successive entries in the hash table.
    414   1.1       cgd  *
    415  1.10       wiz  * Input:
    416  1.10       wiz  *	searchPtr	Area used to keep state about search.
    417  1.10       wiz  *
    418   1.1       cgd  * Results:
    419   1.1       cgd  *    The return value is a pointer to the next HashEntry
    420   1.1       cgd  *    in the table, or NULL when the end of the table is
    421   1.1       cgd  *    reached.
    422   1.1       cgd  *
    423   1.1       cgd  * Side Effects:
    424   1.1       cgd  *    The information in searchPtr is modified to advance to the
    425   1.1       cgd  *    next entry.
    426   1.1       cgd  *
    427   1.1       cgd  *---------------------------------------------------------
    428   1.1       cgd  */
    429   1.1       cgd 
    430   1.1       cgd Hash_Entry *
    431  1.10       wiz Hash_EnumNext(Hash_Search *searchPtr)
    432   1.1       cgd {
    433  1.10       wiz 	Hash_Entry *e;
    434   1.1       cgd 	Hash_Table *t = searchPtr->tablePtr;
    435   1.1       cgd 
    436   1.1       cgd 	/*
    437   1.1       cgd 	 * The hashEntryPtr field points to the most recently returned
    438   1.1       cgd 	 * entry, or is nil if we are starting up.  If not nil, we have
    439   1.1       cgd 	 * to start at the next one in the chain.
    440   1.1       cgd 	 */
    441   1.1       cgd 	e = searchPtr->hashEntryPtr;
    442   1.1       cgd 	if (e != NULL)
    443   1.1       cgd 		e = e->next;
    444   1.1       cgd 	/*
    445   1.1       cgd 	 * If the chain ran out, or if we are starting up, we need to
    446   1.1       cgd 	 * find the next nonempty chain.
    447   1.1       cgd 	 */
    448   1.1       cgd 	while (e == NULL) {
    449   1.1       cgd 		if (searchPtr->nextIndex >= t->size)
    450  1.18       dsl 			return NULL;
    451   1.1       cgd 		e = t->bucketPtr[searchPtr->nextIndex++];
    452   1.1       cgd 	}
    453   1.1       cgd 	searchPtr->hashEntryPtr = e;
    454  1.21    rillig 	return e;
    455   1.1       cgd }
    456   1.1       cgd 
    457   1.1       cgd /*
    458   1.1       cgd  *---------------------------------------------------------
    459   1.1       cgd  *
    460   1.1       cgd  * RebuildTable --
    461   1.1       cgd  *	This local routine makes a new hash table that
    462   1.1       cgd  *	is larger than the old one.
    463   1.1       cgd  *
    464   1.6  christos  * Results:
    465   1.1       cgd  * 	None.
    466   1.1       cgd  *
    467   1.1       cgd  * Side Effects:
    468   1.1       cgd  *	The entire hash table is moved, so any bucket numbers
    469   1.1       cgd  *	from the old table are invalid.
    470   1.1       cgd  *
    471   1.1       cgd  *---------------------------------------------------------
    472   1.1       cgd  */
    473   1.1       cgd 
    474   1.1       cgd static void
    475  1.10       wiz RebuildTable(Hash_Table *t)
    476   1.1       cgd {
    477  1.10       wiz 	Hash_Entry *e, *next = NULL, **hp, **xp;
    478  1.10       wiz 	int i, mask;
    479  1.26    rillig 	Hash_Entry **oldhp;
    480   1.1       cgd 	int oldsize;
    481   1.1       cgd 
    482   1.1       cgd 	oldhp = t->bucketPtr;
    483   1.1       cgd 	oldsize = i = t->size;
    484   1.1       cgd 	i <<= 1;
    485   1.1       cgd 	t->size = i;
    486   1.1       cgd 	t->mask = mask = i - 1;
    487  1.17     joerg 	t->bucketPtr = hp = bmake_malloc(sizeof(*hp) * i);
    488   1.1       cgd 	while (--i >= 0)
    489   1.1       cgd 		*hp++ = NULL;
    490   1.1       cgd 	for (hp = oldhp, i = oldsize; --i >= 0;) {
    491   1.1       cgd 		for (e = *hp++; e != NULL; e = next) {
    492   1.1       cgd 			next = e->next;
    493   1.1       cgd 			xp = &t->bucketPtr[e->namehash & mask];
    494   1.1       cgd 			e->next = *xp;
    495   1.1       cgd 			*xp = e;
    496   1.1       cgd 		}
    497   1.1       cgd 	}
    498  1.16  christos 	free(oldhp);
    499  1.23       sjg 	if (DEBUG(HASH))
    500  1.25       sjg 		fprintf(debug_file, "%s: %p size=%d entries=%d maxchain=%d\n",
    501  1.25       sjg 		    __func__, t, t->size, t->numEntries, t->maxchain);
    502  1.25       sjg 	t->maxchain = 0;
    503   1.1       cgd }
    504  1.22    rillig 
    505  1.22    rillig void Hash_ForEach(Hash_Table *t, void (*action)(void *, void *), void *data)
    506  1.22    rillig {
    507  1.22    rillig 	Hash_Search search;
    508  1.22    rillig 	Hash_Entry *e;
    509  1.22    rillig 
    510  1.22    rillig 	for (e = Hash_EnumFirst(t, &search);
    511  1.22    rillig 	     e != NULL;
    512  1.22    rillig 	     e = Hash_EnumNext(&search))
    513  1.22    rillig 		action(Hash_GetValue(e), data);
    514  1.22    rillig }
    515  1.25       sjg 
    516  1.25       sjg void
    517  1.25       sjg Hash_DebugStats(Hash_Table *t, const char *name)
    518  1.25       sjg {
    519  1.25       sjg     if (DEBUG(HASH))
    520  1.25       sjg 	fprintf(debug_file, "Hash_Table %s: size=%d numEntries=%d maxchain=%d\n",
    521  1.25       sjg 		name, t->size, t->numEntries, t->maxchain);
    522  1.25       sjg }
    523