ht.h 28 KB

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