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