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