hash.c 9.1 KB

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