container.h 29 KB

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