hash.c 8.9 KB

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