Home | History | Annotate | Line # | Download | only in lint2
hash.c revision 1.2.4.1
      1  1.2.4.1  thorpej /*	$NetBSD: hash.c,v 1.2.4.1 1997/11/04 21:42:43 thorpej 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.1      cgd #ifndef lint
     35  1.2.4.1  thorpej static char rcsid[] = "$NetBSD: hash.c,v 1.2.4.1 1997/11/04 21:42:43 thorpej Exp $";
     36      1.1      cgd #endif
     37      1.1      cgd 
     38  1.2.4.1  thorpej /*
     39  1.2.4.1  thorpej  * XXX Really need a generalized hash table package
     40  1.2.4.1  thorpej  */
     41  1.2.4.1  thorpej 
     42      1.1      cgd #include <stddef.h>
     43      1.1      cgd #include <string.h>
     44      1.1      cgd #include <limits.h>
     45      1.1      cgd 
     46      1.1      cgd #include "lint2.h"
     47      1.1      cgd 
     48      1.1      cgd /* pointer to hash table, initialized in inithash() */
     49      1.1      cgd static	hte_t	**htab;
     50      1.1      cgd 
     51      1.1      cgd static	int	hash __P((const char *));
     52      1.1      cgd 
     53      1.1      cgd /*
     54      1.1      cgd  * Initialize hash table.
     55      1.1      cgd  */
     56      1.1      cgd void
     57  1.2.4.1  thorpej _inithash(tablep)
     58  1.2.4.1  thorpej 	hte_t ***tablep;
     59      1.1      cgd {
     60  1.2.4.1  thorpej 
     61  1.2.4.1  thorpej 	if (tablep == NULL)
     62  1.2.4.1  thorpej 		tablep = &htab;
     63  1.2.4.1  thorpej 
     64  1.2.4.1  thorpej 	*tablep = xcalloc(HSHSIZ2, sizeof (hte_t *));
     65      1.1      cgd }
     66      1.1      cgd 
     67      1.1      cgd /*
     68      1.1      cgd  * Compute hash value from a string.
     69      1.1      cgd  */
     70      1.1      cgd static int
     71      1.1      cgd hash(s)
     72      1.1      cgd 	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.2.4.1  thorpej _hsearch(table, s, mknew)
     91  1.2.4.1  thorpej 	hte_t	**table;
     92      1.1      cgd 	const	char *s;
     93      1.1      cgd 	int	mknew;
     94      1.1      cgd {
     95      1.1      cgd 	int	h;
     96      1.1      cgd 	hte_t	*hte;
     97      1.1      cgd 
     98  1.2.4.1  thorpej 	if (table == NULL)
     99  1.2.4.1  thorpej 		table = htab;
    100  1.2.4.1  thorpej 
    101      1.1      cgd 	h = hash(s);
    102  1.2.4.1  thorpej 	for (hte = table[h]; hte != NULL; hte = hte->h_link) {
    103      1.1      cgd 		if (strcmp(hte->h_name, s) == 0)
    104      1.1      cgd 			break;
    105      1.1      cgd 	}
    106      1.1      cgd 
    107      1.1      cgd 	if (hte != NULL || !mknew)
    108      1.1      cgd 		return (hte);
    109      1.1      cgd 
    110      1.1      cgd 	/* create a new hte */
    111  1.2.4.1  thorpej 	hte = xmalloc(sizeof (hte_t));
    112      1.1      cgd 	hte->h_name = xstrdup(s);
    113      1.1      cgd 	hte->h_lsym = &hte->h_syms;
    114      1.1      cgd 	hte->h_lcall = &hte->h_calls;
    115      1.1      cgd 	hte->h_lusym = &hte->h_usyms;
    116  1.2.4.1  thorpej 	hte->h_link = table[h];
    117  1.2.4.1  thorpej 	hte->h_hte = NULL;
    118  1.2.4.1  thorpej 	table[h] = hte;
    119      1.1      cgd 
    120      1.1      cgd 	return (hte);
    121      1.1      cgd }
    122      1.1      cgd 
    123      1.1      cgd /*
    124      1.1      cgd  * Call function f for each name in the hash table.
    125      1.1      cgd  */
    126      1.1      cgd void
    127  1.2.4.1  thorpej _forall(table, f)
    128  1.2.4.1  thorpej 	hte_t	**table;
    129      1.1      cgd 	void	(*f) __P((hte_t *));
    130      1.1      cgd {
    131      1.1      cgd 	int	i;
    132      1.1      cgd 	hte_t	*hte;
    133      1.1      cgd 
    134  1.2.4.1  thorpej 	if (table == NULL)
    135  1.2.4.1  thorpej 		table = htab;
    136  1.2.4.1  thorpej 
    137      1.1      cgd 	for (i = 0; i < HSHSIZ2; i++) {
    138  1.2.4.1  thorpej 		for (hte = table[i]; hte != NULL; hte = hte->h_link)
    139      1.1      cgd 			(*f)(hte);
    140      1.1      cgd 	}
    141  1.2.4.1  thorpej }
    142  1.2.4.1  thorpej 
    143  1.2.4.1  thorpej /*
    144  1.2.4.1  thorpej  * Free all contents of the hash table that this module allocated.
    145  1.2.4.1  thorpej  */
    146  1.2.4.1  thorpej void
    147  1.2.4.1  thorpej _destroyhash(table)
    148  1.2.4.1  thorpej 	hte_t	**table;
    149  1.2.4.1  thorpej {
    150  1.2.4.1  thorpej 	int	i;
    151  1.2.4.1  thorpej 	hte_t	*hte, *nexthte;
    152  1.2.4.1  thorpej 
    153  1.2.4.1  thorpej 	if (table == NULL)
    154  1.2.4.1  thorpej 		err(1, "_destroyhash called on main hash table");
    155  1.2.4.1  thorpej 
    156  1.2.4.1  thorpej 	for (i = 0; i < HSHSIZ2; i++) {
    157  1.2.4.1  thorpej 		for (hte = table[i]; hte != NULL; hte = nexthte) {
    158  1.2.4.1  thorpej 			free(hte->h_name);
    159  1.2.4.1  thorpej 			nexthte = hte->h_link;
    160  1.2.4.1  thorpej 			free(hte);
    161  1.2.4.1  thorpej 		}
    162  1.2.4.1  thorpej 	}
    163  1.2.4.1  thorpej 	free(table);
    164      1.1      cgd }
    165