smartlist.h 15 KB

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