fix up contrib/checkOptionDocs.pl to match current practice (asciidoc manpage, no...
[tor.git] / src / common / container.h
blob3568de01598701fda5245a592c0f1d4e5a7c80d5
1 /* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2010, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 #ifndef _TOR_CONTAINER_H
7 #define _TOR_CONTAINER_H
9 #include "util.h"
11 /** A resizeable list of pointers, with associated helpful functionality.
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 through 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.
22 void **list;
23 int num_used;
24 int capacity;
25 } smartlist_t;
27 smartlist_t *smartlist_create(void);
28 void smartlist_free(smartlist_t *sl);
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);
50 /* smartlist_choose() is defined in crypto.[ch] */
51 #ifdef DEBUG_SMARTLIST
52 /** Return the number of items in sl.
54 static INLINE int smartlist_len(const smartlist_t *sl) ATTR_PURE;
55 static INLINE int smartlist_len(const smartlist_t *sl) {
56 tor_assert(sl);
57 return (sl)->num_used;
59 /** Return the <b>idx</b>th element of sl.
61 static INLINE void *smartlist_get(const smartlist_t *sl, int idx) ATTR_PURE;
62 static INLINE void *smartlist_get(const smartlist_t *sl, int idx) {
63 tor_assert(sl);
64 tor_assert(idx>=0);
65 tor_assert(sl->num_used > idx);
66 return sl->list[idx];
68 static INLINE void smartlist_set(smartlist_t *sl, int idx, void *val) {
69 tor_assert(sl);
70 tor_assert(idx>=0);
71 tor_assert(sl->num_used > idx);
72 sl->list[idx] = val;
74 #else
75 #define smartlist_len(sl) ((sl)->num_used)
76 #define smartlist_get(sl, idx) ((sl)->list[idx])
77 #define smartlist_set(sl, idx, val) ((sl)->list[idx] = (val))
78 #endif
80 /** Exchange the elements at indices <b>idx1</b> and <b>idx2</b> of the
81 * smartlist <b>sl</b>. */
82 static INLINE void smartlist_swap(smartlist_t *sl, int idx1, int idx2)
84 if (idx1 != idx2) {
85 void *elt = smartlist_get(sl, idx1);
86 smartlist_set(sl, idx1, smartlist_get(sl, idx2));
87 smartlist_set(sl, idx2, elt);
91 void smartlist_del(smartlist_t *sl, int idx);
92 void smartlist_del_keeporder(smartlist_t *sl, int idx);
93 void smartlist_insert(smartlist_t *sl, int idx, void *val);
94 void smartlist_sort(smartlist_t *sl,
95 int (*compare)(const void **a, const void **b));
96 void *smartlist_get_most_frequent(const smartlist_t *sl,
97 int (*compare)(const void **a, const void **b));
98 void smartlist_uniq(smartlist_t *sl,
99 int (*compare)(const void **a, const void **b),
100 void (*free_fn)(void *elt));
102 void smartlist_sort_strings(smartlist_t *sl);
103 void smartlist_sort_digests(smartlist_t *sl);
104 void smartlist_sort_digests256(smartlist_t *sl);
106 char *smartlist_get_most_frequent_string(smartlist_t *sl);
107 char *smartlist_get_most_frequent_digest256(smartlist_t *sl);
109 void smartlist_uniq_strings(smartlist_t *sl);
110 void smartlist_uniq_digests(smartlist_t *sl);
111 void smartlist_uniq_digests256(smartlist_t *sl);
112 void *smartlist_bsearch(smartlist_t *sl, const void *key,
113 int (*compare)(const void *key, const void **member))
114 ATTR_PURE;
115 int smartlist_bsearch_idx(const smartlist_t *sl, const void *key,
116 int (*compare)(const void *key, const void **member),
117 int *found_out);
119 void smartlist_pqueue_add(smartlist_t *sl,
120 int (*compare)(const void *a, const void *b),
121 int idx_field_offset,
122 void *item);
123 void *smartlist_pqueue_pop(smartlist_t *sl,
124 int (*compare)(const void *a, const void *b),
125 int idx_field_offset);
126 void smartlist_pqueue_remove(smartlist_t *sl,
127 int (*compare)(const void *a, const void *b),
128 int idx_field_offset,
129 void *item);
130 void smartlist_pqueue_assert_ok(smartlist_t *sl,
131 int (*compare)(const void *a, const void *b),
132 int idx_field_offset);
134 #define SPLIT_SKIP_SPACE 0x01
135 #define SPLIT_IGNORE_BLANK 0x02
136 #define SPLIT_STRIP_SPACE 0x04
137 int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
138 int flags, int max);
139 char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate,
140 size_t *len_out) ATTR_MALLOC;
141 char *smartlist_join_strings2(smartlist_t *sl, const char *join,
142 size_t join_len, int terminate, size_t *len_out)
143 ATTR_MALLOC;
145 /** Iterate over the items in a smartlist <b>sl</b>, in order. For each item,
146 * assign it to a new local variable of type <b>type</b> named <b>var</b>, and
147 * execute the statement <b>cmd</b>. Inside the loop, the loop index can
148 * be accessed as <b>var</b>_sl_idx and the length of the list can be accessed
149 * as <b>var</b>_sl_len.
151 * NOTE: Do not change the length of the list while the loop is in progress,
152 * unless you adjust the _sl_len variable correspondingly. See second example
153 * below.
155 * Example use:
156 * <pre>
157 * smartlist_t *list = smartlist_split("A:B:C", ":", 0, 0);
158 * SMARTLIST_FOREACH(list, char *, cp,
160 * printf("%d: %s\n", cp_sl_idx, cp);
161 * tor_free(cp);
162 * });
163 * smartlist_free(list);
164 * </pre>
166 * Example use (advanced):
167 * <pre>
168 * SMARTLIST_FOREACH(list, char *, cp,
170 * if (!strcmp(cp, "junk")) {
171 * tor_free(cp);
172 * SMARTLIST_DEL_CURRENT(list, cp);
174 * });
175 * </pre>
177 /* Note: these macros use token pasting, and reach into smartlist internals.
178 * This can make them a little daunting. Here's the approximate unpacking of
179 * the above examples, for entertainment value:
181 * <pre>
182 * smartlist_t *list = smartlist_split("A:B:C", ":", 0, 0);
184 * int cp_sl_idx, cp_sl_len = smartlist_len(list);
185 * char *cp;
186 * for (cp_sl_idx = 0; cp_sl_idx < cp_sl_len; ++cp_sl_idx) {
187 * cp = smartlist_get(list, cp_sl_idx);
188 * printf("%d: %s\n", cp_sl_idx, cp);
189 * tor_free(cp);
192 * smartlist_free(list);
193 * </pre>
195 * <pre>
197 * int cp_sl_idx, cp_sl_len = smartlist_len(list);
198 * char *cp;
199 * for (cp_sl_idx = 0; cp_sl_idx < cp_sl_len; ++cp_sl_idx) {
200 * cp = smartlist_get(list, cp_sl_idx);
201 * if (!strcmp(cp, "junk")) {
202 * tor_free(cp);
203 * smartlist_del(list, cp_sl_idx);
204 * --cp_sl_idx;
205 * --cp_sl_len;
209 * </pre>
211 #define SMARTLIST_FOREACH_BEGIN(sl, type, var) \
212 STMT_BEGIN \
213 int var ## _sl_idx, var ## _sl_len=(sl)->num_used; \
214 type var; \
215 for (var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len; \
216 ++var ## _sl_idx) { \
217 var = (sl)->list[var ## _sl_idx];
219 #define SMARTLIST_FOREACH_END(var) \
220 var = NULL; \
221 } STMT_END
223 #define SMARTLIST_FOREACH(sl, type, var, cmd) \
224 SMARTLIST_FOREACH_BEGIN(sl,type,var) { \
225 cmd; \
226 } SMARTLIST_FOREACH_END(var)
228 /** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed
229 * with the variable <b>var</b>, remove the current element in a way that
230 * won't confuse the loop. */
231 #define SMARTLIST_DEL_CURRENT(sl, var) \
232 STMT_BEGIN \
233 smartlist_del(sl, var ## _sl_idx); \
234 --var ## _sl_idx; \
235 --var ## _sl_len; \
236 STMT_END
238 /** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed
239 * with the variable <b>var</b>, replace the current element with <b>val</b>.
240 * Does not deallocate the current value of <b>var</b>.
242 #define SMARTLIST_REPLACE_CURRENT(sl, var, val) \
243 STMT_BEGIN \
244 smartlist_set(sl, var ## _sl_idx, val); \
245 STMT_END
247 /* Helper: Given two lists of items, possibly of different types, such that
248 * both lists are sorted on some common field (as determined by a comparison
249 * expression <b>cmpexpr</b>), and such that one list (<b>sl1</b>) has no
250 * duplicates on the common field, loop through the lists in lockstep, and
251 * execute <b>unmatched_var2</b> on items in var2 that do not appear in
252 * var1.
254 * WARNING: It isn't safe to add remove elements from either list while the
255 * loop is in progress.
257 * Example use:
258 * SMARTLIST_FOREACH_JOIN(routerstatus_list, routerstatus_t *, rs,
259 * routerinfo_list, routerinfo_t *, ri,
260 * memcmp(rs->identity_digest, ri->identity_digest, 20),
261 * log_info(LD_GENERAL,"No match for %s", ri->nickname)) {
262 * log_info(LD_GENERAL, "%s matches routerstatus %p", ri->nickname, rs);
263 * } SMARTLIST_FOREACH_JOIN_END(rs, ri);
265 /* The example above unpacks (approximately) to:
266 * int rs_sl_idx = 0, rs_sl_len = smartlist_len(routerstatus_list);
267 * int ri_sl_idx, ri_sl_len = smartlist_len(routerinfo_list);
268 * int rs_ri_cmp;
269 * routerstatus_t *rs;
270 * routerinfo_t *ri;
271 * for (; ri_sl_idx < ri_sl_len; ++ri_sl_idx) {
272 * ri = smartlist_get(routerinfo_list, ri_sl_idx);
273 * while (rs_sl_idx < rs_sl_len) {
274 * rs = smartlist_get(routerstatus_list, rs_sl_idx);
275 * rs_ri_cmp = memcmp(rs->identity_digest, ri->identity_digest, 20);
276 * if (rs_ri_cmp > 0) {
277 * break;
278 * } else if (rs_ri_cmp == 0) {
279 * goto matched_ri;
280 * } else {
281 * ++rs_sl_idx;
284 * log_info(LD_GENERAL,"No match for %s", ri->nickname);
285 * continue;
286 * matched_ri: {
287 * log_info(LD_GENERAL,"%s matches with routerstatus %p",ri->nickname,rs);
291 #define SMARTLIST_FOREACH_JOIN(sl1, type1, var1, sl2, type2, var2, \
292 cmpexpr, unmatched_var2) \
293 STMT_BEGIN \
294 int var1 ## _sl_idx = 0, var1 ## _sl_len=(sl1)->num_used; \
295 int var2 ## _sl_idx = 0, var2 ## _sl_len=(sl2)->num_used; \
296 int var1 ## _ ## var2 ## _cmp; \
297 type1 var1; \
298 type2 var2; \
299 for (; var2##_sl_idx < var2##_sl_len; ++var2##_sl_idx) { \
300 var2 = (sl2)->list[var2##_sl_idx]; \
301 while (var1##_sl_idx < var1##_sl_len) { \
302 var1 = (sl1)->list[var1##_sl_idx]; \
303 var1##_##var2##_cmp = (cmpexpr); \
304 if (var1##_##var2##_cmp > 0) { \
305 break; \
306 } else if (var1##_##var2##_cmp == 0) { \
307 goto matched_##var2; \
308 } else { \
309 ++var1##_sl_idx; \
312 /* Ran out of v1, or no match for var2. */ \
313 unmatched_var2; \
314 continue; \
315 matched_##var2: ; \
317 #define SMARTLIST_FOREACH_JOIN_END(var1, var2) \
319 STMT_END
321 #define DECLARE_MAP_FNS(maptype, keytype, prefix) \
322 typedef struct maptype maptype; \
323 typedef struct prefix##entry_t *prefix##iter_t; \
324 maptype* prefix##new(void); \
325 void* prefix##set(maptype *map, keytype key, void *val); \
326 void* prefix##get(const maptype *map, keytype key); \
327 void* prefix##remove(maptype *map, keytype key); \
328 void prefix##free(maptype *map, void (*free_val)(void*)); \
329 int prefix##isempty(const maptype *map); \
330 int prefix##size(const maptype *map); \
331 prefix##iter_t *prefix##iter_init(maptype *map); \
332 prefix##iter_t *prefix##iter_next(maptype *map, prefix##iter_t *iter); \
333 prefix##iter_t *prefix##iter_next_rmv(maptype *map, prefix##iter_t *iter); \
334 void prefix##iter_get(prefix##iter_t *iter, keytype *keyp, void **valp); \
335 int prefix##iter_done(prefix##iter_t *iter); \
336 void prefix##assert_ok(const maptype *map)
338 /* Map from const char * to void *. Implemented with a hash table. */
339 DECLARE_MAP_FNS(strmap_t, const char *, strmap_);
340 /* Map from const char[DIGEST_LEN] to void *. Implemented with a hash table. */
341 DECLARE_MAP_FNS(digestmap_t, const char *, digestmap_);
343 #undef DECLARE_MAP_FNS
345 /** Iterates over the key-value pairs in a map <b>map</b> in order.
346 * <b>prefix</b> is as for DECLARE_MAP_FNS (i.e., strmap_ or digestmap_).
347 * The map's keys and values are of type keytype and valtype respectively;
348 * each iteration assigns them to keyvar and valvar.
350 * Example use:
351 * MAP_FOREACH(digestmap_, m, const char *, k, routerinfo_t *, r) {
352 * // use k and r
353 * } MAP_FOREACH_END.
355 /* Unpacks to, approximately:
357 * digestmap_iter_t *k_iter;
358 * for (k_iter = digestmap_iter_init(m); !digestmap_iter_done(k_iter);
359 * k_iter = digestmap_iter_next(m, k_iter)) {
360 * const char *k;
361 * void *r_voidp;
362 * routerinfo_t *r;
363 * digestmap_iter_get(k_iter, &k, &r_voidp);
364 * r = r_voidp;
365 * // use k and r
369 #define MAP_FOREACH(prefix, map, keytype, keyvar, valtype, valvar) \
370 STMT_BEGIN \
371 prefix##iter_t *keyvar##_iter; \
372 for (keyvar##_iter = prefix##iter_init(map); \
373 !prefix##iter_done(keyvar##_iter); \
374 keyvar##_iter = prefix##iter_next(map, keyvar##_iter)) { \
375 keytype keyvar; \
376 void *valvar##_voidp; \
377 valtype valvar; \
378 prefix##iter_get(keyvar##_iter, &keyvar, &valvar##_voidp); \
379 valvar = valvar##_voidp;
381 /** As MAP_FOREACH, except allows members to be removed from the map
382 * during the iteration via MAP_DEL_CURRENT. Example use:
384 * Example use:
385 * MAP_FOREACH(digestmap_, m, const char *, k, routerinfo_t *, r) {
386 * if (is_very_old(r))
387 * MAP_DEL_CURRENT(k);
388 * } MAP_FOREACH_END.
390 /* Unpacks to, approximately:
392 * digestmap_iter_t *k_iter;
393 * int k_del=0;
394 * for (k_iter = digestmap_iter_init(m); !digestmap_iter_done(k_iter);
395 * k_iter = k_del ? digestmap_iter_next(m, k_iter)
396 * : digestmap_iter_next_rmv(m, k_iter)) {
397 * const char *k;
398 * void *r_voidp;
399 * routerinfo_t *r;
400 * k_del=0;
401 * digestmap_iter_get(k_iter, &k, &r_voidp);
402 * r = r_voidp;
403 * if (is_very_old(r)) {
404 * k_del = 1;
409 #define MAP_FOREACH_MODIFY(prefix, map, keytype, keyvar, valtype, valvar) \
410 STMT_BEGIN \
411 prefix##iter_t *keyvar##_iter; \
412 int keyvar##_del=0; \
413 for (keyvar##_iter = prefix##iter_init(map); \
414 !prefix##iter_done(keyvar##_iter); \
415 keyvar##_iter = keyvar##_del ? \
416 prefix##iter_next_rmv(map, keyvar##_iter) : \
417 prefix##iter_next(map, keyvar##_iter)) { \
418 keytype keyvar; \
419 void *valvar##_voidp; \
420 valtype valvar; \
421 keyvar##_del=0; \
422 prefix##iter_get(keyvar##_iter, &keyvar, &valvar##_voidp); \
423 valvar = valvar##_voidp;
425 /** Used with MAP_FOREACH_MODIFY to remove the currently-iterated-upon
426 * member of the map. */
427 #define MAP_DEL_CURRENT(keyvar) \
428 STMT_BEGIN \
429 keyvar##_del = 1; \
430 STMT_END
432 /** Used to end a MAP_FOREACH() block. */
433 #define MAP_FOREACH_END } STMT_END ;
435 /** As MAP_FOREACH, but does not require declaration of prefix or keytype.
436 * Example use:
437 * DIGESTMAP_FOREACH(m, k, routerinfo_t *, r) {
438 * // use k and r
439 * } DIGESTMAP_FOREACH_END.
441 #define DIGESTMAP_FOREACH(map, keyvar, valtype, valvar) \
442 MAP_FOREACH(digestmap_, map, const char *, keyvar, valtype, valvar)
444 /** As MAP_FOREACH_MODIFY, but does not require declaration of prefix or
445 * keytype.
446 * Example use:
447 * DIGESTMAP_FOREACH_MODIFY(m, k, routerinfo_t *, r) {
448 * if (is_very_old(r))
449 * MAP_DEL_CURRENT(k);
450 * } DIGESTMAP_FOREACH_END.
452 #define DIGESTMAP_FOREACH_MODIFY(map, keyvar, valtype, valvar) \
453 MAP_FOREACH_MODIFY(digestmap_, map, const char *, keyvar, valtype, valvar)
454 /** Used to end a DIGESTMAP_FOREACH() block. */
455 #define DIGESTMAP_FOREACH_END MAP_FOREACH_END
457 #define STRMAP_FOREACH(map, keyvar, valtype, valvar) \
458 MAP_FOREACH(strmap_, map, const char *, keyvar, valtype, valvar)
459 #define STRMAP_FOREACH_MODIFY(map, keyvar, valtype, valvar) \
460 MAP_FOREACH_MODIFY(strmap_, map, const char *, keyvar, valtype, valvar)
461 #define STRMAP_FOREACH_END MAP_FOREACH_END
463 void* strmap_set_lc(strmap_t *map, const char *key, void *val);
464 void* strmap_get_lc(const strmap_t *map, const char *key);
465 void* strmap_remove_lc(strmap_t *map, const char *key);
467 #define DECLARE_TYPED_DIGESTMAP_FNS(prefix, maptype, valtype) \
468 typedef struct maptype maptype; \
469 typedef struct prefix##iter_t prefix##iter_t; \
470 static INLINE maptype* prefix##new(void) \
472 return (maptype*)digestmap_new(); \
474 static INLINE digestmap_t* prefix##to_digestmap(maptype *map) \
476 return (digestmap_t*)map; \
478 static INLINE valtype* prefix##get(maptype *map, const char *key) \
480 return (valtype*)digestmap_get((digestmap_t*)map, key); \
482 static INLINE valtype* prefix##set(maptype *map, const char *key, \
483 valtype *val) \
485 return (valtype*)digestmap_set((digestmap_t*)map, key, val); \
487 static INLINE valtype* prefix##remove(maptype *map, const char *key) \
489 return (valtype*)digestmap_remove((digestmap_t*)map, key); \
491 static INLINE void prefix##free(maptype *map, void (*free_val)(void*)) \
493 digestmap_free((digestmap_t*)map, free_val); \
495 static INLINE int prefix##isempty(maptype *map) \
497 return digestmap_isempty((digestmap_t*)map); \
499 static INLINE int prefix##size(maptype *map) \
501 return digestmap_size((digestmap_t*)map); \
503 static INLINE prefix##iter_t *prefix##iter_init(maptype *map) \
505 return (prefix##iter_t*) digestmap_iter_init((digestmap_t*)map); \
507 static INLINE prefix##iter_t *prefix##iter_next(maptype *map, \
508 prefix##iter_t *iter) \
510 return (prefix##iter_t*) digestmap_iter_next( \
511 (digestmap_t*)map, (digestmap_iter_t*)iter); \
513 static INLINE prefix##iter_t *prefix##iter_next_rmv(maptype *map, \
514 prefix##iter_t *iter) \
516 return (prefix##iter_t*) digestmap_iter_next_rmv( \
517 (digestmap_t*)map, (digestmap_iter_t*)iter); \
519 static INLINE void prefix##iter_get(prefix##iter_t *iter, \
520 const char **keyp, \
521 valtype **valp) \
523 void *v; \
524 digestmap_iter_get((digestmap_iter_t*) iter, keyp, &v); \
525 *valp = v; \
527 static INLINE int prefix##iter_done(prefix##iter_t *iter) \
529 return digestmap_iter_done((digestmap_iter_t*)iter); \
532 #if SIZEOF_INT == 4
533 #define BITARRAY_SHIFT 5
534 #elif SIZEOF_INT == 8
535 #define BITARRAY_SHIFT 6
536 #else
537 #error "int is neither 4 nor 8 bytes. I can't deal with that."
538 #endif
539 #define BITARRAY_MASK ((1u<<BITARRAY_SHIFT)-1)
541 /** A random-access array of one-bit-wide elements. */
542 typedef unsigned int bitarray_t;
543 /** Create a new bit array that can hold <b>n_bits</b> bits. */
544 static INLINE bitarray_t *
545 bitarray_init_zero(unsigned int n_bits)
547 /* round up to the next int. */
548 size_t sz = (n_bits+BITARRAY_MASK) >> BITARRAY_SHIFT;
549 return tor_malloc_zero(sz*sizeof(unsigned int));
551 /** Expand <b>ba</b> from holding <b>n_bits_old</b> to <b>n_bits_new</b>,
552 * clearing all new bits. Returns a possibly changed pointer to the
553 * bitarray. */
554 static INLINE bitarray_t *
555 bitarray_expand(bitarray_t *ba,
556 unsigned int n_bits_old, unsigned int n_bits_new)
558 size_t sz_old = (n_bits_old+BITARRAY_MASK) >> BITARRAY_SHIFT;
559 size_t sz_new = (n_bits_new+BITARRAY_MASK) >> BITARRAY_SHIFT;
560 char *ptr;
561 if (sz_new <= sz_old)
562 return ba;
563 ptr = tor_realloc(ba, sz_new*sizeof(unsigned int));
564 /* This memset does nothing to the older excess bytes. But they were
565 * already set to 0 by bitarry_init_zero. */
566 memset(ptr+sz_old*sizeof(unsigned int), 0,
567 (sz_new-sz_old)*sizeof(unsigned int));
568 return (bitarray_t*) ptr;
570 /** Free the bit array <b>ba</b>. */
571 static INLINE void
572 bitarray_free(bitarray_t *ba)
574 tor_free(ba);
576 /** Set the <b>bit</b>th bit in <b>b</b> to 1. */
577 static INLINE void
578 bitarray_set(bitarray_t *b, int bit)
580 b[bit >> BITARRAY_SHIFT] |= (1u << (bit & BITARRAY_MASK));
582 /** Set the <b>bit</b>th bit in <b>b</b> to 0. */
583 static INLINE void
584 bitarray_clear(bitarray_t *b, int bit)
586 b[bit >> BITARRAY_SHIFT] &= ~ (1u << (bit & BITARRAY_MASK));
588 /** Return true iff <b>bit</b>th bit in <b>b</b> is nonzero. NOTE: does
589 * not necessarily return 1 on true. */
590 static INLINE unsigned int
591 bitarray_is_set(bitarray_t *b, int bit)
593 return b[bit >> BITARRAY_SHIFT] & (1u << (bit & BITARRAY_MASK));
596 /** A set of digests, implemented as a Bloom filter. */
597 typedef struct {
598 int mask; /* One less than the number of bits in <b>ba</b>; always one less
599 * than a power of two. */
600 bitarray_t *ba; /* A bit array to implement the Bloom filter. */
601 } digestset_t;
603 #define BIT(n) ((n) & set->mask)
604 /** Add the digest <b>digest</b> to <b>set</b>. */
605 static INLINE void
606 digestset_add(digestset_t *set, const char *digest)
608 const uint32_t *p = (const uint32_t *)digest;
609 const uint32_t d1 = p[0] + (p[1]>>16);
610 const uint32_t d2 = p[1] + (p[2]>>16);
611 const uint32_t d3 = p[2] + (p[3]>>16);
612 const uint32_t d4 = p[3] + (p[0]>>16);
613 bitarray_set(set->ba, BIT(d1));
614 bitarray_set(set->ba, BIT(d2));
615 bitarray_set(set->ba, BIT(d3));
616 bitarray_set(set->ba, BIT(d4));
619 /** If <b>digest</b> is in <b>set</b>, return nonzero. Otherwise,
620 * <em>probably</em> return zero. */
621 static INLINE int
622 digestset_isin(const digestset_t *set, const char *digest)
624 const uint32_t *p = (const uint32_t *)digest;
625 const uint32_t d1 = p[0] + (p[1]>>16);
626 const uint32_t d2 = p[1] + (p[2]>>16);
627 const uint32_t d3 = p[2] + (p[3]>>16);
628 const uint32_t d4 = p[3] + (p[0]>>16);
629 return bitarray_is_set(set->ba, BIT(d1)) &&
630 bitarray_is_set(set->ba, BIT(d2)) &&
631 bitarray_is_set(set->ba, BIT(d3)) &&
632 bitarray_is_set(set->ba, BIT(d4));
634 #undef BIT
636 digestset_t *digestset_new(int max_elements);
637 void digestset_free(digestset_t* set);
639 /* These functions, given an <b>array</b> of <b>n_elements</b>, return the
640 * <b>nth</b> lowest element. <b>nth</b>=0 gives the lowest element;
641 * <b>n_elements</b>-1 gives the highest; and (<b>n_elements</b>-1) / 2 gives
642 * the median. As a side effect, the elements of <b>array</b> are sorted. */
643 int find_nth_int(int *array, int n_elements, int nth);
644 time_t find_nth_time(time_t *array, int n_elements, int nth);
645 double find_nth_double(double *array, int n_elements, int nth);
646 int32_t find_nth_int32(int32_t *array, int n_elements, int nth);
647 uint32_t find_nth_uint32(uint32_t *array, int n_elements, int nth);
648 long find_nth_long(long *array, int n_elements, int nth);
649 static INLINE int
650 median_int(int *array, int n_elements)
652 return find_nth_int(array, n_elements, (n_elements-1)/2);
654 static INLINE time_t
655 median_time(time_t *array, int n_elements)
657 return find_nth_time(array, n_elements, (n_elements-1)/2);
659 static INLINE double
660 median_double(double *array, int n_elements)
662 return find_nth_double(array, n_elements, (n_elements-1)/2);
664 static INLINE uint32_t
665 median_uint32(uint32_t *array, int n_elements)
667 return find_nth_uint32(array, n_elements, (n_elements-1)/2);
669 static INLINE int32_t
670 median_int32(int32_t *array, int n_elements)
672 return find_nth_int32(array, n_elements, (n_elements-1)/2);
674 static INLINE long
675 median_long(long *array, int n_elements)
677 return find_nth_long(array, n_elements, (n_elements-1)/2);
680 #endif