map.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #ifndef TOR_MAP_H
  6. #define TOR_MAP_H
  7. /**
  8. * \file map.h
  9. *
  10. * \brief Headers for map.c.
  11. **/
  12. #include "lib/testsupport/testsupport.h"
  13. #include "lib/cc/torint.h"
  14. #include "siphash.h"
  15. #define DECLARE_MAP_FNS(maptype, keytype, prefix) \
  16. typedef struct maptype maptype; \
  17. typedef struct prefix##entry_t *prefix##iter_t; \
  18. MOCK_DECL(maptype*, prefix##new, (void)); \
  19. void* prefix##set(maptype *map, keytype key, void *val); \
  20. void* prefix##get(const maptype *map, keytype key); \
  21. void* prefix##remove(maptype *map, keytype key); \
  22. MOCK_DECL(void, prefix##free_, (maptype *map, void (*free_val)(void*))); \
  23. int prefix##isempty(const maptype *map); \
  24. int prefix##size(const maptype *map); \
  25. prefix##iter_t *prefix##iter_init(maptype *map); \
  26. prefix##iter_t *prefix##iter_next(maptype *map, prefix##iter_t *iter); \
  27. prefix##iter_t *prefix##iter_next_rmv(maptype *map, prefix##iter_t *iter); \
  28. void prefix##iter_get(prefix##iter_t *iter, keytype *keyp, void **valp); \
  29. int prefix##iter_done(prefix##iter_t *iter); \
  30. void prefix##assert_ok(const maptype *map)
  31. /* Map from const char * to void *. Implemented with a hash table. */
  32. DECLARE_MAP_FNS(strmap_t, const char *, strmap_);
  33. /* Map from const char[DIGEST_LEN] to void *. Implemented with a hash table. */
  34. DECLARE_MAP_FNS(digestmap_t, const char *, digestmap_);
  35. /* Map from const uint8_t[DIGEST256_LEN] to void *. Implemented with a hash
  36. * table. */
  37. DECLARE_MAP_FNS(digest256map_t, const uint8_t *, digest256map_);
  38. #define MAP_FREE_AND_NULL(maptype, map, fn) \
  39. do { \
  40. maptype ## _free_((map), (fn)); \
  41. (map) = NULL; \
  42. } while (0)
  43. #define strmap_free(map, fn) MAP_FREE_AND_NULL(strmap, (map), (fn))
  44. #define digestmap_free(map, fn) MAP_FREE_AND_NULL(digestmap, (map), (fn))
  45. #define digest256map_free(map, fn) MAP_FREE_AND_NULL(digest256map, (map), (fn))
  46. #undef DECLARE_MAP_FNS
  47. /** Iterates over the key-value pairs in a map <b>map</b> in order.
  48. * <b>prefix</b> is as for DECLARE_MAP_FNS (i.e., strmap_ or digestmap_).
  49. * The map's keys and values are of type keytype and valtype respectively;
  50. * each iteration assigns them to keyvar and valvar.
  51. *
  52. * Example use:
  53. * MAP_FOREACH(digestmap_, m, const char *, k, routerinfo_t *, r) {
  54. * // use k and r
  55. * } MAP_FOREACH_END.
  56. */
  57. /* Unpacks to, approximately:
  58. * {
  59. * digestmap_iter_t *k_iter;
  60. * for (k_iter = digestmap_iter_init(m); !digestmap_iter_done(k_iter);
  61. * k_iter = digestmap_iter_next(m, k_iter)) {
  62. * const char *k;
  63. * void *r_voidp;
  64. * routerinfo_t *r;
  65. * digestmap_iter_get(k_iter, &k, &r_voidp);
  66. * r = r_voidp;
  67. * // use k and r
  68. * }
  69. * }
  70. */
  71. #define MAP_FOREACH(prefix, map, keytype, keyvar, valtype, valvar) \
  72. STMT_BEGIN \
  73. prefix##iter_t *keyvar##_iter; \
  74. for (keyvar##_iter = prefix##iter_init(map); \
  75. !prefix##iter_done(keyvar##_iter); \
  76. keyvar##_iter = prefix##iter_next(map, keyvar##_iter)) { \
  77. keytype keyvar; \
  78. void *valvar##_voidp; \
  79. valtype valvar; \
  80. prefix##iter_get(keyvar##_iter, &keyvar, &valvar##_voidp); \
  81. valvar = valvar##_voidp;
  82. /** As MAP_FOREACH, except allows members to be removed from the map
  83. * during the iteration via MAP_DEL_CURRENT. Example use:
  84. *
  85. * Example use:
  86. * MAP_FOREACH(digestmap_, m, const char *, k, routerinfo_t *, r) {
  87. * if (is_very_old(r))
  88. * MAP_DEL_CURRENT(k);
  89. * } MAP_FOREACH_END.
  90. **/
  91. /* Unpacks to, approximately:
  92. * {
  93. * digestmap_iter_t *k_iter;
  94. * int k_del=0;
  95. * for (k_iter = digestmap_iter_init(m); !digestmap_iter_done(k_iter);
  96. * k_iter = k_del ? digestmap_iter_next(m, k_iter)
  97. * : digestmap_iter_next_rmv(m, k_iter)) {
  98. * const char *k;
  99. * void *r_voidp;
  100. * routerinfo_t *r;
  101. * k_del=0;
  102. * digestmap_iter_get(k_iter, &k, &r_voidp);
  103. * r = r_voidp;
  104. * if (is_very_old(r)) {
  105. * k_del = 1;
  106. * }
  107. * }
  108. * }
  109. */
  110. #define MAP_FOREACH_MODIFY(prefix, map, keytype, keyvar, valtype, valvar) \
  111. STMT_BEGIN \
  112. prefix##iter_t *keyvar##_iter; \
  113. int keyvar##_del=0; \
  114. for (keyvar##_iter = prefix##iter_init(map); \
  115. !prefix##iter_done(keyvar##_iter); \
  116. keyvar##_iter = keyvar##_del ? \
  117. prefix##iter_next_rmv(map, keyvar##_iter) : \
  118. prefix##iter_next(map, keyvar##_iter)) { \
  119. keytype keyvar; \
  120. void *valvar##_voidp; \
  121. valtype valvar; \
  122. keyvar##_del=0; \
  123. prefix##iter_get(keyvar##_iter, &keyvar, &valvar##_voidp); \
  124. valvar = valvar##_voidp;
  125. /** Used with MAP_FOREACH_MODIFY to remove the currently-iterated-upon
  126. * member of the map. */
  127. #define MAP_DEL_CURRENT(keyvar) \
  128. STMT_BEGIN \
  129. keyvar##_del = 1; \
  130. STMT_END
  131. /** Used to end a MAP_FOREACH() block. */
  132. #define MAP_FOREACH_END } STMT_END ;
  133. /** As MAP_FOREACH, but does not require declaration of prefix or keytype.
  134. * Example use:
  135. * DIGESTMAP_FOREACH(m, k, routerinfo_t *, r) {
  136. * // use k and r
  137. * } DIGESTMAP_FOREACH_END.
  138. */
  139. #define DIGESTMAP_FOREACH(map, keyvar, valtype, valvar) \
  140. MAP_FOREACH(digestmap_, map, const char *, keyvar, valtype, valvar)
  141. /** As MAP_FOREACH_MODIFY, but does not require declaration of prefix or
  142. * keytype.
  143. * Example use:
  144. * DIGESTMAP_FOREACH_MODIFY(m, k, routerinfo_t *, r) {
  145. * if (is_very_old(r))
  146. * MAP_DEL_CURRENT(k);
  147. * } DIGESTMAP_FOREACH_END.
  148. */
  149. #define DIGESTMAP_FOREACH_MODIFY(map, keyvar, valtype, valvar) \
  150. MAP_FOREACH_MODIFY(digestmap_, map, const char *, keyvar, valtype, valvar)
  151. /** Used to end a DIGESTMAP_FOREACH() block. */
  152. #define DIGESTMAP_FOREACH_END MAP_FOREACH_END
  153. #define DIGEST256MAP_FOREACH(map, keyvar, valtype, valvar) \
  154. MAP_FOREACH(digest256map_, map, const uint8_t *, keyvar, valtype, valvar)
  155. #define DIGEST256MAP_FOREACH_MODIFY(map, keyvar, valtype, valvar) \
  156. MAP_FOREACH_MODIFY(digest256map_, map, const uint8_t *, \
  157. keyvar, valtype, valvar)
  158. #define DIGEST256MAP_FOREACH_END MAP_FOREACH_END
  159. #define STRMAP_FOREACH(map, keyvar, valtype, valvar) \
  160. MAP_FOREACH(strmap_, map, const char *, keyvar, valtype, valvar)
  161. #define STRMAP_FOREACH_MODIFY(map, keyvar, valtype, valvar) \
  162. MAP_FOREACH_MODIFY(strmap_, map, const char *, keyvar, valtype, valvar)
  163. #define STRMAP_FOREACH_END MAP_FOREACH_END
  164. void* strmap_set_lc(strmap_t *map, const char *key, void *val);
  165. void* strmap_get_lc(const strmap_t *map, const char *key);
  166. void* strmap_remove_lc(strmap_t *map, const char *key);
  167. #define DECLARE_TYPED_DIGESTMAP_FNS(prefix, maptype, valtype) \
  168. typedef struct maptype maptype; \
  169. typedef struct prefix##iter_t *prefix##iter_t; \
  170. ATTR_UNUSED static inline maptype* \
  171. prefix##new(void) \
  172. { \
  173. return (maptype*)digestmap_new(); \
  174. } \
  175. ATTR_UNUSED static inline digestmap_t* \
  176. prefix##to_digestmap(maptype *map) \
  177. { \
  178. return (digestmap_t*)map; \
  179. } \
  180. ATTR_UNUSED static inline valtype* \
  181. prefix##get(maptype *map, const char *key) \
  182. { \
  183. return (valtype*)digestmap_get((digestmap_t*)map, key); \
  184. } \
  185. ATTR_UNUSED static inline valtype* \
  186. prefix##set(maptype *map, const char *key, valtype *val) \
  187. { \
  188. return (valtype*)digestmap_set((digestmap_t*)map, key, val); \
  189. } \
  190. ATTR_UNUSED static inline valtype* \
  191. prefix##remove(maptype *map, const char *key) \
  192. { \
  193. return (valtype*)digestmap_remove((digestmap_t*)map, key); \
  194. } \
  195. ATTR_UNUSED static inline void \
  196. prefix##f##ree_(maptype *map, void (*free_val)(void*)) \
  197. { \
  198. digestmap_free_((digestmap_t*)map, free_val); \
  199. } \
  200. ATTR_UNUSED static inline int \
  201. prefix##isempty(maptype *map) \
  202. { \
  203. return digestmap_isempty((digestmap_t*)map); \
  204. } \
  205. ATTR_UNUSED static inline int \
  206. prefix##size(maptype *map) \
  207. { \
  208. return digestmap_size((digestmap_t*)map); \
  209. } \
  210. ATTR_UNUSED static inline \
  211. prefix##iter_t *prefix##iter_init(maptype *map) \
  212. { \
  213. return (prefix##iter_t*) digestmap_iter_init((digestmap_t*)map); \
  214. } \
  215. ATTR_UNUSED static inline \
  216. prefix##iter_t *prefix##iter_next(maptype *map, prefix##iter_t *iter) \
  217. { \
  218. return (prefix##iter_t*) digestmap_iter_next( \
  219. (digestmap_t*)map, (digestmap_iter_t*)iter); \
  220. } \
  221. ATTR_UNUSED static inline prefix##iter_t* \
  222. prefix##iter_next_rmv(maptype *map, prefix##iter_t *iter) \
  223. { \
  224. return (prefix##iter_t*) digestmap_iter_next_rmv( \
  225. (digestmap_t*)map, (digestmap_iter_t*)iter); \
  226. } \
  227. ATTR_UNUSED static inline void \
  228. prefix##iter_get(prefix##iter_t *iter, \
  229. const char **keyp, \
  230. valtype **valp) \
  231. { \
  232. void *v; \
  233. digestmap_iter_get((digestmap_iter_t*) iter, keyp, &v); \
  234. *valp = v; \
  235. } \
  236. ATTR_UNUSED static inline int \
  237. prefix##iter_done(prefix##iter_t *iter) \
  238. { \
  239. return digestmap_iter_done((digestmap_iter_t*)iter); \
  240. }
  241. #endif /* !defined(TOR_CONTAINER_H) */