ht.h 30 KB

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