xref: /illumos-gate/usr/src/uts/common/sys/modhash_impl.h (revision dd72704bd9e794056c558153663c739e2012d721)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _SYS_MODHASH_IMPL_H
27 #define	_SYS_MODHASH_IMPL_H
28 
29 /*
30  * Internal details for the kernel's generic hash implementation.
31  */
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 #ifdef _KERNEL
38 
39 #include <sys/ksynch.h>
40 #include <sys/modhash.h>
41 
42 struct mod_hash_entry {
43 	mod_hash_key_t mhe_key;			/* stored hash key	*/
44 	mod_hash_val_t mhe_val;			/* stored hash value	*/
45 	struct mod_hash_entry *mhe_next;	/* next item in chain	*/
46 };
47 
48 struct mod_hash_stat {
49 	ulong_t mhs_hit;	/* tried a 'find' and it succeeded */
50 	ulong_t mhs_miss;	/* tried a 'find' but it failed */
51 	ulong_t mhs_coll;	/* occur when insert fails because of dup's */
52 	ulong_t mhs_nelems;	/* total number of stored key/value pairs */
53 	ulong_t mhs_nomem;	/* number of times kmem_alloc failed */
54 };
55 
56 struct mod_hash {
57 	krwlock_t	mh_contents;	/* lock protecting contents */
58 	char		*mh_name;	/* hash name */
59 	int		mh_sleep;	/* kmem_alloc flag */
60 	size_t		mh_nchains;	/* # of elements in mh_entries */
61 
62 	/* key and val destructor */
63 	void    (*mh_kdtor)(mod_hash_key_t);
64 	void    (*mh_vdtor)(mod_hash_val_t);
65 
66 	/* key comparator */
67 	int	(*mh_keycmp)(mod_hash_key_t, mod_hash_key_t);
68 
69 	/* hash algorithm, and algorithm-private data */
70 	uint_t  (*mh_hashalg)(void *, mod_hash_key_t);
71 	void    *mh_hashalg_data;
72 
73 	struct mod_hash	*mh_next;	/* next hash in list */
74 
75 	struct mod_hash_stat mh_stat;
76 
77 	struct mod_hash_entry *mh_entries[1];
78 };
79 
80 /*
81  * MH_SIZE()
82  * 	Compute the size of a mod_hash_t, in bytes, given the number of
83  * 	elements it contains.
84  */
85 #define	MH_SIZE(n) \
86 	(sizeof (mod_hash_t) + ((n) - 1) * (sizeof (struct mod_hash_entry *)))
87 
88 /*
89  * Module initialization; called once.
90  */
91 void mod_hash_init(void);
92 
93 /*
94  * Internal routines.  Use directly with care.
95  */
96 uint_t i_mod_hash(mod_hash_t *, mod_hash_key_t);
97 int i_mod_hash_insert_nosync(mod_hash_t *, mod_hash_key_t, mod_hash_val_t,
98     mod_hash_hndl_t);
99 int i_mod_hash_remove_nosync(mod_hash_t *, mod_hash_key_t, mod_hash_val_t *);
100 int i_mod_hash_find_nosync(mod_hash_t *, mod_hash_key_t, mod_hash_val_t *);
101 void i_mod_hash_walk_nosync(mod_hash_t *, uint_t (*)(mod_hash_key_t,
102     mod_hash_val_t *, void *), void *);
103 void i_mod_hash_clear_nosync(mod_hash_t *hash);
104 
105 #endif /* _KERNEL */
106 
107 #ifdef __cplusplus
108 }
109 #endif
110 
111 #endif /* _SYS_MODHASH_IMPL_H */
112