OPENSSL_LH_COMPFUNC.pod 12 KB

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