OPENSSL_LH_COMPFUNC.pod 14 KB

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