container.h 28 KB

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