2007-12-12 22:09:01 +01:00
|
|
|
/* Copyright (c) 2003-2004, Roger Dingledine
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2009-05-02 22:00:54 +02:00
|
|
|
* Copyright (c) 2007-2009, The Tor Project, Inc. */
|
2004-11-01 21:41:47 +01:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
2008-12-29 03:21:02 +01:00
|
|
|
#ifndef _TOR_CONTAINER_H
|
|
|
|
#define _TOR_CONTAINER_H
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2005-10-18 19:52:45 +02:00
|
|
|
#include "util.h"
|
2004-11-12 21:41:52 +01:00
|
|
|
|
2007-02-02 21:06:43 +01:00
|
|
|
/** A resizeable list of pointers, with associated helpful functionality.
|
|
|
|
*
|
|
|
|
* The members of this struct are exposed only so that macros and inlines can
|
2009-05-27 23:55:51 +02:00
|
|
|
* use them; all access to smartlist internals should go through the functions
|
2007-02-02 21:06:43 +01:00
|
|
|
* and macros defined here.
|
|
|
|
**/
|
2005-10-18 06:51:07 +02:00
|
|
|
typedef struct smartlist_t {
|
2004-11-12 21:41:52 +01:00
|
|
|
/** <b>list</b> has enough capacity to store exactly <b>capacity</b> elements
|
|
|
|
* before it needs to be resized. Only the first <b>num_used</b> (\<=
|
|
|
|
* capacity) elements point to valid data.
|
|
|
|
*/
|
|
|
|
void **list;
|
|
|
|
int num_used;
|
|
|
|
int capacity;
|
2005-10-18 06:51:07 +02:00
|
|
|
} smartlist_t;
|
2004-11-01 21:41:47 +01:00
|
|
|
|
|
|
|
smartlist_t *smartlist_create(void);
|
|
|
|
void smartlist_free(smartlist_t *sl);
|
|
|
|
void smartlist_clear(smartlist_t *sl);
|
|
|
|
void smartlist_add(smartlist_t *sl, void *element);
|
|
|
|
void smartlist_add_all(smartlist_t *sl, const smartlist_t *s2);
|
2005-12-03 03:00:51 +01:00
|
|
|
void smartlist_remove(smartlist_t *sl, const void *element);
|
2006-06-18 09:21:35 +02:00
|
|
|
void *smartlist_pop_last(smartlist_t *sl);
|
|
|
|
void smartlist_reverse(smartlist_t *sl);
|
2005-04-03 07:52:32 +02:00
|
|
|
void smartlist_string_remove(smartlist_t *sl, const char *element);
|
2006-08-11 09:09:17 +02:00
|
|
|
int smartlist_isin(const smartlist_t *sl, const void *element) ATTR_PURE;
|
2006-08-11 09:17:16 +02:00
|
|
|
int smartlist_string_isin(const smartlist_t *sl, const char *element)
|
|
|
|
ATTR_PURE;
|
2007-06-04 21:19:01 +02:00
|
|
|
int smartlist_string_pos(const smartlist_t *, const char *elt) ATTR_PURE;
|
2006-12-28 22:29:03 +01:00
|
|
|
int smartlist_string_isin_case(const smartlist_t *sl, const char *element)
|
|
|
|
ATTR_PURE;
|
2006-08-11 09:09:17 +02:00
|
|
|
int smartlist_string_num_isin(const smartlist_t *sl, int num) ATTR_PURE;
|
2007-02-13 02:27:55 +01:00
|
|
|
int smartlist_digest_isin(const smartlist_t *sl, const char *element)
|
|
|
|
ATTR_PURE;
|
2006-08-11 09:17:16 +02:00
|
|
|
int smartlist_overlap(const smartlist_t *sl1, const smartlist_t *sl2)
|
|
|
|
ATTR_PURE;
|
2004-11-01 21:41:47 +01:00
|
|
|
void smartlist_intersect(smartlist_t *sl1, const smartlist_t *sl2);
|
|
|
|
void smartlist_subtract(smartlist_t *sl1, const smartlist_t *sl2);
|
2006-07-31 19:59:37 +02:00
|
|
|
|
2004-11-02 03:28:51 +01:00
|
|
|
/* smartlist_choose() is defined in crypto.[ch] */
|
2005-10-18 07:03:44 +02:00
|
|
|
#ifdef DEBUG_SMARTLIST
|
2005-10-18 06:51:07 +02:00
|
|
|
/** Return the number of items in sl.
|
|
|
|
*/
|
2007-07-02 19:50:57 +02:00
|
|
|
static INLINE int smartlist_len(const smartlist_t *sl) ATTR_PURE;
|
|
|
|
static INLINE int smartlist_len(const smartlist_t *sl) {
|
2005-10-18 06:51:07 +02:00
|
|
|
tor_assert(sl);
|
|
|
|
return (sl)->num_used;
|
|
|
|
}
|
|
|
|
/** Return the <b>idx</b>th element of sl.
|
|
|
|
*/
|
2007-07-02 19:50:57 +02:00
|
|
|
static INLINE void *smartlist_get(const smartlist_t *sl, int idx) ATTR_PURE;
|
|
|
|
static INLINE void *smartlist_get(const smartlist_t *sl, int idx) {
|
2005-10-18 06:51:07 +02:00
|
|
|
tor_assert(sl);
|
|
|
|
tor_assert(idx>=0);
|
2007-07-02 19:48:56 +02:00
|
|
|
tor_assert(sl->num_used > idx);
|
2005-10-18 06:51:07 +02:00
|
|
|
return sl->list[idx];
|
|
|
|
}
|
2007-07-02 19:50:57 +02:00
|
|
|
static INLINE void smartlist_set(smartlist_t *sl, int idx, void *val) {
|
2005-10-18 06:51:07 +02:00
|
|
|
tor_assert(sl);
|
|
|
|
tor_assert(idx>=0);
|
2007-07-02 19:48:56 +02:00
|
|
|
tor_assert(sl->num_used > idx);
|
2005-10-18 06:51:07 +02:00
|
|
|
sl->list[idx] = val;
|
|
|
|
}
|
2005-10-18 07:03:44 +02:00
|
|
|
#else
|
|
|
|
#define smartlist_len(sl) ((sl)->num_used)
|
|
|
|
#define smartlist_get(sl, idx) ((sl)->list[idx])
|
|
|
|
#define smartlist_set(sl, idx, val) ((sl)->list[idx] = (val))
|
|
|
|
#endif
|
|
|
|
|
2007-05-29 19:31:13 +02:00
|
|
|
/** Exchange the elements at indices <b>idx1</b> and <b>idx2</b> of the
|
|
|
|
* smartlist <b>sl</b>. */
|
2007-05-19 22:17:37 +02:00
|
|
|
static INLINE void smartlist_swap(smartlist_t *sl, int idx1, int idx2)
|
2007-05-18 23:19:14 +02:00
|
|
|
{
|
|
|
|
if (idx1 != idx2) {
|
|
|
|
void *elt = smartlist_get(sl, idx1);
|
|
|
|
smartlist_set(sl, idx1, smartlist_get(sl, idx2));
|
|
|
|
smartlist_set(sl, idx2, elt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-12 22:14:06 +01:00
|
|
|
void smartlist_del(smartlist_t *sl, int idx);
|
|
|
|
void smartlist_del_keeporder(smartlist_t *sl, int idx);
|
2004-11-01 21:41:47 +01:00
|
|
|
void smartlist_insert(smartlist_t *sl, int idx, void *val);
|
2005-09-10 01:12:54 +02:00
|
|
|
void smartlist_sort(smartlist_t *sl,
|
|
|
|
int (*compare)(const void **a, const void **b));
|
2006-09-15 06:27:58 +02:00
|
|
|
void smartlist_uniq(smartlist_t *sl,
|
|
|
|
int (*compare)(const void **a, const void **b),
|
|
|
|
void (*free_fn)(void *elt));
|
2005-09-12 10:27:01 +02:00
|
|
|
void smartlist_sort_strings(smartlist_t *sl);
|
2006-06-21 06:57:12 +02:00
|
|
|
void smartlist_sort_digests(smartlist_t *sl);
|
2006-09-15 06:27:58 +02:00
|
|
|
void smartlist_uniq_strings(smartlist_t *sl);
|
|
|
|
void smartlist_uniq_digests(smartlist_t *sl);
|
2005-09-10 01:12:54 +02:00
|
|
|
void *smartlist_bsearch(smartlist_t *sl, const void *key,
|
2006-08-11 09:09:17 +02:00
|
|
|
int (*compare)(const void *key, const void **member))
|
2008-11-26 17:57:46 +01:00
|
|
|
ATTR_PURE;
|
2007-11-03 21:12:38 +01:00
|
|
|
int smartlist_bsearch_idx(const smartlist_t *sl, const void *key,
|
|
|
|
int (*compare)(const void *key, const void **member),
|
2008-11-26 17:57:46 +01:00
|
|
|
int *found_out);
|
2004-11-12 21:41:52 +01:00
|
|
|
|
2006-07-31 19:59:37 +02:00
|
|
|
void smartlist_pqueue_add(smartlist_t *sl,
|
|
|
|
int (*compare)(const void *a, const void *b),
|
|
|
|
void *item);
|
|
|
|
void *smartlist_pqueue_pop(smartlist_t *sl,
|
|
|
|
int (*compare)(const void *a, const void *b));
|
|
|
|
void smartlist_pqueue_assert_ok(smartlist_t *sl,
|
|
|
|
int (*compare)(const void *a, const void *b));
|
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
#define SPLIT_SKIP_SPACE 0x01
|
|
|
|
#define SPLIT_IGNORE_BLANK 0x02
|
2008-10-01 05:41:33 +02:00
|
|
|
#define SPLIT_STRIP_SPACE 0x04
|
2004-11-01 21:41:47 +01:00
|
|
|
int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
|
|
|
|
int flags, int max);
|
2004-11-03 19:28:00 +01:00
|
|
|
char *smartlist_join_strings(smartlist_t *sl, const char *join, int terminate,
|
2006-08-11 09:09:17 +02:00
|
|
|
size_t *len_out) ATTR_MALLOC;
|
2004-11-03 19:28:00 +01:00
|
|
|
char *smartlist_join_strings2(smartlist_t *sl, const char *join,
|
2006-08-11 09:09:17 +02:00
|
|
|
size_t join_len, int terminate, size_t *len_out)
|
|
|
|
ATTR_MALLOC;
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2005-09-12 01:14:14 +02:00
|
|
|
/** Iterate over the items in a smartlist <b>sl</b>, in order. For each item,
|
|
|
|
* assign it to a new local variable of type <b>type</b> named <b>var</b>, and
|
|
|
|
* execute the statement <b>cmd</b>. Inside the loop, the loop index can
|
2007-02-05 19:33:52 +01:00
|
|
|
* be accessed as <b>var</b>_sl_idx and the length of the list can be accessed
|
|
|
|
* as <b>var</b>_sl_len.
|
|
|
|
*
|
|
|
|
* NOTE: Do not change the length of the list while the loop is in progress,
|
|
|
|
* unless you adjust the _sl_len variable correspondingly. See second example
|
|
|
|
* below.
|
2005-09-12 01:14:14 +02:00
|
|
|
*
|
|
|
|
* Example use:
|
|
|
|
* <pre>
|
|
|
|
* smartlist_t *list = smartlist_split("A:B:C", ":", 0, 0);
|
|
|
|
* SMARTLIST_FOREACH(list, char *, cp,
|
|
|
|
* {
|
|
|
|
* printf("%d: %s\n", cp_sl_idx, cp);
|
|
|
|
* tor_free(cp);
|
|
|
|
* });
|
2007-02-05 19:33:52 +01:00
|
|
|
* smartlist_free(list);
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* Example use (advanced):
|
|
|
|
* <pre>
|
|
|
|
* SMARTLIST_FOREACH(list, char *, cp,
|
|
|
|
* {
|
|
|
|
* if (!strcmp(cp, "junk")) {
|
|
|
|
* tor_free(cp);
|
2008-04-04 23:18:51 +02:00
|
|
|
* SMARTLIST_DEL_CURRENT(list, cp);
|
2007-02-05 19:33:52 +01:00
|
|
|
* }
|
|
|
|
* });
|
2005-09-12 01:14:14 +02:00
|
|
|
* </pre>
|
|
|
|
*/
|
2008-04-04 23:18:51 +02:00
|
|
|
/* Note: these macros use token pasting, and reach into smartlist internals.
|
|
|
|
* This can make them a little daunting. Here's the approximate unpacking of
|
|
|
|
* the above examples, for entertainment value:
|
|
|
|
*
|
|
|
|
* <pre>
|
|
|
|
* smartlist_t *list = smartlist_split("A:B:C", ":", 0, 0);
|
|
|
|
* {
|
|
|
|
* int cp_sl_idx, cp_sl_len = smartlist_len(list);
|
|
|
|
* char *cp;
|
|
|
|
* for (cp_sl_idx = 0; cp_sl_idx < cp_sl_len; ++cp_sl_idx) {
|
|
|
|
* cp = smartlist_get(list, cp_sl_idx);
|
|
|
|
* printf("%d: %s\n", cp_sl_idx, cp);
|
|
|
|
* tor_free(cp);
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* smartlist_free(list);
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* <pre>
|
|
|
|
* {
|
|
|
|
* int cp_sl_idx, cp_sl_len = smartlist_len(list);
|
|
|
|
* char *cp;
|
|
|
|
* for (cp_sl_idx = 0; cp_sl_idx < cp_sl_len; ++cp_sl_idx) {
|
|
|
|
* cp = smartlist_get(list, cp_sl_idx);
|
|
|
|
* if (!strcmp(cp, "junk")) {
|
|
|
|
* tor_free(cp);
|
|
|
|
* smartlist_del(list, cp_sl_idx);
|
|
|
|
* --cp_sl_idx;
|
|
|
|
* --cp_sl_len;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* </pre>
|
|
|
|
*/
|
2008-07-30 15:04:28 +02:00
|
|
|
#define SMARTLIST_FOREACH_BEGIN(sl, type, var) \
|
2007-06-17 20:22:39 +02:00
|
|
|
STMT_BEGIN \
|
2005-10-18 19:38:07 +02:00
|
|
|
int var ## _sl_idx, var ## _sl_len=(sl)->num_used; \
|
2004-11-01 21:41:47 +01:00
|
|
|
type var; \
|
2004-11-28 10:05:49 +01:00
|
|
|
for (var ## _sl_idx = 0; var ## _sl_idx < var ## _sl_len; \
|
2004-11-28 12:39:53 +01:00
|
|
|
++var ## _sl_idx) { \
|
2008-07-30 15:04:28 +02:00
|
|
|
var = (sl)->list[var ## _sl_idx];
|
|
|
|
|
|
|
|
#define SMARTLIST_FOREACH_END(var) \
|
2008-09-09 21:29:33 +02:00
|
|
|
var = NULL; \
|
2008-07-30 15:04:28 +02:00
|
|
|
} STMT_END
|
|
|
|
|
|
|
|
#define SMARTLIST_FOREACH(sl, type, var, cmd) \
|
|
|
|
SMARTLIST_FOREACH_BEGIN(sl,type,var) { \
|
|
|
|
cmd; \
|
|
|
|
} SMARTLIST_FOREACH_END(var)
|
2004-11-01 21:41:47 +01:00
|
|
|
|
2007-02-24 08:37:45 +01:00
|
|
|
/** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed
|
2007-02-26 06:36:02 +01:00
|
|
|
* with the variable <b>var</b>, remove the current element in a way that
|
2007-02-24 08:37:45 +01:00
|
|
|
* won't confuse the loop. */
|
|
|
|
#define SMARTLIST_DEL_CURRENT(sl, var) \
|
2007-06-17 20:22:39 +02:00
|
|
|
STMT_BEGIN \
|
2007-02-24 08:37:45 +01:00
|
|
|
smartlist_del(sl, var ## _sl_idx); \
|
|
|
|
--var ## _sl_idx; \
|
|
|
|
--var ## _sl_len; \
|
2007-06-17 20:22:39 +02:00
|
|
|
STMT_END
|
2007-02-24 08:37:45 +01:00
|
|
|
|
2008-09-09 05:48:01 +02:00
|
|
|
/** Helper: While in a SMARTLIST_FOREACH loop over the list <b>sl</b> indexed
|
|
|
|
* with the variable <b>var</b>, replace the current element with <b>val</b>.
|
|
|
|
* Does not deallocate the current value of <b>var</b>.
|
|
|
|
*/
|
|
|
|
#define SMARTLIST_REPLACE_CURRENT(sl, var, val) \
|
|
|
|
STMT_BEGIN \
|
|
|
|
smartlist_set(sl, var ## _sl_idx, val); \
|
|
|
|
STMT_END
|
|
|
|
|
2008-04-07 18:28:34 +02:00
|
|
|
/* Helper: Given two lists of items, possibly of different types, such that
|
2009-05-27 23:55:51 +02:00
|
|
|
* both lists are sorted on some common field (as determined by a comparison
|
2008-04-07 18:28:34 +02:00
|
|
|
* expression <b>cmpexpr</b>), and such that one list (<b>sl1</b>) has no
|
|
|
|
* duplicates on the common field, loop through the lists in lockstep, and
|
|
|
|
* execute <b>unmatched_var2</b> on items in var2 that do not appear in
|
|
|
|
* var1.
|
|
|
|
*
|
|
|
|
* WARNING: It isn't safe to add remove elements from either list while the
|
|
|
|
* loop is in progress.
|
|
|
|
*
|
|
|
|
* Example use:
|
|
|
|
* SMARTLIST_FOREACH_JOIN(routerstatus_list, routerstatus_t *, rs,
|
|
|
|
* routerinfo_list, routerinfo_t *, ri,
|
|
|
|
* memcmp(rs->identity_digest, ri->identity_digest, 20),
|
|
|
|
* log_info(LD_GENERAL,"No match for %s", ri->nickname)) {
|
|
|
|
* log_info(LD_GENERAL, "%s matches routerstatus %p", ri->nickname, rs);
|
|
|
|
* } SMARTLIST_FOREACH_JOIN_END(rs, ri);
|
|
|
|
**/
|
|
|
|
/* The example above unpacks (approximately) to:
|
|
|
|
* int rs_sl_idx = 0, rs_sl_len = smartlist_len(routerstatus_list);
|
|
|
|
* int ri_sl_idx, ri_sl_len = smartlist_len(routerinfo_list);
|
|
|
|
* int rs_ri_cmp;
|
|
|
|
* routerstatus_t *rs;
|
|
|
|
* routerinfo_t *ri;
|
|
|
|
* for (; ri_sl_idx < ri_sl_len; ++ri_sl_idx) {
|
|
|
|
* ri = smartlist_get(routerinfo_list, ri_sl_idx);
|
|
|
|
* while (rs_sl_idx < rs_sl_len) {
|
|
|
|
* rs = smartlist_get(routerstatus_list, rs_sl_idx);
|
|
|
|
* rs_ri_cmp = memcmp(rs->identity_digest, ri->identity_digest, 20);
|
|
|
|
* if (rs_ri_cmp > 0) {
|
|
|
|
* break;
|
|
|
|
* } else if (rs_ri_cmp == 0) {
|
|
|
|
* goto matched_ri;
|
|
|
|
* } else {
|
|
|
|
* ++rs_sl_idx;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* log_info(LD_GENERAL,"No match for %s", ri->nickname);
|
|
|
|
* continue;
|
|
|
|
* matched_ri: {
|
|
|
|
* log_info(LD_GENERAL,"%s matches with routerstatus %p",ri->nickname,rs);
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
#define SMARTLIST_FOREACH_JOIN(sl1, type1, var1, sl2, type2, var2, \
|
|
|
|
cmpexpr, unmatched_var2) \
|
|
|
|
STMT_BEGIN \
|
|
|
|
int var1 ## _sl_idx = 0, var1 ## _sl_len=(sl1)->num_used; \
|
|
|
|
int var2 ## _sl_idx = 0, var2 ## _sl_len=(sl2)->num_used; \
|
|
|
|
int var1 ## _ ## var2 ## _cmp; \
|
|
|
|
type1 var1; \
|
|
|
|
type2 var2; \
|
|
|
|
for (; var2##_sl_idx < var2##_sl_len; ++var2##_sl_idx) { \
|
|
|
|
var2 = (sl2)->list[var2##_sl_idx]; \
|
|
|
|
while (var1##_sl_idx < var1##_sl_len) { \
|
|
|
|
var1 = (sl1)->list[var1##_sl_idx]; \
|
|
|
|
var1##_##var2##_cmp = (cmpexpr); \
|
|
|
|
if (var1##_##var2##_cmp > 0) { \
|
|
|
|
break; \
|
|
|
|
} else if (var1##_##var2##_cmp == 0) { \
|
|
|
|
goto matched_##var2; \
|
|
|
|
} else { \
|
|
|
|
++var1##_sl_idx; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
/* Ran out of v1, or no match for var2. */ \
|
|
|
|
unmatched_var2; \
|
|
|
|
continue; \
|
|
|
|
matched_##var2: ; \
|
|
|
|
|
|
|
|
#define SMARTLIST_FOREACH_JOIN_END(var1, var2) \
|
|
|
|
} \
|
|
|
|
STMT_END
|
|
|
|
|
2005-10-18 22:11:39 +02:00
|
|
|
#define DECLARE_MAP_FNS(maptype, keytype, prefix) \
|
|
|
|
typedef struct maptype maptype; \
|
2005-11-23 05:18:45 +01:00
|
|
|
typedef struct prefix##entry_t *prefix##iter_t; \
|
2005-10-18 22:11:39 +02:00
|
|
|
maptype* prefix##new(void); \
|
|
|
|
void* prefix##set(maptype *map, keytype key, void *val); \
|
2007-05-18 23:19:14 +02:00
|
|
|
void* prefix##get(const maptype *map, keytype key); \
|
2005-10-18 22:11:39 +02:00
|
|
|
void* prefix##remove(maptype *map, keytype key); \
|
|
|
|
void prefix##free(maptype *map, void (*free_val)(void*)); \
|
2007-05-18 23:19:14 +02:00
|
|
|
int prefix##isempty(const maptype *map); \
|
|
|
|
int prefix##size(const maptype *map); \
|
2005-10-18 22:11:39 +02:00
|
|
|
prefix##iter_t *prefix##iter_init(maptype *map); \
|
|
|
|
prefix##iter_t *prefix##iter_next(maptype *map, prefix##iter_t *iter); \
|
|
|
|
prefix##iter_t *prefix##iter_next_rmv(maptype *map, prefix##iter_t *iter); \
|
|
|
|
void prefix##iter_get(prefix##iter_t *iter, keytype *keyp, void **valp); \
|
2006-07-31 20:01:27 +02:00
|
|
|
int prefix##iter_done(prefix##iter_t *iter); \
|
2007-06-17 20:22:35 +02:00
|
|
|
void prefix##assert_ok(const maptype *map)
|
2005-10-18 22:11:39 +02:00
|
|
|
|
2005-12-03 03:00:51 +01:00
|
|
|
/* Map from const char * to void *. Implemented with a hash table. */
|
2005-10-18 22:11:39 +02:00
|
|
|
DECLARE_MAP_FNS(strmap_t, const char *, strmap_);
|
2008-01-06 04:16:06 +01:00
|
|
|
/* Map from const char[DIGEST_LEN] to void *. Implemented with a hash table. */
|
2005-10-18 22:11:39 +02:00
|
|
|
DECLARE_MAP_FNS(digestmap_t, const char *, digestmap_);
|
2008-01-06 04:16:06 +01:00
|
|
|
|
2007-06-12 00:19:37 +02:00
|
|
|
#undef DECLARE_MAP_FNS
|
2005-10-18 22:11:39 +02:00
|
|
|
|
2008-02-21 10:01:32 +01:00
|
|
|
/** Iterates over the key-value pairs in a map <b>map</b> in order.
|
2008-02-21 04:38:44 +01:00
|
|
|
* <b>prefix</b> is as for DECLARE_MAP_FNS (i.e., strmap_ or digestmap_).
|
|
|
|
* The map's keys and values are of type keytype and valtype respectively;
|
|
|
|
* each iteration assigns them to keyvar and valvar.
|
|
|
|
*
|
|
|
|
* Example use:
|
|
|
|
* MAP_FOREACH(digestmap_, m, const char *, k, routerinfo_t *, r) {
|
|
|
|
* // use k and r
|
|
|
|
* } MAP_FOREACH_END.
|
|
|
|
*/
|
2008-04-04 23:18:51 +02:00
|
|
|
/* Unpacks to, approximately:
|
|
|
|
* {
|
|
|
|
* digestmap_iter_t *k_iter;
|
|
|
|
* for (k_iter = digestmap_iter_init(m); !digestmap_iter_done(k_iter);
|
|
|
|
* k_iter = digestmap_iter_next(m, k_iter)) {
|
|
|
|
* const char *k;
|
|
|
|
* void *r_voidp;
|
|
|
|
* routerinfo_t *r;
|
|
|
|
* digestmap_iter_get(k_iter, &k, &r_voidp);
|
|
|
|
* r = r_voidp;
|
|
|
|
* // use k and r
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*/
|
2008-01-26 23:49:36 +01:00
|
|
|
#define MAP_FOREACH(prefix, map, keytype, keyvar, valtype, valvar) \
|
|
|
|
STMT_BEGIN \
|
2008-02-21 04:38:44 +01:00
|
|
|
prefix##iter_t *keyvar##_iter; \
|
|
|
|
for (keyvar##_iter = prefix##iter_init(map); \
|
|
|
|
!prefix##iter_done(keyvar##_iter); \
|
|
|
|
keyvar##_iter = prefix##iter_next(map, keyvar##_iter)) { \
|
2008-01-26 23:49:36 +01:00
|
|
|
keytype keyvar; \
|
|
|
|
void *valvar##_voidp; \
|
|
|
|
valtype valvar; \
|
2008-02-21 04:38:44 +01:00
|
|
|
prefix##iter_get(keyvar##_iter, &keyvar, &valvar##_voidp); \
|
2008-01-26 23:49:36 +01:00
|
|
|
valvar = valvar##_voidp;
|
|
|
|
|
2008-02-21 04:38:44 +01:00
|
|
|
/** As MAP_FOREACH, except allows members to be removed from the map
|
|
|
|
* during the iteration via MAP_DEL_CURRENT. Example use:
|
|
|
|
*
|
|
|
|
* Example use:
|
|
|
|
* MAP_FOREACH(digestmap_, m, const char *, k, routerinfo_t *, r) {
|
|
|
|
* if (is_very_old(r))
|
|
|
|
* MAP_DEL_CURRENT(k);
|
|
|
|
* } MAP_FOREACH_END.
|
|
|
|
**/
|
2008-04-04 23:18:51 +02:00
|
|
|
/* Unpacks to, approximately:
|
|
|
|
* {
|
|
|
|
* digestmap_iter_t *k_iter;
|
|
|
|
* int k_del=0;
|
|
|
|
* for (k_iter = digestmap_iter_init(m); !digestmap_iter_done(k_iter);
|
|
|
|
* k_iter = k_del ? digestmap_iter_next(m, k_iter)
|
|
|
|
* : digestmap_iter_next_rmv(m, k_iter)) {
|
|
|
|
* const char *k;
|
|
|
|
* void *r_voidp;
|
|
|
|
* routerinfo_t *r;
|
|
|
|
* k_del=0;
|
|
|
|
* digestmap_iter_get(k_iter, &k, &r_voidp);
|
|
|
|
* r = r_voidp;
|
|
|
|
* if (is_very_old(r)) {
|
|
|
|
* k_del = 1;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*/
|
2008-02-05 20:40:17 +01:00
|
|
|
#define MAP_FOREACH_MODIFY(prefix, map, keytype, keyvar, valtype, valvar) \
|
|
|
|
STMT_BEGIN \
|
2008-02-21 04:38:44 +01:00
|
|
|
prefix##iter_t *keyvar##_iter; \
|
2008-02-05 20:40:17 +01:00
|
|
|
int keyvar##_del=0; \
|
2008-02-21 04:38:44 +01:00
|
|
|
for (keyvar##_iter = prefix##iter_init(map); \
|
|
|
|
!prefix##iter_done(keyvar##_iter); \
|
|
|
|
keyvar##_iter = keyvar##_del ? \
|
|
|
|
prefix##iter_next_rmv(map, keyvar##_iter) : \
|
|
|
|
prefix##iter_next(map, keyvar##_iter)) { \
|
2008-02-05 20:40:17 +01:00
|
|
|
keytype keyvar; \
|
|
|
|
void *valvar##_voidp; \
|
|
|
|
valtype valvar; \
|
|
|
|
keyvar##_del=0; \
|
2008-02-21 04:38:44 +01:00
|
|
|
prefix##iter_get(keyvar##_iter, &keyvar, &valvar##_voidp); \
|
2008-02-05 20:40:17 +01:00
|
|
|
valvar = valvar##_voidp;
|
|
|
|
|
2008-02-21 04:38:44 +01:00
|
|
|
/** Used with MAP_FOREACH_MODIFY to remove the currently-iterated-upon
|
|
|
|
* member of the map. */
|
2008-02-05 20:40:17 +01:00
|
|
|
#define MAP_DEL_CURRENT(keyvar) \
|
|
|
|
STMT_BEGIN \
|
|
|
|
keyvar##_del = 1; \
|
|
|
|
STMT_END
|
|
|
|
|
2008-02-21 04:38:44 +01:00
|
|
|
/** Used to end a MAP_FOREACH() block. */
|
2008-01-26 23:49:36 +01:00
|
|
|
#define MAP_FOREACH_END } STMT_END ;
|
|
|
|
|
2008-02-21 04:38:44 +01:00
|
|
|
/** As MAP_FOREACH, but does not require declaration of prefix or keytype.
|
|
|
|
* Example use:
|
|
|
|
* DIGESTMAP_FOREACH(m, k, routerinfo_t *, r) {
|
|
|
|
* // use k and r
|
|
|
|
* } DIGESTMAP_FOREACH_END.
|
|
|
|
*/
|
2008-01-26 23:49:36 +01:00
|
|
|
#define DIGESTMAP_FOREACH(map, keyvar, valtype, valvar) \
|
|
|
|
MAP_FOREACH(digestmap_, map, const char *, keyvar, valtype, valvar)
|
2008-02-21 04:38:44 +01:00
|
|
|
|
|
|
|
/** As MAP_FOREACH_MODIFY, but does not require declaration of prefix or
|
|
|
|
* keytype.
|
|
|
|
* Example use:
|
|
|
|
* DIGESTMAP_FOREACH_MODIFY(m, k, routerinfo_t *, r) {
|
|
|
|
* if (is_very_old(r))
|
|
|
|
* MAP_DEL_CURRENT(k);
|
|
|
|
* } DIGESTMAP_FOREACH_END.
|
|
|
|
*/
|
2008-02-05 20:40:17 +01:00
|
|
|
#define DIGESTMAP_FOREACH_MODIFY(map, keyvar, valtype, valvar) \
|
|
|
|
MAP_FOREACH_MODIFY(digestmap_, map, const char *, keyvar, valtype, valvar)
|
2008-02-21 04:38:44 +01:00
|
|
|
/** Used to end a DIGESTMAP_FOREACH() block. */
|
2008-01-26 23:49:36 +01:00
|
|
|
#define DIGESTMAP_FOREACH_END MAP_FOREACH_END
|
|
|
|
|
2008-02-21 19:45:11 +01:00
|
|
|
#define STRMAP_FOREACH(map, keyvar, valtype, valvar) \
|
|
|
|
MAP_FOREACH(strmap_, map, const char *, keyvar, valtype, valvar)
|
|
|
|
#define STRMAP_FOREACH_MODIFY(map, keyvar, valtype, valvar) \
|
|
|
|
MAP_FOREACH_MODIFY(strmap_, map, const char *, keyvar, valtype, valvar)
|
|
|
|
#define STRMAP_FOREACH_END MAP_FOREACH_END
|
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
void* strmap_set_lc(strmap_t *map, const char *key, void *val);
|
2007-05-18 23:19:14 +02:00
|
|
|
void* strmap_get_lc(const strmap_t *map, const char *key);
|
2004-11-01 21:41:47 +01:00
|
|
|
void* strmap_remove_lc(strmap_t *map, const char *key);
|
|
|
|
|
2007-06-12 00:19:37 +02:00
|
|
|
#define DECLARE_TYPED_DIGESTMAP_FNS(prefix, maptype, valtype) \
|
|
|
|
typedef struct maptype maptype; \
|
|
|
|
typedef struct prefix##iter_t prefix##iter_t; \
|
|
|
|
static INLINE maptype* prefix##new(void) \
|
|
|
|
{ \
|
|
|
|
return (maptype*)digestmap_new(); \
|
|
|
|
} \
|
2008-02-06 06:31:15 +01:00
|
|
|
static INLINE digestmap_t* prefix##to_digestmap(maptype *map) \
|
|
|
|
{ \
|
|
|
|
return (digestmap_t*)map; \
|
|
|
|
} \
|
2007-06-12 00:19:37 +02:00
|
|
|
static INLINE valtype* prefix##get(maptype *map, const char *key) \
|
|
|
|
{ \
|
|
|
|
return (valtype*)digestmap_get((digestmap_t*)map, key); \
|
|
|
|
} \
|
|
|
|
static INLINE valtype* prefix##set(maptype *map, const char *key, \
|
|
|
|
valtype *val) \
|
|
|
|
{ \
|
|
|
|
return (valtype*)digestmap_set((digestmap_t*)map, key, val); \
|
|
|
|
} \
|
|
|
|
static INLINE valtype* prefix##remove(maptype *map, const char *key) \
|
|
|
|
{ \
|
2007-06-12 00:46:35 +02:00
|
|
|
return (valtype*)digestmap_remove((digestmap_t*)map, key); \
|
2007-06-12 00:19:37 +02:00
|
|
|
} \
|
|
|
|
static INLINE void prefix##free(maptype *map, void (*free_val)(void*)) \
|
|
|
|
{ \
|
|
|
|
digestmap_free((digestmap_t*)map, free_val); \
|
|
|
|
} \
|
|
|
|
static INLINE int prefix##isempty(maptype *map) \
|
|
|
|
{ \
|
|
|
|
return digestmap_isempty((digestmap_t*)map); \
|
|
|
|
} \
|
|
|
|
static INLINE int prefix##size(maptype *map) \
|
|
|
|
{ \
|
2007-10-21 01:48:35 +02:00
|
|
|
return digestmap_size((digestmap_t*)map); \
|
2007-06-12 00:19:37 +02:00
|
|
|
} \
|
|
|
|
static INLINE prefix##iter_t *prefix##iter_init(maptype *map) \
|
|
|
|
{ \
|
|
|
|
return (prefix##iter_t*) digestmap_iter_init((digestmap_t*)map); \
|
|
|
|
} \
|
|
|
|
static INLINE prefix##iter_t *prefix##iter_next(maptype *map, \
|
|
|
|
prefix##iter_t *iter) \
|
|
|
|
{ \
|
|
|
|
return (prefix##iter_t*) digestmap_iter_next( \
|
|
|
|
(digestmap_t*)map, (digestmap_iter_t*)iter); \
|
|
|
|
} \
|
|
|
|
static INLINE prefix##iter_t *prefix##iter_next_rmv(maptype *map, \
|
|
|
|
prefix##iter_t *iter) \
|
|
|
|
{ \
|
|
|
|
return (prefix##iter_t*) digestmap_iter_next_rmv( \
|
|
|
|
(digestmap_t*)map, (digestmap_iter_t*)iter); \
|
|
|
|
} \
|
|
|
|
static INLINE void prefix##iter_get(prefix##iter_t *iter, \
|
|
|
|
const char **keyp, \
|
|
|
|
valtype **valp) \
|
|
|
|
{ \
|
|
|
|
void *v; \
|
|
|
|
digestmap_iter_get((digestmap_iter_t*) iter, keyp, &v); \
|
|
|
|
*valp = v; \
|
|
|
|
} \
|
|
|
|
static INLINE int prefix##iter_done(prefix##iter_t *iter) \
|
|
|
|
{ \
|
|
|
|
return digestmap_iter_done((digestmap_iter_t*)iter); \
|
|
|
|
}
|
|
|
|
|
2007-07-26 23:26:53 +02:00
|
|
|
#if SIZEOF_INT == 4
|
|
|
|
#define BITARRAY_SHIFT 5
|
|
|
|
#elif SIZEOF_INT == 8
|
|
|
|
#define BITARRAY_SHIFT 6
|
|
|
|
#else
|
2007-07-27 16:41:08 +02:00
|
|
|
#error "int is neither 4 nor 8 bytes. I can't deal with that."
|
2007-07-26 23:26:53 +02:00
|
|
|
#endif
|
2007-08-18 00:43:00 +02:00
|
|
|
#define BITARRAY_MASK ((1u<<BITARRAY_SHIFT)-1)
|
2007-07-26 23:26:53 +02:00
|
|
|
|
2007-07-27 16:41:08 +02:00
|
|
|
/** A random-access array of one-bit-wide elements. */
|
2007-07-26 23:26:53 +02:00
|
|
|
typedef unsigned int bitarray_t;
|
|
|
|
/** Create a new bit array that can hold <b>n_bits</b> bits. */
|
|
|
|
static INLINE bitarray_t *
|
2008-02-20 17:57:41 +01:00
|
|
|
bitarray_init_zero(unsigned int n_bits)
|
2007-07-26 23:26:53 +02:00
|
|
|
{
|
2008-02-20 17:57:41 +01:00
|
|
|
/* round up to the next int. */
|
|
|
|
size_t sz = (n_bits+BITARRAY_MASK) >> BITARRAY_SHIFT;
|
2007-07-26 23:26:53 +02:00
|
|
|
return tor_malloc_zero(sz*sizeof(unsigned int));
|
|
|
|
}
|
2008-02-21 04:38:44 +01:00
|
|
|
/** Expand <b>ba</b> from holding <b>n_bits_old</b> to <b>n_bits_new</b>,
|
|
|
|
* clearing all new bits. Returns a possibly changed pointer to the
|
|
|
|
* bitarray. */
|
2008-02-19 23:46:19 +01:00
|
|
|
static INLINE bitarray_t *
|
2008-02-20 17:57:41 +01:00
|
|
|
bitarray_expand(bitarray_t *ba,
|
|
|
|
unsigned int n_bits_old, unsigned int n_bits_new)
|
2008-02-19 23:46:19 +01:00
|
|
|
{
|
2008-02-20 17:57:41 +01:00
|
|
|
size_t sz_old = (n_bits_old+BITARRAY_MASK) >> BITARRAY_SHIFT;
|
|
|
|
size_t sz_new = (n_bits_new+BITARRAY_MASK) >> BITARRAY_SHIFT;
|
2008-02-19 23:46:19 +01:00
|
|
|
char *ptr;
|
|
|
|
if (sz_new <= sz_old)
|
|
|
|
return ba;
|
2008-02-20 17:57:41 +01:00
|
|
|
ptr = tor_realloc(ba, sz_new*sizeof(unsigned int));
|
|
|
|
/* This memset does nothing to the older excess bytes. But they were
|
|
|
|
* already set to 0 by bitarry_init_zero. */
|
2008-02-20 18:30:00 +01:00
|
|
|
memset(ptr+sz_old*sizeof(unsigned int), 0,
|
|
|
|
(sz_new-sz_old)*sizeof(unsigned int));
|
2008-02-19 23:46:19 +01:00
|
|
|
return (bitarray_t*) ptr;
|
|
|
|
}
|
2007-07-26 23:26:53 +02:00
|
|
|
/** Free the bit array <b>ba</b>. */
|
|
|
|
static INLINE void
|
|
|
|
bitarray_free(bitarray_t *ba)
|
|
|
|
{
|
|
|
|
tor_free(ba);
|
|
|
|
}
|
|
|
|
/** Set the <b>bit</b>th bit in <b>b</b> to 1. */
|
|
|
|
static INLINE void
|
|
|
|
bitarray_set(bitarray_t *b, int bit)
|
|
|
|
{
|
|
|
|
b[bit >> BITARRAY_SHIFT] |= (1u << (bit & BITARRAY_MASK));
|
|
|
|
}
|
|
|
|
/** Set the <b>bit</b>th bit in <b>b</b> to 0. */
|
|
|
|
static INLINE void
|
|
|
|
bitarray_clear(bitarray_t *b, int bit)
|
|
|
|
{
|
|
|
|
b[bit >> BITARRAY_SHIFT] &= ~ (1u << (bit & BITARRAY_MASK));
|
|
|
|
}
|
|
|
|
/** Return true iff <b>bit</b>th bit in <b>b</b> is nonzero. NOTE: does
|
|
|
|
* not necessarily return 1 on true. */
|
|
|
|
static INLINE unsigned int
|
|
|
|
bitarray_is_set(bitarray_t *b, int bit)
|
|
|
|
{
|
|
|
|
return b[bit >> BITARRAY_SHIFT] & (1u << (bit & BITARRAY_MASK));
|
|
|
|
}
|
|
|
|
|
2008-04-08 19:06:41 +02:00
|
|
|
/** A set of digests, implemented as a Bloom filter. */
|
|
|
|
typedef struct {
|
|
|
|
int mask; /* One less than the number of bits in <b>ba</b>; always one less
|
|
|
|
* than a power of two. */
|
|
|
|
bitarray_t *ba; /* A bit array to implement the Bloom filter. */
|
|
|
|
} digestset_t;
|
|
|
|
|
|
|
|
#define BIT(n) ((n) & set->mask)
|
|
|
|
/** Add the digest <b>digest</b> to <b>set</b>. */
|
|
|
|
static INLINE void
|
|
|
|
digestset_add(digestset_t *set, const char *digest)
|
|
|
|
{
|
|
|
|
const uint32_t *p = (const uint32_t *)digest;
|
|
|
|
const uint32_t d1 = p[0] + (p[1]>>16);
|
|
|
|
const uint32_t d2 = p[1] + (p[2]>>16);
|
|
|
|
const uint32_t d3 = p[2] + (p[3]>>16);
|
|
|
|
const uint32_t d4 = p[3] + (p[0]>>16);
|
|
|
|
bitarray_set(set->ba, BIT(d1));
|
|
|
|
bitarray_set(set->ba, BIT(d2));
|
|
|
|
bitarray_set(set->ba, BIT(d3));
|
|
|
|
bitarray_set(set->ba, BIT(d4));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** If <b>digest</b> is in <b>set</b>, return nonzero. Otherwise,
|
|
|
|
* <em>probably</em> return zero. */
|
|
|
|
static INLINE int
|
|
|
|
digestset_isin(const digestset_t *set, const char *digest)
|
|
|
|
{
|
|
|
|
const uint32_t *p = (const uint32_t *)digest;
|
|
|
|
const uint32_t d1 = p[0] + (p[1]>>16);
|
|
|
|
const uint32_t d2 = p[1] + (p[2]>>16);
|
|
|
|
const uint32_t d3 = p[2] + (p[3]>>16);
|
|
|
|
const uint32_t d4 = p[3] + (p[0]>>16);
|
|
|
|
return bitarray_is_set(set->ba, BIT(d1)) &&
|
|
|
|
bitarray_is_set(set->ba, BIT(d2)) &&
|
|
|
|
bitarray_is_set(set->ba, BIT(d3)) &&
|
|
|
|
bitarray_is_set(set->ba, BIT(d4));
|
|
|
|
}
|
|
|
|
#undef BIT
|
|
|
|
|
|
|
|
digestset_t *digestset_new(int max_elements);
|
|
|
|
void digestset_free(digestset_t* set);
|
|
|
|
|
2007-09-17 20:27:49 +02:00
|
|
|
/* These functions, given an <b>array</b> of <b>n_elements</b>, return the
|
|
|
|
* <b>nth</b> lowest element. <b>nth</b>=0 gives the lowest element;
|
|
|
|
* <b>n_elements</b>-1 gives the highest; and (<b>n_elements</b>-1) / 2 gives
|
|
|
|
* the median. As a side effect, the elements of <b>array</b> are sorted. */
|
|
|
|
int find_nth_int(int *array, int n_elements, int nth);
|
|
|
|
time_t find_nth_time(time_t *array, int n_elements, int nth);
|
|
|
|
double find_nth_double(double *array, int n_elements, int nth);
|
|
|
|
uint32_t find_nth_uint32(uint32_t *array, int n_elements, int nth);
|
2007-11-08 17:58:59 +01:00
|
|
|
long find_nth_long(long *array, int n_elements, int nth);
|
2007-09-17 20:27:49 +02:00
|
|
|
static INLINE int
|
|
|
|
median_int(int *array, int n_elements)
|
|
|
|
{
|
|
|
|
return find_nth_int(array, n_elements, (n_elements-1)/2);
|
|
|
|
}
|
|
|
|
static INLINE time_t
|
|
|
|
median_time(time_t *array, int n_elements)
|
|
|
|
{
|
|
|
|
return find_nth_time(array, n_elements, (n_elements-1)/2);
|
|
|
|
}
|
|
|
|
static INLINE double
|
|
|
|
median_double(double *array, int n_elements)
|
|
|
|
{
|
|
|
|
return find_nth_double(array, n_elements, (n_elements-1)/2);
|
|
|
|
}
|
|
|
|
static INLINE uint32_t
|
|
|
|
median_uint32(uint32_t *array, int n_elements)
|
|
|
|
{
|
|
|
|
return find_nth_uint32(array, n_elements, (n_elements-1)/2);
|
|
|
|
}
|
2007-11-08 17:58:59 +01:00
|
|
|
static INLINE long
|
|
|
|
median_long(long *array, int n_elements)
|
|
|
|
{
|
|
|
|
return find_nth_long(array, n_elements, (n_elements-1)/2);
|
|
|
|
}
|
2007-09-17 20:27:49 +02:00
|
|
|
|
2004-11-01 21:41:47 +01:00
|
|
|
#endif
|
2005-06-09 21:03:31 +02:00
|
|
|
|