ht.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /* Copyright (c) 2002, Christopher Clark.
  2. * Copyright (c) 2005-2006, Nick Mathewson.
  3. * Copyright (c) 2007-2017, The Tor Project, Inc. */
  4. /* See license at end. */
  5. /* Based on ideas by Christopher Clark and interfaces from Niels Provos. */
  6. /*
  7. These macros provide an intrustive implementation for a typesafe chaining
  8. hash table, loosely based on the BSD tree.h and queue.h macros. Here's
  9. how to use them.
  10. First, pick a the structure that you'll be storing in the hashtable. Let's
  11. say that's "struct dinosaur". To this structure, you add an HT_ENTRY()
  12. member, as such:
  13. struct dinosaur {
  14. HT_ENTRY(dinosaur) node; // The name inside the () must match the
  15. // struct.
  16. // These are just fields from the dinosaur structure...
  17. long dinosaur_id;
  18. char *name;
  19. long age;
  20. int is_ornithischian;
  21. int is_herbivorous;
  22. };
  23. You can declare the hashtable itself as:
  24. HT_HEAD(dinosaur_ht, dinosaur);
  25. This declares a new 'struct dinosaur_ht' type.
  26. Now you need to declare two functions to help implement the hashtable: one
  27. compares two dinosaurs for equality, and one computes the hash of a
  28. dinosaur. Let's say that two dinosaurs are equal if they have the same ID
  29. and name.
  30. int
  31. dinosaurs_equal(const struct dinosaur *d1, const struct dinosaur *d2)
  32. {
  33. return d1->dinosaur_id == d2->dinosaur_id &&
  34. 0 == strcmp(d1->name, d2->name);
  35. }
  36. unsigned
  37. dinosaur_hash(const struct dinosaur *d)
  38. {
  39. // This is a very bad hash function. Use siphash24g instead.
  40. return (d->dinosaur_id + d->name[0] ) * 1337 + d->name[1] * 1337;
  41. }
  42. Now you'll need to declare the functions that manipulate the hash table.
  43. To do this, you put this declaration either in a header file, or inside
  44. a regular module, depending on what visibility you want.
  45. HT_PROTOTYPE(dinosaur_ht, // The name of the hashtable struct
  46. dinosaur, // The name of the element struct,
  47. node, // The name of HT_ENTRY member
  48. dinosaur_hash, dinosaurs_equal);
  49. Later, inside a C function, you use this macro to declare the hashtable
  50. functions.
  51. HT_GENERATE2(dinosaur_ht, dinosaur, node, dinosaur_hash, dinosaurs_equal,
  52. 0.6, tor_reallocarray, tor_free_);
  53. Note the use of tor_free_, not tor_free. The 0.6 is magic.
  54. Now you can use the hashtable! You can initialize one with
  55. struct dinosaur_ht my_dinos = HT_INITIALIZER();
  56. Or create one in core with
  57. struct dinosaur_ht *dinos = tor_malloc(sizeof(dinosaur_ht));
  58. HT_INIT(dinosaur_ht, dinos);
  59. To the hashtable, you use the HT_FOO(dinosaur_ht, ...) macros. For
  60. example, to put new_dino into dinos, you say:
  61. HT_REPLACE(dinosaur_ht, dinos, new_dino);
  62. If you're searching for an element, you need to use a dummy 'key' element in
  63. the search. For example.
  64. struct dinosaur dino_key;
  65. dino_key.dinosaur_id = 12345;
  66. dino_key.name = tor_strdup("Atrociraptor");
  67. struct dinosaur *found = HT_FIND(dinosaurs_ht, dinos, &dino_key);
  68. Have fun with your hash table!
  69. */
  70. #ifndef HT_H_INCLUDED_
  71. #define HT_H_INCLUDED_
  72. #define HT_HEAD(name, type) \
  73. struct name { \
  74. /* The hash table itself. */ \
  75. struct type **hth_table; \
  76. /* How long is the hash table? */ \
  77. unsigned hth_table_length; \
  78. /* How many elements does the table contain? */ \
  79. unsigned hth_n_entries; \
  80. /* How many elements will we allow in the table before resizing it? */ \
  81. unsigned hth_load_limit; \
  82. /* Position of hth_table_length in the primes table. */ \
  83. int hth_prime_idx; \
  84. }
  85. #define HT_INITIALIZER() \
  86. { NULL, 0, 0, 0, -1 }
  87. #ifdef HT_NO_CACHE_HASH_VALUES
  88. #define HT_ENTRY(type) \
  89. struct { \
  90. struct type *hte_next; \
  91. }
  92. #else
  93. #define HT_ENTRY(type) \
  94. struct { \
  95. struct type *hte_next; \
  96. unsigned hte_hash; \
  97. }
  98. #endif
  99. /* || 0 is for -Wparentheses-equality (-Wall?) appeasement under clang */
  100. #define HT_EMPTY(head) \
  101. (((head)->hth_n_entries == 0) || 0)
  102. /* How many elements in 'head'? */
  103. #define HT_SIZE(head) \
  104. ((head)->hth_n_entries)
  105. /* Return memory usage for a hashtable (not counting the entries themselves) */
  106. #define HT_MEM_USAGE(head) \
  107. (sizeof(*head) + (head)->hth_table_length * sizeof(void*))
  108. #define HT_FIND(name, head, elm) name##_HT_FIND((head), (elm))
  109. #define HT_INSERT(name, head, elm) name##_HT_INSERT((head), (elm))
  110. #define HT_REPLACE(name, head, elm) name##_HT_REPLACE((head), (elm))
  111. #define HT_REMOVE(name, head, elm) name##_HT_REMOVE((head), (elm))
  112. #define HT_START(name, head) name##_HT_START(head)
  113. #define HT_NEXT(name, head, elm) name##_HT_NEXT((head), (elm))
  114. #define HT_NEXT_RMV(name, head, elm) name##_HT_NEXT_RMV((head), (elm))
  115. #define HT_CLEAR(name, head) name##_HT_CLEAR(head)
  116. #define HT_INIT(name, head) name##_HT_INIT(head)
  117. #define HT_REP_IS_BAD_(name, head) name##_HT_REP_IS_BAD_(head)
  118. /* Helper: */
  119. static inline unsigned
  120. ht_improve_hash(unsigned h)
  121. {
  122. /* Aim to protect against poor hash functions by adding logic here
  123. * - logic taken from java 1.4 hashtable source */
  124. h += ~(h << 9);
  125. h ^= ((h >> 14) | (h << 18)); /* >>> */
  126. h += (h << 4);
  127. h ^= ((h >> 10) | (h << 22)); /* >>> */
  128. return h;
  129. }
  130. #if 0
  131. /** Basic string hash function, from Java standard String.hashCode(). */
  132. static inline unsigned
  133. ht_string_hash(const char *s)
  134. {
  135. unsigned h = 0;
  136. int m = 1;
  137. while (*s) {
  138. h += ((signed char)*s++)*m;
  139. m = (m<<5)-1; /* m *= 31 */
  140. }
  141. return h;
  142. }
  143. #endif
  144. #if 0
  145. /** Basic string hash function, from Python's str.__hash__() */
  146. static inline unsigned
  147. ht_string_hash(const char *s)
  148. {
  149. unsigned h;
  150. const unsigned char *cp = (const unsigned char *)s;
  151. h = *cp << 7;
  152. while (*cp) {
  153. h = (1000003*h) ^ *cp++;
  154. }
  155. /* This conversion truncates the length of the string, but that's ok. */
  156. h ^= (unsigned)(cp-(const unsigned char*)s);
  157. return h;
  158. }
  159. #endif
  160. #ifndef HT_NO_CACHE_HASH_VALUES
  161. #define HT_SET_HASH_(elm, field, hashfn) \
  162. do { (elm)->field.hte_hash = hashfn(elm); } while (0)
  163. #define HT_SET_HASHVAL_(elm, field, val) \
  164. do { (elm)->field.hte_hash = (val); } while (0)
  165. #define HT_ELT_HASH_(elm, field, hashfn) \
  166. ((elm)->field.hte_hash)
  167. #else
  168. #define HT_SET_HASH_(elm, field, hashfn) \
  169. ((void)0)
  170. #define HT_ELT_HASH_(elm, field, hashfn) \
  171. (hashfn(elm))
  172. #define HT_SET_HASHVAL_(elm, field, val) \
  173. ((void)0)
  174. #endif
  175. #define HT_BUCKET_NUM_(head, field, elm, hashfn) \
  176. (HT_ELT_HASH_(elm,field,hashfn) % head->hth_table_length)
  177. /* Helper: alias for the bucket containing 'elm'. */
  178. #define HT_BUCKET_(head, field, elm, hashfn) \
  179. ((head)->hth_table[HT_BUCKET_NUM_(head, field, elm, hashfn)])
  180. #define HT_FOREACH(x, name, head) \
  181. for ((x) = HT_START(name, head); \
  182. (x) != NULL; \
  183. (x) = HT_NEXT(name, head, x))
  184. #ifndef HT_NDEBUG
  185. #define HT_ASSERT_(x) tor_assert(x)
  186. #else
  187. #define HT_ASSERT_(x) (void)0
  188. #endif
  189. #define HT_PROTOTYPE(name, type, field, hashfn, eqfn) \
  190. int name##_HT_GROW(struct name *ht, unsigned min_capacity); \
  191. void name##_HT_CLEAR(struct name *ht); \
  192. int name##_HT_REP_IS_BAD_(const struct name *ht); \
  193. static inline void \
  194. name##_HT_INIT(struct name *head) { \
  195. head->hth_table_length = 0; \
  196. head->hth_table = NULL; \
  197. head->hth_n_entries = 0; \
  198. head->hth_load_limit = 0; \
  199. head->hth_prime_idx = -1; \
  200. } \
  201. /* Helper: returns a pointer to the right location in the table \
  202. * 'head' to find or insert the element 'elm'. */ \
  203. static inline struct type ** \
  204. name##_HT_FIND_P_(struct name *head, struct type *elm) \
  205. { \
  206. struct type **p; \
  207. if (!head->hth_table) \
  208. return NULL; \
  209. p = &HT_BUCKET_(head, field, elm, hashfn); \
  210. while (*p) { \
  211. if (eqfn(*p, elm)) \
  212. return p; \
  213. p = &(*p)->field.hte_next; \
  214. } \
  215. return p; \
  216. } \
  217. /* Return a pointer to the element in the table 'head' matching 'elm', \
  218. * or NULL if no such element exists */ \
  219. ATTR_UNUSED static inline struct type * \
  220. name##_HT_FIND(const struct name *head, struct type *elm) \
  221. { \
  222. struct type **p; \
  223. struct name *h = (struct name *) head; \
  224. HT_SET_HASH_(elm, field, hashfn); \
  225. p = name##_HT_FIND_P_(h, elm); \
  226. return p ? *p : NULL; \
  227. } \
  228. /* Insert the element 'elm' into the table 'head'. Do not call this \
  229. * function if the table might already contain a matching element. */ \
  230. ATTR_UNUSED static inline void \
  231. name##_HT_INSERT(struct name *head, struct type *elm) \
  232. { \
  233. struct type **p; \
  234. if (!head->hth_table || head->hth_n_entries >= head->hth_load_limit) \
  235. name##_HT_GROW(head, head->hth_n_entries+1); \
  236. ++head->hth_n_entries; \
  237. HT_SET_HASH_(elm, field, hashfn); \
  238. p = &HT_BUCKET_(head, field, elm, hashfn); \
  239. elm->field.hte_next = *p; \
  240. *p = elm; \
  241. } \
  242. /* Insert the element 'elm' into the table 'head'. If there already \
  243. * a matching element in the table, replace that element and return \
  244. * it. */ \
  245. ATTR_UNUSED static inline struct type * \
  246. name##_HT_REPLACE(struct name *head, struct type *elm) \
  247. { \
  248. struct type **p, *r; \
  249. if (!head->hth_table || head->hth_n_entries >= head->hth_load_limit) \
  250. name##_HT_GROW(head, head->hth_n_entries+1); \
  251. HT_SET_HASH_(elm, field, hashfn); \
  252. p = name##_HT_FIND_P_(head, elm); \
  253. HT_ASSERT_(p != NULL); /* this holds because we called HT_GROW */ \
  254. r = *p; \
  255. *p = elm; \
  256. if (r && (r!=elm)) { \
  257. elm->field.hte_next = r->field.hte_next; \
  258. r->field.hte_next = NULL; \
  259. return r; \
  260. } else { \
  261. ++head->hth_n_entries; \
  262. return NULL; \
  263. } \
  264. } \
  265. /* Remove any element matching 'elm' from the table 'head'. If such \
  266. * an element is found, return it; otherwise return NULL. */ \
  267. ATTR_UNUSED static inline struct type * \
  268. name##_HT_REMOVE(struct name *head, struct type *elm) \
  269. { \
  270. struct type **p, *r; \
  271. HT_SET_HASH_(elm, field, hashfn); \
  272. p = name##_HT_FIND_P_(head,elm); \
  273. if (!p || !*p) \
  274. return NULL; \
  275. r = *p; \
  276. *p = r->field.hte_next; \
  277. r->field.hte_next = NULL; \
  278. --head->hth_n_entries; \
  279. return r; \
  280. } \
  281. /* Invoke the function 'fn' on every element of the table 'head', \
  282. * using 'data' as its second argument. If the function returns \
  283. * nonzero, remove the most recently examined element before invoking \
  284. * the function again. */ \
  285. ATTR_UNUSED static inline void \
  286. name##_HT_FOREACH_FN(struct name *head, \
  287. int (*fn)(struct type *, void *), \
  288. void *data) \
  289. { \
  290. unsigned idx; \
  291. struct type **p, **nextp, *next; \
  292. if (!head->hth_table) \
  293. return; \
  294. for (idx=0; idx < head->hth_table_length; ++idx) { \
  295. p = &head->hth_table[idx]; \
  296. while (*p) { \
  297. nextp = &(*p)->field.hte_next; \
  298. next = *nextp; \
  299. if (fn(*p, data)) { \
  300. --head->hth_n_entries; \
  301. *p = next; \
  302. } else { \
  303. p = nextp; \
  304. } \
  305. } \
  306. } \
  307. } \
  308. /* Return a pointer to the first element in the table 'head', under \
  309. * an arbitrary order. This order is stable under remove operations, \
  310. * but not under others. If the table is empty, return NULL. */ \
  311. ATTR_UNUSED static inline struct type ** \
  312. name##_HT_START(struct name *head) \
  313. { \
  314. unsigned b = 0; \
  315. while (b < head->hth_table_length) { \
  316. if (head->hth_table[b]) { \
  317. HT_ASSERT_(b == \
  318. HT_BUCKET_NUM_(head,field,head->hth_table[b],hashfn)); \
  319. return &head->hth_table[b]; \
  320. } \
  321. ++b; \
  322. } \
  323. return NULL; \
  324. } \
  325. /* Return the next element in 'head' after 'elm', under the arbitrary \
  326. * order used by HT_START. If there are no more elements, return \
  327. * NULL. If 'elm' is to be removed from the table, you must call \
  328. * this function for the next value before you remove it. \
  329. */ \
  330. ATTR_UNUSED static inline struct type ** \
  331. name##_HT_NEXT(struct name *head, struct type **elm) \
  332. { \
  333. if ((*elm)->field.hte_next) { \
  334. HT_ASSERT_(HT_BUCKET_NUM_(head,field,*elm,hashfn) == \
  335. HT_BUCKET_NUM_(head,field,(*elm)->field.hte_next,hashfn)); \
  336. return &(*elm)->field.hte_next; \
  337. } else { \
  338. unsigned b = HT_BUCKET_NUM_(head,field,*elm,hashfn)+1; \
  339. while (b < head->hth_table_length) { \
  340. if (head->hth_table[b]) { \
  341. HT_ASSERT_(b == \
  342. HT_BUCKET_NUM_(head,field,head->hth_table[b],hashfn)); \
  343. return &head->hth_table[b]; \
  344. } \
  345. ++b; \
  346. } \
  347. return NULL; \
  348. } \
  349. } \
  350. ATTR_UNUSED static inline struct type ** \
  351. name##_HT_NEXT_RMV(struct name *head, struct type **elm) \
  352. { \
  353. unsigned h = HT_ELT_HASH_(*elm, field, hashfn); \
  354. *elm = (*elm)->field.hte_next; \
  355. --head->hth_n_entries; \
  356. if (*elm) { \
  357. return elm; \
  358. } else { \
  359. unsigned b = (h % head->hth_table_length)+1; \
  360. while (b < head->hth_table_length) { \
  361. if (head->hth_table[b]) \
  362. return &head->hth_table[b]; \
  363. ++b; \
  364. } \
  365. return NULL; \
  366. } \
  367. }
  368. #define HT_GENERATE2(name, type, field, hashfn, eqfn, load, reallocarrayfn, \
  369. freefn) \
  370. /* Primes that aren't too far from powers of two. We stop at */ \
  371. /* P=402653189 because P*sizeof(void*) is less than SSIZE_MAX */ \
  372. /* even on a 32-bit platform. */ \
  373. static unsigned name##_PRIMES[] = { \
  374. 53, 97, 193, 389, \
  375. 769, 1543, 3079, 6151, \
  376. 12289, 24593, 49157, 98317, \
  377. 196613, 393241, 786433, 1572869, \
  378. 3145739, 6291469, 12582917, 25165843, \
  379. 50331653, 100663319, 201326611, 402653189 \
  380. }; \
  381. static unsigned name##_N_PRIMES = \
  382. (unsigned)(sizeof(name##_PRIMES)/sizeof(name##_PRIMES[0])); \
  383. /* Expand the internal table of 'head' until it is large enough to \
  384. * hold 'size' elements. Return 0 on success, -1 on allocation \
  385. * failure. */ \
  386. int \
  387. name##_HT_GROW(struct name *head, unsigned size) \
  388. { \
  389. unsigned new_len, new_load_limit; \
  390. int prime_idx; \
  391. struct type **new_table; \
  392. if (head->hth_prime_idx == (int)name##_N_PRIMES - 1) \
  393. return 0; \
  394. if (head->hth_load_limit > size) \
  395. return 0; \
  396. prime_idx = head->hth_prime_idx; \
  397. do { \
  398. new_len = name##_PRIMES[++prime_idx]; \
  399. new_load_limit = (unsigned)(load*new_len); \
  400. } while (new_load_limit <= size && \
  401. prime_idx < (int)name##_N_PRIMES); \
  402. if ((new_table = reallocarrayfn(NULL, new_len, sizeof(struct type*)))) { \
  403. unsigned b; \
  404. memset(new_table, 0, new_len*sizeof(struct type*)); \
  405. for (b = 0; b < head->hth_table_length; ++b) { \
  406. struct type *elm, *next; \
  407. unsigned b2; \
  408. elm = head->hth_table[b]; \
  409. while (elm) { \
  410. next = elm->field.hte_next; \
  411. b2 = HT_ELT_HASH_(elm, field, hashfn) % new_len; \
  412. elm->field.hte_next = new_table[b2]; \
  413. new_table[b2] = elm; \
  414. elm = next; \
  415. } \
  416. } \
  417. if (head->hth_table) \
  418. freefn(head->hth_table); \
  419. head->hth_table = new_table; \
  420. } else { \
  421. unsigned b, b2; \
  422. new_table = reallocarrayfn(head->hth_table, new_len, sizeof(struct type*)); \
  423. if (!new_table) return -1; \
  424. memset(new_table + head->hth_table_length, 0, \
  425. (new_len - head->hth_table_length)*sizeof(struct type*)); \
  426. for (b=0; b < head->hth_table_length; ++b) { \
  427. struct type *e, **pE; \
  428. for (pE = &new_table[b], e = *pE; e != NULL; e = *pE) { \
  429. b2 = HT_ELT_HASH_(e, field, hashfn) % new_len; \
  430. if (b2 == b) { \
  431. pE = &e->field.hte_next; \
  432. } else { \
  433. *pE = e->field.hte_next; \
  434. e->field.hte_next = new_table[b2]; \
  435. new_table[b2] = e; \
  436. } \
  437. } \
  438. } \
  439. head->hth_table = new_table; \
  440. } \
  441. head->hth_table_length = new_len; \
  442. head->hth_prime_idx = prime_idx; \
  443. head->hth_load_limit = new_load_limit; \
  444. return 0; \
  445. } \
  446. /* Free all storage held by 'head'. Does not free 'head' itself, or \
  447. * individual elements. */ \
  448. void \
  449. name##_HT_CLEAR(struct name *head) \
  450. { \
  451. if (head->hth_table) \
  452. freefn(head->hth_table); \
  453. head->hth_table_length = 0; \
  454. name##_HT_INIT(head); \
  455. } \
  456. /* Debugging helper: return false iff the representation of 'head' is \
  457. * internally consistent. */ \
  458. int \
  459. name##_HT_REP_IS_BAD_(const struct name *head) \
  460. { \
  461. unsigned n, i; \
  462. struct type *elm; \
  463. if (!head->hth_table_length) { \
  464. if (!head->hth_table && !head->hth_n_entries && \
  465. !head->hth_load_limit && head->hth_prime_idx == -1) \
  466. return 0; \
  467. else \
  468. return 1; \
  469. } \
  470. if (!head->hth_table || head->hth_prime_idx < 0 || \
  471. !head->hth_load_limit) \
  472. return 2; \
  473. if (head->hth_n_entries > head->hth_load_limit) \
  474. return 3; \
  475. if (head->hth_table_length != name##_PRIMES[head->hth_prime_idx]) \
  476. return 4; \
  477. if (head->hth_load_limit != (unsigned)(load*head->hth_table_length)) \
  478. return 5; \
  479. for (n = i = 0; i < head->hth_table_length; ++i) { \
  480. for (elm = head->hth_table[i]; elm; elm = elm->field.hte_next) { \
  481. if (HT_ELT_HASH_(elm, field, hashfn) != hashfn(elm)) \
  482. return 1000 + i; \
  483. if (HT_BUCKET_NUM_(head,field,elm,hashfn) != i) \
  484. return 10000 + i; \
  485. ++n; \
  486. } \
  487. } \
  488. if (n != head->hth_n_entries) \
  489. return 6; \
  490. return 0; \
  491. }
  492. #define HT_GENERATE(name, type, field, hashfn, eqfn, load, mallocfn, \
  493. reallocfn, freefn) \
  494. static void * \
  495. name##_reallocarray(void *arg, size_t a, size_t b) \
  496. { \
  497. if ((b) && (a) > SIZE_MAX / (b)) \
  498. return NULL; \
  499. if (arg) \
  500. return reallocfn((arg),(a)*(b)); \
  501. else \
  502. return mallocfn((a)*(b)); \
  503. } \
  504. HT_GENERATE2(name, type, field, hashfn, eqfn, load, \
  505. name##_reallocarray, freefn)
  506. /** Implements an over-optimized "find and insert if absent" block;
  507. * not meant for direct usage by typical code, or usage outside the critical
  508. * path.*/
  509. #define HT_FIND_OR_INSERT_(name, field, hashfn, head, eltype, elm, var, y, n) \
  510. { \
  511. struct name *var##_head_ = head; \
  512. struct eltype **var; \
  513. if (!var##_head_->hth_table || \
  514. var##_head_->hth_n_entries >= var##_head_->hth_load_limit) \
  515. name##_HT_GROW(var##_head_, var##_head_->hth_n_entries+1); \
  516. HT_SET_HASH_((elm), field, hashfn); \
  517. var = name##_HT_FIND_P_(var##_head_, (elm)); \
  518. HT_ASSERT_(var); /* Holds because we called HT_GROW */ \
  519. if (*var) { \
  520. y; \
  521. } else { \
  522. n; \
  523. } \
  524. }
  525. #define HT_FOI_INSERT_(field, head, elm, newent, var) \
  526. { \
  527. HT_SET_HASHVAL_(newent, field, (elm)->field.hte_hash); \
  528. newent->field.hte_next = NULL; \
  529. *var = newent; \
  530. ++((head)->hth_n_entries); \
  531. }
  532. /*
  533. * Copyright 2005, Nick Mathewson. Implementation logic is adapted from code
  534. * by Christopher Clark, retrofit to allow drop-in memory management, and to
  535. * use the same interface as Niels Provos's tree.h. This is probably still
  536. * a derived work, so the original license below still applies.
  537. *
  538. * Copyright (c) 2002, Christopher Clark
  539. * All rights reserved.
  540. *
  541. * Redistribution and use in source and binary forms, with or without
  542. * modification, are permitted provided that the following conditions
  543. * are met:
  544. *
  545. * * Redistributions of source code must retain the above copyright
  546. * notice, this list of conditions and the following disclaimer.
  547. *
  548. * * Redistributions in binary form must reproduce the above copyright
  549. * notice, this list of conditions and the following disclaimer in the
  550. * documentation and/or other materials provided with the distribution.
  551. *
  552. * * Neither the name of the original author; nor the names of any contributors
  553. * may be used to endorse or promote products derived from this software
  554. * without specific prior written permission.
  555. *
  556. *
  557. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  558. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  559. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  560. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  561. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  562. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  563. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  564. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  565. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  566. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  567. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  568. */
  569. #endif