hash.c 7.3 KB

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