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