| Commit | Line | Data |
|---|---|---|
| e3261593 | 1 | .\" Automatically generated by Pod::Man 2.25 (Pod::Simple 3.19) |
| 8b0cefbb JR |
2 | .\" |
| 3 | .\" Standard preamble: | |
| 4 | .\" ======================================================================== | |
| 8b0cefbb | 5 | .de Sp \" Vertical space (when we can't use .PP) |
| 984263bc MD |
6 | .if t .sp .5v |
| 7 | .if n .sp | |
| 8 | .. | |
| 8b0cefbb | 9 | .de Vb \" Begin verbatim text |
| 984263bc MD |
10 | .ft CW |
| 11 | .nf | |
| 12 | .ne \\$1 | |
| 13 | .. | |
| 8b0cefbb | 14 | .de Ve \" End verbatim text |
| 984263bc | 15 | .ft R |
| 984263bc MD |
16 | .fi |
| 17 | .. | |
| 8b0cefbb JR |
18 | .\" Set up some character translations and predefined strings. \*(-- will |
| 19 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left | |
| e257b235 PA |
20 | .\" double quote, and \*(R" will give a right double quote. \*(C+ will |
| 21 | .\" give a nicer C++. Capital omega is used to do unbreakable dashes and | |
| 22 | .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, | |
| 23 | .\" nothing in troff, for use with C<>. | |
| 24 | .tr \(*W- | |
| 8b0cefbb | 25 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' |
| 984263bc | 26 | .ie n \{\ |
| 8b0cefbb JR |
27 | . ds -- \(*W- |
| 28 | . ds PI pi | |
| 29 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch | |
| 30 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch | |
| 31 | . ds L" "" | |
| 32 | . ds R" "" | |
| 33 | . ds C` "" | |
| 34 | . ds C' "" | |
| 984263bc MD |
35 | 'br\} |
| 36 | .el\{\ | |
| 8b0cefbb JR |
37 | . ds -- \|\(em\| |
| 38 | . ds PI \(*p | |
| 39 | . ds L" `` | |
| 40 | . ds R" '' | |
| 984263bc | 41 | 'br\} |
| 8b0cefbb | 42 | .\" |
| e257b235 PA |
43 | .\" Escape single quotes in literal strings from groff's Unicode transform. |
| 44 | .ie \n(.g .ds Aq \(aq | |
| 45 | .el .ds Aq ' | |
| 46 | .\" | |
| 8b0cefbb | 47 | .\" If the F register is turned on, we'll generate index entries on stderr for |
| 01185282 | 48 | .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index |
| 8b0cefbb JR |
49 | .\" entries marked with X<> in POD. Of course, you'll have to process the |
| 50 | .\" output yourself in some meaningful fashion. | |
| e257b235 | 51 | .ie \nF \{\ |
| 8b0cefbb JR |
52 | . de IX |
| 53 | . tm Index:\\$1\t\\n%\t"\\$2" | |
| 984263bc | 54 | .. |
| 8b0cefbb JR |
55 | . nr % 0 |
| 56 | . rr F | |
| 984263bc | 57 | .\} |
| e257b235 PA |
58 | .el \{\ |
| 59 | . de IX | |
| 60 | .. | |
| 61 | .\} | |
| aac4ff6f | 62 | .\" |
| 8b0cefbb JR |
63 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). |
| 64 | .\" Fear. Run. Save yourself. No user-serviceable parts. | |
| 65 | . \" fudge factors for nroff and troff | |
| 984263bc | 66 | .if n \{\ |
| 8b0cefbb JR |
67 | . ds #H 0 |
| 68 | . ds #V .8m | |
| 69 | . ds #F .3m | |
| 70 | . ds #[ \f1 | |
| 71 | . ds #] \fP | |
| 984263bc MD |
72 | .\} |
| 73 | .if t \{\ | |
| 8b0cefbb JR |
74 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) |
| 75 | . ds #V .6m | |
| 76 | . ds #F 0 | |
| 77 | . ds #[ \& | |
| 78 | . ds #] \& | |
| 984263bc | 79 | .\} |
| 8b0cefbb | 80 | . \" simple accents for nroff and troff |
| 984263bc | 81 | .if n \{\ |
| 8b0cefbb JR |
82 | . ds ' \& |
| 83 | . ds ` \& | |
| 84 | . ds ^ \& | |
| 85 | . ds , \& | |
| 86 | . ds ~ ~ | |
| 87 | . ds / | |
| 984263bc MD |
88 | .\} |
| 89 | .if t \{\ | |
| 8b0cefbb JR |
90 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" |
| 91 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' | |
| 92 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' | |
| 93 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' | |
| 94 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' | |
| 95 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' | |
| 984263bc | 96 | .\} |
| 8b0cefbb | 97 | . \" troff and (daisy-wheel) nroff accents |
| 984263bc MD |
98 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' |
| 99 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' | |
| 100 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] | |
| 101 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' | |
| 102 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' | |
| 103 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] | |
| 104 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] | |
| 105 | .ds ae a\h'-(\w'a'u*4/10)'e | |
| 106 | .ds Ae A\h'-(\w'A'u*4/10)'E | |
| 8b0cefbb | 107 | . \" corrections for vroff |
| 984263bc MD |
108 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' |
| 109 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' | |
| 8b0cefbb | 110 | . \" for low resolution devices (crt and lpr) |
| 984263bc MD |
111 | .if \n(.H>23 .if \n(.V>19 \ |
| 112 | \{\ | |
| 8b0cefbb JR |
113 | . ds : e |
| 114 | . ds 8 ss | |
| 115 | . ds o a | |
| 116 | . ds d- d\h'-1'\(ga | |
| 117 | . ds D- D\h'-1'\(hy | |
| 118 | . ds th \o'bp' | |
| 119 | . ds Th \o'LP' | |
| 120 | . ds ae ae | |
| 121 | . ds Ae AE | |
| 984263bc MD |
122 | .\} |
| 123 | .rm #[ #] #H #V #F C | |
| 8b0cefbb JR |
124 | .\" ======================================================================== |
| 125 | .\" | |
| 126 | .IX Title "lhash 3" | |
| e3261593 | 127 | .TH lhash 3 "2012-01-04" "1.0.0f" "OpenSSL" |
| e257b235 PA |
128 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 129 | .\" way too many mistakes in technical documents. | |
| 130 | .if n .ad l | |
| 131 | .nh | |
| 984263bc MD |
132 | .SH "NAME" |
| 133 | lh_new, lh_free, lh_insert, lh_delete, lh_retrieve, lh_doall, lh_doall_arg, lh_error \- dynamic hash table | |
| 134 | .SH "SYNOPSIS" | |
| 8b0cefbb | 135 | .IX Header "SYNOPSIS" |
| 984263bc MD |
136 | .Vb 1 |
| 137 | \& #include <openssl/lhash.h> | |
| e257b235 | 138 | \& |
| 01185282 PA |
139 | \& DECLARE_LHASH_OF(<type>); |
| 140 | \& | |
| 141 | \& LHASH *lh_<type>_new(); | |
| 142 | \& void lh_<type>_free(LHASH_OF(<type> *table); | |
| e257b235 | 143 | \& |
| 01185282 PA |
144 | \& <type> *lh_<type>_insert(LHASH_OF(<type> *table, <type> *data); |
| 145 | \& <type> *lh_<type>_delete(LHASH_OF(<type> *table, <type> *data); | |
| 146 | \& <type> *lh_retrieve(LHASH_OF<type> *table, <type> *data); | |
| e257b235 | 147 | \& |
| 01185282 PA |
148 | \& void lh_<type>_doall(LHASH_OF(<type> *table, LHASH_DOALL_FN_TYPE func); |
| 149 | \& void lh_<type>_doall_arg(LHASH_OF(<type> *table, LHASH_DOALL_ARG_FN_TYPE func, | |
| 150 | \& <type2>, <type2> *arg); | |
| e257b235 | 151 | \& |
| 01185282 | 152 | \& int lh_<type>_error(LHASH_OF(<type> *table); |
| e257b235 | 153 | \& |
| 984263bc MD |
154 | \& typedef int (*LHASH_COMP_FN_TYPE)(const void *, const void *); |
| 155 | \& typedef unsigned long (*LHASH_HASH_FN_TYPE)(const void *); | |
| 156 | \& typedef void (*LHASH_DOALL_FN_TYPE)(const void *); | |
| 157 | \& typedef void (*LHASH_DOALL_ARG_FN_TYPE)(const void *, const void *); | |
| 158 | .Ve | |
| 159 | .SH "DESCRIPTION" | |
| 8b0cefbb | 160 | .IX Header "DESCRIPTION" |
| 01185282 PA |
161 | This library implements type-checked dynamic hash tables. The hash |
| 162 | table entries can be arbitrary structures. Usually they consist of key | |
| 163 | and value fields. | |
| 164 | .PP | |
| 165 | lh_<type>\fI_new()\fR creates a new \fB\s-1LHASH_OF\s0(<type\fR> structure to store | |
| 166 | arbitrary data entries, and provides the 'hash' and 'compare' | |
| 167 | callbacks to be used in organising the table's entries. The \fBhash\fR | |
| 168 | callback takes a pointer to a table entry as its argument and returns | |
| 169 | an unsigned long hash value for its key field. The hash value is | |
| 170 | normally truncated to a power of 2, so make sure that your hash | |
| 171 | function returns well mixed low order bits. The \fBcompare\fR callback | |
| 172 | takes two arguments (pointers to two hash table entries), and returns | |
| 173 | 0 if their keys are equal, non-zero otherwise. If your hash table | |
| 174 | will contain items of some particular type and the \fBhash\fR and | |
| 175 | \&\fBcompare\fR callbacks hash/compare these types, then the | |
| 176 | \&\fB\s-1DECLARE_LHASH_HASH_FN\s0\fR and \fB\s-1IMPLEMENT_LHASH_COMP_FN\s0\fR macros can be | |
| 177 | used to create callback wrappers of the prototypes required by | |
| 178 | lh_<type>\fI_new()\fR. These provide per-variable casts before calling the | |
| 179 | type-specific callbacks written by the application author. These | |
| 180 | macros, as well as those used for the \*(L"doall\*(R" callbacks, are defined | |
| 181 | as; | |
| 984263bc MD |
182 | .PP |
| 183 | .Vb 7 | |
| 01185282 PA |
184 | \& #define DECLARE_LHASH_HASH_FN(name, o_type) \e |
| 185 | \& unsigned long name##_LHASH_HASH(const void *); | |
| 186 | \& #define IMPLEMENT_LHASH_HASH_FN(name, o_type) \e | |
| 187 | \& unsigned long name##_LHASH_HASH(const void *arg) { \e | |
| 188 | \& const o_type *a = arg; \e | |
| 189 | \& return name##_hash(a); } | |
| 190 | \& #define LHASH_HASH_FN(name) name##_LHASH_HASH | |
| e257b235 | 191 | \& |
| 01185282 PA |
192 | \& #define DECLARE_LHASH_COMP_FN(name, o_type) \e |
| 193 | \& int name##_LHASH_COMP(const void *, const void *); | |
| 194 | \& #define IMPLEMENT_LHASH_COMP_FN(name, o_type) \e | |
| 195 | \& int name##_LHASH_COMP(const void *arg1, const void *arg2) { \e | |
| 196 | \& const o_type *a = arg1; \e | |
| 197 | \& const o_type *b = arg2; \e | |
| 198 | \& return name##_cmp(a,b); } | |
| 199 | \& #define LHASH_COMP_FN(name) name##_LHASH_COMP | |
| e257b235 | 200 | \& |
| 01185282 PA |
201 | \& #define DECLARE_LHASH_DOALL_FN(name, o_type) \e |
| 202 | \& void name##_LHASH_DOALL(void *); | |
| 203 | \& #define IMPLEMENT_LHASH_DOALL_FN(name, o_type) \e | |
| 204 | \& void name##_LHASH_DOALL(void *arg) { \e | |
| 205 | \& o_type *a = arg; \e | |
| 206 | \& name##_doall(a); } | |
| 207 | \& #define LHASH_DOALL_FN(name) name##_LHASH_DOALL | |
| 208 | \& | |
| 209 | \& #define DECLARE_LHASH_DOALL_ARG_FN(name, o_type, a_type) \e | |
| 210 | \& void name##_LHASH_DOALL_ARG(void *, void *); | |
| 211 | \& #define IMPLEMENT_LHASH_DOALL_ARG_FN(name, o_type, a_type) \e | |
| 212 | \& void name##_LHASH_DOALL_ARG(void *arg1, void *arg2) { \e | |
| 213 | \& o_type *a = arg1; \e | |
| 214 | \& a_type *b = arg2; \e | |
| 215 | \& name##_doall_arg(a, b); } | |
| 216 | \& #define LHASH_DOALL_ARG_FN(name) name##_LHASH_DOALL_ARG | |
| 217 | \& | |
| 218 | \& An example of a hash table storing (pointers to) structures of type \*(AqSTUFF\*(Aq | |
| 219 | \& could be defined as follows; | |
| e257b235 | 220 | \& |
| e257b235 | 221 | \& /* Calculates the hash value of \*(Aqtohash\*(Aq (implemented elsewhere) */ |
| 984263bc | 222 | \& unsigned long STUFF_hash(const STUFF *tohash); |
| e257b235 | 223 | \& /* Orders \*(Aqarg1\*(Aq and \*(Aqarg2\*(Aq (implemented elsewhere) */ |
| 01185282 | 224 | \& int stuff_cmp(const STUFF *arg1, const STUFF *arg2); |
| e257b235 | 225 | \& /* Create the type\-safe wrapper functions for use in the LHASH internals */ |
| 01185282 PA |
226 | \& static IMPLEMENT_LHASH_HASH_FN(stuff, STUFF); |
| 227 | \& static IMPLEMENT_LHASH_COMP_FN(stuff, STUFF); | |
| 984263bc MD |
228 | \& /* ... */ |
| 229 | \& int main(int argc, char *argv[]) { | |
| 230 | \& /* Create the new hash table using the hash/compare wrappers */ | |
| 01185282 | 231 | \& LHASH_OF(STUFF) *hashtable = lh_STUFF_new(LHASH_HASH_FN(STUFF_hash), |
| 984263bc MD |
232 | \& LHASH_COMP_FN(STUFF_cmp)); |
| 233 | \& /* ... */ | |
| 234 | \& } | |
| 235 | .Ve | |
| 8b0cefbb | 236 | .PP |
| 01185282 PA |
237 | lh_<type>\fI_free()\fR frees the \fB\s-1LHASH_OF\s0(<type\fR> structure |
| 238 | \&\fBtable\fR. Allocated hash table entries will not be freed; consider | |
| 239 | using lh_<type>\fI_doall()\fR to deallocate any remaining entries in the | |
| 240 | hash table (see below). | |
| 984263bc | 241 | .PP |
| 01185282 PA |
242 | lh_<type>\fI_insert()\fR inserts the structure pointed to by \fBdata\fR into |
| 243 | \&\fBtable\fR. If there already is an entry with the same key, the old | |
| 244 | value is replaced. Note that lh_<type>\fI_insert()\fR stores pointers, the | |
| 245 | data are not copied. | |
| 984263bc | 246 | .PP |
| 01185282 | 247 | lh_<type>\fI_delete()\fR deletes an entry from \fBtable\fR. |
| 984263bc | 248 | .PP |
| 01185282 PA |
249 | lh_<type>\fI_retrieve()\fR looks up an entry in \fBtable\fR. Normally, \fBdata\fR |
| 250 | is a structure with the key field(s) set; the function will return a | |
| 984263bc MD |
251 | pointer to a fully populated structure. |
| 252 | .PP | |
| 01185282 PA |
253 | lh_<type>\fI_doall()\fR will, for every entry in the hash table, call |
| 254 | \&\fBfunc\fR with the data item as its parameter. For lh_<type>\fI_doall()\fR | |
| 255 | and lh_<type>\fI_doall_arg()\fR, function pointer casting should be avoided | |
| 256 | in the callbacks (see \fB\s-1NOTE\s0\fR) \- instead use the declare/implement | |
| 257 | macros to create type-checked wrappers that cast variables prior to | |
| 258 | calling your type-specific callbacks. An example of this is | |
| 259 | illustrated here where the callback is used to cleanup resources for | |
| 260 | items in the hash table prior to the hashtable itself being | |
| 261 | deallocated: | |
| 984263bc MD |
262 | .PP |
| 263 | .Vb 9 | |
| e257b235 | 264 | \& /* Cleans up resources belonging to \*(Aqa\*(Aq (this is implemented elsewhere) */ |
| 01185282 | 265 | \& void STUFF_cleanup_doall(STUFF *a); |
| e257b235 | 266 | \& /* Implement a prototype\-compatible wrapper for "STUFF_cleanup" */ |
| 01185282 | 267 | \& IMPLEMENT_LHASH_DOALL_FN(STUFF_cleanup, STUFF) |
| 984263bc MD |
268 | \& /* ... then later in the code ... */ |
| 269 | \& /* So to run "STUFF_cleanup" against all items in a hash table ... */ | |
| 01185282 | 270 | \& lh_STUFF_doall(hashtable, LHASH_DOALL_FN(STUFF_cleanup)); |
| 984263bc | 271 | \& /* Then the hash table itself can be deallocated */ |
| 01185282 | 272 | \& lh_STUFF_free(hashtable); |
| 984263bc | 273 | .Ve |
| 8b0cefbb | 274 | .PP |
| 984263bc MD |
275 | When doing this, be careful if you delete entries from the hash table |
| 276 | in your callbacks: the table may decrease in size, moving the item | |
| 277 | that you are currently on down lower in the hash table \- this could | |
| 278 | cause some entries to be skipped during the iteration. The second | |
| 8b0cefbb | 279 | best solution to this problem is to set hash\->down_load=0 before |
| 984263bc MD |
280 | you start (which will stop the hash table ever decreasing in size). |
| 281 | The best solution is probably to avoid deleting items from the hash | |
| 282 | table inside a \*(L"doall\*(R" callback! | |
| 283 | .PP | |
| 01185282 PA |
284 | lh_<type>\fI_doall_arg()\fR is the same as lh_<type>\fI_doall()\fR except that |
| 285 | \&\fBfunc\fR will be called with \fBarg\fR as the second argument and \fBfunc\fR | |
| 286 | should be of type \fB\s-1LHASH_DOALL_ARG_FN_TYPE\s0\fR (a callback prototype | |
| 287 | that is passed both the table entry and an extra argument). As with | |
| 288 | \&\fIlh_doall()\fR, you can instead choose to declare your callback with a | |
| 289 | prototype matching the types you are dealing with and use the | |
| 290 | declare/implement macros to create compatible wrappers that cast | |
| 291 | variables before calling your type-specific callbacks. An example of | |
| 292 | this is demonstrated here (printing all hash table entries to a \s-1BIO\s0 | |
| 293 | that is provided by the caller): | |
| 294 | .PP | |
| 295 | .Vb 8 | |
| e257b235 | 296 | \& /* Prints item \*(Aqa\*(Aq to \*(Aqoutput_bio\*(Aq (this is implemented elsewhere) */ |
| 01185282 | 297 | \& void STUFF_print_doall_arg(const STUFF *a, BIO *output_bio); |
| e257b235 | 298 | \& /* Implement a prototype\-compatible wrapper for "STUFF_print" */ |
| 01185282 | 299 | \& static IMPLEMENT_LHASH_DOALL_ARG_FN(STUFF, const STUFF, BIO) |
| 984263bc MD |
300 | \& /* ... then later in the code ... */ |
| 301 | \& /* Print out the entire hashtable to a particular BIO */ | |
| 01185282 PA |
302 | \& lh_STUFF_doall_arg(hashtable, LHASH_DOALL_ARG_FN(STUFF_print), BIO, |
| 303 | \& logging_bio); | |
| 984263bc | 304 | .Ve |
| 8b0cefbb | 305 | .PP |
| 01185282 PA |
306 | lh_<type>\fI_error()\fR can be used to determine if an error occurred in the last |
| 307 | operation. lh_<type>\fI_error()\fR is a macro. | |
| 984263bc | 308 | .SH "RETURN VALUES" |
| 8b0cefbb | 309 | .IX Header "RETURN VALUES" |
| 01185282 | 310 | lh_<type>\fI_new()\fR returns \fB\s-1NULL\s0\fR on error, otherwise a pointer to the new |
| 8b0cefbb | 311 | \&\fB\s-1LHASH\s0\fR structure. |
| 984263bc | 312 | .PP |
| 01185282 | 313 | When a hash table entry is replaced, lh_<type>\fI_insert()\fR returns the value |
| 8b0cefbb | 314 | being replaced. \fB\s-1NULL\s0\fR is returned on normal operation and on error. |
| 984263bc | 315 | .PP |
| 01185282 | 316 | lh_<type>\fI_delete()\fR returns the entry being deleted. \fB\s-1NULL\s0\fR is returned if |
| 984263bc MD |
317 | there is no such value in the hash table. |
| 318 | .PP | |
| 01185282 | 319 | lh_<type>\fI_retrieve()\fR returns the hash table entry if it has been found, |
| 8b0cefbb | 320 | \&\fB\s-1NULL\s0\fR otherwise. |
| 984263bc | 321 | .PP |
| 01185282 | 322 | lh_<type>\fI_error()\fR returns 1 if an error occurred in the last operation, 0 |
| 984263bc MD |
323 | otherwise. |
| 324 | .PP | |
| 01185282 | 325 | lh_<type>\fI_free()\fR, lh_<type>\fI_doall()\fR and lh_<type>\fI_doall_arg()\fR return no values. |
| 984263bc | 326 | .SH "NOTE" |
| 8b0cefbb JR |
327 | .IX Header "NOTE" |
| 328 | The various \s-1LHASH\s0 macros and callback types exist to make it possible | |
| 01185282 | 329 | to write type-checked code without resorting to function-prototype |
| 984263bc MD |
330 | casting \- an evil that makes application code much harder to |
| 331 | audit/verify and also opens the window of opportunity for stack | |
| 332 | corruption and other hard-to-find bugs. It also, apparently, violates | |
| e257b235 | 333 | ANSI-C. |
| 984263bc | 334 | .PP |
| 8b0cefbb | 335 | The \s-1LHASH\s0 code regards table entries as constant data. As such, it |
| 984263bc MD |
336 | internally represents \fIlh_insert()\fR'd items with a \*(L"const void *\*(R" |
| 337 | pointer type. This is why callbacks such as those used by \fIlh_doall()\fR | |
| 338 | and \fIlh_doall_arg()\fR declare their prototypes with \*(L"const\*(R", even for the | |
| 8b0cefbb | 339 | parameters that pass back the table items' data pointers \- for |
| 984263bc | 340 | consistency, user-provided data is \*(L"const\*(R" at all times as far as the |
| 8b0cefbb | 341 | \&\s-1LHASH\s0 code is concerned. However, as callers are themselves providing |
| 984263bc MD |
342 | these pointers, they can choose whether they too should be treating |
| 343 | all such parameters as constant. | |
| 344 | .PP | |
| 345 | As an example, a hash table may be maintained by code that, for | |
| 346 | reasons of encapsulation, has only \*(L"const\*(R" access to the data being | |
| 347 | indexed in the hash table (ie. it is returned as \*(L"const\*(R" from | |
| 8b0cefbb | 348 | elsewhere in their code) \- in this case the \s-1LHASH\s0 prototypes are |
| e257b235 | 349 | appropriate as-is. Conversely, if the caller is responsible for the |
| 984263bc MD |
350 | life-time of the data in question, then they may well wish to make |
| 351 | modifications to table item passed back in the \fIlh_doall()\fR or | |
| 8b0cefbb | 352 | \&\fIlh_doall_arg()\fR callbacks (see the \*(L"STUFF_cleanup\*(R" example above). If |
| 984263bc MD |
353 | so, the caller can either cast the \*(L"const\*(R" away (if they're providing |
| 354 | the raw callbacks themselves) or use the macros to declare/implement | |
| 355 | the wrapper functions without \*(L"const\*(R" types. | |
| 356 | .PP | |
| 357 | Callers that only have \*(L"const\*(R" access to data they're indexing in a | |
| 358 | table, yet declare callbacks without constant types (or cast the | |
| 8b0cefbb JR |
359 | \&\*(L"const\*(R" away themselves), are therefore creating their own risks/bugs |
| 360 | without being encouraged to do so by the \s-1API\s0. On a related note, | |
| 984263bc | 361 | those auditing code should pay special attention to any instances of |
| 8b0cefbb | 362 | DECLARE/IMPLEMENT_LHASH_DOALL_[\s-1ARG_\s0]_FN macros that provide types |
| 984263bc MD |
363 | without any \*(L"const\*(R" qualifiers. |
| 364 | .SH "BUGS" | |
| 8b0cefbb | 365 | .IX Header "BUGS" |
| 01185282 | 366 | lh_<type>\fI_insert()\fR returns \fB\s-1NULL\s0\fR both for success and error. |
| 984263bc | 367 | .SH "INTERNALS" |
| 8b0cefbb | 368 | .IX Header "INTERNALS" |
| 984263bc MD |
369 | The following description is based on the SSLeay documentation: |
| 370 | .PP | |
| 371 | The \fBlhash\fR library implements a hash table described in the | |
| 8b0cefbb | 372 | \&\fICommunications of the \s-1ACM\s0\fR in 1991. What makes this hash table |
| 984263bc | 373 | different is that as the table fills, the hash table is increased (or |
| 8b0cefbb JR |
374 | decreased) in size via \fIOPENSSL_realloc()\fR. When a 'resize' is done, instead of |
| 375 | all hashes being redistributed over twice as many 'buckets', one | |
| 376 | bucket is split. So when an 'expand' is done, there is only a minimal | |
| 984263bc | 377 | cost to redistribute some values. Subsequent inserts will cause more |
| 8b0cefbb JR |
378 | single 'bucket' redistributions but there will never be a sudden large |
| 379 | cost due to redistributing all the 'buckets'. | |
| 984263bc | 380 | .PP |
| 8b0cefbb | 381 | The state for a particular hash table is kept in the \fB\s-1LHASH\s0\fR structure. |
| 984263bc | 382 | The decision to increase or decrease the hash table size is made |
| 8b0cefbb | 383 | depending on the 'load' of the hash table. The load is the number of |
| 984263bc | 384 | items in the hash table divided by the size of the hash table. The |
| 8b0cefbb JR |
385 | default values are as follows. If (hash\->up_load < load) => |
| 386 | expand. if (hash\->down_load > load) => contract. The | |
| 387 | \&\fBup_load\fR has a default value of 1 and \fBdown_load\fR has a default value | |
| 984263bc | 388 | of 2. These numbers can be modified by the application by just |
| 8b0cefbb | 389 | playing with the \fBup_load\fR and \fBdown_load\fR variables. The 'load' is |
| 984263bc | 390 | kept in a form which is multiplied by 256. So |
| 8b0cefbb | 391 | hash\->up_load=8*256; will cause a load of 8 to be set. |
| 984263bc MD |
392 | .PP |
| 393 | If you are interested in performance the field to watch is | |
| 8b0cefbb JR |
394 | num_comp_calls. The hash library keeps track of the 'hash' value for |
| 395 | each item so when a lookup is done, the 'hashes' are compared, if | |
| 984263bc | 396 | there is a match, then a full compare is done, and |
| 8b0cefbb | 397 | hash\->num_comp_calls is incremented. If num_comp_calls is not equal |
| 984263bc MD |
398 | to num_delete plus num_retrieve it means that your hash function is |
| 399 | generating hashes that are the same for different values. It is | |
| 400 | probably worth changing your hash function if this is the case because | |
| 8b0cefbb | 401 | even if your hash table has 10 items in a 'bucket', it can be searched |
| 984263bc MD |
402 | with 10 \fBunsigned long\fR compares and 10 linked list traverses. This |
| 403 | will be much less expensive that 10 calls to your compare function. | |
| 404 | .PP | |
| 8b0cefbb | 405 | \&\fIlh_strhash()\fR is a demo string hashing function: |
| 984263bc MD |
406 | .PP |
| 407 | .Vb 1 | |
| 408 | \& unsigned long lh_strhash(const char *c); | |
| 409 | .Ve | |
| 8b0cefbb JR |
410 | .PP |
| 411 | Since the \fB\s-1LHASH\s0\fR routines would normally be passed structures, this | |
| 01185282 PA |
412 | routine would not normally be passed to lh_<type>\fI_new()\fR, rather it would be |
| 413 | used in the function passed to lh_<type>\fI_new()\fR. | |
| 984263bc | 414 | .SH "SEE ALSO" |
| 8b0cefbb JR |
415 | .IX Header "SEE ALSO" |
| 416 | \&\fIlh_stats\fR\|(3) | |
| 984263bc | 417 | .SH "HISTORY" |
| 8b0cefbb | 418 | .IX Header "HISTORY" |
| 984263bc | 419 | The \fBlhash\fR library is available in all versions of SSLeay and OpenSSL. |
| 8b0cefbb | 420 | \&\fIlh_error()\fR was added in SSLeay 0.9.1b. |
| 984263bc MD |
421 | .PP |
| 422 | This manpage is derived from the SSLeay documentation. | |
| 423 | .PP | |
| 424 | In OpenSSL 0.9.7, all lhash functions that were passed function pointers | |
| 8b0cefbb JR |
425 | were changed for better type safety, and the function types \s-1LHASH_COMP_FN_TYPE\s0, |
| 426 | \&\s-1LHASH_HASH_FN_TYPE\s0, \s-1LHASH_DOALL_FN_TYPE\s0 and \s-1LHASH_DOALL_ARG_FN_TYPE\s0 | |
| 984263bc | 427 | became available. |
| 01185282 PA |
428 | .PP |
| 429 | In OpenSSL 1.0.0, the lhash interface was revamped for even better | |
| 430 | type checking. |