lhash.pod 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. =pod
  2. =head1 NAME
  3. lh_new, lh_free, lh_insert, lh_delete, lh_retrieve, lh_doall, lh_doall_arg, lh_error - dynamic hash table
  4. =head1 SYNOPSIS
  5. #include <openssl/lhash.h>
  6. LHASH *lh_new(LHASH_HASH_FN_TYPE hash, LHASH_COMP_FN_TYPE compare);
  7. void lh_free(LHASH *table);
  8. void *lh_insert(LHASH *table, void *data);
  9. void *lh_delete(LHASH *table, void *data);
  10. void *lh_retrieve(LHASH *table, void *data);
  11. void lh_doall(LHASH *table, LHASH_DOALL_FN_TYPE func);
  12. void lh_doall_arg(LHASH *table, LHASH_DOALL_ARG_FN_TYPE func,
  13. void *arg);
  14. int lh_error(LHASH *table);
  15. typedef int (*LHASH_COMP_FN_TYPE)(const void *, const void *);
  16. typedef unsigned long (*LHASH_HASH_FN_TYPE)(const void *);
  17. typedef void (*LHASH_DOALL_FN_TYPE)(const void *);
  18. typedef void (*LHASH_DOALL_ARG_FN_TYPE)(const void *, const void *);
  19. =head1 DESCRIPTION
  20. This library implements dynamic hash tables. The hash table entries
  21. can be arbitrary structures. Usually they consist of key and value
  22. fields.
  23. lh_new() creates a new B<LHASH> structure to store arbitrary data
  24. entries, and provides the 'hash' and 'compare' callbacks to be used in
  25. organising the table's entries. The B<hash> callback takes a pointer
  26. to a table entry as its argument and returns an unsigned long hash
  27. value for its key field. The hash value is normally truncated to a
  28. power of 2, so make sure that your hash function returns well mixed
  29. low order bits. The B<compare> callback takes two arguments (pointers
  30. to two hash table entries), and returns 0 if their keys are equal,
  31. non-zero otherwise. If your hash table will contain items of some
  32. particular type and the B<hash> and B<compare> callbacks hash/compare
  33. these types, then the B<DECLARE_LHASH_HASH_FN> and
  34. B<IMPLEMENT_LHASH_COMP_FN> macros can be used to create callback
  35. wrappers of the prototypes required by lh_new(). These provide
  36. per-variable casts before calling the type-specific callbacks written
  37. by the application author. These macros, as well as those used for
  38. the "doall" callbacks, are defined as;
  39. #define DECLARE_LHASH_HASH_FN(f_name,o_type) \
  40. unsigned long f_name##_LHASH_HASH(const void *);
  41. #define IMPLEMENT_LHASH_HASH_FN(f_name,o_type) \
  42. unsigned long f_name##_LHASH_HASH(const void *arg) { \
  43. o_type a = (o_type)arg; \
  44. return f_name(a); }
  45. #define LHASH_HASH_FN(f_name) f_name##_LHASH_HASH
  46. #define DECLARE_LHASH_COMP_FN(f_name,o_type) \
  47. int f_name##_LHASH_COMP(const void *, const void *);
  48. #define IMPLEMENT_LHASH_COMP_FN(f_name,o_type) \
  49. int f_name##_LHASH_COMP(const void *arg1, const void *arg2) { \
  50. o_type a = (o_type)arg1; \
  51. o_type b = (o_type)arg2; \
  52. return f_name(a,b); }
  53. #define LHASH_COMP_FN(f_name) f_name##_LHASH_COMP
  54. #define DECLARE_LHASH_DOALL_FN(f_name,o_type) \
  55. void f_name##_LHASH_DOALL(const void *);
  56. #define IMPLEMENT_LHASH_DOALL_FN(f_name,o_type) \
  57. void f_name##_LHASH_DOALL(const void *arg) { \
  58. o_type a = (o_type)arg; \
  59. f_name(a); }
  60. #define LHASH_DOALL_FN(f_name) f_name##_LHASH_DOALL
  61. #define DECLARE_LHASH_DOALL_ARG_FN(f_name,o_type,a_type) \
  62. void f_name##_LHASH_DOALL_ARG(const void *, const void *);
  63. #define IMPLEMENT_LHASH_DOALL_ARG_FN(f_name,o_type,a_type) \
  64. void f_name##_LHASH_DOALL_ARG(const void *arg1, const void *arg2) { \
  65. o_type a = (o_type)arg1; \
  66. a_type b = (a_type)arg2; \
  67. f_name(a,b); }
  68. #define LHASH_DOALL_ARG_FN(f_name) f_name##_LHASH_DOALL_ARG
  69. An example of a hash table storing (pointers to) structures of type 'STUFF'
  70. could be defined as follows;
  71. /* Calculates the hash value of 'tohash' (implemented elsewhere) */
  72. unsigned long STUFF_hash(const STUFF *tohash);
  73. /* Orders 'arg1' and 'arg2' (implemented elsewhere) */
  74. int STUFF_cmp(const STUFF *arg1, const STUFF *arg2);
  75. /* Create the type-safe wrapper functions for use in the LHASH internals */
  76. static IMPLEMENT_LHASH_HASH_FN(STUFF_hash, const STUFF *)
  77. static IMPLEMENT_LHASH_COMP_FN(STUFF_cmp, const STUFF *);
  78. /* ... */
  79. int main(int argc, char *argv[]) {
  80. /* Create the new hash table using the hash/compare wrappers */
  81. LHASH *hashtable = lh_new(LHASH_HASH_FN(STUFF_hash),
  82. LHASH_COMP_FN(STUFF_cmp));
  83. /* ... */
  84. }
  85. lh_free() frees the B<LHASH> structure B<table>. Allocated hash table
  86. entries will not be freed; consider using lh_doall() to deallocate any
  87. remaining entries in the hash table (see below).
  88. lh_insert() inserts the structure pointed to by B<data> into B<table>.
  89. If there already is an entry with the same key, the old value is
  90. replaced. Note that lh_insert() stores pointers, the data are not
  91. copied.
  92. lh_delete() deletes an entry from B<table>.
  93. lh_retrieve() looks up an entry in B<table>. Normally, B<data> is
  94. a structure with the key field(s) set; the function will return a
  95. pointer to a fully populated structure.
  96. lh_doall() will, for every entry in the hash table, call B<func> with
  97. the data item as its parameter. For lh_doall() and lh_doall_arg(),
  98. function pointer casting should be avoided in the callbacks (see
  99. B<NOTE>) - instead, either declare the callbacks to match the
  100. prototype required in lh_new() or use the declare/implement macros to
  101. create type-safe wrappers that cast variables prior to calling your
  102. type-specific callbacks. An example of this is illustrated here where
  103. the callback is used to cleanup resources for items in the hash table
  104. prior to the hashtable itself being deallocated:
  105. /* Cleans up resources belonging to 'a' (this is implemented elsewhere) */
  106. void STUFF_cleanup(STUFF *a);
  107. /* Implement a prototype-compatible wrapper for "STUFF_cleanup" */
  108. IMPLEMENT_LHASH_DOALL_FN(STUFF_cleanup, STUFF *)
  109. /* ... then later in the code ... */
  110. /* So to run "STUFF_cleanup" against all items in a hash table ... */
  111. lh_doall(hashtable, LHASH_DOALL_FN(STUFF_cleanup));
  112. /* Then the hash table itself can be deallocated */
  113. lh_free(hashtable);
  114. When doing this, be careful if you delete entries from the hash table
  115. in your callbacks: the table may decrease in size, moving the item
  116. that you are currently on down lower in the hash table - this could
  117. cause some entries to be skipped during the iteration. The second
  118. best solution to this problem is to set hash-E<gt>down_load=0 before
  119. you start (which will stop the hash table ever decreasing in size).
  120. The best solution is probably to avoid deleting items from the hash
  121. table inside a "doall" callback!
  122. lh_doall_arg() is the same as lh_doall() except that B<func> will be
  123. called with B<arg> as the second argument and B<func> should be of
  124. type B<LHASH_DOALL_ARG_FN_TYPE> (a callback prototype that is passed
  125. both the table entry and an extra argument). As with lh_doall(), you
  126. can instead choose to declare your callback with a prototype matching
  127. the types you are dealing with and use the declare/implement macros to
  128. create compatible wrappers that cast variables before calling your
  129. type-specific callbacks. An example of this is demonstrated here
  130. (printing all hash table entries to a BIO that is provided by the
  131. caller):
  132. /* Prints item 'a' to 'output_bio' (this is implemented elsewhere) */
  133. void STUFF_print(const STUFF *a, BIO *output_bio);
  134. /* Implement a prototype-compatible wrapper for "STUFF_print" */
  135. static IMPLEMENT_LHASH_DOALL_ARG_FN(STUFF_print, const STUFF *, BIO *)
  136. /* ... then later in the code ... */
  137. /* Print out the entire hashtable to a particular BIO */
  138. lh_doall_arg(hashtable, LHASH_DOALL_ARG_FN(STUFF_print), logging_bio);
  139. lh_error() can be used to determine if an error occurred in the last
  140. operation. lh_error() is a macro.
  141. =head1 RETURN VALUES
  142. lh_new() returns B<NULL> on error, otherwise a pointer to the new
  143. B<LHASH> structure.
  144. When a hash table entry is replaced, lh_insert() returns the value
  145. being replaced. B<NULL> is returned on normal operation and on error.
  146. lh_delete() returns the entry being deleted. B<NULL> is returned if
  147. there is no such value in the hash table.
  148. lh_retrieve() returns the hash table entry if it has been found,
  149. B<NULL> otherwise.
  150. lh_error() returns 1 if an error occurred in the last operation, 0
  151. otherwise.
  152. lh_free(), lh_doall() and lh_doall_arg() return no values.
  153. =head1 NOTE
  154. The various LHASH macros and callback types exist to make it possible
  155. to write type-safe code without resorting to function-prototype
  156. casting - an evil that makes application code much harder to
  157. audit/verify and also opens the window of opportunity for stack
  158. corruption and other hard-to-find bugs. It also, apparently, violates
  159. ANSI-C.
  160. The LHASH code regards table entries as constant data. As such, it
  161. internally represents lh_insert()'d items with a "const void *"
  162. pointer type. This is why callbacks such as those used by lh_doall()
  163. and lh_doall_arg() declare their prototypes with "const", even for the
  164. parameters that pass back the table items' data pointers - for
  165. consistency, user-provided data is "const" at all times as far as the
  166. LHASH code is concerned. However, as callers are themselves providing
  167. these pointers, they can choose whether they too should be treating
  168. all such parameters as constant.
  169. As an example, a hash table may be maintained by code that, for
  170. reasons of encapsulation, has only "const" access to the data being
  171. indexed in the hash table (ie. it is returned as "const" from
  172. elsewhere in their code) - in this case the LHASH prototypes are
  173. appropriate as-is. Conversely, if the caller is responsible for the
  174. life-time of the data in question, then they may well wish to make
  175. modifications to table item passed back in the lh_doall() or
  176. lh_doall_arg() callbacks (see the "STUFF_cleanup" example above). If
  177. so, the caller can either cast the "const" away (if they're providing
  178. the raw callbacks themselves) or use the macros to declare/implement
  179. the wrapper functions without "const" types.
  180. Callers that only have "const" access to data they're indexing in a
  181. table, yet declare callbacks without constant types (or cast the
  182. "const" away themselves), are therefore creating their own risks/bugs
  183. without being encouraged to do so by the API. On a related note,
  184. those auditing code should pay special attention to any instances of
  185. DECLARE/IMPLEMENT_LHASH_DOALL_[ARG_]_FN macros that provide types
  186. without any "const" qualifiers.
  187. =head1 BUGS
  188. lh_insert() returns B<NULL> both for success and error.
  189. =head1 INTERNALS
  190. The following description is based on the SSLeay documentation:
  191. The B<lhash> library implements a hash table described in the
  192. I<Communications of the ACM> in 1991. What makes this hash table
  193. different is that as the table fills, the hash table is increased (or
  194. decreased) in size via OPENSSL_realloc(). When a 'resize' is done, instead of
  195. all hashes being redistributed over twice as many 'buckets', one
  196. bucket is split. So when an 'expand' is done, there is only a minimal
  197. cost to redistribute some values. Subsequent inserts will cause more
  198. single 'bucket' redistributions but there will never be a sudden large
  199. cost due to redistributing all the 'buckets'.
  200. The state for a particular hash table is kept in the B<LHASH> structure.
  201. The decision to increase or decrease the hash table size is made
  202. depending on the 'load' of the hash table. The load is the number of
  203. items in the hash table divided by the size of the hash table. The
  204. default values are as follows. If (hash->up_load E<lt> load) =E<gt>
  205. expand. if (hash-E<gt>down_load E<gt> load) =E<gt> contract. The
  206. B<up_load> has a default value of 1 and B<down_load> has a default value
  207. of 2. These numbers can be modified by the application by just
  208. playing with the B<up_load> and B<down_load> variables. The 'load' is
  209. kept in a form which is multiplied by 256. So
  210. hash-E<gt>up_load=8*256; will cause a load of 8 to be set.
  211. If you are interested in performance the field to watch is
  212. num_comp_calls. The hash library keeps track of the 'hash' value for
  213. each item so when a lookup is done, the 'hashes' are compared, if
  214. there is a match, then a full compare is done, and
  215. hash-E<gt>num_comp_calls is incremented. If num_comp_calls is not equal
  216. to num_delete plus num_retrieve it means that your hash function is
  217. generating hashes that are the same for different values. It is
  218. probably worth changing your hash function if this is the case because
  219. even if your hash table has 10 items in a 'bucket', it can be searched
  220. with 10 B<unsigned long> compares and 10 linked list traverses. This
  221. will be much less expensive that 10 calls to your compare function.
  222. lh_strhash() is a demo string hashing function:
  223. unsigned long lh_strhash(const char *c);
  224. Since the B<LHASH> routines would normally be passed structures, this
  225. routine would not normally be passed to lh_new(), rather it would be
  226. used in the function passed to lh_new().
  227. =head1 SEE ALSO
  228. L<lh_stats(3)|lh_stats(3)>
  229. =head1 HISTORY
  230. The B<lhash> library is available in all versions of SSLeay and OpenSSL.
  231. lh_error() was added in SSLeay 0.9.1b.
  232. This manpage is derived from the SSLeay documentation.
  233. =cut