hash.c 8.6 KB

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