aboutsummaryrefslogtreecommitdiff
path: root/ht-internal.h
diff options
context:
space:
mode:
Diffstat (limited to 'ht-internal.h')
-rw-r--r--ht-internal.h97
1 files changed, 47 insertions, 50 deletions
diff --git a/ht-internal.h b/ht-internal.h
index 50375bb..4673825 100644
--- a/ht-internal.h
+++ b/ht-internal.h
@@ -1,12 +1,12 @@
-/* Copyright 2002 Christopher Clark */
+/* Based on work 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 HT_INTERNAL_H_INCLUDED_
-#define HT_INTERNAL_H_INCLUDED_
+#ifndef _EVENT_HT_H
+#define _EVENT_HT_H
#define HT_HEAD(name, type) \
struct name { \
@@ -25,16 +25,16 @@
#define HT_INITIALIZER() \
{ NULL, 0, 0, 0, -1 }
-#ifdef HT_NO_CACHE_HASH_VALUES
+#ifdef HT_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,10 +45,6 @@
#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))
@@ -60,7 +56,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 */
@@ -74,7 +70,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;
@@ -88,7 +84,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;
@@ -101,25 +97,25 @@ ht_string_hash_(const char *s)
return h;
}
-#ifndef HT_NO_CACHE_HASH_VALUES
-#define HT_SET_HASH_(elm, field, hashfn) \
+#ifdef HT_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); \
@@ -129,7 +125,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; \
@@ -141,12 +137,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; \
@@ -161,8 +157,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 \
@@ -174,8 +170,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; \
} \
@@ -188,8 +184,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)) { \
@@ -207,8 +203,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; \
@@ -269,7 +265,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]; \
@@ -281,7 +277,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) { \
@@ -338,7 +334,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; \
@@ -356,7 +352,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 { \
@@ -380,12 +376,13 @@ ht_string_hash_(const char *s)
{ \
if (head->hth_table) \
freefn(head->hth_table); \
+ head->hth_table_length = 0; \
name##_HT_INIT(head); \
} \
/* 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; \
@@ -407,9 +404,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; \
} \
@@ -422,24 +419,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); \
@@ -447,7 +444,7 @@ ht_string_hash_(const char *s)
/*
* Copyright 2005, Nick Mathewson. Implementation logic is adapted from code
- * by Christopher Clark, retrofit to allow drop-in memory management, and to
+ * by Cristopher 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.
*