404 lines
9.8 KiB
C
Raw Normal View History

2015-04-27 22:25:09 +02:00
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
2023-07-02 19:51:09 +02:00
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
2015-04-27 22:25:09 +02:00
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
2021-09-14 00:13:48 +02:00
* are also available at https://curl.se/docs/copyright.html.
2015-04-27 22:25:09 +02:00
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
2022-11-16 20:14:03 +01:00
* SPDX-License-Identifier: curl
*
2015-04-27 22:25:09 +02:00
***************************************************************************/
#include "curl_setup.h"
2016-09-11 17:22:32 +02:00
#include <curl/curl.h>
2015-04-27 22:25:09 +02:00
#include "hash.h"
#include "llist.h"
#include "curl_memory.h"
2016-09-11 17:22:32 +02:00
2015-04-27 22:25:09 +02:00
/* The last #include file should be: */
#include "memdebug.h"
2024-11-11 15:18:55 +01:00
/* random patterns for API verification */
#define HASHINIT 0x7017e781
#define ITERINIT 0x5FEDCBA9
2015-04-27 22:25:09 +02:00
static void
hash_element_dtor(void *user, void *element)
{
2021-09-14 00:13:48 +02:00
struct Curl_hash *h = (struct Curl_hash *) user;
struct Curl_hash_element *e = (struct Curl_hash_element *) element;
2015-04-27 22:25:09 +02:00
if(e->ptr) {
2024-11-11 15:18:55 +01:00
if(e->dtor)
e->dtor(e->key, e->key_len, e->ptr);
else
h->dtor(e->ptr);
2015-04-27 22:25:09 +02:00
e->ptr = NULL;
}
e->key_len = 0;
free(e);
}
2016-09-11 17:22:32 +02:00
/* Initializes a hash structure.
* Return 1 on error, 0 is fine.
*
* @unittest: 1602
* @unittest: 1603
*/
2022-03-29 21:10:50 +02:00
void
2021-09-14 00:13:48 +02:00
Curl_hash_init(struct Curl_hash *h,
2024-07-09 14:46:46 +02:00
size_t slots,
2015-04-27 22:25:09 +02:00
hash_function hfunc,
comp_function comparator,
2021-09-14 00:13:48 +02:00
Curl_hash_dtor dtor)
2015-04-27 22:25:09 +02:00
{
2022-03-29 21:10:50 +02:00
DEBUGASSERT(h);
DEBUGASSERT(slots);
DEBUGASSERT(hfunc);
DEBUGASSERT(comparator);
DEBUGASSERT(dtor);
2015-04-27 22:25:09 +02:00
2022-03-29 21:10:50 +02:00
h->table = NULL;
2015-04-27 22:25:09 +02:00
h->hash_func = hfunc;
h->comp_func = comparator;
h->dtor = dtor;
h->size = 0;
h->slots = slots;
2024-11-11 15:18:55 +01:00
#ifdef DEBUGBUILD
h->init = HASHINIT;
#endif
2015-04-27 22:25:09 +02:00
}
2021-09-14 00:13:48 +02:00
static struct Curl_hash_element *
2024-11-11 15:18:55 +01:00
mk_hash_element(const void *key, size_t key_len, const void *p,
Curl_hash_elem_dtor dtor)
2015-04-27 22:25:09 +02:00
{
2017-07-20 19:35:53 +02:00
/* allocate the struct plus memory after it to store the key */
2021-09-14 00:13:48 +02:00
struct Curl_hash_element *he = malloc(sizeof(struct Curl_hash_element) +
2017-07-20 19:35:53 +02:00
key_len);
2015-04-27 22:25:09 +02:00
if(he) {
2017-07-20 19:35:53 +02:00
/* copy the key */
memcpy(he->key, key, key_len);
he->key_len = key_len;
he->ptr = (void *) p;
2024-11-11 15:18:55 +01:00
he->dtor = dtor;
2015-04-27 22:25:09 +02:00
}
return he;
}
2017-07-20 19:35:53 +02:00
#define FETCH_LIST(x,y,z) &x->table[x->hash_func(y, z, x->slots)]
2015-04-27 22:25:09 +02:00
2024-11-11 15:18:55 +01:00
void *Curl_hash_add2(struct Curl_hash *h, void *key, size_t key_len, void *p,
Curl_hash_elem_dtor dtor)
2015-04-27 22:25:09 +02:00
{
2021-09-14 00:13:48 +02:00
struct Curl_hash_element *he;
2024-11-11 15:18:55 +01:00
struct Curl_llist_node *le;
2022-03-29 21:10:50 +02:00
struct Curl_llist *l;
DEBUGASSERT(h);
DEBUGASSERT(h->slots);
2024-11-11 15:18:55 +01:00
DEBUGASSERT(h->init == HASHINIT);
2022-03-29 21:10:50 +02:00
if(!h->table) {
2024-07-09 14:46:46 +02:00
size_t i;
2022-03-29 21:10:50 +02:00
h->table = malloc(h->slots * sizeof(struct Curl_llist));
if(!h->table)
return NULL; /* OOM */
for(i = 0; i < h->slots; ++i)
Curl_llist_init(&h->table[i], hash_element_dtor);
}
l = FETCH_LIST(h, key, key_len);
2015-04-27 22:25:09 +02:00
2024-11-11 15:18:55 +01:00
for(le = Curl_llist_head(l); le; le = Curl_node_next(le)) {
he = (struct Curl_hash_element *) Curl_node_elem(le);
2015-04-27 22:25:09 +02:00
if(h->comp_func(he->key, he->key_len, key, key_len)) {
2024-11-11 15:18:55 +01:00
Curl_node_uremove(le, (void *)h);
2015-04-27 22:25:09 +02:00
--h->size;
break;
}
}
2024-11-11 15:18:55 +01:00
he = mk_hash_element(key, key_len, p, dtor);
2015-04-27 22:25:09 +02:00
if(he) {
2024-07-09 14:46:46 +02:00
Curl_llist_append(l, he, &he->list);
2017-07-20 19:35:53 +02:00
++h->size;
return p; /* return the new entry */
2015-04-27 22:25:09 +02:00
}
return NULL; /* failure */
}
2024-11-11 15:18:55 +01:00
/* Insert the data in the hash. If there already was a match in the hash, that
* data is replaced. This function also "lazily" allocates the table if
* needed, as it is not done in the _init function (anymore).
*
* @unittest: 1305
* @unittest: 1602
* @unittest: 1603
*/
void *
Curl_hash_add(struct Curl_hash *h, void *key, size_t key_len, void *p)
{
return Curl_hash_add2(h, key, key_len, p, NULL);
}
2016-09-11 17:22:32 +02:00
/* Remove the identified hash entry.
* Returns non-zero on failure.
*
* @unittest: 1603
*/
2021-09-14 00:13:48 +02:00
int Curl_hash_delete(struct Curl_hash *h, void *key, size_t key_len)
2015-04-27 22:25:09 +02:00
{
2022-03-29 21:10:50 +02:00
DEBUGASSERT(h);
DEBUGASSERT(h->slots);
2024-11-11 15:18:55 +01:00
DEBUGASSERT(h->init == HASHINIT);
2022-03-29 21:10:50 +02:00
if(h->table) {
2024-11-11 15:18:55 +01:00
struct Curl_llist_node *le;
struct Curl_llist *l = FETCH_LIST(h, key, key_len);
2022-03-29 21:10:50 +02:00
2024-11-11 15:18:55 +01:00
for(le = Curl_llist_head(l); le; le = Curl_node_next(le)) {
struct Curl_hash_element *he = Curl_node_elem(le);
2022-03-29 21:10:50 +02:00
if(h->comp_func(he->key, he->key_len, key, key_len)) {
2024-11-11 15:18:55 +01:00
Curl_node_uremove(le, (void *) h);
2022-03-29 21:10:50 +02:00
--h->size;
return 0;
}
2015-04-27 22:25:09 +02:00
}
}
return 1;
}
2016-09-11 17:22:32 +02:00
/* Retrieves a hash element.
*
* @unittest: 1603
*/
2015-04-27 22:25:09 +02:00
void *
2021-09-14 00:13:48 +02:00
Curl_hash_pick(struct Curl_hash *h, void *key, size_t key_len)
2015-04-27 22:25:09 +02:00
{
2022-03-29 21:10:50 +02:00
DEBUGASSERT(h);
2024-11-11 15:18:55 +01:00
DEBUGASSERT(h->init == HASHINIT);
2022-03-29 21:10:50 +02:00
if(h->table) {
2024-11-11 15:18:55 +01:00
struct Curl_llist_node *le;
struct Curl_llist *l;
2022-03-29 21:10:50 +02:00
DEBUGASSERT(h->slots);
2015-04-27 22:25:09 +02:00
l = FETCH_LIST(h, key, key_len);
2024-11-11 15:18:55 +01:00
for(le = Curl_llist_head(l); le; le = Curl_node_next(le)) {
struct Curl_hash_element *he = Curl_node_elem(le);
2015-04-27 22:25:09 +02:00
if(h->comp_func(he->key, he->key_len, key, key_len)) {
return he->ptr;
}
}
}
return NULL;
}
2015-11-17 17:22:37 +01:00
/* Destroys all the entries in the given hash and resets its attributes,
* prepping the given hash for [static|dynamic] deallocation.
2016-09-11 17:22:32 +02:00
*
* @unittest: 1305
* @unittest: 1602
* @unittest: 1603
2015-11-17 17:22:37 +01:00
*/
2015-04-27 22:25:09 +02:00
void
2021-09-14 00:13:48 +02:00
Curl_hash_destroy(struct Curl_hash *h)
2015-04-27 22:25:09 +02:00
{
2024-11-11 15:18:55 +01:00
DEBUGASSERT(h->init == HASHINIT);
2022-03-29 21:10:50 +02:00
if(h->table) {
2024-07-09 14:46:46 +02:00
size_t i;
2022-03-29 21:10:50 +02:00
for(i = 0; i < h->slots; ++i) {
Curl_llist_destroy(&h->table[i], (void *) h);
}
Curl_safefree(h->table);
2015-04-27 22:25:09 +02:00
}
h->size = 0;
h->slots = 0;
}
2015-11-17 17:22:37 +01:00
/* Removes all the entries in the given hash.
*
* @unittest: 1602
*/
void
2021-09-14 00:13:48 +02:00
Curl_hash_clean(struct Curl_hash *h)
2015-11-17 17:22:37 +01:00
{
Curl_hash_clean_with_criterium(h, NULL, NULL);
}
2024-11-11 15:18:55 +01:00
size_t Curl_hash_count(struct Curl_hash *h)
{
DEBUGASSERT(h->init == HASHINIT);
return h->size;
}
2015-11-17 17:22:37 +01:00
/* Cleans all entries that pass the comp function criteria. */
2015-04-27 22:25:09 +02:00
void
2021-09-14 00:13:48 +02:00
Curl_hash_clean_with_criterium(struct Curl_hash *h, void *user,
2015-04-27 22:25:09 +02:00
int (*comp)(void *, void *))
{
2024-07-09 14:46:46 +02:00
size_t i;
2015-04-27 22:25:09 +02:00
2022-03-29 21:10:50 +02:00
if(!h || !h->table)
2015-04-27 22:25:09 +02:00
return;
2024-11-11 15:18:55 +01:00
DEBUGASSERT(h->init == HASHINIT);
2015-04-27 22:25:09 +02:00
for(i = 0; i < h->slots; ++i) {
2024-11-11 15:18:55 +01:00
struct Curl_llist *list = &h->table[i];
struct Curl_llist_node *le =
Curl_llist_head(list); /* get first list entry */
2015-04-27 22:25:09 +02:00
while(le) {
2024-11-11 15:18:55 +01:00
struct Curl_hash_element *he = Curl_node_elem(le);
struct Curl_llist_node *lnext = Curl_node_next(le);
2015-04-27 22:25:09 +02:00
/* ask the callback function if we shall remove this entry or not */
2021-09-14 00:13:48 +02:00
if(!comp || comp(user, he->ptr)) {
2024-11-11 15:18:55 +01:00
Curl_node_uremove(le, (void *) h);
2015-04-27 22:25:09 +02:00
--h->size; /* one less entry in the hash now */
}
le = lnext;
}
}
}
2017-07-20 19:35:53 +02:00
size_t Curl_hash_str(void *key, size_t key_length, size_t slots_num)
2015-04-27 22:25:09 +02:00
{
2017-07-20 19:35:53 +02:00
const char *key_str = (const char *) key;
2015-04-27 22:25:09 +02:00
const char *end = key_str + key_length;
2018-08-09 18:06:22 +02:00
size_t h = 5381;
2015-04-27 22:25:09 +02:00
while(key_str < end) {
2024-11-11 15:18:55 +01:00
size_t j = (size_t)*key_str++;
2015-04-27 22:25:09 +02:00
h += h << 5;
2024-11-11 15:18:55 +01:00
h ^= j;
2015-04-27 22:25:09 +02:00
}
return (h % slots_num);
}
2015-11-17 17:22:37 +01:00
size_t Curl_str_key_compare(void *k1, size_t key1_len,
void *k2, size_t key2_len)
2015-04-27 22:25:09 +02:00
{
2015-11-17 17:22:37 +01:00
if((key1_len == key2_len) && !memcmp(k1, k2, key1_len))
2015-04-27 22:25:09 +02:00
return 1;
return 0;
}
2021-09-14 00:13:48 +02:00
void Curl_hash_start_iterate(struct Curl_hash *hash,
struct Curl_hash_iterator *iter)
2015-04-27 22:25:09 +02:00
{
2024-11-11 15:18:55 +01:00
DEBUGASSERT(hash->init == HASHINIT);
2015-04-27 22:25:09 +02:00
iter->hash = hash;
iter->slot_index = 0;
iter->current_element = NULL;
2024-11-11 15:18:55 +01:00
#ifdef DEBUGBUILD
iter->init = ITERINIT;
#endif
2015-04-27 22:25:09 +02:00
}
2021-09-14 00:13:48 +02:00
struct Curl_hash_element *
Curl_hash_next_element(struct Curl_hash_iterator *iter)
2015-04-27 22:25:09 +02:00
{
2024-11-11 15:18:55 +01:00
struct Curl_hash *h;
DEBUGASSERT(iter->init == ITERINIT);
h = iter->hash;
2022-03-29 21:10:50 +02:00
if(!h->table)
return NULL; /* empty hash, nothing to return */
2015-04-27 22:25:09 +02:00
/* Get the next element in the current list, if any */
if(iter->current_element)
2024-11-11 15:18:55 +01:00
iter->current_element = Curl_node_next(iter->current_element);
2015-04-27 22:25:09 +02:00
/* If we have reached the end of the list, find the next one */
if(!iter->current_element) {
2024-07-09 14:46:46 +02:00
size_t i;
2018-01-26 17:06:56 +01:00
for(i = iter->slot_index; i < h->slots; i++) {
2024-11-11 15:18:55 +01:00
if(Curl_llist_head(&h->table[i])) {
iter->current_element = Curl_llist_head(&h->table[i]);
2018-01-26 17:06:56 +01:00
iter->slot_index = i + 1;
2015-04-27 22:25:09 +02:00
break;
}
}
}
if(iter->current_element) {
2024-11-11 15:18:55 +01:00
struct Curl_hash_element *he = Curl_node_elem(iter->current_element);
2015-04-27 22:25:09 +02:00
return he;
}
2017-07-20 19:35:53 +02:00
return NULL;
2015-04-27 22:25:09 +02:00
}
#if 0 /* useful function for debugging hashes and their contents */
2021-09-14 00:13:48 +02:00
void Curl_hash_print(struct Curl_hash *h,
2015-04-27 22:25:09 +02:00
void (*func)(void *))
{
2021-09-14 00:13:48 +02:00
struct Curl_hash_iterator iter;
struct Curl_hash_element *he;
2024-07-09 14:46:46 +02:00
size_t last_index = ~0;
2015-04-27 22:25:09 +02:00
if(!h)
return;
fprintf(stderr, "=Hash dump=\n");
Curl_hash_start_iterate(h, &iter);
he = Curl_hash_next_element(&iter);
while(he) {
if(iter.slot_index != last_index) {
fprintf(stderr, "index %d:", iter.slot_index);
2024-07-09 14:46:46 +02:00
if(last_index != ~0) {
2015-04-27 22:25:09 +02:00
fprintf(stderr, "\n");
}
last_index = iter.slot_index;
}
if(func)
func(he->ptr);
else
fprintf(stderr, " [%p]", (void *)he->ptr);
he = Curl_hash_next_element(&iter);
}
fprintf(stderr, "\n");
}
#endif
2024-07-09 14:46:46 +02:00
void Curl_hash_offt_init(struct Curl_hash *h,
size_t slots,
Curl_hash_dtor dtor)
{
Curl_hash_init(h, slots, Curl_hash_str, Curl_str_key_compare, dtor);
}
void *Curl_hash_offt_set(struct Curl_hash *h, curl_off_t id, void *elem)
{
return Curl_hash_add(h, &id, sizeof(id), elem);
}
int Curl_hash_offt_remove(struct Curl_hash *h, curl_off_t id)
{
return Curl_hash_delete(h, &id, sizeof(id));
}
void *Curl_hash_offt_get(struct Curl_hash *h, curl_off_t id)
{
return Curl_hash_pick(h, &id, sizeof(id));
}