container.h 29 KB

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