2
0

hash.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, 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. ***************************************************************************/
  22. #include "setup.h"
  23. #include "hash.h"
  24. #include "llist.h"
  25. #define _MPRINTF_REPLACE /* use our functions only */
  26. #include <curl/mprintf.h>
  27. #include "curl_memory.h"
  28. /* The last #include file should be: */
  29. #include "memdebug.h"
  30. static void
  31. hash_element_dtor(void *user, void *element)
  32. {
  33. struct curl_hash *h = (struct curl_hash *) user;
  34. struct curl_hash_element *e = (struct curl_hash_element *) element;
  35. Curl_safefree(e->key);
  36. if(e->ptr) {
  37. h->dtor(e->ptr);
  38. e->ptr = NULL;
  39. }
  40. e->key_len = 0;
  41. free(e);
  42. }
  43. /* return 1 on error, 0 is fine */
  44. int
  45. Curl_hash_init(struct curl_hash *h,
  46. int slots,
  47. hash_function hfunc,
  48. comp_function comparator,
  49. curl_hash_dtor dtor)
  50. {
  51. int i;
  52. if(!slots || !hfunc || !comparator ||!dtor) {
  53. return 1; /* failure */
  54. }
  55. h->hash_func = hfunc;
  56. h->comp_func = comparator;
  57. h->dtor = dtor;
  58. h->size = 0;
  59. h->slots = slots;
  60. h->table = 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. h->table[i] = NULL;
  68. }
  69. free(h->table);
  70. h->table = NULL;
  71. h->slots = 0;
  72. return 1; /* failure */
  73. }
  74. }
  75. return 0; /* fine */
  76. }
  77. else {
  78. h->slots = 0;
  79. return 1; /* failure */
  80. }
  81. }
  82. struct curl_hash *
  83. Curl_hash_alloc(int slots,
  84. hash_function hfunc,
  85. comp_function comparator,
  86. curl_hash_dtor dtor)
  87. {
  88. struct curl_hash *h;
  89. if(!slots || !hfunc || !comparator ||!dtor) {
  90. return NULL; /* failure */
  91. }
  92. h = malloc(sizeof(struct curl_hash));
  93. if(h) {
  94. if(Curl_hash_init(h, slots, hfunc, comparator, dtor)) {
  95. /* failure */
  96. free(h);
  97. h = NULL;
  98. }
  99. }
  100. return h;
  101. }
  102. static struct curl_hash_element *
  103. mk_hash_element(const void *key, size_t key_len, const void *p)
  104. {
  105. struct curl_hash_element *he = malloc(sizeof(struct curl_hash_element));
  106. if(he) {
  107. void *dupkey = malloc(key_len);
  108. if(dupkey) {
  109. /* copy the key */
  110. memcpy(dupkey, key, key_len);
  111. he->key = dupkey;
  112. he->key_len = key_len;
  113. he->ptr = (void *) p;
  114. }
  115. else {
  116. /* failed to duplicate the key, free memory and fail */
  117. free(he);
  118. he = NULL;
  119. }
  120. }
  121. return he;
  122. }
  123. #define FETCH_LIST(x,y,z) x->table[x->hash_func(y, z, x->slots)]
  124. /* Insert the data in the hash. If there already was a match in the hash,
  125. * that data is replaced.
  126. *
  127. * @unittest: 1305
  128. */
  129. void *
  130. Curl_hash_add(struct curl_hash *h, void *key, size_t key_len, void *p)
  131. {
  132. struct curl_hash_element *he;
  133. struct curl_llist_element *le;
  134. struct curl_llist *l = FETCH_LIST (h, key, key_len);
  135. for(le = l->head; le; le = le->next) {
  136. he = (struct curl_hash_element *) le->ptr;
  137. if(h->comp_func(he->key, he->key_len, key, key_len)) {
  138. Curl_llist_remove(l, le, (void *)h);
  139. --h->size;
  140. break;
  141. }
  142. }
  143. he = mk_hash_element(key, key_len, p);
  144. if(he) {
  145. if(Curl_llist_insert_next(l, l->tail, he)) {
  146. ++h->size;
  147. return p; /* return the new entry */
  148. }
  149. /*
  150. * Couldn't insert it, destroy the 'he' element and the key again. We
  151. * don't call hash_element_dtor() since that would also call the
  152. * "destructor" for the actual data 'p'. When we fail, we shall not touch
  153. * that data.
  154. */
  155. free(he->key);
  156. free(he);
  157. }
  158. return NULL; /* failure */
  159. }
  160. /* remove the identified hash entry, returns non-zero on failure */
  161. int Curl_hash_delete(struct curl_hash *h, void *key, size_t key_len)
  162. {
  163. struct curl_llist_element *le;
  164. struct curl_hash_element *he;
  165. struct curl_llist *l = FETCH_LIST(h, key, key_len);
  166. for(le = l->head; le; le = le->next) {
  167. he = le->ptr;
  168. if(h->comp_func(he->key, he->key_len, key, key_len)) {
  169. Curl_llist_remove(l, le, (void *) h);
  170. --h->size;
  171. return 0;
  172. }
  173. }
  174. return 1;
  175. }
  176. void *
  177. Curl_hash_pick(struct curl_hash *h, void *key, size_t key_len)
  178. {
  179. struct curl_llist_element *le;
  180. struct curl_hash_element *he;
  181. struct curl_llist *l = FETCH_LIST(h, key, key_len);
  182. for(le = l->head; le; le = le->next) {
  183. he = le->ptr;
  184. if(h->comp_func(he->key, he->key_len, key, key_len)) {
  185. return he->ptr;
  186. }
  187. }
  188. return NULL;
  189. }
  190. #if defined(DEBUGBUILD) && defined(AGGRESIVE_TEST)
  191. void
  192. Curl_hash_apply(curl_hash *h, void *user,
  193. void (*cb)(void *user, void *ptr))
  194. {
  195. struct curl_llist_element *le;
  196. int i;
  197. for(i = 0; i < h->slots; ++i) {
  198. for(le = (h->table[i])->head;
  199. le;
  200. le = le->next) {
  201. curl_hash_element *el = le->ptr;
  202. cb(user, el->ptr);
  203. }
  204. }
  205. }
  206. #endif
  207. void
  208. Curl_hash_clean(struct curl_hash *h)
  209. {
  210. int i;
  211. for(i = 0; i < h->slots; ++i) {
  212. Curl_llist_destroy(h->table[i], (void *) h);
  213. h->table[i] = NULL;
  214. }
  215. free(h->table);
  216. h->table = NULL;
  217. h->size = 0;
  218. h->slots = 0;
  219. }
  220. void
  221. Curl_hash_clean_with_criterium(struct curl_hash *h, void *user,
  222. int (*comp)(void *, void *))
  223. {
  224. struct curl_llist_element *le;
  225. struct curl_llist_element *lnext;
  226. struct curl_llist *list;
  227. int i;
  228. if(!h)
  229. return;
  230. for(i = 0; i < h->slots; ++i) {
  231. list = h->table[i];
  232. le = list->head; /* get first list entry */
  233. while(le) {
  234. struct curl_hash_element *he = le->ptr;
  235. lnext = le->next;
  236. /* ask the callback function if we shall remove this entry or not */
  237. if(comp(user, he->ptr)) {
  238. Curl_llist_remove(list, le, (void *) h);
  239. --h->size; /* one less entry in the hash now */
  240. }
  241. le = lnext;
  242. }
  243. }
  244. }
  245. void
  246. Curl_hash_destroy(struct curl_hash *h)
  247. {
  248. if(!h)
  249. return;
  250. Curl_hash_clean(h);
  251. free(h);
  252. }
  253. size_t Curl_hash_str(void* key, size_t key_length, size_t slots_num)
  254. {
  255. const char* key_str = (const char *) key;
  256. const char *end = key_str + key_length;
  257. unsigned long h = 5381;
  258. while(key_str < end) {
  259. h += h << 5;
  260. h ^= (unsigned long) *key_str++;
  261. }
  262. return (h % slots_num);
  263. }
  264. size_t Curl_str_key_compare(void*k1, size_t key1_len, void*k2, size_t key2_len)
  265. {
  266. char *key1 = (char *)k1;
  267. char *key2 = (char *)k2;
  268. if(key1_len == key2_len &&
  269. *key1 == *key2 &&
  270. memcmp(key1, key2, key1_len) == 0) {
  271. return 1;
  272. }
  273. return 0;
  274. }
  275. #if 0 /* useful function for debugging hashes and their contents */
  276. void Curl_hash_print(struct curl_hash *h,
  277. void (*func)(void *))
  278. {
  279. int i;
  280. struct curl_llist_element *le;
  281. struct curl_llist *list;
  282. struct curl_hash_element *he;
  283. if(!h)
  284. return;
  285. fprintf(stderr, "=Hash dump=\n");
  286. for(i = 0; i < h->slots; i++) {
  287. list = h->table[i];
  288. le = list->head; /* get first list entry */
  289. if(le) {
  290. fprintf(stderr, "index %d:", i);
  291. while(le) {
  292. he = le->ptr;
  293. if(func)
  294. func(he->ptr);
  295. else
  296. fprintf(stderr, " [%p]", he->ptr);
  297. le = le->next;
  298. }
  299. fprintf(stderr, "\n");
  300. }
  301. }
  302. }
  303. #endif