OPENSSL_LH_COMPFUNC.pod 9.6 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, lh_TYPE_flush,
  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 openssl generic
  12. #include <openssl/lhash.h>
  13. DECLARE_LHASH_OF(TYPE);
  14. LHASH *lh_TYPE_new(OPENSSL_LH_HASHFUNC hash, OPENSSL_LH_COMPFUNC compare);
  15. void lh_TYPE_free(LHASH_OF(TYPE) *table);
  16. void lh_TYPE_flush(LHASH_OF(TYPE) *table);
  17. TYPE *lh_TYPE_insert(LHASH_OF(TYPE) *table, TYPE *data);
  18. TYPE *lh_TYPE_delete(LHASH_OF(TYPE) *table, TYPE *data);
  19. TYPE *lh_retrieve(LHASH_OF(TYPE) *table, TYPE *data);
  20. void lh_TYPE_doall(LHASH_OF(TYPE) *table, OPENSSL_LH_DOALL_FUNC func);
  21. void lh_TYPE_doall_arg(LHASH_OF(TYPE) *table, OPENSSL_LH_DOALL_FUNCARG func,
  22. TYPE *arg);
  23. int lh_TYPE_error(LHASH_OF(TYPE) *table);
  24. typedef int (*OPENSSL_LH_COMPFUNC)(const void *, const void *);
  25. typedef unsigned long (*OPENSSL_LH_HASHFUNC)(const void *);
  26. typedef void (*OPENSSL_LH_DOALL_FUNC)(const void *);
  27. typedef void (*LHASH_DOALL_ARG_FN_TYPE)(const void *, const void *);
  28. =head1 DESCRIPTION
  29. This library implements type-checked dynamic hash tables. The hash
  30. table entries can be arbitrary structures. Usually they consist of key
  31. and value fields. In the description here, B<I<TYPE>> is used a placeholder
  32. for any of the OpenSSL datatypes, such as I<SSL_SESSION>.
  33. B<lh_I<TYPE>_new>() creates a new B<LHASH_OF>(B<I<TYPE>>) structure to store
  34. arbitrary data entries, and specifies the 'hash' and 'compare'
  35. callbacks to be used in organising the table's entries. The I<hash>
  36. callback takes a pointer to a table entry as its argument and returns
  37. an unsigned long hash value for its key field. The hash value is
  38. normally truncated to a power of 2, so make sure that your hash
  39. function returns well mixed low order bits. The I<compare> callback
  40. takes two arguments (pointers to two hash table entries), and returns
  41. 0 if their keys are equal, nonzero otherwise.
  42. If your hash table
  43. will contain items of some particular type and the I<hash> and
  44. I<compare> callbacks hash/compare these types, then the
  45. B<IMPLEMENT_LHASH_HASH_FN> and B<IMPLEMENT_LHASH_COMP_FN> macros can be
  46. used to create callback wrappers of the prototypes required by
  47. B<lh_I<TYPE>_new>() as shown in this example:
  48. /*
  49. * Implement the hash and compare functions; "stuff" can be any word.
  50. */
  51. static unsigned long stuff_hash(const TYPE *a)
  52. {
  53. ...
  54. }
  55. static int stuff_cmp(const TYPE *a, const TYPE *b)
  56. {
  57. ...
  58. }
  59. /*
  60. * Implement the wrapper functions.
  61. */
  62. static IMPLEMENT_LHASH_HASH_FN(stuff, TYPE)
  63. static IMPLEMENT_LHASH_COMP_FN(stuff, TYPE)
  64. If the type is going to be used in several places, the following macros
  65. can be used in a common header file to declare the function wrappers:
  66. DECLARE_LHASH_HASH_FN(stuff, TYPE)
  67. DECLARE_LHASH_COMP_FN(stuff, TYPE)
  68. Then a hash table of B<I<TYPE>> objects can be created using this:
  69. LHASH_OF(TYPE) *htable;
  70. htable = B<lh_I<TYPE>_new>(LHASH_HASH_FN(stuff), LHASH_COMP_FN(stuff));
  71. B<lh_I<TYPE>_free>() frees the B<LHASH_OF>(B<I<TYPE>>) structure
  72. I<table>. Allocated hash table entries will not be freed; consider
  73. using B<lh_I<TYPE>_doall>() to deallocate any remaining entries in the
  74. hash table (see below).
  75. B<lh_I<TYPE>_flush>() empties the B<LHASH_OF>(B<I<TYPE>>) structure I<table>. New
  76. entries can be added to the flushed table. Allocated hash table entries
  77. will not be freed; consider using B<lh_I<TYPE>_doall>() to deallocate any
  78. remaining entries in the hash table (see below).
  79. B<lh_I<TYPE>_insert>() inserts the structure pointed to by I<data> into
  80. I<table>. If there already is an entry with the same key, the old
  81. value is replaced. Note that B<lh_I<TYPE>_insert>() stores pointers, the
  82. data are not copied.
  83. B<lh_I<TYPE>_delete>() deletes an entry from I<table>.
  84. B<lh_I<TYPE>_retrieve>() looks up an entry in I<table>. Normally, I<data>
  85. is a structure with the key field(s) set; the function will return a
  86. pointer to a fully populated structure.
  87. B<lh_I<TYPE>_doall>() will, for every entry in the hash table, call
  88. I<func> with the data item as its parameter.
  89. For example:
  90. /* Cleans up resources belonging to 'a' (this is implemented elsewhere) */
  91. void TYPE_cleanup_doall(TYPE *a);
  92. /* Implement a prototype-compatible wrapper for "TYPE_cleanup" */
  93. IMPLEMENT_LHASH_DOALL_FN(TYPE_cleanup, TYPE)
  94. /* Call "TYPE_cleanup" against all items in a hash table. */
  95. lh_TYPE_doall(hashtable, LHASH_DOALL_FN(TYPE_cleanup));
  96. /* Then the hash table itself can be deallocated */
  97. lh_TYPE_free(hashtable);
  98. When doing this, be careful if you delete entries from the hash table
  99. in your callbacks: the table may decrease in size, moving the item
  100. that you are currently on down lower in the hash table - this could
  101. cause some entries to be skipped during the iteration. The second
  102. best solution to this problem is to set hash-E<gt>down_load=0 before
  103. you start (which will stop the hash table ever decreasing in size).
  104. The best solution is probably to avoid deleting items from the hash
  105. table inside a "doall" callback!
  106. B<lh_I<TYPE>_doall_arg>() is the same as B<lh_I<TYPE>_doall>() except that
  107. I<func> will be called with I<arg> as the second argument and I<func>
  108. should be of type B<LHASH_DOALL_ARG_FN>(B<I<TYPE>>) (a callback prototype
  109. that is passed both the table entry and an extra argument). As with
  110. lh_doall(), you can instead choose to declare your callback with a
  111. prototype matching the types you are dealing with and use the
  112. declare/implement macros to create compatible wrappers that cast
  113. variables before calling your type-specific callbacks. An example of
  114. this is demonstrated here (printing all hash table entries to a BIO
  115. that is provided by the caller):
  116. /* Prints item 'a' to 'output_bio' (this is implemented elsewhere) */
  117. void TYPE_print_doall_arg(const TYPE *a, BIO *output_bio);
  118. /* Implement a prototype-compatible wrapper for "TYPE_print" */
  119. static IMPLEMENT_LHASH_DOALL_ARG_FN(TYPE, const TYPE, BIO)
  120. /* Print out the entire hashtable to a particular BIO */
  121. lh_TYPE_doall_arg(hashtable, LHASH_DOALL_ARG_FN(TYPE_print), BIO,
  122. logging_bio);
  123. B<lh_I<TYPE>_error>() can be used to determine if an error occurred in the last
  124. operation.
  125. =head1 RETURN VALUES
  126. B<lh_I<TYPE>_new>() returns NULL on error, otherwise a pointer to the new
  127. B<LHASH> structure.
  128. When a hash table entry is replaced, B<lh_I<TYPE>_insert>() returns the value
  129. being replaced. NULL is returned on normal operation and on error.
  130. B<lh_I<TYPE>_delete>() returns the entry being deleted. NULL is returned if
  131. there is no such value in the hash table.
  132. B<lh_I<TYPE>_retrieve>() returns the hash table entry if it has been found,
  133. NULL otherwise.
  134. B<lh_I<TYPE>_error>() returns 1 if an error occurred in the last operation, 0
  135. otherwise. It's meaningful only after non-retrieve operations.
  136. B<lh_I<TYPE>_free>(), B<lh_I<TYPE>_flush>(), B<lh_I<TYPE>_doall>() and
  137. B<lh_I<TYPE>_doall_arg>() return no values.
  138. =head1 NOTE
  139. The LHASH code is not thread safe. All updating operations, as well as
  140. B<lh_I<TYPE>_error>() call must be performed under a write lock. All retrieve
  141. operations should be performed under a read lock, I<unless> accurate
  142. usage statistics are desired. In which case, a write lock should be used
  143. for retrieve operations as well. For output of the usage statistics,
  144. using the functions from 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 (i.e. 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. B<lh_I<TYPE>_insert>() returns 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-2020 The OpenSSL Project Authors. All Rights Reserved.
  181. Licensed under the Apache License 2.0 (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