hash.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2005, 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. #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 = strdup(key);
  107. if(dup) {
  108. he->key = dup;
  109. he->key_len = key_len;
  110. he->ptr = (void *) p;
  111. }
  112. else {
  113. /* failed to duplicate the key, free memory and fail */
  114. free(he);
  115. he = NULL;
  116. }
  117. }
  118. return he;
  119. }
  120. #define find_slot(__h, __k, __k_len) (hash_str(__k, __k_len) % (__h)->slots)
  121. #define FETCH_LIST(x,y,z) x->table[find_slot(x, y, z)]
  122. /* Return the data in the hash. If there already was a match in the hash,
  123. that data is returned. */
  124. void *
  125. Curl_hash_add(struct curl_hash *h, char *key, size_t key_len, void *p)
  126. {
  127. struct curl_hash_element *he;
  128. struct curl_llist_element *le;
  129. struct curl_llist *l = FETCH_LIST(h, key, key_len);
  130. for (le = l->head; le; le = le->next) {
  131. he = (struct curl_hash_element *) le->ptr;
  132. if (hash_key_compare(he->key, he->key_len, key, key_len)) {
  133. h->dtor(p); /* remove the NEW entry */
  134. return he->ptr; /* return the EXISTING entry */
  135. }
  136. }
  137. he = mk_hash_element(key, key_len, p);
  138. if (he) {
  139. if(Curl_llist_insert_next(l, l->tail, he)) {
  140. ++h->size;
  141. return p; /* return the new entry */
  142. }
  143. /*
  144. * Couldn't insert it, destroy the 'he' element and the key again. We
  145. * don't call hash_element_dtor() since that would also call the
  146. * "destructor" for the actual data 'p'. When we fail, we shall not touch
  147. * that data.
  148. */
  149. free(he->key);
  150. free(he);
  151. }
  152. return NULL; /* failure */
  153. }
  154. void *
  155. Curl_hash_pick(struct curl_hash *h, char *key, size_t key_len)
  156. {
  157. struct curl_llist_element *le;
  158. struct curl_hash_element *he;
  159. struct curl_llist *l = FETCH_LIST(h, key, key_len);
  160. for (le = l->head;
  161. le;
  162. le = le->next) {
  163. he = le->ptr;
  164. if (hash_key_compare(he->key, he->key_len, key, key_len)) {
  165. return he->ptr;
  166. }
  167. }
  168. return NULL;
  169. }
  170. #if defined(CURLDEBUG) && defined(AGGRESIVE_TEST)
  171. void
  172. Curl_hash_apply(curl_hash *h, void *user,
  173. void (*cb)(void *user, void *ptr))
  174. {
  175. struct curl_llist_element *le;
  176. int i;
  177. for (i = 0; i < h->slots; ++i) {
  178. for (le = (h->table[i])->head;
  179. le;
  180. le = le->next) {
  181. curl_hash_element *el = le->ptr;
  182. cb(user, el->ptr);
  183. }
  184. }
  185. }
  186. #endif
  187. void
  188. Curl_hash_clean(struct curl_hash *h)
  189. {
  190. int i;
  191. for (i = 0; i < h->slots; ++i) {
  192. Curl_llist_destroy(h->table[i], (void *) h);
  193. }
  194. free(h->table);
  195. }
  196. void
  197. Curl_hash_clean_with_criterium(struct curl_hash *h, void *user,
  198. int (*comp)(void *, void *))
  199. {
  200. struct curl_llist_element *le;
  201. struct curl_llist_element *lnext;
  202. struct curl_llist *list;
  203. int i;
  204. for (i = 0; i < h->slots; ++i) {
  205. list = h->table[i];
  206. le = list->head; /* get first list entry */
  207. while(le) {
  208. struct curl_hash_element *he = le->ptr;
  209. lnext = le->next;
  210. /* ask the callback function if we shall remove this entry or not */
  211. if (comp(user, he->ptr)) {
  212. Curl_llist_remove(list, le, (void *) h);
  213. --h->size; /* one less entry in the hash now */
  214. }
  215. le = lnext;
  216. }
  217. }
  218. }
  219. void
  220. Curl_hash_destroy(struct curl_hash *h)
  221. {
  222. if (!h)
  223. return;
  224. Curl_hash_clean(h);
  225. free(h);
  226. }