Home | History | Annotate | Line # | Download | only in make
hash.c revision 1.70
      1  1.70    rillig /*	$NetBSD: hash.c,v 1.70 2022/01/27 10:45:36 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.53    rillig /* Hash tables with string keys. */
     73  1.34    rillig 
     74   1.4       cgd #include "make.h"
     75   1.1       cgd 
     76  1.32    rillig /*	"@(#)hash.c	8.1 (Berkeley) 6/6/93"	*/
     77  1.70    rillig MAKE_RCSID("$NetBSD: hash.c,v 1.70 2022/01/27 10:45:36 rillig Exp $");
     78  1.32    rillig 
     79   1.1       cgd /*
     80  1.43    rillig  * The ratio of # entries to # buckets at which we rebuild the table to
     81  1.43    rillig  * make it larger.
     82   1.1       cgd  */
     83  1.43    rillig #define rebuildLimit 3
     84   1.1       cgd 
     85  1.61    rillig /* This hash function matches Gosling's Emacs and java.lang.String. */
     86  1.43    rillig static unsigned int
     87  1.64    rillig Hash_String(const char *key, size_t *out_keylen)
     88  1.43    rillig {
     89  1.59    rillig 	unsigned int h;
     90  1.59    rillig 	const char *p;
     91  1.59    rillig 
     92  1.59    rillig 	h = 0;
     93  1.59    rillig 	for (p = key; *p != '\0'; p++)
     94  1.59    rillig 		h = 31 * h + (unsigned char)*p;
     95  1.59    rillig 
     96  1.43    rillig 	if (out_keylen != NULL)
     97  1.43    rillig 		*out_keylen = (size_t)(p - key);
     98  1.43    rillig 	return h;
     99  1.43    rillig }
    100  1.23       sjg 
    101  1.64    rillig /* This hash function matches Gosling's Emacs and java.lang.String. */
    102  1.48    rillig unsigned int
    103  1.64    rillig Hash_Substring(Substring key)
    104  1.48    rillig {
    105  1.64    rillig 	unsigned int h;
    106  1.64    rillig 	const char *p;
    107  1.64    rillig 
    108  1.64    rillig 	h = 0;
    109  1.64    rillig 	for (p = key.start; p != key.end; p++)
    110  1.64    rillig 		h = 31 * h + (unsigned char)*p;
    111  1.64    rillig 	return h;
    112  1.48    rillig }
    113  1.48    rillig 
    114  1.46    rillig static HashEntry *
    115  1.46    rillig HashTable_Find(HashTable *t, unsigned int h, const char *key)
    116  1.43    rillig {
    117  1.46    rillig 	HashEntry *e;
    118  1.44    rillig 	unsigned int chainlen = 0;
    119  1.41    rillig 
    120  1.43    rillig #ifdef DEBUG_HASH_LOOKUP
    121  1.69    rillig 	DEBUG3(HASH, "HashTable_Find: %p h=%08x key=%s\n", t, h, key);
    122  1.43    rillig #endif
    123  1.41    rillig 
    124  1.43    rillig 	for (e = t->buckets[h & t->bucketsMask]; e != NULL; e = e->next) {
    125  1.43    rillig 		chainlen++;
    126  1.47    rillig 		if (e->key_hash == h && strcmp(e->key, key) == 0)
    127  1.43    rillig 			break;
    128  1.43    rillig 	}
    129  1.41    rillig 
    130  1.43    rillig 	if (chainlen > t->maxchain)
    131  1.43    rillig 		t->maxchain = chainlen;
    132  1.41    rillig 
    133  1.43    rillig 	return e;
    134  1.43    rillig }
    135  1.41    rillig 
    136  1.64    rillig static HashEntry *
    137  1.64    rillig HashTable_FindEntryBySubstring(HashTable *t, Substring key, unsigned int h)
    138  1.64    rillig {
    139  1.64    rillig 	HashEntry *e;
    140  1.64    rillig 	unsigned int chainlen = 0;
    141  1.64    rillig 
    142  1.64    rillig #ifdef DEBUG_HASH_LOOKUP
    143  1.69    rillig 	DEBUG4(HASH, "HashTable_FindEntryBySubstring: %p h=%08x key=%.*s\n",
    144  1.69    rillig 	    t, h, (int)Substring_Length(key), key.start);
    145  1.64    rillig #endif
    146  1.64    rillig 
    147  1.64    rillig 	for (e = t->buckets[h & t->bucketsMask]; e != NULL; e = e->next) {
    148  1.64    rillig 		chainlen++;
    149  1.70    rillig 		if (e->key_hash == h &&
    150  1.70    rillig 		    strncmp(e->key, key.start, Substring_Length(key)) == 0 &&
    151  1.70    rillig 		    e->key[Substring_Length(key)] == '\0')
    152  1.64    rillig 			break;
    153  1.64    rillig 	}
    154  1.64    rillig 
    155  1.64    rillig 	if (chainlen > t->maxchain)
    156  1.64    rillig 		t->maxchain = chainlen;
    157  1.64    rillig 
    158  1.64    rillig 	return e;
    159  1.64    rillig }
    160  1.64    rillig 
    161  1.54    rillig /* Set up the hash table. */
    162   1.1       cgd void
    163  1.54    rillig HashTable_Init(HashTable *t)
    164   1.1       cgd {
    165  1.44    rillig 	unsigned int n = 16, i;
    166  1.56    rillig 	HashEntry **buckets = bmake_malloc(sizeof *buckets * n);
    167  1.50    rillig 	for (i = 0; i < n; i++)
    168  1.50    rillig 		buckets[i] = NULL;
    169   1.1       cgd 
    170  1.50    rillig 	t->buckets = buckets;
    171  1.50    rillig 	t->bucketsSize = n;
    172   1.1       cgd 	t->numEntries = 0;
    173  1.50    rillig 	t->bucketsMask = n - 1;
    174  1.25       sjg 	t->maxchain = 0;
    175   1.1       cgd }
    176   1.1       cgd 
    177  1.61    rillig /*
    178  1.61    rillig  * Remove everything from the hash table and free up the memory for the keys
    179  1.61    rillig  * of the hash table, but not for the values associated to these keys.
    180  1.61    rillig  */
    181   1.1       cgd void
    182  1.54    rillig HashTable_Done(HashTable *t)
    183   1.1       cgd {
    184  1.51    rillig 	HashEntry **buckets = t->buckets;
    185  1.51    rillig 	size_t i, n = t->bucketsSize;
    186   1.1       cgd 
    187  1.51    rillig 	for (i = 0; i < n; i++) {
    188  1.51    rillig 		HashEntry *he = buckets[i];
    189  1.51    rillig 		while (he != NULL) {
    190  1.51    rillig 			HashEntry *next = he->next;
    191  1.51    rillig 			free(he);
    192  1.51    rillig 			he = next;
    193   1.1       cgd 		}
    194   1.1       cgd 	}
    195  1.61    rillig 
    196  1.29    rillig 	free(t->buckets);
    197  1.55    rillig #ifdef CLEANUP
    198  1.29    rillig 	t->buckets = NULL;
    199  1.55    rillig #endif
    200   1.1       cgd }
    201   1.1       cgd 
    202  1.54    rillig /* Find the entry corresponding to the key, or return NULL. */
    203  1.46    rillig HashEntry *
    204  1.54    rillig HashTable_FindEntry(HashTable *t, const char *key)
    205   1.1       cgd {
    206  1.64    rillig 	unsigned int h = Hash_String(key, NULL);
    207  1.43    rillig 	return HashTable_Find(t, h, key);
    208   1.1       cgd }
    209   1.1       cgd 
    210  1.54    rillig /* Find the value corresponding to the key, or return NULL. */
    211  1.33    rillig void *
    212  1.54    rillig HashTable_FindValue(HashTable *t, const char *key)
    213  1.33    rillig {
    214  1.54    rillig 	HashEntry *he = HashTable_FindEntry(t, key);
    215  1.43    rillig 	return he != NULL ? he->value : NULL;
    216  1.43    rillig }
    217  1.43    rillig 
    218  1.60    rillig /*
    219  1.60    rillig  * Find the value corresponding to the key and the precomputed hash,
    220  1.60    rillig  * or return NULL.
    221  1.60    rillig  */
    222  1.48    rillig void *
    223  1.64    rillig HashTable_FindValueBySubstringHash(HashTable *t, Substring key, unsigned int h)
    224  1.48    rillig {
    225  1.64    rillig 	HashEntry *he = HashTable_FindEntryBySubstring(t, key, h);
    226  1.48    rillig 	return he != NULL ? he->value : NULL;
    227  1.48    rillig }
    228  1.48    rillig 
    229  1.60    rillig /*
    230  1.60    rillig  * Make the hash table larger. Any bucket numbers from the old table become
    231  1.60    rillig  * invalid; the hash codes stay valid though.
    232  1.60    rillig  */
    233  1.43    rillig static void
    234  1.54    rillig HashTable_Enlarge(HashTable *t)
    235  1.43    rillig {
    236  1.49    rillig 	unsigned int oldSize = t->bucketsSize;
    237  1.49    rillig 	HashEntry **oldBuckets = t->buckets;
    238  1.49    rillig 	unsigned int newSize = 2 * oldSize;
    239  1.49    rillig 	unsigned int newMask = newSize - 1;
    240  1.56    rillig 	HashEntry **newBuckets = bmake_malloc(sizeof *newBuckets * newSize);
    241  1.49    rillig 	size_t i;
    242  1.49    rillig 
    243  1.49    rillig 	for (i = 0; i < newSize; i++)
    244  1.49    rillig 		newBuckets[i] = NULL;
    245  1.49    rillig 
    246  1.49    rillig 	for (i = 0; i < oldSize; i++) {
    247  1.49    rillig 		HashEntry *he = oldBuckets[i];
    248  1.49    rillig 		while (he != NULL) {
    249  1.49    rillig 			HashEntry *next = he->next;
    250  1.49    rillig 			he->next = newBuckets[he->key_hash & newMask];
    251  1.49    rillig 			newBuckets[he->key_hash & newMask] = he;
    252  1.49    rillig 			he = next;
    253  1.43    rillig 		}
    254  1.43    rillig 	}
    255  1.49    rillig 
    256  1.49    rillig 	free(oldBuckets);
    257  1.49    rillig 
    258  1.49    rillig 	t->bucketsSize = newSize;
    259  1.49    rillig 	t->bucketsMask = newMask;
    260  1.49    rillig 	t->buckets = newBuckets;
    261  1.69    rillig 	DEBUG4(HASH, "HashTable_Enlarge: %p size=%d entries=%d maxchain=%d\n",
    262  1.69    rillig 	    (void *)t, t->bucketsSize, t->numEntries, t->maxchain);
    263  1.43    rillig 	t->maxchain = 0;
    264  1.33    rillig }
    265  1.33    rillig 
    266  1.60    rillig /*
    267  1.60    rillig  * Find or create an entry corresponding to the key.
    268  1.60    rillig  * Return in out_isNew whether a new entry has been created.
    269  1.60    rillig  */
    270  1.46    rillig HashEntry *
    271  1.62    rillig HashTable_CreateEntry(HashTable *t, const char *key, bool *out_isNew)
    272   1.1       cgd {
    273  1.43    rillig 	size_t keylen;
    274  1.64    rillig 	unsigned int h = Hash_String(key, &keylen);
    275  1.52    rillig 	HashEntry *he = HashTable_Find(t, h, key);
    276   1.1       cgd 
    277  1.52    rillig 	if (he != NULL) {
    278  1.52    rillig 		if (out_isNew != NULL)
    279  1.62    rillig 			*out_isNew = false;
    280  1.52    rillig 		return he;
    281  1.42    rillig 	}
    282   1.1       cgd 
    283  1.29    rillig 	if (t->numEntries >= rebuildLimit * t->bucketsSize)
    284  1.54    rillig 		HashTable_Enlarge(t);
    285  1.43    rillig 
    286  1.56    rillig 	he = bmake_malloc(sizeof *he + keylen);
    287  1.52    rillig 	he->value = NULL;
    288  1.52    rillig 	he->key_hash = h;
    289  1.52    rillig 	memcpy(he->key, key, keylen + 1);
    290  1.52    rillig 
    291  1.52    rillig 	he->next = t->buckets[h & t->bucketsMask];
    292  1.52    rillig 	t->buckets[h & t->bucketsMask] = he;
    293   1.1       cgd 	t->numEntries++;
    294   1.1       cgd 
    295  1.52    rillig 	if (out_isNew != NULL)
    296  1.62    rillig 		*out_isNew = true;
    297  1.52    rillig 	return he;
    298   1.1       cgd }
    299   1.1       cgd 
    300  1.67    rillig void
    301  1.57    rillig HashTable_Set(HashTable *t, const char *key, void *value)
    302  1.57    rillig {
    303  1.57    rillig 	HashEntry *he = HashTable_CreateEntry(t, key, NULL);
    304  1.57    rillig 	HashEntry_Set(he, value);
    305  1.57    rillig }
    306  1.57    rillig 
    307  1.54    rillig /* Delete the entry from the table and free the associated memory. */
    308   1.1       cgd void
    309  1.54    rillig HashTable_DeleteEntry(HashTable *t, HashEntry *he)
    310   1.1       cgd {
    311  1.52    rillig 	HashEntry **ref = &t->buckets[he->key_hash & t->bucketsMask];
    312  1.52    rillig 	HashEntry *p;
    313   1.1       cgd 
    314  1.52    rillig 	for (; (p = *ref) != NULL; ref = &p->next) {
    315  1.52    rillig 		if (p == he) {
    316  1.52    rillig 			*ref = p->next;
    317  1.16  christos 			free(p);
    318   1.1       cgd 			t->numEntries--;
    319   1.1       cgd 			return;
    320   1.1       cgd 		}
    321   1.1       cgd 	}
    322   1.1       cgd 	abort();
    323   1.1       cgd }
    324   1.1       cgd 
    325  1.60    rillig /*
    326  1.60    rillig  * Return the next entry in the hash table, or NULL if the end of the table
    327  1.60    rillig  * is reached.
    328  1.60    rillig  */
    329  1.46    rillig HashEntry *
    330  1.45    rillig HashIter_Next(HashIter *hi)
    331   1.1       cgd {
    332  1.46    rillig 	HashTable *t = hi->table;
    333  1.52    rillig 	HashEntry *he = hi->entry;
    334  1.52    rillig 	HashEntry **buckets = t->buckets;
    335  1.52    rillig 	unsigned int bucketsSize = t->bucketsSize;
    336   1.1       cgd 
    337  1.52    rillig 	if (he != NULL)
    338  1.52    rillig 		he = he->next;	/* skip the most recently returned entry */
    339  1.52    rillig 
    340  1.52    rillig 	while (he == NULL) {	/* find the next nonempty chain */
    341  1.52    rillig 		if (hi->nextBucket >= bucketsSize)
    342  1.18       dsl 			return NULL;
    343  1.52    rillig 		he = buckets[hi->nextBucket++];
    344   1.1       cgd 	}
    345  1.52    rillig 	hi->entry = he;
    346  1.52    rillig 	return he;
    347   1.1       cgd }
    348   1.1       cgd 
    349  1.28    rillig void
    350  1.54    rillig HashTable_DebugStats(HashTable *t, const char *name)
    351  1.25       sjg {
    352  1.46    rillig 	DEBUG4(HASH, "HashTable %s: size=%u numEntries=%u maxchain=%u\n",
    353  1.68    rillig 	    name, t->bucketsSize, t->numEntries, t->maxchain);
    354  1.25       sjg }
    355