1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*7c478bd9Sstevel@tonic-gate
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate * Routines for manipulating hash tables
31*7c478bd9Sstevel@tonic-gate */
32*7c478bd9Sstevel@tonic-gate
33*7c478bd9Sstevel@tonic-gate #include <stdio.h>
34*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
35*7c478bd9Sstevel@tonic-gate #include <strings.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
38*7c478bd9Sstevel@tonic-gate
39*7c478bd9Sstevel@tonic-gate #include "hash.h"
40*7c478bd9Sstevel@tonic-gate #include "memory.h"
41*7c478bd9Sstevel@tonic-gate #include "list.h"
42*7c478bd9Sstevel@tonic-gate
43*7c478bd9Sstevel@tonic-gate struct hash {
44*7c478bd9Sstevel@tonic-gate int h_nbuckets;
45*7c478bd9Sstevel@tonic-gate list_t **h_buckets;
46*7c478bd9Sstevel@tonic-gate
47*7c478bd9Sstevel@tonic-gate int (*h_hashfn)(int, void *);
48*7c478bd9Sstevel@tonic-gate int (*h_cmp)(void *, void *);
49*7c478bd9Sstevel@tonic-gate };
50*7c478bd9Sstevel@tonic-gate
51*7c478bd9Sstevel@tonic-gate struct hash_data {
52*7c478bd9Sstevel@tonic-gate hash_t *hd_hash;
53*7c478bd9Sstevel@tonic-gate int (*hd_fun)();
54*7c478bd9Sstevel@tonic-gate void *hd_key;
55*7c478bd9Sstevel@tonic-gate void *hd_private;
56*7c478bd9Sstevel@tonic-gate
57*7c478bd9Sstevel@tonic-gate void *hd_ret;
58*7c478bd9Sstevel@tonic-gate };
59*7c478bd9Sstevel@tonic-gate
60*7c478bd9Sstevel@tonic-gate static int
hash_def_hash(int nbuckets,uintptr_t data)61*7c478bd9Sstevel@tonic-gate hash_def_hash(int nbuckets, uintptr_t data)
62*7c478bd9Sstevel@tonic-gate {
63*7c478bd9Sstevel@tonic-gate return (data % nbuckets);
64*7c478bd9Sstevel@tonic-gate }
65*7c478bd9Sstevel@tonic-gate
66*7c478bd9Sstevel@tonic-gate static int
hash_def_cmp(uintptr_t d1,uintptr_t d2)67*7c478bd9Sstevel@tonic-gate hash_def_cmp(uintptr_t d1, uintptr_t d2)
68*7c478bd9Sstevel@tonic-gate {
69*7c478bd9Sstevel@tonic-gate return (d1 != d2);
70*7c478bd9Sstevel@tonic-gate }
71*7c478bd9Sstevel@tonic-gate
72*7c478bd9Sstevel@tonic-gate
73*7c478bd9Sstevel@tonic-gate int
hash_name(int nbuckets,const char * name)74*7c478bd9Sstevel@tonic-gate hash_name(int nbuckets, const char *name)
75*7c478bd9Sstevel@tonic-gate {
76*7c478bd9Sstevel@tonic-gate const char *c;
77*7c478bd9Sstevel@tonic-gate ulong_t g;
78*7c478bd9Sstevel@tonic-gate int h = 0;
79*7c478bd9Sstevel@tonic-gate
80*7c478bd9Sstevel@tonic-gate for (c = name; *c; c++) {
81*7c478bd9Sstevel@tonic-gate h = (h << 4) + *c;
82*7c478bd9Sstevel@tonic-gate if ((g = (h & 0xf0000000)) != 0) {
83*7c478bd9Sstevel@tonic-gate h ^= (g >> 24);
84*7c478bd9Sstevel@tonic-gate h ^= g;
85*7c478bd9Sstevel@tonic-gate }
86*7c478bd9Sstevel@tonic-gate }
87*7c478bd9Sstevel@tonic-gate
88*7c478bd9Sstevel@tonic-gate return (h % nbuckets);
89*7c478bd9Sstevel@tonic-gate }
90*7c478bd9Sstevel@tonic-gate
91*7c478bd9Sstevel@tonic-gate hash_t *
hash_new(int nbuckets,int (* hashfn)(int,void *),int (* cmp)(void *,void *))92*7c478bd9Sstevel@tonic-gate hash_new(int nbuckets, int (*hashfn)(int, void *), int (*cmp)(void *, void *))
93*7c478bd9Sstevel@tonic-gate {
94*7c478bd9Sstevel@tonic-gate hash_t *hash;
95*7c478bd9Sstevel@tonic-gate
96*7c478bd9Sstevel@tonic-gate hash = xmalloc(sizeof (hash_t));
97*7c478bd9Sstevel@tonic-gate hash->h_buckets = xcalloc(sizeof (list_t *) * nbuckets);
98*7c478bd9Sstevel@tonic-gate hash->h_nbuckets = nbuckets;
99*7c478bd9Sstevel@tonic-gate hash->h_hashfn = hashfn ? hashfn : (int (*)())hash_def_hash;
100*7c478bd9Sstevel@tonic-gate hash->h_cmp = cmp ? cmp : (int (*)())hash_def_cmp;
101*7c478bd9Sstevel@tonic-gate
102*7c478bd9Sstevel@tonic-gate return (hash);
103*7c478bd9Sstevel@tonic-gate }
104*7c478bd9Sstevel@tonic-gate
105*7c478bd9Sstevel@tonic-gate void
hash_add(hash_t * hash,void * key)106*7c478bd9Sstevel@tonic-gate hash_add(hash_t *hash, void *key)
107*7c478bd9Sstevel@tonic-gate {
108*7c478bd9Sstevel@tonic-gate int bucket = hash->h_hashfn(hash->h_nbuckets, key);
109*7c478bd9Sstevel@tonic-gate
110*7c478bd9Sstevel@tonic-gate list_add(&hash->h_buckets[bucket], key);
111*7c478bd9Sstevel@tonic-gate }
112*7c478bd9Sstevel@tonic-gate
113*7c478bd9Sstevel@tonic-gate static int
hash_add_cb(void * node,void * private)114*7c478bd9Sstevel@tonic-gate hash_add_cb(void *node, void *private)
115*7c478bd9Sstevel@tonic-gate {
116*7c478bd9Sstevel@tonic-gate hash_add((hash_t *)private, node);
117*7c478bd9Sstevel@tonic-gate return (0);
118*7c478bd9Sstevel@tonic-gate }
119*7c478bd9Sstevel@tonic-gate
120*7c478bd9Sstevel@tonic-gate void
hash_merge(hash_t * to,hash_t * from)121*7c478bd9Sstevel@tonic-gate hash_merge(hash_t *to, hash_t *from)
122*7c478bd9Sstevel@tonic-gate {
123*7c478bd9Sstevel@tonic-gate (void) hash_iter(from, hash_add_cb, to);
124*7c478bd9Sstevel@tonic-gate }
125*7c478bd9Sstevel@tonic-gate
126*7c478bd9Sstevel@tonic-gate static int
hash_remove_cb(void * key1,void * key2,hash_t * hash)127*7c478bd9Sstevel@tonic-gate hash_remove_cb(void *key1, void *key2, hash_t *hash)
128*7c478bd9Sstevel@tonic-gate {
129*7c478bd9Sstevel@tonic-gate return (hash->h_cmp(key1, key2));
130*7c478bd9Sstevel@tonic-gate }
131*7c478bd9Sstevel@tonic-gate
132*7c478bd9Sstevel@tonic-gate void
hash_remove(hash_t * hash,void * key)133*7c478bd9Sstevel@tonic-gate hash_remove(hash_t *hash, void *key)
134*7c478bd9Sstevel@tonic-gate {
135*7c478bd9Sstevel@tonic-gate int bucket = hash->h_hashfn(hash->h_nbuckets, key);
136*7c478bd9Sstevel@tonic-gate
137*7c478bd9Sstevel@tonic-gate (void) list_remove(&hash->h_buckets[bucket], key,
138*7c478bd9Sstevel@tonic-gate (int (*)())hash_remove_cb, hash);
139*7c478bd9Sstevel@tonic-gate }
140*7c478bd9Sstevel@tonic-gate
141*7c478bd9Sstevel@tonic-gate int
hash_match(hash_t * hash,void * key,int (* fun)(void *,void *),void * private)142*7c478bd9Sstevel@tonic-gate hash_match(hash_t *hash, void *key, int (*fun)(void *, void *),
143*7c478bd9Sstevel@tonic-gate void *private)
144*7c478bd9Sstevel@tonic-gate {
145*7c478bd9Sstevel@tonic-gate int bucket = hash->h_hashfn(hash->h_nbuckets, key);
146*7c478bd9Sstevel@tonic-gate
147*7c478bd9Sstevel@tonic-gate return (list_iter(hash->h_buckets[bucket], fun, private) < 0);
148*7c478bd9Sstevel@tonic-gate }
149*7c478bd9Sstevel@tonic-gate
150*7c478bd9Sstevel@tonic-gate static int
hash_find_list_cb(void * node,struct hash_data * hd)151*7c478bd9Sstevel@tonic-gate hash_find_list_cb(void *node, struct hash_data *hd)
152*7c478bd9Sstevel@tonic-gate {
153*7c478bd9Sstevel@tonic-gate int cbrc;
154*7c478bd9Sstevel@tonic-gate int rc = 0;
155*7c478bd9Sstevel@tonic-gate
156*7c478bd9Sstevel@tonic-gate if (hd->hd_hash->h_cmp(hd->hd_key, node) == 0) {
157*7c478bd9Sstevel@tonic-gate if ((cbrc = hd->hd_fun(node, hd->hd_private)) < 0)
158*7c478bd9Sstevel@tonic-gate return (cbrc);
159*7c478bd9Sstevel@tonic-gate rc += cbrc;
160*7c478bd9Sstevel@tonic-gate }
161*7c478bd9Sstevel@tonic-gate
162*7c478bd9Sstevel@tonic-gate return (rc);
163*7c478bd9Sstevel@tonic-gate }
164*7c478bd9Sstevel@tonic-gate
165*7c478bd9Sstevel@tonic-gate int
hash_find_iter(hash_t * hash,void * key,int (* fun)(void *,void *),void * private)166*7c478bd9Sstevel@tonic-gate hash_find_iter(hash_t *hash, void *key, int (*fun)(void *, void *),
167*7c478bd9Sstevel@tonic-gate void *private)
168*7c478bd9Sstevel@tonic-gate {
169*7c478bd9Sstevel@tonic-gate int bucket = hash->h_hashfn(hash->h_nbuckets, key);
170*7c478bd9Sstevel@tonic-gate struct hash_data hd;
171*7c478bd9Sstevel@tonic-gate
172*7c478bd9Sstevel@tonic-gate hd.hd_hash = hash;
173*7c478bd9Sstevel@tonic-gate hd.hd_fun = fun;
174*7c478bd9Sstevel@tonic-gate hd.hd_key = key;
175*7c478bd9Sstevel@tonic-gate hd.hd_private = private;
176*7c478bd9Sstevel@tonic-gate
177*7c478bd9Sstevel@tonic-gate return (list_iter(hash->h_buckets[bucket], (int (*)())hash_find_list_cb,
178*7c478bd9Sstevel@tonic-gate &hd));
179*7c478bd9Sstevel@tonic-gate }
180*7c478bd9Sstevel@tonic-gate
181*7c478bd9Sstevel@tonic-gate /* stop on first match */
182*7c478bd9Sstevel@tonic-gate static int
hash_find_first_cb(void * node,struct hash_data * hd)183*7c478bd9Sstevel@tonic-gate hash_find_first_cb(void *node, struct hash_data *hd)
184*7c478bd9Sstevel@tonic-gate {
185*7c478bd9Sstevel@tonic-gate if (hd->hd_hash->h_cmp(hd->hd_key, node) == 0) {
186*7c478bd9Sstevel@tonic-gate hd->hd_ret = node;
187*7c478bd9Sstevel@tonic-gate return (-1);
188*7c478bd9Sstevel@tonic-gate }
189*7c478bd9Sstevel@tonic-gate
190*7c478bd9Sstevel@tonic-gate return (0);
191*7c478bd9Sstevel@tonic-gate }
192*7c478bd9Sstevel@tonic-gate
193*7c478bd9Sstevel@tonic-gate int
hash_find(hash_t * hash,void * key,void ** value)194*7c478bd9Sstevel@tonic-gate hash_find(hash_t *hash, void *key, void **value)
195*7c478bd9Sstevel@tonic-gate {
196*7c478bd9Sstevel@tonic-gate int ret;
197*7c478bd9Sstevel@tonic-gate struct hash_data hd;
198*7c478bd9Sstevel@tonic-gate
199*7c478bd9Sstevel@tonic-gate hd.hd_hash = hash;
200*7c478bd9Sstevel@tonic-gate hd.hd_fun = hash_find_first_cb;
201*7c478bd9Sstevel@tonic-gate hd.hd_key = key;
202*7c478bd9Sstevel@tonic-gate
203*7c478bd9Sstevel@tonic-gate ret = hash_match(hash, key, (int (*)())hash_find_first_cb, &hd);
204*7c478bd9Sstevel@tonic-gate if (ret && value)
205*7c478bd9Sstevel@tonic-gate *value = hd.hd_ret;
206*7c478bd9Sstevel@tonic-gate
207*7c478bd9Sstevel@tonic-gate return (ret);
208*7c478bd9Sstevel@tonic-gate }
209*7c478bd9Sstevel@tonic-gate
210*7c478bd9Sstevel@tonic-gate int
hash_iter(hash_t * hash,int (* fun)(void *,void *),void * private)211*7c478bd9Sstevel@tonic-gate hash_iter(hash_t *hash, int (*fun)(void *, void *), void *private)
212*7c478bd9Sstevel@tonic-gate {
213*7c478bd9Sstevel@tonic-gate int cumrc = 0;
214*7c478bd9Sstevel@tonic-gate int cbrc;
215*7c478bd9Sstevel@tonic-gate int i;
216*7c478bd9Sstevel@tonic-gate
217*7c478bd9Sstevel@tonic-gate for (i = 0; i < hash->h_nbuckets; i++) {
218*7c478bd9Sstevel@tonic-gate if (hash->h_buckets[i] != NULL) {
219*7c478bd9Sstevel@tonic-gate if ((cbrc = list_iter(hash->h_buckets[i], fun,
220*7c478bd9Sstevel@tonic-gate private)) < 0)
221*7c478bd9Sstevel@tonic-gate return (cbrc);
222*7c478bd9Sstevel@tonic-gate cumrc += cbrc;
223*7c478bd9Sstevel@tonic-gate }
224*7c478bd9Sstevel@tonic-gate }
225*7c478bd9Sstevel@tonic-gate
226*7c478bd9Sstevel@tonic-gate return (cumrc);
227*7c478bd9Sstevel@tonic-gate }
228*7c478bd9Sstevel@tonic-gate
229*7c478bd9Sstevel@tonic-gate int
hash_count(hash_t * hash)230*7c478bd9Sstevel@tonic-gate hash_count(hash_t *hash)
231*7c478bd9Sstevel@tonic-gate {
232*7c478bd9Sstevel@tonic-gate int num, i;
233*7c478bd9Sstevel@tonic-gate
234*7c478bd9Sstevel@tonic-gate for (num = 0, i = 0; i < hash->h_nbuckets; i++)
235*7c478bd9Sstevel@tonic-gate num += list_count(hash->h_buckets[i]);
236*7c478bd9Sstevel@tonic-gate
237*7c478bd9Sstevel@tonic-gate return (num);
238*7c478bd9Sstevel@tonic-gate }
239*7c478bd9Sstevel@tonic-gate
240*7c478bd9Sstevel@tonic-gate void
hash_free(hash_t * hash,void (* datafree)(void *,void *),void * private)241*7c478bd9Sstevel@tonic-gate hash_free(hash_t *hash, void (*datafree)(void *, void *), void *private)
242*7c478bd9Sstevel@tonic-gate {
243*7c478bd9Sstevel@tonic-gate int i;
244*7c478bd9Sstevel@tonic-gate
245*7c478bd9Sstevel@tonic-gate if (hash == NULL)
246*7c478bd9Sstevel@tonic-gate return;
247*7c478bd9Sstevel@tonic-gate
248*7c478bd9Sstevel@tonic-gate for (i = 0; i < hash->h_nbuckets; i++)
249*7c478bd9Sstevel@tonic-gate list_free(hash->h_buckets[i], datafree, private);
250*7c478bd9Sstevel@tonic-gate free(hash->h_buckets);
251*7c478bd9Sstevel@tonic-gate free(hash);
252*7c478bd9Sstevel@tonic-gate }
253*7c478bd9Sstevel@tonic-gate
254*7c478bd9Sstevel@tonic-gate void
hash_stats(hash_t * hash,int verbose)255*7c478bd9Sstevel@tonic-gate hash_stats(hash_t *hash, int verbose)
256*7c478bd9Sstevel@tonic-gate {
257*7c478bd9Sstevel@tonic-gate int min = list_count(hash->h_buckets[0]);
258*7c478bd9Sstevel@tonic-gate int minidx = 0;
259*7c478bd9Sstevel@tonic-gate int max = min;
260*7c478bd9Sstevel@tonic-gate int maxidx = 0;
261*7c478bd9Sstevel@tonic-gate int tot = min;
262*7c478bd9Sstevel@tonic-gate int count;
263*7c478bd9Sstevel@tonic-gate int i;
264*7c478bd9Sstevel@tonic-gate
265*7c478bd9Sstevel@tonic-gate if (min && verbose)
266*7c478bd9Sstevel@tonic-gate printf("%3d: %d\n", 0, min);
267*7c478bd9Sstevel@tonic-gate for (i = 1; i < hash->h_nbuckets; i++) {
268*7c478bd9Sstevel@tonic-gate count = list_count(hash->h_buckets[i]);
269*7c478bd9Sstevel@tonic-gate if (min > count) {
270*7c478bd9Sstevel@tonic-gate min = count;
271*7c478bd9Sstevel@tonic-gate minidx = i;
272*7c478bd9Sstevel@tonic-gate }
273*7c478bd9Sstevel@tonic-gate if (max < count) {
274*7c478bd9Sstevel@tonic-gate max = count;
275*7c478bd9Sstevel@tonic-gate maxidx = i;
276*7c478bd9Sstevel@tonic-gate }
277*7c478bd9Sstevel@tonic-gate if (count && verbose)
278*7c478bd9Sstevel@tonic-gate printf("%3d: %d\n", i, count);
279*7c478bd9Sstevel@tonic-gate tot += count;
280*7c478bd9Sstevel@tonic-gate }
281*7c478bd9Sstevel@tonic-gate
282*7c478bd9Sstevel@tonic-gate printf("Hash statistics:\n");
283*7c478bd9Sstevel@tonic-gate printf(" Buckets: %d\n", hash->h_nbuckets);
284*7c478bd9Sstevel@tonic-gate printf(" Items : %d\n", tot);
285*7c478bd9Sstevel@tonic-gate printf(" Min/Max: %d in #%d, %d in #%d\n", min, minidx, max, maxidx);
286*7c478bd9Sstevel@tonic-gate printf(" Average: %5.2f\n", (float)tot / (float)hash->h_nbuckets);
287*7c478bd9Sstevel@tonic-gate }
288