mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-24 12:23:32 +01:00
Replace balanced trees with hash tables: this should make stuff significantly faster.
svn:r5441
This commit is contained in:
parent
ae67b87f9a
commit
a39269572f
@ -6,4 +6,4 @@ noinst_LIBRARIES = libor.a libor-crypto.a
|
||||
libor_a_SOURCES = log.c util.c compat.c container.c
|
||||
libor_crypto_a_SOURCES = crypto.c aes.c tortls.c torgzip.c
|
||||
|
||||
noinst_HEADERS = log.h crypto.h test.h util.h compat.h aes.h torint.h tortls.h strlcpy.c strlcat.c torgzip.h container.h
|
||||
noinst_HEADERS = log.h crypto.h test.h util.h compat.h aes.h torint.h tortls.h strlcpy.c strlcat.c torgzip.h container.h ht.h
|
||||
|
@ -14,7 +14,6 @@ const char container_c_id[] = "$Id$";
|
||||
#include "compat.h"
|
||||
#include "util.h"
|
||||
#include "log.h"
|
||||
#include "../or/tree.h"
|
||||
#include "container.h"
|
||||
#include "crypto.h"
|
||||
|
||||
@ -25,6 +24,8 @@ const char container_c_id[] = "$Id$";
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "ht.h"
|
||||
|
||||
/* All newly allocated smartlists have this capacity.
|
||||
*/
|
||||
#define SMARTLIST_DEFAULT_CAPACITY 32
|
||||
@ -445,40 +446,53 @@ smartlist_sort_strings(smartlist_t *sl)
|
||||
|
||||
#define DEFINE_MAP_STRUCTS(maptype, keydecl, prefix) \
|
||||
typedef struct prefix ## entry_t { \
|
||||
SPLAY_ENTRY(prefix ## entry_t) node; \
|
||||
HT_ENTRY(prefix ## entry_t) node; \
|
||||
keydecl; \
|
||||
void *val; \
|
||||
} prefix ## entry_t; \
|
||||
struct maptype { \
|
||||
SPLAY_HEAD(prefix ## tree, prefix ## entry_t) head; \
|
||||
HT_HEAD(prefix ## tree, prefix ## entry_t) head; \
|
||||
};
|
||||
|
||||
DEFINE_MAP_STRUCTS(strmap_t, char *key, strmap_);
|
||||
DEFINE_MAP_STRUCTS(digestmap_t, char key[DIGEST_LEN], digestmap_);
|
||||
|
||||
/** Helper: compare strmap_t_entry objects by key value. */
|
||||
static int
|
||||
compare_strmap_entries(strmap_entry_t *a,
|
||||
strmap_entry_t *b)
|
||||
static INLINE int
|
||||
strmap_entries_eq(strmap_entry_t *a, strmap_entry_t *b)
|
||||
{
|
||||
return strcmp(a->key, b->key);
|
||||
return !strcmp(a->key, b->key);
|
||||
}
|
||||
|
||||
static INLINE unsigned int
|
||||
strmap_entry_hash(strmap_entry_t *a)
|
||||
{
|
||||
return ht_string_hash(a->key);
|
||||
}
|
||||
|
||||
/** Helper: compare digestmap_entry_t objects by key value. */
|
||||
static int
|
||||
compare_digestmap_entries(digestmap_entry_t *a,
|
||||
digestmap_entry_t *b)
|
||||
static INLINE int
|
||||
digestmap_entries_eq(digestmap_entry_t *a, digestmap_entry_t *b)
|
||||
{
|
||||
return memcmp(a->key, b->key, DIGEST_LEN);
|
||||
return !memcmp(a->key, b->key, DIGEST_LEN);
|
||||
}
|
||||
|
||||
SPLAY_PROTOTYPE(strmap_tree, strmap_entry_t, node, compare_strmap_entries);
|
||||
SPLAY_GENERATE(strmap_tree, strmap_entry_t, node, compare_strmap_entries);
|
||||
static INLINE unsigned int
|
||||
digestmap_entry_hash(digestmap_entry_t *a)
|
||||
{
|
||||
uint32_t *p = (uint32_t*)a->key;
|
||||
return ht_improve_hash(p[0] ^ p[1] ^ p[2] ^ p[3] ^ p[4]);
|
||||
}
|
||||
|
||||
SPLAY_PROTOTYPE(digestmap_tree, digestmap_entry_t, node,
|
||||
compare_digestmap_entries);
|
||||
SPLAY_GENERATE(digestmap_tree, digestmap_entry_t, node,
|
||||
compare_digestmap_entries);
|
||||
HT_PROTOTYPE(strmap_tree, strmap_entry_t, node, strmap_entry_hash,
|
||||
strmap_entries_eq);
|
||||
HT_GENERATE(strmap_tree, strmap_entry_t, node, strmap_entry_hash,
|
||||
strmap_entries_eq, 0.6, malloc, realloc, free);
|
||||
|
||||
HT_PROTOTYPE(digestmap_tree, digestmap_entry_t, node, digestmap_entry_hash,
|
||||
digestmap_entries_eq);
|
||||
HT_GENERATE(digestmap_tree, digestmap_entry_t, node, digestmap_entry_hash,
|
||||
digestmap_entries_eq, 0.6, malloc, realloc, free);
|
||||
|
||||
/** Constructor to create a new empty map from strings to void*'s.
|
||||
*/
|
||||
@ -487,7 +501,7 @@ strmap_new(void)
|
||||
{
|
||||
strmap_t *result;
|
||||
result = tor_malloc(sizeof(strmap_t));
|
||||
SPLAY_INIT(&result->head);
|
||||
HT_INIT(&result->head);
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -498,7 +512,7 @@ digestmap_new(void)
|
||||
{
|
||||
digestmap_t *result;
|
||||
result = tor_malloc(sizeof(digestmap_t));
|
||||
SPLAY_INIT(&result->head);
|
||||
HT_INIT(&result->head);
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -518,7 +532,7 @@ strmap_set(strmap_t *map, const char *key, void *val)
|
||||
tor_assert(key);
|
||||
tor_assert(val);
|
||||
search.key = (char*)key;
|
||||
resolve = SPLAY_FIND(strmap_tree, &map->head, &search);
|
||||
resolve = HT_FIND(strmap_tree, &map->head, &search);
|
||||
if (resolve) {
|
||||
oldval = resolve->val;
|
||||
resolve->val = val;
|
||||
@ -527,7 +541,8 @@ strmap_set(strmap_t *map, const char *key, void *val)
|
||||
resolve = tor_malloc_zero(sizeof(strmap_entry_t));
|
||||
resolve->key = tor_strdup(key);
|
||||
resolve->val = val;
|
||||
SPLAY_INSERT(strmap_tree, &map->head, resolve);
|
||||
tor_assert(!HT_FIND(strmap_tree, &map->head, resolve));
|
||||
HT_INSERT(strmap_tree, &map->head, resolve);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
@ -543,7 +558,7 @@ digestmap_set(digestmap_t *map, const char *key, void *val)
|
||||
tor_assert(key);
|
||||
tor_assert(val);
|
||||
memcpy(&search.key, key, DIGEST_LEN);
|
||||
resolve = SPLAY_FIND(digestmap_tree, &map->head, &search);
|
||||
resolve = HT_FIND(digestmap_tree, &map->head, &search);
|
||||
if (resolve) {
|
||||
oldval = resolve->val;
|
||||
resolve->val = val;
|
||||
@ -552,7 +567,7 @@ digestmap_set(digestmap_t *map, const char *key, void *val)
|
||||
resolve = tor_malloc_zero(sizeof(digestmap_entry_t));
|
||||
memcpy(resolve->key, key, DIGEST_LEN);
|
||||
resolve->val = val;
|
||||
SPLAY_INSERT(digestmap_tree, &map->head, resolve);
|
||||
HT_INSERT(digestmap_tree, &map->head, resolve);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
@ -568,7 +583,7 @@ strmap_get(strmap_t *map, const char *key)
|
||||
tor_assert(map);
|
||||
tor_assert(key);
|
||||
search.key = (char*)key;
|
||||
resolve = SPLAY_FIND(strmap_tree, &map->head, &search);
|
||||
resolve = HT_FIND(strmap_tree, &map->head, &search);
|
||||
if (resolve) {
|
||||
return resolve->val;
|
||||
} else {
|
||||
@ -585,7 +600,7 @@ digestmap_get(digestmap_t *map, const char *key)
|
||||
tor_assert(map);
|
||||
tor_assert(key);
|
||||
memcpy(&search.key, key, DIGEST_LEN);
|
||||
resolve = SPLAY_FIND(digestmap_tree, &map->head, &search);
|
||||
resolve = HT_FIND(digestmap_tree, &map->head, &search);
|
||||
if (resolve) {
|
||||
return resolve->val;
|
||||
} else {
|
||||
@ -608,10 +623,9 @@ strmap_remove(strmap_t *map, const char *key)
|
||||
tor_assert(map);
|
||||
tor_assert(key);
|
||||
search.key = (char*)key;
|
||||
resolve = SPLAY_FIND(strmap_tree, &map->head, &search);
|
||||
resolve = HT_REMOVE(strmap_tree, &map->head, &search);
|
||||
if (resolve) {
|
||||
oldval = resolve->val;
|
||||
SPLAY_REMOVE(strmap_tree, &map->head, resolve);
|
||||
tor_free(resolve->key);
|
||||
tor_free(resolve);
|
||||
return oldval;
|
||||
@ -630,10 +644,9 @@ digestmap_remove(digestmap_t *map, const char *key)
|
||||
tor_assert(map);
|
||||
tor_assert(key);
|
||||
memcpy(&search.key, key, DIGEST_LEN);
|
||||
resolve = SPLAY_FIND(digestmap_tree, &map->head, &search);
|
||||
resolve = HT_REMOVE(digestmap_tree, &map->head, &search);
|
||||
if (resolve) {
|
||||
oldval = resolve->val;
|
||||
SPLAY_REMOVE(digestmap_tree, &map->head, resolve);
|
||||
tor_free(resolve);
|
||||
return oldval;
|
||||
} else {
|
||||
@ -679,53 +692,6 @@ strmap_remove_lc(strmap_t *map, const char *key)
|
||||
return v;
|
||||
}
|
||||
|
||||
/** Invoke fn() on every entry of the map, in order. For every entry,
|
||||
* fn() is invoked with that entry's key, that entry's value, and the
|
||||
* value of <b>data</b> supplied to strmap_foreach. fn() must return a new
|
||||
* (possibly unmodified) value for each entry: if fn() returns NULL, the
|
||||
* entry is removed.
|
||||
*
|
||||
* Example:
|
||||
* \code
|
||||
* static void* upcase_and_remove_empty_vals(const char *key, void *val,
|
||||
* void* data) {
|
||||
* char *cp = (char*)val;
|
||||
* if (!*cp) { // val is an empty string.
|
||||
* free(val);
|
||||
* return NULL;
|
||||
* } else {
|
||||
* for (; *cp; cp++)
|
||||
* *cp = toupper(*cp);
|
||||
* }
|
||||
* return val;
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* ...
|
||||
*
|
||||
* strmap_foreach(map, upcase_and_remove_empty_vals, NULL);
|
||||
* \endcode
|
||||
*/
|
||||
void
|
||||
strmap_foreach(strmap_t *map,
|
||||
void* (*fn)(const char *key, void *val, void *data),
|
||||
void *data)
|
||||
{
|
||||
strmap_entry_t *ptr, *next;
|
||||
tor_assert(map);
|
||||
tor_assert(fn);
|
||||
for (ptr = SPLAY_MIN(strmap_tree, &map->head); ptr != NULL; ptr = next) {
|
||||
/* This remove-in-place usage is specifically blessed in tree(3). */
|
||||
next = SPLAY_NEXT(strmap_tree, &map->head, ptr);
|
||||
ptr->val = fn(ptr->key, ptr->val, data);
|
||||
if (!ptr->val) {
|
||||
SPLAY_REMOVE(strmap_tree, &map->head, ptr);
|
||||
tor_free(ptr->key);
|
||||
tor_free(ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** return an <b>iterator</b> pointer to the front of a map.
|
||||
*
|
||||
* Iterator example:
|
||||
@ -756,14 +722,14 @@ strmap_iter_t *
|
||||
strmap_iter_init(strmap_t *map)
|
||||
{
|
||||
tor_assert(map);
|
||||
return SPLAY_MIN(strmap_tree, &map->head);
|
||||
return HT_START(strmap_tree, &map->head);
|
||||
}
|
||||
|
||||
digestmap_iter_t *
|
||||
digestmap_iter_init(digestmap_t *map)
|
||||
{
|
||||
tor_assert(map);
|
||||
return SPLAY_MIN(digestmap_tree, &map->head);
|
||||
return HT_START(digestmap_tree, &map->head);
|
||||
}
|
||||
|
||||
/** Advance the iterator <b>iter</b> for map a single step to the next entry.
|
||||
@ -773,7 +739,7 @@ strmap_iter_next(strmap_t *map, strmap_iter_t *iter)
|
||||
{
|
||||
tor_assert(map);
|
||||
tor_assert(iter);
|
||||
return SPLAY_NEXT(strmap_tree, &map->head, iter);
|
||||
return HT_NEXT(strmap_tree, &map->head, iter);
|
||||
}
|
||||
|
||||
digestmap_iter_t *
|
||||
@ -781,7 +747,7 @@ digestmap_iter_next(digestmap_t *map, digestmap_iter_t *iter)
|
||||
{
|
||||
tor_assert(map);
|
||||
tor_assert(iter);
|
||||
return SPLAY_NEXT(digestmap_tree, &map->head, iter);
|
||||
return HT_NEXT(digestmap_tree, &map->head, iter);
|
||||
}
|
||||
|
||||
/** Advance the iterator <b>iter</b> a single step to the next entry, removing
|
||||
@ -793,10 +759,9 @@ strmap_iter_next_rmv(strmap_t *map, strmap_iter_t *iter)
|
||||
strmap_iter_t *next;
|
||||
tor_assert(map);
|
||||
tor_assert(iter);
|
||||
next = SPLAY_NEXT(strmap_tree, &map->head, iter);
|
||||
SPLAY_REMOVE(strmap_tree, &map->head, iter);
|
||||
tor_free(iter->key);
|
||||
tor_free(iter);
|
||||
next = HT_NEXT_RMV(strmap_tree, &map->head, iter);
|
||||
tor_free((*iter)->key);
|
||||
tor_free(*iter);
|
||||
return next;
|
||||
}
|
||||
|
||||
@ -806,9 +771,8 @@ digestmap_iter_next_rmv(digestmap_t *map, digestmap_iter_t *iter)
|
||||
digestmap_iter_t *next;
|
||||
tor_assert(map);
|
||||
tor_assert(iter);
|
||||
next = SPLAY_NEXT(digestmap_tree, &map->head, iter);
|
||||
SPLAY_REMOVE(digestmap_tree, &map->head, iter);
|
||||
tor_free(iter);
|
||||
next = HT_NEXT_RMV(digestmap_tree, &map->head, iter);
|
||||
tor_free(*iter);
|
||||
return next;
|
||||
}
|
||||
|
||||
@ -820,8 +784,8 @@ strmap_iter_get(strmap_iter_t *iter, const char **keyp, void **valp)
|
||||
tor_assert(iter);
|
||||
tor_assert(keyp);
|
||||
tor_assert(valp);
|
||||
*keyp = iter->key;
|
||||
*valp = iter->val;
|
||||
*keyp = (*iter)->key;
|
||||
*valp = (*iter)->val;
|
||||
}
|
||||
|
||||
void
|
||||
@ -830,8 +794,8 @@ digestmap_iter_get(digestmap_iter_t *iter, const char **keyp, void **valp)
|
||||
tor_assert(iter);
|
||||
tor_assert(keyp);
|
||||
tor_assert(valp);
|
||||
*keyp = iter->key;
|
||||
*valp = iter->val;
|
||||
*keyp = (*iter)->key;
|
||||
*valp = (*iter)->val;
|
||||
}
|
||||
|
||||
/** Return true iff iter has advanced past the last entry of map.
|
||||
@ -853,30 +817,32 @@ digestmap_iter_done(digestmap_iter_t *iter)
|
||||
void
|
||||
strmap_free(strmap_t *map, void (*free_val)(void*))
|
||||
{
|
||||
strmap_entry_t *ent, *next;
|
||||
for (ent = SPLAY_MIN(strmap_tree, &map->head); ent != NULL; ent = next) {
|
||||
next = SPLAY_NEXT(strmap_tree, &map->head, ent);
|
||||
SPLAY_REMOVE(strmap_tree, &map->head, ent);
|
||||
tor_free(ent->key);
|
||||
strmap_entry_t **ent, **next, *this;
|
||||
for (ent = HT_START(strmap_tree, &map->head); ent != NULL; ent = next) {
|
||||
this = *ent;
|
||||
next = HT_NEXT_RMV(strmap_tree, &map->head, ent);
|
||||
tor_free(this->key);
|
||||
if (free_val)
|
||||
free_val(ent->val);
|
||||
tor_free(ent);
|
||||
free_val(this->val);
|
||||
tor_free(this);
|
||||
}
|
||||
tor_assert(SPLAY_EMPTY(&map->head));
|
||||
tor_assert(HT_EMPTY(&map->head));
|
||||
HT_CLEAR(strmap_tree, &map->head);
|
||||
tor_free(map);
|
||||
}
|
||||
void
|
||||
digestmap_free(digestmap_t *map, void (*free_val)(void*))
|
||||
{
|
||||
digestmap_entry_t *ent, *next;
|
||||
for (ent = SPLAY_MIN(digestmap_tree, &map->head); ent != NULL; ent = next) {
|
||||
next = SPLAY_NEXT(digestmap_tree, &map->head, ent);
|
||||
SPLAY_REMOVE(digestmap_tree, &map->head, ent);
|
||||
digestmap_entry_t **ent, **next, *this;
|
||||
for (ent = HT_START(digestmap_tree, &map->head); ent != NULL; ent = next) {
|
||||
this = *ent;
|
||||
next = HT_NEXT_RMV(digestmap_tree, &map->head, ent);
|
||||
if (free_val)
|
||||
free_val(ent->val);
|
||||
tor_free(ent);
|
||||
free_val(this->val);
|
||||
tor_free(this);
|
||||
}
|
||||
tor_assert(SPLAY_EMPTY(&map->head));
|
||||
tor_assert(HT_EMPTY(&map->head));
|
||||
HT_CLEAR(digestmap_tree, &map->head);
|
||||
tor_free(map);
|
||||
}
|
||||
|
||||
@ -884,12 +850,12 @@ digestmap_free(digestmap_t *map, void (*free_val)(void*))
|
||||
int
|
||||
strmap_isempty(strmap_t *map)
|
||||
{
|
||||
return SPLAY_EMPTY(&map->head);
|
||||
return HT_EMPTY(&map->head);
|
||||
}
|
||||
|
||||
int
|
||||
digestmap_isempty(digestmap_t *map)
|
||||
{
|
||||
return SPLAY_EMPTY(&map->head);
|
||||
return HT_EMPTY(&map->head);
|
||||
}
|
||||
|
||||
|
@ -110,13 +110,11 @@ char *smartlist_join_strings2(smartlist_t *sl, const char *join,
|
||||
|
||||
#define DECLARE_MAP_FNS(maptype, keytype, prefix) \
|
||||
typedef struct maptype maptype; \
|
||||
typedef struct prefix##entry_t prefix##iter_t; \
|
||||
typedef struct prefix##entry_t *prefix##iter_t; \
|
||||
maptype* prefix##new(void); \
|
||||
void* prefix##set(maptype *map, keytype key, void *val); \
|
||||
void* prefix##get(maptype *map, keytype key); \
|
||||
void* prefix##remove(maptype *map, keytype key); \
|
||||
typedef void* (*prefix##foreach_fn)(keytype key, void *val, void *data); \
|
||||
void prefix##foreach(maptype *map, prefix##foreach_fn fn, void *data); \
|
||||
void prefix##free(maptype *map, void (*free_val)(void*)); \
|
||||
int prefix##isempty(maptype *map); \
|
||||
prefix##iter_t *prefix##iter_init(maptype *map); \
|
||||
|
495
src/common/ht.h
Normal file
495
src/common/ht.h
Normal file
@ -0,0 +1,495 @@
|
||||
/* Copyright 2002 Christopher Clark */
|
||||
/* Copyright 2005 Nick Mathewson */
|
||||
/* See license at end. */
|
||||
/* $Id$ */
|
||||
|
||||
/* Based on ideas by Christopher Clark and interfaces from Niels Provos. */
|
||||
|
||||
#ifndef __HT_H
|
||||
#define __HT_H
|
||||
#define HT_H_ID "$Id$"
|
||||
|
||||
#define HT_HEAD(name, type) \
|
||||
struct name { \
|
||||
/* How long is the hash table? */ \
|
||||
unsigned hth_table_length; \
|
||||
/* The hash table itself. */ \
|
||||
struct type **hth_table; \
|
||||
/* How many elements does the table contain? */ \
|
||||
unsigned hth_n_entries; \
|
||||
/* How many elements will we allow in the table before resizing it? */ \
|
||||
unsigned hth_load_limit; \
|
||||
/* Position of hth_table_length in the primes table. */ \
|
||||
int hth_prime_idx; \
|
||||
}
|
||||
|
||||
#define HT_INITIALIZER() \
|
||||
{ 0, NULL, 0, 0, -1 }
|
||||
|
||||
#define HT_INIT(root) do { \
|
||||
(root)->hth_table_length = 0; \
|
||||
(root)->hth_table = NULL; \
|
||||
(root)->hth_n_entries = 0; \
|
||||
(root)->hth_load_limit = 0; \
|
||||
(root)->hth_prime_idx = -1; \
|
||||
} while (0)
|
||||
|
||||
#define HT_ENTRY(type) \
|
||||
struct { \
|
||||
struct type *hte_next; \
|
||||
unsigned hte_hash; \
|
||||
}
|
||||
|
||||
#define HT_EMPTY(head) \
|
||||
((head)->hth_n_entries == 0)
|
||||
|
||||
/* Helper: alias for the bucket containing 'elm'. */
|
||||
#define _HT_BUCKET(head, field, elm) \
|
||||
((head)->hth_table[elm->field.hte_hash % head->hth_table_length])
|
||||
|
||||
/* How many elements in 'head'? */
|
||||
#define HT_SIZE(head) \
|
||||
((head)->hth_n_entries)
|
||||
|
||||
#define HT_FIND(name, head, elm) name##_HT_FIND((head), (elm))
|
||||
#define HT_INSERT(name, head, elm) name##_HT_INSERT((head), (elm))
|
||||
#define HT_REPLACE(name, head, elm) name##_HT_REPLACE((head), (elm))
|
||||
#define HT_REMOVE(name, head, elm) name##_HT_REMOVE((head), (elm))
|
||||
#define HT_START(name, head) name##_HT_START(head)
|
||||
#define HT_NEXT(name, head, elm) name##_HT_NEXT((head), (elm))
|
||||
#define HT_NEXT_RMV(name, head, elm) name##_HT_NEXT_RMV((head), (elm))
|
||||
#define HT_CLEAR(name, head) name##_HT_CLEAR(head)
|
||||
|
||||
/* Helper: */
|
||||
static __inline unsigned
|
||||
ht_improve_hash(unsigned h)
|
||||
{
|
||||
/* Aim to protect against poor hash functions by adding logic here
|
||||
* - logic taken from java 1.4 hashtable source */
|
||||
h += ~(h << 9);
|
||||
h ^= ((h >> 14) | (h << 18)); /* >>> */
|
||||
h += (h << 4);
|
||||
h ^= ((h >> 10) | (h << 22)); /* >>> */
|
||||
return h;
|
||||
}
|
||||
|
||||
/** Basic string hash function, from Java standard String.hashCode(). */
|
||||
static __inline unsigned
|
||||
ht_string_hash(const char *s)
|
||||
{
|
||||
unsigned h = 0;
|
||||
int m = 1;
|
||||
while (*s) {
|
||||
h += ((signed char)*s++)*m;
|
||||
m = (m<<5)-1; /* m *= 31 */
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
#define _HT_SET_HASH(elm, field, hashfn) \
|
||||
do { \
|
||||
elm->field.hte_next = NULL; \
|
||||
elm->field.hte_hash = hashfn(elm); \
|
||||
} while (0)
|
||||
|
||||
#define HT_FOREACH(x, name, head) \
|
||||
for ((x) = HT_START(name, head); \
|
||||
(x) != NULL; \
|
||||
(x) = HT_NEXT(name, head, x))
|
||||
|
||||
#define HT_PROTOTYPE(name, type, field, hashfn, eqfn) \
|
||||
int name##_HT_GROW(struct name *ht, unsigned min_capacity); \
|
||||
void name##_HT_CLEAR(struct name *ht); \
|
||||
int _##name##_HT_REP_OK(struct name *ht); \
|
||||
/* Helper: returns a pointer to the right location in the table \
|
||||
* 'head' to find or insert the element 'elm'. */ \
|
||||
static __inline struct type ** \
|
||||
_##name##_HT_FIND_P(struct name *head, struct type *elm) \
|
||||
{ \
|
||||
struct type **p; \
|
||||
if (!head->hth_table) \
|
||||
return NULL; \
|
||||
p = &_HT_BUCKET(head, field, elm); \
|
||||
while (*p) { \
|
||||
if (eqfn(*p, elm)) \
|
||||
return p; \
|
||||
p = &(*p)->field.hte_next; \
|
||||
} \
|
||||
return p; \
|
||||
} \
|
||||
/* Return a pointer to the element in the table 'head' matching 'elm', \
|
||||
* or NULL if no such element exists */ \
|
||||
static __inline struct type * \
|
||||
name##_HT_FIND(struct name *head, struct type *elm) \
|
||||
{ \
|
||||
struct type **p; \
|
||||
_HT_SET_HASH(elm, field, hashfn); \
|
||||
p = _##name##_HT_FIND_P(head, elm); \
|
||||
return p ? *p : NULL; \
|
||||
} \
|
||||
/* Insert the element 'elm' into the table 'head'. Do not call this \
|
||||
* function if the table might already contain a matching element. */ \
|
||||
static __inline void \
|
||||
name##_HT_INSERT(struct name *head, struct type *elm) \
|
||||
{ \
|
||||
struct type **p; \
|
||||
if (!head->hth_table || head->hth_n_entries >= head->hth_load_limit) \
|
||||
name##_HT_GROW(head, head->hth_n_entries+1); \
|
||||
++head->hth_n_entries; \
|
||||
_HT_SET_HASH(elm, field, hashfn); \
|
||||
p = &_HT_BUCKET(head, field, elm); \
|
||||
elm->field.hte_next = *p; \
|
||||
*p = elm; \
|
||||
} \
|
||||
/* Insert the element 'elm' into the table 'head'. If there already \
|
||||
* a matching element in the table, replace that element and return \
|
||||
* it. */ \
|
||||
static __inline struct type * \
|
||||
name##_HT_REPLACE(struct name *head, struct type *elm) \
|
||||
{ \
|
||||
struct type **p, *r; \
|
||||
if (!head->hth_table || head->hth_n_entries >= head->hth_load_limit) \
|
||||
name##_HT_GROW(head, head->hth_n_entries+1); \
|
||||
_HT_SET_HASH(elm, field, hashfn); \
|
||||
p = _##name##_HT_FIND_P(head, elm); \
|
||||
r = *p; \
|
||||
*p = elm; \
|
||||
if (r && (r!=elm)) { \
|
||||
elm->field.hte_next = r->field.hte_next; \
|
||||
r->field.hte_next = NULL; \
|
||||
return r; \
|
||||
} else { \
|
||||
++head->hth_n_entries; \
|
||||
return NULL; \
|
||||
} \
|
||||
} \
|
||||
/* Remove any element matching 'elm' from the table 'head'. If such \
|
||||
* an element is found, return it; otherwise return NULL. */ \
|
||||
static __inline struct type * \
|
||||
name##_HT_REMOVE(struct name *head, struct type *elm) \
|
||||
{ \
|
||||
struct type **p, *r; \
|
||||
_HT_SET_HASH(elm, field, hashfn); \
|
||||
p = _##name##_HT_FIND_P(head,elm); \
|
||||
if (!p || !*p) \
|
||||
return NULL; \
|
||||
r = *p; \
|
||||
*p = r->field.hte_next; \
|
||||
r->field.hte_next = NULL; \
|
||||
--head->hth_n_entries; \
|
||||
return r; \
|
||||
} \
|
||||
/* Invoke the function 'fn' on every element of the table 'head', \
|
||||
* using 'data' as its second argument. If the function returns \
|
||||
* nonzero, remove the most recently examined element before invoking \
|
||||
* the function again. */ \
|
||||
static __inline void \
|
||||
name##_HT_FOREACH_FN(struct name *head, \
|
||||
int (*fn)(struct type *, void *), \
|
||||
void *data) \
|
||||
{ \
|
||||
/* XXXX use tricks to prevent concurrent mod? */ \
|
||||
unsigned idx; \
|
||||
int remove; \
|
||||
struct type **p, **nextp, *next; \
|
||||
if (!head->hth_table) \
|
||||
return; \
|
||||
for (idx=0; idx < head->hth_table_length; ++idx) { \
|
||||
p = &head->hth_table[idx]; \
|
||||
while (*p) { \
|
||||
nextp = &(*p)->field.hte_next; \
|
||||
next = *nextp; \
|
||||
remove = fn(*p, data); \
|
||||
if (remove) { \
|
||||
--head->hth_n_entries; \
|
||||
*p = next; \
|
||||
} else { \
|
||||
p = nextp; \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
/* Return a pointer to the first element in the table 'head', under \
|
||||
* an arbitrary order. This order is stable under remove operations, \
|
||||
* but not under others. If the table is empty, return NULL. */ \
|
||||
static __inline struct type ** \
|
||||
name##_HT_START(struct name *head) \
|
||||
{ \
|
||||
unsigned b = 0; \
|
||||
while (b < head->hth_table_length) { \
|
||||
if (head->hth_table[b]) \
|
||||
return &head->hth_table[b]; \
|
||||
++b; \
|
||||
} \
|
||||
return NULL; \
|
||||
} \
|
||||
/* Return the next element in 'head' after 'elm', under the arbitrary \
|
||||
* order used by HT_START. If there are no more elements, return \
|
||||
* NULL. If 'elm' is to be removed from the table, you must call \
|
||||
* this function for the next value before you remove it. \
|
||||
*/ \
|
||||
static __inline struct type ** \
|
||||
name##_HT_NEXT(struct name *head, struct type **elm) \
|
||||
{ \
|
||||
if ((*elm)->field.hte_next) { \
|
||||
return &(*elm)->field.hte_next; \
|
||||
} else { \
|
||||
unsigned b = ((*elm)->field.hte_hash % head->hth_table_length)+1; \
|
||||
while (b < head->hth_table_length) { \
|
||||
if (head->hth_table[b]) \
|
||||
return &head->hth_table[b]; \
|
||||
++b; \
|
||||
} \
|
||||
return NULL; \
|
||||
} \
|
||||
} \
|
||||
static __inline struct type ** \
|
||||
name##_HT_NEXT_RMV(struct name *head, struct type **elm) \
|
||||
{ \
|
||||
unsigned h = (*elm)->field.hte_hash; \
|
||||
*elm = (*elm)->field.hte_next; \
|
||||
--head->hth_n_entries; \
|
||||
if (*elm) { \
|
||||
return elm; \
|
||||
} else { \
|
||||
unsigned b = (h % head->hth_table_length)+1; \
|
||||
while (b < head->hth_table_length) { \
|
||||
if (head->hth_table[b]) \
|
||||
return &head->hth_table[b]; \
|
||||
++b; \
|
||||
} \
|
||||
return NULL; \
|
||||
} \
|
||||
}
|
||||
|
||||
|
||||
#if 0
|
||||
/* Helpers for an iterator type that saves some mod operations at the expense
|
||||
* of many branches. Not worth it, it seems. */
|
||||
|
||||
#define HT_ITER(type) \
|
||||
struct type##_ITER { \
|
||||
struct type **hti_nextp; \
|
||||
unsigned hti_bucket; \
|
||||
}
|
||||
|
||||
static __inline void \
|
||||
name##_HT_ITER_START(struct name *head, struct type##_ITER *iter) \
|
||||
{ \
|
||||
/* XXXX Magic to stop modifications? */ \
|
||||
iter->hti_bucket = 0; \
|
||||
while (iter->hti_bucket < head->hth_table_length) { \
|
||||
iter->hti_nextp = &head->hth_table[iter->hti_bucket]; \
|
||||
if (*iter->hti_nextp) \
|
||||
return; \
|
||||
++iter->hti_bucket; \
|
||||
} \
|
||||
iter->hti_nextp = NULL; \
|
||||
} \
|
||||
static __inline int \
|
||||
name##_HT_ITER_DONE(struct name *head, struct type##_ITER *iter) \
|
||||
{ \
|
||||
return iter->hti_nextp == NULL; \
|
||||
} \
|
||||
static __inline struct type * \
|
||||
name##_HT_ITER_GET(struct name *head, struct type##_ITER *iter) \
|
||||
{ \
|
||||
return *iter->hti_nextp; \
|
||||
} \
|
||||
static __inline void \
|
||||
name##_HT_ITER_NEXT(struct name *head, struct type##_ITER *iter) \
|
||||
{ \
|
||||
if (!iter->hti_nextp) \
|
||||
return; \
|
||||
if ((*iter->hti_nextp)->field.hte_next) { \
|
||||
iter->hti_nextp = &(*iter->hti_nextp)->field.hte_next; \
|
||||
return; \
|
||||
} \
|
||||
while (++iter->hti_bucket < head->hth_table_length) { \
|
||||
iter->hti_nextp = &head->hth_table[iter->hti_bucket]; \
|
||||
if (*iter->hti_nextp) \
|
||||
return; \
|
||||
++iter->hti_bucket; \
|
||||
} \
|
||||
iter->hti_nextp = NULL; \
|
||||
} \
|
||||
static __inline void \
|
||||
name##_HT_ITER_NEXT_RMV(struct name *head, struct type##_ITER *iter) \
|
||||
{ \
|
||||
if (!iter->hti_nextp) \
|
||||
return; \
|
||||
--head->hth_n_entries; \
|
||||
if ((*iter->hti_nextp)->field.hte_next) { \
|
||||
*iter->hti_nextp = (*iter->hti_nextp)->field.hte_next; \
|
||||
if (*iter->hti_nextp) \
|
||||
return; \
|
||||
} \
|
||||
while (++iter->hti_bucket < head->hth_table_length) { \
|
||||
iter->hti_nextp = &head->hth_table[iter->hti_bucket]; \
|
||||
if (*iter->hti_nextp) \
|
||||
return; \
|
||||
++iter->hti_bucket; \
|
||||
} \
|
||||
iter->hti_nextp = NULL; \
|
||||
}
|
||||
#endif
|
||||
|
||||
#define HT_GENERATE(name, type, field, hashfn, eqfn, load, mallocfn, reallocfn, freefn) \
|
||||
static unsigned name##_PRIMES[] = { \
|
||||
53, 97, 193, 389, \
|
||||
769, 1543, 3079, 6151, \
|
||||
12289, 24593, 49157, 98317, \
|
||||
196613, 393241, 786433, 1572869, \
|
||||
3145739, 6291469, 12582917, 25165843, \
|
||||
50331653, 100663319, 201326611, 402653189, \
|
||||
805306457, 1610612741 \
|
||||
}; \
|
||||
static unsigned name##_N_PRIMES = \
|
||||
sizeof(name##_PRIMES)/sizeof(name##_PRIMES[0]); \
|
||||
/* Expand the internal table of 'head' until it is large enough to \
|
||||
* hold 'size' elements. Return 0 on success, -1 on allocation \
|
||||
* failure. */ \
|
||||
int \
|
||||
name##_HT_GROW(struct name *head, unsigned size) \
|
||||
{ \
|
||||
unsigned new_len, new_load_limit; \
|
||||
int prime_idx; \
|
||||
struct type **new_table; \
|
||||
if (head->hth_prime_idx == (int)name##_N_PRIMES - 1) \
|
||||
return 0; \
|
||||
if (head->hth_load_limit > size) \
|
||||
return 0; \
|
||||
prime_idx = head->hth_prime_idx; \
|
||||
do { \
|
||||
new_len = name##_PRIMES[++prime_idx]; \
|
||||
new_load_limit = (unsigned)(load*new_len); \
|
||||
} while (new_load_limit <= size && \
|
||||
prime_idx < (int)name##_N_PRIMES); \
|
||||
if ((new_table = mallocfn(new_len*sizeof(struct type*)))) { \
|
||||
unsigned b; \
|
||||
memset(new_table, 0, new_len*sizeof(struct type*)); \
|
||||
for (b = 0; b < head->hth_table_length; ++b) { \
|
||||
struct type *elm, *next; \
|
||||
unsigned b2; \
|
||||
elm = head->hth_table[b]; \
|
||||
while (elm) { \
|
||||
next = elm->field.hte_next; \
|
||||
b2 = elm->field.hte_hash % new_len; \
|
||||
elm->field.hte_next = new_table[b2]; \
|
||||
new_table[b2] = elm; \
|
||||
elm = next; \
|
||||
} \
|
||||
} \
|
||||
freefn(head->hth_table); \
|
||||
head->hth_table = new_table; \
|
||||
} else { \
|
||||
unsigned b, b2; \
|
||||
new_table = reallocfn(head->hth_table, new_len*sizeof(struct type*)); \
|
||||
if (!new_table) return -1; \
|
||||
memset(new_table + head->hth_table_length, 0, \
|
||||
(new_len - head->hth_table_length)*sizeof(struct type*)); \
|
||||
for (b=0; b < head->hth_table_length; ++b) { \
|
||||
struct type *e, **pE; \
|
||||
for (pE = &new_table[b], e = *pE; e != NULL; e = *pE) { \
|
||||
b2 = e->field.hte_hash % new_len; \
|
||||
if (b2 == b) { \
|
||||
pE = &e->field.hte_next; \
|
||||
} else { \
|
||||
*pE = e->field.hte_next; \
|
||||
e->field.hte_next = new_table[b2]; \
|
||||
new_table[b2] = e; \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
head->hth_table = new_table; \
|
||||
} \
|
||||
head->hth_table_length = new_len; \
|
||||
head->hth_prime_idx = prime_idx; \
|
||||
head->hth_load_limit = new_load_limit; \
|
||||
return 0; \
|
||||
} \
|
||||
/* Free all storage held by 'head'. Does not free 'head' itself, or \
|
||||
* individual elements. */ \
|
||||
void \
|
||||
name##_HT_CLEAR(struct name *head) \
|
||||
{ \
|
||||
if (head->hth_table) \
|
||||
freefn(head->hth_table); \
|
||||
head->hth_table_length = 0; \
|
||||
HT_INIT(head); \
|
||||
} \
|
||||
/* Debugging helper: return true iff the representation of 'head' is \
|
||||
* internally consistent. */ \
|
||||
int \
|
||||
_##name##_HT_REP_OK(struct name *head) \
|
||||
{ \
|
||||
unsigned n, i; \
|
||||
struct type *elm; \
|
||||
if (!head->hth_table_length) { \
|
||||
return !head->hth_table && !head->hth_n_entries && \
|
||||
!head->hth_load_limit && head->hth_prime_idx == -1; \
|
||||
} \
|
||||
if (!head->hth_table || head->hth_prime_idx < 0 || \
|
||||
!head->hth_load_limit) \
|
||||
return 0; \
|
||||
if (head->hth_n_entries > head->hth_load_limit) \
|
||||
return 0; \
|
||||
if (head->hth_table_length != name##_PRIMES[head->hth_prime_idx]) \
|
||||
return 0; \
|
||||
if (head->hth_load_limit != (unsigned)(load*head->hth_table_length)) \
|
||||
return 0; \
|
||||
for (n = i = 0; i < head->hth_table_length; ++i) { \
|
||||
for (elm = head->hth_table[i]; elm; elm = elm->field.hte_next) { \
|
||||
if (elm->field.hte_hash != hashfn(elm)) \
|
||||
return 0; \
|
||||
if ((elm->field.hte_hash % head->hth_table_length) != i) \
|
||||
return 0; \
|
||||
++n; \
|
||||
} \
|
||||
} \
|
||||
if (n != head->hth_n_entries) \
|
||||
return 0; \
|
||||
return 1; \
|
||||
}
|
||||
|
||||
/*
|
||||
* Copyright 2005, Nick Mathewson. Implementation logic is adapted from code
|
||||
* by Cristopher Clark, retrofit to allow drop-in memory management, and to
|
||||
* use the same interface as Niels Provos's HT_H. I'm not sure whether this
|
||||
* is a derived work any more, but whether it is or not, the license below
|
||||
* applies.
|
||||
*
|
||||
* Copyright (c) 2002, Christopher Clark
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of the original author; nor the names of any contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
||||
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#endif
|
||||
|
@ -12,10 +12,7 @@ const char circuitlist_c_id[] = "$Id$";
|
||||
|
||||
#include "or.h"
|
||||
|
||||
/* Define RB_AUGMENT to avoid warnings about if statements with emtpy bodies.
|
||||
*/
|
||||
#define RB_AUGMENT(x) do{}while(0)
|
||||
#include "tree.h"
|
||||
#include "../common/ht.h"
|
||||
|
||||
/********* START VARIABLES **********/
|
||||
|
||||
@ -31,30 +28,34 @@ static void circuit_free_cpath_node(crypt_path_t *victim);
|
||||
/** A map from OR connection and circuit ID to circuit. (Lookup performance is
|
||||
* very important here, since we need to do it every time a cell arrives.) */
|
||||
typedef struct orconn_circid_circuit_map_t {
|
||||
RB_ENTRY(orconn_circid_circuit_map_t) node;
|
||||
HT_ENTRY(orconn_circid_circuit_map_t) node;
|
||||
connection_t *or_conn;
|
||||
uint16_t circ_id;
|
||||
circuit_t *circuit;
|
||||
} orconn_circid_circuit_map_t;
|
||||
|
||||
/** Helper for RB tree: compare the OR connection and circuit ID for a and b,
|
||||
/** Helper for hash tables: compare the OR connection and circuit ID for a and b,
|
||||
* and return less than, equal to, or greater than zero appropriately.
|
||||
*/
|
||||
static INLINE int
|
||||
compare_orconn_circid_entries(orconn_circid_circuit_map_t *a,
|
||||
orconn_circid_circuit_map_t *b)
|
||||
_orconn_circid_entries_eq(orconn_circid_circuit_map_t *a,
|
||||
orconn_circid_circuit_map_t *b)
|
||||
{
|
||||
if (a->or_conn < b->or_conn)
|
||||
return -1;
|
||||
else if (a->or_conn > b->or_conn)
|
||||
return 1;
|
||||
else
|
||||
return ((int)b->circ_id) - ((int)a->circ_id);
|
||||
};
|
||||
return a->or_conn == b->or_conn && a->circ_id == b->circ_id;
|
||||
}
|
||||
|
||||
static RB_HEAD(orconn_circid_tree, orconn_circid_circuit_map_t) orconn_circid_circuit_map = RB_INITIALIZER(orconn_circid_circuit_map);
|
||||
RB_PROTOTYPE(orconn_circid_tree, orconn_circid_circuit_map_t, node, compare_orconn_circid_entries);
|
||||
RB_GENERATE(orconn_circid_tree, orconn_circid_circuit_map_t, node, compare_orconn_circid_entries);
|
||||
static INLINE unsigned int
|
||||
_orconn_circid_entry_hash(orconn_circid_circuit_map_t *a)
|
||||
{
|
||||
return (((unsigned)a->circ_id)<<16) ^ (unsigned)(uintptr_t)(a->or_conn);
|
||||
}
|
||||
|
||||
static HT_HEAD(orconn_circid_tree, orconn_circid_circuit_map_t) orconn_circid_circuit_map = HT_INITIALIZER();
|
||||
HT_PROTOTYPE(orconn_circid_tree, orconn_circid_circuit_map_t, node,
|
||||
_orconn_circid_entry_hash, _orconn_circid_entries_eq);
|
||||
HT_GENERATE(orconn_circid_tree, orconn_circid_circuit_map_t, node,
|
||||
_orconn_circid_entry_hash, _orconn_circid_entries_eq, 0.6,
|
||||
malloc, realloc, free);
|
||||
|
||||
/** The most recently returned entry from circuit_get_by_circid_orconn;
|
||||
* used to improve performance when many cells arrive in a row from the
|
||||
@ -102,11 +103,10 @@ circuit_set_circid_orconn(circuit_t *circ, uint16_t id,
|
||||
if (old_conn) { /* we may need to remove it from the conn-circid map */
|
||||
search.circ_id = old_id;
|
||||
search.or_conn = old_conn;
|
||||
found = RB_FIND(orconn_circid_tree, &orconn_circid_circuit_map, &search);
|
||||
found = HT_REMOVE(orconn_circid_tree, &orconn_circid_circuit_map, &search);
|
||||
if (found) {
|
||||
RB_REMOVE(orconn_circid_tree, &orconn_circid_circuit_map, found);
|
||||
tor_free(found);
|
||||
}
|
||||
tor_free(found);
|
||||
}
|
||||
|
||||
if (conn == NULL)
|
||||
@ -115,7 +115,7 @@ circuit_set_circid_orconn(circuit_t *circ, uint16_t id,
|
||||
/* now add the new one to the conn-circid map */
|
||||
search.circ_id = id;
|
||||
search.or_conn = conn;
|
||||
found = RB_FIND(orconn_circid_tree, &orconn_circid_circuit_map, &search);
|
||||
found = HT_FIND(orconn_circid_tree, &orconn_circid_circuit_map, &search);
|
||||
if (found) {
|
||||
found->circuit = circ;
|
||||
} else {
|
||||
@ -123,7 +123,7 @@ circuit_set_circid_orconn(circuit_t *circ, uint16_t id,
|
||||
found->circ_id = id;
|
||||
found->or_conn = conn;
|
||||
found->circuit = circ;
|
||||
RB_INSERT(orconn_circid_tree, &orconn_circid_circuit_map, found);
|
||||
HT_INSERT(orconn_circid_tree, &orconn_circid_circuit_map, found);
|
||||
}
|
||||
}
|
||||
|
||||
@ -358,7 +358,7 @@ circuit_get_by_circid_orconn_impl(uint16_t circ_id, connection_t *conn)
|
||||
} else {
|
||||
search.circ_id = circ_id;
|
||||
search.or_conn = conn;
|
||||
found = RB_FIND(orconn_circid_tree, &orconn_circid_circuit_map, &search);
|
||||
found = HT_FIND(orconn_circid_tree, &orconn_circid_circuit_map, &search);
|
||||
_last_circid_orconn_ent = found;
|
||||
}
|
||||
if (found && found->circuit)
|
||||
|
@ -890,8 +890,8 @@ addressmap_get_mappings(smartlist_t *sl, time_t min_expires, time_t max_expires)
|
||||
val = _val;
|
||||
if (val->expires >= min_expires && val->expires <= max_expires) {
|
||||
if (!sl) {
|
||||
addressmap_ent_remove(key, val);
|
||||
iter = strmap_iter_next_rmv(addressmap,iter);
|
||||
addressmap_ent_remove(key, val);
|
||||
continue;
|
||||
} else if (val->new_address) {
|
||||
size_t len = strlen(key)+strlen(val->new_address)+2;
|
||||
|
64
src/or/dns.c
64
src/or/dns.c
@ -18,7 +18,7 @@ const char dns_c_id[] = "$Id$";
|
||||
*/
|
||||
|
||||
#include "or.h"
|
||||
#include "tree.h"
|
||||
#include "../common/ht.h"
|
||||
|
||||
/** Longest hostname we're willing to resolve. */
|
||||
#define MAX_ADDRESSLEN 256
|
||||
@ -55,7 +55,7 @@ typedef struct pending_connection_t {
|
||||
* list from oldest to newest.
|
||||
*/
|
||||
typedef struct cached_resolve_t {
|
||||
SPLAY_ENTRY(cached_resolve_t) node;
|
||||
HT_ENTRY(cached_resolve_t) node;
|
||||
char address[MAX_ADDRESSLEN]; /**< The hostname to be resolved. */
|
||||
uint32_t addr; /**< IPv4 addr for <b>address</b>. */
|
||||
char state; /**< 0 is pending; 1 means answer is valid; 2 means resolve failed. */
|
||||
@ -77,26 +77,33 @@ static int spawn_enough_dnsworkers(void);
|
||||
static void send_resolved_cell(connection_t *conn, uint8_t answer_type);
|
||||
|
||||
/** Splay tree of cached_resolve objects. */
|
||||
static SPLAY_HEAD(cache_tree, cached_resolve_t) cache_root;
|
||||
static HT_HEAD(cache_tree, cached_resolve_t) cache_root;
|
||||
|
||||
/** Function to compare hashed resolves on their addresses; used to
|
||||
* implement splay trees. */
|
||||
static int
|
||||
compare_cached_resolves(cached_resolve_t *a,
|
||||
cached_resolve_t *b)
|
||||
static INLINE int
|
||||
cached_resolves_eq(cached_resolve_t *a, cached_resolve_t *b)
|
||||
{
|
||||
/* make this smarter one day? */
|
||||
return strncmp(a->address, b->address, MAX_ADDRESSLEN);
|
||||
return !strncmp(a->address, b->address, MAX_ADDRESSLEN);
|
||||
}
|
||||
|
||||
SPLAY_PROTOTYPE(cache_tree, cached_resolve_t, node, compare_cached_resolves);
|
||||
SPLAY_GENERATE(cache_tree, cached_resolve_t, node, compare_cached_resolves);
|
||||
static INLINE unsigned int
|
||||
cached_resolve_hash(cached_resolve_t *a)
|
||||
{
|
||||
return ht_string_hash(a->address);
|
||||
}
|
||||
|
||||
HT_PROTOTYPE(cache_tree, cached_resolve_t, node, cached_resolve_hash,
|
||||
cached_resolves_eq);
|
||||
HT_GENERATE(cache_tree, cached_resolve_t, node, cached_resolve_hash,
|
||||
cached_resolves_eq, 0.6, malloc, realloc, free);
|
||||
|
||||
/** Initialize the DNS cache. */
|
||||
static void
|
||||
init_cache_tree(void)
|
||||
{
|
||||
SPLAY_INIT(&cache_root);
|
||||
HT_INIT(&cache_root);
|
||||
}
|
||||
|
||||
/** Initialize the DNS subsystem; called by the OR process. */
|
||||
@ -123,12 +130,13 @@ _free_cached_resolve(cached_resolve_t *r)
|
||||
void
|
||||
dns_free_all(void)
|
||||
{
|
||||
cached_resolve_t *ptr, *next;
|
||||
for (ptr = SPLAY_MIN(cache_tree, &cache_root); ptr != NULL; ptr = next) {
|
||||
next = SPLAY_NEXT(cache_tree, &cache_root, ptr);
|
||||
SPLAY_REMOVE(cache_tree, &cache_root, ptr);
|
||||
_free_cached_resolve(ptr);
|
||||
cached_resolve_t **ptr, **next, *item;
|
||||
for (ptr = HT_START(cache_tree, &cache_root); ptr != NULL; ptr = next) {
|
||||
item = *ptr;
|
||||
next = HT_NEXT_RMV(cache_tree, &cache_root, ptr);
|
||||
_free_cached_resolve(item);
|
||||
}
|
||||
HT_CLEAR(cache_tree, &cache_root);
|
||||
}
|
||||
|
||||
/** Linked list of resolved addresses, oldest to newest. */
|
||||
@ -174,7 +182,7 @@ purge_expired_resolves(uint32_t now)
|
||||
oldest_cached_resolve = resolve->next;
|
||||
if (!oldest_cached_resolve) /* if there are no more, */
|
||||
newest_cached_resolve = NULL; /* then make sure the list's tail knows that too */
|
||||
SPLAY_REMOVE(cache_tree, &cache_root, resolve);
|
||||
HT_REMOVE(cache_tree, &cache_root, resolve);
|
||||
tor_free(resolve);
|
||||
}
|
||||
}
|
||||
@ -230,7 +238,7 @@ insert_resolve(cached_resolve_t *r)
|
||||
}
|
||||
newest_cached_resolve = r;
|
||||
|
||||
SPLAY_INSERT(cache_tree, &cache_root, r);
|
||||
HT_INSERT(cache_tree, &cache_root, r);
|
||||
}
|
||||
|
||||
/** See if we have a cache entry for <b>exitconn</b>-\>address. if so,
|
||||
@ -273,7 +281,7 @@ dns_resolve(connection_t *exitconn)
|
||||
|
||||
/* now check the tree to see if 'address' is already there. */
|
||||
strlcpy(search.address, exitconn->address, sizeof(search.address));
|
||||
resolve = SPLAY_FIND(cache_tree, &cache_root, &search);
|
||||
resolve = HT_FIND(cache_tree, &cache_root, &search);
|
||||
if (resolve) { /* already there */
|
||||
switch (resolve->state) {
|
||||
case CACHE_STATE_PENDING:
|
||||
@ -383,7 +391,7 @@ connection_dns_remove(connection_t *conn)
|
||||
|
||||
strlcpy(search.address, conn->address, sizeof(search.address));
|
||||
|
||||
resolve = SPLAY_FIND(cache_tree, &cache_root, &search);
|
||||
resolve = HT_FIND(cache_tree, &cache_root, &search);
|
||||
if (!resolve) {
|
||||
/* XXXX RD This *is* a bug, right? -NM */
|
||||
notice(LD_BUG,"Address '%s' is not pending. Dropping.", safe_str(conn->address));
|
||||
@ -422,10 +430,10 @@ void
|
||||
assert_connection_edge_not_dns_pending(connection_t *conn)
|
||||
{
|
||||
pending_connection_t *pend;
|
||||
cached_resolve_t *resolve;
|
||||
cached_resolve_t **resolve;
|
||||
|
||||
SPLAY_FOREACH(resolve, cache_tree, &cache_root) {
|
||||
for (pend = resolve->pending_connections;
|
||||
HT_FOREACH(resolve, cache_tree, &cache_root) {
|
||||
for (pend = (*resolve)->pending_connections;
|
||||
pend;
|
||||
pend = pend->next) {
|
||||
tor_assert(pend->conn != conn);
|
||||
@ -439,10 +447,10 @@ void
|
||||
assert_all_pending_dns_resolves_ok(void)
|
||||
{
|
||||
pending_connection_t *pend;
|
||||
cached_resolve_t *resolve;
|
||||
cached_resolve_t **resolve;
|
||||
|
||||
SPLAY_FOREACH(resolve, cache_tree, &cache_root) {
|
||||
for (pend = resolve->pending_connections;
|
||||
HT_FOREACH(resolve, cache_tree, &cache_root) {
|
||||
for (pend = (*resolve)->pending_connections;
|
||||
pend;
|
||||
pend = pend->next) {
|
||||
assert_connection_ok(pend->conn, 0);
|
||||
@ -467,7 +475,7 @@ dns_cancel_pending_resolve(char *address)
|
||||
|
||||
strlcpy(search.address, address, sizeof(search.address));
|
||||
|
||||
resolve = SPLAY_FIND(cache_tree, &cache_root, &search);
|
||||
resolve = HT_FIND(cache_tree, &cache_root, &search);
|
||||
if (!resolve) {
|
||||
/* XXXX RD This *is* a bug, right? -NM */
|
||||
notice(LD_BUG,"Address '%s' is not pending. Dropping.", safe_str(address));
|
||||
@ -529,7 +537,7 @@ dns_purge_resolve(cached_resolve_t *resolve)
|
||||
}
|
||||
|
||||
/* remove resolve from the tree */
|
||||
SPLAY_REMOVE(cache_tree, &cache_root, resolve);
|
||||
HT_REMOVE(cache_tree, &cache_root, resolve);
|
||||
|
||||
tor_free(resolve);
|
||||
}
|
||||
@ -552,7 +560,7 @@ dns_found_answer(char *address, uint32_t addr, char outcome)
|
||||
|
||||
strlcpy(search.address, address, sizeof(search.address));
|
||||
|
||||
resolve = SPLAY_FIND(cache_tree, &cache_root, &search);
|
||||
resolve = HT_FIND(cache_tree, &cache_root, &search);
|
||||
if (!resolve) {
|
||||
info(LD_EXIT,"Resolved unasked address '%s'; caching anyway.",
|
||||
safe_str(address));
|
||||
|
@ -358,8 +358,8 @@ rep_history_clean(time_t before)
|
||||
digestmap_iter_get(orhist_it, &d1, &or_history_p);
|
||||
or_history = or_history_p;
|
||||
if (or_history->changed < before) {
|
||||
free_or_history(or_history);
|
||||
orhist_it = digestmap_iter_next_rmv(history_map, orhist_it);
|
||||
free_or_history(or_history);
|
||||
continue;
|
||||
}
|
||||
for (lhist_it = digestmap_iter_init(or_history->link_history_map);
|
||||
@ -367,9 +367,9 @@ rep_history_clean(time_t before)
|
||||
digestmap_iter_get(lhist_it, &d2, &link_history_p);
|
||||
link_history = link_history_p;
|
||||
if (link_history->changed < before) {
|
||||
lhist_it = digestmap_iter_next_rmv(or_history->link_history_map,lhist_it);
|
||||
rephist_total_alloc -= sizeof(link_history_t);
|
||||
tor_free(link_history);
|
||||
lhist_it = digestmap_iter_next_rmv(or_history->link_history_map,lhist_it);
|
||||
continue;
|
||||
}
|
||||
lhist_it = digestmap_iter_next(or_history->link_history_map,lhist_it);
|
||||
|
@ -972,19 +972,6 @@ test_gzip(void)
|
||||
tor_free(buf1);
|
||||
}
|
||||
|
||||
static void *
|
||||
_squareAndRemoveK4(const char *key, void*val, void *data)
|
||||
{
|
||||
int *ip = (int*)data;
|
||||
intptr_t v;
|
||||
if (strcmp(key,"K4") == 0) {
|
||||
++(*ip);
|
||||
return NULL;
|
||||
}
|
||||
v = (intptr_t)val;
|
||||
return (void*)(v*v);
|
||||
}
|
||||
|
||||
static void
|
||||
test_strmap(void)
|
||||
{
|
||||
@ -1016,13 +1003,7 @@ test_strmap(void)
|
||||
strmap_set(map, "K5", (void*)104);
|
||||
strmap_set(map, "K6", (void*)105);
|
||||
|
||||
count = 0;
|
||||
strmap_foreach(map, _squareAndRemoveK4, &count);
|
||||
test_eq(count, 1);
|
||||
test_eq_ptr(strmap_get(map, "K4"), NULL);
|
||||
test_eq_ptr(strmap_get(map, "K1"), (void*)10000);
|
||||
test_eq_ptr(strmap_get(map, "K6"), (void*)11025);
|
||||
|
||||
#if 0
|
||||
iter = strmap_iter_init(map);
|
||||
strmap_iter_get(iter,&k,&v);
|
||||
test_streq(k, "K1");
|
||||
@ -1045,6 +1026,8 @@ test_strmap(void)
|
||||
/* Make sure we removed K2, but not the others. */
|
||||
test_eq_ptr(strmap_get(map, "K2"), NULL);
|
||||
test_eq_ptr(strmap_get(map, "K5"), (void*)10816);
|
||||
#endif
|
||||
|
||||
|
||||
/* Clean up after ourselves. */
|
||||
strmap_free(map, NULL);
|
||||
|
Loading…
Reference in New Issue
Block a user