30 #define DEFINE_MAP_STRUCTS(maptype, keydecl, prefix) \
31 typedef struct prefix ## entry_t { \
32 HT_ENTRY(prefix ## entry_t) node; \
35 } prefix ## entry_t; \
37 HT_HEAD(prefix ## impl, prefix ## entry_t) head; \
48 return !strcmp(a->key, b->key);
52 static inline unsigned int
55 return (
unsigned) siphash24g(a->key, strlen(a->key));
66 static inline unsigned int
69 return (
unsigned) siphash24g(a->key,
DIGEST_LEN);
75 const digest256map_entry_t *b)
81 static inline unsigned int
97 HT_PROTOTYPE(digest256map_impl, digest256map_entry_t, node,
100 HT_GENERATE2(digest256map_impl, digest256map_entry_t, node,
104 #define strmap_entry_free(ent) \
105 FREE_AND_NULL(strmap_entry_t, strmap_entry_free_, (ent))
106 #define digestmap_entry_free(ent) \
107 FREE_AND_NULL(digestmap_entry_t, digestmap_entry_free_, (ent))
108 #define digest256map_entry_free(ent) \
109 FREE_AND_NULL(digest256map_entry_t, digest256map_entry_free_, (ent))
112 strmap_entry_free_(strmap_entry_t *ent)
118 digestmap_entry_free_(digestmap_entry_t *ent)
123 digest256map_entry_free_(digest256map_entry_t *ent)
129 strmap_assign_tmp_key(strmap_entry_t *ent,
const char *key)
131 ent->key = (
char*)key;
134 digestmap_assign_tmp_key(digestmap_entry_t *ent,
const char *key)
139 digest256map_assign_tmp_key(digest256map_entry_t *ent,
const uint8_t *key)
144 strmap_assign_key(strmap_entry_t *ent,
const char *key)
146 ent->key = tor_strdup(key);
149 digestmap_assign_key(digestmap_entry_t *ent,
const char *key)
154 digest256map_assign_key(digest256map_entry_t *ent,
const uint8_t *key)
167 #define IMPLEMENT_MAP_FNS(maptype, keytype, prefix) \
169 MOCK_IMPL(maptype *, \
170 prefix##_new,(void)) \
173 result = tor_malloc(sizeof(maptype)); \
174 HT_INIT(prefix##_impl, &result->head); \
181 prefix##_get(const maptype *map, const keytype key) \
183 prefix ##_entry_t *resolve; \
184 prefix ##_entry_t search; \
187 prefix ##_assign_tmp_key(&search, key); \
188 resolve = HT_FIND(prefix ##_impl, &map->head, &search); \
190 return resolve->val; \
199 prefix##_set(maptype *map, const keytype key, void *val) \
201 prefix##_entry_t search; \
206 prefix##_assign_tmp_key(&search, key); \
212 HT_FIND_OR_INSERT_(prefix##_impl, node, prefix##_entry_hash, \
214 prefix##_entry_t, &search, ptr, \
217 oldval = (*ptr)->val; \
223 prefix##_entry_t *newent = \
224 tor_malloc_zero(sizeof(prefix##_entry_t)); \
225 prefix##_assign_key(newent, key); \
227 HT_FOI_INSERT_(node, &(map->head), \
228 &search, newent, ptr); \
240 prefix##_remove(maptype *map, const keytype key) \
242 prefix##_entry_t *resolve; \
243 prefix##_entry_t search; \
247 prefix##_assign_tmp_key(&search, key); \
248 resolve = HT_REMOVE(prefix##_impl, &map->head, &search); \
250 oldval = resolve->val; \
251 prefix##_entry_free(resolve); \
260 prefix##_size(const maptype *map) \
262 return HT_SIZE(&map->head); \
267 prefix##_isempty(const maptype *map) \
269 return HT_EMPTY(&map->head); \
274 prefix##_assert_ok(const maptype *map) \
276 tor_assert(!prefix##_impl_HT_REP_IS_BAD_(&map->head)); \
283 prefix##_free_, (maptype *map, void (*free_val)(void*))) \
285 prefix##_entry_t **ent, **next, *this; \
288 for (ent = HT_START(prefix##_impl, &map->head); ent != NULL; \
291 next = HT_NEXT_RMV(prefix##_impl, &map->head, ent); \
293 free_val(this->val); \
294 prefix##_entry_free(this); \
296 tor_assert(HT_EMPTY(&map->head)); \
297 HT_CLEAR(prefix##_impl, &map->head); \
323 prefix##_iter_init(maptype *map) \
326 return HT_START(prefix##_impl, &map->head); \
332 prefix##_iter_next(maptype *map, prefix##_iter_t *iter) \
336 return HT_NEXT(prefix##_impl, &map->head, iter); \
341 prefix##_iter_next_rmv(maptype *map, prefix##_iter_t *iter) \
343 prefix##_entry_t *rmv; \
348 iter = HT_NEXT_RMV(prefix##_impl, &map->head, iter); \
349 prefix##_entry_free(rmv); \
355 prefix##_iter_get(prefix##_iter_t *iter, const keytype *keyp, \
362 *keyp = (*iter)->key; \
363 *valp = (*iter)->val; \
368 prefix##_iter_done(prefix##_iter_t *iter) \
370 return iter == NULL; \
384 char *lc_key = tor_strdup(key);
386 v = strmap_set(map,lc_key,val);
396 char *lc_key = tor_strdup(key);
398 v = strmap_get(map,lc_key);
408 char *lc_key = tor_strdup(key);
410 v = strmap_remove(map,lc_key);
int tor_memeq(const void *a, const void *b, size_t sz)
Definitions for common sizes of cryptographic digests.
HT_PROTOTYPE(hs_circuitmap_ht, circuit_t, hs_circuitmap_node, hs_circuit_hash_token, hs_circuits_have_same_token)
void tor_free_(void *mem)
void * tor_reallocarray_(void *ptr, size_t sz1, size_t sz2)
Headers for util_malloc.c.
static int strmap_entries_eq(const strmap_entry_t *a, const strmap_entry_t *b)
void * strmap_get_lc(const strmap_t *map, const char *key)
static unsigned int strmap_entry_hash(const strmap_entry_t *a)
static int digest256map_entries_eq(const digest256map_entry_t *a, const digest256map_entry_t *b)
#define IMPLEMENT_MAP_FNS(maptype, keytype, prefix)
static unsigned int digest256map_entry_hash(const digest256map_entry_t *a)
static int digestmap_entries_eq(const digestmap_entry_t *a, const digestmap_entry_t *b)
void * strmap_remove_lc(strmap_t *map, const char *key)
#define DEFINE_MAP_STRUCTS(maptype, keydecl, prefix)
void * strmap_set_lc(strmap_t *map, const char *key, void *val)
static unsigned int digestmap_entry_hash(const digestmap_entry_t *a)
Macros to manage assertions, fatal and non-fatal.
void tor_strlower(char *s)
Header for util_string.c.