ht.h 28 KB

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