container.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 "compat.h"
  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. extern INLINE int smartlist_len(const smartlist_t *sl) ATTR_PURE {
  54. tor_assert(sl);
  55. return (sl)->num_used;
  56. }
  57. /** Return the <b>idx</b>th element of sl.
  58. */
  59. extern INLINE void *smartlist_get(const smartlist_t *sl, int idx) ATTR_PURE {
  60. tor_assert(sl);
  61. tor_assert(idx>=0);
  62. tor_assert(sl->num_used < idx);
  63. return sl->list[idx];
  64. }
  65. extern INLINE void smartlist_set(smartlist_t *sl, int idx, void *val) {
  66. tor_assert(sl);
  67. tor_assert(idx>=0);
  68. tor_assert(sl->num_used < idx);
  69. sl->list[idx] = val;
  70. }
  71. #else
  72. #define smartlist_len(sl) ((sl)->num_used)
  73. #define smartlist_get(sl, idx) ((sl)->list[idx])
  74. #define smartlist_set(sl, idx, val) ((sl)->list[idx] = (val))
  75. #endif
  76. /** Exchange the elements at indices <b>idx1</b> and <b>idx2</b> of the
  77. * smartlist <b>sl</b>. */
  78. static INLINE void smartlist_swap(smartlist_t *sl, int idx1, int idx2)
  79. {
  80. if (idx1 != idx2) {
  81. void *elt = smartlist_get(sl, idx1);
  82. smartlist_set(sl, idx1, smartlist_get(sl, idx2));
  83. smartlist_set(sl, idx2, elt);
  84. }
  85. }
  86. void smartlist_del(smartlist_t *sl, int idx);
  87. void smartlist_del_keeporder(smartlist_t *sl, int idx);
  88. void smartlist_insert(smartlist_t *sl, int idx, void *val);
  89. void smartlist_sort(smartlist_t *sl,
  90. int (*compare)(const void **a, const void **b));
  91. void smartlist_uniq(smartlist_t *sl,
  92. int (*compare)(const void **a, const void **b),
  93. void (*free_fn)(void *elt));
  94. void smartlist_sort_strings(smartlist_t *sl);
  95. void smartlist_sort_digests(smartlist_t *sl);
  96. void smartlist_uniq_strings(smartlist_t *sl);
  97. void smartlist_uniq_digests(smartlist_t *sl);
  98. void *smartlist_bsearch(smartlist_t *sl, const void *key,
  99. int (*compare)(const void *key, const void **member))
  100. ATTR_PURE;
  101. void smartlist_pqueue_add(smartlist_t *sl,
  102. int (*compare)(const void *a, const void *b),
  103. void *item);
  104. void *smartlist_pqueue_pop(smartlist_t *sl,
  105. int (*compare)(const void *a, const void *b));
  106. void smartlist_pqueue_assert_ok(smartlist_t *sl,
  107. int (*compare)(const void *a, const void *b));
  108. #define SPLIT_SKIP_SPACE 0x01
  109. #define SPLIT_IGNORE_BLANK 0x02
  110. int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
  111. int flags, int max);
  112. char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate,
  113. size_t *len_out) ATTR_MALLOC;
  114. char *smartlist_join_strings2(smartlist_t *sl, const char *join,
  115. size_t join_len, int terminate, size_t *len_out)
  116. ATTR_MALLOC;
  117. /** Iterate over the items in a smartlist <b>sl</b>, in order. For each item,
  118. * assign it to a new local variable of type <b>type</b> named <b>var</b>, and
  119. * execute the statement <b>cmd</b>. Inside the loop, the loop index can
  120. * be accessed as <b>var</b>_sl_idx and the length of the list can be accessed
  121. * as <b>var</b>_sl_len.
  122. *
  123. * NOTE: Do not change the length of the list while the loop is in progress,
  124. * unless you adjust the _sl_len variable correspondingly. See second example
  125. * below.
  126. *
  127. * Example use:
  128. * <pre>
  129. * smartlist_t *list = smartlist_split("A:B:C", ":", 0, 0);
  130. * SMARTLIST_FOREACH(list, char *, cp,
  131. * {
  132. * printf("%d: %s\n", cp_sl_idx, cp);
  133. * tor_free(cp);
  134. * });
  135. * smartlist_free(list);
  136. * </pre>
  137. *
  138. * Example use (advanced):
  139. * <pre>
  140. * SMARTLIST_FOREACH(list, char *, cp,
  141. * {
  142. * if (!strcmp(cp, "junk")) {
  143. * smartlist_del(list, cp_sl_idx);
  144. * tor_free(cp);
  145. * --cp_sl_len; // decrement length of list so we don't run off the end
  146. * --cp_sl_idx; // decrement idx so we consider the item that moved here
  147. * }
  148. * });
  149. * </pre>
  150. */
  151. #define SMARTLIST_FOREACH(sl, type, var, cmd) \
  152. do { \
  153. int var ## _sl_idx, var ## _sl_len=(sl)->num_used; \
  154. type var; \
  155. for (var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len; \
  156. ++var ## _sl_idx) { \
  157. var = (sl)->list[var ## _sl_idx]; \
  158. cmd; \
  159. } } while (0)
  160. /** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed
  161. * with the variable <b>var</b>, remove the current element in a way that
  162. * won't confuse the loop. */
  163. #define SMARTLIST_DEL_CURRENT(sl, var) \
  164. do { \
  165. smartlist_del(sl, var ## _sl_idx); \
  166. --var ## _sl_idx; \
  167. --var ## _sl_len; \
  168. } while (0);
  169. #define DECLARE_MAP_FNS(maptype, keytype, prefix) \
  170. typedef struct maptype maptype; \
  171. typedef struct prefix##entry_t *prefix##iter_t; \
  172. maptype* prefix##new(void); \
  173. void* prefix##set(maptype *map, keytype key, void *val); \
  174. void* prefix##get(const maptype *map, keytype key); \
  175. void* prefix##remove(maptype *map, keytype key); \
  176. void prefix##free(maptype *map, void (*free_val)(void*)); \
  177. int prefix##isempty(const maptype *map); \
  178. int prefix##size(const maptype *map); \
  179. prefix##iter_t *prefix##iter_init(maptype *map); \
  180. prefix##iter_t *prefix##iter_next(maptype *map, prefix##iter_t *iter); \
  181. prefix##iter_t *prefix##iter_next_rmv(maptype *map, prefix##iter_t *iter); \
  182. void prefix##iter_get(prefix##iter_t *iter, keytype *keyp, void **valp); \
  183. int prefix##iter_done(prefix##iter_t *iter); \
  184. void prefix##assert_ok(const maptype *map);
  185. /* Map from const char * to void *. Implemented with a hash table. */
  186. DECLARE_MAP_FNS(strmap_t, const char *, strmap_);
  187. DECLARE_MAP_FNS(digestmap_t, const char *, digestmap_);
  188. #undef DECLARE_MAP_FNS
  189. void* strmap_set_lc(strmap_t *map, const char *key, void *val);
  190. void* strmap_get_lc(const strmap_t *map, const char *key);
  191. void* strmap_remove_lc(strmap_t *map, const char *key);
  192. #define DECLARE_TYPED_DIGESTMAP_FNS(prefix, maptype, valtype) \
  193. typedef struct maptype maptype; \
  194. typedef struct prefix##iter_t prefix##iter_t; \
  195. static INLINE maptype* prefix##new(void) \
  196. { \
  197. return (maptype*)digestmap_new(); \
  198. } \
  199. static INLINE valtype* prefix##get(maptype *map, const char *key) \
  200. { \
  201. return (valtype*)digestmap_get((digestmap_t*)map, key); \
  202. } \
  203. static INLINE valtype* prefix##set(maptype *map, const char *key, \
  204. valtype *val) \
  205. { \
  206. return (valtype*)digestmap_set((digestmap_t*)map, key, val); \
  207. } \
  208. static INLINE valtype* prefix##remove(maptype *map, const char *key) \
  209. { \
  210. return (valtype*)digestmap_remove((digestmap_t*)map, key); \
  211. } \
  212. static INLINE void prefix##free(maptype *map, void (*free_val)(void*)) \
  213. { \
  214. digestmap_free((digestmap_t*)map, free_val); \
  215. } \
  216. static INLINE int prefix##isempty(maptype *map) \
  217. { \
  218. return digestmap_isempty((digestmap_t*)map); \
  219. } \
  220. static INLINE int prefix##size(maptype *map) \
  221. { \
  222. return digestmap_isempty((digestmap_t*)map); \
  223. } \
  224. static INLINE prefix##iter_t *prefix##iter_init(maptype *map) \
  225. { \
  226. return (prefix##iter_t*) digestmap_iter_init((digestmap_t*)map); \
  227. } \
  228. static INLINE prefix##iter_t *prefix##iter_next(maptype *map, \
  229. prefix##iter_t *iter) \
  230. { \
  231. return (prefix##iter_t*) digestmap_iter_next( \
  232. (digestmap_t*)map, (digestmap_iter_t*)iter); \
  233. } \
  234. static INLINE prefix##iter_t *prefix##iter_next_rmv(maptype *map, \
  235. prefix##iter_t *iter) \
  236. { \
  237. return (prefix##iter_t*) digestmap_iter_next_rmv( \
  238. (digestmap_t*)map, (digestmap_iter_t*)iter); \
  239. } \
  240. static INLINE void prefix##iter_get(prefix##iter_t *iter, \
  241. const char **keyp, \
  242. valtype **valp) \
  243. { \
  244. void *v; \
  245. digestmap_iter_get((digestmap_iter_t*) iter, keyp, &v); \
  246. *valp = v; \
  247. } \
  248. static INLINE int prefix##iter_done(prefix##iter_t *iter) \
  249. { \
  250. return digestmap_iter_done((digestmap_iter_t*)iter); \
  251. }
  252. #endif