container.h 29 KB

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