1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
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 https://curl.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * SPDX-License-Identifier: curl
23 ***************************************************************************/
25 #include "curl_setup.h"
27 #include <curl/curl.h>
31 #include "curl_memory.h"
33 /* The last #include file should be: */
37 hash_element_dtor(void *user, void *element)
39 struct Curl_hash *h = (struct Curl_hash *) user;
40 struct Curl_hash_element *e = (struct Curl_hash_element *) element;
52 /* Initializes a hash structure.
53 * Return 1 on error, 0 is fine.
59 Curl_hash_init(struct Curl_hash *h,
62 comp_function comparator,
68 DEBUGASSERT(comparator);
73 h->comp_func = comparator;
79 static struct Curl_hash_element *
80 mk_hash_element(const void *key, size_t key_len, const void *p)
82 /* allocate the struct plus memory after it to store the key */
83 struct Curl_hash_element *he = malloc(sizeof(struct Curl_hash_element) +
87 memcpy(he->key, key, key_len);
88 he->key_len = key_len;
94 #define FETCH_LIST(x,y,z) &x->table[x->hash_func(y, z, x->slots)]
96 /* Insert the data in the hash. If there already was a match in the hash, that
97 * data is replaced. This function also "lazily" allocates the table if
98 * needed, as it isn't done in the _init function (anymore).
105 Curl_hash_add(struct Curl_hash *h, void *key, size_t key_len, void *p)
107 struct Curl_hash_element *he;
108 struct Curl_llist_element *le;
109 struct Curl_llist *l;
112 DEBUGASSERT(h->slots);
115 h->table = malloc(h->slots * sizeof(struct Curl_llist));
117 return NULL; /* OOM */
118 for(i = 0; i < h->slots; ++i)
119 Curl_llist_init(&h->table[i], hash_element_dtor);
122 l = FETCH_LIST(h, key, key_len);
124 for(le = l->head; le; le = le->next) {
125 he = (struct Curl_hash_element *) le->ptr;
126 if(h->comp_func(he->key, he->key_len, key, key_len)) {
127 Curl_llist_remove(l, le, (void *)h);
133 he = mk_hash_element(key, key_len, p);
135 Curl_llist_insert_next(l, l->tail, he, &he->list);
137 return p; /* return the new entry */
140 return NULL; /* failure */
143 /* Remove the identified hash entry.
144 * Returns non-zero on failure.
148 int Curl_hash_delete(struct Curl_hash *h, void *key, size_t key_len)
150 struct Curl_llist_element *le;
151 struct Curl_llist *l;
154 DEBUGASSERT(h->slots);
156 l = FETCH_LIST(h, key, key_len);
158 for(le = l->head; le; le = le->next) {
159 struct Curl_hash_element *he = le->ptr;
160 if(h->comp_func(he->key, he->key_len, key, key_len)) {
161 Curl_llist_remove(l, le, (void *) h);
170 /* Retrieves a hash element.
175 Curl_hash_pick(struct Curl_hash *h, void *key, size_t key_len)
177 struct Curl_llist_element *le;
178 struct Curl_llist *l;
182 DEBUGASSERT(h->slots);
183 l = FETCH_LIST(h, key, key_len);
184 for(le = l->head; le; le = le->next) {
185 struct Curl_hash_element *he = le->ptr;
186 if(h->comp_func(he->key, he->key_len, key, key_len)) {
195 #if defined(DEBUGBUILD) && defined(AGGRESSIVE_TEST)
197 Curl_hash_apply(Curl_hash *h, void *user,
198 void (*cb)(void *user, void *ptr))
200 struct Curl_llist_element *le;
203 for(i = 0; i < h->slots; ++i) {
204 for(le = (h->table[i])->head;
207 Curl_hash_element *el = le->ptr;
214 /* Destroys all the entries in the given hash and resets its attributes,
215 * prepping the given hash for [static|dynamic] deallocation.
222 Curl_hash_destroy(struct Curl_hash *h)
226 for(i = 0; i < h->slots; ++i) {
227 Curl_llist_destroy(&h->table[i], (void *) h);
229 Curl_safefree(h->table);
235 /* Removes all the entries in the given hash.
240 Curl_hash_clean(struct Curl_hash *h)
242 Curl_hash_clean_with_criterium(h, NULL, NULL);
245 /* Cleans all entries that pass the comp function criteria. */
247 Curl_hash_clean_with_criterium(struct Curl_hash *h, void *user,
248 int (*comp)(void *, void *))
250 struct Curl_llist_element *le;
251 struct Curl_llist_element *lnext;
252 struct Curl_llist *list;
258 for(i = 0; i < h->slots; ++i) {
260 le = list->head; /* get first list entry */
262 struct Curl_hash_element *he = le->ptr;
264 /* ask the callback function if we shall remove this entry or not */
265 if(!comp || comp(user, he->ptr)) {
266 Curl_llist_remove(list, le, (void *) h);
267 --h->size; /* one less entry in the hash now */
274 size_t Curl_hash_str(void *key, size_t key_length, size_t slots_num)
276 const char *key_str = (const char *) key;
277 const char *end = key_str + key_length;
280 while(key_str < end) {
285 return (h % slots_num);
288 size_t Curl_str_key_compare(void *k1, size_t key1_len,
289 void *k2, size_t key2_len)
291 if((key1_len == key2_len) && !memcmp(k1, k2, key1_len))
297 void Curl_hash_start_iterate(struct Curl_hash *hash,
298 struct Curl_hash_iterator *iter)
301 iter->slot_index = 0;
302 iter->current_element = NULL;
305 struct Curl_hash_element *
306 Curl_hash_next_element(struct Curl_hash_iterator *iter)
308 struct Curl_hash *h = iter->hash;
311 return NULL; /* empty hash, nothing to return */
313 /* Get the next element in the current list, if any */
314 if(iter->current_element)
315 iter->current_element = iter->current_element->next;
317 /* If we have reached the end of the list, find the next one */
318 if(!iter->current_element) {
320 for(i = iter->slot_index; i < h->slots; i++) {
321 if(h->table[i].head) {
322 iter->current_element = h->table[i].head;
323 iter->slot_index = i + 1;
329 if(iter->current_element) {
330 struct Curl_hash_element *he = iter->current_element->ptr;
333 iter->current_element = NULL;
337 #if 0 /* useful function for debugging hashes and their contents */
338 void Curl_hash_print(struct Curl_hash *h,
339 void (*func)(void *))
341 struct Curl_hash_iterator iter;
342 struct Curl_hash_element *he;
348 fprintf(stderr, "=Hash dump=\n");
350 Curl_hash_start_iterate(h, &iter);
352 he = Curl_hash_next_element(&iter);
354 if(iter.slot_index != last_index) {
355 fprintf(stderr, "index %d:", iter.slot_index);
356 if(last_index >= 0) {
357 fprintf(stderr, "\n");
359 last_index = iter.slot_index;
365 fprintf(stderr, " [%p]", (void *)he->ptr);
367 he = Curl_hash_next_element(&iter);
369 fprintf(stderr, "\n");