hash.c 7.8 KB

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