hash.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, 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. #ifdef CURLDEBUG
  29. /* this must be the last include file */
  30. #include "memdebug.h"
  31. #endif
  32. static unsigned long
  33. hash_str(const char *key, size_t key_length)
  34. {
  35. char *end = (char *) key + key_length;
  36. unsigned long h = 5381;
  37. while (key < end) {
  38. h += h << 5;
  39. h ^= (unsigned long) *key++;
  40. }
  41. return h;
  42. }
  43. static void
  44. hash_element_dtor(void *user, void *element)
  45. {
  46. curl_hash *h = (curl_hash *) user;
  47. curl_hash_element *e = (curl_hash_element *) element;
  48. if (e->key) {
  49. free(e->key);
  50. }
  51. h->dtor(e->ptr);
  52. free(e);
  53. }
  54. /* return 1 on error, 0 is fine */
  55. int
  56. Curl_hash_init(curl_hash *h, int slots, curl_hash_dtor dtor)
  57. {
  58. int i;
  59. h->dtor = dtor;
  60. h->size = 0;
  61. h->slots = slots;
  62. h->table = (curl_llist **) malloc(slots * sizeof(curl_llist *));
  63. if(h->table) {
  64. for (i = 0; i < slots; ++i) {
  65. h->table[i] = Curl_llist_alloc((curl_llist_dtor) hash_element_dtor);
  66. if(!h->table[i]) {
  67. while(i--)
  68. Curl_llist_destroy(h->table[i], NULL);
  69. free(h->table);
  70. return 1; /* failure */
  71. }
  72. }
  73. return 0; /* fine */
  74. }
  75. else
  76. return 1; /* failure */
  77. }
  78. curl_hash *
  79. Curl_hash_alloc(int slots, curl_hash_dtor dtor)
  80. {
  81. curl_hash *h;
  82. h = (curl_hash *) malloc(sizeof(curl_hash));
  83. if (h) {
  84. if(Curl_hash_init(h, slots, dtor)) {
  85. /* failure */
  86. free(h);
  87. h = NULL;
  88. }
  89. }
  90. return h;
  91. }
  92. static int
  93. hash_key_compare(char *key1, size_t key1_len, char *key2, size_t key2_len)
  94. {
  95. if (key1_len == key2_len &&
  96. *key1 == *key2 &&
  97. memcmp(key1, key2, key1_len) == 0) {
  98. return 1;
  99. }
  100. return 0;
  101. }
  102. static curl_hash_element *
  103. mk_hash_element(char *key, size_t key_len, const void *p)
  104. {
  105. curl_hash_element *he =
  106. (curl_hash_element *) malloc(sizeof(curl_hash_element));
  107. if(he) {
  108. he->key = strdup(key);
  109. he->key_len = key_len;
  110. he->ptr = (void *) p;
  111. }
  112. return he;
  113. }
  114. #define find_slot(__h, __k, __k_len) (hash_str(__k, __k_len) % (__h)->slots)
  115. #define FETCH_LIST(x,y,z) x->table[find_slot(x, y, z)]
  116. /* Return the data in the hash. If there already was a match in the hash,
  117. that data is returned. */
  118. void *
  119. Curl_hash_add(curl_hash *h, char *key, size_t key_len, void *p)
  120. {
  121. curl_hash_element *he;
  122. curl_llist_element *le;
  123. curl_llist *l = FETCH_LIST(h, key, key_len);
  124. for (le = l->head; le; le = le->next) {
  125. he = (curl_hash_element *) le->ptr;
  126. if (hash_key_compare(he->key, he->key_len, key, key_len)) {
  127. h->dtor(p); /* remove the NEW entry */
  128. return he->ptr; /* return the EXISTING entry */
  129. }
  130. }
  131. he = mk_hash_element(key, key_len, p);
  132. if (he) {
  133. if(Curl_llist_insert_next(l, l->tail, he)) {
  134. ++h->size;
  135. return p; /* return the new entry */
  136. }
  137. /* couldn't insert it, destroy the 'he' element again */
  138. hash_element_dtor(h, he);
  139. }
  140. h->dtor(p); /* remove the NEW entry */
  141. return NULL; /* failure */
  142. }
  143. #if 0
  144. int
  145. Curl_hash_delete(curl_hash *h, char *key, size_t key_len)
  146. {
  147. curl_hash_element *he;
  148. curl_llist_element *le;
  149. curl_llist *l = FETCH_LIST(h, key, key_len);
  150. for (le = l->head;
  151. le;
  152. le = le->next) {
  153. he = le->ptr;
  154. if (hash_key_compare(he->key, he->key_len, key, key_len)) {
  155. Curl_llist_remove(l, le, (void *) h);
  156. --h->size;
  157. return 1;
  158. }
  159. }
  160. return 0;
  161. }
  162. #endif
  163. void *
  164. Curl_hash_pick(curl_hash *h, char *key, size_t key_len)
  165. {
  166. curl_llist_element *le;
  167. curl_hash_element *he;
  168. curl_llist *l = FETCH_LIST(h, key, key_len);
  169. for (le = l->head;
  170. le;
  171. le = le->next) {
  172. he = le->ptr;
  173. if (hash_key_compare(he->key, he->key_len, key, key_len)) {
  174. return he->ptr;
  175. }
  176. }
  177. return NULL;
  178. }
  179. #if defined(CURLDEBUG) && defined(AGGRESIVE_TEST)
  180. void
  181. Curl_hash_apply(curl_hash *h, void *user,
  182. void (*cb)(void *user, void *ptr))
  183. {
  184. curl_llist_element *le;
  185. int i;
  186. for (i = 0; i < h->slots; ++i) {
  187. for (le = (h->table[i])->head;
  188. le;
  189. le = le->next) {
  190. curl_hash_element *el = le->ptr;
  191. cb(user, el->ptr);
  192. }
  193. }
  194. }
  195. #endif
  196. void
  197. Curl_hash_clean(curl_hash *h)
  198. {
  199. int i;
  200. for (i = 0; i < h->slots; ++i) {
  201. Curl_llist_destroy(h->table[i], (void *) h);
  202. }
  203. free(h->table);
  204. }
  205. void
  206. Curl_hash_clean_with_criterium(curl_hash *h, void *user,
  207. int (*comp)(void *, void *))
  208. {
  209. curl_llist_element *le;
  210. curl_llist_element *lnext;
  211. curl_llist *list;
  212. int i;
  213. for (i = 0; i < h->slots; ++i) {
  214. list = h->table[i];
  215. le = list->head; /* get first list entry */
  216. while(le) {
  217. curl_hash_element *he = le->ptr;
  218. lnext = le->next;
  219. /* ask the callback function if we shall remove this entry or not */
  220. if (comp(user, he->ptr)) {
  221. Curl_llist_remove(list, le, (void *) h);
  222. --h->size; /* one less entry in the hash now */
  223. }
  224. le = lnext;
  225. }
  226. }
  227. }
  228. #if 0
  229. int
  230. Curl_hash_count(curl_hash *h)
  231. {
  232. return h->size;
  233. }
  234. #endif
  235. void
  236. Curl_hash_destroy(curl_hash *h)
  237. {
  238. if (!h)
  239. return;
  240. Curl_hash_clean(h);
  241. free(h);
  242. }