smartlist.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #ifndef TOR_SMARTLIST_H
  6. #define TOR_SMARTLIST_H
  7. #include <stddef.h>
  8. #include <stdarg.h>
  9. #include "lib/cc/compat_compiler.h"
  10. #include "lib/cc/torint.h"
  11. #include "lib/testsupport/testsupport.h"
  12. /** A resizeable list of pointers, with associated helpful functionality.
  13. *
  14. * The members of this struct are exposed only so that macros and inlines can
  15. * use them; all access to smartlist internals should go through the functions
  16. * and macros defined here.
  17. **/
  18. typedef struct smartlist_t {
  19. /** @{ */
  20. /** <b>list</b> has enough capacity to store exactly <b>capacity</b> elements
  21. * before it needs to be resized. Only the first <b>num_used</b> (\<=
  22. * capacity) elements point to valid data.
  23. */
  24. void **list;
  25. int num_used;
  26. int capacity;
  27. /** @} */
  28. } smartlist_t;
  29. MOCK_DECL(smartlist_t *, smartlist_new, (void));
  30. MOCK_DECL(void, smartlist_free_, (smartlist_t *sl));
  31. #define smartlist_free(sl) FREE_AND_NULL(smartlist_t, smartlist_free_, (sl))
  32. void smartlist_clear(smartlist_t *sl);
  33. void smartlist_add(smartlist_t *sl, void *element);
  34. void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2);
  35. void smartlist_add_strdup(struct smartlist_t *sl, const char *string);
  36. void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern, ...)
  37. CHECK_PRINTF(2, 3);
  38. void smartlist_add_vasprintf(struct smartlist_t *sl, const char *pattern,
  39. va_list args)
  40. CHECK_PRINTF(2, 0);
  41. void smartlist_remove(smartlist_t *sl, const void *element);
  42. void smartlist_remove_keeporder(smartlist_t *sl, const void *element);
  43. void *smartlist_pop_last(smartlist_t *sl);
  44. void smartlist_reverse(smartlist_t *sl);
  45. void smartlist_string_remove(smartlist_t *sl, const char *element);
  46. int smartlist_contains(const smartlist_t *sl, const void *element);
  47. int smartlist_contains_string(const smartlist_t *sl, const char *element);
  48. int smartlist_pos(const smartlist_t *sl, const void *element);
  49. int smartlist_string_pos(const smartlist_t *, const char *elt);
  50. int smartlist_contains_string_case(const smartlist_t *sl, const char *element);
  51. int smartlist_contains_int_as_string(const smartlist_t *sl, int num);
  52. int smartlist_strings_eq(const smartlist_t *sl1, const smartlist_t *sl2);
  53. int smartlist_contains_digest(const smartlist_t *sl, const char *element);
  54. int smartlist_ints_eq(const smartlist_t *sl1, const smartlist_t *sl2);
  55. int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2);
  56. void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
  57. void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
  58. /* smartlist_choose() is defined in crypto.[ch] */
  59. #ifdef DEBUG_SMARTLIST
  60. #include "lib/err/torerr.h"
  61. #include <stdlib.h>
  62. /** Return the number of items in sl.
  63. */
  64. static inline int smartlist_len(const smartlist_t *sl);
  65. static inline int smartlist_len(const smartlist_t *sl) {
  66. raw_assert(sl);
  67. return (sl)->num_used;
  68. }
  69. /** Return the <b>idx</b>th element of sl.
  70. */
  71. static inline void *smartlist_get(const smartlist_t *sl, int idx);
  72. static inline void *smartlist_get(const smartlist_t *sl, int idx) {
  73. raw_assert(sl);
  74. raw_assert(idx>=0);
  75. raw_assert(sl->num_used > idx);
  76. return sl->list[idx];
  77. }
  78. static inline void smartlist_set(smartlist_t *sl, int idx, void *val) {
  79. raw_assert(sl);
  80. raw_assert(idx>=0);
  81. raw_assert(sl->num_used > idx);
  82. sl->list[idx] = val;
  83. }
  84. #else /* !(defined(DEBUG_SMARTLIST)) */
  85. #define smartlist_len(sl) ((sl)->num_used)
  86. #define smartlist_get(sl, idx) ((sl)->list[idx])
  87. #define smartlist_set(sl, idx, val) ((sl)->list[idx] = (val))
  88. #endif /* defined(DEBUG_SMARTLIST) */
  89. /** Exchange the elements at indices <b>idx1</b> and <b>idx2</b> of the
  90. * smartlist <b>sl</b>. */
  91. static inline void smartlist_swap(smartlist_t *sl, int idx1, int idx2)
  92. {
  93. if (idx1 != idx2) {
  94. void *elt = smartlist_get(sl, idx1);
  95. smartlist_set(sl, idx1, smartlist_get(sl, idx2));
  96. smartlist_set(sl, idx2, elt);
  97. }
  98. }
  99. void smartlist_del(smartlist_t *sl, int idx);
  100. void smartlist_del_keeporder(smartlist_t *sl, int idx);
  101. void smartlist_insert(smartlist_t *sl, int idx, void *val);
  102. void smartlist_sort(smartlist_t *sl,
  103. int (*compare)(const void **a, const void **b));
  104. void *smartlist_get_most_frequent_(const smartlist_t *sl,
  105. int (*compare)(const void **a, const void **b),
  106. int *count_out);
  107. #define smartlist_get_most_frequent(sl, compare) \
  108. smartlist_get_most_frequent_((sl), (compare), NULL)
  109. void smartlist_uniq(smartlist_t *sl,
  110. int (*compare)(const void **a, const void **b),
  111. void (*free_fn)(void *elt));
  112. void smartlist_sort_strings(smartlist_t *sl);
  113. void smartlist_sort_digests(smartlist_t *sl);
  114. void smartlist_sort_digests256(smartlist_t *sl);
  115. void smartlist_sort_pointers(smartlist_t *sl);
  116. const char *smartlist_get_most_frequent_string(smartlist_t *sl);
  117. const char *smartlist_get_most_frequent_string_(smartlist_t *sl,
  118. int *count_out);
  119. const uint8_t *smartlist_get_most_frequent_digest256(smartlist_t *sl);
  120. void smartlist_uniq_strings(smartlist_t *sl);
  121. void smartlist_uniq_digests(smartlist_t *sl);
  122. void smartlist_uniq_digests256(smartlist_t *sl);
  123. void *smartlist_bsearch(smartlist_t *sl, const void *key,
  124. int (*compare)(const void *key, const void **member));
  125. int smartlist_bsearch_idx(const smartlist_t *sl, const void *key,
  126. int (*compare)(const void *key, const void **member),
  127. int *found_out);
  128. void smartlist_pqueue_add(smartlist_t *sl,
  129. int (*compare)(const void *a, const void *b),
  130. int idx_field_offset,
  131. void *item);
  132. void *smartlist_pqueue_pop(smartlist_t *sl,
  133. int (*compare)(const void *a, const void *b),
  134. int idx_field_offset);
  135. void smartlist_pqueue_remove(smartlist_t *sl,
  136. int (*compare)(const void *a, const void *b),
  137. int idx_field_offset,
  138. void *item);
  139. void smartlist_pqueue_assert_ok(smartlist_t *sl,
  140. int (*compare)(const void *a, const void *b),
  141. int idx_field_offset);
  142. #define SPLIT_SKIP_SPACE 0x01
  143. #define SPLIT_IGNORE_BLANK 0x02
  144. #define SPLIT_STRIP_SPACE 0x04
  145. int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
  146. int flags, int max);
  147. char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate,
  148. size_t *len_out) ATTR_MALLOC;
  149. char *smartlist_join_strings2(smartlist_t *sl, const char *join,
  150. size_t join_len, int terminate, size_t *len_out)
  151. ATTR_MALLOC;
  152. /** Iterate over the items in a smartlist <b>sl</b>, in order. For each item,
  153. * assign it to a new local variable of type <b>type</b> named <b>var</b>, and
  154. * execute the statements inside the loop body. Inside the loop, the loop
  155. * index can be accessed as <b>var</b>_sl_idx and the length of the list can
  156. * be accessed as <b>var</b>_sl_len.
  157. *
  158. * NOTE: Do not change the length of the list while the loop is in progress,
  159. * unless you adjust the _sl_len variable correspondingly. See second example
  160. * below.
  161. *
  162. * Example use:
  163. * <pre>
  164. * smartlist_t *list = smartlist_split("A:B:C", ":", 0, 0);
  165. * SMARTLIST_FOREACH_BEGIN(list, char *, cp) {
  166. * printf("%d: %s\n", cp_sl_idx, cp);
  167. * tor_free(cp);
  168. * } SMARTLIST_FOREACH_END(cp);
  169. * smartlist_free(list);
  170. * </pre>
  171. *
  172. * Example use (advanced):
  173. * <pre>
  174. * SMARTLIST_FOREACH_BEGIN(list, char *, cp) {
  175. * if (!strcmp(cp, "junk")) {
  176. * tor_free(cp);
  177. * SMARTLIST_DEL_CURRENT(list, cp);
  178. * }
  179. * } SMARTLIST_FOREACH_END(cp);
  180. * </pre>
  181. */
  182. /* Note: these macros use token pasting, and reach into smartlist internals.
  183. * This can make them a little daunting. Here's the approximate unpacking of
  184. * the above examples, for entertainment value:
  185. *
  186. * <pre>
  187. * smartlist_t *list = smartlist_split("A:B:C", ":", 0, 0);
  188. * {
  189. * int cp_sl_idx, cp_sl_len = smartlist_len(list);
  190. * char *cp;
  191. * for (cp_sl_idx = 0; cp_sl_idx < cp_sl_len; ++cp_sl_idx) {
  192. * cp = smartlist_get(list, cp_sl_idx);
  193. * printf("%d: %s\n", cp_sl_idx, cp);
  194. * tor_free(cp);
  195. * }
  196. * }
  197. * smartlist_free(list);
  198. * </pre>
  199. *
  200. * <pre>
  201. * {
  202. * int cp_sl_idx, cp_sl_len = smartlist_len(list);
  203. * char *cp;
  204. * for (cp_sl_idx = 0; cp_sl_idx < cp_sl_len; ++cp_sl_idx) {
  205. * cp = smartlist_get(list, cp_sl_idx);
  206. * if (!strcmp(cp, "junk")) {
  207. * tor_free(cp);
  208. * smartlist_del(list, cp_sl_idx);
  209. * --cp_sl_idx;
  210. * --cp_sl_len;
  211. * }
  212. * }
  213. * }
  214. * </pre>
  215. */
  216. #define SMARTLIST_FOREACH_BEGIN(sl, type, var) \
  217. STMT_BEGIN \
  218. int var ## _sl_idx, var ## _sl_len=(sl)->num_used; \
  219. type var; \
  220. for (var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len; \
  221. ++var ## _sl_idx) { \
  222. var = (sl)->list[var ## _sl_idx];
  223. #define SMARTLIST_FOREACH_END(var) \
  224. var = NULL; \
  225. (void) var ## _sl_idx; \
  226. } STMT_END
  227. /**
  228. * An alias for SMARTLIST_FOREACH_BEGIN and SMARTLIST_FOREACH_END, using
  229. * <b>cmd</b> as the loop body. This wrapper is here for convenience with
  230. * very short loops.
  231. *
  232. * By convention, we do not use this for loops which nest, or for loops over
  233. * 10 lines or so. Use SMARTLIST_FOREACH_{BEGIN,END} for those.
  234. */
  235. #define SMARTLIST_FOREACH(sl, type, var, cmd) \
  236. SMARTLIST_FOREACH_BEGIN(sl,type,var) { \
  237. cmd; \
  238. } SMARTLIST_FOREACH_END(var)
  239. /** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed
  240. * with the variable <b>var</b>, remove the current element in a way that
  241. * won't confuse the loop. */
  242. #define SMARTLIST_DEL_CURRENT(sl, var) \
  243. STMT_BEGIN \
  244. smartlist_del(sl, var ## _sl_idx); \
  245. --var ## _sl_idx; \
  246. --var ## _sl_len; \
  247. STMT_END
  248. /** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed
  249. * with the variable <b>var</b>, remove the current element in a way that
  250. * won't confuse the loop. */
  251. #define SMARTLIST_DEL_CURRENT_KEEPORDER(sl, var) \
  252. STMT_BEGIN \
  253. smartlist_del_keeporder(sl, var ## _sl_idx); \
  254. --var ## _sl_idx; \
  255. --var ## _sl_len; \
  256. STMT_END
  257. /** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed
  258. * with the variable <b>var</b>, replace the current element with <b>val</b>.
  259. * Does not deallocate the current value of <b>var</b>.
  260. */
  261. #define SMARTLIST_REPLACE_CURRENT(sl, var, val) \
  262. STMT_BEGIN \
  263. smartlist_set(sl, var ## _sl_idx, val); \
  264. STMT_END
  265. /* Helper: Given two lists of items, possibly of different types, such that
  266. * both lists are sorted on some common field (as determined by a comparison
  267. * expression <b>cmpexpr</b>), and such that one list (<b>sl1</b>) has no
  268. * duplicates on the common field, loop through the lists in lockstep, and
  269. * execute <b>unmatched_var2</b> on items in var2 that do not appear in
  270. * var1.
  271. *
  272. * WARNING: It isn't safe to add remove elements from either list while the
  273. * loop is in progress.
  274. *
  275. * Example use:
  276. * SMARTLIST_FOREACH_JOIN(routerstatus_list, routerstatus_t *, rs,
  277. * routerinfo_list, routerinfo_t *, ri,
  278. * tor_memcmp(rs->identity_digest, ri->identity_digest, 20),
  279. * log_info(LD_GENERAL,"No match for %s", ri->nickname)) {
  280. * log_info(LD_GENERAL, "%s matches routerstatus %p", ri->nickname, rs);
  281. * } SMARTLIST_FOREACH_JOIN_END(rs, ri);
  282. **/
  283. /* The example above unpacks (approximately) to:
  284. * int rs_sl_idx = 0, rs_sl_len = smartlist_len(routerstatus_list);
  285. * int ri_sl_idx, ri_sl_len = smartlist_len(routerinfo_list);
  286. * int rs_ri_cmp;
  287. * routerstatus_t *rs;
  288. * routerinfo_t *ri;
  289. * for (; ri_sl_idx < ri_sl_len; ++ri_sl_idx) {
  290. * ri = smartlist_get(routerinfo_list, ri_sl_idx);
  291. * while (rs_sl_idx < rs_sl_len) {
  292. * rs = smartlist_get(routerstatus_list, rs_sl_idx);
  293. * rs_ri_cmp = tor_memcmp(rs->identity_digest, ri->identity_digest, 20);
  294. * if (rs_ri_cmp > 0) {
  295. * break;
  296. * } else if (rs_ri_cmp == 0) {
  297. * goto matched_ri;
  298. * } else {
  299. * ++rs_sl_idx;
  300. * }
  301. * }
  302. * log_info(LD_GENERAL,"No match for %s", ri->nickname);
  303. * continue;
  304. * matched_ri: {
  305. * log_info(LD_GENERAL,"%s matches with routerstatus %p",ri->nickname,rs);
  306. * }
  307. * }
  308. */
  309. #define SMARTLIST_FOREACH_JOIN(sl1, type1, var1, sl2, type2, var2, \
  310. cmpexpr, unmatched_var2) \
  311. STMT_BEGIN \
  312. int var1 ## _sl_idx = 0, var1 ## _sl_len=(sl1)->num_used; \
  313. int var2 ## _sl_idx = 0, var2 ## _sl_len=(sl2)->num_used; \
  314. int var1 ## _ ## var2 ## _cmp; \
  315. type1 var1; \
  316. type2 var2; \
  317. for (; var2##_sl_idx < var2##_sl_len; ++var2##_sl_idx) { \
  318. var2 = (sl2)->list[var2##_sl_idx]; \
  319. while (var1##_sl_idx < var1##_sl_len) { \
  320. var1 = (sl1)->list[var1##_sl_idx]; \
  321. var1##_##var2##_cmp = (cmpexpr); \
  322. if (var1##_##var2##_cmp > 0) { \
  323. break; \
  324. } else if (var1##_##var2##_cmp == 0) { \
  325. goto matched_##var2; \
  326. } else { \
  327. ++var1##_sl_idx; \
  328. } \
  329. } \
  330. /* Ran out of v1, or no match for var2. */ \
  331. unmatched_var2; \
  332. continue; \
  333. matched_##var2: ; \
  334. #define SMARTLIST_FOREACH_JOIN_END(var1, var2) \
  335. } \
  336. STMT_END
  337. #endif /* !defined(TOR_CONTAINER_H) */