1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate /* 30*0Sstevel@tonic-gate * mod_hash: flexible hash table implementation. 31*0Sstevel@tonic-gate * 32*0Sstevel@tonic-gate * This is a reasonably fast, reasonably flexible hash table implementation 33*0Sstevel@tonic-gate * which features pluggable hash algorithms to support storing arbitrary keys 34*0Sstevel@tonic-gate * and values. It is designed to handle small (< 100,000 items) amounts of 35*0Sstevel@tonic-gate * data. The hash uses chaining to resolve collisions, and does not feature a 36*0Sstevel@tonic-gate * mechanism to grow the hash. Care must be taken to pick nchains to be large 37*0Sstevel@tonic-gate * enough for the application at hand, or lots of time will be wasted searching 38*0Sstevel@tonic-gate * hash chains. 39*0Sstevel@tonic-gate * 40*0Sstevel@tonic-gate * The client of the hash is required to supply a number of items to support 41*0Sstevel@tonic-gate * the various hash functions: 42*0Sstevel@tonic-gate * 43*0Sstevel@tonic-gate * - Destructor functions for the key and value being hashed. 44*0Sstevel@tonic-gate * A destructor is responsible for freeing an object when the hash 45*0Sstevel@tonic-gate * table is no longer storing it. Since keys and values can be of 46*0Sstevel@tonic-gate * arbitrary type, separate destructors for keys & values are used. 47*0Sstevel@tonic-gate * These may be mod_hash_null_keydtor and mod_hash_null_valdtor if no 48*0Sstevel@tonic-gate * destructor is needed for either a key or value. 49*0Sstevel@tonic-gate * 50*0Sstevel@tonic-gate * - A hashing algorithm which returns a uint_t representing a hash index 51*0Sstevel@tonic-gate * The number returned need _not_ be between 0 and nchains. The mod_hash 52*0Sstevel@tonic-gate * code will take care of doing that. The second argument (after the 53*0Sstevel@tonic-gate * key) to the hashing function is a void * that represents 54*0Sstevel@tonic-gate * hash_alg_data-- this is provided so that the hashing algrorithm can 55*0Sstevel@tonic-gate * maintain some state across calls, or keep algorithm-specific 56*0Sstevel@tonic-gate * constants associated with the hash table. 57*0Sstevel@tonic-gate * 58*0Sstevel@tonic-gate * A pointer-hashing and a string-hashing algorithm are supplied in 59*0Sstevel@tonic-gate * this file. 60*0Sstevel@tonic-gate * 61*0Sstevel@tonic-gate * - A key comparator (a la qsort). 62*0Sstevel@tonic-gate * This is used when searching the hash chain. The key comparator 63*0Sstevel@tonic-gate * determines if two keys match. It should follow the return value 64*0Sstevel@tonic-gate * semantics of strcmp. 65*0Sstevel@tonic-gate * 66*0Sstevel@tonic-gate * string and pointer comparators are supplied in this file. 67*0Sstevel@tonic-gate * 68*0Sstevel@tonic-gate * mod_hash_create_strhash() and mod_hash_create_ptrhash() provide good 69*0Sstevel@tonic-gate * examples of how to create a customized hash table. 70*0Sstevel@tonic-gate * 71*0Sstevel@tonic-gate * Basic hash operations: 72*0Sstevel@tonic-gate * 73*0Sstevel@tonic-gate * mod_hash_create_strhash(name, nchains, dtor), 74*0Sstevel@tonic-gate * create a hash using strings as keys. 75*0Sstevel@tonic-gate * NOTE: This create a hash which automatically cleans up the string 76*0Sstevel@tonic-gate * values it is given for keys. 77*0Sstevel@tonic-gate * 78*0Sstevel@tonic-gate * mod_hash_create_ptrhash(name, nchains, dtor, key_elem_size): 79*0Sstevel@tonic-gate * create a hash using pointers as keys. 80*0Sstevel@tonic-gate * 81*0Sstevel@tonic-gate * mod_hash_create_extended(name, nchains, kdtor, vdtor, 82*0Sstevel@tonic-gate * hash_alg, hash_alg_data, 83*0Sstevel@tonic-gate * keycmp, sleep) 84*0Sstevel@tonic-gate * create a customized hash table. 85*0Sstevel@tonic-gate * 86*0Sstevel@tonic-gate * mod_hash_destroy_hash(hash): 87*0Sstevel@tonic-gate * destroy the given hash table, calling the key and value destructors 88*0Sstevel@tonic-gate * on each key-value pair stored in the hash. 89*0Sstevel@tonic-gate * 90*0Sstevel@tonic-gate * mod_hash_insert(hash, key, val): 91*0Sstevel@tonic-gate * place a key, value pair into the given hash. 92*0Sstevel@tonic-gate * duplicate keys are rejected. 93*0Sstevel@tonic-gate * 94*0Sstevel@tonic-gate * mod_hash_insert_reserve(hash, key, val, handle): 95*0Sstevel@tonic-gate * place a key, value pair into the given hash, using handle to indicate 96*0Sstevel@tonic-gate * the reserved storage for the pair. (no memory allocation is needed 97*0Sstevel@tonic-gate * during a mod_hash_insert_reserve.) duplicate keys are rejected. 98*0Sstevel@tonic-gate * 99*0Sstevel@tonic-gate * mod_hash_reserve(hash, *handle): 100*0Sstevel@tonic-gate * reserve storage for a key-value pair using the memory allocation 101*0Sstevel@tonic-gate * policy of 'hash', returning the storage handle in 'handle'. 102*0Sstevel@tonic-gate * 103*0Sstevel@tonic-gate * mod_hash_reserve_nosleep(hash, *handle): reserve storage for a key-value 104*0Sstevel@tonic-gate * pair ignoring the memory allocation policy of 'hash' and always without 105*0Sstevel@tonic-gate * sleep, returning the storage handle in 'handle'. 106*0Sstevel@tonic-gate * 107*0Sstevel@tonic-gate * mod_hash_remove(hash, key, *val): 108*0Sstevel@tonic-gate * remove a key-value pair with key 'key' from 'hash', destroying the 109*0Sstevel@tonic-gate * stored key, and returning the value in val. 110*0Sstevel@tonic-gate * 111*0Sstevel@tonic-gate * mod_hash_replace(hash, key, val) 112*0Sstevel@tonic-gate * atomically remove an existing key-value pair from a hash, and replace 113*0Sstevel@tonic-gate * the key and value with the ones supplied. The removed key and value 114*0Sstevel@tonic-gate * (if any) are destroyed. 115*0Sstevel@tonic-gate * 116*0Sstevel@tonic-gate * mod_hash_destroy(hash, key): 117*0Sstevel@tonic-gate * remove a key-value pair with key 'key' from 'hash', destroying both 118*0Sstevel@tonic-gate * stored key and stored value. 119*0Sstevel@tonic-gate * 120*0Sstevel@tonic-gate * mod_hash_find(hash, key, val): 121*0Sstevel@tonic-gate * find a value in the hash table corresponding to the given key. 122*0Sstevel@tonic-gate * 123*0Sstevel@tonic-gate * mod_hash_find_cb(hash, key, val, found_callback) 124*0Sstevel@tonic-gate * find a value in the hash table corresponding to the given key. 125*0Sstevel@tonic-gate * If a value is found, call specified callback passing key and val to it. 126*0Sstevel@tonic-gate * The callback is called with the hash lock held. 127*0Sstevel@tonic-gate * It is intended to be used in situations where the act of locating the 128*0Sstevel@tonic-gate * data must also modify it - such as in reference counting schemes. 129*0Sstevel@tonic-gate * 130*0Sstevel@tonic-gate * mod_hash_walk(hash, callback(key, elem, arg), arg) 131*0Sstevel@tonic-gate * walks all the elements in the hashtable and invokes the callback 132*0Sstevel@tonic-gate * function with the key/value pair for each element. the hashtable 133*0Sstevel@tonic-gate * is locked for readers so the callback function should not attempt 134*0Sstevel@tonic-gate * to do any updates to the hashable. the callback function should 135*0Sstevel@tonic-gate * return MH_WALK_CONTINUE to continue walking the hashtable or 136*0Sstevel@tonic-gate * MH_WALK_TERMINATE to abort the walk of the hashtable. 137*0Sstevel@tonic-gate * 138*0Sstevel@tonic-gate * mod_hash_clear(hash): 139*0Sstevel@tonic-gate * clears the given hash table of entries, calling the key and value 140*0Sstevel@tonic-gate * destructors for every element in the hash. 141*0Sstevel@tonic-gate */ 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gate #include <sys/bitmap.h> 144*0Sstevel@tonic-gate #include <sys/debug.h> 145*0Sstevel@tonic-gate #include <sys/kmem.h> 146*0Sstevel@tonic-gate #include <sys/sunddi.h> 147*0Sstevel@tonic-gate 148*0Sstevel@tonic-gate #include <sys/modhash_impl.h> 149*0Sstevel@tonic-gate 150*0Sstevel@tonic-gate /* 151*0Sstevel@tonic-gate * MH_KEY_DESTROY() 152*0Sstevel@tonic-gate * Invoke the key destructor. 153*0Sstevel@tonic-gate */ 154*0Sstevel@tonic-gate #define MH_KEY_DESTROY(hash, key) ((hash->mh_kdtor)(key)) 155*0Sstevel@tonic-gate 156*0Sstevel@tonic-gate /* 157*0Sstevel@tonic-gate * MH_VAL_DESTROY() 158*0Sstevel@tonic-gate * Invoke the value destructor. 159*0Sstevel@tonic-gate */ 160*0Sstevel@tonic-gate #define MH_VAL_DESTROY(hash, val) ((hash->mh_vdtor)(val)) 161*0Sstevel@tonic-gate 162*0Sstevel@tonic-gate /* 163*0Sstevel@tonic-gate * MH_KEYCMP() 164*0Sstevel@tonic-gate * Call the key comparator for the given hash keys. 165*0Sstevel@tonic-gate */ 166*0Sstevel@tonic-gate #define MH_KEYCMP(hash, key1, key2) ((hash->mh_keycmp)(key1, key2)) 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate static void i_mod_hash_clear_nosync(mod_hash_t *); 169*0Sstevel@tonic-gate static int i_mod_hash_find_nosync(mod_hash_t *, mod_hash_key_t, 170*0Sstevel@tonic-gate mod_hash_val_t *); 171*0Sstevel@tonic-gate static int i_mod_hash_insert_nosync(mod_hash_t *, mod_hash_key_t, 172*0Sstevel@tonic-gate mod_hash_val_t, mod_hash_hndl_t); 173*0Sstevel@tonic-gate static int i_mod_hash_remove_nosync(mod_hash_t *, mod_hash_key_t, 174*0Sstevel@tonic-gate mod_hash_val_t *); 175*0Sstevel@tonic-gate static uint_t i_mod_hash(mod_hash_t *, mod_hash_key_t); 176*0Sstevel@tonic-gate 177*0Sstevel@tonic-gate /* 178*0Sstevel@tonic-gate * Cache for struct mod_hash_entry 179*0Sstevel@tonic-gate */ 180*0Sstevel@tonic-gate kmem_cache_t *mh_e_cache = NULL; 181*0Sstevel@tonic-gate mod_hash_t *mh_head = NULL; 182*0Sstevel@tonic-gate kmutex_t mh_head_lock; 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gate /* 185*0Sstevel@tonic-gate * mod_hash_null_keydtor() 186*0Sstevel@tonic-gate * mod_hash_null_valdtor() 187*0Sstevel@tonic-gate * no-op key and value destructors. 188*0Sstevel@tonic-gate */ 189*0Sstevel@tonic-gate /*ARGSUSED*/ 190*0Sstevel@tonic-gate void 191*0Sstevel@tonic-gate mod_hash_null_keydtor(mod_hash_key_t key) 192*0Sstevel@tonic-gate { 193*0Sstevel@tonic-gate } 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gate /*ARGSUSED*/ 196*0Sstevel@tonic-gate void 197*0Sstevel@tonic-gate mod_hash_null_valdtor(mod_hash_val_t val) 198*0Sstevel@tonic-gate { 199*0Sstevel@tonic-gate } 200*0Sstevel@tonic-gate 201*0Sstevel@tonic-gate /* 202*0Sstevel@tonic-gate * mod_hash_bystr() 203*0Sstevel@tonic-gate * mod_hash_strkey_cmp() 204*0Sstevel@tonic-gate * mod_hash_strkey_dtor() 205*0Sstevel@tonic-gate * mod_hash_strval_dtor() 206*0Sstevel@tonic-gate * Hash and key comparison routines for hashes with string keys. 207*0Sstevel@tonic-gate * 208*0Sstevel@tonic-gate * mod_hash_create_strhash() 209*0Sstevel@tonic-gate * Create a hash using strings as keys 210*0Sstevel@tonic-gate * 211*0Sstevel@tonic-gate * The string hashing algorithm is from the "Dragon Book" -- 212*0Sstevel@tonic-gate * "Compilers: Principles, Tools & Techniques", by Aho, Sethi, Ullman 213*0Sstevel@tonic-gate */ 214*0Sstevel@tonic-gate 215*0Sstevel@tonic-gate /*ARGSUSED*/ 216*0Sstevel@tonic-gate uint_t 217*0Sstevel@tonic-gate mod_hash_bystr(void *hash_data, mod_hash_key_t key) 218*0Sstevel@tonic-gate { 219*0Sstevel@tonic-gate uint_t hash = 0; 220*0Sstevel@tonic-gate uint_t g; 221*0Sstevel@tonic-gate char *p, *k = (char *)key; 222*0Sstevel@tonic-gate 223*0Sstevel@tonic-gate ASSERT(k); 224*0Sstevel@tonic-gate for (p = k; *p != '\0'; p++) { 225*0Sstevel@tonic-gate hash = (hash << 4) + *p; 226*0Sstevel@tonic-gate if ((g = (hash & 0xf0000000)) != 0) { 227*0Sstevel@tonic-gate hash ^= (g >> 24); 228*0Sstevel@tonic-gate hash ^= g; 229*0Sstevel@tonic-gate } 230*0Sstevel@tonic-gate } 231*0Sstevel@tonic-gate return (hash); 232*0Sstevel@tonic-gate } 233*0Sstevel@tonic-gate 234*0Sstevel@tonic-gate int 235*0Sstevel@tonic-gate mod_hash_strkey_cmp(mod_hash_key_t key1, mod_hash_key_t key2) 236*0Sstevel@tonic-gate { 237*0Sstevel@tonic-gate return (strcmp((char *)key1, (char *)key2)); 238*0Sstevel@tonic-gate } 239*0Sstevel@tonic-gate 240*0Sstevel@tonic-gate void 241*0Sstevel@tonic-gate mod_hash_strkey_dtor(mod_hash_key_t key) 242*0Sstevel@tonic-gate { 243*0Sstevel@tonic-gate char *c = (char *)key; 244*0Sstevel@tonic-gate kmem_free(c, strlen(c) + 1); 245*0Sstevel@tonic-gate } 246*0Sstevel@tonic-gate 247*0Sstevel@tonic-gate void 248*0Sstevel@tonic-gate mod_hash_strval_dtor(mod_hash_val_t val) 249*0Sstevel@tonic-gate { 250*0Sstevel@tonic-gate char *c = (char *)val; 251*0Sstevel@tonic-gate kmem_free(c, strlen(c) + 1); 252*0Sstevel@tonic-gate } 253*0Sstevel@tonic-gate 254*0Sstevel@tonic-gate mod_hash_t * 255*0Sstevel@tonic-gate mod_hash_create_strhash(char *name, size_t nchains, 256*0Sstevel@tonic-gate void (*val_dtor)(mod_hash_val_t)) 257*0Sstevel@tonic-gate { 258*0Sstevel@tonic-gate return mod_hash_create_extended(name, nchains, mod_hash_strkey_dtor, 259*0Sstevel@tonic-gate val_dtor, mod_hash_bystr, NULL, mod_hash_strkey_cmp, KM_SLEEP); 260*0Sstevel@tonic-gate } 261*0Sstevel@tonic-gate 262*0Sstevel@tonic-gate void 263*0Sstevel@tonic-gate mod_hash_destroy_strhash(mod_hash_t *strhash) 264*0Sstevel@tonic-gate { 265*0Sstevel@tonic-gate ASSERT(strhash); 266*0Sstevel@tonic-gate mod_hash_destroy_hash(strhash); 267*0Sstevel@tonic-gate } 268*0Sstevel@tonic-gate 269*0Sstevel@tonic-gate 270*0Sstevel@tonic-gate /* 271*0Sstevel@tonic-gate * mod_hash_byptr() 272*0Sstevel@tonic-gate * mod_hash_ptrkey_cmp() 273*0Sstevel@tonic-gate * Hash and key comparison routines for hashes with pointer keys. 274*0Sstevel@tonic-gate * 275*0Sstevel@tonic-gate * mod_hash_create_ptrhash() 276*0Sstevel@tonic-gate * mod_hash_destroy_ptrhash() 277*0Sstevel@tonic-gate * Create a hash that uses pointers as keys. This hash algorithm 278*0Sstevel@tonic-gate * picks an appropriate set of middle bits in the address to hash on 279*0Sstevel@tonic-gate * based on the size of the hash table and a hint about the size of 280*0Sstevel@tonic-gate * the items pointed at. 281*0Sstevel@tonic-gate */ 282*0Sstevel@tonic-gate uint_t 283*0Sstevel@tonic-gate mod_hash_byptr(void *hash_data, mod_hash_key_t key) 284*0Sstevel@tonic-gate { 285*0Sstevel@tonic-gate uintptr_t k = (uintptr_t)key; 286*0Sstevel@tonic-gate k >>= (int)(uintptr_t)hash_data; 287*0Sstevel@tonic-gate 288*0Sstevel@tonic-gate return ((uint_t)k); 289*0Sstevel@tonic-gate } 290*0Sstevel@tonic-gate 291*0Sstevel@tonic-gate int 292*0Sstevel@tonic-gate mod_hash_ptrkey_cmp(mod_hash_key_t key1, mod_hash_key_t key2) 293*0Sstevel@tonic-gate { 294*0Sstevel@tonic-gate uintptr_t k1 = (uintptr_t)key1; 295*0Sstevel@tonic-gate uintptr_t k2 = (uintptr_t)key2; 296*0Sstevel@tonic-gate if (k1 > k2) 297*0Sstevel@tonic-gate return (-1); 298*0Sstevel@tonic-gate else if (k1 < k2) 299*0Sstevel@tonic-gate return (1); 300*0Sstevel@tonic-gate else 301*0Sstevel@tonic-gate return (0); 302*0Sstevel@tonic-gate } 303*0Sstevel@tonic-gate 304*0Sstevel@tonic-gate mod_hash_t * 305*0Sstevel@tonic-gate mod_hash_create_ptrhash(char *name, size_t nchains, 306*0Sstevel@tonic-gate void (*val_dtor)(mod_hash_val_t), size_t key_elem_size) 307*0Sstevel@tonic-gate { 308*0Sstevel@tonic-gate size_t rshift; 309*0Sstevel@tonic-gate 310*0Sstevel@tonic-gate /* 311*0Sstevel@tonic-gate * We want to hash on the bits in the middle of the address word 312*0Sstevel@tonic-gate * Bits far to the right in the word have little significance, and 313*0Sstevel@tonic-gate * are likely to all look the same (for example, an array of 314*0Sstevel@tonic-gate * 256-byte structures will have the bottom 8 bits of address 315*0Sstevel@tonic-gate * words the same). So we want to right-shift each address to 316*0Sstevel@tonic-gate * ignore the bottom bits. 317*0Sstevel@tonic-gate * 318*0Sstevel@tonic-gate * The high bits, which are also unused, will get taken out when 319*0Sstevel@tonic-gate * mod_hash takes hashkey % nchains. 320*0Sstevel@tonic-gate */ 321*0Sstevel@tonic-gate rshift = highbit(key_elem_size); 322*0Sstevel@tonic-gate 323*0Sstevel@tonic-gate return mod_hash_create_extended(name, nchains, mod_hash_null_keydtor, 324*0Sstevel@tonic-gate val_dtor, mod_hash_byptr, (void *)rshift, mod_hash_ptrkey_cmp, 325*0Sstevel@tonic-gate KM_SLEEP); 326*0Sstevel@tonic-gate } 327*0Sstevel@tonic-gate 328*0Sstevel@tonic-gate void 329*0Sstevel@tonic-gate mod_hash_destroy_ptrhash(mod_hash_t *hash) 330*0Sstevel@tonic-gate { 331*0Sstevel@tonic-gate ASSERT(hash); 332*0Sstevel@tonic-gate mod_hash_destroy_hash(hash); 333*0Sstevel@tonic-gate } 334*0Sstevel@tonic-gate 335*0Sstevel@tonic-gate /* 336*0Sstevel@tonic-gate * mod_hash_byid() 337*0Sstevel@tonic-gate * mod_hash_idkey_cmp() 338*0Sstevel@tonic-gate * Hash and key comparison routines for hashes with 32-bit unsigned keys. 339*0Sstevel@tonic-gate * 340*0Sstevel@tonic-gate * mod_hash_create_idhash() 341*0Sstevel@tonic-gate * mod_hash_destroy_idhash() 342*0Sstevel@tonic-gate * mod_hash_iddata_gen() 343*0Sstevel@tonic-gate * Create a hash that uses numeric keys. 344*0Sstevel@tonic-gate * 345*0Sstevel@tonic-gate * The hash algorithm is documented in "Introduction to Algorithms" 346*0Sstevel@tonic-gate * (Cormen, Leiserson, Rivest); when the hash table is created, it 347*0Sstevel@tonic-gate * attempts to find the next largest prime above the number of hash 348*0Sstevel@tonic-gate * slots. The hash index is then this number times the key modulo 349*0Sstevel@tonic-gate * the hash size, or (key * prime) % nchains. 350*0Sstevel@tonic-gate */ 351*0Sstevel@tonic-gate uint_t 352*0Sstevel@tonic-gate mod_hash_byid(void *hash_data, mod_hash_key_t key) 353*0Sstevel@tonic-gate { 354*0Sstevel@tonic-gate uint_t kval = (uint_t)(uintptr_t)hash_data; 355*0Sstevel@tonic-gate return ((uint_t)(uintptr_t)key * (uint_t)kval); 356*0Sstevel@tonic-gate } 357*0Sstevel@tonic-gate 358*0Sstevel@tonic-gate int 359*0Sstevel@tonic-gate mod_hash_idkey_cmp(mod_hash_key_t key1, mod_hash_key_t key2) 360*0Sstevel@tonic-gate { 361*0Sstevel@tonic-gate return ((uint_t)(uintptr_t)key1 - (uint_t)(uintptr_t)key2); 362*0Sstevel@tonic-gate } 363*0Sstevel@tonic-gate 364*0Sstevel@tonic-gate /* 365*0Sstevel@tonic-gate * Generate the next largest prime number greater than nchains; this value 366*0Sstevel@tonic-gate * is intended to be later passed in to mod_hash_create_extended() as the 367*0Sstevel@tonic-gate * hash_data. 368*0Sstevel@tonic-gate */ 369*0Sstevel@tonic-gate uint_t 370*0Sstevel@tonic-gate mod_hash_iddata_gen(size_t nchains) 371*0Sstevel@tonic-gate { 372*0Sstevel@tonic-gate uint_t kval, i, prime; 373*0Sstevel@tonic-gate 374*0Sstevel@tonic-gate /* 375*0Sstevel@tonic-gate * Pick the first (odd) prime greater than nchains. Make sure kval is 376*0Sstevel@tonic-gate * odd (so start with nchains +1 or +2 as appropriate). 377*0Sstevel@tonic-gate */ 378*0Sstevel@tonic-gate kval = (nchains % 2 == 0) ? nchains + 1 : nchains + 2; 379*0Sstevel@tonic-gate 380*0Sstevel@tonic-gate for (;;) { 381*0Sstevel@tonic-gate prime = 1; 382*0Sstevel@tonic-gate for (i = 3; i * i <= kval; i += 2) { 383*0Sstevel@tonic-gate if (kval % i == 0) 384*0Sstevel@tonic-gate prime = 0; 385*0Sstevel@tonic-gate } 386*0Sstevel@tonic-gate if (prime == 1) 387*0Sstevel@tonic-gate break; 388*0Sstevel@tonic-gate kval += 2; 389*0Sstevel@tonic-gate } 390*0Sstevel@tonic-gate return (kval); 391*0Sstevel@tonic-gate } 392*0Sstevel@tonic-gate 393*0Sstevel@tonic-gate mod_hash_t * 394*0Sstevel@tonic-gate mod_hash_create_idhash(char *name, size_t nchains, 395*0Sstevel@tonic-gate void (*val_dtor)(mod_hash_val_t)) 396*0Sstevel@tonic-gate { 397*0Sstevel@tonic-gate uint_t kval = mod_hash_iddata_gen(nchains); 398*0Sstevel@tonic-gate 399*0Sstevel@tonic-gate return (mod_hash_create_extended(name, nchains, mod_hash_null_keydtor, 400*0Sstevel@tonic-gate val_dtor, mod_hash_byid, (void *)(uintptr_t)kval, 401*0Sstevel@tonic-gate mod_hash_idkey_cmp, KM_SLEEP)); 402*0Sstevel@tonic-gate } 403*0Sstevel@tonic-gate 404*0Sstevel@tonic-gate void 405*0Sstevel@tonic-gate mod_hash_destroy_idhash(mod_hash_t *hash) 406*0Sstevel@tonic-gate { 407*0Sstevel@tonic-gate ASSERT(hash); 408*0Sstevel@tonic-gate mod_hash_destroy_hash(hash); 409*0Sstevel@tonic-gate } 410*0Sstevel@tonic-gate 411*0Sstevel@tonic-gate /* 412*0Sstevel@tonic-gate * mod_hash_init() 413*0Sstevel@tonic-gate * sets up globals, etc for mod_hash_* 414*0Sstevel@tonic-gate */ 415*0Sstevel@tonic-gate void 416*0Sstevel@tonic-gate mod_hash_init(void) 417*0Sstevel@tonic-gate { 418*0Sstevel@tonic-gate ASSERT(mh_e_cache == NULL); 419*0Sstevel@tonic-gate mh_e_cache = kmem_cache_create("mod_hash_entries", 420*0Sstevel@tonic-gate sizeof (struct mod_hash_entry), 0, NULL, NULL, NULL, NULL, 421*0Sstevel@tonic-gate NULL, 0); 422*0Sstevel@tonic-gate } 423*0Sstevel@tonic-gate 424*0Sstevel@tonic-gate /* 425*0Sstevel@tonic-gate * mod_hash_create_extended() 426*0Sstevel@tonic-gate * The full-blown hash creation function. 427*0Sstevel@tonic-gate * 428*0Sstevel@tonic-gate * notes: 429*0Sstevel@tonic-gate * nchains - how many hash slots to create. More hash slots will 430*0Sstevel@tonic-gate * result in shorter hash chains, but will consume 431*0Sstevel@tonic-gate * slightly more memory up front. 432*0Sstevel@tonic-gate * sleep - should be KM_SLEEP or KM_NOSLEEP, to indicate whether 433*0Sstevel@tonic-gate * to sleep for memory, or fail in low-memory conditions. 434*0Sstevel@tonic-gate * 435*0Sstevel@tonic-gate * Fails only if KM_NOSLEEP was specified, and no memory was available. 436*0Sstevel@tonic-gate */ 437*0Sstevel@tonic-gate mod_hash_t * 438*0Sstevel@tonic-gate mod_hash_create_extended( 439*0Sstevel@tonic-gate char *hname, /* descriptive name for hash */ 440*0Sstevel@tonic-gate size_t nchains, /* number of hash slots */ 441*0Sstevel@tonic-gate void (*kdtor)(mod_hash_key_t), /* key destructor */ 442*0Sstevel@tonic-gate void (*vdtor)(mod_hash_val_t), /* value destructor */ 443*0Sstevel@tonic-gate uint_t (*hash_alg)(void *, mod_hash_key_t), /* hash algorithm */ 444*0Sstevel@tonic-gate void *hash_alg_data, /* pass-thru arg for hash_alg */ 445*0Sstevel@tonic-gate int (*keycmp)(mod_hash_key_t, mod_hash_key_t), /* key comparator */ 446*0Sstevel@tonic-gate int sleep) /* whether to sleep for mem */ 447*0Sstevel@tonic-gate { 448*0Sstevel@tonic-gate mod_hash_t *mod_hash; 449*0Sstevel@tonic-gate ASSERT(hname && keycmp && hash_alg && vdtor && kdtor); 450*0Sstevel@tonic-gate 451*0Sstevel@tonic-gate if ((mod_hash = kmem_zalloc(MH_SIZE(nchains), sleep)) == NULL) 452*0Sstevel@tonic-gate return (NULL); 453*0Sstevel@tonic-gate 454*0Sstevel@tonic-gate mod_hash->mh_name = kmem_alloc(strlen(hname) + 1, sleep); 455*0Sstevel@tonic-gate if (mod_hash->mh_name == NULL) { 456*0Sstevel@tonic-gate kmem_free(mod_hash, MH_SIZE(nchains)); 457*0Sstevel@tonic-gate return (NULL); 458*0Sstevel@tonic-gate } 459*0Sstevel@tonic-gate (void) strcpy(mod_hash->mh_name, hname); 460*0Sstevel@tonic-gate 461*0Sstevel@tonic-gate mod_hash->mh_sleep = sleep; 462*0Sstevel@tonic-gate mod_hash->mh_nchains = nchains; 463*0Sstevel@tonic-gate mod_hash->mh_kdtor = kdtor; 464*0Sstevel@tonic-gate mod_hash->mh_vdtor = vdtor; 465*0Sstevel@tonic-gate mod_hash->mh_hashalg = hash_alg; 466*0Sstevel@tonic-gate mod_hash->mh_hashalg_data = hash_alg_data; 467*0Sstevel@tonic-gate mod_hash->mh_keycmp = keycmp; 468*0Sstevel@tonic-gate 469*0Sstevel@tonic-gate /* 470*0Sstevel@tonic-gate * Link the hash up on the list of hashes 471*0Sstevel@tonic-gate */ 472*0Sstevel@tonic-gate mutex_enter(&mh_head_lock); 473*0Sstevel@tonic-gate mod_hash->mh_next = mh_head; 474*0Sstevel@tonic-gate mh_head = mod_hash; 475*0Sstevel@tonic-gate mutex_exit(&mh_head_lock); 476*0Sstevel@tonic-gate 477*0Sstevel@tonic-gate return (mod_hash); 478*0Sstevel@tonic-gate } 479*0Sstevel@tonic-gate 480*0Sstevel@tonic-gate /* 481*0Sstevel@tonic-gate * mod_hash_destroy_hash() 482*0Sstevel@tonic-gate * destroy a hash table, destroying all of its stored keys and values 483*0Sstevel@tonic-gate * as well. 484*0Sstevel@tonic-gate */ 485*0Sstevel@tonic-gate void 486*0Sstevel@tonic-gate mod_hash_destroy_hash(mod_hash_t *hash) 487*0Sstevel@tonic-gate { 488*0Sstevel@tonic-gate mod_hash_t *mhp, *mhpp; 489*0Sstevel@tonic-gate 490*0Sstevel@tonic-gate mutex_enter(&mh_head_lock); 491*0Sstevel@tonic-gate /* 492*0Sstevel@tonic-gate * Remove the hash from the hash list 493*0Sstevel@tonic-gate */ 494*0Sstevel@tonic-gate if (hash == mh_head) { /* removing 1st list elem */ 495*0Sstevel@tonic-gate mh_head = mh_head->mh_next; 496*0Sstevel@tonic-gate } else { 497*0Sstevel@tonic-gate /* 498*0Sstevel@tonic-gate * mhpp can start out NULL since we know the 1st elem isn't the 499*0Sstevel@tonic-gate * droid we're looking for. 500*0Sstevel@tonic-gate */ 501*0Sstevel@tonic-gate mhpp = NULL; 502*0Sstevel@tonic-gate for (mhp = mh_head; mhp != NULL; mhp = mhp->mh_next) { 503*0Sstevel@tonic-gate if (mhp == hash) { 504*0Sstevel@tonic-gate mhpp->mh_next = mhp->mh_next; 505*0Sstevel@tonic-gate break; 506*0Sstevel@tonic-gate } 507*0Sstevel@tonic-gate mhpp = mhp; 508*0Sstevel@tonic-gate } 509*0Sstevel@tonic-gate } 510*0Sstevel@tonic-gate mutex_exit(&mh_head_lock); 511*0Sstevel@tonic-gate 512*0Sstevel@tonic-gate /* 513*0Sstevel@tonic-gate * Clean out keys and values. 514*0Sstevel@tonic-gate */ 515*0Sstevel@tonic-gate mod_hash_clear(hash); 516*0Sstevel@tonic-gate 517*0Sstevel@tonic-gate kmem_free(hash->mh_name, strlen(hash->mh_name) + 1); 518*0Sstevel@tonic-gate kmem_free(hash, MH_SIZE(hash->mh_nchains)); 519*0Sstevel@tonic-gate } 520*0Sstevel@tonic-gate 521*0Sstevel@tonic-gate /* 522*0Sstevel@tonic-gate * i_mod_hash() 523*0Sstevel@tonic-gate * Call the hashing algorithm for this hash table, with the given key. 524*0Sstevel@tonic-gate */ 525*0Sstevel@tonic-gate static uint_t 526*0Sstevel@tonic-gate i_mod_hash(mod_hash_t *hash, mod_hash_key_t key) 527*0Sstevel@tonic-gate { 528*0Sstevel@tonic-gate uint_t h; 529*0Sstevel@tonic-gate /* 530*0Sstevel@tonic-gate * Prevent div by 0 problems; 531*0Sstevel@tonic-gate * Also a nice shortcut when using a hash as a list 532*0Sstevel@tonic-gate */ 533*0Sstevel@tonic-gate if (hash->mh_nchains == 1) 534*0Sstevel@tonic-gate return (0); 535*0Sstevel@tonic-gate 536*0Sstevel@tonic-gate h = (hash->mh_hashalg)(hash->mh_hashalg_data, key); 537*0Sstevel@tonic-gate return (h % (hash->mh_nchains - 1)); 538*0Sstevel@tonic-gate } 539*0Sstevel@tonic-gate 540*0Sstevel@tonic-gate /* 541*0Sstevel@tonic-gate * i_mod_hash_insert_nosync() 542*0Sstevel@tonic-gate * mod_hash_insert() 543*0Sstevel@tonic-gate * mod_hash_insert_reserve() 544*0Sstevel@tonic-gate * insert 'val' into the hash table, using 'key' as its key. If 'key' is 545*0Sstevel@tonic-gate * already a key in the hash, an error will be returned, and the key-val 546*0Sstevel@tonic-gate * pair will not be inserted. i_mod_hash_insert_nosync() supports a simple 547*0Sstevel@tonic-gate * handle abstraction, allowing hash entry allocation to be separated from 548*0Sstevel@tonic-gate * the hash insertion. this abstraction allows simple use of the mod_hash 549*0Sstevel@tonic-gate * structure in situations where mod_hash_insert() with a KM_SLEEP 550*0Sstevel@tonic-gate * allocation policy would otherwise be unsafe. 551*0Sstevel@tonic-gate */ 552*0Sstevel@tonic-gate int 553*0Sstevel@tonic-gate i_mod_hash_insert_nosync(mod_hash_t *hash, mod_hash_key_t key, 554*0Sstevel@tonic-gate mod_hash_val_t val, mod_hash_hndl_t handle) 555*0Sstevel@tonic-gate { 556*0Sstevel@tonic-gate uint_t hashidx; 557*0Sstevel@tonic-gate struct mod_hash_entry *entry; 558*0Sstevel@tonic-gate 559*0Sstevel@tonic-gate ASSERT(hash); 560*0Sstevel@tonic-gate 561*0Sstevel@tonic-gate /* 562*0Sstevel@tonic-gate * If we've not been given reserved storage, allocate storage directly, 563*0Sstevel@tonic-gate * using the hash's allocation policy. 564*0Sstevel@tonic-gate */ 565*0Sstevel@tonic-gate if (handle == (mod_hash_hndl_t)0) { 566*0Sstevel@tonic-gate entry = kmem_cache_alloc(mh_e_cache, hash->mh_sleep); 567*0Sstevel@tonic-gate if (entry == NULL) { 568*0Sstevel@tonic-gate hash->mh_stat.mhs_nomem++; 569*0Sstevel@tonic-gate return (MH_ERR_NOMEM); 570*0Sstevel@tonic-gate } 571*0Sstevel@tonic-gate } else { 572*0Sstevel@tonic-gate entry = (struct mod_hash_entry *)handle; 573*0Sstevel@tonic-gate } 574*0Sstevel@tonic-gate 575*0Sstevel@tonic-gate hashidx = i_mod_hash(hash, key); 576*0Sstevel@tonic-gate entry->mhe_key = key; 577*0Sstevel@tonic-gate entry->mhe_val = val; 578*0Sstevel@tonic-gate entry->mhe_next = hash->mh_entries[hashidx]; 579*0Sstevel@tonic-gate 580*0Sstevel@tonic-gate hash->mh_entries[hashidx] = entry; 581*0Sstevel@tonic-gate hash->mh_stat.mhs_nelems++; 582*0Sstevel@tonic-gate 583*0Sstevel@tonic-gate return (0); 584*0Sstevel@tonic-gate } 585*0Sstevel@tonic-gate 586*0Sstevel@tonic-gate int 587*0Sstevel@tonic-gate mod_hash_insert(mod_hash_t *hash, mod_hash_key_t key, mod_hash_val_t val) 588*0Sstevel@tonic-gate { 589*0Sstevel@tonic-gate int res; 590*0Sstevel@tonic-gate mod_hash_val_t v; 591*0Sstevel@tonic-gate 592*0Sstevel@tonic-gate rw_enter(&hash->mh_contents, RW_WRITER); 593*0Sstevel@tonic-gate 594*0Sstevel@tonic-gate /* 595*0Sstevel@tonic-gate * Disallow duplicate keys in the hash 596*0Sstevel@tonic-gate */ 597*0Sstevel@tonic-gate if (i_mod_hash_find_nosync(hash, key, &v) == 0) { 598*0Sstevel@tonic-gate rw_exit(&hash->mh_contents); 599*0Sstevel@tonic-gate hash->mh_stat.mhs_coll++; 600*0Sstevel@tonic-gate return (MH_ERR_DUPLICATE); 601*0Sstevel@tonic-gate } 602*0Sstevel@tonic-gate 603*0Sstevel@tonic-gate res = i_mod_hash_insert_nosync(hash, key, val, (mod_hash_hndl_t)0); 604*0Sstevel@tonic-gate rw_exit(&hash->mh_contents); 605*0Sstevel@tonic-gate 606*0Sstevel@tonic-gate return (res); 607*0Sstevel@tonic-gate } 608*0Sstevel@tonic-gate 609*0Sstevel@tonic-gate int 610*0Sstevel@tonic-gate mod_hash_insert_reserve(mod_hash_t *hash, mod_hash_key_t key, 611*0Sstevel@tonic-gate mod_hash_val_t val, mod_hash_hndl_t handle) 612*0Sstevel@tonic-gate { 613*0Sstevel@tonic-gate int res; 614*0Sstevel@tonic-gate mod_hash_val_t v; 615*0Sstevel@tonic-gate 616*0Sstevel@tonic-gate rw_enter(&hash->mh_contents, RW_WRITER); 617*0Sstevel@tonic-gate 618*0Sstevel@tonic-gate /* 619*0Sstevel@tonic-gate * Disallow duplicate keys in the hash 620*0Sstevel@tonic-gate */ 621*0Sstevel@tonic-gate if (i_mod_hash_find_nosync(hash, key, &v) == 0) { 622*0Sstevel@tonic-gate rw_exit(&hash->mh_contents); 623*0Sstevel@tonic-gate hash->mh_stat.mhs_coll++; 624*0Sstevel@tonic-gate return (MH_ERR_DUPLICATE); 625*0Sstevel@tonic-gate } 626*0Sstevel@tonic-gate res = i_mod_hash_insert_nosync(hash, key, val, handle); 627*0Sstevel@tonic-gate rw_exit(&hash->mh_contents); 628*0Sstevel@tonic-gate 629*0Sstevel@tonic-gate return (res); 630*0Sstevel@tonic-gate } 631*0Sstevel@tonic-gate 632*0Sstevel@tonic-gate /* 633*0Sstevel@tonic-gate * mod_hash_reserve() 634*0Sstevel@tonic-gate * mod_hash_reserve_nosleep() 635*0Sstevel@tonic-gate * mod_hash_cancel() 636*0Sstevel@tonic-gate * Make or cancel a mod_hash_entry_t reservation. Reservations are used in 637*0Sstevel@tonic-gate * mod_hash_insert_reserve() above. 638*0Sstevel@tonic-gate */ 639*0Sstevel@tonic-gate int 640*0Sstevel@tonic-gate mod_hash_reserve(mod_hash_t *hash, mod_hash_hndl_t *handlep) 641*0Sstevel@tonic-gate { 642*0Sstevel@tonic-gate *handlep = kmem_cache_alloc(mh_e_cache, hash->mh_sleep); 643*0Sstevel@tonic-gate if (*handlep == NULL) { 644*0Sstevel@tonic-gate hash->mh_stat.mhs_nomem++; 645*0Sstevel@tonic-gate return (MH_ERR_NOMEM); 646*0Sstevel@tonic-gate } 647*0Sstevel@tonic-gate 648*0Sstevel@tonic-gate return (0); 649*0Sstevel@tonic-gate } 650*0Sstevel@tonic-gate 651*0Sstevel@tonic-gate int 652*0Sstevel@tonic-gate mod_hash_reserve_nosleep(mod_hash_t *hash, mod_hash_hndl_t *handlep) 653*0Sstevel@tonic-gate { 654*0Sstevel@tonic-gate *handlep = kmem_cache_alloc(mh_e_cache, KM_NOSLEEP); 655*0Sstevel@tonic-gate if (*handlep == NULL) { 656*0Sstevel@tonic-gate hash->mh_stat.mhs_nomem++; 657*0Sstevel@tonic-gate return (MH_ERR_NOMEM); 658*0Sstevel@tonic-gate } 659*0Sstevel@tonic-gate 660*0Sstevel@tonic-gate return (0); 661*0Sstevel@tonic-gate 662*0Sstevel@tonic-gate } 663*0Sstevel@tonic-gate 664*0Sstevel@tonic-gate /*ARGSUSED*/ 665*0Sstevel@tonic-gate void 666*0Sstevel@tonic-gate mod_hash_cancel(mod_hash_t *hash, mod_hash_hndl_t *handlep) 667*0Sstevel@tonic-gate { 668*0Sstevel@tonic-gate kmem_cache_free(mh_e_cache, *handlep); 669*0Sstevel@tonic-gate *handlep = (mod_hash_hndl_t)0; 670*0Sstevel@tonic-gate } 671*0Sstevel@tonic-gate 672*0Sstevel@tonic-gate /* 673*0Sstevel@tonic-gate * i_mod_hash_remove_nosync() 674*0Sstevel@tonic-gate * mod_hash_remove() 675*0Sstevel@tonic-gate * Remove an element from the hash table. 676*0Sstevel@tonic-gate */ 677*0Sstevel@tonic-gate int 678*0Sstevel@tonic-gate i_mod_hash_remove_nosync(mod_hash_t *hash, mod_hash_key_t key, 679*0Sstevel@tonic-gate mod_hash_val_t *val) 680*0Sstevel@tonic-gate { 681*0Sstevel@tonic-gate int hashidx; 682*0Sstevel@tonic-gate struct mod_hash_entry *e, *ep; 683*0Sstevel@tonic-gate 684*0Sstevel@tonic-gate hashidx = i_mod_hash(hash, key); 685*0Sstevel@tonic-gate ep = NULL; /* e's parent */ 686*0Sstevel@tonic-gate 687*0Sstevel@tonic-gate for (e = hash->mh_entries[hashidx]; e != NULL; e = e->mhe_next) { 688*0Sstevel@tonic-gate if (MH_KEYCMP(hash, e->mhe_key, key) == 0) 689*0Sstevel@tonic-gate break; 690*0Sstevel@tonic-gate ep = e; 691*0Sstevel@tonic-gate } 692*0Sstevel@tonic-gate 693*0Sstevel@tonic-gate if (e == NULL) { /* not found */ 694*0Sstevel@tonic-gate return (MH_ERR_NOTFOUND); 695*0Sstevel@tonic-gate } 696*0Sstevel@tonic-gate 697*0Sstevel@tonic-gate if (ep == NULL) /* special case 1st element in bucket */ 698*0Sstevel@tonic-gate hash->mh_entries[hashidx] = e->mhe_next; 699*0Sstevel@tonic-gate else 700*0Sstevel@tonic-gate ep->mhe_next = e->mhe_next; 701*0Sstevel@tonic-gate 702*0Sstevel@tonic-gate /* 703*0Sstevel@tonic-gate * Clean up resources used by the node's key. 704*0Sstevel@tonic-gate */ 705*0Sstevel@tonic-gate MH_KEY_DESTROY(hash, e->mhe_key); 706*0Sstevel@tonic-gate 707*0Sstevel@tonic-gate *val = e->mhe_val; 708*0Sstevel@tonic-gate kmem_cache_free(mh_e_cache, e); 709*0Sstevel@tonic-gate hash->mh_stat.mhs_nelems--; 710*0Sstevel@tonic-gate 711*0Sstevel@tonic-gate return (0); 712*0Sstevel@tonic-gate } 713*0Sstevel@tonic-gate 714*0Sstevel@tonic-gate int 715*0Sstevel@tonic-gate mod_hash_remove(mod_hash_t *hash, mod_hash_key_t key, mod_hash_val_t *val) 716*0Sstevel@tonic-gate { 717*0Sstevel@tonic-gate int res; 718*0Sstevel@tonic-gate 719*0Sstevel@tonic-gate rw_enter(&hash->mh_contents, RW_WRITER); 720*0Sstevel@tonic-gate res = i_mod_hash_remove_nosync(hash, key, val); 721*0Sstevel@tonic-gate rw_exit(&hash->mh_contents); 722*0Sstevel@tonic-gate 723*0Sstevel@tonic-gate return (res); 724*0Sstevel@tonic-gate } 725*0Sstevel@tonic-gate 726*0Sstevel@tonic-gate /* 727*0Sstevel@tonic-gate * mod_hash_replace() 728*0Sstevel@tonic-gate * atomically remove an existing key-value pair from a hash, and replace 729*0Sstevel@tonic-gate * the key and value with the ones supplied. The removed key and value 730*0Sstevel@tonic-gate * (if any) are destroyed. 731*0Sstevel@tonic-gate */ 732*0Sstevel@tonic-gate int 733*0Sstevel@tonic-gate mod_hash_replace(mod_hash_t *hash, mod_hash_key_t key, mod_hash_val_t val) 734*0Sstevel@tonic-gate { 735*0Sstevel@tonic-gate int res; 736*0Sstevel@tonic-gate mod_hash_val_t v; 737*0Sstevel@tonic-gate 738*0Sstevel@tonic-gate rw_enter(&hash->mh_contents, RW_WRITER); 739*0Sstevel@tonic-gate 740*0Sstevel@tonic-gate if (i_mod_hash_remove_nosync(hash, key, &v) == 0) { 741*0Sstevel@tonic-gate /* 742*0Sstevel@tonic-gate * mod_hash_remove() takes care of freeing up the key resources. 743*0Sstevel@tonic-gate */ 744*0Sstevel@tonic-gate MH_VAL_DESTROY(hash, v); 745*0Sstevel@tonic-gate } 746*0Sstevel@tonic-gate res = i_mod_hash_insert_nosync(hash, key, val, (mod_hash_hndl_t)0); 747*0Sstevel@tonic-gate 748*0Sstevel@tonic-gate rw_exit(&hash->mh_contents); 749*0Sstevel@tonic-gate 750*0Sstevel@tonic-gate return (res); 751*0Sstevel@tonic-gate } 752*0Sstevel@tonic-gate 753*0Sstevel@tonic-gate /* 754*0Sstevel@tonic-gate * mod_hash_destroy() 755*0Sstevel@tonic-gate * Remove an element from the hash table matching 'key', and destroy it. 756*0Sstevel@tonic-gate */ 757*0Sstevel@tonic-gate int 758*0Sstevel@tonic-gate mod_hash_destroy(mod_hash_t *hash, mod_hash_key_t key) 759*0Sstevel@tonic-gate { 760*0Sstevel@tonic-gate mod_hash_val_t val; 761*0Sstevel@tonic-gate int rv; 762*0Sstevel@tonic-gate 763*0Sstevel@tonic-gate rw_enter(&hash->mh_contents, RW_WRITER); 764*0Sstevel@tonic-gate 765*0Sstevel@tonic-gate if ((rv = i_mod_hash_remove_nosync(hash, key, &val)) == 0) { 766*0Sstevel@tonic-gate /* 767*0Sstevel@tonic-gate * mod_hash_remove() takes care of freeing up the key resources. 768*0Sstevel@tonic-gate */ 769*0Sstevel@tonic-gate MH_VAL_DESTROY(hash, val); 770*0Sstevel@tonic-gate } 771*0Sstevel@tonic-gate 772*0Sstevel@tonic-gate rw_exit(&hash->mh_contents); 773*0Sstevel@tonic-gate return (rv); 774*0Sstevel@tonic-gate } 775*0Sstevel@tonic-gate 776*0Sstevel@tonic-gate /* 777*0Sstevel@tonic-gate * i_mod_hash_find_nosync() 778*0Sstevel@tonic-gate * mod_hash_find() 779*0Sstevel@tonic-gate * Find a value in the hash table corresponding to the given key. 780*0Sstevel@tonic-gate */ 781*0Sstevel@tonic-gate static int 782*0Sstevel@tonic-gate i_mod_hash_find_nosync(mod_hash_t *hash, mod_hash_key_t key, 783*0Sstevel@tonic-gate mod_hash_val_t *val) 784*0Sstevel@tonic-gate { 785*0Sstevel@tonic-gate uint_t hashidx; 786*0Sstevel@tonic-gate struct mod_hash_entry *e; 787*0Sstevel@tonic-gate 788*0Sstevel@tonic-gate hashidx = i_mod_hash(hash, key); 789*0Sstevel@tonic-gate 790*0Sstevel@tonic-gate for (e = hash->mh_entries[hashidx]; e != NULL; e = e->mhe_next) { 791*0Sstevel@tonic-gate if (MH_KEYCMP(hash, e->mhe_key, key) == 0) { 792*0Sstevel@tonic-gate *val = e->mhe_val; 793*0Sstevel@tonic-gate hash->mh_stat.mhs_hit++; 794*0Sstevel@tonic-gate return (0); 795*0Sstevel@tonic-gate } 796*0Sstevel@tonic-gate } 797*0Sstevel@tonic-gate hash->mh_stat.mhs_miss++; 798*0Sstevel@tonic-gate return (MH_ERR_NOTFOUND); 799*0Sstevel@tonic-gate } 800*0Sstevel@tonic-gate 801*0Sstevel@tonic-gate int 802*0Sstevel@tonic-gate mod_hash_find(mod_hash_t *hash, mod_hash_key_t key, mod_hash_val_t *val) 803*0Sstevel@tonic-gate { 804*0Sstevel@tonic-gate int res; 805*0Sstevel@tonic-gate 806*0Sstevel@tonic-gate rw_enter(&hash->mh_contents, RW_READER); 807*0Sstevel@tonic-gate res = i_mod_hash_find_nosync(hash, key, val); 808*0Sstevel@tonic-gate rw_exit(&hash->mh_contents); 809*0Sstevel@tonic-gate 810*0Sstevel@tonic-gate return (res); 811*0Sstevel@tonic-gate } 812*0Sstevel@tonic-gate 813*0Sstevel@tonic-gate int 814*0Sstevel@tonic-gate mod_hash_find_cb(mod_hash_t *hash, mod_hash_key_t key, mod_hash_val_t *val, 815*0Sstevel@tonic-gate void (*find_cb)(mod_hash_key_t, mod_hash_val_t)) 816*0Sstevel@tonic-gate { 817*0Sstevel@tonic-gate int res; 818*0Sstevel@tonic-gate 819*0Sstevel@tonic-gate rw_enter(&hash->mh_contents, RW_READER); 820*0Sstevel@tonic-gate res = i_mod_hash_find_nosync(hash, key, val); 821*0Sstevel@tonic-gate if (res == 0) { 822*0Sstevel@tonic-gate find_cb(key, *val); 823*0Sstevel@tonic-gate } 824*0Sstevel@tonic-gate rw_exit(&hash->mh_contents); 825*0Sstevel@tonic-gate 826*0Sstevel@tonic-gate return (res); 827*0Sstevel@tonic-gate } 828*0Sstevel@tonic-gate 829*0Sstevel@tonic-gate static void 830*0Sstevel@tonic-gate i_mod_hash_walk_nosync(mod_hash_t *hash, 831*0Sstevel@tonic-gate uint_t (*callback)(mod_hash_key_t, mod_hash_val_t *, void *), void *arg) 832*0Sstevel@tonic-gate { 833*0Sstevel@tonic-gate struct mod_hash_entry *e; 834*0Sstevel@tonic-gate uint_t hashidx; 835*0Sstevel@tonic-gate int res = MH_WALK_CONTINUE; 836*0Sstevel@tonic-gate 837*0Sstevel@tonic-gate for (hashidx = 0; 838*0Sstevel@tonic-gate (hashidx < (hash->mh_nchains - 1)) && (res == MH_WALK_CONTINUE); 839*0Sstevel@tonic-gate hashidx++) { 840*0Sstevel@tonic-gate e = hash->mh_entries[hashidx]; 841*0Sstevel@tonic-gate while ((e != NULL) && (res == MH_WALK_CONTINUE)) { 842*0Sstevel@tonic-gate res = callback(e->mhe_key, e->mhe_val, arg); 843*0Sstevel@tonic-gate e = e->mhe_next; 844*0Sstevel@tonic-gate } 845*0Sstevel@tonic-gate } 846*0Sstevel@tonic-gate } 847*0Sstevel@tonic-gate 848*0Sstevel@tonic-gate /* 849*0Sstevel@tonic-gate * mod_hash_walk() 850*0Sstevel@tonic-gate * Walks all the elements in the hashtable and invokes the callback 851*0Sstevel@tonic-gate * function with the key/value pair for each element. The hashtable 852*0Sstevel@tonic-gate * is locked for readers so the callback function should not attempt 853*0Sstevel@tonic-gate * to do any updates to the hashable. The callback function should 854*0Sstevel@tonic-gate * return MH_WALK_CONTINUE to continue walking the hashtable or 855*0Sstevel@tonic-gate * MH_WALK_TERMINATE to abort the walk of the hashtable. 856*0Sstevel@tonic-gate */ 857*0Sstevel@tonic-gate void 858*0Sstevel@tonic-gate mod_hash_walk(mod_hash_t *hash, 859*0Sstevel@tonic-gate uint_t (*callback)(mod_hash_key_t, mod_hash_val_t *, void *), void *arg) 860*0Sstevel@tonic-gate { 861*0Sstevel@tonic-gate rw_enter(&hash->mh_contents, RW_READER); 862*0Sstevel@tonic-gate i_mod_hash_walk_nosync(hash, callback, arg); 863*0Sstevel@tonic-gate rw_exit(&hash->mh_contents); 864*0Sstevel@tonic-gate } 865*0Sstevel@tonic-gate 866*0Sstevel@tonic-gate 867*0Sstevel@tonic-gate /* 868*0Sstevel@tonic-gate * i_mod_hash_clear_nosync() 869*0Sstevel@tonic-gate * mod_hash_clear() 870*0Sstevel@tonic-gate * Clears the given hash table by calling the destructor of every hash 871*0Sstevel@tonic-gate * element and freeing up all mod_hash_entry's. 872*0Sstevel@tonic-gate */ 873*0Sstevel@tonic-gate static void 874*0Sstevel@tonic-gate i_mod_hash_clear_nosync(mod_hash_t *hash) 875*0Sstevel@tonic-gate { 876*0Sstevel@tonic-gate int i; 877*0Sstevel@tonic-gate struct mod_hash_entry *e, *old_e; 878*0Sstevel@tonic-gate 879*0Sstevel@tonic-gate for (i = 0; i < hash->mh_nchains; i++) { 880*0Sstevel@tonic-gate e = hash->mh_entries[i]; 881*0Sstevel@tonic-gate while (e != NULL) { 882*0Sstevel@tonic-gate MH_KEY_DESTROY(hash, e->mhe_key); 883*0Sstevel@tonic-gate MH_VAL_DESTROY(hash, e->mhe_val); 884*0Sstevel@tonic-gate old_e = e; 885*0Sstevel@tonic-gate e = e->mhe_next; 886*0Sstevel@tonic-gate kmem_cache_free(mh_e_cache, old_e); 887*0Sstevel@tonic-gate } 888*0Sstevel@tonic-gate hash->mh_entries[i] = NULL; 889*0Sstevel@tonic-gate } 890*0Sstevel@tonic-gate hash->mh_stat.mhs_nelems = 0; 891*0Sstevel@tonic-gate } 892*0Sstevel@tonic-gate 893*0Sstevel@tonic-gate void 894*0Sstevel@tonic-gate mod_hash_clear(mod_hash_t *hash) 895*0Sstevel@tonic-gate { 896*0Sstevel@tonic-gate ASSERT(hash); 897*0Sstevel@tonic-gate rw_enter(&hash->mh_contents, RW_WRITER); 898*0Sstevel@tonic-gate i_mod_hash_clear_nosync(hash); 899*0Sstevel@tonic-gate rw_exit(&hash->mh_contents); 900*0Sstevel@tonic-gate } 901