lhash.3 (100947) | lhash.3 (110010) |
---|---|
1.\" Automatically generated by Pod::Man version 1.15 | 1.\" Automatically generated by Pod::Man version 1.15 |
2.\" Tue Jul 30 09:22:07 2002 | 2.\" Mon Jan 13 19:29:23 2003 |
3.\" 4.\" Standard preamble: 5.\" ====================================================================== 6.de Sh \" Subsection heading 7.br 8.if t .Sp 9.ne 5 10.PP --- 122 unchanged lines hidden (view full) --- 133. ds Th \o'LP' 134. ds ae ae 135. ds Ae AE 136.\} 137.rm #[ #] #H #V #F C 138.\" ====================================================================== 139.\" 140.IX Title "lhash 3" | 3.\" 4.\" Standard preamble: 5.\" ====================================================================== 6.de Sh \" Subsection heading 7.br 8.if t .Sp 9.ne 5 10.PP --- 122 unchanged lines hidden (view full) --- 133. ds Th \o'LP' 134. ds ae ae 135. ds Ae AE 136.\} 137.rm #[ #] #H #V #F C 138.\" ====================================================================== 139.\" 140.IX Title "lhash 3" |
141.TH lhash 3 "0.9.6e" "2000-11-12" "OpenSSL" | 141.TH lhash 3 "0.9.7" "2003-01-13" "OpenSSL" |
142.UC 143.SH "NAME" | 142.UC 143.SH "NAME" |
144lh_new, lh_free, lh_insert, lh_delete, lh_retrieve, lh_doall, 145lh_doall_arg, lh_error \- dynamic hash table | 144lh_new, lh_free, lh_insert, lh_delete, lh_retrieve, lh_doall, lh_doall_arg, lh_error \- dynamic hash table |
146.SH "SYNOPSIS" 147.IX Header "SYNOPSIS" 148.Vb 1 149\& #include <openssl/lhash.h> 150.Ve | 145.SH "SYNOPSIS" 146.IX Header "SYNOPSIS" 147.Vb 1 148\& #include <openssl/lhash.h> 149.Ve |
151.Vb 3 152\& LHASH *lh_new(unsigned long (*hash)(/*void *a*/), 153\& int (*compare)(/*void *a,void *b*/)); | 150.Vb 2 151\& LHASH *lh_new(LHASH_HASH_FN_TYPE hash, LHASH_COMP_FN_TYPE compare); |
154\& void lh_free(LHASH *table); 155.Ve 156.Vb 3 157\& void *lh_insert(LHASH *table, void *data); 158\& void *lh_delete(LHASH *table, void *data); 159\& void *lh_retrieve(LHASH *table, void *data); 160.Ve 161.Vb 3 | 152\& void lh_free(LHASH *table); 153.Ve 154.Vb 3 155\& void *lh_insert(LHASH *table, void *data); 156\& void *lh_delete(LHASH *table, void *data); 157\& void *lh_retrieve(LHASH *table, void *data); 158.Ve 159.Vb 3 |
162\& void lh_doall(LHASH *table, void (*func)(/*void *b*/)); 163\& void lh_doall_arg(LHASH *table, void (*func)(/*void *a,void *b*/), | 160\& void lh_doall(LHASH *table, LHASH_DOALL_FN_TYPE func); 161\& void lh_doall_arg(LHASH *table, LHASH_DOALL_ARG_FN_TYPE func, |
164\& void *arg); 165.Ve 166.Vb 1 167\& int lh_error(LHASH *table); 168.Ve | 162\& void *arg); 163.Ve 164.Vb 1 165\& int lh_error(LHASH *table); 166.Ve |
167.Vb 4 168\& typedef int (*LHASH_COMP_FN_TYPE)(const void *, const void *); 169\& typedef unsigned long (*LHASH_HASH_FN_TYPE)(const void *); 170\& typedef void (*LHASH_DOALL_FN_TYPE)(const void *); 171\& typedef void (*LHASH_DOALL_ARG_FN_TYPE)(const void *, const void *); 172.Ve |
|
169.SH "DESCRIPTION" 170.IX Header "DESCRIPTION" 171This library implements dynamic hash tables. The hash table entries 172can be arbitrary structures. Usually they consist of key and value 173fields. 174.PP | 173.SH "DESCRIPTION" 174.IX Header "DESCRIPTION" 175This library implements dynamic hash tables. The hash table entries 176can be arbitrary structures. Usually they consist of key and value 177fields. 178.PP |
175\&\fIlh_new()\fR creates a new \fB\s-1LHASH\s0\fR structure. \fBhash\fR takes a pointer to 176the structure and returns an unsigned long hash value of its key 177field. The hash value is normally truncated to a power of 2, so make 178sure that your hash function returns well mixed low order 179bits. \fBcompare\fR takes two arguments, and returns 0 if their keys are 180equal, non-zero otherwise. | 179\&\fIlh_new()\fR creates a new \fB\s-1LHASH\s0\fR structure to store arbitrary data 180entries, and provides the 'hash' and 'compare' callbacks to be used in 181organising the table's entries. The \fBhash\fR callback takes a pointer 182to a table entry as its argument and returns an unsigned long hash 183value for its key field. The hash value is normally truncated to a 184power of 2, so make sure that your hash function returns well mixed 185low order bits. The \fBcompare\fR callback takes two arguments (pointers 186to two hash table entries), and returns 0 if their keys are equal, 187non-zero otherwise. If your hash table will contain items of some 188particular type and the \fBhash\fR and \fBcompare\fR callbacks hash/compare 189these types, then the \fB\s-1DECLARE_LHASH_HASH_FN\s0\fR and 190\&\fB\s-1IMPLEMENT_LHASH_COMP_FN\s0\fR macros can be used to create callback 191wrappers of the prototypes required by \fIlh_new()\fR. These provide 192per-variable casts before calling the type-specific callbacks written 193by the application author. These macros, as well as those used for 194the \*(L"doall\*(R" callbacks, are defined as; |
181.PP | 195.PP |
196.Vb 7 197\& #define DECLARE_LHASH_HASH_FN(f_name,o_type) \e 198\& unsigned long f_name##_LHASH_HASH(const void *); 199\& #define IMPLEMENT_LHASH_HASH_FN(f_name,o_type) \e 200\& unsigned long f_name##_LHASH_HASH(const void *arg) { \e 201\& o_type a = (o_type)arg; \e 202\& return f_name(a); } 203\& #define LHASH_HASH_FN(f_name) f_name##_LHASH_HASH 204.Ve 205.Vb 8 206\& #define DECLARE_LHASH_COMP_FN(f_name,o_type) \e 207\& int f_name##_LHASH_COMP(const void *, const void *); 208\& #define IMPLEMENT_LHASH_COMP_FN(f_name,o_type) \e 209\& int f_name##_LHASH_COMP(const void *arg1, const void *arg2) { \e 210\& o_type a = (o_type)arg1; \e 211\& o_type b = (o_type)arg2; \e 212\& return f_name(a,b); } 213\& #define LHASH_COMP_FN(f_name) f_name##_LHASH_COMP 214.Ve 215.Vb 7 216\& #define DECLARE_LHASH_DOALL_FN(f_name,o_type) \e 217\& void f_name##_LHASH_DOALL(const void *); 218\& #define IMPLEMENT_LHASH_DOALL_FN(f_name,o_type) \e 219\& void f_name##_LHASH_DOALL(const void *arg) { \e 220\& o_type a = (o_type)arg; \e 221\& f_name(a); } 222\& #define LHASH_DOALL_FN(f_name) f_name##_LHASH_DOALL 223.Ve 224.Vb 8 225\& #define DECLARE_LHASH_DOALL_ARG_FN(f_name,o_type,a_type) \e 226\& void f_name##_LHASH_DOALL_ARG(const void *, const void *); 227\& #define IMPLEMENT_LHASH_DOALL_ARG_FN(f_name,o_type,a_type) \e 228\& void f_name##_LHASH_DOALL_ARG(const void *arg1, const void *arg2) { \e 229\& o_type a = (o_type)arg1; \e 230\& a_type b = (a_type)arg2; \e 231\& f_name(a,b); } 232\& #define LHASH_DOALL_ARG_FN(f_name) f_name##_LHASH_DOALL_ARG 233.Ve 234An example of a hash table storing (pointers to) structures of type '\s-1STUFF\s0' 235could be defined as follows; 236.PP 237.Vb 14 238\& /* Calculates the hash value of 'tohash' (implemented elsewhere) */ 239\& unsigned long STUFF_hash(const STUFF *tohash); 240\& /* Orders 'arg1' and 'arg2' (implemented elsewhere) */ 241\& int STUFF_cmp(const STUFF *arg1, const STUFF *arg2); 242\& /* Create the type-safe wrapper functions for use in the LHASH internals */ 243\& static IMPLEMENT_LHASH_HASH_FN(STUFF_hash, const STUFF *) 244\& static IMPLEMENT_LHASH_COMP_FN(STUFF_cmp, const STUFF *); 245\& /* ... */ 246\& int main(int argc, char *argv[]) { 247\& /* Create the new hash table using the hash/compare wrappers */ 248\& LHASH *hashtable = lh_new(LHASH_HASH_FN(STUFF_hash), 249\& LHASH_COMP_FN(STUFF_cmp)); 250\& /* ... */ 251\& } 252.Ve |
|
182\&\fIlh_free()\fR frees the \fB\s-1LHASH\s0\fR structure \fBtable\fR. Allocated hash table 183entries will not be freed; consider using \fIlh_doall()\fR to deallocate any | 253\&\fIlh_free()\fR frees the \fB\s-1LHASH\s0\fR structure \fBtable\fR. Allocated hash table 254entries will not be freed; consider using \fIlh_doall()\fR to deallocate any |
184remaining entries in the hash table. | 255remaining entries in the hash table (see below). |
185.PP 186\&\fIlh_insert()\fR inserts the structure pointed to by \fBdata\fR into \fBtable\fR. 187If there already is an entry with the same key, the old value is 188replaced. Note that \fIlh_insert()\fR stores pointers, the data are not 189copied. 190.PP 191\&\fIlh_delete()\fR deletes an entry from \fBtable\fR. 192.PP 193\&\fIlh_retrieve()\fR looks up an entry in \fBtable\fR. Normally, \fBdata\fR is 194a structure with the key \fIfield\fR\|(s) set; the function will return a 195pointer to a fully populated structure. 196.PP 197\&\fIlh_doall()\fR will, for every entry in the hash table, call \fBfunc\fR with | 256.PP 257\&\fIlh_insert()\fR inserts the structure pointed to by \fBdata\fR into \fBtable\fR. 258If there already is an entry with the same key, the old value is 259replaced. Note that \fIlh_insert()\fR stores pointers, the data are not 260copied. 261.PP 262\&\fIlh_delete()\fR deletes an entry from \fBtable\fR. 263.PP 264\&\fIlh_retrieve()\fR looks up an entry in \fBtable\fR. Normally, \fBdata\fR is 265a structure with the key \fIfield\fR\|(s) set; the function will return a 266pointer to a fully populated structure. 267.PP 268\&\fIlh_doall()\fR will, for every entry in the hash table, call \fBfunc\fR with |
198the data item as parameters. 199This function can be quite useful when used as follows: 200 void cleanup(\s-1STUFF\s0 *a) 201 { \fISTUFF_free\fR\|(a); } 202 lh_doall(hash,cleanup); 203 lh_free(hash); 204This can be used to free all the entries. \fIlh_free()\fR then cleans up the 205\&'buckets' that point to nothing. When doing this, be careful if you 206delete entries from the hash table in \fBfunc\fR: the table may decrease 207in size, moving item that you are currently on down lower in the hash 208table. This could cause some entries to be skipped. The best 209solution to this problem is to set hash->down_load=0 before you 210start. This will stop the hash table ever being decreased in size. | 269the data item as its parameter. For \fIlh_doall()\fR and \fIlh_doall_arg()\fR, 270function pointer casting should be avoided in the callbacks (see 271\&\fB\s-1NOTE\s0\fR) \- instead, either declare the callbacks to match the 272prototype required in \fIlh_new()\fR or use the declare/implement macros to 273create type-safe wrappers that cast variables prior to calling your 274type-specific callbacks. An example of this is illustrated here where 275the callback is used to cleanup resources for items in the hash table 276prior to the hashtable itself being deallocated: |
211.PP | 277.PP |
212\&\fIlh_doall_arg()\fR is the same as \fIlh_doall()\fR except that \fBfunc\fR will 213be called with \fBarg\fR as the second argument. | 278.Vb 9 279\& /* Cleans up resources belonging to 'a' (this is implemented elsewhere) */ 280\& void STUFF_cleanup(STUFF *a); 281\& /* Implement a prototype-compatible wrapper for "STUFF_cleanup" */ 282\& IMPLEMENT_LHASH_DOALL_FN(STUFF_cleanup, STUFF *) 283\& /* ... then later in the code ... */ 284\& /* So to run "STUFF_cleanup" against all items in a hash table ... */ 285\& lh_doall(hashtable, LHASH_DOALL_FN(STUFF_cleanup)); 286\& /* Then the hash table itself can be deallocated */ 287\& lh_free(hashtable); 288.Ve 289When doing this, be careful if you delete entries from the hash table 290in your callbacks: the table may decrease in size, moving the item 291that you are currently on down lower in the hash table \- this could 292cause some entries to be skipped during the iteration. The second 293best solution to this problem is to set hash->down_load=0 before 294you start (which will stop the hash table ever decreasing in size). 295The best solution is probably to avoid deleting items from the hash 296table inside a \*(L"doall\*(R" callback! |
214.PP | 297.PP |
298\&\fIlh_doall_arg()\fR is the same as \fIlh_doall()\fR except that \fBfunc\fR will be 299called with \fBarg\fR as the second argument and \fBfunc\fR should be of 300type \fB\s-1LHASH_DOALL_ARG_FN_TYPE\s0\fR (a callback prototype that is passed 301both the table entry and an extra argument). As with \fIlh_doall()\fR, you 302can instead choose to declare your callback with a prototype matching 303the types you are dealing with and use the declare/implement macros to 304create compatible wrappers that cast variables before calling your 305type-specific callbacks. An example of this is demonstrated here 306(printing all hash table entries to a \s-1BIO\s0 that is provided by the 307caller): 308.PP 309.Vb 7 310\& /* Prints item 'a' to 'output_bio' (this is implemented elsewhere) */ 311\& void STUFF_print(const STUFF *a, BIO *output_bio); 312\& /* Implement a prototype-compatible wrapper for "STUFF_print" */ 313\& static IMPLEMENT_LHASH_DOALL_ARG_FN(STUFF_print, const STUFF *, BIO *) 314\& /* ... then later in the code ... */ 315\& /* Print out the entire hashtable to a particular BIO */ 316\& lh_doall_arg(hashtable, LHASH_DOALL_ARG_FN(STUFF_print), logging_bio); 317.Ve |
|
215\&\fIlh_error()\fR can be used to determine if an error occurred in the last 216operation. \fIlh_error()\fR is a macro. 217.SH "RETURN VALUES" 218.IX Header "RETURN VALUES" 219\&\fIlh_new()\fR returns \fB\s-1NULL\s0\fR on error, otherwise a pointer to the new 220\&\fB\s-1LHASH\s0\fR structure. 221.PP 222When a hash table entry is replaced, \fIlh_insert()\fR returns the value --- 4 unchanged lines hidden (view full) --- 227.PP 228\&\fIlh_retrieve()\fR returns the hash table entry if it has been found, 229\&\fB\s-1NULL\s0\fR otherwise. 230.PP 231\&\fIlh_error()\fR returns 1 if an error occurred in the last operation, 0 232otherwise. 233.PP 234\&\fIlh_free()\fR, \fIlh_doall()\fR and \fIlh_doall_arg()\fR return no values. | 318\&\fIlh_error()\fR can be used to determine if an error occurred in the last 319operation. \fIlh_error()\fR is a macro. 320.SH "RETURN VALUES" 321.IX Header "RETURN VALUES" 322\&\fIlh_new()\fR returns \fB\s-1NULL\s0\fR on error, otherwise a pointer to the new 323\&\fB\s-1LHASH\s0\fR structure. 324.PP 325When a hash table entry is replaced, \fIlh_insert()\fR returns the value --- 4 unchanged lines hidden (view full) --- 330.PP 331\&\fIlh_retrieve()\fR returns the hash table entry if it has been found, 332\&\fB\s-1NULL\s0\fR otherwise. 333.PP 334\&\fIlh_error()\fR returns 1 if an error occurred in the last operation, 0 335otherwise. 336.PP 337\&\fIlh_free()\fR, \fIlh_doall()\fR and \fIlh_doall_arg()\fR return no values. |
338.SH "NOTE" 339.IX Header "NOTE" 340The various \s-1LHASH\s0 macros and callback types exist to make it possible 341to write type-safe code without resorting to function-prototype 342casting \- an evil that makes application code much harder to 343audit/verify and also opens the window of opportunity for stack 344corruption and other hard-to-find bugs. It also, apparently, violates 345\&\s-1ANSI-C\s0. 346.PP 347The \s-1LHASH\s0 code regards table entries as constant data. As such, it 348internally represents \fIlh_insert()\fR'd items with a \*(L"const void *\*(R" 349pointer type. This is why callbacks such as those used by \fIlh_doall()\fR 350and \fIlh_doall_arg()\fR declare their prototypes with \*(L"const\*(R", even for the 351parameters that pass back the table items' data pointers \- for 352consistency, user-provided data is \*(L"const\*(R" at all times as far as the 353\&\s-1LHASH\s0 code is concerned. However, as callers are themselves providing 354these pointers, they can choose whether they too should be treating 355all such parameters as constant. 356.PP 357As an example, a hash table may be maintained by code that, for 358reasons of encapsulation, has only \*(L"const\*(R" access to the data being 359indexed in the hash table (ie. it is returned as \*(L"const\*(R" from 360elsewhere in their code) \- in this case the \s-1LHASH\s0 prototypes are 361appropriate as-is. Conversely, if the caller is responsible for the 362life-time of the data in question, then they may well wish to make 363modifications to table item passed back in the \fIlh_doall()\fR or 364\&\fIlh_doall_arg()\fR callbacks (see the \*(L"STUFF_cleanup\*(R" example above). If 365so, the caller can either cast the \*(L"const\*(R" away (if they're providing 366the raw callbacks themselves) or use the macros to declare/implement 367the wrapper functions without \*(L"const\*(R" types. 368.PP 369Callers that only have \*(L"const\*(R" access to data they're indexing in a 370table, yet declare callbacks without constant types (or cast the 371\&\*(L"const\*(R" away themselves), are therefore creating their own risks/bugs 372without being encouraged to do so by the \s-1API\s0. On a related note, 373those auditing code should pay special attention to any instances of 374DECLARE/IMPLEMENT_LHASH_DOALL_[\s-1ARG_\s0]_FN macros that provide types 375without any \*(L"const\*(R" qualifiers. |
|
235.SH "BUGS" 236.IX Header "BUGS" 237\&\fIlh_insert()\fR returns \fB\s-1NULL\s0\fR both for success and error. 238.SH "INTERNALS" 239.IX Header "INTERNALS" 240The following description is based on the SSLeay documentation: 241.PP 242The \fBlhash\fR library implements a hash table described in the --- 23 unchanged lines hidden (view full) --- 266each item so when a lookup is done, the 'hashes' are compared, if 267there is a match, then a full compare is done, and 268hash->num_comp_calls is incremented. If num_comp_calls is not equal 269to num_delete plus num_retrieve it means that your hash function is 270generating hashes that are the same for different values. It is 271probably worth changing your hash function if this is the case because 272even if your hash table has 10 items in a 'bucket', it can be searched 273with 10 \fBunsigned long\fR compares and 10 linked list traverses. This | 376.SH "BUGS" 377.IX Header "BUGS" 378\&\fIlh_insert()\fR returns \fB\s-1NULL\s0\fR both for success and error. 379.SH "INTERNALS" 380.IX Header "INTERNALS" 381The following description is based on the SSLeay documentation: 382.PP 383The \fBlhash\fR library implements a hash table described in the --- 23 unchanged lines hidden (view full) --- 407each item so when a lookup is done, the 'hashes' are compared, if 408there is a match, then a full compare is done, and 409hash->num_comp_calls is incremented. If num_comp_calls is not equal 410to num_delete plus num_retrieve it means that your hash function is 411generating hashes that are the same for different values. It is 412probably worth changing your hash function if this is the case because 413even if your hash table has 10 items in a 'bucket', it can be searched 414with 10 \fBunsigned long\fR compares and 10 linked list traverses. This |
274will be much less expensive that 10 calls to you compare function. | 415will be much less expensive that 10 calls to your compare function. |
275.PP 276\&\fIlh_strhash()\fR is a demo string hashing function: 277.PP 278.Vb 1 279\& unsigned long lh_strhash(const char *c); 280.Ve 281Since the \fB\s-1LHASH\s0\fR routines would normally be passed structures, this 282routine would not normally be passed to \fIlh_new()\fR, rather it would be 283used in the function passed to \fIlh_new()\fR. 284.SH "SEE ALSO" 285.IX Header "SEE ALSO" 286lh_stats(3) 287.SH "HISTORY" 288.IX Header "HISTORY" 289The \fBlhash\fR library is available in all versions of SSLeay and OpenSSL. 290\&\fIlh_error()\fR was added in SSLeay 0.9.1b. 291.PP 292This manpage is derived from the SSLeay documentation. | 416.PP 417\&\fIlh_strhash()\fR is a demo string hashing function: 418.PP 419.Vb 1 420\& unsigned long lh_strhash(const char *c); 421.Ve 422Since the \fB\s-1LHASH\s0\fR routines would normally be passed structures, this 423routine would not normally be passed to \fIlh_new()\fR, rather it would be 424used in the function passed to \fIlh_new()\fR. 425.SH "SEE ALSO" 426.IX Header "SEE ALSO" 427lh_stats(3) 428.SH "HISTORY" 429.IX Header "HISTORY" 430The \fBlhash\fR library is available in all versions of SSLeay and OpenSSL. 431\&\fIlh_error()\fR was added in SSLeay 0.9.1b. 432.PP 433This manpage is derived from the SSLeay documentation. |
434.PP 435In OpenSSL 0.9.7, all lhash functions that were passed function pointers 436were changed for better type safety, and the function types \s-1LHASH_COMP_FN_TYPE\s0, 437\&\s-1LHASH_HASH_FN_TYPE\s0, \s-1LHASH_DOALL_FN_TYPE\s0 and \s-1LHASH_DOALL_ARG_FN_TYPE\s0 438became available. |
|