2
0

hash.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #ifdef DEBUGBUILD
  51. int init;
  52. #endif
  53. };
  54. typedef void (*Curl_hash_elem_dtor)(void *key, size_t key_len, void *p);
  55. struct Curl_hash_element {
  56. struct Curl_llist_node list;
  57. void *ptr;
  58. Curl_hash_elem_dtor dtor;
  59. size_t key_len;
  60. #ifdef DEBUGBUILD
  61. int init;
  62. #endif
  63. char key[1]; /* allocated memory following the struct */
  64. };
  65. struct Curl_hash_iterator {
  66. struct Curl_hash *hash;
  67. size_t slot_index;
  68. struct Curl_llist_node *current_element;
  69. #ifdef DEBUGBUILD
  70. int init;
  71. #endif
  72. };
  73. void Curl_hash_init(struct Curl_hash *h,
  74. size_t slots,
  75. hash_function hfunc,
  76. comp_function comparator,
  77. Curl_hash_dtor dtor);
  78. void *Curl_hash_add(struct Curl_hash *h, void *key, size_t key_len, void *p);
  79. void *Curl_hash_add2(struct Curl_hash *h, void *key, size_t key_len, void *p,
  80. Curl_hash_elem_dtor dtor);
  81. int Curl_hash_delete(struct Curl_hash *h, void *key, size_t key_len);
  82. void *Curl_hash_pick(struct Curl_hash *, void *key, size_t key_len);
  83. void Curl_hash_destroy(struct Curl_hash *h);
  84. size_t Curl_hash_count(struct Curl_hash *h);
  85. void Curl_hash_clean(struct Curl_hash *h);
  86. void Curl_hash_clean_with_criterium(struct Curl_hash *h, void *user,
  87. int (*comp)(void *, void *));
  88. size_t Curl_hash_str(void *key, size_t key_length, size_t slots_num);
  89. size_t Curl_str_key_compare(void *k1, size_t key1_len, void *k2,
  90. size_t key2_len);
  91. void Curl_hash_start_iterate(struct Curl_hash *hash,
  92. struct Curl_hash_iterator *iter);
  93. struct Curl_hash_element *
  94. Curl_hash_next_element(struct Curl_hash_iterator *iter);
  95. void Curl_hash_print(struct Curl_hash *h,
  96. void (*func)(void *));
  97. /* Hash for `curl_off_t` as key */
  98. void Curl_hash_offt_init(struct Curl_hash *h, size_t slots,
  99. Curl_hash_dtor dtor);
  100. void *Curl_hash_offt_set(struct Curl_hash *h, curl_off_t id, void *elem);
  101. int Curl_hash_offt_remove(struct Curl_hash *h, curl_off_t id);
  102. void *Curl_hash_offt_get(struct Curl_hash *h, curl_off_t id);
  103. #endif /* HEADER_CURL_HASH_H */