hash.c 5.9 KB

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