14900c5f9SNick Mathewson /* Copyright 2002 Christopher Clark */
2e49e2891SNick Mathewson /* Copyright 2005-2012 Nick Mathewson */
3e49e2891SNick Mathewson /* Copyright 2009-2012 Niels Provos and Nick Mathewson */
455bcd7d2SNick Mathewson /* See license at end. */
555bcd7d2SNick Mathewson
655bcd7d2SNick Mathewson /* Based on ideas by Christopher Clark and interfaces from Niels Provos. */
755bcd7d2SNick Mathewson
83f8c7cd0SNick Mathewson #ifndef HT_INTERNAL_H_INCLUDED_
93f8c7cd0SNick Mathewson #define HT_INTERNAL_H_INCLUDED_
1055bcd7d2SNick Mathewson
1155bcd7d2SNick Mathewson #define HT_HEAD(name, type) \
1255bcd7d2SNick Mathewson struct name { \
1355bcd7d2SNick Mathewson /* The hash table itself. */ \
1455bcd7d2SNick Mathewson struct type **hth_table; \
1555bcd7d2SNick Mathewson /* How long is the hash table? */ \
1655bcd7d2SNick Mathewson unsigned hth_table_length; \
1755bcd7d2SNick Mathewson /* How many elements does the table contain? */ \
1855bcd7d2SNick Mathewson unsigned hth_n_entries; \
1955bcd7d2SNick Mathewson /* How many elements will we allow in the table before resizing it? */ \
2055bcd7d2SNick Mathewson unsigned hth_load_limit; \
2155bcd7d2SNick Mathewson /* Position of hth_table_length in the primes table. */ \
2255bcd7d2SNick Mathewson int hth_prime_idx; \
2355bcd7d2SNick Mathewson }
2455bcd7d2SNick Mathewson
2555bcd7d2SNick Mathewson #define HT_INITIALIZER() \
2655bcd7d2SNick Mathewson { NULL, 0, 0, 0, -1 }
2755bcd7d2SNick Mathewson
2846e5bb7bSNick Mathewson #ifdef HT_NO_CACHE_HASH_VALUES
2955bcd7d2SNick Mathewson #define HT_ENTRY(type) \
3055bcd7d2SNick Mathewson struct { \
3155bcd7d2SNick Mathewson struct type *hte_next; \
3255bcd7d2SNick Mathewson }
33a66e947bSNick Mathewson #else
34a66e947bSNick Mathewson #define HT_ENTRY(type) \
35a66e947bSNick Mathewson struct { \
36a66e947bSNick Mathewson struct type *hte_next; \
3746e5bb7bSNick Mathewson unsigned hte_hash; \
38a66e947bSNick Mathewson }
39a66e947bSNick Mathewson #endif
4055bcd7d2SNick Mathewson
4155bcd7d2SNick Mathewson #define HT_EMPTY(head) \
4255bcd7d2SNick Mathewson ((head)->hth_n_entries == 0)
4355bcd7d2SNick Mathewson
4455bcd7d2SNick Mathewson /* How many elements in 'head'? */
4555bcd7d2SNick Mathewson #define HT_SIZE(head) \
4655bcd7d2SNick Mathewson ((head)->hth_n_entries)
4755bcd7d2SNick Mathewson
484900c5f9SNick Mathewson /* Return memory usage for a hashtable (not counting the entries themselves) */
494900c5f9SNick Mathewson #define HT_MEM_USAGE(head) \
504900c5f9SNick Mathewson (sizeof(*head) + (head)->hth_table_length * sizeof(void*))
514900c5f9SNick Mathewson
5255bcd7d2SNick Mathewson #define HT_FIND(name, head, elm) name##_HT_FIND((head), (elm))
5355bcd7d2SNick Mathewson #define HT_INSERT(name, head, elm) name##_HT_INSERT((head), (elm))
5455bcd7d2SNick Mathewson #define HT_REPLACE(name, head, elm) name##_HT_REPLACE((head), (elm))
5555bcd7d2SNick Mathewson #define HT_REMOVE(name, head, elm) name##_HT_REMOVE((head), (elm))
5655bcd7d2SNick Mathewson #define HT_START(name, head) name##_HT_START(head)
5755bcd7d2SNick Mathewson #define HT_NEXT(name, head, elm) name##_HT_NEXT((head), (elm))
5855bcd7d2SNick Mathewson #define HT_NEXT_RMV(name, head, elm) name##_HT_NEXT_RMV((head), (elm))
5955bcd7d2SNick Mathewson #define HT_CLEAR(name, head) name##_HT_CLEAR(head)
6055bcd7d2SNick Mathewson #define HT_INIT(name, head) name##_HT_INIT(head)
6155bcd7d2SNick Mathewson /* Helper: */
6255bcd7d2SNick Mathewson static inline unsigned
ht_improve_hash_(unsigned h)63*8ac3c4c2SNick Mathewson ht_improve_hash_(unsigned h)
6455bcd7d2SNick Mathewson {
6555bcd7d2SNick Mathewson /* Aim to protect against poor hash functions by adding logic here
6655bcd7d2SNick Mathewson * - logic taken from java 1.4 hashtable source */
6755bcd7d2SNick Mathewson h += ~(h << 9);
6855bcd7d2SNick Mathewson h ^= ((h >> 14) | (h << 18)); /* >>> */
6955bcd7d2SNick Mathewson h += (h << 4);
7055bcd7d2SNick Mathewson h ^= ((h >> 10) | (h << 22)); /* >>> */
7155bcd7d2SNick Mathewson return h;
7255bcd7d2SNick Mathewson }
7355bcd7d2SNick Mathewson
7455bcd7d2SNick Mathewson #if 0
7555bcd7d2SNick Mathewson /** Basic string hash function, from Java standard String.hashCode(). */
7655bcd7d2SNick Mathewson static inline unsigned
77*8ac3c4c2SNick Mathewson ht_string_hash_(const char *s)
7855bcd7d2SNick Mathewson {
7955bcd7d2SNick Mathewson unsigned h = 0;
8055bcd7d2SNick Mathewson int m = 1;
8155bcd7d2SNick Mathewson while (*s) {
8255bcd7d2SNick Mathewson h += ((signed char)*s++)*m;
8355bcd7d2SNick Mathewson m = (m<<5)-1; /* m *= 31 */
8455bcd7d2SNick Mathewson }
8555bcd7d2SNick Mathewson return h;
8655bcd7d2SNick Mathewson }
8755bcd7d2SNick Mathewson #endif
8855bcd7d2SNick Mathewson
8955bcd7d2SNick Mathewson /** Basic string hash function, from Python's str.__hash__() */
9055bcd7d2SNick Mathewson static inline unsigned
ht_string_hash_(const char * s)91*8ac3c4c2SNick Mathewson ht_string_hash_(const char *s)
9255bcd7d2SNick Mathewson {
9355bcd7d2SNick Mathewson unsigned h;
9455bcd7d2SNick Mathewson const unsigned char *cp = (const unsigned char *)s;
9555bcd7d2SNick Mathewson h = *cp << 7;
9655bcd7d2SNick Mathewson while (*cp) {
9755bcd7d2SNick Mathewson h = (1000003*h) ^ *cp++;
9855bcd7d2SNick Mathewson }
9955bcd7d2SNick Mathewson /* This conversion truncates the length of the string, but that's ok. */
10055bcd7d2SNick Mathewson h ^= (unsigned)(cp-(const unsigned char*)s);
10155bcd7d2SNick Mathewson return h;
10255bcd7d2SNick Mathewson }
10355bcd7d2SNick Mathewson
10446e5bb7bSNick Mathewson #ifndef HT_NO_CACHE_HASH_VALUES
105f088d0c5SNick Mathewson #define HT_SET_HASH_(elm, field, hashfn) \
106a66e947bSNick Mathewson do { (elm)->field.hte_hash = hashfn(elm); } while (0)
107f088d0c5SNick Mathewson #define HT_SET_HASHVAL_(elm, field, val) \
108a66e947bSNick Mathewson do { (elm)->field.hte_hash = (val); } while (0)
109f088d0c5SNick Mathewson #define HT_ELT_HASH_(elm, field, hashfn) \
110a66e947bSNick Mathewson ((elm)->field.hte_hash)
111a66e947bSNick Mathewson #else
112f088d0c5SNick Mathewson #define HT_SET_HASH_(elm, field, hashfn) \
113a66e947bSNick Mathewson ((void)0)
114f088d0c5SNick Mathewson #define HT_ELT_HASH_(elm, field, hashfn) \
115a66e947bSNick Mathewson (hashfn(elm))
116f088d0c5SNick Mathewson #define HT_SET_HASHVAL_(elm, field, val) \
117a66e947bSNick Mathewson ((void)0)
118a66e947bSNick Mathewson #endif
119a66e947bSNick Mathewson
120a66e947bSNick Mathewson /* Helper: alias for the bucket containing 'elm'. */
121f088d0c5SNick Mathewson #define HT_BUCKET_(head, field, elm, hashfn) \
122f088d0c5SNick Mathewson ((head)->hth_table[HT_ELT_HASH_(elm,field,hashfn) % head->hth_table_length])
12355bcd7d2SNick Mathewson
12455bcd7d2SNick Mathewson #define HT_FOREACH(x, name, head) \
12555bcd7d2SNick Mathewson for ((x) = HT_START(name, head); \
12655bcd7d2SNick Mathewson (x) != NULL; \
12755bcd7d2SNick Mathewson (x) = HT_NEXT(name, head, x))
12855bcd7d2SNick Mathewson
12955bcd7d2SNick Mathewson #define HT_PROTOTYPE(name, type, field, hashfn, eqfn) \
13055bcd7d2SNick Mathewson int name##_HT_GROW(struct name *ht, unsigned min_capacity); \
13155bcd7d2SNick Mathewson void name##_HT_CLEAR(struct name *ht); \
132f088d0c5SNick Mathewson int name##_HT_REP_IS_BAD_(const struct name *ht); \
13355bcd7d2SNick Mathewson static inline void \
13455bcd7d2SNick Mathewson name##_HT_INIT(struct name *head) { \
13555bcd7d2SNick Mathewson head->hth_table_length = 0; \
13655bcd7d2SNick Mathewson head->hth_table = NULL; \
13755bcd7d2SNick Mathewson head->hth_n_entries = 0; \
13855bcd7d2SNick Mathewson head->hth_load_limit = 0; \
13955bcd7d2SNick Mathewson head->hth_prime_idx = -1; \
14055bcd7d2SNick Mathewson } \
14155bcd7d2SNick Mathewson /* Helper: returns a pointer to the right location in the table \
14255bcd7d2SNick Mathewson * 'head' to find or insert the element 'elm'. */ \
14355bcd7d2SNick Mathewson static inline struct type ** \
144f088d0c5SNick Mathewson name##_HT_FIND_P_(struct name *head, struct type *elm) \
14555bcd7d2SNick Mathewson { \
14655bcd7d2SNick Mathewson struct type **p; \
14755bcd7d2SNick Mathewson if (!head->hth_table) \
14855bcd7d2SNick Mathewson return NULL; \
149f088d0c5SNick Mathewson p = &HT_BUCKET_(head, field, elm, hashfn); \
15055bcd7d2SNick Mathewson while (*p) { \
15155bcd7d2SNick Mathewson if (eqfn(*p, elm)) \
15255bcd7d2SNick Mathewson return p; \
15355bcd7d2SNick Mathewson p = &(*p)->field.hte_next; \
15455bcd7d2SNick Mathewson } \
15555bcd7d2SNick Mathewson return p; \
15655bcd7d2SNick Mathewson } \
15755bcd7d2SNick Mathewson /* Return a pointer to the element in the table 'head' matching 'elm', \
15855bcd7d2SNick Mathewson * or NULL if no such element exists */ \
15955bcd7d2SNick Mathewson static inline struct type * \
16055bcd7d2SNick Mathewson name##_HT_FIND(const struct name *head, struct type *elm) \
16155bcd7d2SNick Mathewson { \
16255bcd7d2SNick Mathewson struct type **p; \
16355bcd7d2SNick Mathewson struct name *h = (struct name *) head; \
164f088d0c5SNick Mathewson HT_SET_HASH_(elm, field, hashfn); \
165f088d0c5SNick Mathewson p = name##_HT_FIND_P_(h, elm); \
16655bcd7d2SNick Mathewson return p ? *p : NULL; \
16755bcd7d2SNick Mathewson } \
16855bcd7d2SNick Mathewson /* Insert the element 'elm' into the table 'head'. Do not call this \
16955bcd7d2SNick Mathewson * function if the table might already contain a matching element. */ \
17055bcd7d2SNick Mathewson static inline void \
17155bcd7d2SNick Mathewson name##_HT_INSERT(struct name *head, struct type *elm) \
17255bcd7d2SNick Mathewson { \
17355bcd7d2SNick Mathewson struct type **p; \
17455bcd7d2SNick Mathewson if (!head->hth_table || head->hth_n_entries >= head->hth_load_limit) \
17555bcd7d2SNick Mathewson name##_HT_GROW(head, head->hth_n_entries+1); \
17655bcd7d2SNick Mathewson ++head->hth_n_entries; \
177f088d0c5SNick Mathewson HT_SET_HASH_(elm, field, hashfn); \
178f088d0c5SNick Mathewson p = &HT_BUCKET_(head, field, elm, hashfn); \
17955bcd7d2SNick Mathewson elm->field.hte_next = *p; \
18055bcd7d2SNick Mathewson *p = elm; \
18155bcd7d2SNick Mathewson } \
18255bcd7d2SNick Mathewson /* Insert the element 'elm' into the table 'head'. If there already \
18355bcd7d2SNick Mathewson * a matching element in the table, replace that element and return \
18455bcd7d2SNick Mathewson * it. */ \
18555bcd7d2SNick Mathewson static inline struct type * \
18655bcd7d2SNick Mathewson name##_HT_REPLACE(struct name *head, struct type *elm) \
18755bcd7d2SNick Mathewson { \
18855bcd7d2SNick Mathewson struct type **p, *r; \
18955bcd7d2SNick Mathewson if (!head->hth_table || head->hth_n_entries >= head->hth_load_limit) \
19055bcd7d2SNick Mathewson name##_HT_GROW(head, head->hth_n_entries+1); \
191f088d0c5SNick Mathewson HT_SET_HASH_(elm, field, hashfn); \
192f088d0c5SNick Mathewson p = name##_HT_FIND_P_(head, elm); \
19355bcd7d2SNick Mathewson r = *p; \
19455bcd7d2SNick Mathewson *p = elm; \
19555bcd7d2SNick Mathewson if (r && (r!=elm)) { \
19655bcd7d2SNick Mathewson elm->field.hte_next = r->field.hte_next; \
19755bcd7d2SNick Mathewson r->field.hte_next = NULL; \
19855bcd7d2SNick Mathewson return r; \
19955bcd7d2SNick Mathewson } else { \
20055bcd7d2SNick Mathewson ++head->hth_n_entries; \
20155bcd7d2SNick Mathewson return NULL; \
20255bcd7d2SNick Mathewson } \
20355bcd7d2SNick Mathewson } \
20455bcd7d2SNick Mathewson /* Remove any element matching 'elm' from the table 'head'. If such \
20555bcd7d2SNick Mathewson * an element is found, return it; otherwise return NULL. */ \
20655bcd7d2SNick Mathewson static inline struct type * \
20755bcd7d2SNick Mathewson name##_HT_REMOVE(struct name *head, struct type *elm) \
20855bcd7d2SNick Mathewson { \
20955bcd7d2SNick Mathewson struct type **p, *r; \
210f088d0c5SNick Mathewson HT_SET_HASH_(elm, field, hashfn); \
211f088d0c5SNick Mathewson p = name##_HT_FIND_P_(head,elm); \
21255bcd7d2SNick Mathewson if (!p || !*p) \
21355bcd7d2SNick Mathewson return NULL; \
21455bcd7d2SNick Mathewson r = *p; \
21555bcd7d2SNick Mathewson *p = r->field.hte_next; \
21655bcd7d2SNick Mathewson r->field.hte_next = NULL; \
21755bcd7d2SNick Mathewson --head->hth_n_entries; \
21855bcd7d2SNick Mathewson return r; \
21955bcd7d2SNick Mathewson } \
22055bcd7d2SNick Mathewson /* Invoke the function 'fn' on every element of the table 'head', \
22155bcd7d2SNick Mathewson * using 'data' as its second argument. If the function returns \
22255bcd7d2SNick Mathewson * nonzero, remove the most recently examined element before invoking \
22355bcd7d2SNick Mathewson * the function again. */ \
22455bcd7d2SNick Mathewson static inline void \
22555bcd7d2SNick Mathewson name##_HT_FOREACH_FN(struct name *head, \
22655bcd7d2SNick Mathewson int (*fn)(struct type *, void *), \
22755bcd7d2SNick Mathewson void *data) \
22855bcd7d2SNick Mathewson { \
22955bcd7d2SNick Mathewson unsigned idx; \
23055bcd7d2SNick Mathewson struct type **p, **nextp, *next; \
23155bcd7d2SNick Mathewson if (!head->hth_table) \
23255bcd7d2SNick Mathewson return; \
23355bcd7d2SNick Mathewson for (idx=0; idx < head->hth_table_length; ++idx) { \
23455bcd7d2SNick Mathewson p = &head->hth_table[idx]; \
23555bcd7d2SNick Mathewson while (*p) { \
23655bcd7d2SNick Mathewson nextp = &(*p)->field.hte_next; \
23755bcd7d2SNick Mathewson next = *nextp; \
2388ee9f9c1SNicholas Marriott if (fn(*p, data)) { \
23955bcd7d2SNick Mathewson --head->hth_n_entries; \
24055bcd7d2SNick Mathewson *p = next; \
24155bcd7d2SNick Mathewson } else { \
24255bcd7d2SNick Mathewson p = nextp; \
24355bcd7d2SNick Mathewson } \
24455bcd7d2SNick Mathewson } \
24555bcd7d2SNick Mathewson } \
24655bcd7d2SNick Mathewson } \
24755bcd7d2SNick Mathewson /* Return a pointer to the first element in the table 'head', under \
24855bcd7d2SNick Mathewson * an arbitrary order. This order is stable under remove operations, \
24955bcd7d2SNick Mathewson * but not under others. If the table is empty, return NULL. */ \
25055bcd7d2SNick Mathewson static inline struct type ** \
25155bcd7d2SNick Mathewson name##_HT_START(struct name *head) \
25255bcd7d2SNick Mathewson { \
25355bcd7d2SNick Mathewson unsigned b = 0; \
25455bcd7d2SNick Mathewson while (b < head->hth_table_length) { \
25555bcd7d2SNick Mathewson if (head->hth_table[b]) \
25655bcd7d2SNick Mathewson return &head->hth_table[b]; \
25755bcd7d2SNick Mathewson ++b; \
25855bcd7d2SNick Mathewson } \
25955bcd7d2SNick Mathewson return NULL; \
26055bcd7d2SNick Mathewson } \
26155bcd7d2SNick Mathewson /* Return the next element in 'head' after 'elm', under the arbitrary \
26255bcd7d2SNick Mathewson * order used by HT_START. If there are no more elements, return \
26355bcd7d2SNick Mathewson * NULL. If 'elm' is to be removed from the table, you must call \
26455bcd7d2SNick Mathewson * this function for the next value before you remove it. \
26555bcd7d2SNick Mathewson */ \
26655bcd7d2SNick Mathewson static inline struct type ** \
26755bcd7d2SNick Mathewson name##_HT_NEXT(struct name *head, struct type **elm) \
26855bcd7d2SNick Mathewson { \
26955bcd7d2SNick Mathewson if ((*elm)->field.hte_next) { \
27055bcd7d2SNick Mathewson return &(*elm)->field.hte_next; \
27155bcd7d2SNick Mathewson } else { \
272f088d0c5SNick Mathewson unsigned b = (HT_ELT_HASH_(*elm, field, hashfn) % head->hth_table_length)+1; \
27355bcd7d2SNick Mathewson while (b < head->hth_table_length) { \
27455bcd7d2SNick Mathewson if (head->hth_table[b]) \
27555bcd7d2SNick Mathewson return &head->hth_table[b]; \
27655bcd7d2SNick Mathewson ++b; \
27755bcd7d2SNick Mathewson } \
27855bcd7d2SNick Mathewson return NULL; \
27955bcd7d2SNick Mathewson } \
28055bcd7d2SNick Mathewson } \
28155bcd7d2SNick Mathewson static inline struct type ** \
28255bcd7d2SNick Mathewson name##_HT_NEXT_RMV(struct name *head, struct type **elm) \
28355bcd7d2SNick Mathewson { \
284f088d0c5SNick Mathewson unsigned h = HT_ELT_HASH_(*elm, field, hashfn); \
28555bcd7d2SNick Mathewson *elm = (*elm)->field.hte_next; \
28655bcd7d2SNick Mathewson --head->hth_n_entries; \
28755bcd7d2SNick Mathewson if (*elm) { \
28855bcd7d2SNick Mathewson return elm; \
28955bcd7d2SNick Mathewson } else { \
29055bcd7d2SNick Mathewson unsigned b = (h % head->hth_table_length)+1; \
29155bcd7d2SNick Mathewson while (b < head->hth_table_length) { \
29255bcd7d2SNick Mathewson if (head->hth_table[b]) \
29355bcd7d2SNick Mathewson return &head->hth_table[b]; \
29455bcd7d2SNick Mathewson ++b; \
29555bcd7d2SNick Mathewson } \
29655bcd7d2SNick Mathewson return NULL; \
29755bcd7d2SNick Mathewson } \
29855bcd7d2SNick Mathewson }
29955bcd7d2SNick Mathewson
30055bcd7d2SNick Mathewson #define HT_GENERATE(name, type, field, hashfn, eqfn, load, mallocfn, \
30155bcd7d2SNick Mathewson reallocfn, freefn) \
30255bcd7d2SNick Mathewson static unsigned name##_PRIMES[] = { \
30355bcd7d2SNick Mathewson 53, 97, 193, 389, \
30455bcd7d2SNick Mathewson 769, 1543, 3079, 6151, \
30555bcd7d2SNick Mathewson 12289, 24593, 49157, 98317, \
30655bcd7d2SNick Mathewson 196613, 393241, 786433, 1572869, \
30755bcd7d2SNick Mathewson 3145739, 6291469, 12582917, 25165843, \
30855bcd7d2SNick Mathewson 50331653, 100663319, 201326611, 402653189, \
30955bcd7d2SNick Mathewson 805306457, 1610612741 \
31055bcd7d2SNick Mathewson }; \
31155bcd7d2SNick Mathewson static unsigned name##_N_PRIMES = \
31255bcd7d2SNick Mathewson (unsigned)(sizeof(name##_PRIMES)/sizeof(name##_PRIMES[0])); \
31355bcd7d2SNick Mathewson /* Expand the internal table of 'head' until it is large enough to \
31455bcd7d2SNick Mathewson * hold 'size' elements. Return 0 on success, -1 on allocation \
31555bcd7d2SNick Mathewson * failure. */ \
31655bcd7d2SNick Mathewson int \
31755bcd7d2SNick Mathewson name##_HT_GROW(struct name *head, unsigned size) \
31855bcd7d2SNick Mathewson { \
31955bcd7d2SNick Mathewson unsigned new_len, new_load_limit; \
32055bcd7d2SNick Mathewson int prime_idx; \
32155bcd7d2SNick Mathewson struct type **new_table; \
32255bcd7d2SNick Mathewson if (head->hth_prime_idx == (int)name##_N_PRIMES - 1) \
32355bcd7d2SNick Mathewson return 0; \
32455bcd7d2SNick Mathewson if (head->hth_load_limit > size) \
32555bcd7d2SNick Mathewson return 0; \
32655bcd7d2SNick Mathewson prime_idx = head->hth_prime_idx; \
32755bcd7d2SNick Mathewson do { \
32855bcd7d2SNick Mathewson new_len = name##_PRIMES[++prime_idx]; \
32955bcd7d2SNick Mathewson new_load_limit = (unsigned)(load*new_len); \
33055bcd7d2SNick Mathewson } while (new_load_limit <= size && \
33155bcd7d2SNick Mathewson prime_idx < (int)name##_N_PRIMES); \
33255bcd7d2SNick Mathewson if ((new_table = mallocfn(new_len*sizeof(struct type*)))) { \
33355bcd7d2SNick Mathewson unsigned b; \
33455bcd7d2SNick Mathewson memset(new_table, 0, new_len*sizeof(struct type*)); \
33555bcd7d2SNick Mathewson for (b = 0; b < head->hth_table_length; ++b) { \
33655bcd7d2SNick Mathewson struct type *elm, *next; \
33755bcd7d2SNick Mathewson unsigned b2; \
33855bcd7d2SNick Mathewson elm = head->hth_table[b]; \
33955bcd7d2SNick Mathewson while (elm) { \
34055bcd7d2SNick Mathewson next = elm->field.hte_next; \
341f088d0c5SNick Mathewson b2 = HT_ELT_HASH_(elm, field, hashfn) % new_len; \
34255bcd7d2SNick Mathewson elm->field.hte_next = new_table[b2]; \
34355bcd7d2SNick Mathewson new_table[b2] = elm; \
34455bcd7d2SNick Mathewson elm = next; \
34555bcd7d2SNick Mathewson } \
34655bcd7d2SNick Mathewson } \
34755bcd7d2SNick Mathewson if (head->hth_table) \
34855bcd7d2SNick Mathewson freefn(head->hth_table); \
34955bcd7d2SNick Mathewson head->hth_table = new_table; \
35055bcd7d2SNick Mathewson } else { \
35155bcd7d2SNick Mathewson unsigned b, b2; \
35255bcd7d2SNick Mathewson new_table = reallocfn(head->hth_table, new_len*sizeof(struct type*)); \
35355bcd7d2SNick Mathewson if (!new_table) return -1; \
35455bcd7d2SNick Mathewson memset(new_table + head->hth_table_length, 0, \
35555bcd7d2SNick Mathewson (new_len - head->hth_table_length)*sizeof(struct type*)); \
35655bcd7d2SNick Mathewson for (b=0; b < head->hth_table_length; ++b) { \
35755bcd7d2SNick Mathewson struct type *e, **pE; \
35855bcd7d2SNick Mathewson for (pE = &new_table[b], e = *pE; e != NULL; e = *pE) { \
359f088d0c5SNick Mathewson b2 = HT_ELT_HASH_(e, field, hashfn) % new_len; \
36055bcd7d2SNick Mathewson if (b2 == b) { \
36155bcd7d2SNick Mathewson pE = &e->field.hte_next; \
36255bcd7d2SNick Mathewson } else { \
36355bcd7d2SNick Mathewson *pE = e->field.hte_next; \
36455bcd7d2SNick Mathewson e->field.hte_next = new_table[b2]; \
36555bcd7d2SNick Mathewson new_table[b2] = e; \
36655bcd7d2SNick Mathewson } \
36755bcd7d2SNick Mathewson } \
36855bcd7d2SNick Mathewson } \
36955bcd7d2SNick Mathewson head->hth_table = new_table; \
37055bcd7d2SNick Mathewson } \
37155bcd7d2SNick Mathewson head->hth_table_length = new_len; \
37255bcd7d2SNick Mathewson head->hth_prime_idx = prime_idx; \
37355bcd7d2SNick Mathewson head->hth_load_limit = new_load_limit; \
37455bcd7d2SNick Mathewson return 0; \
37555bcd7d2SNick Mathewson } \
37655bcd7d2SNick Mathewson /* Free all storage held by 'head'. Does not free 'head' itself, or \
37755bcd7d2SNick Mathewson * individual elements. */ \
37855bcd7d2SNick Mathewson void \
37955bcd7d2SNick Mathewson name##_HT_CLEAR(struct name *head) \
38055bcd7d2SNick Mathewson { \
38155bcd7d2SNick Mathewson if (head->hth_table) \
38255bcd7d2SNick Mathewson freefn(head->hth_table); \
38355bcd7d2SNick Mathewson name##_HT_INIT(head); \
38455bcd7d2SNick Mathewson } \
38555bcd7d2SNick Mathewson /* Debugging helper: return false iff the representation of 'head' is \
38655bcd7d2SNick Mathewson * internally consistent. */ \
38755bcd7d2SNick Mathewson int \
388f088d0c5SNick Mathewson name##_HT_REP_IS_BAD_(const struct name *head) \
38955bcd7d2SNick Mathewson { \
39055bcd7d2SNick Mathewson unsigned n, i; \
39155bcd7d2SNick Mathewson struct type *elm; \
39255bcd7d2SNick Mathewson if (!head->hth_table_length) { \
39355bcd7d2SNick Mathewson if (!head->hth_table && !head->hth_n_entries && \
39455bcd7d2SNick Mathewson !head->hth_load_limit && head->hth_prime_idx == -1) \
39555bcd7d2SNick Mathewson return 0; \
39655bcd7d2SNick Mathewson else \
39755bcd7d2SNick Mathewson return 1; \
39855bcd7d2SNick Mathewson } \
39955bcd7d2SNick Mathewson if (!head->hth_table || head->hth_prime_idx < 0 || \
40055bcd7d2SNick Mathewson !head->hth_load_limit) \
40155bcd7d2SNick Mathewson return 2; \
40255bcd7d2SNick Mathewson if (head->hth_n_entries > head->hth_load_limit) \
40355bcd7d2SNick Mathewson return 3; \
40455bcd7d2SNick Mathewson if (head->hth_table_length != name##_PRIMES[head->hth_prime_idx]) \
40555bcd7d2SNick Mathewson return 4; \
40655bcd7d2SNick Mathewson if (head->hth_load_limit != (unsigned)(load*head->hth_table_length)) \
40755bcd7d2SNick Mathewson return 5; \
40855bcd7d2SNick Mathewson for (n = i = 0; i < head->hth_table_length; ++i) { \
40955bcd7d2SNick Mathewson for (elm = head->hth_table[i]; elm; elm = elm->field.hte_next) { \
410f088d0c5SNick Mathewson if (HT_ELT_HASH_(elm, field, hashfn) != hashfn(elm)) \
41155bcd7d2SNick Mathewson return 1000 + i; \
412f088d0c5SNick Mathewson if ((HT_ELT_HASH_(elm, field, hashfn) % head->hth_table_length) != i) \
41355bcd7d2SNick Mathewson return 10000 + i; \
41455bcd7d2SNick Mathewson ++n; \
41555bcd7d2SNick Mathewson } \
41655bcd7d2SNick Mathewson } \
41755bcd7d2SNick Mathewson if (n != head->hth_n_entries) \
41855bcd7d2SNick Mathewson return 6; \
41955bcd7d2SNick Mathewson return 0; \
42055bcd7d2SNick Mathewson }
42155bcd7d2SNick Mathewson
42255bcd7d2SNick Mathewson /** Implements an over-optimized "find and insert if absent" block;
42355bcd7d2SNick Mathewson * not meant for direct usage by typical code, or usage outside the critical
42455bcd7d2SNick Mathewson * path.*/
425f088d0c5SNick Mathewson #define HT_FIND_OR_INSERT_(name, field, hashfn, head, eltype, elm, var, y, n) \
42655bcd7d2SNick Mathewson { \
427f088d0c5SNick Mathewson struct name *var##_head_ = head; \
42855bcd7d2SNick Mathewson struct eltype **var; \
429f088d0c5SNick Mathewson if (!var##_head_->hth_table || \
430f088d0c5SNick Mathewson var##_head_->hth_n_entries >= var##_head_->hth_load_limit) \
431f088d0c5SNick Mathewson name##_HT_GROW(var##_head_, var##_head_->hth_n_entries+1); \
432f088d0c5SNick Mathewson HT_SET_HASH_((elm), field, hashfn); \
433f088d0c5SNick Mathewson var = name##_HT_FIND_P_(var##_head_, (elm)); \
43455bcd7d2SNick Mathewson if (*var) { \
43555bcd7d2SNick Mathewson y; \
43655bcd7d2SNick Mathewson } else { \
43755bcd7d2SNick Mathewson n; \
43855bcd7d2SNick Mathewson } \
43955bcd7d2SNick Mathewson }
440f088d0c5SNick Mathewson #define HT_FOI_INSERT_(field, head, elm, newent, var) \
44155bcd7d2SNick Mathewson { \
442f088d0c5SNick Mathewson HT_SET_HASHVAL_(newent, field, (elm)->field.hte_hash); \
44355bcd7d2SNick Mathewson newent->field.hte_next = NULL; \
44455bcd7d2SNick Mathewson *var = newent; \
44555bcd7d2SNick Mathewson ++((head)->hth_n_entries); \
44655bcd7d2SNick Mathewson }
44755bcd7d2SNick Mathewson
44855bcd7d2SNick Mathewson /*
44955bcd7d2SNick Mathewson * Copyright 2005, Nick Mathewson. Implementation logic is adapted from code
4504900c5f9SNick Mathewson * by Christopher Clark, retrofit to allow drop-in memory management, and to
45178772c35SNick Mathewson * use the same interface as Niels Provos's tree.h. This is probably still
45278772c35SNick Mathewson * a derived work, so the original license below still applies.
45355bcd7d2SNick Mathewson *
45455bcd7d2SNick Mathewson * Copyright (c) 2002, Christopher Clark
45555bcd7d2SNick Mathewson * All rights reserved.
45655bcd7d2SNick Mathewson *
45755bcd7d2SNick Mathewson * Redistribution and use in source and binary forms, with or without
45855bcd7d2SNick Mathewson * modification, are permitted provided that the following conditions
45955bcd7d2SNick Mathewson * are met:
46055bcd7d2SNick Mathewson *
46155bcd7d2SNick Mathewson * * Redistributions of source code must retain the above copyright
46255bcd7d2SNick Mathewson * notice, this list of conditions and the following disclaimer.
46355bcd7d2SNick Mathewson *
46455bcd7d2SNick Mathewson * * Redistributions in binary form must reproduce the above copyright
46555bcd7d2SNick Mathewson * notice, this list of conditions and the following disclaimer in the
46655bcd7d2SNick Mathewson * documentation and/or other materials provided with the distribution.
46755bcd7d2SNick Mathewson *
46855bcd7d2SNick Mathewson * * Neither the name of the original author; nor the names of any contributors
46955bcd7d2SNick Mathewson * may be used to endorse or promote products derived from this software
47055bcd7d2SNick Mathewson * without specific prior written permission.
47155bcd7d2SNick Mathewson *
47255bcd7d2SNick Mathewson *
47355bcd7d2SNick Mathewson * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
47455bcd7d2SNick Mathewson * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
47555bcd7d2SNick Mathewson * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
47655bcd7d2SNick Mathewson * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
47755bcd7d2SNick Mathewson * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
47855bcd7d2SNick Mathewson * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
47955bcd7d2SNick Mathewson * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
48055bcd7d2SNick Mathewson * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
48155bcd7d2SNick Mathewson * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
48255bcd7d2SNick Mathewson * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
48355bcd7d2SNick Mathewson * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48455bcd7d2SNick Mathewson */
48555bcd7d2SNick Mathewson
48655bcd7d2SNick Mathewson #endif
48755bcd7d2SNick Mathewson
488