1 1.1 christos =pod 2 1.1 christos 3 1.1 christos =head1 NAME 4 1.1 christos 5 1.1 christos LHASH, DECLARE_LHASH_OF, 6 1.1 christos OPENSSL_LH_COMPFUNC, OPENSSL_LH_HASHFUNC, OPENSSL_LH_DOALL_FUNC, 7 1.1 christos LHASH_DOALL_ARG_FN_TYPE, 8 1.1 christos IMPLEMENT_LHASH_HASH_FN, IMPLEMENT_LHASH_COMP_FN, 9 1.1.1.4 christos lh_TYPE_new, lh_TYPE_free, lh_TYPE_flush, 10 1.1 christos lh_TYPE_insert, lh_TYPE_delete, lh_TYPE_retrieve, 11 1.1.1.5 christos lh_TYPE_doall, lh_TYPE_doall_arg, lh_TYPE_num_items, lh_TYPE_get_down_load, 12 1.1.1.5 christos lh_TYPE_set_down_load, lh_TYPE_error, 13 1.1.1.4 christos OPENSSL_LH_new, OPENSSL_LH_free, OPENSSL_LH_flush, 14 1.1.1.4 christos OPENSSL_LH_insert, OPENSSL_LH_delete, OPENSSL_LH_retrieve, 15 1.1.1.5 christos OPENSSL_LH_doall, OPENSSL_LH_doall_arg, OPENSSL_LH_num_items, 16 1.1.1.5 christos OPENSSL_LH_get_down_load, OPENSSL_LH_set_down_load, OPENSSL_LH_error 17 1.1.1.4 christos - dynamic hash table 18 1.1 christos 19 1.1 christos =head1 SYNOPSIS 20 1.1 christos 21 1.1.1.4 christos =for openssl generic 22 1.1 christos 23 1.1 christos #include <openssl/lhash.h> 24 1.1 christos 25 1.1 christos DECLARE_LHASH_OF(TYPE); 26 1.1 christos 27 1.1.1.4 christos LHASH_OF(TYPE) *lh_TYPE_new(OPENSSL_LH_HASHFUNC hash, OPENSSL_LH_COMPFUNC compare); 28 1.1 christos void lh_TYPE_free(LHASH_OF(TYPE) *table); 29 1.1.1.4 christos void lh_TYPE_flush(LHASH_OF(TYPE) *table); 30 1.1 christos 31 1.1 christos TYPE *lh_TYPE_insert(LHASH_OF(TYPE) *table, TYPE *data); 32 1.1 christos TYPE *lh_TYPE_delete(LHASH_OF(TYPE) *table, TYPE *data); 33 1.1.1.3 christos TYPE *lh_TYPE_retrieve(LHASH_OF(TYPE) *table, TYPE *data); 34 1.1 christos 35 1.1 christos void lh_TYPE_doall(LHASH_OF(TYPE) *table, OPENSSL_LH_DOALL_FUNC func); 36 1.1 christos void lh_TYPE_doall_arg(LHASH_OF(TYPE) *table, OPENSSL_LH_DOALL_FUNCARG func, 37 1.1 christos TYPE *arg); 38 1.1 christos 39 1.1.1.5 christos unsigned long lh_TYPE_num_items(OPENSSL_LHASH *lh); 40 1.1.1.5 christos unsigned long lh_TYPE_get_down_load(OPENSSL_LHASH *lh); 41 1.1.1.5 christos void lh_TYPE_set_down_load(OPENSSL_LHASH *lh, unsigned long dl); 42 1.1.1.5 christos 43 1.1 christos int lh_TYPE_error(LHASH_OF(TYPE) *table); 44 1.1 christos 45 1.1 christos typedef int (*OPENSSL_LH_COMPFUNC)(const void *, const void *); 46 1.1 christos typedef unsigned long (*OPENSSL_LH_HASHFUNC)(const void *); 47 1.1 christos typedef void (*OPENSSL_LH_DOALL_FUNC)(const void *); 48 1.1 christos typedef void (*LHASH_DOALL_ARG_FN_TYPE)(const void *, const void *); 49 1.1 christos 50 1.1.1.4 christos OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c); 51 1.1.1.4 christos void OPENSSL_LH_free(OPENSSL_LHASH *lh); 52 1.1.1.4 christos void OPENSSL_LH_flush(OPENSSL_LHASH *lh); 53 1.1.1.4 christos 54 1.1.1.4 christos void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data); 55 1.1.1.4 christos void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data); 56 1.1.1.4 christos void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data); 57 1.1.1.4 christos 58 1.1.1.4 christos void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func); 59 1.1.1.4 christos void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg); 60 1.1.1.4 christos 61 1.1.1.5 christos unsigned long OPENSSL_LH_num_items(OPENSSL_LHASH *lh); 62 1.1.1.5 christos unsigned long OPENSSL_LH_get_down_load(OPENSSL_LHASH *lh); 63 1.1.1.5 christos void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long dl); 64 1.1.1.5 christos 65 1.1.1.4 christos int OPENSSL_LH_error(OPENSSL_LHASH *lh); 66 1.1.1.4 christos 67 1.1.1.5 christos #define LH_LOAD_MULT /* integer constant */ 68 1.1.1.5 christos 69 1.1 christos =head1 DESCRIPTION 70 1.1 christos 71 1.1 christos This library implements type-checked dynamic hash tables. The hash 72 1.1 christos table entries can be arbitrary structures. Usually they consist of key 73 1.1.1.4 christos and value fields. In the description here, B<I<TYPE>> is used a placeholder 74 1.1 christos for any of the OpenSSL datatypes, such as I<SSL_SESSION>. 75 1.1 christos 76 1.1.1.4 christos B<lh_I<TYPE>_new>() creates a new B<LHASH_OF>(B<I<TYPE>>) structure to store 77 1.1 christos arbitrary data entries, and specifies the 'hash' and 'compare' 78 1.1.1.4 christos callbacks to be used in organising the table's entries. The I<hash> 79 1.1 christos callback takes a pointer to a table entry as its argument and returns 80 1.1 christos an unsigned long hash value for its key field. The hash value is 81 1.1 christos normally truncated to a power of 2, so make sure that your hash 82 1.1.1.4 christos function returns well mixed low order bits. The I<compare> callback 83 1.1 christos takes two arguments (pointers to two hash table entries), and returns 84 1.1.1.2 christos 0 if their keys are equal, nonzero otherwise. 85 1.1 christos 86 1.1 christos If your hash table 87 1.1.1.4 christos will contain items of some particular type and the I<hash> and 88 1.1.1.4 christos I<compare> callbacks hash/compare these types, then the 89 1.1 christos B<IMPLEMENT_LHASH_HASH_FN> and B<IMPLEMENT_LHASH_COMP_FN> macros can be 90 1.1 christos used to create callback wrappers of the prototypes required by 91 1.1.1.4 christos B<lh_I<TYPE>_new>() as shown in this example: 92 1.1 christos 93 1.1 christos /* 94 1.1 christos * Implement the hash and compare functions; "stuff" can be any word. 95 1.1 christos */ 96 1.1 christos static unsigned long stuff_hash(const TYPE *a) 97 1.1 christos { 98 1.1 christos ... 99 1.1 christos } 100 1.1 christos static int stuff_cmp(const TYPE *a, const TYPE *b) 101 1.1 christos { 102 1.1 christos ... 103 1.1 christos } 104 1.1 christos 105 1.1 christos /* 106 1.1 christos * Implement the wrapper functions. 107 1.1 christos */ 108 1.1 christos static IMPLEMENT_LHASH_HASH_FN(stuff, TYPE) 109 1.1 christos static IMPLEMENT_LHASH_COMP_FN(stuff, TYPE) 110 1.1 christos 111 1.1 christos If the type is going to be used in several places, the following macros 112 1.1 christos can be used in a common header file to declare the function wrappers: 113 1.1 christos 114 1.1 christos DECLARE_LHASH_HASH_FN(stuff, TYPE) 115 1.1 christos DECLARE_LHASH_COMP_FN(stuff, TYPE) 116 1.1 christos 117 1.1.1.4 christos Then a hash table of B<I<TYPE>> objects can be created using this: 118 1.1 christos 119 1.1 christos LHASH_OF(TYPE) *htable; 120 1.1 christos 121 1.1.1.4 christos htable = B<lh_I<TYPE>_new>(LHASH_HASH_FN(stuff), LHASH_COMP_FN(stuff)); 122 1.1 christos 123 1.1.1.4 christos B<lh_I<TYPE>_free>() frees the B<LHASH_OF>(B<I<TYPE>>) structure 124 1.1.1.4 christos I<table>. Allocated hash table entries will not be freed; consider 125 1.1.1.4 christos using B<lh_I<TYPE>_doall>() to deallocate any remaining entries in the 126 1.1.1.6 christos hash table (see below). If the argument is NULL, nothing is done. 127 1.1 christos 128 1.1.1.4 christos B<lh_I<TYPE>_flush>() empties the B<LHASH_OF>(B<I<TYPE>>) structure I<table>. New 129 1.1.1.4 christos entries can be added to the flushed table. Allocated hash table entries 130 1.1.1.4 christos will not be freed; consider using B<lh_I<TYPE>_doall>() to deallocate any 131 1.1.1.4 christos remaining entries in the hash table (see below). 132 1.1.1.4 christos 133 1.1.1.4 christos B<lh_I<TYPE>_insert>() inserts the structure pointed to by I<data> into 134 1.1.1.4 christos I<table>. If there already is an entry with the same key, the old 135 1.1.1.4 christos value is replaced. Note that B<lh_I<TYPE>_insert>() stores pointers, the 136 1.1 christos data are not copied. 137 1.1 christos 138 1.1.1.4 christos B<lh_I<TYPE>_delete>() deletes an entry from I<table>. 139 1.1 christos 140 1.1.1.4 christos B<lh_I<TYPE>_retrieve>() looks up an entry in I<table>. Normally, I<data> 141 1.1 christos is a structure with the key field(s) set; the function will return a 142 1.1 christos pointer to a fully populated structure. 143 1.1 christos 144 1.1.1.4 christos B<lh_I<TYPE>_doall>() will, for every entry in the hash table, call 145 1.1.1.4 christos I<func> with the data item as its parameter. 146 1.1 christos For example: 147 1.1 christos 148 1.1 christos /* Cleans up resources belonging to 'a' (this is implemented elsewhere) */ 149 1.1 christos void TYPE_cleanup_doall(TYPE *a); 150 1.1 christos 151 1.1 christos /* Implement a prototype-compatible wrapper for "TYPE_cleanup" */ 152 1.1 christos IMPLEMENT_LHASH_DOALL_FN(TYPE_cleanup, TYPE) 153 1.1 christos 154 1.1 christos /* Call "TYPE_cleanup" against all items in a hash table. */ 155 1.1 christos lh_TYPE_doall(hashtable, LHASH_DOALL_FN(TYPE_cleanup)); 156 1.1 christos 157 1.1 christos /* Then the hash table itself can be deallocated */ 158 1.1 christos lh_TYPE_free(hashtable); 159 1.1 christos 160 1.1.1.4 christos B<lh_I<TYPE>_doall_arg>() is the same as B<lh_I<TYPE>_doall>() except that 161 1.1.1.4 christos I<func> will be called with I<arg> as the second argument and I<func> 162 1.1.1.4 christos should be of type B<LHASH_DOALL_ARG_FN>(B<I<TYPE>>) (a callback prototype 163 1.1 christos that is passed both the table entry and an extra argument). As with 164 1.1 christos lh_doall(), you can instead choose to declare your callback with a 165 1.1 christos prototype matching the types you are dealing with and use the 166 1.1 christos declare/implement macros to create compatible wrappers that cast 167 1.1 christos variables before calling your type-specific callbacks. An example of 168 1.1 christos this is demonstrated here (printing all hash table entries to a BIO 169 1.1 christos that is provided by the caller): 170 1.1 christos 171 1.1 christos /* Prints item 'a' to 'output_bio' (this is implemented elsewhere) */ 172 1.1 christos void TYPE_print_doall_arg(const TYPE *a, BIO *output_bio); 173 1.1 christos 174 1.1 christos /* Implement a prototype-compatible wrapper for "TYPE_print" */ 175 1.1 christos static IMPLEMENT_LHASH_DOALL_ARG_FN(TYPE, const TYPE, BIO) 176 1.1 christos 177 1.1 christos /* Print out the entire hashtable to a particular BIO */ 178 1.1 christos lh_TYPE_doall_arg(hashtable, LHASH_DOALL_ARG_FN(TYPE_print), BIO, 179 1.1 christos logging_bio); 180 1.1 christos 181 1.1.1.5 christos Note that it is by default B<not> safe to use B<lh_I<TYPE>_delete>() inside a 182 1.1.1.5 christos callback passed to B<lh_I<TYPE>_doall>() or B<lh_I<TYPE>_doall_arg>(). The 183 1.1.1.5 christos reason for this is that deleting an item from the hash table may result in the 184 1.1.1.5 christos hash table being contracted to a smaller size and rehashed. 185 1.1.1.5 christos B<lh_I<TYPE>_doall>() and B<lh_I<TYPE>_doall_arg>() are unsafe and will exhibit 186 1.1.1.5 christos undefined behaviour under these conditions, as these functions assume the hash 187 1.1.1.5 christos table size and bucket pointers do not change during the call. 188 1.1.1.5 christos 189 1.1.1.5 christos If it is desired to use B<lh_I<TYPE>_doall>() or B<lh_I<TYPE>_doall_arg>() with 190 1.1.1.5 christos B<lh_I<TYPE>_delete>(), it is essential that you call 191 1.1.1.5 christos B<lh_I<TYPE>_set_down_load>() with a I<down_load> argument of 0 first. This 192 1.1.1.5 christos disables hash table contraction and guarantees that it will be safe to delete 193 1.1.1.5 christos items from a hash table during a call to B<lh_I<TYPE>_doall>() or 194 1.1.1.5 christos B<lh_I<TYPE>_doall_arg>(). 195 1.1.1.5 christos 196 1.1.1.5 christos It is never safe to call B<lh_I<TYPE>_insert>() during a call to 197 1.1.1.5 christos B<lh_I<TYPE>_doall>() or B<lh_I<TYPE>_doall_arg>(). 198 1.1 christos 199 1.1.1.4 christos B<lh_I<TYPE>_error>() can be used to determine if an error occurred in the last 200 1.1 christos operation. 201 1.1 christos 202 1.1.1.5 christos B<lh_I<TYPE>_num_items>() returns the number of items in the hash table. 203 1.1.1.5 christos 204 1.1.1.5 christos B<lh_I<TYPE>_get_down_load>() and B<lh_I<TYPE>_set_down_load>() get and set the 205 1.1.1.5 christos factor used to determine when the hash table is contracted. The factor is the 206 1.1.1.5 christos load factor at or below which hash table contraction will occur, multiplied by 207 1.1.1.5 christos B<LH_LOAD_MULT>, where the load factor is the number of items divided by the 208 1.1.1.5 christos number of nodes. Setting this value to 0 disables hash table contraction. 209 1.1.1.5 christos 210 1.1.1.4 christos OPENSSL_LH_new() is the same as the B<lh_I<TYPE>_new>() except that it is not 211 1.1.1.4 christos type specific. So instead of returning an B<LHASH_OF(I<TYPE>)> value it returns 212 1.1.1.4 christos a B<void *>. In the same way the functions OPENSSL_LH_free(), 213 1.1.1.4 christos OPENSSL_LH_flush(), OPENSSL_LH_insert(), OPENSSL_LH_delete(), 214 1.1.1.5 christos OPENSSL_LH_retrieve(), OPENSSL_LH_doall(), OPENSSL_LH_doall_arg(), 215 1.1.1.5 christos OPENSSL_LH_num_items(), OPENSSL_LH_get_down_load(), OPENSSL_LH_set_down_load() 216 1.1.1.5 christos and OPENSSL_LH_error() are equivalent to the similarly named B<lh_I<TYPE>> 217 1.1.1.5 christos functions except that they return or use a B<void *> where the equivalent 218 1.1.1.5 christos B<lh_I<TYPE>> function returns or uses a B<I<TYPE> *> or B<LHASH_OF(I<TYPE>) *>. 219 1.1.1.5 christos B<lh_I<TYPE>> functions are implemented as type checked wrappers around the 220 1.1.1.5 christos B<OPENSSL_LH> functions. Most applications should not call the B<OPENSSL_LH> 221 1.1.1.5 christos functions directly. 222 1.1 christos 223 1.1.1.4 christos =head1 RETURN VALUES 224 1.1 christos 225 1.1.1.4 christos B<lh_I<TYPE>_new>() and OPENSSL_LH_new() return NULL on error, otherwise a 226 1.1.1.4 christos pointer to the new B<LHASH> structure. 227 1.1 christos 228 1.1.1.4 christos When a hash table entry is replaced, B<lh_I<TYPE>_insert>() or 229 1.1.1.4 christos OPENSSL_LH_insert() return the value being replaced. NULL is returned on normal 230 1.1.1.4 christos operation and on error. 231 1.1.1.4 christos 232 1.1.1.4 christos B<lh_I<TYPE>_delete>() and OPENSSL_LH_delete() return the entry being deleted. 233 1.1.1.4 christos NULL is returned if there is no such value in the hash table. 234 1.1.1.4 christos 235 1.1.1.4 christos B<lh_I<TYPE>_retrieve>() and OPENSSL_LH_retrieve() return the hash table entry 236 1.1.1.4 christos if it has been found, NULL otherwise. 237 1.1.1.4 christos 238 1.1.1.4 christos B<lh_I<TYPE>_error>() and OPENSSL_LH_error() return 1 if an error occurred in 239 1.1.1.4 christos the last operation, 0 otherwise. It's meaningful only after non-retrieve 240 1.1.1.4 christos operations. 241 1.1.1.4 christos 242 1.1.1.4 christos B<lh_I<TYPE>_free>(), OPENSSL_LH_free(), B<lh_I<TYPE>_flush>(), 243 1.1.1.4 christos OPENSSL_LH_flush(), B<lh_I<TYPE>_doall>() OPENSSL_LH_doall(), 244 1.1.1.4 christos B<lh_I<TYPE>_doall_arg>() and OPENSSL_LH_doall_arg() return no values. 245 1.1 christos 246 1.1 christos =head1 NOTE 247 1.1 christos 248 1.1 christos The LHASH code is not thread safe. All updating operations, as well as 249 1.1.1.4 christos B<lh_I<TYPE>_error>() or OPENSSL_LH_error() calls must be performed under 250 1.1.1.4 christos a write lock. All retrieve operations should be performed under a read lock, 251 1.1.1.4 christos I<unless> accurate usage statistics are desired. In which case, a write lock 252 1.1.1.4 christos should be used for retrieve operations as well. For output of the usage 253 1.1.1.4 christos statistics, using the functions from L<OPENSSL_LH_stats(3)>, a read lock 254 1.1.1.4 christos suffices. 255 1.1 christos 256 1.1 christos The LHASH code regards table entries as constant data. As such, it 257 1.1 christos internally represents lh_insert()'d items with a "const void *" 258 1.1 christos pointer type. This is why callbacks such as those used by lh_doall() 259 1.1 christos and lh_doall_arg() declare their prototypes with "const", even for the 260 1.1 christos parameters that pass back the table items' data pointers - for 261 1.1 christos consistency, user-provided data is "const" at all times as far as the 262 1.1 christos LHASH code is concerned. However, as callers are themselves providing 263 1.1 christos these pointers, they can choose whether they too should be treating 264 1.1 christos all such parameters as constant. 265 1.1 christos 266 1.1 christos As an example, a hash table may be maintained by code that, for 267 1.1 christos reasons of encapsulation, has only "const" access to the data being 268 1.1.1.2 christos indexed in the hash table (i.e. it is returned as "const" from 269 1.1 christos elsewhere in their code) - in this case the LHASH prototypes are 270 1.1 christos appropriate as-is. Conversely, if the caller is responsible for the 271 1.1 christos life-time of the data in question, then they may well wish to make 272 1.1 christos modifications to table item passed back in the lh_doall() or 273 1.1 christos lh_doall_arg() callbacks (see the "TYPE_cleanup" example above). If 274 1.1 christos so, the caller can either cast the "const" away (if they're providing 275 1.1 christos the raw callbacks themselves) or use the macros to declare/implement 276 1.1 christos the wrapper functions without "const" types. 277 1.1 christos 278 1.1 christos Callers that only have "const" access to data they're indexing in a 279 1.1 christos table, yet declare callbacks without constant types (or cast the 280 1.1 christos "const" away themselves), are therefore creating their own risks/bugs 281 1.1 christos without being encouraged to do so by the API. On a related note, 282 1.1 christos those auditing code should pay special attention to any instances of 283 1.1 christos DECLARE/IMPLEMENT_LHASH_DOALL_[ARG_]_FN macros that provide types 284 1.1 christos without any "const" qualifiers. 285 1.1 christos 286 1.1 christos =head1 BUGS 287 1.1 christos 288 1.1.1.4 christos B<lh_I<TYPE>_insert>() and OPENSSL_LH_insert() return NULL both for success 289 1.1.1.4 christos and error. 290 1.1 christos 291 1.1 christos =head1 SEE ALSO 292 1.1 christos 293 1.1 christos L<OPENSSL_LH_stats(3)> 294 1.1 christos 295 1.1 christos =head1 HISTORY 296 1.1 christos 297 1.1 christos In OpenSSL 1.0.0, the lhash interface was revamped for better 298 1.1 christos type checking. 299 1.1 christos 300 1.1 christos =head1 COPYRIGHT 301 1.1 christos 302 1.1.1.6 christos Copyright 2000-2024 The OpenSSL Project Authors. All Rights Reserved. 303 1.1 christos 304 1.1.1.4 christos Licensed under the Apache License 2.0 (the "License"). You may not use 305 1.1 christos this file except in compliance with the License. You can obtain a copy 306 1.1 christos in the file LICENSE in the source distribution or at 307 1.1 christos L<https://www.openssl.org/source/license.html>. 308 1.1 christos 309 1.1 christos =cut 310