Back to index

tor  0.2.3.19-rc
ht.h
Go to the documentation of this file.
00001 /* Copyright (c) 2002, Christopher Clark.
00002  * Copyright (c) 2005-2006, Nick Mathewson.
00003  * Copyright (c) 2007-2012, The Tor Project, Inc. */
00004 /* See license at end. */
00005 
00006 /* Based on ideas by Christopher Clark and interfaces from Niels Provos. */
00007 
00008 #ifndef _TOR_HT_H
00009 #define _TOR_HT_H
00010 
00011 #define HT_HEAD(name, type)                                             \
00012   struct name {                                                         \
00013     /* The hash table itself. */                                        \
00014     struct type **hth_table;                                            \
00015     /* How long is the hash table? */                                   \
00016     unsigned hth_table_length;                                          \
00017     /* How many elements does the table contain? */                     \
00018     unsigned hth_n_entries;                                             \
00019     /* How many elements will we allow in the table before resizing it? */ \
00020     unsigned hth_load_limit;                                            \
00021     /* Position of hth_table_length in the primes table. */             \
00022     int hth_prime_idx;                                                  \
00023   }
00024 
00025 #define HT_INITIALIZER()                        \
00026   { NULL, 0, 0, 0, -1 }
00027 
00028 #ifdef HT_NO_CACHE_HASH_VALUES
00029 #define HT_ENTRY(type)                          \
00030   struct {                                      \
00031     struct type *hte_next;                      \
00032   }
00033 #else
00034 #define HT_ENTRY(type)                          \
00035   struct {                                      \
00036     struct type *hte_next;                      \
00037     unsigned hte_hash;                          \
00038   }
00039 #endif
00040 
00041 #define HT_EMPTY(head)                          \
00042   ((head)->hth_n_entries == 0)
00043 
00044 /* How many elements in 'head'? */
00045 #define HT_SIZE(head)                           \
00046   ((head)->hth_n_entries)
00047 
00048 /* Return memory usage for a hashtable (not counting the entries themselves) */
00049 #define HT_MEM_USAGE(head)                         \
00050   (sizeof(*head) + (head)->hth_table_length * sizeof(void*))
00051 
00052 #define HT_FIND(name, head, elm)     name##_HT_FIND((head), (elm))
00053 #define HT_INSERT(name, head, elm)   name##_HT_INSERT((head), (elm))
00054 #define HT_REPLACE(name, head, elm)  name##_HT_REPLACE((head), (elm))
00055 #define HT_REMOVE(name, head, elm)   name##_HT_REMOVE((head), (elm))
00056 #define HT_START(name, head)         name##_HT_START(head)
00057 #define HT_NEXT(name, head, elm)     name##_HT_NEXT((head), (elm))
00058 #define HT_NEXT_RMV(name, head, elm) name##_HT_NEXT_RMV((head), (elm))
00059 #define HT_CLEAR(name, head)         name##_HT_CLEAR(head)
00060 #define HT_INIT(name, head)          name##_HT_INIT(head)
00061 /* Helper: */
00062 static INLINE unsigned
00063 ht_improve_hash(unsigned h)
00064 {
00065   /* Aim to protect against poor hash functions by adding logic here
00066    * - logic taken from java 1.4 hashtable source */
00067   h += ~(h << 9);
00068   h ^=  ((h >> 14) | (h << 18)); /* >>> */
00069   h +=  (h << 4);
00070   h ^=  ((h >> 10) | (h << 22)); /* >>> */
00071   return h;
00072 }
00073 
00074 #if 0
00075 
00076 static INLINE unsigned
00077 ht_string_hash(const char *s)
00078 {
00079   unsigned h = 0;
00080   int m = 1;
00081   while (*s) {
00082     h += ((signed char)*s++)*m;
00083     m = (m<<5)-1; /* m *= 31 */
00084   }
00085   return h;
00086 }
00087 #endif
00088 
00090 static INLINE unsigned
00091 ht_string_hash(const char *s)
00092 {
00093   unsigned h;
00094   const unsigned char *cp = (const unsigned char *)s;
00095   h = *cp << 7;
00096   while (*cp) {
00097     h = (1000003*h) ^ *cp++;
00098   }
00099   /* This conversion truncates the length of the string, but that's ok. */
00100   h ^= (unsigned)(cp-(const unsigned char*)s);
00101   return h;
00102 }
00103 
00104 #ifndef HT_NO_CACHE_HASH_VALUES
00105 #define HT_SET_HASH_(elm, field, hashfn)        \
00106     do { (elm)->field.hte_hash = hashfn(elm); } while (0)
00107 #define HT_SET_HASHVAL_(elm, field, val)        \
00108     do { (elm)->field.hte_hash = (val); } while (0)
00109 #define HT_ELT_HASH_(elm, field, hashfn)        \
00110     ((elm)->field.hte_hash)
00111 #else
00112 #define HT_SET_HASH_(elm, field, hashfn)        \
00113     ((void)0)
00114 #define HT_ELT_HASH_(elm, field, hashfn)        \
00115     (hashfn(elm))
00116 #define HT_SET_HASHVAL_(elm, field, val)        \
00117         ((void)0)
00118 #endif
00119 
00120 /* Helper: alias for the bucket containing 'elm'. */
00121 #define HT_BUCKET_(head, field, elm, hashfn)                            \
00122     ((head)->hth_table[HT_ELT_HASH_(elm,field,hashfn)                   \
00123         % head->hth_table_length])
00124 
00125 #define HT_FOREACH(x, name, head)                 \
00126   for ((x) = HT_START(name, head);                \
00127        (x) != NULL;                               \
00128        (x) = HT_NEXT(name, head, x))
00129 
00130 #define HT_PROTOTYPE(name, type, field, hashfn, eqfn)                   \
00131   int name##_HT_GROW(struct name *ht, unsigned min_capacity);           \
00132   void name##_HT_CLEAR(struct name *ht);                                \
00133   int name##_HT_REP_IS_BAD_(const struct name *ht);                     \
00134   static INLINE void                                                    \
00135   name##_HT_INIT(struct name *head) {                                   \
00136     head->hth_table_length = 0;                                         \
00137     head->hth_table = NULL;                                             \
00138     head->hth_n_entries = 0;                                            \
00139     head->hth_load_limit = 0;                                           \
00140     head->hth_prime_idx = -1;                                           \
00141   }                                                                     \
00142   /* Helper: returns a pointer to the right location in the table       \
00143    * 'head' to find or insert the element 'elm'. */                     \
00144   static INLINE struct type **                                          \
00145   name##_HT_FIND_P_(struct name *head, struct type *elm)                \
00146   {                                                                     \
00147     struct type **p;                                                    \
00148     if (!head->hth_table)                                               \
00149       return NULL;                                                      \
00150     p = &HT_BUCKET_(head, field, elm, hashfn);                          \
00151     while (*p) {                                                        \
00152       if (eqfn(*p, elm))                                                \
00153         return p;                                                       \
00154       p = &(*p)->field.hte_next;                                        \
00155     }                                                                   \
00156     return p;                                                           \
00157   }                                                                     \
00158   /* Return a pointer to the element in the table 'head' matching 'elm', \
00159    * or NULL if no such element exists */                               \
00160   static INLINE struct type *                                           \
00161   name##_HT_FIND(const struct name *head, struct type *elm)             \
00162   {                                                                     \
00163     struct type **p;                                                    \
00164     struct name *h = (struct name *) head;                              \
00165     HT_SET_HASH_(elm, field, hashfn);                                   \
00166     p = name##_HT_FIND_P_(h, elm);                                      \
00167     return p ? *p : NULL;                                               \
00168   }                                                                     \
00169   /* Insert the element 'elm' into the table 'head'.  Do not call this  \
00170    * function if the table might already contain a matching element. */ \
00171   static INLINE void                                                    \
00172   name##_HT_INSERT(struct name *head, struct type *elm)                 \
00173   {                                                                     \
00174     struct type **p;                                                    \
00175     if (!head->hth_table || head->hth_n_entries >= head->hth_load_limit) \
00176       name##_HT_GROW(head, head->hth_n_entries+1);                      \
00177     ++head->hth_n_entries;                                              \
00178     HT_SET_HASH_(elm, field, hashfn);                                   \
00179     p = &HT_BUCKET_(head, field, elm, hashfn);                          \
00180     elm->field.hte_next = *p;                                           \
00181     *p = elm;                                                           \
00182   }                                                                     \
00183   /* Insert the element 'elm' into the table 'head'. If there already   \
00184    * a matching element in the table, replace that element and return   \
00185    * it. */                                                             \
00186   static INLINE struct type *                                           \
00187   name##_HT_REPLACE(struct name *head, struct type *elm)                \
00188   {                                                                     \
00189     struct type **p, *r;                                                \
00190     if (!head->hth_table || head->hth_n_entries >= head->hth_load_limit) \
00191       name##_HT_GROW(head, head->hth_n_entries+1);                      \
00192     HT_SET_HASH_(elm, field, hashfn);                                   \
00193     p = name##_HT_FIND_P_(head, elm);                                   \
00194     r = *p;                                                             \
00195     *p = elm;                                                           \
00196     if (r && (r!=elm)) {                                                \
00197       elm->field.hte_next = r->field.hte_next;                          \
00198       r->field.hte_next = NULL;                                         \
00199       return r;                                                         \
00200     } else {                                                            \
00201       ++head->hth_n_entries;                                            \
00202       return NULL;                                                      \
00203     }                                                                   \
00204   }                                                                     \
00205   /* Remove any element matching 'elm' from the table 'head'.  If such  \
00206    * an element is found, return it; otherwise return NULL. */          \
00207   static INLINE struct type *                                           \
00208   name##_HT_REMOVE(struct name *head, struct type *elm)                 \
00209   {                                                                     \
00210     struct type **p, *r;                                                \
00211     HT_SET_HASH_(elm, field, hashfn);                                   \
00212     p = name##_HT_FIND_P_(head,elm);                                    \
00213     if (!p || !*p)                                                      \
00214       return NULL;                                                      \
00215     r = *p;                                                             \
00216     *p = r->field.hte_next;                                             \
00217     r->field.hte_next = NULL;                                           \
00218     --head->hth_n_entries;                                              \
00219     return r;                                                           \
00220   }                                                                     \
00221   /* Invoke the function 'fn' on every element of the table 'head',     \
00222    * using 'data' as its second argument.  If the function returns      \
00223    * nonzero, remove the most recently examined element before invoking \
00224    * the function again. */                                             \
00225   static INLINE void                                                    \
00226   name##_HT_FOREACH_FN(struct name *head,                               \
00227                        int (*fn)(struct type *, void *),                \
00228                        void *data)                                      \
00229   {                                                                     \
00230     unsigned idx;                                                       \
00231     struct type **p, **nextp, *next;                                    \
00232     if (!head->hth_table)                                               \
00233       return;                                                           \
00234     for (idx=0; idx < head->hth_table_length; ++idx) {                  \
00235       p = &head->hth_table[idx];                                        \
00236       while (*p) {                                                      \
00237         nextp = &(*p)->field.hte_next;                                  \
00238         next = *nextp;                                                  \
00239         if (fn(*p, data)) {                                             \
00240           --head->hth_n_entries;                                        \
00241           *p = next;                                                    \
00242         } else {                                                        \
00243           p = nextp;                                                    \
00244         }                                                               \
00245       }                                                                 \
00246     }                                                                   \
00247   }                                                                     \
00248   /* Return a pointer to the first element in the table 'head', under   \
00249    * an arbitrary order.  This order is stable under remove operations, \
00250    * but not under others. If the table is empty, return NULL. */       \
00251   static INLINE struct type **                                          \
00252   name##_HT_START(struct name *head)                                    \
00253   {                                                                     \
00254     unsigned b = 0;                                                     \
00255     while (b < head->hth_table_length) {                                \
00256       if (head->hth_table[b])                                           \
00257         return &head->hth_table[b];                                     \
00258       ++b;                                                              \
00259     }                                                                   \
00260     return NULL;                                                        \
00261   }                                                                     \
00262   /* Return the next element in 'head' after 'elm', under the arbitrary \
00263    * order used by HT_START.  If there are no more elements, return     \
00264    * NULL.  If 'elm' is to be removed from the table, you must call     \
00265    * this function for the next value before you remove it.             \
00266    */                                                                   \
00267   static INLINE struct type **                                          \
00268   name##_HT_NEXT(struct name *head, struct type **elm)                  \
00269   {                                                                     \
00270     if ((*elm)->field.hte_next) {                                       \
00271       return &(*elm)->field.hte_next;                                   \
00272     } else {                                                            \
00273       unsigned b = (HT_ELT_HASH_(*elm, field, hashfn)                   \
00274       % head->hth_table_length)+1;                                      \
00275       while (b < head->hth_table_length) {                              \
00276         if (head->hth_table[b])                                         \
00277           return &head->hth_table[b];                                   \
00278         ++b;                                                            \
00279       }                                                                 \
00280       return NULL;                                                      \
00281     }                                                                   \
00282   }                                                                     \
00283   static INLINE struct type **                                          \
00284   name##_HT_NEXT_RMV(struct name *head, struct type **elm)              \
00285   {                                                                     \
00286     unsigned h = HT_ELT_HASH_(*elm, field, hashfn);                     \
00287     *elm = (*elm)->field.hte_next;                                      \
00288     --head->hth_n_entries;                                              \
00289     if (*elm) {                                                         \
00290       return elm;                                                       \
00291     } else {                                                            \
00292       unsigned b = (h % head->hth_table_length)+1;                      \
00293       while (b < head->hth_table_length) {                              \
00294         if (head->hth_table[b])                                         \
00295           return &head->hth_table[b];                                   \
00296         ++b;                                                            \
00297       }                                                                 \
00298       return NULL;                                                      \
00299     }                                                                   \
00300   }
00301 
00302 #define HT_GENERATE(name, type, field, hashfn, eqfn, load, mallocfn,    \
00303                     reallocfn, freefn)                                  \
00304   static unsigned name##_PRIMES[] = {                                   \
00305     53, 97, 193, 389,                                                   \
00306     769, 1543, 3079, 6151,                                              \
00307     12289, 24593, 49157, 98317,                                         \
00308     196613, 393241, 786433, 1572869,                                    \
00309     3145739, 6291469, 12582917, 25165843,                               \
00310     50331653, 100663319, 201326611, 402653189,                          \
00311     805306457, 1610612741                                               \
00312   };                                                                    \
00313   static unsigned name##_N_PRIMES =                                     \
00314     (unsigned)(sizeof(name##_PRIMES)/sizeof(name##_PRIMES[0]));         \
00315   /* Expand the internal table of 'head' until it is large enough to    \
00316    * hold 'size' elements.  Return 0 on success, -1 on allocation       \
00317    * failure. */                                                        \
00318   int                                                                   \
00319   name##_HT_GROW(struct name *head, unsigned size)                      \
00320   {                                                                     \
00321     unsigned new_len, new_load_limit;                                   \
00322     int prime_idx;                                                      \
00323     struct type **new_table;                                            \
00324     if (head->hth_prime_idx == (int)name##_N_PRIMES - 1)                \
00325       return 0;                                                         \
00326     if (head->hth_load_limit > size)                                    \
00327       return 0;                                                         \
00328     prime_idx = head->hth_prime_idx;                                    \
00329     do {                                                                \
00330       new_len = name##_PRIMES[++prime_idx];                             \
00331       new_load_limit = (unsigned)(load*new_len);                        \
00332     } while (new_load_limit <= size &&                                  \
00333              prime_idx < (int)name##_N_PRIMES);                         \
00334     if ((new_table = mallocfn(new_len*sizeof(struct type*)))) {         \
00335       unsigned b;                                                       \
00336       memset(new_table, 0, new_len*sizeof(struct type*));               \
00337       for (b = 0; b < head->hth_table_length; ++b) {                    \
00338         struct type *elm, *next;                                        \
00339         unsigned b2;                                                    \
00340         elm = head->hth_table[b];                                       \
00341         while (elm) {                                                   \
00342           next = elm->field.hte_next;                                   \
00343           b2 = HT_ELT_HASH_(elm, field, hashfn) % new_len;              \
00344           elm->field.hte_next = new_table[b2];                          \
00345           new_table[b2] = elm;                                          \
00346           elm = next;                                                   \
00347         }                                                               \
00348       }                                                                 \
00349       if (head->hth_table)                                              \
00350         freefn(head->hth_table);                                        \
00351       head->hth_table = new_table;                                      \
00352     } else {                                                            \
00353       unsigned b, b2;                                                   \
00354       new_table = reallocfn(head->hth_table, new_len*sizeof(struct type*)); \
00355       if (!new_table) return -1;                                        \
00356       memset(new_table + head->hth_table_length, 0,                     \
00357              (new_len - head->hth_table_length)*sizeof(struct type*));  \
00358       for (b=0; b < head->hth_table_length; ++b) {                      \
00359         struct type *e, **pE;                                           \
00360         for (pE = &new_table[b], e = *pE; e != NULL; e = *pE) {         \
00361           b2 = HT_ELT_HASH_(e, field, hashfn) % new_len;                \
00362           if (b2 == b) {                                                \
00363             pE = &e->field.hte_next;                                    \
00364           } else {                                                      \
00365             *pE = e->field.hte_next;                                    \
00366             e->field.hte_next = new_table[b2];                          \
00367             new_table[b2] = e;                                          \
00368           }                                                             \
00369         }                                                               \
00370       }                                                                 \
00371       head->hth_table = new_table;                                      \
00372     }                                                                   \
00373     head->hth_table_length = new_len;                                   \
00374     head->hth_prime_idx = prime_idx;                                    \
00375     head->hth_load_limit = new_load_limit;                              \
00376     return 0;                                                           \
00377   }                                                                     \
00378   /* Free all storage held by 'head'.  Does not free 'head' itself, or  \
00379    * individual elements. */                                            \
00380   void                                                                  \
00381   name##_HT_CLEAR(struct name *head)                                    \
00382   {                                                                     \
00383     if (head->hth_table)                                                \
00384       freefn(head->hth_table);                                          \
00385     head->hth_table_length = 0;                                         \
00386     name##_HT_INIT(head);                                               \
00387   }                                                                     \
00388   /* Debugging helper: return false iff the representation of 'head' is \
00389    * internally consistent. */                                          \
00390   int                                                                   \
00391   name##_HT_REP_IS_BAD_(const struct name *head)                        \
00392   {                                                                     \
00393     unsigned n, i;                                                      \
00394     struct type *elm;                                                   \
00395     if (!head->hth_table_length) {                                      \
00396       if (!head->hth_table && !head->hth_n_entries &&                   \
00397           !head->hth_load_limit && head->hth_prime_idx == -1)           \
00398         return 0;                                                       \
00399       else                                                              \
00400         return 1;                                                       \
00401     }                                                                   \
00402     if (!head->hth_table || head->hth_prime_idx < 0 ||                  \
00403         !head->hth_load_limit)                                          \
00404       return 2;                                                         \
00405     if (head->hth_n_entries > head->hth_load_limit)                     \
00406       return 3;                                                         \
00407     if (head->hth_table_length != name##_PRIMES[head->hth_prime_idx])   \
00408       return 4;                                                         \
00409     if (head->hth_load_limit != (unsigned)(load*head->hth_table_length)) \
00410       return 5;                                                         \
00411     for (n = i = 0; i < head->hth_table_length; ++i) {                  \
00412       for (elm = head->hth_table[i]; elm; elm = elm->field.hte_next) {  \
00413         if (HT_ELT_HASH_(elm, field, hashfn) != hashfn(elm))            \
00414           return 1000 + i;                                              \
00415         if ((HT_ELT_HASH_(elm, field, hashfn) % head->hth_table_length) != i) \
00416           return 10000 + i;                                             \
00417         ++n;                                                            \
00418       }                                                                 \
00419     }                                                                   \
00420     if (n != head->hth_n_entries)                                       \
00421       return 6;                                                         \
00422     return 0;                                                           \
00423   }
00424 
00428 #define HT_FIND_OR_INSERT_(name, field, hashfn, head, eltype, elm, var, y, n) \
00429   {                                                                     \
00430     struct name *var##_head_ = head;                                    \
00431     struct eltype **var;                                                \
00432     if (!var##_head_->hth_table ||                                      \
00433         var##_head_->hth_n_entries >= var##_head_->hth_load_limit)      \
00434       name##_HT_GROW(var##_head_, var##_head_->hth_n_entries+1);        \
00435     HT_SET_HASH_((elm), field, hashfn);                                 \
00436     var = name##_HT_FIND_P_(var##_head_, (elm));                        \
00437     if (*var) {                                                         \
00438       y;                                                                \
00439     } else {                                                            \
00440       n;                                                                \
00441     }                                                                   \
00442   }
00443 #define HT_FOI_INSERT_(field, head, elm, newent, var)       \
00444   {                                                         \
00445     HT_SET_HASHVAL_(newent, field, (elm)->field.hte_hash);  \
00446     newent->field.hte_next = NULL;                          \
00447     *var = newent;                                          \
00448     ++((head)->hth_n_entries);                              \
00449   }
00450 
00451 /*
00452  * Copyright 2005, Nick Mathewson.  Implementation logic is adapted from code
00453  * by Christopher Clark, retrofit to allow drop-in memory management, and to
00454  * use the same interface as Niels Provos's tree.h.  This is probably still
00455  * a derived work, so the original license below still applies.
00456  *
00457  * Copyright (c) 2002, Christopher Clark
00458  * All rights reserved.
00459  *
00460  * Redistribution and use in source and binary forms, with or without
00461  * modification, are permitted provided that the following conditions
00462  * are met:
00463  *
00464  * * Redistributions of source code must retain the above copyright
00465  * notice, this list of conditions and the following disclaimer.
00466  *
00467  * * Redistributions in binary form must reproduce the above copyright
00468  * notice, this list of conditions and the following disclaimer in the
00469  * documentation and/or other materials provided with the distribution.
00470  *
00471  * * Neither the name of the original author; nor the names of any contributors
00472  * may be used to endorse or promote products derived from this software
00473  * without specific prior written permission.
00474  *
00475  *
00476  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00477  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00478  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00479  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER
00480  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00481  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00482  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00483  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00484  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00485  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00486  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00487 */
00488 
00489 #endif
00490