ht.h 29 KB

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