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