lookup_last_hid_serv_request() could overflow and leak memory
[tor/rransom.git] / src / common / container.h
blobe6265524677c1674c1023fe1130e7e6f1ce33d66
1 /* Copyright (c) 2003-2004, Roger Dingledine
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2009, 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_uniq(smartlist_t *sl,
97 int (*compare)(const void **a, const void **b),
98 void (*free_fn)(void *elt));
99 void smartlist_sort_strings(smartlist_t *sl);
100 void smartlist_sort_digests(smartlist_t *sl);
101 void smartlist_uniq_strings(smartlist_t *sl);
102 void smartlist_uniq_digests(smartlist_t *sl);
103 void *smartlist_bsearch(smartlist_t *sl, const void *key,
104 int (*compare)(const void *key, const void **member))
105 ATTR_PURE;
106 int smartlist_bsearch_idx(const smartlist_t *sl, const void *key,
107 int (*compare)(const void *key, const void **member),
108 int *found_out);
110 void smartlist_pqueue_add(smartlist_t *sl,
111 int (*compare)(const void *a, const void *b),
112 void *item);
113 void *smartlist_pqueue_pop(smartlist_t *sl,
114 int (*compare)(const void *a, const void *b));
115 void smartlist_pqueue_assert_ok(smartlist_t *sl,
116 int (*compare)(const void *a, const void *b));
118 #define SPLIT_SKIP_SPACE 0x01
119 #define SPLIT_IGNORE_BLANK 0x02
120 #define SPLIT_STRIP_SPACE 0x04
121 int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
122 int flags, int max);
123 char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate,
124 size_t *len_out) ATTR_MALLOC;
125 char *smartlist_join_strings2(smartlist_t *sl, const char *join,
126 size_t join_len, int terminate, size_t *len_out)
127 ATTR_MALLOC;
129 /** Iterate over the items in a smartlist <b>sl</b>, in order. For each item,
130 * assign it to a new local variable of type <b>type</b> named <b>var</b>, and
131 * execute the statement <b>cmd</b>. Inside the loop, the loop index can
132 * be accessed as <b>var</b>_sl_idx and the length of the list can be accessed
133 * as <b>var</b>_sl_len.
135 * NOTE: Do not change the length of the list while the loop is in progress,
136 * unless you adjust the _sl_len variable correspondingly. See second example
137 * below.
139 * Example use:
140 * <pre>
141 * smartlist_t *list = smartlist_split("A:B:C", ":", 0, 0);
142 * SMARTLIST_FOREACH(list, char *, cp,
144 * printf("%d: %s\n", cp_sl_idx, cp);
145 * tor_free(cp);
146 * });
147 * smartlist_free(list);
148 * </pre>
150 * Example use (advanced):
151 * <pre>
152 * SMARTLIST_FOREACH(list, char *, cp,
154 * if (!strcmp(cp, "junk")) {
155 * tor_free(cp);
156 * SMARTLIST_DEL_CURRENT(list, cp);
158 * });
159 * </pre>
161 /* Note: these macros use token pasting, and reach into smartlist internals.
162 * This can make them a little daunting. Here's the approximate unpacking of
163 * the above examples, for entertainment value:
165 * <pre>
166 * smartlist_t *list = smartlist_split("A:B:C", ":", 0, 0);
168 * int cp_sl_idx, cp_sl_len = smartlist_len(list);
169 * char *cp;
170 * for (cp_sl_idx = 0; cp_sl_idx < cp_sl_len; ++cp_sl_idx) {
171 * cp = smartlist_get(list, cp_sl_idx);
172 * printf("%d: %s\n", cp_sl_idx, cp);
173 * tor_free(cp);
176 * smartlist_free(list);
177 * </pre>
179 * <pre>
181 * int cp_sl_idx, cp_sl_len = smartlist_len(list);
182 * char *cp;
183 * for (cp_sl_idx = 0; cp_sl_idx < cp_sl_len; ++cp_sl_idx) {
184 * cp = smartlist_get(list, cp_sl_idx);
185 * if (!strcmp(cp, "junk")) {
186 * tor_free(cp);
187 * smartlist_del(list, cp_sl_idx);
188 * --cp_sl_idx;
189 * --cp_sl_len;
193 * </pre>
195 #define SMARTLIST_FOREACH_BEGIN(sl, type, var) \
196 STMT_BEGIN \
197 int var ## _sl_idx, var ## _sl_len=(sl)->num_used; \
198 type var; \
199 for (var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len; \
200 ++var ## _sl_idx) { \
201 var = (sl)->list[var ## _sl_idx];
203 #define SMARTLIST_FOREACH_END(var) \
204 var = NULL; \
205 } STMT_END
207 #define SMARTLIST_FOREACH(sl, type, var, cmd) \
208 SMARTLIST_FOREACH_BEGIN(sl,type,var) { \
209 cmd; \
210 } SMARTLIST_FOREACH_END(var)
212 /** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed
213 * with the variable <b>var</b>, remove the current element in a way that
214 * won't confuse the loop. */
215 #define SMARTLIST_DEL_CURRENT(sl, var) \
216 STMT_BEGIN \
217 smartlist_del(sl, var ## _sl_idx); \
218 --var ## _sl_idx; \
219 --var ## _sl_len; \
220 STMT_END
222 /** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed
223 * with the variable <b>var</b>, replace the current element with <b>val</b>.
224 * Does not deallocate the current value of <b>var</b>.
226 #define SMARTLIST_REPLACE_CURRENT(sl, var, val) \
227 STMT_BEGIN \
228 smartlist_set(sl, var ## _sl_idx, val); \
229 STMT_END
231 /* Helper: Given two lists of items, possibly of different types, such that
232 * both lists are sorted on some common field (as determined by a comparison
233 * expression <b>cmpexpr</b>), and such that one list (<b>sl1</b>) has no
234 * duplicates on the common field, loop through the lists in lockstep, and
235 * execute <b>unmatched_var2</b> on items in var2 that do not appear in
236 * var1.
238 * WARNING: It isn't safe to add remove elements from either list while the
239 * loop is in progress.
241 * Example use:
242 * SMARTLIST_FOREACH_JOIN(routerstatus_list, routerstatus_t *, rs,
243 * routerinfo_list, routerinfo_t *, ri,
244 * memcmp(rs->identity_digest, ri->identity_digest, 20),
245 * log_info(LD_GENERAL,"No match for %s", ri->nickname)) {
246 * log_info(LD_GENERAL, "%s matches routerstatus %p", ri->nickname, rs);
247 * } SMARTLIST_FOREACH_JOIN_END(rs, ri);
249 /* The example above unpacks (approximately) to:
250 * int rs_sl_idx = 0, rs_sl_len = smartlist_len(routerstatus_list);
251 * int ri_sl_idx, ri_sl_len = smartlist_len(routerinfo_list);
252 * int rs_ri_cmp;
253 * routerstatus_t *rs;
254 * routerinfo_t *ri;
255 * for (; ri_sl_idx < ri_sl_len; ++ri_sl_idx) {
256 * ri = smartlist_get(routerinfo_list, ri_sl_idx);
257 * while (rs_sl_idx < rs_sl_len) {
258 * rs = smartlist_get(routerstatus_list, rs_sl_idx);
259 * rs_ri_cmp = memcmp(rs->identity_digest, ri->identity_digest, 20);
260 * if (rs_ri_cmp > 0) {
261 * break;
262 * } else if (rs_ri_cmp == 0) {
263 * goto matched_ri;
264 * } else {
265 * ++rs_sl_idx;
268 * log_info(LD_GENERAL,"No match for %s", ri->nickname);
269 * continue;
270 * matched_ri: {
271 * log_info(LD_GENERAL,"%s matches with routerstatus %p",ri->nickname,rs);
275 #define SMARTLIST_FOREACH_JOIN(sl1, type1, var1, sl2, type2, var2, \
276 cmpexpr, unmatched_var2) \
277 STMT_BEGIN \
278 int var1 ## _sl_idx = 0, var1 ## _sl_len=(sl1)->num_used; \
279 int var2 ## _sl_idx = 0, var2 ## _sl_len=(sl2)->num_used; \
280 int var1 ## _ ## var2 ## _cmp; \
281 type1 var1; \
282 type2 var2; \
283 for (; var2##_sl_idx < var2##_sl_len; ++var2##_sl_idx) { \
284 var2 = (sl2)->list[var2##_sl_idx]; \
285 while (var1##_sl_idx < var1##_sl_len) { \
286 var1 = (sl1)->list[var1##_sl_idx]; \
287 var1##_##var2##_cmp = (cmpexpr); \
288 if (var1##_##var2##_cmp > 0) { \
289 break; \
290 } else if (var1##_##var2##_cmp == 0) { \
291 goto matched_##var2; \
292 } else { \
293 ++var1##_sl_idx; \
296 /* Ran out of v1, or no match for var2. */ \
297 unmatched_var2; \
298 continue; \
299 matched_##var2: ; \
301 #define SMARTLIST_FOREACH_JOIN_END(var1, var2) \
303 STMT_END
305 #define DECLARE_MAP_FNS(maptype, keytype, prefix) \
306 typedef struct maptype maptype; \
307 typedef struct prefix##entry_t *prefix##iter_t; \
308 maptype* prefix##new(void); \
309 void* prefix##set(maptype *map, keytype key, void *val); \
310 void* prefix##get(const maptype *map, keytype key); \
311 void* prefix##remove(maptype *map, keytype key); \
312 void prefix##free(maptype *map, void (*free_val)(void*)); \
313 int prefix##isempty(const maptype *map); \
314 int prefix##size(const maptype *map); \
315 prefix##iter_t *prefix##iter_init(maptype *map); \
316 prefix##iter_t *prefix##iter_next(maptype *map, prefix##iter_t *iter); \
317 prefix##iter_t *prefix##iter_next_rmv(maptype *map, prefix##iter_t *iter); \
318 void prefix##iter_get(prefix##iter_t *iter, keytype *keyp, void **valp); \
319 int prefix##iter_done(prefix##iter_t *iter); \
320 void prefix##assert_ok(const maptype *map)
322 /* Map from const char * to void *. Implemented with a hash table. */
323 DECLARE_MAP_FNS(strmap_t, const char *, strmap_);
324 /* Map from const char[DIGEST_LEN] to void *. Implemented with a hash table. */
325 DECLARE_MAP_FNS(digestmap_t, const char *, digestmap_);
327 #undef DECLARE_MAP_FNS
329 /** Iterates over the key-value pairs in a map <b>map</b> in order.
330 * <b>prefix</b> is as for DECLARE_MAP_FNS (i.e., strmap_ or digestmap_).
331 * The map's keys and values are of type keytype and valtype respectively;
332 * each iteration assigns them to keyvar and valvar.
334 * Example use:
335 * MAP_FOREACH(digestmap_, m, const char *, k, routerinfo_t *, r) {
336 * // use k and r
337 * } MAP_FOREACH_END.
339 /* Unpacks to, approximately:
341 * digestmap_iter_t *k_iter;
342 * for (k_iter = digestmap_iter_init(m); !digestmap_iter_done(k_iter);
343 * k_iter = digestmap_iter_next(m, k_iter)) {
344 * const char *k;
345 * void *r_voidp;
346 * routerinfo_t *r;
347 * digestmap_iter_get(k_iter, &k, &r_voidp);
348 * r = r_voidp;
349 * // use k and r
353 #define MAP_FOREACH(prefix, map, keytype, keyvar, valtype, valvar) \
354 STMT_BEGIN \
355 prefix##iter_t *keyvar##_iter; \
356 for (keyvar##_iter = prefix##iter_init(map); \
357 !prefix##iter_done(keyvar##_iter); \
358 keyvar##_iter = prefix##iter_next(map, keyvar##_iter)) { \
359 keytype keyvar; \
360 void *valvar##_voidp; \
361 valtype valvar; \
362 prefix##iter_get(keyvar##_iter, &keyvar, &valvar##_voidp); \
363 valvar = valvar##_voidp;
365 /** As MAP_FOREACH, except allows members to be removed from the map
366 * during the iteration via MAP_DEL_CURRENT. Example use:
368 * Example use:
369 * MAP_FOREACH(digestmap_, m, const char *, k, routerinfo_t *, r) {
370 * if (is_very_old(r))
371 * MAP_DEL_CURRENT(k);
372 * } MAP_FOREACH_END.
374 /* Unpacks to, approximately:
376 * digestmap_iter_t *k_iter;
377 * int k_del=0;
378 * for (k_iter = digestmap_iter_init(m); !digestmap_iter_done(k_iter);
379 * k_iter = k_del ? digestmap_iter_next(m, k_iter)
380 * : digestmap_iter_next_rmv(m, k_iter)) {
381 * const char *k;
382 * void *r_voidp;
383 * routerinfo_t *r;
384 * k_del=0;
385 * digestmap_iter_get(k_iter, &k, &r_voidp);
386 * r = r_voidp;
387 * if (is_very_old(r)) {
388 * k_del = 1;
393 #define MAP_FOREACH_MODIFY(prefix, map, keytype, keyvar, valtype, valvar) \
394 STMT_BEGIN \
395 prefix##iter_t *keyvar##_iter; \
396 int keyvar##_del=0; \
397 for (keyvar##_iter = prefix##iter_init(map); \
398 !prefix##iter_done(keyvar##_iter); \
399 keyvar##_iter = keyvar##_del ? \
400 prefix##iter_next_rmv(map, keyvar##_iter) : \
401 prefix##iter_next(map, keyvar##_iter)) { \
402 keytype keyvar; \
403 void *valvar##_voidp; \
404 valtype valvar; \
405 keyvar##_del=0; \
406 prefix##iter_get(keyvar##_iter, &keyvar, &valvar##_voidp); \
407 valvar = valvar##_voidp;
409 /** Used with MAP_FOREACH_MODIFY to remove the currently-iterated-upon
410 * member of the map. */
411 #define MAP_DEL_CURRENT(keyvar) \
412 STMT_BEGIN \
413 keyvar##_del = 1; \
414 STMT_END
416 /** Used to end a MAP_FOREACH() block. */
417 #define MAP_FOREACH_END } STMT_END ;
419 /** As MAP_FOREACH, but does not require declaration of prefix or keytype.
420 * Example use:
421 * DIGESTMAP_FOREACH(m, k, routerinfo_t *, r) {
422 * // use k and r
423 * } DIGESTMAP_FOREACH_END.
425 #define DIGESTMAP_FOREACH(map, keyvar, valtype, valvar) \
426 MAP_FOREACH(digestmap_, map, const char *, keyvar, valtype, valvar)
428 /** As MAP_FOREACH_MODIFY, but does not require declaration of prefix or
429 * keytype.
430 * Example use:
431 * DIGESTMAP_FOREACH_MODIFY(m, k, routerinfo_t *, r) {
432 * if (is_very_old(r))
433 * MAP_DEL_CURRENT(k);
434 * } DIGESTMAP_FOREACH_END.
436 #define DIGESTMAP_FOREACH_MODIFY(map, keyvar, valtype, valvar) \
437 MAP_FOREACH_MODIFY(digestmap_, map, const char *, keyvar, valtype, valvar)
438 /** Used to end a DIGESTMAP_FOREACH() block. */
439 #define DIGESTMAP_FOREACH_END MAP_FOREACH_END
441 #define STRMAP_FOREACH(map, keyvar, valtype, valvar) \
442 MAP_FOREACH(strmap_, map, const char *, keyvar, valtype, valvar)
443 #define STRMAP_FOREACH_MODIFY(map, keyvar, valtype, valvar) \
444 MAP_FOREACH_MODIFY(strmap_, map, const char *, keyvar, valtype, valvar)
445 #define STRMAP_FOREACH_END MAP_FOREACH_END
447 void* strmap_set_lc(strmap_t *map, const char *key, void *val);
448 void* strmap_get_lc(const strmap_t *map, const char *key);
449 void* strmap_remove_lc(strmap_t *map, const char *key);
451 #define DECLARE_TYPED_DIGESTMAP_FNS(prefix, maptype, valtype) \
452 typedef struct maptype maptype; \
453 typedef struct prefix##iter_t prefix##iter_t; \
454 static INLINE maptype* prefix##new(void) \
456 return (maptype*)digestmap_new(); \
458 static INLINE digestmap_t* prefix##to_digestmap(maptype *map) \
460 return (digestmap_t*)map; \
462 static INLINE valtype* prefix##get(maptype *map, const char *key) \
464 return (valtype*)digestmap_get((digestmap_t*)map, key); \
466 static INLINE valtype* prefix##set(maptype *map, const char *key, \
467 valtype *val) \
469 return (valtype*)digestmap_set((digestmap_t*)map, key, val); \
471 static INLINE valtype* prefix##remove(maptype *map, const char *key) \
473 return (valtype*)digestmap_remove((digestmap_t*)map, key); \
475 static INLINE void prefix##free(maptype *map, void (*free_val)(void*)) \
477 digestmap_free((digestmap_t*)map, free_val); \
479 static INLINE int prefix##isempty(maptype *map) \
481 return digestmap_isempty((digestmap_t*)map); \
483 static INLINE int prefix##size(maptype *map) \
485 return digestmap_size((digestmap_t*)map); \
487 static INLINE prefix##iter_t *prefix##iter_init(maptype *map) \
489 return (prefix##iter_t*) digestmap_iter_init((digestmap_t*)map); \
491 static INLINE prefix##iter_t *prefix##iter_next(maptype *map, \
492 prefix##iter_t *iter) \
494 return (prefix##iter_t*) digestmap_iter_next( \
495 (digestmap_t*)map, (digestmap_iter_t*)iter); \
497 static INLINE prefix##iter_t *prefix##iter_next_rmv(maptype *map, \
498 prefix##iter_t *iter) \
500 return (prefix##iter_t*) digestmap_iter_next_rmv( \
501 (digestmap_t*)map, (digestmap_iter_t*)iter); \
503 static INLINE void prefix##iter_get(prefix##iter_t *iter, \
504 const char **keyp, \
505 valtype **valp) \
507 void *v; \
508 digestmap_iter_get((digestmap_iter_t*) iter, keyp, &v); \
509 *valp = v; \
511 static INLINE int prefix##iter_done(prefix##iter_t *iter) \
513 return digestmap_iter_done((digestmap_iter_t*)iter); \
516 #if SIZEOF_INT == 4
517 #define BITARRAY_SHIFT 5
518 #elif SIZEOF_INT == 8
519 #define BITARRAY_SHIFT 6
520 #else
521 #error "int is neither 4 nor 8 bytes. I can't deal with that."
522 #endif
523 #define BITARRAY_MASK ((1u<<BITARRAY_SHIFT)-1)
525 /** A random-access array of one-bit-wide elements. */
526 typedef unsigned int bitarray_t;
527 /** Create a new bit array that can hold <b>n_bits</b> bits. */
528 static INLINE bitarray_t *
529 bitarray_init_zero(unsigned int n_bits)
531 /* round up to the next int. */
532 size_t sz = (n_bits+BITARRAY_MASK) >> BITARRAY_SHIFT;
533 return tor_malloc_zero(sz*sizeof(unsigned int));
535 /** Expand <b>ba</b> from holding <b>n_bits_old</b> to <b>n_bits_new</b>,
536 * clearing all new bits. Returns a possibly changed pointer to the
537 * bitarray. */
538 static INLINE bitarray_t *
539 bitarray_expand(bitarray_t *ba,
540 unsigned int n_bits_old, unsigned int n_bits_new)
542 size_t sz_old = (n_bits_old+BITARRAY_MASK) >> BITARRAY_SHIFT;
543 size_t sz_new = (n_bits_new+BITARRAY_MASK) >> BITARRAY_SHIFT;
544 char *ptr;
545 if (sz_new <= sz_old)
546 return ba;
547 ptr = tor_realloc(ba, sz_new*sizeof(unsigned int));
548 /* This memset does nothing to the older excess bytes. But they were
549 * already set to 0 by bitarry_init_zero. */
550 memset(ptr+sz_old*sizeof(unsigned int), 0,
551 (sz_new-sz_old)*sizeof(unsigned int));
552 return (bitarray_t*) ptr;
554 /** Free the bit array <b>ba</b>. */
555 static INLINE void
556 bitarray_free(bitarray_t *ba)
558 tor_free(ba);
560 /** Set the <b>bit</b>th bit in <b>b</b> to 1. */
561 static INLINE void
562 bitarray_set(bitarray_t *b, int bit)
564 b[bit >> BITARRAY_SHIFT] |= (1u << (bit & BITARRAY_MASK));
566 /** Set the <b>bit</b>th bit in <b>b</b> to 0. */
567 static INLINE void
568 bitarray_clear(bitarray_t *b, int bit)
570 b[bit >> BITARRAY_SHIFT] &= ~ (1u << (bit & BITARRAY_MASK));
572 /** Return true iff <b>bit</b>th bit in <b>b</b> is nonzero. NOTE: does
573 * not necessarily return 1 on true. */
574 static INLINE unsigned int
575 bitarray_is_set(bitarray_t *b, int bit)
577 return b[bit >> BITARRAY_SHIFT] & (1u << (bit & BITARRAY_MASK));
580 /** A set of digests, implemented as a Bloom filter. */
581 typedef struct {
582 int mask; /* One less than the number of bits in <b>ba</b>; always one less
583 * than a power of two. */
584 bitarray_t *ba; /* A bit array to implement the Bloom filter. */
585 } digestset_t;
587 #define BIT(n) ((n) & set->mask)
588 /** Add the digest <b>digest</b> to <b>set</b>. */
589 static INLINE void
590 digestset_add(digestset_t *set, const char *digest)
592 const uint32_t *p = (const uint32_t *)digest;
593 const uint32_t d1 = p[0] + (p[1]>>16);
594 const uint32_t d2 = p[1] + (p[2]>>16);
595 const uint32_t d3 = p[2] + (p[3]>>16);
596 const uint32_t d4 = p[3] + (p[0]>>16);
597 bitarray_set(set->ba, BIT(d1));
598 bitarray_set(set->ba, BIT(d2));
599 bitarray_set(set->ba, BIT(d3));
600 bitarray_set(set->ba, BIT(d4));
603 /** If <b>digest</b> is in <b>set</b>, return nonzero. Otherwise,
604 * <em>probably</em> return zero. */
605 static INLINE int
606 digestset_isin(const 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 return bitarray_is_set(set->ba, BIT(d1)) &&
614 bitarray_is_set(set->ba, BIT(d2)) &&
615 bitarray_is_set(set->ba, BIT(d3)) &&
616 bitarray_is_set(set->ba, BIT(d4));
618 #undef BIT
620 digestset_t *digestset_new(int max_elements);
621 void digestset_free(digestset_t* set);
623 /* These functions, given an <b>array</b> of <b>n_elements</b>, return the
624 * <b>nth</b> lowest element. <b>nth</b>=0 gives the lowest element;
625 * <b>n_elements</b>-1 gives the highest; and (<b>n_elements</b>-1) / 2 gives
626 * the median. As a side effect, the elements of <b>array</b> are sorted. */
627 int find_nth_int(int *array, int n_elements, int nth);
628 time_t find_nth_time(time_t *array, int n_elements, int nth);
629 double find_nth_double(double *array, int n_elements, int nth);
630 uint32_t find_nth_uint32(uint32_t *array, int n_elements, int nth);
631 long find_nth_long(long *array, int n_elements, int nth);
632 static INLINE int
633 median_int(int *array, int n_elements)
635 return find_nth_int(array, n_elements, (n_elements-1)/2);
637 static INLINE time_t
638 median_time(time_t *array, int n_elements)
640 return find_nth_time(array, n_elements, (n_elements-1)/2);
642 static INLINE double
643 median_double(double *array, int n_elements)
645 return find_nth_double(array, n_elements, (n_elements-1)/2);
647 static INLINE uint32_t
648 median_uint32(uint32_t *array, int n_elements)
650 return find_nth_uint32(array, n_elements, (n_elements-1)/2);
652 static INLINE long
653 median_long(long *array, int n_elements)
655 return find_nth_long(array, n_elements, (n_elements-1)/2);
658 #endif