xref: /freebsd-src/contrib/libevent/ht-internal.h (revision c6879c6c14eedbd060ba588a3129a6c60ebbe783)
1*c43e99fdSEd Maste /* Copyright 2002 Christopher Clark */
2*c43e99fdSEd Maste /* Copyright 2005-2012 Nick Mathewson */
3*c43e99fdSEd Maste /* Copyright 2009-2012 Niels Provos and Nick Mathewson */
4*c43e99fdSEd Maste /* See license at end. */
5*c43e99fdSEd Maste 
6*c43e99fdSEd Maste /* Based on ideas by Christopher Clark and interfaces from Niels Provos. */
7*c43e99fdSEd Maste 
8*c43e99fdSEd Maste #ifndef HT_INTERNAL_H_INCLUDED_
9*c43e99fdSEd Maste #define HT_INTERNAL_H_INCLUDED_
10*c43e99fdSEd Maste 
11*c43e99fdSEd Maste #define HT_HEAD(name, type)                                             \
12*c43e99fdSEd Maste   struct name {                                                         \
13*c43e99fdSEd Maste     /* The hash table itself. */                                        \
14*c43e99fdSEd Maste     struct type **hth_table;                                            \
15*c43e99fdSEd Maste     /* How long is the hash table? */                                   \
16*c43e99fdSEd Maste     unsigned hth_table_length;                                          \
17*c43e99fdSEd Maste     /* How many elements does the table contain? */                     \
18*c43e99fdSEd Maste     unsigned hth_n_entries;                                             \
19*c43e99fdSEd Maste     /* How many elements will we allow in the table before resizing it? */ \
20*c43e99fdSEd Maste     unsigned hth_load_limit;                                            \
21*c43e99fdSEd Maste     /* Position of hth_table_length in the primes table. */             \
22*c43e99fdSEd Maste     int hth_prime_idx;                                                  \
23*c43e99fdSEd Maste   }
24*c43e99fdSEd Maste 
25*c43e99fdSEd Maste #define HT_INITIALIZER()                        \
26*c43e99fdSEd Maste   { NULL, 0, 0, 0, -1 }
27*c43e99fdSEd Maste 
28*c43e99fdSEd Maste #ifdef HT_NO_CACHE_HASH_VALUES
29*c43e99fdSEd Maste #define HT_ENTRY(type)                          \
30*c43e99fdSEd Maste   struct {                                      \
31*c43e99fdSEd Maste     struct type *hte_next;                      \
32*c43e99fdSEd Maste   }
33*c43e99fdSEd Maste #else
34*c43e99fdSEd Maste #define HT_ENTRY(type)                          \
35*c43e99fdSEd Maste   struct {                                      \
36*c43e99fdSEd Maste     struct type *hte_next;                      \
37*c43e99fdSEd Maste     unsigned hte_hash;                          \
38*c43e99fdSEd Maste   }
39*c43e99fdSEd Maste #endif
40*c43e99fdSEd Maste 
41*c43e99fdSEd Maste #define HT_EMPTY(head)                          \
42*c43e99fdSEd Maste   ((head)->hth_n_entries == 0)
43*c43e99fdSEd Maste 
44*c43e99fdSEd Maste /* How many elements in 'head'? */
45*c43e99fdSEd Maste #define HT_SIZE(head)                           \
46*c43e99fdSEd Maste   ((head)->hth_n_entries)
47*c43e99fdSEd Maste 
48*c43e99fdSEd Maste /* Return memory usage for a hashtable (not counting the entries themselves) */
49*c43e99fdSEd Maste #define HT_MEM_USAGE(head)                         \
50*c43e99fdSEd Maste   (sizeof(*head) + (head)->hth_table_length * sizeof(void*))
51*c43e99fdSEd Maste 
52*c43e99fdSEd Maste #define HT_FIND(name, head, elm)     name##_HT_FIND((head), (elm))
53*c43e99fdSEd Maste #define HT_INSERT(name, head, elm)   name##_HT_INSERT((head), (elm))
54*c43e99fdSEd Maste #define HT_REPLACE(name, head, elm)  name##_HT_REPLACE((head), (elm))
55*c43e99fdSEd Maste #define HT_REMOVE(name, head, elm)   name##_HT_REMOVE((head), (elm))
56*c43e99fdSEd Maste #define HT_START(name, head)         name##_HT_START(head)
57*c43e99fdSEd Maste #define HT_NEXT(name, head, elm)     name##_HT_NEXT((head), (elm))
58*c43e99fdSEd Maste #define HT_NEXT_RMV(name, head, elm) name##_HT_NEXT_RMV((head), (elm))
59*c43e99fdSEd Maste #define HT_CLEAR(name, head)         name##_HT_CLEAR(head)
60*c43e99fdSEd Maste #define HT_INIT(name, head)          name##_HT_INIT(head)
61*c43e99fdSEd Maste /* Helper: */
62*c43e99fdSEd Maste static inline unsigned
ht_improve_hash_(unsigned h)63*c43e99fdSEd Maste ht_improve_hash_(unsigned h)
64*c43e99fdSEd Maste {
65*c43e99fdSEd Maste   /* Aim to protect against poor hash functions by adding logic here
66*c43e99fdSEd Maste    * - logic taken from java 1.4 hashtable source */
67*c43e99fdSEd Maste   h += ~(h << 9);
68*c43e99fdSEd Maste   h ^=  ((h >> 14) | (h << 18)); /* >>> */
69*c43e99fdSEd Maste   h +=  (h << 4);
70*c43e99fdSEd Maste   h ^=  ((h >> 10) | (h << 22)); /* >>> */
71*c43e99fdSEd Maste   return h;
72*c43e99fdSEd Maste }
73*c43e99fdSEd Maste 
74*c43e99fdSEd Maste #if 0
75*c43e99fdSEd Maste /** Basic string hash function, from Java standard String.hashCode(). */
76*c43e99fdSEd Maste static inline unsigned
77*c43e99fdSEd Maste ht_string_hash_(const char *s)
78*c43e99fdSEd Maste {
79*c43e99fdSEd Maste   unsigned h = 0;
80*c43e99fdSEd Maste   int m = 1;
81*c43e99fdSEd Maste   while (*s) {
82*c43e99fdSEd Maste     h += ((signed char)*s++)*m;
83*c43e99fdSEd Maste     m = (m<<5)-1; /* m *= 31 */
84*c43e99fdSEd Maste   }
85*c43e99fdSEd Maste   return h;
86*c43e99fdSEd Maste }
87*c43e99fdSEd Maste #endif
88*c43e99fdSEd Maste 
89*c43e99fdSEd Maste /** Basic string hash function, from Python's str.__hash__() */
90*c43e99fdSEd Maste static inline unsigned
ht_string_hash_(const char * s)91*c43e99fdSEd Maste ht_string_hash_(const char *s)
92*c43e99fdSEd Maste {
93*c43e99fdSEd Maste   unsigned h;
94*c43e99fdSEd Maste   const unsigned char *cp = (const unsigned char *)s;
95*c43e99fdSEd Maste   h = *cp << 7;
96*c43e99fdSEd Maste   while (*cp) {
97*c43e99fdSEd Maste     h = (1000003*h) ^ *cp++;
98*c43e99fdSEd Maste   }
99*c43e99fdSEd Maste   /* This conversion truncates the length of the string, but that's ok. */
100*c43e99fdSEd Maste   h ^= (unsigned)(cp-(const unsigned char*)s);
101*c43e99fdSEd Maste   return h;
102*c43e99fdSEd Maste }
103*c43e99fdSEd Maste 
104*c43e99fdSEd Maste #ifndef HT_NO_CACHE_HASH_VALUES
105*c43e99fdSEd Maste #define HT_SET_HASH_(elm, field, hashfn)        \
106*c43e99fdSEd Maste 	do { (elm)->field.hte_hash = hashfn(elm); } while (0)
107*c43e99fdSEd Maste #define HT_SET_HASHVAL_(elm, field, val)	\
108*c43e99fdSEd Maste 	do { (elm)->field.hte_hash = (val); } while (0)
109*c43e99fdSEd Maste #define HT_ELT_HASH_(elm, field, hashfn)	\
110*c43e99fdSEd Maste 	((elm)->field.hte_hash)
111*c43e99fdSEd Maste #else
112*c43e99fdSEd Maste #define HT_SET_HASH_(elm, field, hashfn)	\
113*c43e99fdSEd Maste 	((void)0)
114*c43e99fdSEd Maste #define HT_ELT_HASH_(elm, field, hashfn)	\
115*c43e99fdSEd Maste 	(hashfn(elm))
116*c43e99fdSEd Maste #define HT_SET_HASHVAL_(elm, field, val)	\
117*c43e99fdSEd Maste         ((void)0)
118*c43e99fdSEd Maste #endif
119*c43e99fdSEd Maste 
120*c43e99fdSEd Maste /* Helper: alias for the bucket containing 'elm'. */
121*c43e99fdSEd Maste #define HT_BUCKET_(head, field, elm, hashfn)				\
122*c43e99fdSEd Maste 	((head)->hth_table[HT_ELT_HASH_(elm,field,hashfn) % head->hth_table_length])
123*c43e99fdSEd Maste 
124*c43e99fdSEd Maste #define HT_FOREACH(x, name, head)                 \
125*c43e99fdSEd Maste   for ((x) = HT_START(name, head);                \
126*c43e99fdSEd Maste        (x) != NULL;                               \
127*c43e99fdSEd Maste        (x) = HT_NEXT(name, head, x))
128*c43e99fdSEd Maste 
129*c43e99fdSEd Maste #define HT_PROTOTYPE(name, type, field, hashfn, eqfn)                   \
130*c43e99fdSEd Maste   int name##_HT_GROW(struct name *ht, unsigned min_capacity);           \
131*c43e99fdSEd Maste   void name##_HT_CLEAR(struct name *ht);                                \
132*c43e99fdSEd Maste   int name##_HT_REP_IS_BAD_(const struct name *ht);			\
133*c43e99fdSEd Maste   static inline void                                                    \
134*c43e99fdSEd Maste   name##_HT_INIT(struct name *head) {                                   \
135*c43e99fdSEd Maste     head->hth_table_length = 0;                                         \
136*c43e99fdSEd Maste     head->hth_table = NULL;                                             \
137*c43e99fdSEd Maste     head->hth_n_entries = 0;                                            \
138*c43e99fdSEd Maste     head->hth_load_limit = 0;                                           \
139*c43e99fdSEd Maste     head->hth_prime_idx = -1;                                           \
140*c43e99fdSEd Maste   }                                                                     \
141*c43e99fdSEd Maste   /* Helper: returns a pointer to the right location in the table       \
142*c43e99fdSEd Maste    * 'head' to find or insert the element 'elm'. */                     \
143*c43e99fdSEd Maste   static inline struct type **                                          \
144*c43e99fdSEd Maste   name##_HT_FIND_P_(struct name *head, struct type *elm)		\
145*c43e99fdSEd Maste   {                                                                     \
146*c43e99fdSEd Maste     struct type **p;                                                    \
147*c43e99fdSEd Maste     if (!head->hth_table)                                               \
148*c43e99fdSEd Maste       return NULL;                                                      \
149*c43e99fdSEd Maste     p = &HT_BUCKET_(head, field, elm, hashfn);				\
150*c43e99fdSEd Maste     while (*p) {                                                        \
151*c43e99fdSEd Maste       if (eqfn(*p, elm))                                                \
152*c43e99fdSEd Maste         return p;                                                       \
153*c43e99fdSEd Maste       p = &(*p)->field.hte_next;                                        \
154*c43e99fdSEd Maste     }                                                                   \
155*c43e99fdSEd Maste     return p;                                                           \
156*c43e99fdSEd Maste   }                                                                     \
157*c43e99fdSEd Maste   /* Return a pointer to the element in the table 'head' matching 'elm', \
158*c43e99fdSEd Maste    * or NULL if no such element exists */                               \
159*c43e99fdSEd Maste   static inline struct type *                                           \
160*c43e99fdSEd Maste   name##_HT_FIND(const struct name *head, struct type *elm)             \
161*c43e99fdSEd Maste   {                                                                     \
162*c43e99fdSEd Maste     struct type **p;                                                    \
163*c43e99fdSEd Maste     struct name *h = (struct name *) head;                              \
164*c43e99fdSEd Maste     HT_SET_HASH_(elm, field, hashfn);                                   \
165*c43e99fdSEd Maste     p = name##_HT_FIND_P_(h, elm);					\
166*c43e99fdSEd Maste     return p ? *p : NULL;                                               \
167*c43e99fdSEd Maste   }                                                                     \
168*c43e99fdSEd Maste   /* Insert the element 'elm' into the table 'head'.  Do not call this  \
169*c43e99fdSEd Maste    * function if the table might already contain a matching element. */ \
170*c43e99fdSEd Maste   static inline void                                                    \
171*c43e99fdSEd Maste   name##_HT_INSERT(struct name *head, struct type *elm)                 \
172*c43e99fdSEd Maste   {                                                                     \
173*c43e99fdSEd Maste     struct type **p;                                                    \
174*c43e99fdSEd Maste     if (!head->hth_table || head->hth_n_entries >= head->hth_load_limit) \
175*c43e99fdSEd Maste       name##_HT_GROW(head, head->hth_n_entries+1);                      \
176*c43e99fdSEd Maste     ++head->hth_n_entries;                                              \
177*c43e99fdSEd Maste     HT_SET_HASH_(elm, field, hashfn);                                   \
178*c43e99fdSEd Maste     p = &HT_BUCKET_(head, field, elm, hashfn);				\
179*c43e99fdSEd Maste     elm->field.hte_next = *p;                                           \
180*c43e99fdSEd Maste     *p = elm;                                                           \
181*c43e99fdSEd Maste   }                                                                     \
182*c43e99fdSEd Maste   /* Insert the element 'elm' into the table 'head'. If there already   \
183*c43e99fdSEd Maste    * a matching element in the table, replace that element and return   \
184*c43e99fdSEd Maste    * it. */                                                             \
185*c43e99fdSEd Maste   static inline struct type *                                           \
186*c43e99fdSEd Maste   name##_HT_REPLACE(struct name *head, struct type *elm)                \
187*c43e99fdSEd Maste   {                                                                     \
188*c43e99fdSEd Maste     struct type **p, *r;                                                \
189*c43e99fdSEd Maste     if (!head->hth_table || head->hth_n_entries >= head->hth_load_limit) \
190*c43e99fdSEd Maste       name##_HT_GROW(head, head->hth_n_entries+1);                      \
191*c43e99fdSEd Maste     HT_SET_HASH_(elm, field, hashfn);                                   \
192*c43e99fdSEd Maste     p = name##_HT_FIND_P_(head, elm);					\
193*c43e99fdSEd Maste     r = *p;                                                             \
194*c43e99fdSEd Maste     *p = elm;                                                           \
195*c43e99fdSEd Maste     if (r && (r!=elm)) {                                                \
196*c43e99fdSEd Maste       elm->field.hte_next = r->field.hte_next;                          \
197*c43e99fdSEd Maste       r->field.hte_next = NULL;                                         \
198*c43e99fdSEd Maste       return r;                                                         \
199*c43e99fdSEd Maste     } else {                                                            \
200*c43e99fdSEd Maste       ++head->hth_n_entries;                                            \
201*c43e99fdSEd Maste       return NULL;                                                      \
202*c43e99fdSEd Maste     }                                                                   \
203*c43e99fdSEd Maste   }                                                                     \
204*c43e99fdSEd Maste   /* Remove any element matching 'elm' from the table 'head'.  If such  \
205*c43e99fdSEd Maste    * an element is found, return it; otherwise return NULL. */          \
206*c43e99fdSEd Maste   static inline struct type *                                           \
207*c43e99fdSEd Maste   name##_HT_REMOVE(struct name *head, struct type *elm)                 \
208*c43e99fdSEd Maste   {                                                                     \
209*c43e99fdSEd Maste     struct type **p, *r;                                                \
210*c43e99fdSEd Maste     HT_SET_HASH_(elm, field, hashfn);                                   \
211*c43e99fdSEd Maste     p = name##_HT_FIND_P_(head,elm);					\
212*c43e99fdSEd Maste     if (!p || !*p)                                                      \
213*c43e99fdSEd Maste       return NULL;                                                      \
214*c43e99fdSEd Maste     r = *p;                                                             \
215*c43e99fdSEd Maste     *p = r->field.hte_next;                                             \
216*c43e99fdSEd Maste     r->field.hte_next = NULL;                                           \
217*c43e99fdSEd Maste     --head->hth_n_entries;                                              \
218*c43e99fdSEd Maste     return r;                                                           \
219*c43e99fdSEd Maste   }                                                                     \
220*c43e99fdSEd Maste   /* Invoke the function 'fn' on every element of the table 'head',     \
221*c43e99fdSEd Maste    * using 'data' as its second argument.  If the function returns      \
222*c43e99fdSEd Maste    * nonzero, remove the most recently examined element before invoking \
223*c43e99fdSEd Maste    * the function again. */                                             \
224*c43e99fdSEd Maste   static inline void                                                    \
225*c43e99fdSEd Maste   name##_HT_FOREACH_FN(struct name *head,                               \
226*c43e99fdSEd Maste                        int (*fn)(struct type *, void *),                \
227*c43e99fdSEd Maste                        void *data)                                      \
228*c43e99fdSEd Maste   {                                                                     \
229*c43e99fdSEd Maste     unsigned idx;                                                       \
230*c43e99fdSEd Maste     struct type **p, **nextp, *next;                                    \
231*c43e99fdSEd Maste     if (!head->hth_table)                                               \
232*c43e99fdSEd Maste       return;                                                           \
233*c43e99fdSEd Maste     for (idx=0; idx < head->hth_table_length; ++idx) {                  \
234*c43e99fdSEd Maste       p = &head->hth_table[idx];                                        \
235*c43e99fdSEd Maste       while (*p) {                                                      \
236*c43e99fdSEd Maste         nextp = &(*p)->field.hte_next;                                  \
237*c43e99fdSEd Maste         next = *nextp;                                                  \
238*c43e99fdSEd Maste         if (fn(*p, data)) {                                             \
239*c43e99fdSEd Maste           --head->hth_n_entries;                                        \
240*c43e99fdSEd Maste           *p = next;                                                    \
241*c43e99fdSEd Maste         } else {                                                        \
242*c43e99fdSEd Maste           p = nextp;                                                    \
243*c43e99fdSEd Maste         }                                                               \
244*c43e99fdSEd Maste       }                                                                 \
245*c43e99fdSEd Maste     }                                                                   \
246*c43e99fdSEd Maste   }                                                                     \
247*c43e99fdSEd Maste   /* Return a pointer to the first element in the table 'head', under   \
248*c43e99fdSEd Maste    * an arbitrary order.  This order is stable under remove operations, \
249*c43e99fdSEd Maste    * but not under others. If the table is empty, return NULL. */       \
250*c43e99fdSEd Maste   static inline struct type **                                          \
251*c43e99fdSEd Maste   name##_HT_START(struct name *head)                                    \
252*c43e99fdSEd Maste   {                                                                     \
253*c43e99fdSEd Maste     unsigned b = 0;                                                     \
254*c43e99fdSEd Maste     while (b < head->hth_table_length) {                                \
255*c43e99fdSEd Maste       if (head->hth_table[b])                                           \
256*c43e99fdSEd Maste         return &head->hth_table[b];                                     \
257*c43e99fdSEd Maste       ++b;                                                              \
258*c43e99fdSEd Maste     }                                                                   \
259*c43e99fdSEd Maste     return NULL;                                                        \
260*c43e99fdSEd Maste   }                                                                     \
261*c43e99fdSEd Maste   /* Return the next element in 'head' after 'elm', under the arbitrary \
262*c43e99fdSEd Maste    * order used by HT_START.  If there are no more elements, return     \
263*c43e99fdSEd Maste    * NULL.  If 'elm' is to be removed from the table, you must call     \
264*c43e99fdSEd Maste    * this function for the next value before you remove it.             \
265*c43e99fdSEd Maste    */                                                                   \
266*c43e99fdSEd Maste   static inline struct type **                                          \
267*c43e99fdSEd Maste   name##_HT_NEXT(struct name *head, struct type **elm)                  \
268*c43e99fdSEd Maste   {                                                                     \
269*c43e99fdSEd Maste     if ((*elm)->field.hte_next) {                                       \
270*c43e99fdSEd Maste       return &(*elm)->field.hte_next;                                   \
271*c43e99fdSEd Maste     } else {                                                            \
272*c43e99fdSEd Maste       unsigned b = (HT_ELT_HASH_(*elm, field, hashfn) % head->hth_table_length)+1; \
273*c43e99fdSEd Maste       while (b < head->hth_table_length) {                              \
274*c43e99fdSEd Maste         if (head->hth_table[b])                                         \
275*c43e99fdSEd Maste           return &head->hth_table[b];                                   \
276*c43e99fdSEd Maste         ++b;                                                            \
277*c43e99fdSEd Maste       }                                                                 \
278*c43e99fdSEd Maste       return NULL;                                                      \
279*c43e99fdSEd Maste     }                                                                   \
280*c43e99fdSEd Maste   }                                                                     \
281*c43e99fdSEd Maste   static inline struct type **                                          \
282*c43e99fdSEd Maste   name##_HT_NEXT_RMV(struct name *head, struct type **elm)              \
283*c43e99fdSEd Maste   {                                                                     \
284*c43e99fdSEd Maste     unsigned h = HT_ELT_HASH_(*elm, field, hashfn);		        \
285*c43e99fdSEd Maste     *elm = (*elm)->field.hte_next;                                      \
286*c43e99fdSEd Maste     --head->hth_n_entries;                                              \
287*c43e99fdSEd Maste     if (*elm) {                                                         \
288*c43e99fdSEd Maste       return elm;                                                       \
289*c43e99fdSEd Maste     } else {                                                            \
290*c43e99fdSEd Maste       unsigned b = (h % head->hth_table_length)+1;                      \
291*c43e99fdSEd Maste       while (b < head->hth_table_length) {                              \
292*c43e99fdSEd Maste         if (head->hth_table[b])                                         \
293*c43e99fdSEd Maste           return &head->hth_table[b];                                   \
294*c43e99fdSEd Maste         ++b;                                                            \
295*c43e99fdSEd Maste       }                                                                 \
296*c43e99fdSEd Maste       return NULL;                                                      \
297*c43e99fdSEd Maste     }                                                                   \
298*c43e99fdSEd Maste   }
299*c43e99fdSEd Maste 
300*c43e99fdSEd Maste #define HT_GENERATE(name, type, field, hashfn, eqfn, load, mallocfn,    \
301*c43e99fdSEd Maste                     reallocfn, freefn)                                  \
302*c43e99fdSEd Maste   static unsigned name##_PRIMES[] = {                                   \
303*c43e99fdSEd Maste     53, 97, 193, 389,                                                   \
304*c43e99fdSEd Maste     769, 1543, 3079, 6151,                                              \
305*c43e99fdSEd Maste     12289, 24593, 49157, 98317,                                         \
306*c43e99fdSEd Maste     196613, 393241, 786433, 1572869,                                    \
307*c43e99fdSEd Maste     3145739, 6291469, 12582917, 25165843,                               \
308*c43e99fdSEd Maste     50331653, 100663319, 201326611, 402653189,                          \
309*c43e99fdSEd Maste     805306457, 1610612741                                               \
310*c43e99fdSEd Maste   };                                                                    \
311*c43e99fdSEd Maste   static unsigned name##_N_PRIMES =                                     \
312*c43e99fdSEd Maste     (unsigned)(sizeof(name##_PRIMES)/sizeof(name##_PRIMES[0]));         \
313*c43e99fdSEd Maste   /* Expand the internal table of 'head' until it is large enough to    \
314*c43e99fdSEd Maste    * hold 'size' elements.  Return 0 on success, -1 on allocation       \
315*c43e99fdSEd Maste    * failure. */                                                        \
316*c43e99fdSEd Maste   int                                                                   \
317*c43e99fdSEd Maste   name##_HT_GROW(struct name *head, unsigned size)                      \
318*c43e99fdSEd Maste   {                                                                     \
319*c43e99fdSEd Maste     unsigned new_len, new_load_limit;                                   \
320*c43e99fdSEd Maste     int prime_idx;                                                      \
321*c43e99fdSEd Maste     struct type **new_table;                                            \
322*c43e99fdSEd Maste     if (head->hth_prime_idx == (int)name##_N_PRIMES - 1)                \
323*c43e99fdSEd Maste       return 0;                                                         \
324*c43e99fdSEd Maste     if (head->hth_load_limit > size)                                    \
325*c43e99fdSEd Maste       return 0;                                                         \
326*c43e99fdSEd Maste     prime_idx = head->hth_prime_idx;                                    \
327*c43e99fdSEd Maste     do {                                                                \
328*c43e99fdSEd Maste       new_len = name##_PRIMES[++prime_idx];                             \
329*c43e99fdSEd Maste       new_load_limit = (unsigned)(load*new_len);                        \
330*c43e99fdSEd Maste     } while (new_load_limit <= size &&                                  \
331*c43e99fdSEd Maste              prime_idx < (int)name##_N_PRIMES);                         \
332*c43e99fdSEd Maste     if ((new_table = mallocfn(new_len*sizeof(struct type*)))) {         \
333*c43e99fdSEd Maste       unsigned b;                                                       \
334*c43e99fdSEd Maste       memset(new_table, 0, new_len*sizeof(struct type*));               \
335*c43e99fdSEd Maste       for (b = 0; b < head->hth_table_length; ++b) {                    \
336*c43e99fdSEd Maste         struct type *elm, *next;                                        \
337*c43e99fdSEd Maste         unsigned b2;                                                    \
338*c43e99fdSEd Maste         elm = head->hth_table[b];                                       \
339*c43e99fdSEd Maste         while (elm) {                                                   \
340*c43e99fdSEd Maste           next = elm->field.hte_next;                                   \
341*c43e99fdSEd Maste           b2 = HT_ELT_HASH_(elm, field, hashfn) % new_len;              \
342*c43e99fdSEd Maste           elm->field.hte_next = new_table[b2];                          \
343*c43e99fdSEd Maste           new_table[b2] = elm;                                          \
344*c43e99fdSEd Maste           elm = next;                                                   \
345*c43e99fdSEd Maste         }                                                               \
346*c43e99fdSEd Maste       }                                                                 \
347*c43e99fdSEd Maste       if (head->hth_table)                                              \
348*c43e99fdSEd Maste         freefn(head->hth_table);                                        \
349*c43e99fdSEd Maste       head->hth_table = new_table;                                      \
350*c43e99fdSEd Maste     } else {                                                            \
351*c43e99fdSEd Maste       unsigned b, b2;                                                   \
352*c43e99fdSEd Maste       new_table = reallocfn(head->hth_table, new_len*sizeof(struct type*)); \
353*c43e99fdSEd Maste       if (!new_table) return -1;                                        \
354*c43e99fdSEd Maste       memset(new_table + head->hth_table_length, 0,                     \
355*c43e99fdSEd Maste              (new_len - head->hth_table_length)*sizeof(struct type*));  \
356*c43e99fdSEd Maste       for (b=0; b < head->hth_table_length; ++b) {                      \
357*c43e99fdSEd Maste         struct type *e, **pE;                                           \
358*c43e99fdSEd Maste         for (pE = &new_table[b], e = *pE; e != NULL; e = *pE) {         \
359*c43e99fdSEd Maste           b2 = HT_ELT_HASH_(e, field, hashfn) % new_len;                \
360*c43e99fdSEd Maste           if (b2 == b) {                                                \
361*c43e99fdSEd Maste             pE = &e->field.hte_next;                                    \
362*c43e99fdSEd Maste           } else {                                                      \
363*c43e99fdSEd Maste             *pE = e->field.hte_next;                                    \
364*c43e99fdSEd Maste             e->field.hte_next = new_table[b2];                          \
365*c43e99fdSEd Maste             new_table[b2] = e;                                          \
366*c43e99fdSEd Maste           }                                                             \
367*c43e99fdSEd Maste         }                                                               \
368*c43e99fdSEd Maste       }                                                                 \
369*c43e99fdSEd Maste       head->hth_table = new_table;                                      \
370*c43e99fdSEd Maste     }                                                                   \
371*c43e99fdSEd Maste     head->hth_table_length = new_len;                                   \
372*c43e99fdSEd Maste     head->hth_prime_idx = prime_idx;                                    \
373*c43e99fdSEd Maste     head->hth_load_limit = new_load_limit;                              \
374*c43e99fdSEd Maste     return 0;                                                           \
375*c43e99fdSEd Maste   }                                                                     \
376*c43e99fdSEd Maste   /* Free all storage held by 'head'.  Does not free 'head' itself, or  \
377*c43e99fdSEd Maste    * individual elements. */                                            \
378*c43e99fdSEd Maste   void                                                                  \
379*c43e99fdSEd Maste   name##_HT_CLEAR(struct name *head)                                    \
380*c43e99fdSEd Maste   {                                                                     \
381*c43e99fdSEd Maste     if (head->hth_table)                                                \
382*c43e99fdSEd Maste       freefn(head->hth_table);                                          \
383*c43e99fdSEd Maste     name##_HT_INIT(head);                                               \
384*c43e99fdSEd Maste   }                                                                     \
385*c43e99fdSEd Maste   /* Debugging helper: return false iff the representation of 'head' is \
386*c43e99fdSEd Maste    * internally consistent. */                                          \
387*c43e99fdSEd Maste   int                                                                   \
388*c43e99fdSEd Maste   name##_HT_REP_IS_BAD_(const struct name *head)			\
389*c43e99fdSEd Maste   {                                                                     \
390*c43e99fdSEd Maste     unsigned n, i;                                                      \
391*c43e99fdSEd Maste     struct type *elm;                                                   \
392*c43e99fdSEd Maste     if (!head->hth_table_length) {                                      \
393*c43e99fdSEd Maste       if (!head->hth_table && !head->hth_n_entries &&                   \
394*c43e99fdSEd Maste           !head->hth_load_limit && head->hth_prime_idx == -1)           \
395*c43e99fdSEd Maste         return 0;                                                       \
396*c43e99fdSEd Maste       else                                                              \
397*c43e99fdSEd Maste         return 1;                                                       \
398*c43e99fdSEd Maste     }                                                                   \
399*c43e99fdSEd Maste     if (!head->hth_table || head->hth_prime_idx < 0 ||                  \
400*c43e99fdSEd Maste         !head->hth_load_limit)                                          \
401*c43e99fdSEd Maste       return 2;                                                         \
402*c43e99fdSEd Maste     if (head->hth_n_entries > head->hth_load_limit)                     \
403*c43e99fdSEd Maste       return 3;                                                         \
404*c43e99fdSEd Maste     if (head->hth_table_length != name##_PRIMES[head->hth_prime_idx])   \
405*c43e99fdSEd Maste       return 4;                                                         \
406*c43e99fdSEd Maste     if (head->hth_load_limit != (unsigned)(load*head->hth_table_length)) \
407*c43e99fdSEd Maste       return 5;                                                         \
408*c43e99fdSEd Maste     for (n = i = 0; i < head->hth_table_length; ++i) {                  \
409*c43e99fdSEd Maste       for (elm = head->hth_table[i]; elm; elm = elm->field.hte_next) {  \
410*c43e99fdSEd Maste         if (HT_ELT_HASH_(elm, field, hashfn) != hashfn(elm))	        \
411*c43e99fdSEd Maste           return 1000 + i;                                              \
412*c43e99fdSEd Maste         if ((HT_ELT_HASH_(elm, field, hashfn) % head->hth_table_length) != i) \
413*c43e99fdSEd Maste           return 10000 + i;                                             \
414*c43e99fdSEd Maste         ++n;                                                            \
415*c43e99fdSEd Maste       }                                                                 \
416*c43e99fdSEd Maste     }                                                                   \
417*c43e99fdSEd Maste     if (n != head->hth_n_entries)                                       \
418*c43e99fdSEd Maste       return 6;                                                         \
419*c43e99fdSEd Maste     return 0;                                                           \
420*c43e99fdSEd Maste   }
421*c43e99fdSEd Maste 
422*c43e99fdSEd Maste /** Implements an over-optimized "find and insert if absent" block;
423*c43e99fdSEd Maste  * not meant for direct usage by typical code, or usage outside the critical
424*c43e99fdSEd Maste  * path.*/
425*c43e99fdSEd Maste #define HT_FIND_OR_INSERT_(name, field, hashfn, head, eltype, elm, var, y, n) \
426*c43e99fdSEd Maste   {                                                                     \
427*c43e99fdSEd Maste     struct name *var##_head_ = head;                                    \
428*c43e99fdSEd Maste     struct eltype **var;                                                \
429*c43e99fdSEd Maste     if (!var##_head_->hth_table ||                                      \
430*c43e99fdSEd Maste         var##_head_->hth_n_entries >= var##_head_->hth_load_limit)      \
431*c43e99fdSEd Maste       name##_HT_GROW(var##_head_, var##_head_->hth_n_entries+1);        \
432*c43e99fdSEd Maste     HT_SET_HASH_((elm), field, hashfn);                                 \
433*c43e99fdSEd Maste     var = name##_HT_FIND_P_(var##_head_, (elm));                        \
434*c43e99fdSEd Maste     if (*var) {                                                         \
435*c43e99fdSEd Maste       y;                                                                \
436*c43e99fdSEd Maste     } else {                                                            \
437*c43e99fdSEd Maste       n;                                                                \
438*c43e99fdSEd Maste     }                                                                   \
439*c43e99fdSEd Maste   }
440*c43e99fdSEd Maste #define HT_FOI_INSERT_(field, head, elm, newent, var)       \
441*c43e99fdSEd Maste   {                                                         \
442*c43e99fdSEd Maste     HT_SET_HASHVAL_(newent, field, (elm)->field.hte_hash);  \
443*c43e99fdSEd Maste     newent->field.hte_next = NULL;                          \
444*c43e99fdSEd Maste     *var = newent;                                          \
445*c43e99fdSEd Maste     ++((head)->hth_n_entries);                              \
446*c43e99fdSEd Maste   }
447*c43e99fdSEd Maste 
448*c43e99fdSEd Maste /*
449*c43e99fdSEd Maste  * Copyright 2005, Nick Mathewson.  Implementation logic is adapted from code
450*c43e99fdSEd Maste  * by Christopher Clark, retrofit to allow drop-in memory management, and to
451*c43e99fdSEd Maste  * use the same interface as Niels Provos's tree.h.  This is probably still
452*c43e99fdSEd Maste  * a derived work, so the original license below still applies.
453*c43e99fdSEd Maste  *
454*c43e99fdSEd Maste  * Copyright (c) 2002, Christopher Clark
455*c43e99fdSEd Maste  * All rights reserved.
456*c43e99fdSEd Maste  *
457*c43e99fdSEd Maste  * Redistribution and use in source and binary forms, with or without
458*c43e99fdSEd Maste  * modification, are permitted provided that the following conditions
459*c43e99fdSEd Maste  * are met:
460*c43e99fdSEd Maste  *
461*c43e99fdSEd Maste  * * Redistributions of source code must retain the above copyright
462*c43e99fdSEd Maste  * notice, this list of conditions and the following disclaimer.
463*c43e99fdSEd Maste  *
464*c43e99fdSEd Maste  * * Redistributions in binary form must reproduce the above copyright
465*c43e99fdSEd Maste  * notice, this list of conditions and the following disclaimer in the
466*c43e99fdSEd Maste  * documentation and/or other materials provided with the distribution.
467*c43e99fdSEd Maste  *
468*c43e99fdSEd Maste  * * Neither the name of the original author; nor the names of any contributors
469*c43e99fdSEd Maste  * may be used to endorse or promote products derived from this software
470*c43e99fdSEd Maste  * without specific prior written permission.
471*c43e99fdSEd Maste  *
472*c43e99fdSEd Maste  *
473*c43e99fdSEd Maste  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
474*c43e99fdSEd Maste  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
475*c43e99fdSEd Maste  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
476*c43e99fdSEd Maste  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER
477*c43e99fdSEd Maste  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
478*c43e99fdSEd Maste  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
479*c43e99fdSEd Maste  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
480*c43e99fdSEd Maste  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
481*c43e99fdSEd Maste  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
482*c43e99fdSEd Maste  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
483*c43e99fdSEd Maste  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
484*c43e99fdSEd Maste */
485*c43e99fdSEd Maste 
486*c43e99fdSEd Maste #endif
487*c43e99fdSEd Maste 
488