hash.c 9.6 KB

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