hash.c revision 1.2 1 1.2 cgd /* $NetBSD: hash.c,v 1.2 1995/07/03 21:24:47 cgd 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 cgd static char rcsid[] = "$NetBSD: hash.c,v 1.2 1995/07/03 21:24:47 cgd Exp $";
36 1.1 cgd #endif
37 1.1 cgd
38 1.1 cgd #include <stddef.h>
39 1.1 cgd #include <string.h>
40 1.1 cgd #include <limits.h>
41 1.1 cgd
42 1.1 cgd #include "lint2.h"
43 1.1 cgd
44 1.1 cgd /* pointer to hash table, initialized in inithash() */
45 1.1 cgd static hte_t **htab;
46 1.1 cgd
47 1.1 cgd static int hash __P((const char *));
48 1.1 cgd
49 1.1 cgd /*
50 1.1 cgd * Initialize hash table.
51 1.1 cgd */
52 1.1 cgd void
53 1.1 cgd inithash()
54 1.1 cgd {
55 1.1 cgd htab = xcalloc(HSHSIZ2, sizeof (hte_t *));
56 1.1 cgd }
57 1.1 cgd
58 1.1 cgd /*
59 1.1 cgd * Compute hash value from a string.
60 1.1 cgd */
61 1.1 cgd static int
62 1.1 cgd hash(s)
63 1.1 cgd const char *s;
64 1.1 cgd {
65 1.1 cgd u_int v;
66 1.1 cgd const u_char *us;
67 1.1 cgd
68 1.1 cgd v = 0;
69 1.1 cgd for (us = (const u_char *)s; *us != '\0'; us++) {
70 1.1 cgd v = (v << sizeof (v)) + *us;
71 1.1 cgd v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v));
72 1.1 cgd }
73 1.1 cgd return (v % HSHSIZ2);
74 1.1 cgd }
75 1.1 cgd
76 1.1 cgd /*
77 1.1 cgd * Look for a hash table entry. If no hash table entry for the
78 1.1 cgd * given name exists and mknew is set, create a new one.
79 1.1 cgd */
80 1.1 cgd hte_t *
81 1.1 cgd hsearch(s, mknew)
82 1.1 cgd const char *s;
83 1.1 cgd int mknew;
84 1.1 cgd {
85 1.1 cgd int h;
86 1.1 cgd hte_t *hte;
87 1.1 cgd
88 1.1 cgd h = hash(s);
89 1.1 cgd for (hte = htab[h]; hte != NULL; hte = hte->h_link) {
90 1.1 cgd if (strcmp(hte->h_name, s) == 0)
91 1.1 cgd break;
92 1.1 cgd }
93 1.1 cgd
94 1.1 cgd if (hte != NULL || !mknew)
95 1.1 cgd return (hte);
96 1.1 cgd
97 1.1 cgd /* create a new hte */
98 1.1 cgd hte = xalloc(sizeof (hte_t));
99 1.1 cgd hte->h_name = xstrdup(s);
100 1.1 cgd hte->h_lsym = &hte->h_syms;
101 1.1 cgd hte->h_lcall = &hte->h_calls;
102 1.1 cgd hte->h_lusym = &hte->h_usyms;
103 1.1 cgd hte->h_link = htab[h];
104 1.1 cgd htab[h] = hte;
105 1.1 cgd
106 1.1 cgd return (hte);
107 1.1 cgd }
108 1.1 cgd
109 1.1 cgd /*
110 1.1 cgd * Call function f for each name in the hash table.
111 1.1 cgd */
112 1.1 cgd void
113 1.1 cgd forall(f)
114 1.1 cgd void (*f) __P((hte_t *));
115 1.1 cgd {
116 1.1 cgd int i;
117 1.1 cgd hte_t *hte;
118 1.1 cgd
119 1.1 cgd for (i = 0; i < HSHSIZ2; i++) {
120 1.1 cgd for (hte = htab[i]; hte != NULL; hte = hte->h_link)
121 1.1 cgd (*f)(hte);
122 1.1 cgd }
123 1.1 cgd }
124