lhash.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * Header for dynamic hash table routines Author - Eric Young
  11. */
  12. #ifndef HEADER_LHASH_H
  13. # define HEADER_LHASH_H
  14. # include <openssl/e_os2.h>
  15. # include <openssl/bio.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. typedef struct lhash_node_st OPENSSL_LH_NODE;
  20. typedef int (*OPENSSL_LH_COMPFUNC) (const void *, const void *);
  21. typedef unsigned long (*OPENSSL_LH_HASHFUNC) (const void *);
  22. typedef void (*OPENSSL_LH_DOALL_FUNC) (void *);
  23. typedef void (*OPENSSL_LH_DOALL_FUNCARG) (void *, void *);
  24. typedef struct lhash_st OPENSSL_LHASH;
  25. /*
  26. * Macros for declaring and implementing type-safe wrappers for LHASH
  27. * callbacks. This way, callbacks can be provided to LHASH structures without
  28. * function pointer casting and the macro-defined callbacks provide
  29. * per-variable casting before deferring to the underlying type-specific
  30. * callbacks. NB: It is possible to place a "static" in front of both the
  31. * DECLARE and IMPLEMENT macros if the functions are strictly internal.
  32. */
  33. /* First: "hash" functions */
  34. # define DECLARE_LHASH_HASH_FN(name, o_type) \
  35. unsigned long name##_LHASH_HASH(const void *);
  36. # define IMPLEMENT_LHASH_HASH_FN(name, o_type) \
  37. unsigned long name##_LHASH_HASH(const void *arg) { \
  38. const o_type *a = arg; \
  39. return name##_hash(a); }
  40. # define LHASH_HASH_FN(name) name##_LHASH_HASH
  41. /* Second: "compare" functions */
  42. # define DECLARE_LHASH_COMP_FN(name, o_type) \
  43. int name##_LHASH_COMP(const void *, const void *);
  44. # define IMPLEMENT_LHASH_COMP_FN(name, o_type) \
  45. int name##_LHASH_COMP(const void *arg1, const void *arg2) { \
  46. const o_type *a = arg1; \
  47. const o_type *b = arg2; \
  48. return name##_cmp(a,b); }
  49. # define LHASH_COMP_FN(name) name##_LHASH_COMP
  50. /* Fourth: "doall_arg" functions */
  51. # define DECLARE_LHASH_DOALL_ARG_FN(name, o_type, a_type) \
  52. void name##_LHASH_DOALL_ARG(void *, void *);
  53. # define IMPLEMENT_LHASH_DOALL_ARG_FN(name, o_type, a_type) \
  54. void name##_LHASH_DOALL_ARG(void *arg1, void *arg2) { \
  55. o_type *a = arg1; \
  56. a_type *b = arg2; \
  57. name##_doall_arg(a, b); }
  58. # define LHASH_DOALL_ARG_FN(name) name##_LHASH_DOALL_ARG
  59. # define LH_LOAD_MULT 256
  60. int OPENSSL_LH_error(OPENSSL_LHASH *lh);
  61. OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c);
  62. void OPENSSL_LH_free(OPENSSL_LHASH *lh);
  63. void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data);
  64. void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data);
  65. void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data);
  66. void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func);
  67. void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg);
  68. unsigned long OPENSSL_LH_strhash(const char *c);
  69. unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh);
  70. unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh);
  71. void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load);
  72. # ifndef OPENSSL_NO_STDIO
  73. void OPENSSL_LH_stats(const OPENSSL_LHASH *lh, FILE *fp);
  74. void OPENSSL_LH_node_stats(const OPENSSL_LHASH *lh, FILE *fp);
  75. void OPENSSL_LH_node_usage_stats(const OPENSSL_LHASH *lh, FILE *fp);
  76. # endif
  77. void OPENSSL_LH_stats_bio(const OPENSSL_LHASH *lh, BIO *out);
  78. void OPENSSL_LH_node_stats_bio(const OPENSSL_LHASH *lh, BIO *out);
  79. void OPENSSL_LH_node_usage_stats_bio(const OPENSSL_LHASH *lh, BIO *out);
  80. # if OPENSSL_API_COMPAT < 0x10100000L
  81. # define _LHASH OPENSSL_LHASH
  82. # define LHASH_NODE OPENSSL_LH_NODE
  83. # define lh_error OPENSSL_LH_error
  84. # define lh_new OPENSSL_LH_new
  85. # define lh_free OPENSSL_LH_free
  86. # define lh_insert OPENSSL_LH_insert
  87. # define lh_delete OPENSSL_LH_delete
  88. # define lh_retrieve OPENSSL_LH_retrieve
  89. # define lh_doall OPENSSL_LH_doall
  90. # define lh_doall_arg OPENSSL_LH_doall_arg
  91. # define lh_strhash OPENSSL_LH_strhash
  92. # define lh_num_items OPENSSL_LH_num_items
  93. # ifndef OPENSSL_NO_STDIO
  94. # define lh_stats OPENSSL_LH_stats
  95. # define lh_node_stats OPENSSL_LH_node_stats
  96. # define lh_node_usage_stats OPENSSL_LH_node_usage_stats
  97. # endif
  98. # define lh_stats_bio OPENSSL_LH_stats_bio
  99. # define lh_node_stats_bio OPENSSL_LH_node_stats_bio
  100. # define lh_node_usage_stats_bio OPENSSL_LH_node_usage_stats_bio
  101. # endif
  102. /* Type checking... */
  103. # define LHASH_OF(type) struct lhash_st_##type
  104. # define DEFINE_LHASH_OF(type) \
  105. LHASH_OF(type) { union lh_##type##_dummy { void* d1; unsigned long d2; int d3; } dummy; }; \
  106. static ossl_inline LHASH_OF(type) * \
  107. lh_##type##_new(unsigned long (*hfn)(const type *), \
  108. int (*cfn)(const type *, const type *)) \
  109. { \
  110. return (LHASH_OF(type) *) \
  111. OPENSSL_LH_new((OPENSSL_LH_HASHFUNC)hfn, (OPENSSL_LH_COMPFUNC)cfn); \
  112. } \
  113. static ossl_inline void lh_##type##_free(LHASH_OF(type) *lh) \
  114. { \
  115. OPENSSL_LH_free((OPENSSL_LHASH *)lh); \
  116. } \
  117. static ossl_inline type *lh_##type##_insert(LHASH_OF(type) *lh, type *d) \
  118. { \
  119. return (type *)OPENSSL_LH_insert((OPENSSL_LHASH *)lh, d); \
  120. } \
  121. static ossl_inline type *lh_##type##_delete(LHASH_OF(type) *lh, const type *d) \
  122. { \
  123. return (type *)OPENSSL_LH_delete((OPENSSL_LHASH *)lh, d); \
  124. } \
  125. static ossl_inline type *lh_##type##_retrieve(LHASH_OF(type) *lh, const type *d) \
  126. { \
  127. return (type *)OPENSSL_LH_retrieve((OPENSSL_LHASH *)lh, d); \
  128. } \
  129. static ossl_inline int lh_##type##_error(LHASH_OF(type) *lh) \
  130. { \
  131. return OPENSSL_LH_error((OPENSSL_LHASH *)lh); \
  132. } \
  133. static ossl_inline unsigned long lh_##type##_num_items(LHASH_OF(type) *lh) \
  134. { \
  135. return OPENSSL_LH_num_items((OPENSSL_LHASH *)lh); \
  136. } \
  137. static ossl_inline void lh_##type##_node_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
  138. { \
  139. OPENSSL_LH_node_stats_bio((const OPENSSL_LHASH *)lh, out); \
  140. } \
  141. static ossl_inline void lh_##type##_node_usage_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
  142. { \
  143. OPENSSL_LH_node_usage_stats_bio((const OPENSSL_LHASH *)lh, out); \
  144. } \
  145. static ossl_inline void lh_##type##_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
  146. { \
  147. OPENSSL_LH_stats_bio((const OPENSSL_LHASH *)lh, out); \
  148. } \
  149. static ossl_inline unsigned long lh_##type##_get_down_load(LHASH_OF(type) *lh) \
  150. { \
  151. return OPENSSL_LH_get_down_load((OPENSSL_LHASH *)lh); \
  152. } \
  153. static ossl_inline void lh_##type##_set_down_load(LHASH_OF(type) *lh, unsigned long dl) \
  154. { \
  155. OPENSSL_LH_set_down_load((OPENSSL_LHASH *)lh, dl); \
  156. } \
  157. static ossl_inline void lh_##type##_doall(LHASH_OF(type) *lh, \
  158. void (*doall)(type *)) \
  159. { \
  160. OPENSSL_LH_doall((OPENSSL_LHASH *)lh, (OPENSSL_LH_DOALL_FUNC)doall); \
  161. } \
  162. LHASH_OF(type)
  163. #define IMPLEMENT_LHASH_DOALL_ARG_CONST(type, argtype) \
  164. int_implement_lhash_doall(type, argtype, const type)
  165. #define IMPLEMENT_LHASH_DOALL_ARG(type, argtype) \
  166. int_implement_lhash_doall(type, argtype, type)
  167. #define int_implement_lhash_doall(type, argtype, cbargtype) \
  168. static ossl_inline void \
  169. lh_##type##_doall_##argtype(LHASH_OF(type) *lh, \
  170. void (*fn)(cbargtype *, argtype *), \
  171. argtype *arg) \
  172. { \
  173. OPENSSL_LH_doall_arg((OPENSSL_LHASH *)lh, (OPENSSL_LH_DOALL_FUNCARG)fn, (void *)arg); \
  174. } \
  175. LHASH_OF(type)
  176. DEFINE_LHASH_OF(OPENSSL_STRING);
  177. # ifdef _MSC_VER
  178. /*
  179. * push and pop this warning:
  180. * warning C4090: 'function': different 'const' qualifiers
  181. */
  182. # pragma warning (push)
  183. # pragma warning (disable: 4090)
  184. # endif
  185. DEFINE_LHASH_OF(OPENSSL_CSTRING);
  186. # ifdef _MSC_VER
  187. # pragma warning (pop)
  188. # endif
  189. #ifdef __cplusplus
  190. }
  191. #endif
  192. #endif