xref: /titanic_44/usr/src/lib/libpool/common/dict.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 2003 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  * This file contains a basic dictionary implementation which stores
31*7c478bd9Sstevel@tonic-gate  * arbitrary key-value mappings. It is used by libpool to store
32*7c478bd9Sstevel@tonic-gate  * information about element pointers (pool_elem_t) in the kernel
33*7c478bd9Sstevel@tonic-gate  * provider implementation.
34*7c478bd9Sstevel@tonic-gate  */
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #include <stdio.h>
37*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
39*7c478bd9Sstevel@tonic-gate #include <unistd.h>
40*7c478bd9Sstevel@tonic-gate #include <string.h>
41*7c478bd9Sstevel@tonic-gate #include <assert.h>
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #include "dict.h"
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate /*
46*7c478bd9Sstevel@tonic-gate  * HASH_64_INIT is the same as the INIT value since it is the value
47*7c478bd9Sstevel@tonic-gate  * used by FNV (FNV1_64_INIT). More details on FNV are available at:
48*7c478bd9Sstevel@tonic-gate  *
49*7c478bd9Sstevel@tonic-gate  * http://www.isthe.com/chongo/tech/comp/fnv/index.html
50*7c478bd9Sstevel@tonic-gate  */
51*7c478bd9Sstevel@tonic-gate #define	HASH_64_INIT	(0xcbf29ce484222325ULL) /* Hash initializer */
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate /*
54*7c478bd9Sstevel@tonic-gate  * HASH_64_PRIME is a large prime number chosen to minimize hashing
55*7c478bd9Sstevel@tonic-gate  * collisions.
56*7c478bd9Sstevel@tonic-gate  */
57*7c478bd9Sstevel@tonic-gate #define	HASH_64_PRIME	(0x100000001b3ULL)	/* Large Prime */
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate /*
60*7c478bd9Sstevel@tonic-gate  * DICT_SIZE is chosen as it is the nearest prime to 2^9 (512). 512 is
61*7c478bd9Sstevel@tonic-gate  * chosen as it is unlikely that this dictionary will contain more
62*7c478bd9Sstevel@tonic-gate  * elements than this in normal operation. Of course overflow in each
63*7c478bd9Sstevel@tonic-gate  * bucket is acceptable, but if there is too much overflow, then
64*7c478bd9Sstevel@tonic-gate  * performance will degrade to that of a list.
65*7c478bd9Sstevel@tonic-gate  */
66*7c478bd9Sstevel@tonic-gate #define	DICT_SIZE	509			/* Reasonable prime */
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate /*
69*7c478bd9Sstevel@tonic-gate  * Data Types
70*7c478bd9Sstevel@tonic-gate  */
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate /*
73*7c478bd9Sstevel@tonic-gate  * A key bucket.
74*7c478bd9Sstevel@tonic-gate  */
75*7c478bd9Sstevel@tonic-gate typedef struct dict_bucket
76*7c478bd9Sstevel@tonic-gate {
77*7c478bd9Sstevel@tonic-gate 	const void		*db_key;	/* key */
78*7c478bd9Sstevel@tonic-gate 	void			*db_value;	/* value */
79*7c478bd9Sstevel@tonic-gate 	struct dict_bucket	*db_next;	/* next bucket */
80*7c478bd9Sstevel@tonic-gate } dict_bucket_t;
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate /*
83*7c478bd9Sstevel@tonic-gate  * A dictionary which holds a mapping between a key and a value.
84*7c478bd9Sstevel@tonic-gate  *	dh_change	- detects changes to the dictionary.
85*7c478bd9Sstevel@tonic-gate  *	dh_cmp		- comparison function
86*7c478bd9Sstevel@tonic-gate  *	dh_hash		- hashing function
87*7c478bd9Sstevel@tonic-gate  *	dh_buckets	- key storage
88*7c478bd9Sstevel@tonic-gate  *	dh_size		- # of buckets
89*7c478bd9Sstevel@tonic-gate  */
90*7c478bd9Sstevel@tonic-gate struct dict_hdl {
91*7c478bd9Sstevel@tonic-gate 	uint64_t		dh_change;
92*7c478bd9Sstevel@tonic-gate 	int			(*dh_cmp)(const void *, const void *);
93*7c478bd9Sstevel@tonic-gate 	uint64_t		(*dh_hash)(const void *);
94*7c478bd9Sstevel@tonic-gate 	uint64_t		dh_length;
95*7c478bd9Sstevel@tonic-gate 	dict_bucket_t		**dh_buckets;
96*7c478bd9Sstevel@tonic-gate 	uint64_t		dh_size;
97*7c478bd9Sstevel@tonic-gate };
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate /*
100*7c478bd9Sstevel@tonic-gate  * Utility functions. Mainly used for debugging
101*7c478bd9Sstevel@tonic-gate  */
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate #if defined(DEBUG)
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate static void		bit_print_32(unsigned int);
106*7c478bd9Sstevel@tonic-gate static void		bit_print_64(unsigned long long);
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate /*
111*7c478bd9Sstevel@tonic-gate  * Default functions for hashing and comparing if the user does not specify
112*7c478bd9Sstevel@tonic-gate  * these values when creating the dictionary.
113*7c478bd9Sstevel@tonic-gate  */
114*7c478bd9Sstevel@tonic-gate static int		cmp_addr(const void *, const void *);
115*7c478bd9Sstevel@tonic-gate static uint64_t		hash_addr(const void *);
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate /*
118*7c478bd9Sstevel@tonic-gate  * static functions
119*7c478bd9Sstevel@tonic-gate  */
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate #if defined(DEBUG)
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate /*
124*7c478bd9Sstevel@tonic-gate  * Print to standard out the bit representation of the supplied value
125*7c478bd9Sstevel@tonic-gate  */
126*7c478bd9Sstevel@tonic-gate void
bit_print_32(unsigned int v)127*7c478bd9Sstevel@tonic-gate bit_print_32(unsigned int v)
128*7c478bd9Sstevel@tonic-gate {
129*7c478bd9Sstevel@tonic-gate #pragma	error_messages(off, E_INTEGER_OVERFLOW_DETECTED)
130*7c478bd9Sstevel@tonic-gate 	int i, mask = 1 << 31;
131*7c478bd9Sstevel@tonic-gate #pragma	error_messages(on, E_INTEGER_OVERFLOW_DETECTED)
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	for (i = 1; i <= 32; i++) {
134*7c478bd9Sstevel@tonic-gate 		(void) putchar(((v & mask) == 0) ? '0' : '1');
135*7c478bd9Sstevel@tonic-gate 		v <<= 1;
136*7c478bd9Sstevel@tonic-gate 		if (i % 8 == 0 && i != 32)
137*7c478bd9Sstevel@tonic-gate 			(void) putchar(' ');
138*7c478bd9Sstevel@tonic-gate 	}
139*7c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
140*7c478bd9Sstevel@tonic-gate }
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate /*
143*7c478bd9Sstevel@tonic-gate  * Print to standard out the bit representation of the supplied value
144*7c478bd9Sstevel@tonic-gate  */
145*7c478bd9Sstevel@tonic-gate void
bit_print_64(unsigned long long v)146*7c478bd9Sstevel@tonic-gate bit_print_64(unsigned long long v)
147*7c478bd9Sstevel@tonic-gate {
148*7c478bd9Sstevel@tonic-gate #pragma	error_messages(off, E_INTEGER_OVERFLOW_DETECTED)
149*7c478bd9Sstevel@tonic-gate 	long long mask = 1ll << 63;
150*7c478bd9Sstevel@tonic-gate #pragma	error_messages(on, E_INTEGER_OVERFLOW_DETECTED)
151*7c478bd9Sstevel@tonic-gate 	int i;
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	for (i = 1; i <= 64; i++) {
154*7c478bd9Sstevel@tonic-gate 		(void) putchar(((v & mask) == 0) ? '0' : '1');
155*7c478bd9Sstevel@tonic-gate 		v <<= 1;
156*7c478bd9Sstevel@tonic-gate 		if (i % 8 == 0 && i != 64)
157*7c478bd9Sstevel@tonic-gate 			(void) putchar(' ');
158*7c478bd9Sstevel@tonic-gate 	}
159*7c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
160*7c478bd9Sstevel@tonic-gate }
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate /*
167*7c478bd9Sstevel@tonic-gate  * Default comparison function which is used if no comparison function
168*7c478bd9Sstevel@tonic-gate  * is supplied when the dictionary is created. The default behaviour
169*7c478bd9Sstevel@tonic-gate  * is to compare memory address.
170*7c478bd9Sstevel@tonic-gate  */
171*7c478bd9Sstevel@tonic-gate int
cmp_addr(const void * x,const void * y)172*7c478bd9Sstevel@tonic-gate cmp_addr(const void *x, const void *y)
173*7c478bd9Sstevel@tonic-gate {
174*7c478bd9Sstevel@tonic-gate 	return (x != y);
175*7c478bd9Sstevel@tonic-gate }
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate /*
179*7c478bd9Sstevel@tonic-gate  * The default hashing function which is used if no hashing function
180*7c478bd9Sstevel@tonic-gate  * is provided when the dictionary is created. The default behaviour
181*7c478bd9Sstevel@tonic-gate  * is to use the hash_buf() function.
182*7c478bd9Sstevel@tonic-gate  */
183*7c478bd9Sstevel@tonic-gate uint64_t
hash_addr(const void * key)184*7c478bd9Sstevel@tonic-gate hash_addr(const void *key)
185*7c478bd9Sstevel@tonic-gate {
186*7c478bd9Sstevel@tonic-gate 	return (hash_buf(&key, sizeof (key)));
187*7c478bd9Sstevel@tonic-gate }
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate /*
191*7c478bd9Sstevel@tonic-gate  * public interface
192*7c478bd9Sstevel@tonic-gate  */
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate /*
195*7c478bd9Sstevel@tonic-gate  * Return a hash which is built by manipulating each byte in the
196*7c478bd9Sstevel@tonic-gate  * supplied data. The hash logic follows the approach suggested in the
197*7c478bd9Sstevel@tonic-gate  * FNV hash.
198*7c478bd9Sstevel@tonic-gate  */
199*7c478bd9Sstevel@tonic-gate uint64_t
hash_buf(const void * buf,size_t len)200*7c478bd9Sstevel@tonic-gate hash_buf(const void *buf, size_t len)
201*7c478bd9Sstevel@tonic-gate {
202*7c478bd9Sstevel@tonic-gate 	uchar_t *start = (uchar_t *)buf;
203*7c478bd9Sstevel@tonic-gate 	uchar_t *end = start + len;
204*7c478bd9Sstevel@tonic-gate 	uint64_t hash = HASH_64_INIT;
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	while (start < end) {
207*7c478bd9Sstevel@tonic-gate 		hash *= HASH_64_PRIME;
208*7c478bd9Sstevel@tonic-gate 		hash ^= (uint64_t)*start++;
209*7c478bd9Sstevel@tonic-gate 	}
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 	return (hash);
212*7c478bd9Sstevel@tonic-gate }
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate /*
216*7c478bd9Sstevel@tonic-gate  * Return a hash which is built by manipulating each byte in the
217*7c478bd9Sstevel@tonic-gate  * supplied string. The hash logic follows the approach suggested in
218*7c478bd9Sstevel@tonic-gate  * the FNV hash.
219*7c478bd9Sstevel@tonic-gate  */
220*7c478bd9Sstevel@tonic-gate uint64_t
hash_str(const char * str)221*7c478bd9Sstevel@tonic-gate hash_str(const char *str)
222*7c478bd9Sstevel@tonic-gate {
223*7c478bd9Sstevel@tonic-gate 	uchar_t *p = (uchar_t *)str;
224*7c478bd9Sstevel@tonic-gate 	uint64_t hash = HASH_64_INIT;
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 	while (*p) {
227*7c478bd9Sstevel@tonic-gate 		hash *= HASH_64_PRIME;
228*7c478bd9Sstevel@tonic-gate 		hash ^= (uint64_t)*p++;
229*7c478bd9Sstevel@tonic-gate 	}
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate 	return (hash);
232*7c478bd9Sstevel@tonic-gate }
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate /*
235*7c478bd9Sstevel@tonic-gate  * Return the number of keys held in the supplied dictionary.
236*7c478bd9Sstevel@tonic-gate  */
237*7c478bd9Sstevel@tonic-gate uint64_t
dict_length(dict_hdl_t * hdl)238*7c478bd9Sstevel@tonic-gate dict_length(dict_hdl_t *hdl)
239*7c478bd9Sstevel@tonic-gate {
240*7c478bd9Sstevel@tonic-gate 	return (hdl->dh_length);
241*7c478bd9Sstevel@tonic-gate }
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate /*
244*7c478bd9Sstevel@tonic-gate  * Free the supplied dictionary and all it's associated resource.
245*7c478bd9Sstevel@tonic-gate  */
246*7c478bd9Sstevel@tonic-gate void
dict_free(dict_hdl_t ** hdl)247*7c478bd9Sstevel@tonic-gate dict_free(dict_hdl_t **hdl)
248*7c478bd9Sstevel@tonic-gate {
249*7c478bd9Sstevel@tonic-gate 	if ((*hdl)->dh_length > 0) {
250*7c478bd9Sstevel@tonic-gate 		uint64_t i;
251*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < (*hdl)->dh_size; i++) {
252*7c478bd9Sstevel@tonic-gate 			dict_bucket_t *this, *next;
253*7c478bd9Sstevel@tonic-gate 			for (this = (*hdl)->dh_buckets[i]; this != NULL;
254*7c478bd9Sstevel@tonic-gate 			    this = next) {
255*7c478bd9Sstevel@tonic-gate 				next = this->db_next;
256*7c478bd9Sstevel@tonic-gate 				free(this);
257*7c478bd9Sstevel@tonic-gate 			}
258*7c478bd9Sstevel@tonic-gate 		}
259*7c478bd9Sstevel@tonic-gate 	}
260*7c478bd9Sstevel@tonic-gate 	free((*hdl)->dh_buckets);
261*7c478bd9Sstevel@tonic-gate 	free((*hdl));
262*7c478bd9Sstevel@tonic-gate 	*hdl = NULL;
263*7c478bd9Sstevel@tonic-gate }
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate /*
266*7c478bd9Sstevel@tonic-gate  * Create a new dictionary using the supplied comparison and hashing
267*7c478bd9Sstevel@tonic-gate  * functions. If none are supplied then the defaults are used.
268*7c478bd9Sstevel@tonic-gate  */
269*7c478bd9Sstevel@tonic-gate dict_hdl_t *
dict_new(int (* cmp)(const void *,const void *),uint64_t (* hash)(const void *))270*7c478bd9Sstevel@tonic-gate dict_new(int (*cmp)(const void *, const void *),
271*7c478bd9Sstevel@tonic-gate     uint64_t (*hash)(const void *))
272*7c478bd9Sstevel@tonic-gate {
273*7c478bd9Sstevel@tonic-gate 	dict_hdl_t *hdl;
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate 	if ((hdl = calloc(1, sizeof (dict_hdl_t))) == NULL)
276*7c478bd9Sstevel@tonic-gate 		return (NULL);
277*7c478bd9Sstevel@tonic-gate 	hdl->dh_size = DICT_SIZE;
278*7c478bd9Sstevel@tonic-gate 	if ((hdl->dh_buckets = calloc(hdl->dh_size, sizeof (dict_bucket_t *)))
279*7c478bd9Sstevel@tonic-gate 	    == NULL) {
280*7c478bd9Sstevel@tonic-gate 		free(hdl);
281*7c478bd9Sstevel@tonic-gate 		return (NULL);
282*7c478bd9Sstevel@tonic-gate 	}
283*7c478bd9Sstevel@tonic-gate 	hdl->dh_cmp = cmp ? cmp : cmp_addr;
284*7c478bd9Sstevel@tonic-gate 	hdl->dh_hash = hash ? hash : hash_addr;
285*7c478bd9Sstevel@tonic-gate 	return (hdl);
286*7c478bd9Sstevel@tonic-gate }
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate /*
289*7c478bd9Sstevel@tonic-gate  * Get a value from the hash. Null is returned if the key cannot be
290*7c478bd9Sstevel@tonic-gate  * found.
291*7c478bd9Sstevel@tonic-gate  */
292*7c478bd9Sstevel@tonic-gate void *
dict_get(dict_hdl_t * hdl,const void * key)293*7c478bd9Sstevel@tonic-gate dict_get(dict_hdl_t *hdl, const void *key)
294*7c478bd9Sstevel@tonic-gate {
295*7c478bd9Sstevel@tonic-gate 	uint64_t i;
296*7c478bd9Sstevel@tonic-gate 	dict_bucket_t *bucket;
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 	i = (*hdl->dh_hash)(key)%hdl->dh_size;
299*7c478bd9Sstevel@tonic-gate 	for (bucket = hdl->dh_buckets[i]; bucket != NULL;
300*7c478bd9Sstevel@tonic-gate 	    bucket = bucket->db_next)
301*7c478bd9Sstevel@tonic-gate 		if ((*hdl->dh_cmp)(key, bucket->db_key) == 0)
302*7c478bd9Sstevel@tonic-gate 			break;
303*7c478bd9Sstevel@tonic-gate 	return (bucket ? bucket->db_value : NULL);
304*7c478bd9Sstevel@tonic-gate }
305*7c478bd9Sstevel@tonic-gate 
306*7c478bd9Sstevel@tonic-gate /*
307*7c478bd9Sstevel@tonic-gate  * Put an entry into the hash. Null is returned if this key was not
308*7c478bd9Sstevel@tonic-gate  * already present, otherwise the previous value is returned.
309*7c478bd9Sstevel@tonic-gate  */
310*7c478bd9Sstevel@tonic-gate void *
dict_put(dict_hdl_t * hdl,const void * key,void * value)311*7c478bd9Sstevel@tonic-gate dict_put(dict_hdl_t *hdl, const void *key, void *value)
312*7c478bd9Sstevel@tonic-gate {
313*7c478bd9Sstevel@tonic-gate 	uint64_t i;
314*7c478bd9Sstevel@tonic-gate 	dict_bucket_t *bucket;
315*7c478bd9Sstevel@tonic-gate 	void *prev = NULL;
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate 	i = (*hdl->dh_hash)(key)%hdl->dh_size;
318*7c478bd9Sstevel@tonic-gate 	for (bucket = hdl->dh_buckets[i]; bucket != NULL;
319*7c478bd9Sstevel@tonic-gate 	    bucket = bucket->db_next)
320*7c478bd9Sstevel@tonic-gate 		if ((*hdl->dh_cmp)(key, bucket->db_key) == 0)
321*7c478bd9Sstevel@tonic-gate 			break;
322*7c478bd9Sstevel@tonic-gate 	if (bucket) {
323*7c478bd9Sstevel@tonic-gate 		prev = bucket->db_value;
324*7c478bd9Sstevel@tonic-gate 	} else {
325*7c478bd9Sstevel@tonic-gate 		bucket = malloc(sizeof (dict_bucket_t));
326*7c478bd9Sstevel@tonic-gate 		bucket->db_key = key;
327*7c478bd9Sstevel@tonic-gate 		bucket->db_next = hdl->dh_buckets[i];
328*7c478bd9Sstevel@tonic-gate 		hdl->dh_buckets[i] = bucket;
329*7c478bd9Sstevel@tonic-gate 		hdl->dh_length++;
330*7c478bd9Sstevel@tonic-gate 	}
331*7c478bd9Sstevel@tonic-gate 	hdl->dh_change++;
332*7c478bd9Sstevel@tonic-gate 	bucket->db_value = value;
333*7c478bd9Sstevel@tonic-gate 	return (prev);
334*7c478bd9Sstevel@tonic-gate }
335*7c478bd9Sstevel@tonic-gate 
336*7c478bd9Sstevel@tonic-gate /*
337*7c478bd9Sstevel@tonic-gate  * Remove the key/value from the dictionary. The value is returned if
338*7c478bd9Sstevel@tonic-gate  * the key is found. NULL is returned if the key cannot be located.
339*7c478bd9Sstevel@tonic-gate  */
340*7c478bd9Sstevel@tonic-gate void *
dict_remove(dict_hdl_t * hdl,const void * key)341*7c478bd9Sstevel@tonic-gate dict_remove(dict_hdl_t *hdl, const void *key)
342*7c478bd9Sstevel@tonic-gate {
343*7c478bd9Sstevel@tonic-gate 	uint64_t i;
344*7c478bd9Sstevel@tonic-gate 	dict_bucket_t	**pbucket;
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate 	hdl->dh_change++;
347*7c478bd9Sstevel@tonic-gate 	i = (*hdl->dh_hash)(key)%hdl->dh_size;
348*7c478bd9Sstevel@tonic-gate 
349*7c478bd9Sstevel@tonic-gate 	for (pbucket = &hdl->dh_buckets[i]; *pbucket != NULL;
350*7c478bd9Sstevel@tonic-gate 	    pbucket = &(*pbucket)->db_next) {
351*7c478bd9Sstevel@tonic-gate 		if ((*hdl->dh_cmp)(key, (*pbucket)->db_key) == 0) {
352*7c478bd9Sstevel@tonic-gate 			dict_bucket_t *bucket = *pbucket;
353*7c478bd9Sstevel@tonic-gate 			void *value = bucket->db_value;
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate 			*pbucket = bucket->db_next;
356*7c478bd9Sstevel@tonic-gate 			free(bucket);
357*7c478bd9Sstevel@tonic-gate 			hdl->dh_length--;
358*7c478bd9Sstevel@tonic-gate 			return (value);
359*7c478bd9Sstevel@tonic-gate 		}
360*7c478bd9Sstevel@tonic-gate 	}
361*7c478bd9Sstevel@tonic-gate 	return (NULL);
362*7c478bd9Sstevel@tonic-gate }
363*7c478bd9Sstevel@tonic-gate 
364*7c478bd9Sstevel@tonic-gate /*
365*7c478bd9Sstevel@tonic-gate  * For all entries in the dictionary call the user supplied function
366*7c478bd9Sstevel@tonic-gate  * (apply) with the key, value and user supplied data. If the
367*7c478bd9Sstevel@tonic-gate  * dictionary is modifed while this function is executing, then the
368*7c478bd9Sstevel@tonic-gate  * function will fail with an assertion about table modifcation.
369*7c478bd9Sstevel@tonic-gate  */
370*7c478bd9Sstevel@tonic-gate void
dict_map(dict_hdl_t * hdl,void (* apply)(const void *,void **,void *),void * cl)371*7c478bd9Sstevel@tonic-gate dict_map(dict_hdl_t *hdl, void (*apply)(const void *, void **, void *),
372*7c478bd9Sstevel@tonic-gate     void *cl)
373*7c478bd9Sstevel@tonic-gate {
374*7c478bd9Sstevel@tonic-gate 	uint64_t i;
375*7c478bd9Sstevel@tonic-gate 	dict_bucket_t *bucket = NULL;
376*7c478bd9Sstevel@tonic-gate 	uint64_t change_stamp = hdl->dh_change;
377*7c478bd9Sstevel@tonic-gate 
378*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < hdl->dh_size; i++) {
379*7c478bd9Sstevel@tonic-gate 		for (bucket = hdl->dh_buckets[i]; bucket != NULL;
380*7c478bd9Sstevel@tonic-gate 		    bucket = bucket->db_next) {
381*7c478bd9Sstevel@tonic-gate 			apply(bucket->db_key, &bucket->db_value, cl);
382*7c478bd9Sstevel@tonic-gate 			if (hdl->dh_change != change_stamp)
383*7c478bd9Sstevel@tonic-gate 				assert(!"table modified illegally");
384*7c478bd9Sstevel@tonic-gate 		}
385*7c478bd9Sstevel@tonic-gate 	}
386*7c478bd9Sstevel@tonic-gate }
387