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