Home | History | Annotate | Line # | Download | only in lint2
hash.c revision 1.8
      1  1.8        tv /*	$NetBSD: hash.c,v 1.8 2002/01/31 19:36:55 tv 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.1       cgd  *      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.4  christos #include <sys/cdefs.h>
     35  1.8        tv #if defined(__RCSID) && !defined(lint)
     36  1.8        tv __RCSID("$NetBSD: hash.c,v 1.8 2002/01/31 19:36:55 tv Exp $");
     37  1.1       cgd #endif
     38  1.1       cgd 
     39  1.3       cgd /*
     40  1.3       cgd  * XXX Really need a generalized hash table package
     41  1.3       cgd  */
     42  1.3       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.1       cgd /* pointer to hash table, initialized in inithash() */
     51  1.1       cgd static	hte_t	**htab;
     52  1.1       cgd 
     53  1.6     lukem static	int	hash(const char *);
     54  1.1       cgd 
     55  1.1       cgd /*
     56  1.1       cgd  * Initialize hash table.
     57  1.1       cgd  */
     58  1.1       cgd void
     59  1.6     lukem _inithash(hte_t ***tablep)
     60  1.1       cgd {
     61  1.3       cgd 
     62  1.3       cgd 	if (tablep == NULL)
     63  1.3       cgd 		tablep = &htab;
     64  1.3       cgd 
     65  1.3       cgd 	*tablep = xcalloc(HSHSIZ2, sizeof (hte_t *));
     66  1.1       cgd }
     67  1.1       cgd 
     68  1.1       cgd /*
     69  1.1       cgd  * Compute hash value from a string.
     70  1.1       cgd  */
     71  1.1       cgd static int
     72  1.6     lukem hash(const char *s)
     73  1.1       cgd {
     74  1.1       cgd 	u_int	v;
     75  1.1       cgd 	const	u_char *us;
     76  1.1       cgd 
     77  1.1       cgd 	v = 0;
     78  1.1       cgd 	for (us = (const u_char *)s; *us != '\0'; us++) {
     79  1.1       cgd 		v = (v << sizeof (v)) + *us;
     80  1.1       cgd 		v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v));
     81  1.1       cgd 	}
     82  1.1       cgd 	return (v % HSHSIZ2);
     83  1.1       cgd }
     84  1.1       cgd 
     85  1.1       cgd /*
     86  1.1       cgd  * Look for a hash table entry. If no hash table entry for the
     87  1.1       cgd  * given name exists and mknew is set, create a new one.
     88  1.1       cgd  */
     89  1.1       cgd hte_t *
     90  1.6     lukem _hsearch(hte_t **table, const char *s, int mknew)
     91  1.1       cgd {
     92  1.1       cgd 	int	h;
     93  1.1       cgd 	hte_t	*hte;
     94  1.1       cgd 
     95  1.3       cgd 	if (table == NULL)
     96  1.3       cgd 		table = htab;
     97  1.3       cgd 
     98  1.1       cgd 	h = hash(s);
     99  1.3       cgd 	for (hte = table[h]; hte != NULL; hte = hte->h_link) {
    100  1.1       cgd 		if (strcmp(hte->h_name, s) == 0)
    101  1.1       cgd 			break;
    102  1.1       cgd 	}
    103  1.1       cgd 
    104  1.1       cgd 	if (hte != NULL || !mknew)
    105  1.1       cgd 		return (hte);
    106  1.1       cgd 
    107  1.1       cgd 	/* create a new hte */
    108  1.3       cgd 	hte = xmalloc(sizeof (hte_t));
    109  1.1       cgd 	hte->h_name = xstrdup(s);
    110  1.5   mycroft 	hte->h_used = 0;
    111  1.5   mycroft 	hte->h_def = 0;
    112  1.5   mycroft 	hte->h_static = 0;
    113  1.5   mycroft 	hte->h_syms = NULL;
    114  1.1       cgd 	hte->h_lsym = &hte->h_syms;
    115  1.5   mycroft 	hte->h_calls = NULL;
    116  1.1       cgd 	hte->h_lcall = &hte->h_calls;
    117  1.5   mycroft 	hte->h_usyms = NULL;
    118  1.1       cgd 	hte->h_lusym = &hte->h_usyms;
    119  1.3       cgd 	hte->h_link = table[h];
    120  1.3       cgd 	hte->h_hte = NULL;
    121  1.3       cgd 	table[h] = hte;
    122  1.1       cgd 
    123  1.1       cgd 	return (hte);
    124  1.1       cgd }
    125  1.1       cgd 
    126  1.1       cgd /*
    127  1.1       cgd  * Call function f for each name in the hash table.
    128  1.1       cgd  */
    129  1.1       cgd void
    130  1.6     lukem _forall(hte_t **table, void (*f)(hte_t *))
    131  1.1       cgd {
    132  1.1       cgd 	int	i;
    133  1.1       cgd 	hte_t	*hte;
    134  1.1       cgd 
    135  1.3       cgd 	if (table == NULL)
    136  1.3       cgd 		table = htab;
    137  1.3       cgd 
    138  1.1       cgd 	for (i = 0; i < HSHSIZ2; i++) {
    139  1.3       cgd 		for (hte = table[i]; hte != NULL; hte = hte->h_link)
    140  1.1       cgd 			(*f)(hte);
    141  1.1       cgd 	}
    142  1.3       cgd }
    143  1.3       cgd 
    144  1.3       cgd /*
    145  1.3       cgd  * Free all contents of the hash table that this module allocated.
    146  1.3       cgd  */
    147  1.3       cgd void
    148  1.6     lukem _destroyhash(hte_t **table)
    149  1.3       cgd {
    150  1.3       cgd 	int	i;
    151  1.3       cgd 	hte_t	*hte, *nexthte;
    152  1.3       cgd 
    153  1.3       cgd 	if (table == NULL)
    154  1.3       cgd 		err(1, "_destroyhash called on main hash table");
    155  1.3       cgd 
    156  1.3       cgd 	for (i = 0; i < HSHSIZ2; i++) {
    157  1.3       cgd 		for (hte = table[i]; hte != NULL; hte = nexthte) {
    158  1.4  christos 			free((void *)hte->h_name);
    159  1.3       cgd 			nexthte = hte->h_link;
    160  1.3       cgd 			free(hte);
    161  1.3       cgd 		}
    162  1.3       cgd 	}
    163  1.3       cgd 	free(table);
    164  1.1       cgd }
    165