hash.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include <curl/curl.h>
  26. #include "hash.h"
  27. #include "llist.h"
  28. #include "curl_memory.h"
  29. /* The last #include file should be: */
  30. #include "memdebug.h"
  31. /* random patterns for API verification */
  32. #define HASHINIT 0x7017e781
  33. #define ITERINIT 0x5FEDCBA9
  34. static void
  35. hash_element_dtor(void *user, void *element)
  36. {
  37. struct Curl_hash *h = (struct Curl_hash *) user;
  38. struct Curl_hash_element *e = (struct Curl_hash_element *) element;
  39. DEBUGASSERT(h);
  40. DEBUGASSERT(e);
  41. if(e->ptr) {
  42. if(e->dtor)
  43. e->dtor(e->key, e->key_len, e->ptr);
  44. else
  45. h->dtor(e->ptr);
  46. e->ptr = NULL;
  47. }
  48. e->key_len = 0;
  49. free(e);
  50. }
  51. /* Initializes a hash structure.
  52. * Return 1 on error, 0 is fine.
  53. *
  54. * @unittest: 1602
  55. * @unittest: 1603
  56. */
  57. void
  58. Curl_hash_init(struct Curl_hash *h,
  59. size_t slots,
  60. hash_function hfunc,
  61. comp_function comparator,
  62. Curl_hash_dtor dtor)
  63. {
  64. DEBUGASSERT(h);
  65. DEBUGASSERT(slots);
  66. DEBUGASSERT(hfunc);
  67. DEBUGASSERT(comparator);
  68. DEBUGASSERT(dtor);
  69. h->table = NULL;
  70. h->hash_func = hfunc;
  71. h->comp_func = comparator;
  72. h->dtor = dtor;
  73. h->size = 0;
  74. h->slots = slots;
  75. #ifdef DEBUGBUILD
  76. h->init = HASHINIT;
  77. #endif
  78. }
  79. static struct Curl_hash_element *
  80. mk_hash_element(const void *key, size_t key_len, const void *p,
  81. Curl_hash_elem_dtor dtor)
  82. {
  83. /* allocate the struct plus memory after it to store the key */
  84. struct Curl_hash_element *he = malloc(sizeof(struct Curl_hash_element) +
  85. key_len);
  86. if(he) {
  87. /* copy the key */
  88. memcpy(he->key, key, key_len);
  89. he->key_len = key_len;
  90. he->ptr = (void *) p;
  91. he->dtor = dtor;
  92. }
  93. return he;
  94. }
  95. #define FETCH_LIST(x,y,z) &x->table[x->hash_func(y, z, x->slots)]
  96. void *Curl_hash_add2(struct Curl_hash *h, void *key, size_t key_len, void *p,
  97. Curl_hash_elem_dtor dtor)
  98. {
  99. struct Curl_hash_element *he;
  100. struct Curl_llist_node *le;
  101. struct Curl_llist *l;
  102. DEBUGASSERT(h);
  103. DEBUGASSERT(h->slots);
  104. DEBUGASSERT(h->init == HASHINIT);
  105. if(!h->table) {
  106. size_t i;
  107. h->table = malloc(h->slots * sizeof(struct Curl_llist));
  108. if(!h->table)
  109. return NULL; /* OOM */
  110. for(i = 0; i < h->slots; ++i)
  111. Curl_llist_init(&h->table[i], hash_element_dtor);
  112. }
  113. l = FETCH_LIST(h, key, key_len);
  114. for(le = Curl_llist_head(l); le; le = Curl_node_next(le)) {
  115. he = (struct Curl_hash_element *) Curl_node_elem(le);
  116. if(h->comp_func(he->key, he->key_len, key, key_len)) {
  117. Curl_node_uremove(le, (void *)h);
  118. --h->size;
  119. break;
  120. }
  121. }
  122. he = mk_hash_element(key, key_len, p, dtor);
  123. if(he) {
  124. Curl_llist_append(l, he, &he->list);
  125. ++h->size;
  126. return p; /* return the new entry */
  127. }
  128. return NULL; /* failure */
  129. }
  130. /* Insert the data in the hash. If there already was a match in the hash, that
  131. * data is replaced. This function also "lazily" allocates the table if
  132. * needed, as it is not done in the _init function (anymore).
  133. *
  134. * @unittest: 1305
  135. * @unittest: 1602
  136. * @unittest: 1603
  137. */
  138. void *
  139. Curl_hash_add(struct Curl_hash *h, void *key, size_t key_len, void *p)
  140. {
  141. return Curl_hash_add2(h, key, key_len, p, NULL);
  142. }
  143. /* Remove the identified hash entry.
  144. * Returns non-zero on failure.
  145. *
  146. * @unittest: 1603
  147. */
  148. int Curl_hash_delete(struct Curl_hash *h, void *key, size_t key_len)
  149. {
  150. DEBUGASSERT(h);
  151. DEBUGASSERT(h->slots);
  152. DEBUGASSERT(h->init == HASHINIT);
  153. if(h->table) {
  154. struct Curl_llist_node *le;
  155. struct Curl_llist *l = FETCH_LIST(h, key, key_len);
  156. for(le = Curl_llist_head(l); le; le = Curl_node_next(le)) {
  157. struct Curl_hash_element *he = Curl_node_elem(le);
  158. if(h->comp_func(he->key, he->key_len, key, key_len)) {
  159. Curl_node_uremove(le, (void *) h);
  160. --h->size;
  161. return 0;
  162. }
  163. }
  164. }
  165. return 1;
  166. }
  167. /* Retrieves a hash element.
  168. *
  169. * @unittest: 1603
  170. */
  171. void *
  172. Curl_hash_pick(struct Curl_hash *h, void *key, size_t key_len)
  173. {
  174. DEBUGASSERT(h);
  175. DEBUGASSERT(h->init == HASHINIT);
  176. if(h->table) {
  177. struct Curl_llist_node *le;
  178. struct Curl_llist *l;
  179. DEBUGASSERT(h->slots);
  180. l = FETCH_LIST(h, key, key_len);
  181. for(le = Curl_llist_head(l); le; le = Curl_node_next(le)) {
  182. struct Curl_hash_element *he = Curl_node_elem(le);
  183. if(h->comp_func(he->key, he->key_len, key, key_len)) {
  184. return he->ptr;
  185. }
  186. }
  187. }
  188. return NULL;
  189. }
  190. /* Destroys all the entries in the given hash and resets its attributes,
  191. * prepping the given hash for [static|dynamic] deallocation.
  192. *
  193. * @unittest: 1305
  194. * @unittest: 1602
  195. * @unittest: 1603
  196. */
  197. void
  198. Curl_hash_destroy(struct Curl_hash *h)
  199. {
  200. DEBUGASSERT(h->init == HASHINIT);
  201. if(h->table) {
  202. size_t i;
  203. for(i = 0; i < h->slots; ++i) {
  204. Curl_llist_destroy(&h->table[i], (void *) h);
  205. }
  206. Curl_safefree(h->table);
  207. }
  208. h->size = 0;
  209. h->slots = 0;
  210. }
  211. /* Removes all the entries in the given hash.
  212. *
  213. * @unittest: 1602
  214. */
  215. void
  216. Curl_hash_clean(struct Curl_hash *h)
  217. {
  218. Curl_hash_clean_with_criterium(h, NULL, NULL);
  219. }
  220. size_t Curl_hash_count(struct Curl_hash *h)
  221. {
  222. DEBUGASSERT(h->init == HASHINIT);
  223. return h->size;
  224. }
  225. /* Cleans all entries that pass the comp function criteria. */
  226. void
  227. Curl_hash_clean_with_criterium(struct Curl_hash *h, void *user,
  228. int (*comp)(void *, void *))
  229. {
  230. size_t i;
  231. if(!h || !h->table)
  232. return;
  233. DEBUGASSERT(h->init == HASHINIT);
  234. for(i = 0; i < h->slots; ++i) {
  235. struct Curl_llist *list = &h->table[i];
  236. struct Curl_llist_node *le =
  237. Curl_llist_head(list); /* get first list entry */
  238. while(le) {
  239. struct Curl_hash_element *he = Curl_node_elem(le);
  240. struct Curl_llist_node *lnext = Curl_node_next(le);
  241. /* ask the callback function if we shall remove this entry or not */
  242. if(!comp || comp(user, he->ptr)) {
  243. Curl_node_uremove(le, (void *) h);
  244. --h->size; /* one less entry in the hash now */
  245. }
  246. le = lnext;
  247. }
  248. }
  249. }
  250. size_t Curl_hash_str(void *key, size_t key_length, size_t slots_num)
  251. {
  252. const char *key_str = (const char *) key;
  253. const char *end = key_str + key_length;
  254. size_t h = 5381;
  255. while(key_str < end) {
  256. size_t j = (size_t)*key_str++;
  257. h += h << 5;
  258. h ^= j;
  259. }
  260. return (h % slots_num);
  261. }
  262. size_t Curl_str_key_compare(void *k1, size_t key1_len,
  263. void *k2, size_t key2_len)
  264. {
  265. if((key1_len == key2_len) && !memcmp(k1, k2, key1_len))
  266. return 1;
  267. return 0;
  268. }
  269. void Curl_hash_start_iterate(struct Curl_hash *hash,
  270. struct Curl_hash_iterator *iter)
  271. {
  272. DEBUGASSERT(hash->init == HASHINIT);
  273. iter->hash = hash;
  274. iter->slot_index = 0;
  275. iter->current_element = NULL;
  276. #ifdef DEBUGBUILD
  277. iter->init = ITERINIT;
  278. #endif
  279. }
  280. struct Curl_hash_element *
  281. Curl_hash_next_element(struct Curl_hash_iterator *iter)
  282. {
  283. struct Curl_hash *h;
  284. DEBUGASSERT(iter->init == ITERINIT);
  285. h = iter->hash;
  286. if(!h->table)
  287. return NULL; /* empty hash, nothing to return */
  288. /* Get the next element in the current list, if any */
  289. if(iter->current_element)
  290. iter->current_element = Curl_node_next(iter->current_element);
  291. /* If we have reached the end of the list, find the next one */
  292. if(!iter->current_element) {
  293. size_t i;
  294. for(i = iter->slot_index; i < h->slots; i++) {
  295. if(Curl_llist_head(&h->table[i])) {
  296. iter->current_element = Curl_llist_head(&h->table[i]);
  297. iter->slot_index = i + 1;
  298. break;
  299. }
  300. }
  301. }
  302. if(iter->current_element) {
  303. struct Curl_hash_element *he = Curl_node_elem(iter->current_element);
  304. return he;
  305. }
  306. return NULL;
  307. }
  308. #if 0 /* useful function for debugging hashes and their contents */
  309. void Curl_hash_print(struct Curl_hash *h,
  310. void (*func)(void *))
  311. {
  312. struct Curl_hash_iterator iter;
  313. struct Curl_hash_element *he;
  314. size_t last_index = ~0;
  315. if(!h)
  316. return;
  317. fprintf(stderr, "=Hash dump=\n");
  318. Curl_hash_start_iterate(h, &iter);
  319. he = Curl_hash_next_element(&iter);
  320. while(he) {
  321. if(iter.slot_index != last_index) {
  322. fprintf(stderr, "index %d:", iter.slot_index);
  323. if(last_index != ~0) {
  324. fprintf(stderr, "\n");
  325. }
  326. last_index = iter.slot_index;
  327. }
  328. if(func)
  329. func(he->ptr);
  330. else
  331. fprintf(stderr, " [%p]", (void *)he->ptr);
  332. he = Curl_hash_next_element(&iter);
  333. }
  334. fprintf(stderr, "\n");
  335. }
  336. #endif
  337. void Curl_hash_offt_init(struct Curl_hash *h,
  338. size_t slots,
  339. Curl_hash_dtor dtor)
  340. {
  341. Curl_hash_init(h, slots, Curl_hash_str, Curl_str_key_compare, dtor);
  342. }
  343. void *Curl_hash_offt_set(struct Curl_hash *h, curl_off_t id, void *elem)
  344. {
  345. return Curl_hash_add(h, &id, sizeof(id), elem);
  346. }
  347. int Curl_hash_offt_remove(struct Curl_hash *h, curl_off_t id)
  348. {
  349. return Curl_hash_delete(h, &id, sizeof(id));
  350. }
  351. void *Curl_hash_offt_get(struct Curl_hash *h, curl_off_t id)
  352. {
  353. return Curl_hash_pick(h, &id, sizeof(id));
  354. }