Home | History | Annotate | Line # | Download | only in lint2
      1  1.30    rillig /*	$NetBSD: hash.c,v 1.30 2025/05/24 07:38:59 rillig Exp $	*/
      2   1.2       cgd 
      3   1.1       cgd /*
      4   1.1       cgd  * Copyright (c) 1994, 1995 Jochen Pohl
      5   1.1       cgd  * All Rights Reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17  1.28    rillig  *	This product includes software developed by Jochen Pohl for
     18   1.1       cgd  *	The NetBSD Project.
     19   1.1       cgd  * 4. The name of the author may not be used to endorse or promote products
     20   1.1       cgd  *    derived from this software without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.1       cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1       cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1       cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.1       cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27   1.1       cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28   1.1       cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29   1.1       cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30   1.1       cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31   1.1       cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1       cgd  */
     33   1.1       cgd 
     34   1.9       jmc #if HAVE_NBTOOL_CONFIG_H
     35   1.9       jmc #include "nbtool_config.h"
     36   1.9       jmc #endif
     37   1.9       jmc 
     38   1.4  christos #include <sys/cdefs.h>
     39  1.24    rillig #if defined(__RCSID)
     40  1.30    rillig __RCSID("$NetBSD: hash.c,v 1.30 2025/05/24 07:38:59 rillig Exp $");
     41   1.1       cgd #endif
     42   1.1       cgd 
     43   1.7        tv #include <limits.h>
     44   1.1       cgd #include <stddef.h>
     45   1.4  christos #include <stdlib.h>
     46   1.1       cgd #include <string.h>
     47   1.1       cgd 
     48   1.1       cgd #include "lint2.h"
     49   1.1       cgd 
     50  1.26    rillig #define HTAB_BUCKETS		1009
     51  1.26    rillig 
     52  1.29    rillig static hte_t **htab;
     53   1.1       cgd 
     54  1.23    rillig hte_t **
     55  1.23    rillig htab_new(void)
     56   1.1       cgd {
     57  1.26    rillig 	return xcalloc(HTAB_BUCKETS, sizeof(*htab_new()));
     58   1.1       cgd }
     59   1.1       cgd 
     60  1.17    rillig static unsigned int
     61   1.6     lukem hash(const char *s)
     62   1.1       cgd {
     63  1.17    rillig 	unsigned int v;
     64  1.22    rillig 	const char *p;
     65   1.1       cgd 
     66   1.1       cgd 	v = 0;
     67  1.22    rillig 	for (p = s; *p != '\0'; p++) {
     68  1.22    rillig 		v = (v << 4) + (unsigned char)*p;
     69  1.21    rillig 		v ^= v >> 28;
     70   1.1       cgd 	}
     71  1.26    rillig 	return v % HTAB_BUCKETS;
     72   1.1       cgd }
     73   1.1       cgd 
     74   1.1       cgd /*
     75   1.1       cgd  * Look for a hash table entry. If no hash table entry for the
     76   1.1       cgd  * given name exists and mknew is set, create a new one.
     77   1.1       cgd  */
     78   1.1       cgd hte_t *
     79  1.27    rillig hash_search(hte_t **table, const char *s, bool mknew)
     80   1.1       cgd {
     81  1.17    rillig 	unsigned int h;
     82  1.25    rillig 	hte_t *hte;
     83   1.1       cgd 
     84   1.3       cgd 	if (table == NULL)
     85   1.3       cgd 		table = htab;
     86   1.3       cgd 
     87   1.1       cgd 	h = hash(s);
     88   1.3       cgd 	for (hte = table[h]; hte != NULL; hte = hte->h_link) {
     89   1.1       cgd 		if (strcmp(hte->h_name, s) == 0)
     90   1.1       cgd 			break;
     91   1.1       cgd 	}
     92   1.1       cgd 
     93   1.1       cgd 	if (hte != NULL || !mknew)
     94  1.12    rillig 		return hte;
     95   1.1       cgd 
     96   1.1       cgd 	/* create a new hte */
     97  1.15    rillig 	hte = xmalloc(sizeof(*hte));
     98   1.1       cgd 	hte->h_name = xstrdup(s);
     99  1.13    rillig 	hte->h_used = false;
    100  1.13    rillig 	hte->h_def = false;
    101  1.13    rillig 	hte->h_static = false;
    102   1.5   mycroft 	hte->h_syms = NULL;
    103   1.1       cgd 	hte->h_lsym = &hte->h_syms;
    104   1.5   mycroft 	hte->h_calls = NULL;
    105   1.1       cgd 	hte->h_lcall = &hte->h_calls;
    106   1.5   mycroft 	hte->h_usyms = NULL;
    107   1.1       cgd 	hte->h_lusym = &hte->h_usyms;
    108   1.3       cgd 	hte->h_link = table[h];
    109   1.3       cgd 	hte->h_hte = NULL;
    110   1.3       cgd 	table[h] = hte;
    111   1.1       cgd 
    112  1.12    rillig 	return hte;
    113   1.1       cgd }
    114   1.1       cgd 
    115  1.19    rillig struct hte_list {
    116  1.19    rillig 	hte_t **items;
    117  1.19    rillig 	size_t len;
    118  1.19    rillig 	size_t cap;
    119  1.19    rillig };
    120  1.19    rillig 
    121  1.19    rillig static void
    122  1.19    rillig hte_list_add(struct hte_list *list, hte_t *item)
    123  1.19    rillig {
    124  1.19    rillig 	if (list->len >= list->cap) {
    125  1.19    rillig 		list->cap = list->cap == 0 ? 1024 : 2 * list->cap;
    126  1.19    rillig 		list->items = xrealloc(list->items,
    127  1.19    rillig 		    sizeof(list->items[0]) * list->cap);
    128  1.19    rillig 	}
    129  1.19    rillig 	list->items[list->len++] = item;
    130  1.19    rillig }
    131  1.19    rillig 
    132  1.19    rillig static int
    133  1.19    rillig hte_by_name(const void *va, const void *vb)
    134  1.19    rillig {
    135  1.19    rillig 	const hte_t *a = *((const hte_t *const *)va);
    136  1.19    rillig 	const hte_t *b = *((const hte_t *const *)vb);
    137  1.19    rillig 
    138  1.19    rillig 	return strcmp(a->h_name, b->h_name);
    139  1.19    rillig }
    140  1.19    rillig 
    141  1.23    rillig void
    142  1.23    rillig symtab_init(void)
    143  1.23    rillig {
    144  1.23    rillig 	htab = htab_new();
    145  1.23    rillig }
    146  1.23    rillig 
    147   1.1       cgd /*
    148  1.18    rillig  * Call the action for each name in the hash table.
    149   1.1       cgd  */
    150   1.1       cgd void
    151  1.18    rillig symtab_forall(void (*action)(hte_t *))
    152   1.1       cgd {
    153  1.25    rillig 	int i;
    154  1.25    rillig 	hte_t *hte;
    155  1.25    rillig 	hte_t **table = htab;
    156   1.3       cgd 
    157  1.26    rillig 	for (i = 0; i < HTAB_BUCKETS; i++) {
    158   1.3       cgd 		for (hte = table[i]; hte != NULL; hte = hte->h_link)
    159  1.18    rillig 			action(hte);
    160   1.1       cgd 	}
    161   1.3       cgd }
    162   1.3       cgd 
    163  1.19    rillig /* Run the action for each name in the symbol table, in alphabetic order. */
    164  1.19    rillig void
    165  1.30    rillig symtab_forall_sorted(void (*action)(const hte_t *))
    166  1.19    rillig {
    167  1.19    rillig 	hte_t *hte;
    168  1.19    rillig 	struct hte_list sorted = { NULL, 0, 0 };
    169  1.19    rillig 	size_t i;
    170  1.19    rillig 	hte_t **table = htab;
    171  1.19    rillig 
    172  1.26    rillig 	for (i = 0; i < HTAB_BUCKETS; i++)
    173  1.19    rillig 		for (hte = table[i]; hte != NULL; hte = hte->h_link)
    174  1.19    rillig 			hte_list_add(&sorted, hte);
    175  1.19    rillig 
    176  1.19    rillig 	qsort(sorted.items, sorted.len, sizeof(sorted.items[0]), hte_by_name);
    177  1.19    rillig 
    178  1.19    rillig 	for (i = 0; i < sorted.len; i++)
    179  1.19    rillig 		action(sorted.items[i]);
    180  1.20    rillig 
    181  1.20    rillig 	free(sorted.items);
    182  1.19    rillig }
    183  1.19    rillig 
    184   1.3       cgd /*
    185   1.3       cgd  * Free all contents of the hash table that this module allocated.
    186   1.3       cgd  */
    187   1.3       cgd void
    188  1.27    rillig hash_free(hte_t **table)
    189   1.3       cgd {
    190  1.25    rillig 	int i;
    191  1.25    rillig 	hte_t *hte, *nexthte;
    192   1.3       cgd 
    193  1.26    rillig 	for (i = 0; i < HTAB_BUCKETS; i++) {
    194   1.3       cgd 		for (hte = table[i]; hte != NULL; hte = nexthte) {
    195  1.11  christos 			free(__UNCONST(hte->h_name));
    196   1.3       cgd 			nexthte = hte->h_link;
    197   1.3       cgd 			free(hte);
    198   1.3       cgd 		}
    199   1.3       cgd 	}
    200   1.3       cgd 	free(table);
    201   1.1       cgd }
    202