container.h 14 KB

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