container.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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_CONTAINER_H
  6. #define TOR_CONTAINER_H
  7. #include <stddef.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "lib/cc/compat_compiler.h"
  11. #include "lib/cc/torint.h"
  12. #include "lib/testsupport/testsupport.h"
  13. #include "lib/malloc/util_malloc.h"
  14. #include "common/util_bug.h"
  15. #include "siphash.h"
  16. /** A resizeable list of pointers, with associated helpful functionality.
  17. *
  18. * The members of this struct are exposed only so that macros and inlines can
  19. * use them; all access to smartlist internals should go through the functions
  20. * and macros defined here.
  21. **/
  22. typedef struct smartlist_t {
  23. /** @{ */
  24. /** <b>list</b> has enough capacity to store exactly <b>capacity</b> elements
  25. * before it needs to be resized. Only the first <b>num_used</b> (\<=
  26. * capacity) elements point to valid data.
  27. */
  28. void **list;
  29. int num_used;
  30. int capacity;
  31. /** @} */
  32. } smartlist_t;
  33. MOCK_DECL(smartlist_t *, smartlist_new, (void));
  34. MOCK_DECL(void, smartlist_free_, (smartlist_t *sl));
  35. #define smartlist_free(sl) FREE_AND_NULL(smartlist_t, smartlist_free_, (sl))
  36. void smartlist_clear(smartlist_t *sl);
  37. void smartlist_add(smartlist_t *sl, void *element);
  38. void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2);
  39. void smartlist_add_strdup(struct smartlist_t *sl, const char *string);
  40. void smartlist_remove(smartlist_t *sl, const void *element);
  41. void smartlist_remove_keeporder(smartlist_t *sl, const void *element);
  42. void *smartlist_pop_last(smartlist_t *sl);
  43. void smartlist_reverse(smartlist_t *sl);
  44. void smartlist_string_remove(smartlist_t *sl, const char *element);
  45. int smartlist_contains(const smartlist_t *sl, const void *element);
  46. int smartlist_contains_string(const smartlist_t *sl, const char *element);
  47. int smartlist_pos(const smartlist_t *sl, const void *element);
  48. int smartlist_string_pos(const smartlist_t *, const char *elt);
  49. int smartlist_contains_string_case(const smartlist_t *sl, const char *element);
  50. int smartlist_contains_int_as_string(const smartlist_t *sl, int num);
  51. int smartlist_strings_eq(const smartlist_t *sl1, const smartlist_t *sl2);
  52. int smartlist_contains_digest(const smartlist_t *sl, const char *element);
  53. int smartlist_ints_eq(const smartlist_t *sl1, const smartlist_t *sl2);
  54. int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2);
  55. void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
  56. void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
  57. /* smartlist_choose() is defined in crypto.[ch] */
  58. #ifdef DEBUG_SMARTLIST
  59. /** Return the number of items in sl.
  60. */
  61. static inline int smartlist_len(const smartlist_t *sl);
  62. static inline int smartlist_len(const smartlist_t *sl) {
  63. tor_assert(sl);
  64. return (sl)->num_used;
  65. }
  66. /** Return the <b>idx</b>th element of sl.
  67. */
  68. static inline void *smartlist_get(const smartlist_t *sl, int idx);
  69. static inline void *smartlist_get(const smartlist_t *sl, int idx) {
  70. tor_assert(sl);
  71. tor_assert(idx>=0);
  72. tor_assert(sl->num_used > idx);
  73. return sl->list[idx];
  74. }
  75. static inline void smartlist_set(smartlist_t *sl, int idx, void *val) {
  76. tor_assert(sl);
  77. tor_assert(idx>=0);
  78. tor_assert(sl->num_used > idx);
  79. sl->list[idx] = val;
  80. }
  81. #else /* !(defined(DEBUG_SMARTLIST)) */
  82. #define smartlist_len(sl) ((sl)->num_used)
  83. #define smartlist_get(sl, idx) ((sl)->list[idx])
  84. #define smartlist_set(sl, idx, val) ((sl)->list[idx] = (val))
  85. #endif /* defined(DEBUG_SMARTLIST) */
  86. /** Exchange the elements at indices <b>idx1</b> and <b>idx2</b> of the
  87. * smartlist <b>sl</b>. */
  88. static inline void smartlist_swap(smartlist_t *sl, int idx1, int idx2)
  89. {
  90. if (idx1 != idx2) {
  91. void *elt = smartlist_get(sl, idx1);
  92. smartlist_set(sl, idx1, smartlist_get(sl, idx2));
  93. smartlist_set(sl, idx2, elt);
  94. }
  95. }
  96. void smartlist_del(smartlist_t *sl, int idx);
  97. void smartlist_del_keeporder(smartlist_t *sl, int idx);
  98. void smartlist_insert(smartlist_t *sl, int idx, void *val);
  99. void smartlist_sort(smartlist_t *sl,
  100. int (*compare)(const void **a, const void **b));
  101. void *smartlist_get_most_frequent_(const smartlist_t *sl,
  102. int (*compare)(const void **a, const void **b),
  103. int *count_out);
  104. #define smartlist_get_most_frequent(sl, compare) \
  105. smartlist_get_most_frequent_((sl), (compare), NULL)
  106. void smartlist_uniq(smartlist_t *sl,
  107. int (*compare)(const void **a, const void **b),
  108. void (*free_fn)(void *elt));
  109. void smartlist_sort_strings(smartlist_t *sl);
  110. void smartlist_sort_digests(smartlist_t *sl);
  111. void smartlist_sort_digests256(smartlist_t *sl);
  112. void smartlist_sort_pointers(smartlist_t *sl);
  113. const char *smartlist_get_most_frequent_string(smartlist_t *sl);
  114. const char *smartlist_get_most_frequent_string_(smartlist_t *sl,
  115. int *count_out);
  116. const uint8_t *smartlist_get_most_frequent_digest256(smartlist_t *sl);
  117. void smartlist_uniq_strings(smartlist_t *sl);
  118. void smartlist_uniq_digests(smartlist_t *sl);
  119. void smartlist_uniq_digests256(smartlist_t *sl);
  120. void *smartlist_bsearch(smartlist_t *sl, const void *key,
  121. int (*compare)(const void *key, const void **member));
  122. int smartlist_bsearch_idx(const smartlist_t *sl, const void *key,
  123. int (*compare)(const void *key, const void **member),
  124. int *found_out);
  125. void smartlist_pqueue_add(smartlist_t *sl,
  126. int (*compare)(const void *a, const void *b),
  127. int idx_field_offset,
  128. void *item);
  129. void *smartlist_pqueue_pop(smartlist_t *sl,
  130. int (*compare)(const void *a, const void *b),
  131. int idx_field_offset);
  132. void smartlist_pqueue_remove(smartlist_t *sl,
  133. int (*compare)(const void *a, const void *b),
  134. int idx_field_offset,
  135. void *item);
  136. void smartlist_pqueue_assert_ok(smartlist_t *sl,
  137. int (*compare)(const void *a, const void *b),
  138. int idx_field_offset);
  139. #define SPLIT_SKIP_SPACE 0x01
  140. #define SPLIT_IGNORE_BLANK 0x02
  141. #define SPLIT_STRIP_SPACE 0x04
  142. int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
  143. int flags, int max);
  144. char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate,
  145. size_t *len_out) ATTR_MALLOC;
  146. char *smartlist_join_strings2(smartlist_t *sl, const char *join,
  147. size_t join_len, int terminate, size_t *len_out)
  148. ATTR_MALLOC;
  149. /** Iterate over the items in a smartlist <b>sl</b>, in order. For each item,
  150. * assign it to a new local variable of type <b>type</b> named <b>var</b>, and
  151. * execute the statements inside the loop body. Inside the loop, the loop
  152. * index can be accessed as <b>var</b>_sl_idx and the length of the list can
  153. * be accessed as <b>var</b>_sl_len.
  154. *
  155. * NOTE: Do not change the length of the list while the loop is in progress,
  156. * unless you adjust the _sl_len variable correspondingly. See second example
  157. * below.
  158. *
  159. * Example use:
  160. * <pre>
  161. * smartlist_t *list = smartlist_split("A:B:C", ":", 0, 0);
  162. * SMARTLIST_FOREACH_BEGIN(list, char *, cp) {
  163. * printf("%d: %s\n", cp_sl_idx, cp);
  164. * tor_free(cp);
  165. * } SMARTLIST_FOREACH_END(cp);
  166. * smartlist_free(list);
  167. * </pre>
  168. *
  169. * Example use (advanced):
  170. * <pre>
  171. * SMARTLIST_FOREACH_BEGIN(list, char *, cp) {
  172. * if (!strcmp(cp, "junk")) {
  173. * tor_free(cp);
  174. * SMARTLIST_DEL_CURRENT(list, cp);
  175. * }
  176. * } SMARTLIST_FOREACH_END(cp);
  177. * </pre>
  178. */
  179. /* Note: these macros use token pasting, and reach into smartlist internals.
  180. * This can make them a little daunting. Here's the approximate unpacking of
  181. * the above examples, for entertainment value:
  182. *
  183. * <pre>
  184. * smartlist_t *list = smartlist_split("A:B:C", ":", 0, 0);
  185. * {
  186. * int cp_sl_idx, cp_sl_len = smartlist_len(list);
  187. * char *cp;
  188. * for (cp_sl_idx = 0; cp_sl_idx < cp_sl_len; ++cp_sl_idx) {
  189. * cp = smartlist_get(list, cp_sl_idx);
  190. * printf("%d: %s\n", cp_sl_idx, cp);
  191. * tor_free(cp);
  192. * }
  193. * }
  194. * smartlist_free(list);
  195. * </pre>
  196. *
  197. * <pre>
  198. * {
  199. * int cp_sl_idx, cp_sl_len = smartlist_len(list);
  200. * char *cp;
  201. * for (cp_sl_idx = 0; cp_sl_idx < cp_sl_len; ++cp_sl_idx) {
  202. * cp = smartlist_get(list, cp_sl_idx);
  203. * if (!strcmp(cp, "junk")) {
  204. * tor_free(cp);
  205. * smartlist_del(list, cp_sl_idx);
  206. * --cp_sl_idx;
  207. * --cp_sl_len;
  208. * }
  209. * }
  210. * }
  211. * </pre>
  212. */
  213. #define SMARTLIST_FOREACH_BEGIN(sl, type, var) \
  214. STMT_BEGIN \
  215. int var ## _sl_idx, var ## _sl_len=(sl)->num_used; \
  216. type var; \
  217. for (var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len; \
  218. ++var ## _sl_idx) { \
  219. var = (sl)->list[var ## _sl_idx];
  220. #define SMARTLIST_FOREACH_END(var) \
  221. var = NULL; \
  222. (void) var ## _sl_idx; \
  223. } STMT_END
  224. /**
  225. * An alias for SMARTLIST_FOREACH_BEGIN and SMARTLIST_FOREACH_END, using
  226. * <b>cmd</b> as the loop body. This wrapper is here for convenience with
  227. * very short loops.
  228. *
  229. * By convention, we do not use this for loops which nest, or for loops over
  230. * 10 lines or so. Use SMARTLIST_FOREACH_{BEGIN,END} for those.
  231. */
  232. #define SMARTLIST_FOREACH(sl, type, var, cmd) \
  233. SMARTLIST_FOREACH_BEGIN(sl,type,var) { \
  234. cmd; \
  235. } SMARTLIST_FOREACH_END(var)
  236. /** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed
  237. * with the variable <b>var</b>, remove the current element in a way that
  238. * won't confuse the loop. */
  239. #define SMARTLIST_DEL_CURRENT(sl, var) \
  240. STMT_BEGIN \
  241. smartlist_del(sl, var ## _sl_idx); \
  242. --var ## _sl_idx; \
  243. --var ## _sl_len; \
  244. STMT_END
  245. /** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed
  246. * with the variable <b>var</b>, remove the current element in a way that
  247. * won't confuse the loop. */
  248. #define SMARTLIST_DEL_CURRENT_KEEPORDER(sl, var) \
  249. STMT_BEGIN \
  250. smartlist_del_keeporder(sl, var ## _sl_idx); \
  251. --var ## _sl_idx; \
  252. --var ## _sl_len; \
  253. STMT_END
  254. /** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed
  255. * with the variable <b>var</b>, replace the current element with <b>val</b>.
  256. * Does not deallocate the current value of <b>var</b>.
  257. */
  258. #define SMARTLIST_REPLACE_CURRENT(sl, var, val) \
  259. STMT_BEGIN \
  260. smartlist_set(sl, var ## _sl_idx, val); \
  261. STMT_END
  262. /* Helper: Given two lists of items, possibly of different types, such that
  263. * both lists are sorted on some common field (as determined by a comparison
  264. * expression <b>cmpexpr</b>), and such that one list (<b>sl1</b>) has no
  265. * duplicates on the common field, loop through the lists in lockstep, and
  266. * execute <b>unmatched_var2</b> on items in var2 that do not appear in
  267. * var1.
  268. *
  269. * WARNING: It isn't safe to add remove elements from either list while the
  270. * loop is in progress.
  271. *
  272. * Example use:
  273. * SMARTLIST_FOREACH_JOIN(routerstatus_list, routerstatus_t *, rs,
  274. * routerinfo_list, routerinfo_t *, ri,
  275. * tor_memcmp(rs->identity_digest, ri->identity_digest, 20),
  276. * log_info(LD_GENERAL,"No match for %s", ri->nickname)) {
  277. * log_info(LD_GENERAL, "%s matches routerstatus %p", ri->nickname, rs);
  278. * } SMARTLIST_FOREACH_JOIN_END(rs, ri);
  279. **/
  280. /* The example above unpacks (approximately) to:
  281. * int rs_sl_idx = 0, rs_sl_len = smartlist_len(routerstatus_list);
  282. * int ri_sl_idx, ri_sl_len = smartlist_len(routerinfo_list);
  283. * int rs_ri_cmp;
  284. * routerstatus_t *rs;
  285. * routerinfo_t *ri;
  286. * for (; ri_sl_idx < ri_sl_len; ++ri_sl_idx) {
  287. * ri = smartlist_get(routerinfo_list, ri_sl_idx);
  288. * while (rs_sl_idx < rs_sl_len) {
  289. * rs = smartlist_get(routerstatus_list, rs_sl_idx);
  290. * rs_ri_cmp = tor_memcmp(rs->identity_digest, ri->identity_digest, 20);
  291. * if (rs_ri_cmp > 0) {
  292. * break;
  293. * } else if (rs_ri_cmp == 0) {
  294. * goto matched_ri;
  295. * } else {
  296. * ++rs_sl_idx;
  297. * }
  298. * }
  299. * log_info(LD_GENERAL,"No match for %s", ri->nickname);
  300. * continue;
  301. * matched_ri: {
  302. * log_info(LD_GENERAL,"%s matches with routerstatus %p",ri->nickname,rs);
  303. * }
  304. * }
  305. */
  306. #define SMARTLIST_FOREACH_JOIN(sl1, type1, var1, sl2, type2, var2, \
  307. cmpexpr, unmatched_var2) \
  308. STMT_BEGIN \
  309. int var1 ## _sl_idx = 0, var1 ## _sl_len=(sl1)->num_used; \
  310. int var2 ## _sl_idx = 0, var2 ## _sl_len=(sl2)->num_used; \
  311. int var1 ## _ ## var2 ## _cmp; \
  312. type1 var1; \
  313. type2 var2; \
  314. for (; var2##_sl_idx < var2##_sl_len; ++var2##_sl_idx) { \
  315. var2 = (sl2)->list[var2##_sl_idx]; \
  316. while (var1##_sl_idx < var1##_sl_len) { \
  317. var1 = (sl1)->list[var1##_sl_idx]; \
  318. var1##_##var2##_cmp = (cmpexpr); \
  319. if (var1##_##var2##_cmp > 0) { \
  320. break; \
  321. } else if (var1##_##var2##_cmp == 0) { \
  322. goto matched_##var2; \
  323. } else { \
  324. ++var1##_sl_idx; \
  325. } \
  326. } \
  327. /* Ran out of v1, or no match for var2. */ \
  328. unmatched_var2; \
  329. continue; \
  330. matched_##var2: ; \
  331. #define SMARTLIST_FOREACH_JOIN_END(var1, var2) \
  332. } \
  333. STMT_END
  334. #define DECLARE_MAP_FNS(maptype, keytype, prefix) \
  335. typedef struct maptype maptype; \
  336. typedef struct prefix##entry_t *prefix##iter_t; \
  337. MOCK_DECL(maptype*, prefix##new, (void)); \
  338. void* prefix##set(maptype *map, keytype key, void *val); \
  339. void* prefix##get(const maptype *map, keytype key); \
  340. void* prefix##remove(maptype *map, keytype key); \
  341. MOCK_DECL(void, prefix##free_, (maptype *map, void (*free_val)(void*))); \
  342. int prefix##isempty(const maptype *map); \
  343. int prefix##size(const maptype *map); \
  344. prefix##iter_t *prefix##iter_init(maptype *map); \
  345. prefix##iter_t *prefix##iter_next(maptype *map, prefix##iter_t *iter); \
  346. prefix##iter_t *prefix##iter_next_rmv(maptype *map, prefix##iter_t *iter); \
  347. void prefix##iter_get(prefix##iter_t *iter, keytype *keyp, void **valp); \
  348. int prefix##iter_done(prefix##iter_t *iter); \
  349. void prefix##assert_ok(const maptype *map)
  350. /* Map from const char * to void *. Implemented with a hash table. */
  351. DECLARE_MAP_FNS(strmap_t, const char *, strmap_);
  352. /* Map from const char[DIGEST_LEN] to void *. Implemented with a hash table. */
  353. DECLARE_MAP_FNS(digestmap_t, const char *, digestmap_);
  354. /* Map from const uint8_t[DIGEST256_LEN] to void *. Implemented with a hash
  355. * table. */
  356. DECLARE_MAP_FNS(digest256map_t, const uint8_t *, digest256map_);
  357. #define MAP_FREE_AND_NULL(maptype, map, fn) \
  358. do { \
  359. maptype ## _free_((map), (fn)); \
  360. (map) = NULL; \
  361. } while (0)
  362. #define strmap_free(map, fn) MAP_FREE_AND_NULL(strmap, (map), (fn))
  363. #define digestmap_free(map, fn) MAP_FREE_AND_NULL(digestmap, (map), (fn))
  364. #define digest256map_free(map, fn) MAP_FREE_AND_NULL(digest256map, (map), (fn))
  365. #undef DECLARE_MAP_FNS
  366. /** Iterates over the key-value pairs in a map <b>map</b> in order.
  367. * <b>prefix</b> is as for DECLARE_MAP_FNS (i.e., strmap_ or digestmap_).
  368. * The map's keys and values are of type keytype and valtype respectively;
  369. * each iteration assigns them to keyvar and valvar.
  370. *
  371. * Example use:
  372. * MAP_FOREACH(digestmap_, m, const char *, k, routerinfo_t *, r) {
  373. * // use k and r
  374. * } MAP_FOREACH_END.
  375. */
  376. /* Unpacks to, approximately:
  377. * {
  378. * digestmap_iter_t *k_iter;
  379. * for (k_iter = digestmap_iter_init(m); !digestmap_iter_done(k_iter);
  380. * k_iter = digestmap_iter_next(m, k_iter)) {
  381. * const char *k;
  382. * void *r_voidp;
  383. * routerinfo_t *r;
  384. * digestmap_iter_get(k_iter, &k, &r_voidp);
  385. * r = r_voidp;
  386. * // use k and r
  387. * }
  388. * }
  389. */
  390. #define MAP_FOREACH(prefix, map, keytype, keyvar, valtype, valvar) \
  391. STMT_BEGIN \
  392. prefix##iter_t *keyvar##_iter; \
  393. for (keyvar##_iter = prefix##iter_init(map); \
  394. !prefix##iter_done(keyvar##_iter); \
  395. keyvar##_iter = prefix##iter_next(map, keyvar##_iter)) { \
  396. keytype keyvar; \
  397. void *valvar##_voidp; \
  398. valtype valvar; \
  399. prefix##iter_get(keyvar##_iter, &keyvar, &valvar##_voidp); \
  400. valvar = valvar##_voidp;
  401. /** As MAP_FOREACH, except allows members to be removed from the map
  402. * during the iteration via MAP_DEL_CURRENT. Example use:
  403. *
  404. * Example use:
  405. * MAP_FOREACH(digestmap_, m, const char *, k, routerinfo_t *, r) {
  406. * if (is_very_old(r))
  407. * MAP_DEL_CURRENT(k);
  408. * } MAP_FOREACH_END.
  409. **/
  410. /* Unpacks to, approximately:
  411. * {
  412. * digestmap_iter_t *k_iter;
  413. * int k_del=0;
  414. * for (k_iter = digestmap_iter_init(m); !digestmap_iter_done(k_iter);
  415. * k_iter = k_del ? digestmap_iter_next(m, k_iter)
  416. * : digestmap_iter_next_rmv(m, k_iter)) {
  417. * const char *k;
  418. * void *r_voidp;
  419. * routerinfo_t *r;
  420. * k_del=0;
  421. * digestmap_iter_get(k_iter, &k, &r_voidp);
  422. * r = r_voidp;
  423. * if (is_very_old(r)) {
  424. * k_del = 1;
  425. * }
  426. * }
  427. * }
  428. */
  429. #define MAP_FOREACH_MODIFY(prefix, map, keytype, keyvar, valtype, valvar) \
  430. STMT_BEGIN \
  431. prefix##iter_t *keyvar##_iter; \
  432. int keyvar##_del=0; \
  433. for (keyvar##_iter = prefix##iter_init(map); \
  434. !prefix##iter_done(keyvar##_iter); \
  435. keyvar##_iter = keyvar##_del ? \
  436. prefix##iter_next_rmv(map, keyvar##_iter) : \
  437. prefix##iter_next(map, keyvar##_iter)) { \
  438. keytype keyvar; \
  439. void *valvar##_voidp; \
  440. valtype valvar; \
  441. keyvar##_del=0; \
  442. prefix##iter_get(keyvar##_iter, &keyvar, &valvar##_voidp); \
  443. valvar = valvar##_voidp;
  444. /** Used with MAP_FOREACH_MODIFY to remove the currently-iterated-upon
  445. * member of the map. */
  446. #define MAP_DEL_CURRENT(keyvar) \
  447. STMT_BEGIN \
  448. keyvar##_del = 1; \
  449. STMT_END
  450. /** Used to end a MAP_FOREACH() block. */
  451. #define MAP_FOREACH_END } STMT_END ;
  452. /** As MAP_FOREACH, but does not require declaration of prefix or keytype.
  453. * Example use:
  454. * DIGESTMAP_FOREACH(m, k, routerinfo_t *, r) {
  455. * // use k and r
  456. * } DIGESTMAP_FOREACH_END.
  457. */
  458. #define DIGESTMAP_FOREACH(map, keyvar, valtype, valvar) \
  459. MAP_FOREACH(digestmap_, map, const char *, keyvar, valtype, valvar)
  460. /** As MAP_FOREACH_MODIFY, but does not require declaration of prefix or
  461. * keytype.
  462. * Example use:
  463. * DIGESTMAP_FOREACH_MODIFY(m, k, routerinfo_t *, r) {
  464. * if (is_very_old(r))
  465. * MAP_DEL_CURRENT(k);
  466. * } DIGESTMAP_FOREACH_END.
  467. */
  468. #define DIGESTMAP_FOREACH_MODIFY(map, keyvar, valtype, valvar) \
  469. MAP_FOREACH_MODIFY(digestmap_, map, const char *, keyvar, valtype, valvar)
  470. /** Used to end a DIGESTMAP_FOREACH() block. */
  471. #define DIGESTMAP_FOREACH_END MAP_FOREACH_END
  472. #define DIGEST256MAP_FOREACH(map, keyvar, valtype, valvar) \
  473. MAP_FOREACH(digest256map_, map, const uint8_t *, keyvar, valtype, valvar)
  474. #define DIGEST256MAP_FOREACH_MODIFY(map, keyvar, valtype, valvar) \
  475. MAP_FOREACH_MODIFY(digest256map_, map, const uint8_t *, \
  476. keyvar, valtype, valvar)
  477. #define DIGEST256MAP_FOREACH_END MAP_FOREACH_END
  478. #define STRMAP_FOREACH(map, keyvar, valtype, valvar) \
  479. MAP_FOREACH(strmap_, map, const char *, keyvar, valtype, valvar)
  480. #define STRMAP_FOREACH_MODIFY(map, keyvar, valtype, valvar) \
  481. MAP_FOREACH_MODIFY(strmap_, map, const char *, keyvar, valtype, valvar)
  482. #define STRMAP_FOREACH_END MAP_FOREACH_END
  483. void* strmap_set_lc(strmap_t *map, const char *key, void *val);
  484. void* strmap_get_lc(const strmap_t *map, const char *key);
  485. void* strmap_remove_lc(strmap_t *map, const char *key);
  486. #define DECLARE_TYPED_DIGESTMAP_FNS(prefix, maptype, valtype) \
  487. typedef struct maptype maptype; \
  488. typedef struct prefix##iter_t *prefix##iter_t; \
  489. ATTR_UNUSED static inline maptype* \
  490. prefix##new(void) \
  491. { \
  492. return (maptype*)digestmap_new(); \
  493. } \
  494. ATTR_UNUSED static inline digestmap_t* \
  495. prefix##to_digestmap(maptype *map) \
  496. { \
  497. return (digestmap_t*)map; \
  498. } \
  499. ATTR_UNUSED static inline valtype* \
  500. prefix##get(maptype *map, const char *key) \
  501. { \
  502. return (valtype*)digestmap_get((digestmap_t*)map, key); \
  503. } \
  504. ATTR_UNUSED static inline valtype* \
  505. prefix##set(maptype *map, const char *key, valtype *val) \
  506. { \
  507. return (valtype*)digestmap_set((digestmap_t*)map, key, val); \
  508. } \
  509. ATTR_UNUSED static inline valtype* \
  510. prefix##remove(maptype *map, const char *key) \
  511. { \
  512. return (valtype*)digestmap_remove((digestmap_t*)map, key); \
  513. } \
  514. ATTR_UNUSED static inline void \
  515. prefix##f##ree_(maptype *map, void (*free_val)(void*)) \
  516. { \
  517. digestmap_free_((digestmap_t*)map, free_val); \
  518. } \
  519. ATTR_UNUSED static inline int \
  520. prefix##isempty(maptype *map) \
  521. { \
  522. return digestmap_isempty((digestmap_t*)map); \
  523. } \
  524. ATTR_UNUSED static inline int \
  525. prefix##size(maptype *map) \
  526. { \
  527. return digestmap_size((digestmap_t*)map); \
  528. } \
  529. ATTR_UNUSED static inline \
  530. prefix##iter_t *prefix##iter_init(maptype *map) \
  531. { \
  532. return (prefix##iter_t*) digestmap_iter_init((digestmap_t*)map); \
  533. } \
  534. ATTR_UNUSED static inline \
  535. prefix##iter_t *prefix##iter_next(maptype *map, prefix##iter_t *iter) \
  536. { \
  537. return (prefix##iter_t*) digestmap_iter_next( \
  538. (digestmap_t*)map, (digestmap_iter_t*)iter); \
  539. } \
  540. ATTR_UNUSED static inline prefix##iter_t* \
  541. prefix##iter_next_rmv(maptype *map, prefix##iter_t *iter) \
  542. { \
  543. return (prefix##iter_t*) digestmap_iter_next_rmv( \
  544. (digestmap_t*)map, (digestmap_iter_t*)iter); \
  545. } \
  546. ATTR_UNUSED static inline void \
  547. prefix##iter_get(prefix##iter_t *iter, \
  548. const char **keyp, \
  549. valtype **valp) \
  550. { \
  551. void *v; \
  552. digestmap_iter_get((digestmap_iter_t*) iter, keyp, &v); \
  553. *valp = v; \
  554. } \
  555. ATTR_UNUSED static inline int \
  556. prefix##iter_done(prefix##iter_t *iter) \
  557. { \
  558. return digestmap_iter_done((digestmap_iter_t*)iter); \
  559. }
  560. #if SIZEOF_INT == 4
  561. #define BITARRAY_SHIFT 5
  562. #elif SIZEOF_INT == 8
  563. #define BITARRAY_SHIFT 6
  564. #else
  565. #error "int is neither 4 nor 8 bytes. I can't deal with that."
  566. #endif /* SIZEOF_INT == 4 || ... */
  567. #define BITARRAY_MASK ((1u<<BITARRAY_SHIFT)-1)
  568. /** A random-access array of one-bit-wide elements. */
  569. typedef unsigned int bitarray_t;
  570. /** Create a new bit array that can hold <b>n_bits</b> bits. */
  571. static inline bitarray_t *
  572. bitarray_init_zero(unsigned int n_bits)
  573. {
  574. /* round up to the next int. */
  575. size_t sz = (n_bits+BITARRAY_MASK) >> BITARRAY_SHIFT;
  576. return tor_calloc(sz, sizeof(unsigned int));
  577. }
  578. /** Expand <b>ba</b> from holding <b>n_bits_old</b> to <b>n_bits_new</b>,
  579. * clearing all new bits. Returns a possibly changed pointer to the
  580. * bitarray. */
  581. static inline bitarray_t *
  582. bitarray_expand(bitarray_t *ba,
  583. unsigned int n_bits_old, unsigned int n_bits_new)
  584. {
  585. size_t sz_old = (n_bits_old+BITARRAY_MASK) >> BITARRAY_SHIFT;
  586. size_t sz_new = (n_bits_new+BITARRAY_MASK) >> BITARRAY_SHIFT;
  587. char *ptr;
  588. if (sz_new <= sz_old)
  589. return ba;
  590. ptr = tor_reallocarray(ba, sz_new, sizeof(unsigned int));
  591. /* This memset does nothing to the older excess bytes. But they were
  592. * already set to 0 by bitarry_init_zero. */
  593. memset(ptr+sz_old*sizeof(unsigned int), 0,
  594. (sz_new-sz_old)*sizeof(unsigned int));
  595. return (bitarray_t*) ptr;
  596. }
  597. /** Free the bit array <b>ba</b>. */
  598. static inline void
  599. bitarray_free_(bitarray_t *ba)
  600. {
  601. tor_free(ba);
  602. }
  603. #define bitarray_free(ba) FREE_AND_NULL(bitarray_t, bitarray_free_, (ba))
  604. /** Set the <b>bit</b>th bit in <b>b</b> to 1. */
  605. static inline void
  606. bitarray_set(bitarray_t *b, int bit)
  607. {
  608. b[bit >> BITARRAY_SHIFT] |= (1u << (bit & BITARRAY_MASK));
  609. }
  610. /** Set the <b>bit</b>th bit in <b>b</b> to 0. */
  611. static inline void
  612. bitarray_clear(bitarray_t *b, int bit)
  613. {
  614. b[bit >> BITARRAY_SHIFT] &= ~ (1u << (bit & BITARRAY_MASK));
  615. }
  616. /** Return true iff <b>bit</b>th bit in <b>b</b> is nonzero. NOTE: does
  617. * not necessarily return 1 on true. */
  618. static inline unsigned int
  619. bitarray_is_set(bitarray_t *b, int bit)
  620. {
  621. return b[bit >> BITARRAY_SHIFT] & (1u << (bit & BITARRAY_MASK));
  622. }
  623. /** A set of digests, implemented as a Bloom filter. */
  624. typedef struct {
  625. int mask; /**< One less than the number of bits in <b>ba</b>; always one less
  626. * than a power of two. */
  627. bitarray_t *ba; /**< A bit array to implement the Bloom filter. */
  628. } digestset_t;
  629. #define BIT(n) ((n) & set->mask)
  630. /** Add the digest <b>digest</b> to <b>set</b>. */
  631. static inline void
  632. digestset_add(digestset_t *set, const char *digest)
  633. {
  634. const uint64_t x = siphash24g(digest, 20);
  635. const uint32_t d1 = (uint32_t) x;
  636. const uint32_t d2 = (uint32_t)( (x>>16) + x);
  637. const uint32_t d3 = (uint32_t)( (x>>32) + x);
  638. const uint32_t d4 = (uint32_t)( (x>>48) + x);
  639. bitarray_set(set->ba, BIT(d1));
  640. bitarray_set(set->ba, BIT(d2));
  641. bitarray_set(set->ba, BIT(d3));
  642. bitarray_set(set->ba, BIT(d4));
  643. }
  644. /** If <b>digest</b> is in <b>set</b>, return nonzero. Otherwise,
  645. * <em>probably</em> return zero. */
  646. static inline int
  647. digestset_contains(const digestset_t *set, const char *digest)
  648. {
  649. const uint64_t x = siphash24g(digest, 20);
  650. const uint32_t d1 = (uint32_t) x;
  651. const uint32_t d2 = (uint32_t)( (x>>16) + x);
  652. const uint32_t d3 = (uint32_t)( (x>>32) + x);
  653. const uint32_t d4 = (uint32_t)( (x>>48) + x);
  654. return bitarray_is_set(set->ba, BIT(d1)) &&
  655. bitarray_is_set(set->ba, BIT(d2)) &&
  656. bitarray_is_set(set->ba, BIT(d3)) &&
  657. bitarray_is_set(set->ba, BIT(d4));
  658. }
  659. #undef BIT
  660. digestset_t *digestset_new(int max_elements);
  661. void digestset_free_(digestset_t* set);
  662. #define digestset_free(set) FREE_AND_NULL(digestset_t, digestset_free_, (set))
  663. /* These functions, given an <b>array</b> of <b>n_elements</b>, return the
  664. * <b>nth</b> lowest element. <b>nth</b>=0 gives the lowest element;
  665. * <b>n_elements</b>-1 gives the highest; and (<b>n_elements</b>-1) / 2 gives
  666. * the median. As a side effect, the elements of <b>array</b> are sorted. */
  667. int find_nth_int(int *array, int n_elements, int nth);
  668. time_t find_nth_time(time_t *array, int n_elements, int nth);
  669. double find_nth_double(double *array, int n_elements, int nth);
  670. int32_t find_nth_int32(int32_t *array, int n_elements, int nth);
  671. uint32_t find_nth_uint32(uint32_t *array, int n_elements, int nth);
  672. long find_nth_long(long *array, int n_elements, int nth);
  673. static inline int
  674. median_int(int *array, int n_elements)
  675. {
  676. return find_nth_int(array, n_elements, (n_elements-1)/2);
  677. }
  678. static inline time_t
  679. median_time(time_t *array, int n_elements)
  680. {
  681. return find_nth_time(array, n_elements, (n_elements-1)/2);
  682. }
  683. static inline double
  684. median_double(double *array, int n_elements)
  685. {
  686. return find_nth_double(array, n_elements, (n_elements-1)/2);
  687. }
  688. static inline uint32_t
  689. median_uint32(uint32_t *array, int n_elements)
  690. {
  691. return find_nth_uint32(array, n_elements, (n_elements-1)/2);
  692. }
  693. static inline int32_t
  694. median_int32(int32_t *array, int n_elements)
  695. {
  696. return find_nth_int32(array, n_elements, (n_elements-1)/2);
  697. }
  698. static inline uint32_t
  699. third_quartile_uint32(uint32_t *array, int n_elements)
  700. {
  701. return find_nth_uint32(array, n_elements, (n_elements*3)/4);
  702. }
  703. #endif /* !defined(TOR_CONTAINER_H) */