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