hash.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, 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. 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. 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_hash_element *he;
  125. struct curl_llist *l = FETCH_LIST(h, key, key_len);
  126. for(le = l->head; le; le = le->next) {
  127. he = le->ptr;
  128. if(h->comp_func(he->key, he->key_len, key, key_len)) {
  129. Curl_llist_remove(l, le, (void *) h);
  130. --h->size;
  131. return 0;
  132. }
  133. }
  134. return 1;
  135. }
  136. /* Retrieves a hash element.
  137. *
  138. * @unittest: 1603
  139. */
  140. void *
  141. Curl_hash_pick(struct curl_hash *h, void *key, size_t key_len)
  142. {
  143. struct curl_llist_element *le;
  144. struct curl_hash_element *he;
  145. struct curl_llist *l;
  146. if(h) {
  147. l = FETCH_LIST(h, key, key_len);
  148. for(le = l->head; le; le = le->next) {
  149. he = le->ptr;
  150. if(h->comp_func(he->key, he->key_len, key, key_len)) {
  151. return he->ptr;
  152. }
  153. }
  154. }
  155. return NULL;
  156. }
  157. #if defined(DEBUGBUILD) && defined(AGGRESIVE_TEST)
  158. void
  159. Curl_hash_apply(curl_hash *h, void *user,
  160. void (*cb)(void *user, void *ptr))
  161. {
  162. struct curl_llist_element *le;
  163. int i;
  164. for(i = 0; i < h->slots; ++i) {
  165. for(le = (h->table[i])->head;
  166. le;
  167. le = le->next) {
  168. curl_hash_element *el = le->ptr;
  169. cb(user, el->ptr);
  170. }
  171. }
  172. }
  173. #endif
  174. /* Destroys all the entries in the given hash and resets its attributes,
  175. * prepping the given hash for [static|dynamic] deallocation.
  176. *
  177. * @unittest: 1305
  178. * @unittest: 1602
  179. * @unittest: 1603
  180. */
  181. void
  182. Curl_hash_destroy(struct curl_hash *h)
  183. {
  184. int i;
  185. for(i = 0; i < h->slots; ++i) {
  186. Curl_llist_destroy(&h->table[i], (void *) h);
  187. }
  188. Curl_safefree(h->table);
  189. h->size = 0;
  190. h->slots = 0;
  191. }
  192. /* Removes all the entries in the given hash.
  193. *
  194. * @unittest: 1602
  195. */
  196. void
  197. Curl_hash_clean(struct curl_hash *h)
  198. {
  199. Curl_hash_clean_with_criterium(h, NULL, NULL);
  200. }
  201. /* Cleans all entries that pass the comp function criteria. */
  202. void
  203. Curl_hash_clean_with_criterium(struct curl_hash *h, void *user,
  204. int (*comp)(void *, void *))
  205. {
  206. struct curl_llist_element *le;
  207. struct curl_llist_element *lnext;
  208. struct curl_llist *list;
  209. int i;
  210. if(!h)
  211. return;
  212. for(i = 0; i < h->slots; ++i) {
  213. list = &h->table[i];
  214. le = list->head; /* get first list entry */
  215. while(le) {
  216. struct curl_hash_element *he = le->ptr;
  217. lnext = le->next;
  218. /* ask the callback function if we shall remove this entry or not */
  219. if(comp == NULL || comp(user, he->ptr)) {
  220. Curl_llist_remove(list, le, (void *) h);
  221. --h->size; /* one less entry in the hash now */
  222. }
  223. le = lnext;
  224. }
  225. }
  226. }
  227. size_t Curl_hash_str(void *key, size_t key_length, size_t slots_num)
  228. {
  229. const char *key_str = (const char *) key;
  230. const char *end = key_str + key_length;
  231. unsigned long h = 5381;
  232. while(key_str < end) {
  233. h += h << 5;
  234. h ^= (unsigned long) *key_str++;
  235. }
  236. return (h % slots_num);
  237. }
  238. size_t Curl_str_key_compare(void *k1, size_t key1_len,
  239. void *k2, size_t key2_len)
  240. {
  241. if((key1_len == key2_len) && !memcmp(k1, k2, key1_len))
  242. return 1;
  243. return 0;
  244. }
  245. void Curl_hash_start_iterate(struct curl_hash *hash,
  246. struct curl_hash_iterator *iter)
  247. {
  248. iter->hash = hash;
  249. iter->slot_index = 0;
  250. iter->current_element = NULL;
  251. }
  252. struct curl_hash_element *
  253. Curl_hash_next_element(struct curl_hash_iterator *iter)
  254. {
  255. int i;
  256. struct curl_hash *h = iter->hash;
  257. /* Get the next element in the current list, if any */
  258. if(iter->current_element)
  259. iter->current_element = iter->current_element->next;
  260. /* If we have reached the end of the list, find the next one */
  261. if(!iter->current_element) {
  262. for(i = iter->slot_index; i < h->slots; i++) {
  263. if(h->table[i].head) {
  264. iter->current_element = h->table[i].head;
  265. iter->slot_index = i + 1;
  266. break;
  267. }
  268. }
  269. }
  270. if(iter->current_element) {
  271. struct curl_hash_element *he = iter->current_element->ptr;
  272. return he;
  273. }
  274. iter->current_element = NULL;
  275. return NULL;
  276. }
  277. #if 0 /* useful function for debugging hashes and their contents */
  278. void Curl_hash_print(struct curl_hash *h,
  279. void (*func)(void *))
  280. {
  281. struct curl_hash_iterator iter;
  282. struct curl_hash_element *he;
  283. int last_index = -1;
  284. if(!h)
  285. return;
  286. fprintf(stderr, "=Hash dump=\n");
  287. Curl_hash_start_iterate(h, &iter);
  288. he = Curl_hash_next_element(&iter);
  289. while(he) {
  290. if(iter.slot_index != last_index) {
  291. fprintf(stderr, "index %d:", iter.slot_index);
  292. if(last_index >= 0) {
  293. fprintf(stderr, "\n");
  294. }
  295. last_index = iter.slot_index;
  296. }
  297. if(func)
  298. func(he->ptr);
  299. else
  300. fprintf(stderr, " [%p]", (void *)he->ptr);
  301. he = Curl_hash_next_element(&iter);
  302. }
  303. fprintf(stderr, "\n");
  304. }
  305. #endif