xref: /titanic_52/usr/src/uts/common/sys/modhash.h (revision 24da5b34f49324ed742a340010ed5bd3d4e06625)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _SYS_MODHASH_H
28 #define	_SYS_MODHASH_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*
33  * Generic hash implementation for the kernel.
34  */
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 #ifdef _KERNEL
41 
42 #include <sys/types.h>
43 
44 /*
45  * Opaque data types for storing keys and values
46  */
47 typedef void *mod_hash_val_t;
48 typedef void *mod_hash_key_t;
49 
50 /*
51  * Opaque data type for reservation
52  */
53 typedef void *mod_hash_hndl_t;
54 
55 /*
56  * Opaque type for hash itself.
57  */
58 struct mod_hash;
59 typedef struct mod_hash mod_hash_t;
60 
61 /*
62  * String hash table
63  */
64 mod_hash_t *mod_hash_create_strhash(char *, size_t, void (*)(mod_hash_val_t));
65 void mod_hash_destroy_strhash(mod_hash_t *);
66 int mod_hash_strkey_cmp(mod_hash_key_t, mod_hash_key_t);
67 void mod_hash_strkey_dtor(mod_hash_key_t);
68 void mod_hash_strval_dtor(mod_hash_val_t);
69 uint_t mod_hash_bystr(void *, mod_hash_key_t);
70 
71 /*
72  * Pointer hash table
73  */
74 mod_hash_t *mod_hash_create_ptrhash(char *, size_t, void (*)(mod_hash_val_t),
75     size_t);
76 void mod_hash_destroy_ptrhash(mod_hash_t *);
77 int mod_hash_ptrkey_cmp(mod_hash_key_t, mod_hash_key_t);
78 uint_t mod_hash_byptr(void *, mod_hash_key_t);
79 
80 /*
81  * ID hash table
82  */
83 mod_hash_t *mod_hash_create_idhash(char *, size_t, void (*)(mod_hash_val_t));
84 void mod_hash_destroy_idhash(mod_hash_t *);
85 int mod_hash_idkey_cmp(mod_hash_key_t, mod_hash_key_t);
86 uint_t mod_hash_byid(void *, mod_hash_key_t);
87 uint_t mod_hash_iddata_gen(size_t);
88 
89 /*
90  * Hash management functions
91  */
92 mod_hash_t *mod_hash_create_extended(char *, size_t, void (*)(mod_hash_key_t),
93     void (*)(mod_hash_val_t), uint_t (*)(void *, mod_hash_key_t), void *,
94     int (*)(mod_hash_key_t, mod_hash_key_t), int);
95 
96 void mod_hash_destroy_hash(mod_hash_t *);
97 void mod_hash_clear(mod_hash_t *);
98 
99 /*
100  * Null key and value destructors
101  */
102 void mod_hash_null_keydtor(mod_hash_key_t);
103 void mod_hash_null_valdtor(mod_hash_val_t);
104 
105 /*
106  * Basic hash operations
107  */
108 
109 /*
110  * Error codes for insert, remove, find, destroy.
111  */
112 #define	MH_ERR_NOMEM -1
113 #define	MH_ERR_DUPLICATE -2
114 #define	MH_ERR_NOTFOUND -3
115 
116 /*
117  * Return codes for hash walkers
118  */
119 #define	MH_WALK_CONTINUE 0
120 #define	MH_WALK_TERMINATE 1
121 
122 /*
123  * Basic hash operations
124  */
125 int mod_hash_insert(mod_hash_t *, mod_hash_key_t, mod_hash_val_t);
126 int mod_hash_replace(mod_hash_t *, mod_hash_key_t, mod_hash_val_t);
127 int mod_hash_remove(mod_hash_t *, mod_hash_key_t, mod_hash_val_t *);
128 int mod_hash_destroy(mod_hash_t *, mod_hash_key_t);
129 int mod_hash_find(mod_hash_t *, mod_hash_key_t, mod_hash_val_t *);
130 int mod_hash_find_cb(mod_hash_t *, mod_hash_key_t, mod_hash_val_t *,
131     void (*)(mod_hash_key_t, mod_hash_val_t));
132 void mod_hash_walk(mod_hash_t *,
133     uint_t (*)(mod_hash_key_t, mod_hash_val_t *, void *), void *);
134 
135 /*
136  * Reserving hash operations
137  */
138 int mod_hash_reserve(mod_hash_t *, mod_hash_hndl_t *);
139 int mod_hash_reserve_nosleep(mod_hash_t *, mod_hash_hndl_t *);
140 void mod_hash_cancel(mod_hash_t *, mod_hash_hndl_t *);
141 int mod_hash_insert_reserve(mod_hash_t *, mod_hash_key_t, mod_hash_val_t,
142     mod_hash_hndl_t);
143 
144 #endif /* _KERNEL */
145 
146 #ifdef __cplusplus
147 }
148 #endif
149 
150 #endif /* _SYS_MODHASH_H */
151