hash.c 7.7 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. ***************************************************************************/
  22. #include "setup.h"
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include "hash.h"
  26. #include "llist.h"
  27. #define _MPRINTF_REPLACE /* use our functions only */
  28. #include <curl/mprintf.h>
  29. #include "curl_memory.h"
  30. /* The last #include file should be: */
  31. #include "memdebug.h"
  32. static void
  33. hash_element_dtor(void *user, void *element)
  34. {
  35. struct curl_hash *h = (struct curl_hash *) user;
  36. struct curl_hash_element *e = (struct curl_hash_element *) element;
  37. if(e->key)
  38. free(e->key);
  39. if(e->ptr)
  40. h->dtor(e->ptr);
  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. free(h->table);
  68. return 1; /* failure */
  69. }
  70. }
  71. return 0; /* fine */
  72. }
  73. else
  74. return 1; /* failure */
  75. }
  76. struct curl_hash *
  77. Curl_hash_alloc(int slots,
  78. hash_function hfunc,
  79. comp_function comparator,
  80. curl_hash_dtor dtor)
  81. {
  82. struct curl_hash *h;
  83. if(!slots || !hfunc || !comparator ||!dtor) {
  84. return NULL; /* failure */
  85. }
  86. h = malloc(sizeof(struct curl_hash));
  87. if(h) {
  88. if(Curl_hash_init(h, slots, hfunc, comparator, dtor)) {
  89. /* failure */
  90. free(h);
  91. h = NULL;
  92. }
  93. }
  94. return h;
  95. }
  96. static struct curl_hash_element *
  97. mk_hash_element(const void *key, size_t key_len, const void *p)
  98. {
  99. struct curl_hash_element *he = malloc(sizeof(struct curl_hash_element));
  100. if(he) {
  101. void *dupkey = malloc(key_len);
  102. if(dupkey) {
  103. /* copy the key */
  104. memcpy(dupkey, key, key_len);
  105. he->key = dupkey;
  106. he->key_len = key_len;
  107. he->ptr = (void *) p;
  108. }
  109. else {
  110. /* failed to duplicate the key, free memory and fail */
  111. free(he);
  112. he = NULL;
  113. }
  114. }
  115. return he;
  116. }
  117. #define FETCH_LIST(x,y,z) x->table[x->hash_func(y, z, x->slots)]
  118. /* Insert the data in the hash. If there already was a match in the hash,
  119. that data is replaced. */
  120. void *
  121. Curl_hash_add(struct curl_hash *h, void *key, size_t key_len, void *p)
  122. {
  123. struct curl_hash_element *he;
  124. struct curl_llist_element *le;
  125. struct curl_llist *l = FETCH_LIST (h, key, key_len);
  126. for (le = l->head; le; le = le->next) {
  127. he = (struct curl_hash_element *) 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. break;
  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(DEBUGBUILD) && 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