2
0

hash.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #ifndef HEADER_CURL_HASH_H
  2. #define HEADER_CURL_HASH_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  11. *
  12. * This software is licensed as described in the file COPYING, which
  13. * you should have received as part of this distribution. The terms
  14. * are also available at https://curl.se/docs/copyright.html.
  15. *
  16. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  17. * copies of the Software, and permit persons to whom the Software is
  18. * furnished to do so, under the terms of the COPYING file.
  19. *
  20. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. * KIND, either express or implied.
  22. *
  23. * SPDX-License-Identifier: curl
  24. *
  25. ***************************************************************************/
  26. #include "curl_setup.h"
  27. #include <stddef.h>
  28. #include "llist.h"
  29. /* Hash function prototype */
  30. typedef size_t (*hash_function) (void *key,
  31. size_t key_length,
  32. size_t slots_num);
  33. /*
  34. Comparator function prototype. Compares two keys.
  35. */
  36. typedef size_t (*comp_function) (void *key1,
  37. size_t key1_len,
  38. void *key2,
  39. size_t key2_len);
  40. typedef void (*Curl_hash_dtor)(void *);
  41. struct Curl_hash {
  42. struct Curl_llist *table;
  43. /* Hash function to be used for this hash table */
  44. hash_function hash_func;
  45. /* Comparator function to compare keys */
  46. comp_function comp_func;
  47. Curl_hash_dtor dtor;
  48. size_t slots;
  49. size_t size;
  50. };
  51. typedef void (*Curl_hash_elem_dtor)(void *key, size_t key_len, void *p);
  52. struct Curl_hash_element {
  53. struct Curl_llist_element list;
  54. void *ptr;
  55. Curl_hash_elem_dtor dtor;
  56. size_t key_len;
  57. char key[1]; /* allocated memory following the struct */
  58. };
  59. struct Curl_hash_iterator {
  60. struct Curl_hash *hash;
  61. size_t slot_index;
  62. struct Curl_llist_element *current_element;
  63. };
  64. void Curl_hash_init(struct Curl_hash *h,
  65. size_t slots,
  66. hash_function hfunc,
  67. comp_function comparator,
  68. Curl_hash_dtor dtor);
  69. void *Curl_hash_add(struct Curl_hash *h, void *key, size_t key_len, void *p);
  70. void *Curl_hash_add2(struct Curl_hash *h, void *key, size_t key_len, void *p,
  71. Curl_hash_elem_dtor dtor);
  72. int Curl_hash_delete(struct Curl_hash *h, void *key, size_t key_len);
  73. void *Curl_hash_pick(struct Curl_hash *, void *key, size_t key_len);
  74. #define Curl_hash_count(h) ((h)->size)
  75. void Curl_hash_destroy(struct Curl_hash *h);
  76. void Curl_hash_clean(struct Curl_hash *h);
  77. void Curl_hash_clean_with_criterium(struct Curl_hash *h, void *user,
  78. int (*comp)(void *, void *));
  79. size_t Curl_hash_str(void *key, size_t key_length, size_t slots_num);
  80. size_t Curl_str_key_compare(void *k1, size_t key1_len, void *k2,
  81. size_t key2_len);
  82. void Curl_hash_start_iterate(struct Curl_hash *hash,
  83. struct Curl_hash_iterator *iter);
  84. struct Curl_hash_element *
  85. Curl_hash_next_element(struct Curl_hash_iterator *iter);
  86. void Curl_hash_print(struct Curl_hash *h,
  87. void (*func)(void *));
  88. /* Hash for `curl_off_t` as key */
  89. void Curl_hash_offt_init(struct Curl_hash *h, size_t slots,
  90. Curl_hash_dtor dtor);
  91. void *Curl_hash_offt_set(struct Curl_hash *h, curl_off_t id, void *elem);
  92. int Curl_hash_offt_remove(struct Curl_hash *h, curl_off_t id);
  93. void *Curl_hash_offt_get(struct Curl_hash *h, curl_off_t id);
  94. #endif /* HEADER_CURL_HASH_H */