OPENSSL_LH_COMPFUNC.pod 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. =pod
  2. =head1 NAME
  3. LHASH, DECLARE_LHASH_OF,
  4. OPENSSL_LH_COMPFUNC, OPENSSL_LH_HASHFUNC, OPENSSL_LH_DOALL_FUNC,
  5. LHASH_DOALL_ARG_FN_TYPE,
  6. IMPLEMENT_LHASH_HASH_FN, IMPLEMENT_LHASH_COMP_FN,
  7. lh_TYPE_new, lh_TYPE_free,
  8. lh_TYPE_insert, lh_TYPE_delete, lh_TYPE_retrieve,
  9. lh_TYPE_doall, lh_TYPE_doall_arg, lh_TYPE_error - dynamic hash table
  10. =head1 SYNOPSIS
  11. =for comment generic
  12. #include <openssl/lhash.h>
  13. DECLARE_LHASH_OF(TYPE);
  14. LHASH *lh_TYPE_new();
  15. void lh_TYPE_free(LHASH_OF(TYPE *table);
  16. TYPE *lh_TYPE_insert(LHASH_OF(TYPE *table, TYPE *data);
  17. TYPE *lh_TYPE_delete(LHASH_OF(TYPE *table, TYPE *data);
  18. TYPE *lh_retrieve(LHASH_OFTYPE *table, TYPE *data);
  19. void lh_TYPE_doall(LHASH_OF(TYPE *table, OPENSSL_LH_DOALL_FUNC func);
  20. void lh_TYPE_doall_arg(LHASH_OF(TYPE) *table, OPENSSL_LH_DOALL_FUNCARG func,
  21. TYPE, TYPE *arg);
  22. int lh_TYPE_error(LHASH_OF(TYPE) *table);
  23. typedef int (*OPENSSL_LH_COMPFUNC)(const void *, const void *);
  24. typedef unsigned long (*OPENSSL_LH_HASHFUNC)(const void *);
  25. typedef void (*OPENSSL_LH_DOALL_FUNC)(const void *);
  26. typedef void (*LHASH_DOALL_ARG_FN_TYPE)(const void *, const void *);
  27. =head1 DESCRIPTION
  28. This library implements type-checked dynamic hash tables. The hash
  29. table entries can be arbitrary structures. Usually they consist of key
  30. and value fields. In the description here, I<TYPE> is used a placeholder
  31. for any of the OpenSSL datatypes, such as I<SSL_SESSION>.
  32. lh_TYPE_new() creates a new B<LHASH_OF(TYPE)> structure to store
  33. arbitrary data entries, and specifies the 'hash' and 'compare'
  34. callbacks to be used in organising the table's entries. The B<hash>
  35. callback takes a pointer to a table entry as its argument and returns
  36. an unsigned long hash value for its key field. The hash value is
  37. normally truncated to a power of 2, so make sure that your hash
  38. function returns well mixed low order bits. The B<compare> callback
  39. takes two arguments (pointers to two hash table entries), and returns
  40. 0 if their keys are equal, non-zero otherwise.
  41. If your hash table
  42. will contain items of some particular type and the B<hash> and
  43. B<compare> callbacks hash/compare these types, then the
  44. B<IMPLEMENT_LHASH_HASH_FN> and B<IMPLEMENT_LHASH_COMP_FN> macros can be
  45. used to create callback wrappers of the prototypes required by
  46. lh_TYPE_new() as shown in this example:
  47. /*
  48. * Implement the hash and compare functions; "stuff" can be any word.
  49. */
  50. static unsigned long stuff_hash(const TYPE *a)
  51. {
  52. ...
  53. }
  54. static int stuff_cmp(const TYPE *a, const TYPE *b)
  55. {
  56. ...
  57. }
  58. /*
  59. * Implement the wrapper functions.
  60. */
  61. static IMPLEMENT_LHASH_HASH_FN(stuff, TYPE)
  62. static IMPLEMENT_LHASH_COMP_FN(stuff, TYPE)
  63. If the type is going to be used in several places, the following macros
  64. can be used in a common header file to declare the function wrappers:
  65. DECLARE_LHASH_HASH_FN(stuff, TYPE)
  66. DECLARE_LHASH_COMP_FN(stuff, TYPE)
  67. Then a hash table of TYPE objects can be created using this:
  68. LHASH_OF(TYPE) *htable;
  69. htable = lh_TYPE_new(LHASH_HASH_FN(stuff), LHASH_COMP_FN(stuff));
  70. lh_TYPE_free() frees the B<LHASH_OF(TYPE)> structure
  71. B<table>. Allocated hash table entries will not be freed; consider
  72. using lh_TYPE_doall() to deallocate any remaining entries in the
  73. hash table (see below).
  74. lh_TYPE_insert() inserts the structure pointed to by B<data> into
  75. B<table>. If there already is an entry with the same key, the old
  76. value is replaced. Note that lh_TYPE_insert() stores pointers, the
  77. data are not copied.
  78. lh_TYPE_delete() deletes an entry from B<table>.
  79. lh_TYPE_retrieve() looks up an entry in B<table>. Normally, B<data>
  80. is a structure with the key field(s) set; the function will return a
  81. pointer to a fully populated structure.
  82. lh_TYPE_doall() will, for every entry in the hash table, call
  83. B<func> with the data item as its parameter.
  84. For example:
  85. /* Cleans up resources belonging to 'a' (this is implemented elsewhere) */
  86. void TYPE_cleanup_doall(TYPE *a);
  87. /* Implement a prototype-compatible wrapper for "TYPE_cleanup" */
  88. IMPLEMENT_LHASH_DOALL_FN(TYPE_cleanup, TYPE)
  89. /* Call "TYPE_cleanup" against all items in a hash table. */
  90. lh_TYPE_doall(hashtable, LHASH_DOALL_FN(TYPE_cleanup));
  91. /* Then the hash table itself can be deallocated */
  92. lh_TYPE_free(hashtable);
  93. When doing this, be careful if you delete entries from the hash table
  94. in your callbacks: the table may decrease in size, moving the item
  95. that you are currently on down lower in the hash table - this could
  96. cause some entries to be skipped during the iteration. The second
  97. best solution to this problem is to set hash-E<gt>down_load=0 before
  98. you start (which will stop the hash table ever decreasing in size).
  99. The best solution is probably to avoid deleting items from the hash
  100. table inside a "doall" callback!
  101. lh_TYPE_doall_arg() is the same as lh_TYPE_doall() except that
  102. B<func> will be called with B<arg> as the second argument and B<func>
  103. should be of type B<LHASH_DOALL_ARG_FN_TYPE> (a callback prototype
  104. that is passed both the table entry and an extra argument). As with
  105. lh_doall(), you can instead choose to declare your callback with a
  106. prototype matching the types you are dealing with and use the
  107. declare/implement macros to create compatible wrappers that cast
  108. variables before calling your type-specific callbacks. An example of
  109. this is demonstrated here (printing all hash table entries to a BIO
  110. that is provided by the caller):
  111. /* Prints item 'a' to 'output_bio' (this is implemented elsewhere) */
  112. void TYPE_print_doall_arg(const TYPE *a, BIO *output_bio);
  113. /* Implement a prototype-compatible wrapper for "TYPE_print" */
  114. static IMPLEMENT_LHASH_DOALL_ARG_FN(TYPE, const TYPE, BIO)
  115. /* Print out the entire hashtable to a particular BIO */
  116. lh_TYPE_doall_arg(hashtable, LHASH_DOALL_ARG_FN(TYPE_print), BIO,
  117. logging_bio);
  118. lh_TYPE_error() can be used to determine if an error occurred in the last
  119. operation.
  120. =head1 RETURN VALUES
  121. lh_TYPE_new() returns B<NULL> on error, otherwise a pointer to the new
  122. B<LHASH> structure.
  123. When a hash table entry is replaced, lh_TYPE_insert() returns the value
  124. being replaced. B<NULL> is returned on normal operation and on error.
  125. lh_TYPE_delete() returns the entry being deleted. B<NULL> is returned if
  126. there is no such value in the hash table.
  127. lh_TYPE_retrieve() returns the hash table entry if it has been found,
  128. B<NULL> otherwise.
  129. lh_TYPE_error() returns 1 if an error occurred in the last operation, 0
  130. otherwise.
  131. lh_TYPE_free(), lh_TYPE_doall() and lh_TYPE_doall_arg() return no values.
  132. =head1 NOTE
  133. The various LHASH macros and callback types exist to make it possible
  134. to write type-checked code without resorting to function-prototype
  135. casting - an evil that makes application code much harder to
  136. audit/verify and also opens the window of opportunity for stack
  137. corruption and other hard-to-find bugs. It also, apparently, violates
  138. ANSI-C.
  139. The LHASH code is not thread safe. All updating operations must be
  140. performed under a write lock. All retrieve operations should be performed
  141. under a read lock, I<unless> accurate usage statistics are desired.
  142. In which case, a write lock should be used for retrieve operations
  143. as well. For output of the usage statistics, using the functions from
  144. L<OPENSSL_LH_stats(3)>, a read lock suffices.
  145. The LHASH code regards table entries as constant data. As such, it
  146. internally represents lh_insert()'d items with a "const void *"
  147. pointer type. This is why callbacks such as those used by lh_doall()
  148. and lh_doall_arg() declare their prototypes with "const", even for the
  149. parameters that pass back the table items' data pointers - for
  150. consistency, user-provided data is "const" at all times as far as the
  151. LHASH code is concerned. However, as callers are themselves providing
  152. these pointers, they can choose whether they too should be treating
  153. all such parameters as constant.
  154. As an example, a hash table may be maintained by code that, for
  155. reasons of encapsulation, has only "const" access to the data being
  156. indexed in the hash table (ie. it is returned as "const" from
  157. elsewhere in their code) - in this case the LHASH prototypes are
  158. appropriate as-is. Conversely, if the caller is responsible for the
  159. life-time of the data in question, then they may well wish to make
  160. modifications to table item passed back in the lh_doall() or
  161. lh_doall_arg() callbacks (see the "TYPE_cleanup" example above). If
  162. so, the caller can either cast the "const" away (if they're providing
  163. the raw callbacks themselves) or use the macros to declare/implement
  164. the wrapper functions without "const" types.
  165. Callers that only have "const" access to data they're indexing in a
  166. table, yet declare callbacks without constant types (or cast the
  167. "const" away themselves), are therefore creating their own risks/bugs
  168. without being encouraged to do so by the API. On a related note,
  169. those auditing code should pay special attention to any instances of
  170. DECLARE/IMPLEMENT_LHASH_DOALL_[ARG_]_FN macros that provide types
  171. without any "const" qualifiers.
  172. =head1 BUGS
  173. lh_TYPE_insert() returns B<NULL> both for success and error.
  174. =head1 SEE ALSO
  175. L<OPENSSL_LH_stats(3)>
  176. =head1 HISTORY
  177. In OpenSSL 1.0.0, the lhash interface was revamped for better
  178. type checking.
  179. =head1 COPYRIGHT
  180. Copyright 2000-2017 The OpenSSL Project Authors. All Rights Reserved.
  181. Licensed under the OpenSSL license (the "License"). You may not use
  182. this file except in compliance with the License. You can obtain a copy
  183. in the file LICENSE in the source distribution or at
  184. L<https://www.openssl.org/source/license.html>.
  185. =cut