1 /* Copyright 2003-2004 Roger Dingledine
2 * Copyright 2004-2007 Roger Dingledine, Nick Mathewson */
3 /* See LICENSE for licensing information */
8 #define CONTAINER_H_ID \
13 /** A resizeable list of pointers, with associated helpful functionality.
15 * The members of this struct are exposed only so that macros and inlines can
16 * use them; all access to smartlist internals should go throuch the functions
17 * and macros defined here.
19 typedef struct smartlist_t
{
20 /** <b>list</b> has enough capacity to store exactly <b>capacity</b> elements
21 * before it needs to be resized. Only the first <b>num_used</b> (\<=
22 * capacity) elements point to valid data.
29 smartlist_t
*smartlist_create(void);
30 void smartlist_free(smartlist_t
*sl
);
31 void smartlist_set_capacity(smartlist_t
*sl
, int n
);
32 void smartlist_clear(smartlist_t
*sl
);
33 void smartlist_add(smartlist_t
*sl
, void *element
);
34 void smartlist_add_all(smartlist_t
*sl
, const smartlist_t
*s2
);
35 void smartlist_remove(smartlist_t
*sl
, const void *element
);
36 void *smartlist_pop_last(smartlist_t
*sl
);
37 void smartlist_reverse(smartlist_t
*sl
);
38 void smartlist_string_remove(smartlist_t
*sl
, const char *element
);
39 int smartlist_isin(const smartlist_t
*sl
, const void *element
) ATTR_PURE
;
40 int smartlist_string_isin(const smartlist_t
*sl
, const char *element
)
42 int smartlist_string_pos(const smartlist_t
*, const char *elt
) ATTR_PURE
;
43 int smartlist_string_isin_case(const smartlist_t
*sl
, const char *element
)
45 int smartlist_string_num_isin(const smartlist_t
*sl
, int num
) ATTR_PURE
;
46 int smartlist_digest_isin(const smartlist_t
*sl
, const char *element
)
48 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
);
53 /* smartlist_choose() is defined in crypto.[ch] */
54 #ifdef DEBUG_SMARTLIST
55 /** Return the number of items in sl.
57 static INLINE
int smartlist_len(const smartlist_t
*sl
) ATTR_PURE
;
58 static INLINE
int smartlist_len(const smartlist_t
*sl
) {
60 return (sl
)->num_used
;
62 /** Return the <b>idx</b>th element of sl.
64 static INLINE
void *smartlist_get(const smartlist_t
*sl
, int idx
) ATTR_PURE
;
65 static INLINE
void *smartlist_get(const smartlist_t
*sl
, int idx
) {
68 tor_assert(sl
->num_used
> idx
);
71 static INLINE
void smartlist_set(smartlist_t
*sl
, int idx
, void *val
) {
74 tor_assert(sl
->num_used
> idx
);
78 #define smartlist_len(sl) ((sl)->num_used)
79 #define smartlist_get(sl, idx) ((sl)->list[idx])
80 #define smartlist_set(sl, idx, val) ((sl)->list[idx] = (val))
83 /** Exchange the elements at indices <b>idx1</b> and <b>idx2</b> of the
84 * smartlist <b>sl</b>. */
85 static INLINE
void smartlist_swap(smartlist_t
*sl
, int idx1
, int idx2
)
88 void *elt
= smartlist_get(sl
, idx1
);
89 smartlist_set(sl
, idx1
, smartlist_get(sl
, idx2
));
90 smartlist_set(sl
, idx2
, elt
);
94 void smartlist_del(smartlist_t
*sl
, int idx
);
95 void smartlist_del_keeporder(smartlist_t
*sl
, int idx
);
96 void smartlist_insert(smartlist_t
*sl
, int idx
, void *val
);
97 void smartlist_sort(smartlist_t
*sl
,
98 int (*compare
)(const void **a
, const void **b
));
99 void smartlist_uniq(smartlist_t
*sl
,
100 int (*compare
)(const void **a
, const void **b
),
101 void (*free_fn
)(void *elt
));
102 void smartlist_sort_strings(smartlist_t
*sl
);
103 void smartlist_sort_digests(smartlist_t
*sl
);
104 void smartlist_uniq_strings(smartlist_t
*sl
);
105 void smartlist_uniq_digests(smartlist_t
*sl
);
106 void *smartlist_bsearch(smartlist_t
*sl
, const void *key
,
107 int (*compare
)(const void *key
, const void **member
))
109 int smartlist_bsearch_idx(const smartlist_t
*sl
, const void *key
,
110 int (*compare
)(const void *key
, const void **member
),
114 void smartlist_pqueue_add(smartlist_t
*sl
,
115 int (*compare
)(const void *a
, const void *b
),
117 void *smartlist_pqueue_pop(smartlist_t
*sl
,
118 int (*compare
)(const void *a
, const void *b
));
119 void smartlist_pqueue_assert_ok(smartlist_t
*sl
,
120 int (*compare
)(const void *a
, const void *b
));
122 #define SPLIT_SKIP_SPACE 0x01
123 #define SPLIT_IGNORE_BLANK 0x02
124 int smartlist_split_string(smartlist_t
*sl
, const char *str
, const char *sep
,
126 char *smartlist_join_strings(smartlist_t
*sl
, const char *join
, int terminate
,
127 size_t *len_out
) ATTR_MALLOC
;
128 char *smartlist_join_strings2(smartlist_t
*sl
, const char *join
,
129 size_t join_len
, int terminate
, size_t *len_out
)
132 /** Iterate over the items in a smartlist <b>sl</b>, in order. For each item,
133 * assign it to a new local variable of type <b>type</b> named <b>var</b>, and
134 * execute the statement <b>cmd</b>. Inside the loop, the loop index can
135 * be accessed as <b>var</b>_sl_idx and the length of the list can be accessed
136 * as <b>var</b>_sl_len.
138 * NOTE: Do not change the length of the list while the loop is in progress,
139 * unless you adjust the _sl_len variable correspondingly. See second example
144 * smartlist_t *list = smartlist_split("A:B:C", ":", 0, 0);
145 * SMARTLIST_FOREACH(list, char *, cp,
147 * printf("%d: %s\n", cp_sl_idx, cp);
150 * smartlist_free(list);
153 * Example use (advanced):
155 * SMARTLIST_FOREACH(list, char *, cp,
157 * if (!strcmp(cp, "junk")) {
158 * smartlist_del(list, cp_sl_idx);
160 * --cp_sl_len; // decrement length of list so we don't run off the end
161 * --cp_sl_idx; // decrement idx so we consider the item that moved here
166 #define SMARTLIST_FOREACH(sl, type, var, cmd) \
168 int var ## _sl_idx, var ## _sl_len=(sl)->num_used; \
170 for (var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len; \
171 ++var ## _sl_idx) { \
172 var = (sl)->list[var ## _sl_idx]; \
176 /** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed
177 * with the variable <b>var</b>, remove the current element in a way that
178 * won't confuse the loop. */
179 #define SMARTLIST_DEL_CURRENT(sl, var) \
181 smartlist_del(sl, var ## _sl_idx); \
186 #define DECLARE_MAP_FNS(maptype, keytype, prefix) \
187 typedef struct maptype maptype; \
188 typedef struct prefix##entry_t *prefix##iter_t; \
189 maptype* prefix##new(void); \
190 void* prefix##set(maptype *map, keytype key, void *val); \
191 void* prefix##get(const maptype *map, keytype key); \
192 void* prefix##remove(maptype *map, keytype key); \
193 void prefix##free(maptype *map, void (*free_val)(void*)); \
194 int prefix##isempty(const maptype *map); \
195 int prefix##size(const maptype *map); \
196 prefix##iter_t *prefix##iter_init(maptype *map); \
197 prefix##iter_t *prefix##iter_next(maptype *map, prefix##iter_t *iter); \
198 prefix##iter_t *prefix##iter_next_rmv(maptype *map, prefix##iter_t *iter); \
199 void prefix##iter_get(prefix##iter_t *iter, keytype *keyp, void **valp); \
200 int prefix##iter_done(prefix##iter_t *iter); \
201 void prefix##assert_ok(const maptype *map)
203 /* Map from const char * to void *. Implemented with a hash table. */
204 DECLARE_MAP_FNS(strmap_t
, const char *, strmap_
);
205 DECLARE_MAP_FNS(digestmap_t
, const char *, digestmap_
);
206 #undef DECLARE_MAP_FNS
208 void* strmap_set_lc(strmap_t
*map
, const char *key
, void *val
);
209 void* strmap_get_lc(const strmap_t
*map
, const char *key
);
210 void* strmap_remove_lc(strmap_t
*map
, const char *key
);
212 #define DECLARE_TYPED_DIGESTMAP_FNS(prefix, maptype, valtype) \
213 typedef struct maptype maptype; \
214 typedef struct prefix##iter_t prefix##iter_t; \
215 static INLINE maptype* prefix##new(void) \
217 return (maptype*)digestmap_new(); \
219 static INLINE valtype* prefix##get(maptype *map, const char *key) \
221 return (valtype*)digestmap_get((digestmap_t*)map, key); \
223 static INLINE valtype* prefix##set(maptype *map, const char *key, \
226 return (valtype*)digestmap_set((digestmap_t*)map, key, val); \
228 static INLINE valtype* prefix##remove(maptype *map, const char *key) \
230 return (valtype*)digestmap_remove((digestmap_t*)map, key); \
232 static INLINE void prefix##free(maptype *map, void (*free_val)(void*)) \
234 digestmap_free((digestmap_t*)map, free_val); \
236 static INLINE int prefix##isempty(maptype *map) \
238 return digestmap_isempty((digestmap_t*)map); \
240 static INLINE int prefix##size(maptype *map) \
242 return digestmap_size((digestmap_t*)map); \
244 static INLINE prefix##iter_t *prefix##iter_init(maptype *map) \
246 return (prefix##iter_t*) digestmap_iter_init((digestmap_t*)map); \
248 static INLINE prefix##iter_t *prefix##iter_next(maptype *map, \
249 prefix##iter_t *iter) \
251 return (prefix##iter_t*) digestmap_iter_next( \
252 (digestmap_t*)map, (digestmap_iter_t*)iter); \
254 static INLINE prefix##iter_t *prefix##iter_next_rmv(maptype *map, \
255 prefix##iter_t *iter) \
257 return (prefix##iter_t*) digestmap_iter_next_rmv( \
258 (digestmap_t*)map, (digestmap_iter_t*)iter); \
260 static INLINE void prefix##iter_get(prefix##iter_t *iter, \
265 digestmap_iter_get((digestmap_iter_t*) iter, keyp, &v); \
268 static INLINE int prefix##iter_done(prefix##iter_t *iter) \
270 return digestmap_iter_done((digestmap_iter_t*)iter); \
274 #define BITARRAY_SHIFT 5
275 #elif SIZEOF_INT == 8
276 #define BITARRAY_SHIFT 6
278 #error "int is neither 4 nor 8 bytes. I can't deal with that."
280 #define BITARRAY_MASK ((1u<<BITARRAY_SHIFT)-1)
282 /** A random-access array of one-bit-wide elements. */
283 typedef unsigned int bitarray_t
;
284 /** Create a new bit array that can hold <b>n_bits</b> bits. */
285 static INLINE bitarray_t
*
286 bitarray_init_zero(int n_bits
)
288 size_t sz
= (n_bits
+BITARRAY_MASK
) / (1u << BITARRAY_SHIFT
);
289 return tor_malloc_zero(sz
*sizeof(unsigned int));
291 /** Free the bit array <b>ba</b>. */
293 bitarray_free(bitarray_t
*ba
)
297 /** Set the <b>bit</b>th bit in <b>b</b> to 1. */
299 bitarray_set(bitarray_t
*b
, int bit
)
301 b
[bit
>> BITARRAY_SHIFT
] |= (1u << (bit
& BITARRAY_MASK
));
303 /** Set the <b>bit</b>th bit in <b>b</b> to 0. */
305 bitarray_clear(bitarray_t
*b
, int bit
)
307 b
[bit
>> BITARRAY_SHIFT
] &= ~ (1u << (bit
& BITARRAY_MASK
));
309 /** Return true iff <b>bit</b>th bit in <b>b</b> is nonzero. NOTE: does
310 * not necessarily return 1 on true. */
311 static INLINE
unsigned int
312 bitarray_is_set(bitarray_t
*b
, int bit
)
314 return b
[bit
>> BITARRAY_SHIFT
] & (1u << (bit
& BITARRAY_MASK
));
317 /* These functions, given an <b>array</b> of <b>n_elements</b>, return the
318 * <b>nth</b> lowest element. <b>nth</b>=0 gives the lowest element;
319 * <b>n_elements</b>-1 gives the highest; and (<b>n_elements</b>-1) / 2 gives
320 * the median. As a side effect, the elements of <b>array</b> are sorted. */
321 int find_nth_int(int *array
, int n_elements
, int nth
);
322 time_t find_nth_time(time_t *array
, int n_elements
, int nth
);
323 double find_nth_double(double *array
, int n_elements
, int nth
);
324 uint32_t find_nth_uint32(uint32_t *array
, int n_elements
, int nth
);
326 median_int(int *array
, int n_elements
)
328 return find_nth_int(array
, n_elements
, (n_elements
-1)/2);
331 median_time(time_t *array
, int n_elements
)
333 return find_nth_time(array
, n_elements
, (n_elements
-1)/2);
336 median_double(double *array
, int n_elements
)
338 return find_nth_double(array
, n_elements
, (n_elements
-1)/2);
340 static INLINE
uint32_t
341 median_uint32(uint32_t *array
, int n_elements
)
343 return find_nth_uint32(array
, n_elements
, (n_elements
-1)/2);