Home | History | Annotate | Line # | Download | only in lint2
hash.c revision 1.3
      1 /*	$NetBSD: hash.c,v 1.3 1997/11/03 22:36:59 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994, 1995 Jochen Pohl
      5  * All Rights Reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Jochen Pohl for
     18  *	The NetBSD Project.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 static char rcsid[] = "$NetBSD: hash.c,v 1.3 1997/11/03 22:36:59 cgd Exp $";
     36 #endif
     37 
     38 /*
     39  * XXX Really need a generalized hash table package
     40  */
     41 
     42 #include <stddef.h>
     43 #include <string.h>
     44 #include <limits.h>
     45 
     46 #include "lint2.h"
     47 
     48 /* pointer to hash table, initialized in inithash() */
     49 static	hte_t	**htab;
     50 
     51 static	int	hash __P((const char *));
     52 
     53 /*
     54  * Initialize hash table.
     55  */
     56 void
     57 _inithash(tablep)
     58 	hte_t ***tablep;
     59 {
     60 
     61 	if (tablep == NULL)
     62 		tablep = &htab;
     63 
     64 	*tablep = xcalloc(HSHSIZ2, sizeof (hte_t *));
     65 }
     66 
     67 /*
     68  * Compute hash value from a string.
     69  */
     70 static int
     71 hash(s)
     72 	const	char *s;
     73 {
     74 	u_int	v;
     75 	const	u_char *us;
     76 
     77 	v = 0;
     78 	for (us = (const u_char *)s; *us != '\0'; us++) {
     79 		v = (v << sizeof (v)) + *us;
     80 		v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v));
     81 	}
     82 	return (v % HSHSIZ2);
     83 }
     84 
     85 /*
     86  * Look for a hash table entry. If no hash table entry for the
     87  * given name exists and mknew is set, create a new one.
     88  */
     89 hte_t *
     90 _hsearch(table, s, mknew)
     91 	hte_t	**table;
     92 	const	char *s;
     93 	int	mknew;
     94 {
     95 	int	h;
     96 	hte_t	*hte;
     97 
     98 	if (table == NULL)
     99 		table = htab;
    100 
    101 	h = hash(s);
    102 	for (hte = table[h]; hte != NULL; hte = hte->h_link) {
    103 		if (strcmp(hte->h_name, s) == 0)
    104 			break;
    105 	}
    106 
    107 	if (hte != NULL || !mknew)
    108 		return (hte);
    109 
    110 	/* create a new hte */
    111 	hte = xmalloc(sizeof (hte_t));
    112 	hte->h_name = xstrdup(s);
    113 	hte->h_lsym = &hte->h_syms;
    114 	hte->h_lcall = &hte->h_calls;
    115 	hte->h_lusym = &hte->h_usyms;
    116 	hte->h_link = table[h];
    117 	hte->h_hte = NULL;
    118 	table[h] = hte;
    119 
    120 	return (hte);
    121 }
    122 
    123 /*
    124  * Call function f for each name in the hash table.
    125  */
    126 void
    127 _forall(table, f)
    128 	hte_t	**table;
    129 	void	(*f) __P((hte_t *));
    130 {
    131 	int	i;
    132 	hte_t	*hte;
    133 
    134 	if (table == NULL)
    135 		table = htab;
    136 
    137 	for (i = 0; i < HSHSIZ2; i++) {
    138 		for (hte = table[i]; hte != NULL; hte = hte->h_link)
    139 			(*f)(hte);
    140 	}
    141 }
    142 
    143 /*
    144  * Free all contents of the hash table that this module allocated.
    145  */
    146 void
    147 _destroyhash(table)
    148 	hte_t	**table;
    149 {
    150 	int	i;
    151 	hte_t	*hte, *nexthte;
    152 
    153 	if (table == NULL)
    154 		err(1, "_destroyhash called on main hash table");
    155 
    156 	for (i = 0; i < HSHSIZ2; i++) {
    157 		for (hte = table[i]; hte != NULL; hte = nexthte) {
    158 			free(hte->h_name);
    159 			nexthte = hte->h_link;
    160 			free(hte);
    161 		}
    162 	}
    163 	free(table);
    164 }
    165