container.h 30 KB

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