hash.c 7.7 KB

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