2
0

hash.c 9.8 KB

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