container.h 31 KB

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