smartlist.h 15 KB

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