container.h 29 KB

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