lhash.h.in 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. use OpenSSL::stackhash qw(generate_lhash_macros);
  11. -}
  12. /*
  13. * Header for dynamic hash table routines Author - Eric Young
  14. */
  15. #ifndef OPENSSL_LHASH_H
  16. # define OPENSSL_LHASH_H
  17. # pragma once
  18. # include <openssl/macros.h>
  19. # ifndef OPENSSL_NO_DEPRECATED_3_0
  20. # define HEADER_LHASH_H
  21. # endif
  22. # include <openssl/e_os2.h>
  23. # include <openssl/bio.h>
  24. # ifndef OPENSSL_NO_STDIO
  25. # include <stdio.h>
  26. # endif
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. typedef struct lhash_node_st OPENSSL_LH_NODE;
  31. typedef int (*OPENSSL_LH_COMPFUNC) (const void *, const void *);
  32. typedef unsigned long (*OPENSSL_LH_HASHFUNC) (const void *);
  33. typedef void (*OPENSSL_LH_DOALL_FUNC) (void *);
  34. typedef void (*OPENSSL_LH_DOALL_FUNCARG) (void *, void *);
  35. typedef struct lhash_st OPENSSL_LHASH;
  36. /*
  37. * Macros for declaring and implementing type-safe wrappers for LHASH
  38. * callbacks. This way, callbacks can be provided to LHASH structures without
  39. * function pointer casting and the macro-defined callbacks provide
  40. * per-variable casting before deferring to the underlying type-specific
  41. * callbacks. NB: It is possible to place a "static" in front of both the
  42. * DECLARE and IMPLEMENT macros if the functions are strictly internal.
  43. */
  44. /* First: "hash" functions */
  45. # define DECLARE_LHASH_HASH_FN(name, o_type) \
  46. unsigned long name##_LHASH_HASH(const void *);
  47. # define IMPLEMENT_LHASH_HASH_FN(name, o_type) \
  48. unsigned long name##_LHASH_HASH(const void *arg) { \
  49. const o_type *a = arg; \
  50. return name##_hash(a); }
  51. # define LHASH_HASH_FN(name) name##_LHASH_HASH
  52. /* Second: "compare" functions */
  53. # define DECLARE_LHASH_COMP_FN(name, o_type) \
  54. int name##_LHASH_COMP(const void *, const void *);
  55. # define IMPLEMENT_LHASH_COMP_FN(name, o_type) \
  56. int name##_LHASH_COMP(const void *arg1, const void *arg2) { \
  57. const o_type *a = arg1; \
  58. const o_type *b = arg2; \
  59. return name##_cmp(a,b); }
  60. # define LHASH_COMP_FN(name) name##_LHASH_COMP
  61. /* Fourth: "doall_arg" functions */
  62. # define DECLARE_LHASH_DOALL_ARG_FN(name, o_type, a_type) \
  63. void name##_LHASH_DOALL_ARG(void *, void *);
  64. # define IMPLEMENT_LHASH_DOALL_ARG_FN(name, o_type, a_type) \
  65. void name##_LHASH_DOALL_ARG(void *arg1, void *arg2) { \
  66. o_type *a = arg1; \
  67. a_type *b = arg2; \
  68. name##_doall_arg(a, b); }
  69. # define LHASH_DOALL_ARG_FN(name) name##_LHASH_DOALL_ARG
  70. # define LH_LOAD_MULT 256
  71. int OPENSSL_LH_error(OPENSSL_LHASH *lh);
  72. OPENSSL_LHASH *OPENSSL_LH_new(OPENSSL_LH_HASHFUNC h, OPENSSL_LH_COMPFUNC c);
  73. void OPENSSL_LH_free(OPENSSL_LHASH *lh);
  74. void OPENSSL_LH_flush(OPENSSL_LHASH *lh);
  75. void *OPENSSL_LH_insert(OPENSSL_LHASH *lh, void *data);
  76. void *OPENSSL_LH_delete(OPENSSL_LHASH *lh, const void *data);
  77. void *OPENSSL_LH_retrieve(OPENSSL_LHASH *lh, const void *data);
  78. void OPENSSL_LH_doall(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNC func);
  79. void OPENSSL_LH_doall_arg(OPENSSL_LHASH *lh, OPENSSL_LH_DOALL_FUNCARG func, void *arg);
  80. unsigned long OPENSSL_LH_strhash(const char *c);
  81. unsigned long OPENSSL_LH_num_items(const OPENSSL_LHASH *lh);
  82. unsigned long OPENSSL_LH_get_down_load(const OPENSSL_LHASH *lh);
  83. void OPENSSL_LH_set_down_load(OPENSSL_LHASH *lh, unsigned long down_load);
  84. # ifndef OPENSSL_NO_STDIO
  85. void OPENSSL_LH_stats(const OPENSSL_LHASH *lh, FILE *fp);
  86. void OPENSSL_LH_node_stats(const OPENSSL_LHASH *lh, FILE *fp);
  87. void OPENSSL_LH_node_usage_stats(const OPENSSL_LHASH *lh, FILE *fp);
  88. # endif
  89. void OPENSSL_LH_stats_bio(const OPENSSL_LHASH *lh, BIO *out);
  90. void OPENSSL_LH_node_stats_bio(const OPENSSL_LHASH *lh, BIO *out);
  91. void OPENSSL_LH_node_usage_stats_bio(const OPENSSL_LHASH *lh, BIO *out);
  92. # ifndef OPENSSL_NO_DEPRECATED_1_1_0
  93. # define _LHASH OPENSSL_LHASH
  94. # define LHASH_NODE OPENSSL_LH_NODE
  95. # define lh_error OPENSSL_LH_error
  96. # define lh_new OPENSSL_LH_new
  97. # define lh_free OPENSSL_LH_free
  98. # define lh_insert OPENSSL_LH_insert
  99. # define lh_delete OPENSSL_LH_delete
  100. # define lh_retrieve OPENSSL_LH_retrieve
  101. # define lh_doall OPENSSL_LH_doall
  102. # define lh_doall_arg OPENSSL_LH_doall_arg
  103. # define lh_strhash OPENSSL_LH_strhash
  104. # define lh_num_items OPENSSL_LH_num_items
  105. # ifndef OPENSSL_NO_STDIO
  106. # define lh_stats OPENSSL_LH_stats
  107. # define lh_node_stats OPENSSL_LH_node_stats
  108. # define lh_node_usage_stats OPENSSL_LH_node_usage_stats
  109. # endif
  110. # define lh_stats_bio OPENSSL_LH_stats_bio
  111. # define lh_node_stats_bio OPENSSL_LH_node_stats_bio
  112. # define lh_node_usage_stats_bio OPENSSL_LH_node_usage_stats_bio
  113. # endif
  114. /* Type checking... */
  115. # define LHASH_OF(type) struct lhash_st_##type
  116. /* Helper macro for internal use */
  117. # define DEFINE_LHASH_OF_INTERNAL(type) \
  118. LHASH_OF(type) { union lh_##type##_dummy { void* d1; unsigned long d2; int d3; } dummy; }; \
  119. typedef int (*lh_##type##_compfunc)(const type *a, const type *b); \
  120. typedef unsigned long (*lh_##type##_hashfunc)(const type *a); \
  121. typedef void (*lh_##type##_doallfunc)(type *a); \
  122. static ossl_unused ossl_inline type *ossl_check_##type##_lh_plain_type(type *ptr) \
  123. { \
  124. return ptr; \
  125. } \
  126. static ossl_unused ossl_inline const type *ossl_check_const_##type##_lh_plain_type(const type *ptr) \
  127. { \
  128. return ptr; \
  129. } \
  130. static ossl_unused ossl_inline const OPENSSL_LHASH *ossl_check_const_##type##_lh_type(const LHASH_OF(type) *lh) \
  131. { \
  132. return (const OPENSSL_LHASH *)lh; \
  133. } \
  134. static ossl_unused ossl_inline OPENSSL_LHASH *ossl_check_##type##_lh_type(LHASH_OF(type) *lh) \
  135. { \
  136. return (OPENSSL_LHASH *)lh; \
  137. } \
  138. static ossl_unused ossl_inline OPENSSL_LH_COMPFUNC ossl_check_##type##_lh_compfunc_type(lh_##type##_compfunc cmp) \
  139. { \
  140. return (OPENSSL_LH_COMPFUNC)cmp; \
  141. } \
  142. static ossl_unused ossl_inline OPENSSL_LH_HASHFUNC ossl_check_##type##_lh_hashfunc_type(lh_##type##_hashfunc hfn) \
  143. { \
  144. return (OPENSSL_LH_HASHFUNC)hfn; \
  145. } \
  146. static ossl_unused ossl_inline OPENSSL_LH_DOALL_FUNC ossl_check_##type##_lh_doallfunc_type(lh_##type##_doallfunc dfn) \
  147. { \
  148. return (OPENSSL_LH_DOALL_FUNC)dfn; \
  149. } \
  150. LHASH_OF(type)
  151. # define DEFINE_LHASH_OF(type) \
  152. LHASH_OF(type) { union lh_##type##_dummy { void* d1; unsigned long d2; int d3; } dummy; }; \
  153. static ossl_unused ossl_inline LHASH_OF(type) *lh_##type##_new(unsigned long (*hfn)(const type *), \
  154. int (*cfn)(const type *, const type *)) \
  155. { \
  156. return (LHASH_OF(type) *) \
  157. OPENSSL_LH_new((OPENSSL_LH_HASHFUNC)hfn, (OPENSSL_LH_COMPFUNC)cfn); \
  158. } \
  159. static ossl_unused ossl_inline void lh_##type##_free(LHASH_OF(type) *lh) \
  160. { \
  161. OPENSSL_LH_free((OPENSSL_LHASH *)lh); \
  162. } \
  163. static ossl_unused ossl_inline void lh_##type##_flush(LHASH_OF(type) *lh) \
  164. { \
  165. OPENSSL_LH_flush((OPENSSL_LHASH *)lh); \
  166. } \
  167. static ossl_unused ossl_inline type *lh_##type##_insert(LHASH_OF(type) *lh, type *d) \
  168. { \
  169. return (type *)OPENSSL_LH_insert((OPENSSL_LHASH *)lh, d); \
  170. } \
  171. static ossl_unused ossl_inline type *lh_##type##_delete(LHASH_OF(type) *lh, const type *d) \
  172. { \
  173. return (type *)OPENSSL_LH_delete((OPENSSL_LHASH *)lh, d); \
  174. } \
  175. static ossl_unused ossl_inline type *lh_##type##_retrieve(LHASH_OF(type) *lh, const type *d) \
  176. { \
  177. return (type *)OPENSSL_LH_retrieve((OPENSSL_LHASH *)lh, d); \
  178. } \
  179. static ossl_unused ossl_inline int lh_##type##_error(LHASH_OF(type) *lh) \
  180. { \
  181. return OPENSSL_LH_error((OPENSSL_LHASH *)lh); \
  182. } \
  183. static ossl_unused ossl_inline unsigned long lh_##type##_num_items(LHASH_OF(type) *lh) \
  184. { \
  185. return OPENSSL_LH_num_items((OPENSSL_LHASH *)lh); \
  186. } \
  187. static ossl_unused ossl_inline void lh_##type##_node_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
  188. { \
  189. OPENSSL_LH_node_stats_bio((const OPENSSL_LHASH *)lh, out); \
  190. } \
  191. static ossl_unused ossl_inline void lh_##type##_node_usage_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
  192. { \
  193. OPENSSL_LH_node_usage_stats_bio((const OPENSSL_LHASH *)lh, out); \
  194. } \
  195. static ossl_unused ossl_inline void lh_##type##_stats_bio(const LHASH_OF(type) *lh, BIO *out) \
  196. { \
  197. OPENSSL_LH_stats_bio((const OPENSSL_LHASH *)lh, out); \
  198. } \
  199. static ossl_unused ossl_inline unsigned long lh_##type##_get_down_load(LHASH_OF(type) *lh) \
  200. { \
  201. return OPENSSL_LH_get_down_load((OPENSSL_LHASH *)lh); \
  202. } \
  203. static ossl_unused ossl_inline void lh_##type##_set_down_load(LHASH_OF(type) *lh, unsigned long dl) \
  204. { \
  205. OPENSSL_LH_set_down_load((OPENSSL_LHASH *)lh, dl); \
  206. } \
  207. static ossl_unused ossl_inline void lh_##type##_doall(LHASH_OF(type) *lh, \
  208. void (*doall)(type *)) \
  209. { \
  210. OPENSSL_LH_doall((OPENSSL_LHASH *)lh, (OPENSSL_LH_DOALL_FUNC)doall); \
  211. } \
  212. static ossl_unused ossl_inline void lh_##type##_doall_arg(LHASH_OF(type) *lh, \
  213. void (*doallarg)(type *, void *), \
  214. void *arg) \
  215. { \
  216. OPENSSL_LH_doall_arg((OPENSSL_LHASH *)lh, \
  217. (OPENSSL_LH_DOALL_FUNCARG)doallarg, arg); \
  218. } \
  219. LHASH_OF(type)
  220. #define IMPLEMENT_LHASH_DOALL_ARG_CONST(type, argtype) \
  221. int_implement_lhash_doall(type, argtype, const type)
  222. #define IMPLEMENT_LHASH_DOALL_ARG(type, argtype) \
  223. int_implement_lhash_doall(type, argtype, type)
  224. #define int_implement_lhash_doall(type, argtype, cbargtype) \
  225. static ossl_unused ossl_inline void \
  226. lh_##type##_doall_##argtype(LHASH_OF(type) *lh, \
  227. void (*fn)(cbargtype *, argtype *), \
  228. argtype *arg) \
  229. { \
  230. OPENSSL_LH_doall_arg((OPENSSL_LHASH *)lh, (OPENSSL_LH_DOALL_FUNCARG)fn, (void *)arg); \
  231. } \
  232. LHASH_OF(type)
  233. {-
  234. generate_lhash_macros("OPENSSL_STRING")
  235. .generate_lhash_macros("OPENSSL_CSTRING");
  236. -}
  237. #ifdef __cplusplus
  238. }
  239. #endif
  240. #endif