diff options
Diffstat (limited to 'ht-internal.h')
-rw-r--r-- | ht-internal.h | 96 |
1 files changed, 50 insertions, 46 deletions
diff --git a/ht-internal.h b/ht-internal.h index 46738251..8842f4cd 100644 --- a/ht-internal.h +++ b/ht-internal.h @@ -1,12 +1,12 @@ -/* Based on work Copyright 2002 Christopher Clark */ +/* Copyright 2002 Christopher Clark */ /* Copyright 2005-2012 Nick Mathewson */ /* Copyright 2009-2012 Niels Provos and Nick Mathewson */ /* See license at end. */ /* Based on ideas by Christopher Clark and interfaces from Niels Provos. */ -#ifndef _EVENT_HT_H -#define _EVENT_HT_H +#ifndef HT_INTERNAL_H_INCLUDED_ +#define HT_INTERNAL_H_INCLUDED_ #define HT_HEAD(name, type) \ struct name { \ @@ -25,16 +25,16 @@ #define HT_INITIALIZER() \ { NULL, 0, 0, 0, -1 } -#ifdef HT_CACHE_HASH_VALUES +#ifdef HT_NO_CACHE_HASH_VALUES #define HT_ENTRY(type) \ struct { \ struct type *hte_next; \ - unsigned hte_hash; \ } #else #define HT_ENTRY(type) \ struct { \ struct type *hte_next; \ + unsigned hte_hash; \ } #endif @@ -45,6 +45,10 @@ #define HT_SIZE(head) \ ((head)->hth_n_entries) +/* Return memory usage for a hashtable (not counting the entries themselves) */ +#define HT_MEM_USAGE(head) \ + (sizeof(*head) + (head)->hth_table_length * sizeof(void*)) + #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)) @@ -56,7 +60,7 @@ #define HT_INIT(name, head) name##_HT_INIT(head) /* Helper: */ static inline unsigned -ht_improve_hash(unsigned h) +ht_improve_hash_(unsigned h) { /* Aim to protect against poor hash functions by adding logic here * - logic taken from java 1.4 hashtable source */ @@ -70,7 +74,7 @@ ht_improve_hash(unsigned h) #if 0 /** Basic string hash function, from Java standard String.hashCode(). */ static inline unsigned -ht_string_hash(const char *s) +ht_string_hash_(const char *s) { unsigned h = 0; int m = 1; @@ -84,7 +88,7 @@ ht_string_hash(const char *s) /** Basic string hash function, from Python's str.__hash__() */ static inline unsigned -ht_string_hash(const char *s) +ht_string_hash_(const char *s) { unsigned h; const unsigned char *cp = (const unsigned char *)s; @@ -97,25 +101,25 @@ ht_string_hash(const char *s) return h; } -#ifdef HT_CACHE_HASH_VALUES -#define _HT_SET_HASH(elm, field, hashfn) \ +#ifndef HT_NO_CACHE_HASH_VALUES +#define HT_SET_HASH_(elm, field, hashfn) \ 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) -#define _HT_ELT_HASH(elm, field, hashfn) \ +#define HT_ELT_HASH_(elm, field, hashfn) \ ((elm)->field.hte_hash) #else -#define _HT_SET_HASH(elm, field, hashfn) \ +#define HT_SET_HASH_(elm, field, hashfn) \ ((void)0) -#define _HT_ELT_HASH(elm, field, hashfn) \ +#define HT_ELT_HASH_(elm, field, hashfn) \ (hashfn(elm)) -#define _HT_SET_HASHVAL(elm, field, val) \ +#define HT_SET_HASHVAL_(elm, field, val) \ ((void)0) #endif /* Helper: alias for the bucket containing 'elm'. */ -#define _HT_BUCKET(head, field, elm, hashfn) \ - ((head)->hth_table[_HT_ELT_HASH(elm,field,hashfn) % head->hth_table_length]) +#define HT_BUCKET_(head, field, elm, hashfn) \ + ((head)->hth_table[HT_ELT_HASH_(elm,field,hashfn) % head->hth_table_length]) #define HT_FOREACH(x, name, head) \ for ((x) = HT_START(name, head); \ @@ -125,7 +129,7 @@ ht_string_hash(const char *s) #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_IS_BAD(const struct name *ht); \ + int name##_HT_REP_IS_BAD_(const struct name *ht); \ static inline void \ name##_HT_INIT(struct name *head) { \ head->hth_table_length = 0; \ @@ -137,12 +141,12 @@ ht_string_hash(const char *s) /* 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) \ + 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, hashfn); \ + p = &HT_BUCKET_(head, field, elm, hashfn); \ while (*p) { \ if (eqfn(*p, elm)) \ return p; \ @@ -157,8 +161,8 @@ ht_string_hash(const char *s) { \ struct type **p; \ struct name *h = (struct name *) head; \ - _HT_SET_HASH(elm, field, hashfn); \ - p = _##name##_HT_FIND_P(h, elm); \ + HT_SET_HASH_(elm, field, hashfn); \ + p = name##_HT_FIND_P_(h, elm); \ return p ? *p : NULL; \ } \ /* Insert the element 'elm' into the table 'head'. Do not call this \ @@ -170,8 +174,8 @@ ht_string_hash(const char *s) 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, hashfn); \ + HT_SET_HASH_(elm, field, hashfn); \ + p = &HT_BUCKET_(head, field, elm, hashfn); \ elm->field.hte_next = *p; \ *p = elm; \ } \ @@ -184,8 +188,8 @@ ht_string_hash(const char *s) 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); \ + HT_SET_HASH_(elm, field, hashfn); \ + p = name##_HT_FIND_P_(head, elm); \ r = *p; \ *p = elm; \ if (r && (r!=elm)) { \ @@ -203,8 +207,8 @@ ht_string_hash(const char *s) 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); \ + HT_SET_HASH_(elm, field, hashfn); \ + p = name##_HT_FIND_P_(head,elm); \ if (!p || !*p) \ return NULL; \ r = *p; \ @@ -265,7 +269,7 @@ ht_string_hash(const char *s) if ((*elm)->field.hte_next) { \ return &(*elm)->field.hte_next; \ } 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) { \ if (head->hth_table[b]) \ return &head->hth_table[b]; \ @@ -277,7 +281,7 @@ ht_string_hash(const char *s) static inline struct type ** \ 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; \ --head->hth_n_entries; \ if (*elm) { \ @@ -334,7 +338,7 @@ ht_string_hash(const char *s) elm = head->hth_table[b]; \ while (elm) { \ 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]; \ new_table[b2] = elm; \ elm = next; \ @@ -352,7 +356,7 @@ ht_string_hash(const char *s) 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 = _HT_ELT_HASH(e, field, hashfn) % new_len; \ + b2 = HT_ELT_HASH_(e, field, hashfn) % new_len; \ if (b2 == b) { \ pE = &e->field.hte_next; \ } else { \ @@ -382,7 +386,7 @@ ht_string_hash(const char *s) /* Debugging helper: return false iff the representation of 'head' is \ * internally consistent. */ \ int \ - _##name##_HT_REP_IS_BAD(const struct name *head) \ + name##_HT_REP_IS_BAD_(const struct name *head) \ { \ unsigned n, i; \ struct type *elm; \ @@ -404,9 +408,9 @@ ht_string_hash(const char *s) return 5; \ for (n = i = 0; i < head->hth_table_length; ++i) { \ 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; \ - 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; \ ++n; \ } \ @@ -419,24 +423,24 @@ ht_string_hash(const char *s) /** Implements an over-optimized "find and insert if absent" block; * not meant for direct usage by typical code, or usage outside the critical * 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 eltype **var; \ - if (!_##var##_head->hth_table || \ - _##var##_head->hth_n_entries >= _##var##_head->hth_load_limit) \ - name##_HT_GROW(_##var##_head, _##var##_head->hth_n_entries+1); \ - _HT_SET_HASH((elm), field, hashfn); \ - var = _##name##_HT_FIND_P(_##var##_head, (elm)); \ + struct name *var##_head_ = head; \ + struct eltype **var; \ + if (!var##_head_->hth_table || \ + var##_head_->hth_n_entries >= var##_head_->hth_load_limit) \ + name##_HT_GROW(var##_head_, var##_head_->hth_n_entries+1); \ + HT_SET_HASH_((elm), field, hashfn); \ + var = name##_HT_FIND_P_(var##_head_, (elm)); \ if (*var) { \ y; \ } else { \ 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; \ *var = newent; \ ++((head)->hth_n_entries); \ @@ -444,7 +448,7 @@ ht_string_hash(const char *s) /* * Copyright 2005, Nick Mathewson. Implementation logic is adapted from code - * by Cristopher Clark, retrofit to allow drop-in memory management, and to + * by Christopher Clark, retrofit to allow drop-in memory management, and to * use the same interface as Niels Provos's tree.h. This is probably still * a derived work, so the original license below still applies. * |