Port over the last ht.h changes from libevent: avoid _reserved identifiers

This commit is contained in:
Nick Mathewson 2012-02-13 18:40:30 -05:00
parent 107f604f31
commit a31fb42d2e
2 changed files with 40 additions and 40 deletions

View File

@ -1027,7 +1027,7 @@ digestmap_set(digestmap_t *map, const char *key, void *val)
* the hash table that we do in the unoptimized code above. (Each of * the hash table that we do in the unoptimized code above. (Each of
* HT_INSERT and HT_FIND calls HT_SET_HASH and HT_FIND_P.) * HT_INSERT and HT_FIND calls HT_SET_HASH and HT_FIND_P.)
*/ */
_HT_FIND_OR_INSERT(digestmap_impl, node, digestmap_entry_hash, &(map->head), HT_FIND_OR_INSERT_(digestmap_impl, node, digestmap_entry_hash, &(map->head),
digestmap_entry_t, &search, ptr, digestmap_entry_t, &search, ptr,
{ {
/* we found an entry. */ /* we found an entry. */
@ -1041,7 +1041,7 @@ digestmap_set(digestmap_t *map, const char *key, void *val)
tor_malloc_zero(sizeof(digestmap_entry_t)); tor_malloc_zero(sizeof(digestmap_entry_t));
memcpy(newent->key, key, DIGEST_LEN); memcpy(newent->key, key, DIGEST_LEN);
newent->val = val; newent->val = val;
_HT_FOI_INSERT(node, &(map->head), &search, newent, ptr); HT_FOI_INSERT_(node, &(map->head), &search, newent, ptr);
return NULL; return NULL;
}); });
#endif #endif
@ -1354,14 +1354,14 @@ digestmap_free(digestmap_t *map, void (*free_val)(void*))
void void
strmap_assert_ok(const strmap_t *map) strmap_assert_ok(const strmap_t *map)
{ {
tor_assert(!_strmap_impl_HT_REP_IS_BAD(&map->head)); tor_assert(!strmap_impl_HT_REP_IS_BAD_(&map->head));
} }
/** Fail with an assertion error if anything has gone wrong with the internal /** Fail with an assertion error if anything has gone wrong with the internal
* representation of <b>map</b>. */ * representation of <b>map</b>. */
void void
digestmap_assert_ok(const digestmap_t *map) digestmap_assert_ok(const digestmap_t *map)
{ {
tor_assert(!_digestmap_impl_HT_REP_IS_BAD(&map->head)); tor_assert(!digestmap_impl_HT_REP_IS_BAD_(&map->head));
} }
/** Return true iff <b>map</b> has no entries. */ /** Return true iff <b>map</b> has no entries. */

View File

