container.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2008, 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. /* Map from const char[DIGEST_LEN] to void *. Implemented with a hash table. */
  194. DECLARE_MAP_FNS(digestmap_t, const char *, digestmap_);
  195. #undef DECLARE_MAP_FNS
  196. #define MAP_FOREACH(prefix, map, keytype, keyvar, valtype, valvar) \
  197. STMT_BEGIN \
  198. prefix##iter_t *key##_iter; \
  199. for (key##_iter = prefix##iter_init(map); \
  200. !prefix##iter_done(key##_iter); \
  201. key##_iter = prefix##iter_next(map, key##_iter)) { \
  202. keytype keyvar; \
  203. void *valvar##_voidp; \
  204. valtype valvar; \
  205. prefix##iter_get(key##_iter, &keyvar, &valvar##_voidp); \
  206. valvar = valvar##_voidp;
  207. #define MAP_FOREACH_MODIFY(prefix, map, keytype, keyvar, valtype, valvar) \
  208. STMT_BEGIN \
  209. prefix##iter_t *key##_iter; \
  210. int keyvar##_del=0; \
  211. for (key##_iter = prefix##iter_init(map); \
  212. !prefix##iter_done(key##_iter); \
  213. key##_iter = keyvar##_del ? \
  214. prefix##iter_next_rmv(map, key##_iter) : \
  215. prefix##iter_next(map, key##_iter)) { \
  216. keytype keyvar; \
  217. void *valvar##_voidp; \
  218. valtype valvar; \
  219. keyvar##_del=0; \
  220. prefix##iter_get(key##_iter, &keyvar, &valvar##_voidp); \
  221. valvar = valvar##_voidp;
  222. #define MAP_DEL_CURRENT(keyvar) \
  223. STMT_BEGIN \
  224. keyvar##_del = 1; \
  225. STMT_END
  226. #define MAP_FOREACH_END } STMT_END ;
  227. #define DIGESTMAP_FOREACH(map, keyvar, valtype, valvar) \
  228. MAP_FOREACH(digestmap_, map, const char *, keyvar, valtype, valvar)
  229. #define DIGESTMAP_FOREACH_MODIFY(map, keyvar, valtype, valvar) \
  230. MAP_FOREACH_MODIFY(digestmap_, map, const char *, keyvar, valtype, valvar)
  231. #define DIGESTMAP_FOREACH_END MAP_FOREACH_END
  232. void* strmap_set_lc(strmap_t *map, const char *key, void *val);
  233. void* strmap_get_lc(const strmap_t *map, const char *key);
  234. void* strmap_remove_lc(strmap_t *map, const char *key);
  235. #define DECLARE_TYPED_DIGESTMAP_FNS(prefix, maptype, valtype) \
  236. typedef struct maptype maptype; \
  237. typedef struct prefix##iter_t prefix##iter_t; \
  238. static INLINE maptype* prefix##new(void) \
  239. { \
  240. return (maptype*)digestmap_new(); \
  241. } \
  242. static INLINE digestmap_t* prefix##to_digestmap(maptype *map) \
  243. { \
  244. return (digestmap_t*)map; \
  245. } \
  246. static INLINE valtype* prefix##get(maptype *map, const char *key) \
  247. { \
  248. return (valtype*)digestmap_get((digestmap_t*)map, key); \
  249. } \
  250. static INLINE valtype* prefix##set(maptype *map, const char *key, \
  251. valtype *val) \
  252. { \
  253. return (valtype*)digestmap_set((digestmap_t*)map, key, val); \
  254. } \
  255. static INLINE valtype* prefix##remove(maptype *map, const char *key) \
  256. { \
  257. return (valtype*)digestmap_remove((digestmap_t*)map, key); \
  258. } \
  259. static INLINE void prefix##free(maptype *map, void (*free_val)(void*)) \
  260. { \
  261. digestmap_free((digestmap_t*)map, free_val); \
  262. } \
  263. static INLINE int prefix##isempty(maptype *map) \
  264. { \
  265. return digestmap_isempty((digestmap_t*)map); \
  266. } \
  267. static INLINE int prefix##size(maptype *map) \
  268. { \
  269. return digestmap_size((digestmap_t*)map); \
  270. } \
  271. static INLINE prefix##iter_t *prefix##iter_init(maptype *map) \
  272. { \
  273. return (prefix##iter_t*) digestmap_iter_init((digestmap_t*)map); \
  274. } \
  275. static INLINE prefix##iter_t *prefix##iter_next(maptype *map, \
  276. prefix##iter_t *iter) \
  277. { \
  278. return (prefix##iter_t*) digestmap_iter_next( \
  279. (digestmap_t*)map, (digestmap_iter_t*)iter); \
  280. } \
  281. static INLINE prefix##iter_t *prefix##iter_next_rmv(maptype *map, \
  282. prefix##iter_t *iter) \
  283. { \
  284. return (prefix##iter_t*) digestmap_iter_next_rmv( \
  285. (digestmap_t*)map, (digestmap_iter_t*)iter); \
  286. } \
  287. static INLINE void prefix##iter_get(prefix##iter_t *iter, \
  288. const char **keyp, \
  289. valtype **valp) \
  290. { \
  291. void *v; \
  292. digestmap_iter_get((digestmap_iter_t*) iter, keyp, &v); \
  293. *valp = v; \
  294. } \
  295. static INLINE int prefix##iter_done(prefix##iter_t *iter) \
  296. { \
  297. return digestmap_iter_done((digestmap_iter_t*)iter); \
  298. }
  299. #if SIZEOF_INT == 4
  300. #define BITARRAY_SHIFT 5
  301. #elif SIZEOF_INT == 8
  302. #define BITARRAY_SHIFT 6
  303. #else
  304. #error "int is neither 4 nor 8 bytes. I can't deal with that."
  305. #endif
  306. #define BITARRAY_MASK ((1u<<BITARRAY_SHIFT)-1)
  307. /** A random-access array of one-bit-wide elements. */
  308. typedef unsigned int bitarray_t;
  309. /** Create a new bit array that can hold <b>n_bits</b> bits. */
  310. static INLINE bitarray_t *
  311. bitarray_init_zero(int n_bits)
  312. {
  313. size_t sz = (n_bits+BITARRAY_MASK) / (1u << BITARRAY_SHIFT);
  314. return tor_malloc_zero(sz*sizeof(unsigned int));
  315. }
  316. /** Free the bit array <b>ba</b>. */
  317. static INLINE void
  318. bitarray_free(bitarray_t *ba)
  319. {
  320. tor_free(ba);
  321. }
  322. /** Set the <b>bit</b>th bit in <b>b</b> to 1. */
  323. static INLINE void
  324. bitarray_set(bitarray_t *b, int bit)
  325. {
  326. b[bit >> BITARRAY_SHIFT] |= (1u << (bit & BITARRAY_MASK));
  327. }
  328. /** Set the <b>bit</b>th bit in <b>b</b> to 0. */
  329. static INLINE void
  330. bitarray_clear(bitarray_t *b, int bit)
  331. {
  332. b[bit >> BITARRAY_SHIFT] &= ~ (1u << (bit & BITARRAY_MASK));
  333. }
  334. /** Return true iff <b>bit</b>th bit in <b>b</b> is nonzero. NOTE: does
  335. * not necessarily return 1 on true. */
  336. static INLINE unsigned int
  337. bitarray_is_set(bitarray_t *b, int bit)
  338. {
  339. return b[bit >> BITARRAY_SHIFT] & (1u << (bit & BITARRAY_MASK));
  340. }
  341. /* These functions, given an <b>array</b> of <b>n_elements</b>, return the
  342. * <b>nth</b> lowest element. <b>nth</b>=0 gives the lowest element;
  343. * <b>n_elements</b>-1 gives the highest; and (<b>n_elements</b>-1) / 2 gives
  344. * the median. As a side effect, the elements of <b>array</b> are sorted. */
  345. int find_nth_int(int *array, int n_elements, int nth);
  346. time_t find_nth_time(time_t *array, int n_elements, int nth);
  347. double find_nth_double(double *array, int n_elements, int nth);
  348. uint32_t find_nth_uint32(uint32_t *array, int n_elements, int nth);
  349. long find_nth_long(long *array, int n_elements, int nth);
  350. static INLINE int
  351. median_int(int *array, int n_elements)
  352. {
  353. return find_nth_int(array, n_elements, (n_elements-1)/2);
  354. }
  355. static INLINE time_t
  356. median_time(time_t *array, int n_elements)
  357. {
  358. return find_nth_time(array, n_elements, (n_elements-1)/2);
  359. }
  360. static INLINE double
  361. median_double(double *array, int n_elements)
  362. {
  363. return find_nth_double(array, n_elements, (n_elements-1)/2);
  364. }
  365. static INLINE uint32_t
  366. median_uint32(uint32_t *array, int n_elements)
  367. {
  368. return find_nth_uint32(array, n_elements, (n_elements-1)/2);
  369. }
  370. static INLINE long
  371. median_long(long *array, int n_elements)
  372. {
  373. return find_nth_long(array, n_elements, (n_elements-1)/2);
  374. }
  375. #endif