hash.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2018, 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.haxx.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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include <curl/curl.h>
  24. #include "hash.h"
  25. #include "llist.h"
  26. #include "curl_memory.h"
  27. /* The last #include file should be: */
  28. #include "memdebug.h"
  29. static void
  30. hash_element_dtor(void *user, void *element)
  31. {
  32. struct curl_hash *h = (struct curl_hash *) user;
  33. struct curl_hash_element *e = (struct curl_hash_element *) element;
  34. if(e->ptr) {
  35. h->dtor(e->ptr);
  36. e->ptr = NULL;
  37. }
  38. e->key_len = 0;
  39. free(e);
  40. }
  41. /* Initializes a hash structure.
  42. * Return 1 on error, 0 is fine.
  43. *
  44. * @unittest: 1602
  45. * @unittest: 1603
  46. */
  47. int
  48. Curl_hash_init(struct curl_hash *h,
  49. int slots,
  50. hash_function hfunc,
  51. comp_function comparator,
  52. curl_hash_dtor dtor)
  53. {
  54. if(!slots || !hfunc || !comparator ||!dtor) {
  55. return 1; /* failure */
  56. }
  57. h->hash_func = hfunc;
  58. h->comp_func = comparator;
  59. h->dtor = dtor;
  60. h->size = 0;
  61. h->slots = slots;
  62. h->table = malloc(slots * sizeof(struct curl_llist));
  63. if(h->table) {
  64. int i;
  65. for(i = 0; i < slots; ++i)
  66. Curl_llist_init(&h->table[i], (curl_llist_dtor) hash_element_dtor);
  67. return 0; /* fine */
  68. }
  69. h->slots = 0;
  70. return 1; /* failure */
  71. }
  72. static struct curl_hash_element *
  73. mk_hash_element(const void *key, size_t key_len, const void *p)
  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. }
  84. return he;
  85. }
  86. #define FETCH_LIST(x,y,z) &x->table[x->hash_func(y, z, x->slots)]
  87. /* Insert the data in the hash. If there already was a match in the hash,
  88. * that data is replaced.
  89. *
  90. * @unittest: 1305
  91. * @unittest: 1602
  92. * @unittest: 1603
  93. */
  94. void *
  95. Curl_hash_add(struct curl_hash *h, void *key, size_t key_len, void *p)
  96. {
  97. struct curl_hash_element *he;
  98. struct curl_llist_element *le;
  99. struct curl_llist *l = FETCH_LIST(h, key, key_len);
  100. for(le = l->head; le; le = le->next) {
  101. he = (struct curl_hash_element *) le->ptr;
  102. if(h->comp_func(he->key, he->key_len, key, key_len)) {
  103. Curl_llist_remove(l, le, (void *)h);
  104. --h->size;
  105. break;
  106. }
  107. }
  108. he = mk_hash_element(key, key_len, p);
  109. if(he) {
  110. Curl_llist_insert_next(l, l->tail, he, &he->list);
  111. ++h->size;
  112. return p; /* return the new entry */
  113. }
  114. return NULL; /* failure */
  115. }
  116. /* Remove the identified hash entry.
  117. * Returns non-zero on failure.
  118. *
  119. * @unittest: 1603
  120. */
  121. int Curl_hash_delete(struct curl_hash *h, void *key, size_t key_len)
  122. {
  123. struct curl_llist_element *le;
  124. struct curl_llist *l = FETCH_LIST(h, key, key_len);
  125. for(le = l->head; le; le = le->next) {
  126. struct curl_hash_element *he = le->ptr;
  127. if(h->comp_func(he->key, he->key_len, key, key_len)) {
  128. Curl_llist_remove(l, le, (void *) h);
  129. --h->size;
  130. return 0;
  131. }
  132. }
  133. return 1;
  134. }
  135. /* Retrieves a hash element.
  136. *
  137. * @unittest: 1603
  138. */
  139. void *
  140. Curl_hash_pick(struct curl_hash *h, void *key, size_t key_len)
  141. {
  142. struct curl_llist_element *le;
  143. struct curl_llist *l;
  144. if(h) {
  145. l = FETCH_LIST(h, key, key_len);
  146. for(le = l->head; le; le = le->next) {
  147. struct curl_hash_element *he = le->ptr;
  148. if(h->comp_func(he->key, he->key_len, key, key_len)) {
  149. return he->ptr;
  150. }
  151. }
  152. }
  153. return NULL;
  154. }
  155. #if defined(DEBUGBUILD) && defined(AGGRESIVE_TEST)
  156. void
  157. Curl_hash_apply(curl_hash *h, void *user,
  158. void (*cb)(void *user, void *ptr))
  159. {
  160. struct curl_llist_element *le;
  161. int i;
  162. for(i = 0; i < h->slots; ++i) {
  163. for(le = (h->table[i])->head;
  164. le;
  165. le = le->next) {
  166. curl_hash_element *el = le->ptr;
  167. cb(user, el->ptr);
  168. }
  169. }
  170. }
  171. #endif
  172. /* Destroys all the entries in the given hash and resets its attributes,
  173. * prepping the given hash for [static|dynamic] deallocation.
  174. *
  175. * @unittest: 1305
  176. * @unittest: 1602
  177. * @unittest: 1603
  178. */
  179. void
  180. Curl_hash_destroy(struct curl_hash *h)
  181. {
  182. int i;
  183. for(i = 0; i < h->slots; ++i) {
  184. Curl_llist_destroy(&h->table[i], (void *) h);
  185. }
  186. Curl_safefree(h->table);
  187. h->size = 0;
  188. h->slots = 0;
  189. }
  190. /* Removes all the entries in the given hash.
  191. *
  192. * @unittest: 1602
  193. */
  194. void
  195. Curl_hash_clean(struct curl_hash *h)
  196. {
  197. Curl_hash_clean_with_criterium(h, NULL, NULL);
  198. }
  199. /* Cleans all entries that pass the comp function criteria. */
  200. void
  201. Curl_hash_clean_with_criterium(struct curl_hash *h, void *user,
  202. int (*comp)(void *, void *))
  203. {
  204. struct curl_llist_element *le;
  205. struct curl_llist_element *lnext;
  206. struct curl_llist *list;
  207. int i;
  208. if(!h)
  209. return;
  210. for(i = 0; i < h->slots; ++i) {
  211. list = &h->table[i];
  212. le = list->head; /* get first list entry */
  213. while(le) {
  214. struct curl_hash_element *he = le->ptr;
  215. lnext = le->next;
  216. /* ask the callback function if we shall remove this entry or not */
  217. if(comp == NULL || comp(user, he->ptr)) {
  218. Curl_llist_remove(list, le, (void *) h);
  219. --h->size; /* one less entry in the hash now */
  220. }
  221. le = lnext;
  222. }
  223. }
  224. }
  225. size_t Curl_hash_str(void *key, size_t key_length, size_t slots_num)
  226. {
  227. const char *key_str = (const char *) key;
  228. const char *end = key_str + key_length;
  229. size_t h = 5381;
  230. while(key_str < end) {
  231. h += h << 5;
  232. h ^= *key_str++;
  233. }
  234. return (h % slots_num);
  235. }
  236. size_t Curl_str_key_compare(void *k1, size_t key1_len,
  237. void *k2, size_t key2_len)
  238. {
  239. if((key1_len == key2_len) && !memcmp(k1, k2, key1_len))
  240. return 1;
  241. return 0;
  242. }
  243. void Curl_hash_start_iterate(struct curl_hash *hash,
  244. struct curl_hash_iterator *iter)
  245. {
  246. iter->hash = hash;
  247. iter->slot_index = 0;
  248. iter->current_element = NULL;
  249. }
  250. struct curl_hash_element *
  251. Curl_hash_next_element(struct curl_hash_iterator *iter)
  252. {
  253. struct curl_hash *h = iter->hash;
  254. /* Get the next element in the current list, if any */
  255. if(iter->current_element)
  256. iter->current_element = iter->current_element->next;
  257. /* If we have reached the end of the list, find the next one */
  258. if(!iter->current_element) {
  259. int i;
  260. for(i = iter->slot_index; i < h->slots; i++) {
  261. if(h->table[i].head) {
  262. iter->current_element = h->table[i].head;
  263. iter->slot_index = i + 1;
  264. break;
  265. }
  266. }
  267. }
  268. if(iter->current_element) {
  269. struct curl_hash_element *he = iter->current_element->ptr;
  270. return he;
  271. }
  272. iter->current_element = NULL;
  273. return NULL;
  274. }
  275. #if 0 /* useful function for debugging hashes and their contents */
  276. void Curl_hash_print(struct curl_hash *h,
  277. void (*func)(void *))
  278. {
  279. struct curl_hash_iterator iter;
  280. struct curl_hash_element *he;
  281. int last_index = -1;
  282. if(!h)
  283. return;
  284. fprintf(stderr, "=Hash dump=\n");
  285. Curl_hash_start_iterate(h, &iter);
  286. he = Curl_hash_next_element(&iter);
  287. while(he) {
  288. if(iter.slot_index != last_index) {
  289. fprintf(stderr, "index %d:", iter.slot_index);
  290. if(last_index >= 0) {
  291. fprintf(stderr, "\n");
  292. }
  293. last_index = iter.slot_index;
  294. }
  295. if(func)
  296. func(he->ptr);
  297. else
  298. fprintf(stderr, " [%p]", (void *)he->ptr);
  299. he = Curl_hash_next_element(&iter);
  300. }
  301. fprintf(stderr, "\n");
  302. }
  303. #endif