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 2008 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 #ifndef _SYS_MODHASH_H
  27 #define _SYS_MODHASH_H
  28 
  29 /*
  30  * Generic hash implementation for the kernel.
  31  */
  32 
  33 #ifdef __cplusplus
  34 extern "C" {
  35 #endif
  36 
  37 #if defined(_KERNEL) || defined(_FAKE_KERNEL)
  38 
  39 #include <sys/types.h>
  40 
  41 /*
  42  * Opaque data types for storing keys and values
  43  */
  44 typedef void *mod_hash_val_t;
  45 typedef void *mod_hash_key_t;
  46 
  47 /*
  48  * Opaque data type for reservation
  49  */
  50 typedef void *mod_hash_hndl_t;
  51 
  52 /*
  53  * Opaque type for hash itself.
  54  */
  55 struct mod_hash;
  56 typedef struct mod_hash mod_hash_t;
  57 
  58 /*
  59  * String hash table
  60  */
  61 mod_hash_t *mod_hash_create_strhash(char *, size_t, void (*)(mod_hash_val_t));
  62 void mod_hash_destroy_strhash(mod_hash_t *);
  63 int mod_hash_strkey_cmp(mod_hash_key_t, mod_hash_key_t);
  64 void mod_hash_strkey_dtor(mod_hash_key_t);
  65 void mod_hash_strval_dtor(mod_hash_val_t);
  66 uint_t mod_hash_bystr(void *, mod_hash_key_t);
  67 
  68 /*
  69  * Pointer hash table
  70  */
  71 mod_hash_t *mod_hash_create_ptrhash(char *, size_t, void (*)(mod_hash_val_t),
  72     size_t);
  73 void mod_hash_destroy_ptrhash(mod_hash_t *);
  74 int mod_hash_ptrkey_cmp(mod_hash_key_t, mod_hash_key_t);
  75 uint_t mod_hash_byptr(void *, mod_hash_key_t);
  76 
  77 /*
  78  * ID hash table
  79  */
  80 mod_hash_t *mod_hash_create_idhash(char *, size_t, void (*)(mod_hash_val_t));
  81 void mod_hash_destroy_idhash(mod_hash_t *);
  82 int mod_hash_idkey_cmp(mod_hash_key_t, mod_hash_key_t);
  83 uint_t mod_hash_byid(void *, mod_hash_key_t);
  84 uint_t mod_hash_iddata_gen(size_t);
  85 
  86 /*
  87  * Hash management functions
  88  */
  89 mod_hash_t *mod_hash_create_extended(char *, size_t, void (*)(mod_hash_key_t),
  90     void (*)(mod_hash_val_t), uint_t (*)(void *, mod_hash_key_t), void *,
  91     int (*)(mod_hash_key_t, mod_hash_key_t), int);
  92 
  93 void mod_hash_destroy_hash(mod_hash_t *);
  94 void mod_hash_clear(mod_hash_t *);
  95 
  96 /*
  97  * Null key and value destructors
  98  */
  99 void mod_hash_null_keydtor(mod_hash_key_t);
 100 void mod_hash_null_valdtor(mod_hash_val_t);
 101 
 102 /*
 103  * Basic hash operations
 104  */
 105 
 106 /*
 107  * Error codes for insert, remove, find, destroy.
 108  */
 109 #define MH_ERR_NOMEM -1
 110 #define MH_ERR_DUPLICATE -2
 111 #define MH_ERR_NOTFOUND -3
 112 
 113 /*
 114  * Return codes for hash walkers
 115  */
 116 #define MH_WALK_CONTINUE 0
 117 #define MH_WALK_TERMINATE 1
 118 
 119 /*
 120  * Basic hash operations
 121  */
 122 int mod_hash_insert(mod_hash_t *, mod_hash_key_t, mod_hash_val_t);
 123 int mod_hash_replace(mod_hash_t *, mod_hash_key_t, mod_hash_val_t);
 124 int mod_hash_remove(mod_hash_t *, mod_hash_key_t, mod_hash_val_t *);
 125 int mod_hash_destroy(mod_hash_t *, mod_hash_key_t);
 126 int mod_hash_find(mod_hash_t *, mod_hash_key_t, mod_hash_val_t *);
 127 int mod_hash_find_cb(mod_hash_t *, mod_hash_key_t, mod_hash_val_t *,
 128     void (*)(mod_hash_key_t, mod_hash_val_t));
 129 int mod_hash_find_cb_rval(mod_hash_t *, mod_hash_key_t, mod_hash_val_t *,
 130     int (*)(mod_hash_key_t, mod_hash_val_t), int *);
 131 void mod_hash_walk(mod_hash_t *,
 132     uint_t (*)(mod_hash_key_t, mod_hash_val_t *, void *), void *);
 133 
 134 /*
 135  * Reserving hash operations
 136  */
 137 int mod_hash_reserve(mod_hash_t *, mod_hash_hndl_t *);
 138 int mod_hash_reserve_nosleep(mod_hash_t *, mod_hash_hndl_t *);
 139 void mod_hash_cancel(mod_hash_t *, mod_hash_hndl_t *);
 140 int mod_hash_insert_reserve(mod_hash_t *, mod_hash_key_t, mod_hash_val_t,
 141     mod_hash_hndl_t);
 142 
 143 #endif /* _KERNEL */
 144 
 145 #ifdef __cplusplus
 146 }
 147 #endif
 148 
 149 #endif /* _SYS_MODHASH_H */