ht.h 28 KB

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