xref: /curl/lib/hash.h (revision c0233a35)
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 
27 #include "curl_setup.h"
28 
29 #include <stddef.h>
30 
31 #include "llist.h"
32 
33 /* Hash function prototype */
34 typedef size_t (*hash_function) (void *key,
35                                  size_t key_length,
36                                  size_t slots_num);
37 
38 /*
39    Comparator function prototype. Compares two keys.
40 */
41 typedef size_t (*comp_function) (void *key1,
42                                  size_t key1_len,
43                                  void *key2,
44                                  size_t key2_len);
45 
46 typedef void (*Curl_hash_dtor)(void *);
47 
48 struct Curl_hash {
49   struct Curl_llist *table;
50 
51   /* Hash function to be used for this hash table */
52   hash_function hash_func;
53 
54   /* Comparator function to compare keys */
55   comp_function comp_func;
56   Curl_hash_dtor   dtor;
57   size_t slots;
58   size_t size;
59 #ifdef DEBUGBUILD
60   int init;
61 #endif
62 };
63 
64 typedef void (*Curl_hash_elem_dtor)(void *key, size_t key_len, void *p);
65 
66 struct Curl_hash_element {
67   struct Curl_llist_node list;
68   void   *ptr;
69   Curl_hash_elem_dtor dtor;
70   size_t key_len;
71 #ifdef DEBUGBUILD
72   int init;
73 #endif
74   char   key[1]; /* allocated memory following the struct */
75 };
76 
77 struct Curl_hash_iterator {
78   struct Curl_hash *hash;
79   size_t slot_index;
80   struct Curl_llist_node *current_element;
81 #ifdef DEBUGBUILD
82   int init;
83 #endif
84 };
85 
86 void Curl_hash_init(struct Curl_hash *h,
87                     size_t slots,
88                     hash_function hfunc,
89                     comp_function comparator,
90                     Curl_hash_dtor dtor);
91 
92 void *Curl_hash_add(struct Curl_hash *h, void *key, size_t key_len, void *p);
93 void *Curl_hash_add2(struct Curl_hash *h, void *key, size_t key_len, void *p,
94                      Curl_hash_elem_dtor dtor);
95 int Curl_hash_delete(struct Curl_hash *h, void *key, size_t key_len);
96 void *Curl_hash_pick(struct Curl_hash *, void *key, size_t key_len);
97 
98 void Curl_hash_destroy(struct Curl_hash *h);
99 size_t Curl_hash_count(struct Curl_hash *h);
100 void Curl_hash_clean(struct Curl_hash *h);
101 void Curl_hash_clean_with_criterium(struct Curl_hash *h, void *user,
102                                     int (*comp)(void *, void *));
103 size_t Curl_hash_str(void *key, size_t key_length, size_t slots_num);
104 size_t Curl_str_key_compare(void *k1, size_t key1_len, void *k2,
105                             size_t key2_len);
106 void Curl_hash_start_iterate(struct Curl_hash *hash,
107                              struct Curl_hash_iterator *iter);
108 struct Curl_hash_element *
109 Curl_hash_next_element(struct Curl_hash_iterator *iter);
110 
111 void Curl_hash_print(struct Curl_hash *h,
112                      void (*func)(void *));
113 
114 /* Hash for `curl_off_t` as key */
115 void Curl_hash_offt_init(struct Curl_hash *h, size_t slots,
116                          Curl_hash_dtor dtor);
117 
118 void *Curl_hash_offt_set(struct Curl_hash *h, curl_off_t id, void *elem);
119 int Curl_hash_offt_remove(struct Curl_hash *h, curl_off_t id);
120 void *Curl_hash_offt_get(struct Curl_hash *h, curl_off_t id);
121 
122 
123 #endif /* HEADER_CURL_HASH_H */
124