hash.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2002, 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. #ifdef MALLOCDEBUG
  29. /* this must be the last include file */
  30. #include "memdebug.h"
  31. #endif
  32. /* {{{ static unsigned long _hash_str (const char *, size_t)
  33. */
  34. static unsigned long
  35. _hash_str (const char *key, size_t key_length)
  36. {
  37. char *end = (char *) key + key_length;
  38. unsigned long h = 5381;
  39. while (key < end) {
  40. h += h << 5;
  41. h ^= (unsigned long) *key++;
  42. }
  43. return h;
  44. }
  45. /* }}} */
  46. /* {{{ static void _hash_element_dtor (void *, void *)
  47. */
  48. static void
  49. _hash_element_dtor (void *user, void *element)
  50. {
  51. curl_hash *h = (curl_hash *) user;
  52. curl_hash_element *e = (curl_hash_element *) element;
  53. if (e->key) {
  54. free(e->key);
  55. }
  56. h->dtor(e->ptr);
  57. free(e);
  58. }
  59. /* }}} */
  60. /* {{{ void curl_hash_init (curl_hash *, int, curl_hash_dtor)
  61. */
  62. void
  63. Curl_hash_init (curl_hash *h, int slots, curl_hash_dtor dtor)
  64. {
  65. int i;
  66. h->dtor = dtor;
  67. h->size = 0;
  68. h->slots = slots;
  69. h->table = (curl_llist **) malloc(slots * sizeof(curl_llist *));
  70. for (i = 0; i < slots; ++i) {
  71. h->table[i] = Curl_llist_alloc((curl_llist_dtor) _hash_element_dtor);
  72. }
  73. }
  74. /* }}} */
  75. /* {{{ curl_hash *curl_hash_alloc (int, curl_hash_dtor)
  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 (NULL == h)
  83. return NULL;
  84. Curl_hash_init(h, slots, dtor);
  85. return h;
  86. }
  87. /* }}} */
  88. /* {{{ static int _hash_key_compare (char *, size_t, char *, size_t)
  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. /* }}} */
  101. /* {{{ static int _mk_hash_element (curl_hash_element **, char *, size_t, const void *)
  102. */
  103. static int
  104. _mk_hash_element (curl_hash_element **e, char *key, size_t key_len, const void *p)
  105. {
  106. *e = (curl_hash_element *) malloc(sizeof(curl_hash_element));
  107. (*e)->key = strdup(key);
  108. (*e)->key_len = key_len;
  109. (*e)->ptr = (void *) p;
  110. return 0;
  111. }
  112. /* }}} */
  113. #define find_slot(__h, __k, __k_len) (_hash_str(__k, __k_len) % (__h)->slots)
  114. #define FETCH_LIST \
  115. curl_llist *l = h->table[find_slot(h, key, key_len)]
  116. /* {{{ int curl_hash_add (curl_hash *, char *, size_t, const void *)
  117. */
  118. int
  119. Curl_hash_add (curl_hash *h, char *key, size_t key_len, const void *p)
  120. {
  121. curl_hash_element *he;
  122. curl_llist_element *le;
  123. FETCH_LIST;
  124. for (le = CURL_LLIST_HEAD(l);
  125. le != NULL;
  126. le = CURL_LLIST_NEXT(le)) {
  127. he = (curl_hash_element *) CURL_LLIST_VALP(le);
  128. if (_hash_key_compare(he->key, he->key_len, key, key_len)) {
  129. h->dtor(he->ptr);
  130. he->ptr = (void *) p;
  131. return 1;
  132. }
  133. }
  134. if (_mk_hash_element(&he, key, key_len, p) != 0)
  135. return 0;
  136. if (Curl_llist_insert_next(l, CURL_LLIST_TAIL(l), he)) {
  137. ++h->size;
  138. return 1;
  139. }
  140. return 0;
  141. }
  142. /* }}} */
  143. /* {{{ int curl_hash_delete (curl_hash *, char *, size_t)
  144. */
  145. int
  146. Curl_hash_delete(curl_hash *h, char *key, size_t key_len)
  147. {
  148. curl_hash_element *he;
  149. curl_llist_element *le;
  150. FETCH_LIST;
  151. for (le = CURL_LLIST_HEAD(l);
  152. le != NULL;
  153. le = CURL_LLIST_NEXT(le)) {
  154. he = CURL_LLIST_VALP(le);
  155. if (_hash_key_compare(he->key, he->key_len, key, key_len)) {
  156. Curl_llist_remove(l, le, (void *) h);
  157. --h->size;
  158. return 1;
  159. }
  160. }
  161. return 0;
  162. }
  163. /* }}} */
  164. /* {{{ int curl_hash_pick (curl_hash *, char *, size_t, void **)
  165. */
  166. void *
  167. Curl_hash_pick(curl_hash *h, char *key, size_t key_len)
  168. {
  169. curl_llist_element *le;
  170. curl_hash_element *he;
  171. FETCH_LIST;
  172. for (le = CURL_LLIST_HEAD(l);
  173. le != NULL;
  174. le = CURL_LLIST_NEXT(le)) {
  175. he = CURL_LLIST_VALP(le);
  176. if (_hash_key_compare(he->key, he->key_len, key, key_len)) {
  177. return he->ptr;
  178. }
  179. }
  180. return NULL;
  181. }
  182. /* }}} */
  183. /* {{{ void curl_hash_apply (curl_hash *, void *, void (*)(void *, curl_hash_element *))
  184. */
  185. void
  186. Curl_hash_apply(curl_hash *h, void *user,
  187. void (*cb)(void *user, void *ptr))
  188. {
  189. curl_llist_element *le;
  190. int i;
  191. for (i = 0; i < h->slots; ++i) {
  192. for (le = CURL_LLIST_HEAD(h->table[i]);
  193. le != NULL;
  194. le = CURL_LLIST_NEXT(le)) {
  195. curl_hash_element *el = CURL_LLIST_VALP(le);
  196. cb(user, el->ptr);
  197. }
  198. }
  199. }
  200. /* }}} */
  201. /* {{{ void curl_hash_clean (curl_hash *)
  202. */
  203. void
  204. Curl_hash_clean(curl_hash *h)
  205. {
  206. int i;
  207. for (i = 0; i < h->slots; ++i) {
  208. Curl_llist_destroy(h->table[i], (void *) h);
  209. }
  210. free(h->table);
  211. }
  212. /* }}} */
  213. /* {{{ void curl_hash_clean_with_criterium (curl_hash *, void *,
  214. int (*)(void *, void *))
  215. */
  216. void
  217. Curl_hash_clean_with_criterium(curl_hash *h, void *user,
  218. int (*comp)(void *, void *))
  219. {
  220. curl_llist_element *le;
  221. curl_llist_element *lnext;
  222. int i;
  223. for (i = 0; i < h->slots; ++i) {
  224. le = CURL_LLIST_HEAD(h->table[i]);
  225. while(le != NULL)
  226. if (comp(user, ((curl_hash_element *) CURL_LLIST_VALP(le))->ptr)) {
  227. lnext = CURL_LLIST_NEXT(le);
  228. Curl_llist_remove(h->table[i], le, (void *) h);
  229. --h->size;
  230. le = lnext;
  231. }
  232. else
  233. le = CURL_LLIST_NEXT(le);
  234. }
  235. }
  236. /* {{{ int curl_hash_count (curl_hash *)
  237. */
  238. int
  239. Curl_hash_count(curl_hash *h)
  240. {
  241. return h->size;
  242. }
  243. /* }}} */
  244. /* {{{ void curl_hash_destroy (curl_hash *)
  245. */
  246. void
  247. Curl_hash_destroy(curl_hash *h)
  248. {
  249. if (!h)
  250. return;
  251. Curl_hash_clean(h);
  252. free(h);
  253. }
  254. /* }}} */
  255. /*
  256. * local variables:
  257. * eval: (load-file "../curl-mode.el")
  258. * end:
  259. * vim600: fdm=marker
  260. * vim: et sw=2 ts=2 sts=2 tw=78
  261. */