smartlist.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. /** Return the number of items in sl.
  61. */
  62. static inline int smartlist_len(const smartlist_t *sl);
  63. static inline int smartlist_len(const smartlist_t *sl) {
  64. raw_assert(sl);
  65. return (sl)->num_used;
  66. }
  67. /** Return the <b>idx</b>th element of sl.
  68. */
  69. static inline void *smartlist_get(const smartlist_t *sl, int idx);
  70. static inline void *smartlist_get(const smartlist_t *sl, int idx) {
  71. raw_assert(sl);
  72. raw_assert(idx>=0);
  73. raw_assert(sl->num_used > idx);
  74. return sl->list[idx];
  75. }
  76. static inline void smartlist_set(smartlist_t *sl, int idx, void *val) {
  77. raw_assert(sl);
  78. raw_assert(idx>=0);
  79. raw_assert(sl->num_used > idx);
  80. sl->list[idx] = val;
  81. }
  82. #else /* !(defined(DEBUG_SMARTLIST)) */
  83. #define smartlist_len(sl) ((sl)->num_used)
  84. #define smartlist_get(sl, idx) ((sl)->list[idx])
  85. #define smartlist_set(sl, idx, val) ((sl)->list[idx] = (val))
  86. #endif /* defined(DEBUG_SMARTLIST) */
  87. /** Exchange the elements at indices <b>idx1</b> and <b>idx2</b> of the
  88. * smartlist <b>sl</b>. */
  89. static inline void smartlist_swap(smartlist_t *sl, int idx1, int idx2)
  90. {
  91. if (idx1 != idx2) {
  92. void *elt = smartlist_get(sl, idx1);
  93. smartlist_set(sl, idx1, smartlist_get(sl, idx2));
  94. smartlist_set(sl, idx2, elt);
  95. }
  96. }
  97. void smartlist_del(smartlist_t *sl, int idx);
  98. void smartlist_del_keeporder(smartlist_t *sl, int idx);
  99. void smartlist_insert(smartlist_t *sl, int idx, void *val);
  100. void smartlist_sort(smartlist_t *sl,
  101. int (*compare)(const void **a, const void **b));
  102. void *smartlist_get_most_frequent_(const smartlist_t *sl,
  103. int (*compare)(const void **a, const void **b),
  104. int *count_out);
  105. #define smartlist_get_most_frequent(sl, compare) \
  106. smartlist_get_most_frequent_((sl), (compare), NULL)
  107. void smartlist_uniq(smartlist_t *sl,
  108. int (*compare)(const void **a, const void **b),
  109. void (*free_fn)(void *elt));
  110. void smartlist_sort_strings(smartlist_t *sl);
  111. void smartlist_sort_digests(smartlist_t *sl);
  112. void smartlist_sort_digests256(smartlist_t *sl);
  113. void smartlist_sort_pointers(smartlist_t *sl);
  114. const char *smartlist_get_most_frequent_string(smartlist_t *sl);
  115. const char *smartlist_get_most_frequent_string_(smartlist_t *sl,
  116. int *count_out);
  117. const uint8_t *smartlist_get_most_frequent_digest256(smartlist_t *sl);
  118. void smartlist_uniq_strings(smartlist_t *sl);
  119. void smartlist_uniq_digests(smartlist_t *sl);
  120. void smartlist_uniq_digests256(smartlist_t *sl);
  121. void *smartlist_bsearch(smartlist_t *sl, const void *key,
  122. int (*compare)(const void *key, const void **member));
  123. int smartlist_bsearch_idx(const smartlist_t *sl, const void *key,
  124. int (*compare)(const void *key, const void **member),
  125. int *found_out);
  126. void smartlist_pqueue_add(smartlist_t *sl,
  127. int (*compare)(const void *a, const void *b),
  128. int idx_field_offset,
  129. void *item);
  130. void *smartlist_pqueue_pop(smartlist_t *sl,
  131. int (*compare)(const void *a, const void *b),
  132. int idx_field_offset);
  133. void smartlist_pqueue_remove(smartlist_t *sl,
  134. int (*compare)(const void *a, const void *b),
  135. int idx_field_offset,
  136. void *item);
  137. void smartlist_pqueue_assert_ok(smartlist_t *sl,
  138. int (*compare)(const void *a, const void *b),
  139. int idx_field_offset);
  140. #define SPLIT_SKIP_SPACE 0x01
  141. #define SPLIT_IGNORE_BLANK 0x02
  142. #define SPLIT_STRIP_SPACE 0x04
  143. int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
  144. int flags, int max);
  145. char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate,
  146. size_t *len_out) ATTR_MALLOC;
  147. char *smartlist_join_strings2(smartlist_t *sl, const char *join,
  148. size_t join_len, int terminate, size_t *len_out)
  149. ATTR_MALLOC;
  150. /** Iterate over the items in a smartlist <b>sl</b>, in order. For each item,
  151. * assign it to a new local variable of type <b>type</b> named <b>var</b>, and
  152. * execute the statements inside the loop body. Inside the loop, the loop
  153. * index can be accessed as <b>var</b>_sl_idx and the length of the list can
  154. * be accessed as <b>var</b>_sl_len.
  155. *
  156. * NOTE: Do not change the length of the list while the loop is in progress,
  157. * unless you adjust the _sl_len variable correspondingly. See second example
  158. * below.
  159. *
  160. * Example use:
  161. * <pre>
  162. * smartlist_t *list = smartlist_split("A:B:C", ":", 0, 0);
  163. * SMARTLIST_FOREACH_BEGIN(list, char *, cp) {
  164. * printf("%d: %s\n", cp_sl_idx, cp);
  165. * tor_free(cp);
  166. * } SMARTLIST_FOREACH_END(cp);
  167. * smartlist_free(list);
  168. * </pre>
  169. *
  170. * Example use (advanced):
  171. * <pre>
  172. * SMARTLIST_FOREACH_BEGIN(list, char *, cp) {
  173. * if (!strcmp(cp, "junk")) {
  174. * tor_free(cp);
  175. * SMARTLIST_DEL_CURRENT(list, cp);
  176. * }
  177. * } SMARTLIST_FOREACH_END(cp);
  178. * </pre>
  179. */
  180. /* Note: these macros use token pasting, and reach into smartlist internals.
  181. * This can make them a little daunting. Here's the approximate unpacking of
  182. * the above examples, for entertainment value:
  183. *
  184. * <pre>
  185. * smartlist_t *list = smartlist_split("A:B:C", ":", 0, 0);
  186. * {
  187. * int cp_sl_idx, cp_sl_len = smartlist_len(list);
  188. * char *cp;
  189. * for (cp_sl_idx = 0; cp_sl_idx < cp_sl_len; ++cp_sl_idx) {
  190. * cp = smartlist_get(list, cp_sl_idx);
  191. * printf("%d: %s\n", cp_sl_idx, cp);
  192. * tor_free(cp);
  193. * }
  194. * }
  195. * smartlist_free(list);
  196. * </pre>
  197. *
  198. * <pre>
  199. * {
  200. * int cp_sl_idx, cp_sl_len = smartlist_len(list);
  201. * char *cp;
  202. * for (cp_sl_idx = 0; cp_sl_idx < cp_sl_len; ++cp_sl_idx) {
  203. * cp = smartlist_get(list, cp_sl_idx);
  204. * if (!strcmp(cp, "junk")) {
  205. * tor_free(cp);
  206. * smartlist_del(list, cp_sl_idx);
  207. * --cp_sl_idx;
  208. * --cp_sl_len;
  209. * }
  210. * }
  211. * }
  212. * </pre>
  213. */
  214. #define SMARTLIST_FOREACH_BEGIN(sl, type, var) \
  215. STMT_BEGIN \
  216. int var ## _sl_idx, var ## _sl_len=(sl)->num_used; \
  217. type var; \
  218. for (var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len; \
  219. ++var ## _sl_idx) { \
  220. var = (sl)->list[var ## _sl_idx];
  221. #define SMARTLIST_FOREACH_END(var) \
  222. var = NULL; \
  223. (void) var ## _sl_idx; \
  224. } STMT_END
  225. /**
  226. * An alias for SMARTLIST_FOREACH_BEGIN and SMARTLIST_FOREACH_END, using
  227. * <b>cmd</b> as the loop body. This wrapper is here for convenience with
  228. * very short loops.
  229. *
  230. * By convention, we do not use this for loops which nest, or for loops over
  231. * 10 lines or so. Use SMARTLIST_FOREACH_{BEGIN,END} for those.
  232. */
  233. #define SMARTLIST_FOREACH(sl, type, var, cmd) \
  234. SMARTLIST_FOREACH_BEGIN(sl,type,var) { \
  235. cmd; \
  236. } SMARTLIST_FOREACH_END(var)
  237. /** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed
  238. * with the variable <b>var</b>, remove the current element in a way that
  239. * won't confuse the loop. */
  240. #define SMARTLIST_DEL_CURRENT(sl, var) \
  241. STMT_BEGIN \
  242. smartlist_del(sl, var ## _sl_idx); \
  243. --var ## _sl_idx; \
  244. --var ## _sl_len; \
  245. STMT_END
  246. /** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed
  247. * with the variable <b>var</b>, remove the current element in a way that
  248. * won't confuse the loop. */
  249. #define SMARTLIST_DEL_CURRENT_KEEPORDER(sl, var) \
  250. STMT_BEGIN \
  251. smartlist_del_keeporder(sl, var ## _sl_idx); \
  252. --var ## _sl_idx; \
  253. --var ## _sl_len; \
  254. STMT_END
  255. /** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed
  256. * with the variable <b>var</b>, replace the current element with <b>val</b>.
  257. * Does not deallocate the current value of <b>var</b>.
  258. */
  259. #define SMARTLIST_REPLACE_CURRENT(sl, var, val) \
  260. STMT_BEGIN \
  261. smartlist_set(sl, var ## _sl_idx, val); \
  262. STMT_END
  263. /* Helper: Given two lists of items, possibly of different types, such that
  264. * both lists are sorted on some common field (as determined by a comparison
  265. * expression <b>cmpexpr</b>), and such that one list (<b>sl1</b>) has no
  266. * duplicates on the common field, loop through the lists in lockstep, and
  267. * execute <b>unmatched_var2</b> on items in var2 that do not appear in
  268. * var1.
  269. *
  270. * WARNING: It isn't safe to add remove elements from either list while the
  271. * loop is in progress.
  272. *
  273. * Example use:
  274. * SMARTLIST_FOREACH_JOIN(routerstatus_list, routerstatus_t *, rs,
  275. * routerinfo_list, routerinfo_t *, ri,
  276. * tor_memcmp(rs->identity_digest, ri->identity_digest, 20),
  277. * log_info(LD_GENERAL,"No match for %s", ri->nickname)) {
  278. * log_info(LD_GENERAL, "%s matches routerstatus %p", ri->nickname, rs);
  279. * } SMARTLIST_FOREACH_JOIN_END(rs, ri);
  280. **/
  281. /* The example above unpacks (approximately) to:
  282. * int rs_sl_idx = 0, rs_sl_len = smartlist_len(routerstatus_list);
  283. * int ri_sl_idx, ri_sl_len = smartlist_len(routerinfo_list);
  284. * int rs_ri_cmp;
  285. * routerstatus_t *rs;
  286. * routerinfo_t *ri;
  287. * for (; ri_sl_idx < ri_sl_len; ++ri_sl_idx) {
  288. * ri = smartlist_get(routerinfo_list, ri_sl_idx);
  289. * while (rs_sl_idx < rs_sl_len) {
  290. * rs = smartlist_get(routerstatus_list, rs_sl_idx);
  291. * rs_ri_cmp = tor_memcmp(rs->identity_digest, ri->identity_digest, 20);
  292. * if (rs_ri_cmp > 0) {
  293. * break;
  294. * } else if (rs_ri_cmp == 0) {
  295. * goto matched_ri;
  296. * } else {
  297. * ++rs_sl_idx;
  298. * }
  299. * }
  300. * log_info(LD_GENERAL,"No match for %s", ri->nickname);
  301. * continue;
  302. * matched_ri: {
  303. * log_info(LD_GENERAL,"%s matches with routerstatus %p",ri->nickname,rs);
  304. * }
  305. * }
  306. */
  307. #define SMARTLIST_FOREACH_JOIN(sl1, type1, var1, sl2, type2, var2, \
  308. cmpexpr, unmatched_var2) \
  309. STMT_BEGIN \
  310. int var1 ## _sl_idx = 0, var1 ## _sl_len=(sl1)->num_used; \
  311. int var2 ## _sl_idx = 0, var2 ## _sl_len=(sl2)->num_used; \
  312. int var1 ## _ ## var2 ## _cmp; \
  313. type1 var1; \
  314. type2 var2; \
  315. for (; var2##_sl_idx < var2##_sl_len; ++var2##_sl_idx) { \
  316. var2 = (sl2)->list[var2##_sl_idx]; \
  317. while (var1##_sl_idx < var1##_sl_len) { \
  318. var1 = (sl1)->list[var1##_sl_idx]; \
  319. var1##_##var2##_cmp = (cmpexpr); \
  320. if (var1##_##var2##_cmp > 0) { \
  321. break; \
  322. } else if (var1##_##var2##_cmp == 0) { \
  323. goto matched_##var2; \
  324. } else { \
  325. ++var1##_sl_idx; \
  326. } \
  327. } \
  328. /* Ran out of v1, or no match for var2. */ \
  329. unmatched_var2; \
  330. continue; \
  331. matched_##var2: ; \
  332. #define SMARTLIST_FOREACH_JOIN_END(var1, var2) \
  333. } \
  334. STMT_END
  335. #endif /* !defined(TOR_CONTAINER_H) */