@ -102,24 +102,24 @@ ht_string_hash(const char *s)
} }
#ifndef HT_NO_CACHE_HASH_VALUES #ifndef HT_NO_CACHE_HASH_VALUES
#define _HT_SET_HASH(elm, field, hashfn) \ #define HT_SET_HASH_(elm, field, hashfn) \
do { (elm)->field.hte_hash = hashfn(elm); } while (0) do { (elm)->field.hte_hash = hashfn(elm); } while (0)
#define _HT_SET_HASHVAL(elm, field, val) \ #define HT_SET_HASHVAL_(elm, field, val) \
do { (elm)->field.hte_hash = (val); } while (0) do { (elm)->field.hte_hash = (val); } while (0)
#define _HT_ELT_HASH(elm, field, hashfn) \ #define HT_ELT_HASH_(elm, field, hashfn) \
((elm)->field.hte_hash) ((elm)->field.hte_hash)
#else #else
#define _HT_SET_HASH(elm, field, hashfn) \ #define HT_SET_HASH_(elm, field, hashfn) \
((void)0) ((void)0)
#define _HT_ELT_HASH(elm, field, hashfn) \ #define HT_ELT_HASH_(elm, field, hashfn) \
(hashfn(elm)) (hashfn(elm))
#define _HT_SET_HASHVAL(elm, field, val) \ #define HT_SET_HASHVAL_(elm, field, val) \
((void)0) ((void)0)
#endif #endif
/* Helper: alias for the bucket containing 'elm'. */ /* Helper: alias for the bucket containing 'elm'. */
#define _HT_BUCKET(head, field, elm, hashfn) \ #define HT_BUCKET_(head, field, elm, hashfn) \
((head)->hth_table[_HT_ELT_HASH(elm,field,hashfn) % head->hth_table_length]) ((head)->hth_table[HT_ELT_HASH_(elm,field,hashfn) % head->hth_table_length])
#define HT_FOREACH(x, name, head) \ #define HT_FOREACH(x, name, head) \
for ((x) = HT_START(name, head); \ for ((x) = HT_START(name, head); \
@ -129,7 +129,7 @@ ht_string_hash(const char *s)
#define HT_PROTOTYPE(name, type, field, hashfn, eqfn) \ #define HT_PROTOTYPE(name, type, field, hashfn, eqfn) \
int name##_HT_GROW(struct name *ht, unsigned min_capacity); \ int name##_HT_GROW(struct name *ht, unsigned min_capacity); \
void name##_HT_CLEAR(struct name *ht); \ void name##_HT_CLEAR(struct name *ht); \
int _##name##_HT_REP_IS_BAD(const struct name *ht); \ int name##_HT_REP_IS_BAD_(const struct name *ht); \
static INLINE void \ static INLINE void \
name##_HT_INIT(struct name *head) { \ name##_HT_INIT(struct name *head) { \
head->hth_table_length = 0; \ head->hth_table_length = 0; \
@ -141,12 +141,12 @@ ht_string_hash(const char *s)
/* Helper: returns a pointer to the right location in the table \ /* Helper: returns a pointer to the right location in the table \
* 'head' to find or insert the element 'elm'. */ \ * 'head' to find or insert the element 'elm'. */ \
static INLINE struct type ** \ static INLINE struct type ** \
_##name##_HT_FIND_P(struct name *head, struct type *elm) \ name##_HT_FIND_P_(struct name *head, struct type *elm) \
{ \ { \
struct type **p; \ struct type **p; \
if (!head->hth_table) \ if (!head->hth_table) \
return NULL; \ return NULL; \
p = &_HT_BUCKET(head, field, elm, hashfn); \ p = &HT_BUCKET_(head, field, elm, hashfn); \
while (*p) { \ while (*p) { \
if (eqfn(*p, elm)) \ if (eqfn(*p, elm)) \
return p; \ return p; \
@ -161,8 +161,8 @@ ht_string_hash(const char *s)
{ \ { \
struct type **p; \ struct type **p; \
struct name *h = (struct name *) head; \ struct name *h = (struct name *) head; \
_HT_SET_HASH(elm, field, hashfn); \ HT_SET_HASH_(elm, field, hashfn); \
p = _##name##_HT_FIND_P(h, elm); \ p = name##_HT_FIND_P_(h, elm); \
return p ? *p : NULL; \ return p ? *p : NULL; \
} \ } \
/* Insert the element 'elm' into the table 'head'. Do not call this \ /* Insert the element 'elm' into the table 'head'. Do not call this \
@ -174,8 +174,8 @@ ht_string_hash(const char *s)
if (!head->hth_table || head->hth_n_entries >= head->hth_load_limit) \ if (!head->hth_table || head->hth_n_entries >= head->hth_load_limit) \
name##_HT_GROW(head, head->hth_n_entries+1); \ name##_HT_GROW(head, head->hth_n_entries+1); \
++head->hth_n_entries; \ ++head->hth_n_entries; \
_HT_SET_HASH(elm, field, hashfn); \ HT_SET_HASH_(elm, field, hashfn); \
p = &_HT_BUCKET(head, field, elm, hashfn); \ p = &HT_BUCKET_(head, field, elm, hashfn); \
elm->field.hte_next = *p; \ elm->field.hte_next = *p; \
*p = elm; \ *p = elm; \
} \ } \
@ -188,8 +188,8 @@ ht_string_hash(const char *s)
struct type **p, *r; \ struct type **p, *r; \
if (!head->hth_table || head->hth_n_entries >= head->hth_load_limit) \ if (!head->hth_table || head->hth_n_entries >= head->hth_load_limit) \
name##_HT_GROW(head, head->hth_n_entries+1); \ name##_HT_GROW(head, head->hth_n_entries+1); \
_HT_SET_HASH(elm, field, hashfn); \ HT_SET_HASH_(elm, field, hashfn); \
p = _##name##_HT_FIND_P(head, elm); \ p = name##_HT_FIND_P_(head, elm); \
r = *p; \ r = *p; \
*p = elm; \ *p = elm; \
if (r && (r!=elm)) { \ if (r && (r!=elm)) { \
@ -207,8 +207,8 @@ ht_string_hash(const char *s)
name##_HT_REMOVE(struct name *head, struct type *elm) \ name##_HT_REMOVE(struct name *head, struct type *elm) \
{ \ { \
struct type **p, *r; \ struct type **p, *r; \
_HT_SET_HASH(elm, field, hashfn); \ HT_SET_HASH_(elm, field, hashfn); \
p = _##name##_HT_FIND_P(head,elm); \ p = name##_HT_FIND_P_(head,elm); \
if (!p || !*p) \ if (!p || !*p) \
return NULL; \ return NULL; \
r = *p; \ r = *p; \
@ -269,7 +269,7 @@ ht_string_hash(const char *s)
if ((*elm)->field.hte_next) { \ if ((*elm)->field.hte_next) { \
return &(*elm)->field.hte_next; \ return &(*elm)->field.hte_next; \
} else { \ } else { \
unsigned b = (_HT_ELT_HASH(*elm, field, hashfn) % head->hth_table_length)+1; \ unsigned b = (HT_ELT_HASH_(*elm, field, hashfn) % head->hth_table_length)+1; \
while (b < head->hth_table_length) { \ while (b < head->hth_table_length) { \
if (head->hth_table[b]) \ if (head->hth_table[b]) \
return &head->hth_table[b]; \ return &head->hth_table[b]; \
@ -281,7 +281,7 @@ ht_string_hash(const char *s)
static INLINE struct type ** \ static INLINE struct type ** \
name##_HT_NEXT_RMV(struct name *head, struct type **elm) \ name##_HT_NEXT_RMV(struct name *head, struct type **elm) \
{ \ { \
unsigned h = _HT_ELT_HASH(*elm, field, hashfn); \ unsigned h = HT_ELT_HASH_(*elm, field, hashfn); \
*elm = (*elm)->field.hte_next; \ *elm = (*elm)->field.hte_next; \
--head->hth_n_entries; \ --head->hth_n_entries; \
if (*elm) { \ if (*elm) { \
@ -338,7 +338,7 @@ ht_string_hash(const char *s)
elm = head->hth_table[b]; \ elm = head->hth_table[b]; \
while (elm) { \ while (elm) { \
next = elm->field.hte_next; \ next = elm->field.hte_next; \
b2 = _HT_ELT_HASH(elm, field, hashfn) % new_len; \ b2 = HT_ELT_HASH_(elm, field, hashfn) % new_len; \
elm->field.hte_next = new_table[b2]; \ elm->field.hte_next = new_table[b2]; \
new_table[b2] = elm; \ new_table[b2] = elm; \
elm = next; \ elm = next; \
@ -356,7 +356,7 @@ ht_string_hash(const char *s)
for (b=0; b < head->hth_table_length; ++b) { \ for (b=0; b < head->hth_table_length; ++b) { \
struct type *e, **pE; \ struct type *e, **pE; \
for (pE = &new_table[b], e = *pE; e != NULL; e = *pE) { \ for (pE = &new_table[b], e = *pE; e != NULL; e = *pE) { \
b2 = _HT_ELT_HASH(e, field, hashfn) % new_len; \ b2 = HT_ELT_HASH_(e, field, hashfn) % new_len; \
if (b2 == b) { \ if (b2 == b) { \
pE = &e->field.hte_next; \ pE = &e->field.hte_next; \
} else { \ } else { \
@ -386,7 +386,7 @@ ht_string_hash(const char *s)
/* Debugging helper: return false iff the representation of 'head' is \ /* Debugging helper: return false iff the representation of 'head' is \
* internally consistent. */ \ * internally consistent. */ \
int \ int \
_##name##_HT_REP_IS_BAD(const struct name *head) \ name##_HT_REP_IS_BAD_(const struct name *head) \
{ \ { \
unsigned n, i; \ unsigned n, i; \
struct type *elm; \ struct type *elm; \
@ -408,9 +408,9 @@ ht_string_hash(const char *s)
return 5; \ return 5; \
for (n = i = 0; i < head->hth_table_length; ++i) { \ for (n = i = 0; i < head->hth_table_length; ++i) { \
for (elm = head->hth_table[i]; elm; elm = elm->field.hte_next) { \ for (elm = head->hth_table[i]; elm; elm = elm->field.hte_next) { \
if (_HT_ELT_HASH(elm, field, hashfn) != hashfn(elm)) \ if (HT_ELT_HASH_(elm, field, hashfn) != hashfn(elm)) \
return 1000 + i; \ return 1000 + i; \
if ((_HT_ELT_HASH(elm, field, hashfn) % head->hth_table_length) != i) \ if ((HT_ELT_HASH_(elm, field, hashfn) % head->hth_table_length) != i) \
return 10000 + i; \ return 10000 + i; \
++n; \ ++n; \
} \ } \
@ -423,24 +423,24 @@ ht_string_hash(const char *s)
/** Implements an over-optimized "find and insert if absent" block; /** Implements an over-optimized "find and insert if absent" block;
* not meant for direct usage by typical code, or usage outside the critical * not meant for direct usage by typical code, or usage outside the critical
* path.*/ * path.*/
#define _HT_FIND_OR_INSERT(name, field, hashfn, head, eltype, elm, var, y, n) \ #define HT_FIND_OR_INSERT_(name, field, hashfn, head, eltype, elm, var, y, n) \
{ \ { \
struct name *_##var##_head = head; \ struct name *var##_head_ = head; \
struct eltype **var; \ struct eltype **var; \
if (!_##var##_head->hth_table || \ if (!var##_head_->hth_table || \
_##var##_head->hth_n_entries >= _##var##_head->hth_load_limit) \ var##_head_->hth_n_entries >= var##_head_->hth_load_limit) \
name##_HT_GROW(_##var##_head, _##var##_head->hth_n_entries+1); \ name##_HT_GROW(var##_head_, var##_head_->hth_n_entries+1); \
_HT_SET_HASH((elm), field, hashfn); \ HT_SET_HASH_((elm), field, hashfn); \
var = _##name##_HT_FIND_P(_##var##_head, (elm)); \ var = name##_HT_FIND_P_(var##_head_, (elm)); \
if (*var) { \ if (*var) { \
y; \ y; \
} else { \ } else { \
n; \ n; \
} \ } \
} }
#define _HT_FOI_INSERT(field, head, elm, newent, var) \ #define HT_FOI_INSERT_(field, head, elm, newent, var) \
{ \ { \
_HT_SET_HASHVAL(newent, field, (elm)->field.hte_hash); \ HT_SET_HASHVAL_(newent, field, (elm)->field.hte_hash); \
newent->field.hte_next = NULL; \ newent->field.hte_next = NULL; \
*var = newent; \ *var = newent; \
++((head)->hth_n_entries); \ ++((head)->hth_n_entries); \