hash.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, 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.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. int i;
  55. if(!slots || !hfunc || !comparator ||!dtor) {
  56. return 1; /* failure */
  57. }
  58. h->hash_func = hfunc;
  59. h->comp_func = comparator;
  60. h->dtor = dtor;
  61. h->size = 0;
  62. h->slots = slots;
  63. h->table = malloc(slots * sizeof(struct curl_llist));
  64. if(h->table) {
  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. if(Curl_llist_insert_next(l, l->tail, he)) {
  111. ++h->size;
  112. return p; /* return the new entry */
  113. }
  114. /*
  115. * Couldn't insert it, destroy the 'he' element and the key again. We
  116. * don't call hash_element_dtor() since that would also call the
  117. * "destructor" for the actual data 'p'. When we fail, we shall not touch
  118. * that data.
  119. */
  120. free(he);
  121. }
  122. return NULL; /* failure */
  123. }
  124. /* Remove the identified hash entry.
  125. * Returns non-zero on failure.
  126. *
  127. * @unittest: 1603
  128. */
  129. int Curl_hash_delete(struct curl_hash *h, void *key, size_t key_len)
  130. {
  131. struct curl_llist_element *le;
  132. struct curl_hash_element *he;
  133. struct curl_llist *l = FETCH_LIST(h, key, key_len);
  134. for(le = l->head; le; le = le->next) {
  135. he = le->ptr;
  136. if(h->comp_func(he->key, he->key_len, key, key_len)) {
  137. Curl_llist_remove(l, le, (void *) h);
  138. --h->size;
  139. return 0;
  140. }
  141. }
  142. return 1;
  143. }
  144. /* Retrieves a hash element.
  145. *
  146. * @unittest: 1603
  147. */
  148. void *
  149. Curl_hash_pick(struct curl_hash *h, void *key, size_t key_len)
  150. {
  151. struct curl_llist_element *le;
  152. struct curl_hash_element *he;
  153. struct curl_llist *l;
  154. if(h) {
  155. l = FETCH_LIST(h, key, key_len);
  156. for(le = l->head; le; le = le->next) {
  157. he = le->ptr;
  158. if(h->comp_func(he->key, he->key_len, key, key_len)) {
  159. return he->ptr;
  160. }
  161. }
  162. }
  163. return NULL;
  164. }
  165. #if defined(DEBUGBUILD) && defined(AGGRESIVE_TEST)
  166. void
  167. Curl_hash_apply(curl_hash *h, void *user,
  168. void (*cb)(void *user, void *ptr))
  169. {
  170. struct curl_llist_element *le;
  171. int i;
  172. for(i = 0; i < h->slots; ++i) {
  173. for(le = (h->table[i])->head;
  174. le;
  175. le = le->next) {
  176. curl_hash_element *el = le->ptr;
  177. cb(user, el->ptr);
  178. }
  179. }
  180. }
  181. #endif
  182. /* Destroys all the entries in the given hash and resets its attributes,
  183. * prepping the given hash for [static|dynamic] deallocation.
  184. *
  185. * @unittest: 1305
  186. * @unittest: 1602
  187. * @unittest: 1603
  188. */
  189. void
  190. Curl_hash_destroy(struct curl_hash *h)
  191. {
  192. int i;
  193. for(i = 0; i < h->slots; ++i) {
  194. Curl_llist_destroy(&h->table[i], (void *) h);
  195. }
  196. Curl_safefree(h->table);
  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. int i;
  218. if(!h)
  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 == NULL || 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. unsigned long h = 5381;
  240. while(key_str < end) {
  241. h += h << 5;
  242. h ^= (unsigned long) *key_str++;
  243. }
  244. return (h % slots_num);
  245. }
  246. size_t Curl_str_key_compare(void *k1, size_t key1_len,
  247. void *k2, size_t key2_len)
  248. {
  249. if((key1_len == key2_len) && !memcmp(k1, k2, key1_len))
  250. return 1;
  251. return 0;
  252. }
  253. void Curl_hash_start_iterate(struct curl_hash *hash,
  254. struct curl_hash_iterator *iter)
  255. {
  256. iter->hash = hash;
  257. iter->slot_index = 0;
  258. iter->current_element = NULL;
  259. }
  260. struct curl_hash_element *
  261. Curl_hash_next_element(struct curl_hash_iterator *iter)
  262. {
  263. int i;
  264. struct curl_hash *h = iter->hash;
  265. /* Get the next element in the current list, if any */
  266. if(iter->current_element)
  267. iter->current_element = iter->current_element->next;
  268. /* If we have reached the end of the list, find the next one */
  269. if(!iter->current_element) {
  270. for(i = iter->slot_index;i < h->slots;i++) {
  271. if(h->table[i].head) {
  272. iter->current_element = h->table[i].head;
  273. iter->slot_index = i+1;
  274. break;
  275. }
  276. }
  277. }
  278. if(iter->current_element) {
  279. struct curl_hash_element *he = iter->current_element->ptr;
  280. return he;
  281. }
  282. iter->current_element = NULL;
  283. return NULL;
  284. }
  285. #if 0 /* useful function for debugging hashes and their contents */
  286. void Curl_hash_print(struct curl_hash *h,
  287. void (*func)(void *))
  288. {
  289. struct curl_hash_iterator iter;
  290. struct curl_hash_element *he;
  291. int last_index = -1;
  292. if(!h)
  293. return;
  294. fprintf(stderr, "=Hash dump=\n");
  295. Curl_hash_start_iterate(h, &iter);
  296. he = Curl_hash_next_element(&iter);
  297. while(he) {
  298. if(iter.slot_index != last_index) {
  299. fprintf(stderr, "index %d:", iter.slot_index);
  300. if(last_index >= 0) {
  301. fprintf(stderr, "\n");
  302. }
  303. last_index = iter.slot_index;
  304. }
  305. if(func)
  306. func(he->ptr);
  307. else
  308. fprintf(stderr, " [%p]", (void *)he->ptr);
  309. he = Curl_hash_next_element(&iter);
  310. }
  311. fprintf(stderr, "\n");
  312. }
  313. #endif