container.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. #ifndef __CONTAINER_H
  7. #define __CONTAINER_H
  8. #define CONTAINER_H_ID \
  9. "$Id$"
  10. #include "util.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 throuch the functions
  15. * and macros defined here.
  16. **/
  17. typedef struct smartlist_t {
  18. /** <b>list</b> has enough capacity to store exactly <b>capacity</b> elements
  19. * before it needs to be resized. Only the first <b>num_used</b> (\<=
  20. * capacity) elements point to valid data.
  21. */
  22. void **list;
  23. int num_used;
  24. int capacity;
  25. } smartlist_t;
  26. smartlist_t *smartlist_create(void);
  27. void smartlist_free(smartlist_t *sl);
  28. void smartlist_set_capacity(smartlist_t *sl, int n);
  29. void smartlist_clear(smartlist_t *sl);
  30. void smartlist_add(smartlist_t *sl, void *element);
  31. void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2);
  32. void smartlist_remove(smartlist_t *sl, const void *element);
  33. void *smartlist_pop_last(smartlist_t *sl);
  34. void smartlist_reverse(smartlist_t *sl);
  35. void smartlist_string_remove(smartlist_t *sl, const char *element);
  36. int smartlist_isin(const smartlist_t *sl, const void *element) ATTR_PURE;
  37. int smartlist_string_isin(const smartlist_t *sl, const char *element)
  38. ATTR_PURE;
  39. int smartlist_string_pos(const smartlist_t *, const char *elt) ATTR_PURE;
  40. int smartlist_string_isin_case(const smartlist_t *sl, const char *element)
  41. ATTR_PURE;
  42. int smartlist_string_num_isin(const smartlist_t *sl, int num) ATTR_PURE;
  43. int smartlist_digest_isin(const smartlist_t *sl, const char *element)
  44. ATTR_PURE;
  45. int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2)
  46. ATTR_PURE;
  47. void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
  48. void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
  49. /* smartlist_choose() is defined in crypto.[ch] */
  50. #ifdef DEBUG_SMARTLIST
  51. /** Return the number of items in sl.
  52. */
  53. static INLINE int smartlist_len(const smartlist_t *sl) ATTR_PURE;
  54. static INLINE int smartlist_len(const smartlist_t *sl) {
  55. tor_assert(sl);
  56. return (sl)->num_used;
  57. }
  58. /** Return the <b>idx</b>th element of sl.
  59. */
  60. static INLINE void *smartlist_get(const smartlist_t *sl, int idx) ATTR_PURE;
  61. static INLINE void *smartlist_get(const smartlist_t *sl, int idx) {
  62. tor_assert(sl);
  63. tor_assert(idx>=0);
  64. tor_assert(sl->num_used > idx);
  65. return sl->list[idx];
  66. }
  67. static INLINE void smartlist_set(smartlist_t *sl, int idx, void *val) {
  68. tor_assert(sl);
  69. tor_assert(idx>=0);
  70. tor_assert(sl->num_used > idx);
  71. sl->list[idx] = val;
  72. }
  73. #else
  74. #define smartlist_len(sl) ((sl)->num_used)
  75. #define smartlist_get(sl, idx) ((sl)->list[idx])
  76. #define smartlist_set(sl, idx, val) ((sl)->list[idx] = (val))
  77. #endif
  78. /** Exchange the elements at indices <b>idx1</b> and <b>idx2</b> of the
  79. * smartlist <b>sl</b>. */
  80. static INLINE void smartlist_swap(smartlist_t *sl, int idx1, int idx2)
  81. {
  82. if (idx1 != idx2) {
  83. void *elt = smartlist_get(sl, idx1);
  84. smartlist_set(sl, idx1, smartlist_get(sl, idx2));
  85. smartlist_set(sl, idx2, elt);
  86. }
  87. }
  88. void smartlist_del(smartlist_t *sl, int idx);
  89. void smartlist_del_keeporder(smartlist_t *sl, int idx);
  90. void smartlist_insert(smartlist_t *sl, int idx, void *val);
  91. void smartlist_sort(smartlist_t *sl,
  92. int (*compare)(const void **a, const void **b));
  93. void smartlist_uniq(smartlist_t *sl,
  94. int (*compare)(const void **a, const void **b),
  95. void (*free_fn)(void *elt));
  96. void smartlist_sort_strings(smartlist_t *sl);
  97. void smartlist_sort_digests(smartlist_t *sl);
  98. void smartlist_uniq_strings(smartlist_t *sl);
  99. void smartlist_uniq_digests(smartlist_t *sl);
  100. void *smartlist_bsearch(smartlist_t *sl, const void *key,
  101. int (*compare)(const void *key, const void **member))
  102. ATTR_PURE;
  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. ATTR_PURE;
  107. void smartlist_pqueue_add(smartlist_t *sl,
  108. int (*compare)(const void *a, const void *b),
  109. void *item);
  110. void *smartlist_pqueue_pop(smartlist_t *sl,
  111. int (*compare)(const void *a, const void *b));
  112. void smartlist_pqueue_assert_ok(smartlist_t *sl,
  113. int (*compare)(const void *a, const void *b));
  114. #define SPLIT_SKIP_SPACE 0x01
  115. #define SPLIT_IGNORE_BLANK 0x02
  116. int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
  117. int flags, int max);
  118. char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate,
  119. size_t *len_out) ATTR_MALLOC;
  120. char *smartlist_join_strings2(smartlist_t *sl, const char *join,
  121. size_t join_len, int terminate, size_t *len_out)
  122. ATTR_MALLOC;
  123. /** Iterate over the items in a smartlist <b>sl</b>, in order. For each item,
  124. * assign it to a new local variable of type <b>type</b> named <b>var</b>, and
  125. * execute the statement <b>cmd</b>. Inside the loop, the loop index can
  126. * be accessed as <b>var</b>_sl_idx and the length of the list can be accessed
  127. * as <b>var</b>_sl_len.
  128. *
  129. * NOTE: Do not change the length of the list while the loop is in progress,
  130. * unless you adjust the _sl_len variable correspondingly. See second example
  131. * below.
  132. *
  133. * Example use:
  134. * <pre>
  135. * smartlist_t *list = smartlist_split("A:B:C", ":", 0, 0);
  136. * SMARTLIST_FOREACH(list, char *, cp,
  137. * {
  138. * printf("%d: %s\n", cp_sl_idx, cp);
  139. * tor_free(cp);
  140. * });
  141. * smartlist_free(list);
  142. * </pre>
  143. *
  144. * Example use (advanced):
  145. * <pre>
  146. * SMARTLIST_FOREACH(list, char *, cp,
  147. * {
  148. * if (!strcmp(cp, "junk")) {
  149. * smartlist_del(list, cp_sl_idx);
  150. * tor_free(cp);
  151. * --cp_sl_len; // decrement length of list so we don't run off the end
  152. * --cp_sl_idx; // decrement idx so we consider the item that moved here
  153. * }
  154. * });
  155. * </pre>
  156. */
  157. #define SMARTLIST_FOREACH(sl, type, var, cmd) \
  158. STMT_BEGIN \
  159. int var ## _sl_idx, var ## _sl_len=(sl)->num_used; \
  160. type var; \
  161. for (var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len; \
  162. ++var ## _sl_idx) { \
  163. var = (sl)->list[var ## _sl_idx]; \
  164. cmd; \
  165. } STMT_END
  166. /** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed
  167. * with the variable <b>var</b>, remove the current element in a way that
  168. * won't confuse the loop. */
  169. #define SMARTLIST_DEL_CURRENT(sl, var) \
  170. STMT_BEGIN \
  171. smartlist_del(sl, var ## _sl_idx); \
  172. --var ## _sl_idx; \
  173. --var ## _sl_len; \
  174. STMT_END
  175. #define DECLARE_MAP_FNS(maptype, keytype, prefix) \
  176. typedef struct maptype maptype; \
  177. typedef struct prefix##entry_t *prefix##iter_t; \
  178. maptype* prefix##new(void); \
  179. void* prefix##set(maptype *map, keytype key, void *val); \
  180. void* prefix##get(const maptype *map, keytype key); \
  181. void* prefix##remove(maptype *map, keytype key); \
  182. void prefix##free(maptype *map, void (*free_val)(void*)); \
  183. int prefix##isempty(const maptype *map); \
  184. int prefix##size(const maptype *map); \
  185. prefix##iter_t *prefix##iter_init(maptype *map); \
  186. prefix##iter_t *prefix##iter_next(maptype *map, prefix##iter_t *iter); \
  187. prefix##iter_t *prefix##iter_next_rmv(maptype *map, prefix##iter_t *iter); \
  188. void prefix##iter_get(prefix##iter_t *iter, keytype *keyp, void **valp); \
  189. int prefix##iter_done(prefix##iter_t *iter); \
  190. void prefix##assert_ok(const maptype *map)
  191. /* Map from const char * to void *. Implemented with a hash table. */
  192. DECLARE_MAP_FNS(strmap_t, const char *, strmap_);
  193. DECLARE_MAP_FNS(digestmap_t, const char *, digestmap_);
  194. #undef DECLARE_MAP_FNS
  195. void* strmap_set_lc(strmap_t *map, const char *key, void *val);
  196. void* strmap_get_lc(const strmap_t *map, const char *key);
  197. void* strmap_remove_lc(strmap_t *map, const char *key);
  198. #define DECLARE_TYPED_DIGESTMAP_FNS(prefix, maptype, valtype) \
  199. typedef struct maptype maptype; \
  200. typedef struct prefix##iter_t prefix##iter_t; \
  201. static INLINE maptype* prefix##new(void) \
  202. { \
  203. return (maptype*)digestmap_new(); \
  204. } \
  205. static INLINE valtype* prefix##get(maptype *map, const char *key) \
  206. { \
  207. return (valtype*)digestmap_get((digestmap_t*)map, key); \
  208. } \
  209. static INLINE valtype* prefix##set(maptype *map, const char *key, \
  210. valtype *val) \
  211. { \
  212. return (valtype*)digestmap_set((digestmap_t*)map, key, val); \
  213. } \
  214. static INLINE valtype* prefix##remove(maptype *map, const char *key) \
  215. { \
  216. return (valtype*)digestmap_remove((digestmap_t*)map, key); \
  217. } \
  218. static INLINE void prefix##free(maptype *map, void (*free_val)(void*)) \
  219. { \
  220. digestmap_free((digestmap_t*)map, free_val); \
  221. } \
  222. static INLINE int prefix##isempty(maptype *map) \
  223. { \
  224. return digestmap_isempty((digestmap_t*)map); \
  225. } \
  226. static INLINE int prefix##size(maptype *map) \
  227. { \
  228. return digestmap_size((digestmap_t*)map); \
  229. } \
  230. static INLINE prefix##iter_t *prefix##iter_init(maptype *map) \
  231. { \
  232. return (prefix##iter_t*) digestmap_iter_init((digestmap_t*)map); \
  233. } \
  234. static INLINE prefix##iter_t *prefix##iter_next(maptype *map, \
  235. prefix##iter_t *iter) \
  236. { \
  237. return (prefix##iter_t*) digestmap_iter_next( \
  238. (digestmap_t*)map, (digestmap_iter_t*)iter); \
  239. } \
  240. static INLINE prefix##iter_t *prefix##iter_next_rmv(maptype *map, \
  241. prefix##iter_t *iter) \
  242. { \
  243. return (prefix##iter_t*) digestmap_iter_next_rmv( \
  244. (digestmap_t*)map, (digestmap_iter_t*)iter); \
  245. } \
  246. static INLINE void prefix##iter_get(prefix##iter_t *iter, \
  247. const char **keyp, \
  248. valtype **valp) \
  249. { \
  250. void *v; \
  251. digestmap_iter_get((digestmap_iter_t*) iter, keyp, &v); \
  252. *valp = v; \
  253. } \
  254. static INLINE int prefix##iter_done(prefix##iter_t *iter) \
  255. { \
  256. return digestmap_iter_done((digestmap_iter_t*)iter); \
  257. }
  258. #if SIZEOF_INT == 4
  259. #define BITARRAY_SHIFT 5
  260. #elif SIZEOF_INT == 8
  261. #define BITARRAY_SHIFT 6
  262. #else
  263. #error "int is neither 4 nor 8 bytes. I can't deal with that."
  264. #endif
  265. #define BITARRAY_MASK ((1u<<BITARRAY_SHIFT)-1)
  266. /** A random-access array of one-bit-wide elements. */
  267. typedef unsigned int bitarray_t;
  268. /** Create a new bit array that can hold <b>n_bits</b> bits. */
  269. static INLINE bitarray_t *
  270. bitarray_init_zero(int n_bits)
  271. {
  272. size_t sz = (n_bits+BITARRAY_MASK) / (1u << BITARRAY_SHIFT);
  273. return tor_malloc_zero(sz*sizeof(unsigned int));
  274. }
  275. /** Free the bit array <b>ba</b>. */
  276. static INLINE void
  277. bitarray_free(bitarray_t *ba)
  278. {
  279. tor_free(ba);
  280. }
  281. /** Set the <b>bit</b>th bit in <b>b</b> to 1. */
  282. static INLINE void
  283. bitarray_set(bitarray_t *b, int bit)
  284. {
  285. b[bit >> BITARRAY_SHIFT] |= (1u << (bit & BITARRAY_MASK));
  286. }
  287. /** Set the <b>bit</b>th bit in <b>b</b> to 0. */
  288. static INLINE void
  289. bitarray_clear(bitarray_t *b, int bit)
  290. {
  291. b[bit >> BITARRAY_SHIFT] &= ~ (1u << (bit & BITARRAY_MASK));
  292. }
  293. /** Return true iff <b>bit</b>th bit in <b>b</b> is nonzero. NOTE: does
  294. * not necessarily return 1 on true. */
  295. static INLINE unsigned int
  296. bitarray_is_set(bitarray_t *b, int bit)
  297. {
  298. return b[bit >> BITARRAY_SHIFT] & (1u << (bit & BITARRAY_MASK));
  299. }
  300. /* These functions, given an <b>array</b> of <b>n_elements</b>, return the
  301. * <b>nth</b> lowest element. <b>nth</b>=0 gives the lowest element;
  302. * <b>n_elements</b>-1 gives the highest; and (<b>n_elements</b>-1) / 2 gives
  303. * the median. As a side effect, the elements of <b>array</b> are sorted. */
  304. int find_nth_int(int *array, int n_elements, int nth);
  305. time_t find_nth_time(time_t *array, int n_elements, int nth);
  306. double find_nth_double(double *array, int n_elements, int nth);
  307. uint32_t find_nth_uint32(uint32_t *array, int n_elements, int nth);
  308. long find_nth_long(long *array, int n_elements, int nth);
  309. static INLINE int
  310. median_int(int *array, int n_elements)
  311. {
  312. return find_nth_int(array, n_elements, (n_elements-1)/2);
  313. }
  314. static INLINE time_t
  315. median_time(time_t *array, int n_elements)
  316. {
  317. return find_nth_time(array, n_elements, (n_elements-1)/2);
  318. }
  319. static INLINE double
  320. median_double(double *array, int n_elements)
  321. {
  322. return find_nth_double(array, n_elements, (n_elements-1)/2);
  323. }
  324. static INLINE uint32_t
  325. median_uint32(uint32_t *array, int n_elements)
  326. {
  327. return find_nth_uint32(array, n_elements, (n_elements-1)/2);
  328. }
  329. static INLINE long
  330. median_long(long *array, int n_elements)
  331. {
  332. return find_nth_long(array, n_elements, (n_elements-1)/2);
  333. }
  334. #endif