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