xref: /titanic_50/usr/src/uts/common/sys/modhash_impl.h (revision 3c112a2b34403220c06c3e2fcac403358cfba168)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 /*
32  * Internal details for the kernel's generic hash implementation.
33  */
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 #ifdef _KERNEL
40 
41 #include <sys/ksynch.h>
42 #include <sys/modhash.h>
43 
44 struct mod_hash_entry {
45 	mod_hash_key_t mhe_key;			/* stored hash key	*/
46 	mod_hash_val_t mhe_val;			/* stored hash value	*/
47 	struct mod_hash_entry *mhe_next;	/* next item in chain	*/
48 };
49 
50 struct mod_hash_stat {
51 	ulong_t mhs_hit;	/* tried a 'find' and it succeeded */
52 	ulong_t mhs_miss;	/* tried a 'find' but it failed */
53 	ulong_t mhs_coll;	/* occur when insert fails because of dup's */
54 	ulong_t mhs_nelems;	/* total number of stored key/value pairs */
55 	ulong_t mhs_nomem;	/* number of times kmem_alloc failed */
56 };
57 
58 struct mod_hash {
59 	krwlock_t	mh_contents;	/* lock protecting contents */
60 	char		*mh_name;	/* hash name */
61 	int		mh_sleep;	/* kmem_alloc flag */
62 	size_t		mh_nchains;	/* # of elements in mh_entries */
63 
64 	/* key and val destructor */
65 	void    (*mh_kdtor)(mod_hash_key_t);
66 	void    (*mh_vdtor)(mod_hash_val_t);
67 
68 	/* key comparator */
69 	int	(*mh_keycmp)(mod_hash_key_t, mod_hash_key_t);
70 
71 	/* hash algorithm, and algorithm-private data */
72 	uint_t  (*mh_hashalg)(void *, mod_hash_key_t);
73 	void    *mh_hashalg_data;
74 
75 	struct mod_hash	*mh_next;	/* next hash in list */
76 
77 	struct mod_hash_stat mh_stat;
78 
79 	struct mod_hash_entry *mh_entries[1];
80 };
81 
82 /*
83  * MH_SIZE()
84  * 	Compute the size of a mod_hash_t, in bytes, given the number of
85  * 	elements it contains.
86  */
87 #define	MH_SIZE(n) \
88 	(sizeof (mod_hash_t) + ((n) - 1) * (sizeof (struct mod_hash_entry *)))
89 
90 /*
91  * Module initialization; called once.
92  */
93 void mod_hash_init(void);
94 
95 /*
96  * Internal routines.  Use directly with care.
97  */
98 uint_t i_mod_hash(mod_hash_t *, mod_hash_key_t);
99 int i_mod_hash_insert_nosync(mod_hash_t *, mod_hash_key_t, mod_hash_val_t,
100     mod_hash_hndl_t);
101 int i_mod_hash_remove_nosync(mod_hash_t *, mod_hash_key_t, mod_hash_val_t *);
102 int i_mod_hash_find_nosync(mod_hash_t *, mod_hash_key_t, mod_hash_val_t *);
103 void i_mod_hash_walk_nosync(mod_hash_t *, uint_t (*)(mod_hash_key_t,
104     mod_hash_val_t *, void *), void *);
105 void i_mod_hash_clear_nosync(mod_hash_t *hash);
106 
107 #endif /* _KERNEL */
108 
109 #ifdef __cplusplus
110 }
111 #endif
112 
113 #endif /* _SYS_MODHASH_IMPL_H */
114