hash.c 9.0 KB

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