ht.h 29 KB

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