2002-09-03 19:52:59 +08:00
|
|
|
/***************************************************************************
|
2004-06-24 15:43:48 +08:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
2002-01-03 18:22:59 +08:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2007-06-27 05:09:28 +08:00
|
|
|
* Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2002-01-03 18:22:59 +08:00
|
|
|
*
|
2002-09-03 19:52:59 +08:00
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
|
|
|
* are also available at http://curl.haxx.se/docs/copyright.html.
|
2004-06-24 15:43:48 +08:00
|
|
|
*
|
2002-01-03 18:22:59 +08: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
|
2002-09-03 19:52:59 +08:00
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
2002-01-03 18:22:59 +08:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
* $Id$
|
2002-09-03 19:52:59 +08:00
|
|
|
***************************************************************************/
|
2002-01-03 18:22:59 +08:00
|
|
|
|
|
|
|
#include "setup.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
2002-02-17 22:55:35 +08:00
|
|
|
#include <stdlib.h>
|
2002-11-05 18:51:41 +08:00
|
|
|
|
2002-01-03 18:22:59 +08:00
|
|
|
#include "hash.h"
|
|
|
|
#include "llist.h"
|
2004-05-11 19:30:23 +08:00
|
|
|
#include "memory.h"
|
2002-01-03 18:22:59 +08:00
|
|
|
|
|
|
|
/* this must be the last include file */
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
2004-06-24 15:43:48 +08:00
|
|
|
static void
|
2003-09-05 20:44:35 +08:00
|
|
|
hash_element_dtor(void *user, void *element)
|
2002-01-03 18:22:59 +08:00
|
|
|
{
|
2005-01-25 08:06:29 +08:00
|
|
|
struct curl_hash *h = (struct curl_hash *) user;
|
|
|
|
struct curl_hash_element *e = (struct curl_hash_element *) element;
|
2002-04-13 07:40:19 +08:00
|
|
|
|
2005-01-25 08:06:29 +08:00
|
|
|
if (e->key)
|
2002-04-13 07:40:19 +08:00
|
|
|
free(e->key);
|
|
|
|
|
2002-01-03 18:22:59 +08:00
|
|
|
h->dtor(e->ptr);
|
|
|
|
|
|
|
|
free(e);
|
|
|
|
}
|
|
|
|
|
2003-08-14 23:05:13 +08:00
|
|
|
/* return 1 on error, 0 is fine */
|
|
|
|
int
|
2007-06-27 05:09:28 +08:00
|
|
|
Curl_hash_init(struct curl_hash *h,
|
|
|
|
int slots,
|
|
|
|
hash_function hfunc,
|
|
|
|
comp_function comparator,
|
|
|
|
curl_hash_dtor dtor)
|
2002-01-03 18:22:59 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2007-06-27 05:09:28 +08:00
|
|
|
if (!slots || !hfunc || !comparator ||!dtor) {
|
|
|
|
return 1; /* failure */
|
|
|
|
}
|
|
|
|
|
|
|
|
h->hash_func = hfunc;
|
|
|
|
h->comp_func = comparator;
|
2002-01-03 18:22:59 +08:00
|
|
|
h->dtor = dtor;
|
|
|
|
h->size = 0;
|
2004-06-24 15:43:48 +08:00
|
|
|
h->slots = slots;
|
2002-01-03 18:22:59 +08:00
|
|
|
|
2005-01-25 08:06:29 +08:00
|
|
|
h->table = (struct curl_llist **) malloc(slots * sizeof(struct curl_llist *));
|
2003-08-14 23:05:13 +08:00
|
|
|
if(h->table) {
|
|
|
|
for (i = 0; i < slots; ++i) {
|
2003-09-05 20:44:35 +08:00
|
|
|
h->table[i] = Curl_llist_alloc((curl_llist_dtor) hash_element_dtor);
|
2003-08-14 23:05:13 +08:00
|
|
|
if(!h->table[i]) {
|
|
|
|
while(i--)
|
|
|
|
Curl_llist_destroy(h->table[i], NULL);
|
|
|
|
free(h->table);
|
|
|
|
return 1; /* failure */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0; /* fine */
|
2002-01-03 18:22:59 +08:00
|
|
|
}
|
2003-08-14 23:05:13 +08:00
|
|
|
else
|
|
|
|
return 1; /* failure */
|
2002-01-03 18:22:59 +08:00
|
|
|
}
|
|
|
|
|
2005-01-25 08:06:29 +08:00
|
|
|
struct curl_hash *
|
2007-06-27 05:09:28 +08:00
|
|
|
Curl_hash_alloc(int slots,
|
|
|
|
hash_function hfunc,
|
|
|
|
comp_function comparator,
|
|
|
|
curl_hash_dtor dtor)
|
2002-01-03 18:22:59 +08:00
|
|
|
{
|
2005-01-25 08:06:29 +08:00
|
|
|
struct curl_hash *h;
|
2002-01-03 18:22:59 +08:00
|
|
|
|
2007-06-27 05:09:28 +08:00
|
|
|
if (!slots || !hfunc || !comparator ||!dtor) {
|
|
|
|
return NULL; /* failure */
|
|
|
|
}
|
|
|
|
|
2005-01-25 08:06:29 +08:00
|
|
|
h = (struct curl_hash *) malloc(sizeof(struct curl_hash));
|
2003-08-14 23:05:13 +08:00
|
|
|
if (h) {
|
2007-06-27 05:09:28 +08:00
|
|
|
if(Curl_hash_init(h, slots, hfunc, comparator, dtor)) {
|
2003-08-14 23:05:13 +08:00
|
|
|
/* failure */
|
|
|
|
free(h);
|
|
|
|
h = NULL;
|
|
|
|
}
|
|
|
|
}
|
2002-01-03 18:22:59 +08:00
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-01-25 08:06:29 +08:00
|
|
|
static struct curl_hash_element *
|
2007-08-29 13:36:53 +08:00
|
|
|
mk_hash_element(const void *key, size_t key_len, const void *p)
|
2002-04-13 07:40:19 +08:00
|
|
|
{
|
2005-01-25 08:06:29 +08:00
|
|
|
struct curl_hash_element *he =
|
|
|
|
(struct curl_hash_element *) malloc(sizeof(struct curl_hash_element));
|
2003-09-05 20:44:35 +08:00
|
|
|
|
|
|
|
if(he) {
|
2007-06-27 05:09:28 +08:00
|
|
|
void *dup = malloc(key_len);
|
2004-05-10 16:57:18 +08:00
|
|
|
if(dup) {
|
2006-04-10 23:00:53 +08:00
|
|
|
/* copy the key */
|
|
|
|
memcpy(dup, key, key_len);
|
|
|
|
|
2004-05-10 16:57:18 +08:00
|
|
|
he->key = dup;
|
|
|
|
he->key_len = key_len;
|
|
|
|
he->ptr = (void *) p;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* failed to duplicate the key, free memory and fail */
|
|
|
|
free(he);
|
|
|
|
he = NULL;
|
|
|
|
}
|
2003-09-05 20:44:35 +08:00
|
|
|
}
|
|
|
|
return he;
|
2002-01-03 18:22:59 +08:00
|
|
|
}
|
2002-04-13 07:40:19 +08:00
|
|
|
|
2007-06-27 05:09:28 +08:00
|
|
|
#define FETCH_LIST(x,y,z) x->table[x->hash_func(y, z, x->slots)]
|
2002-04-13 07:40:19 +08:00
|
|
|
|
2003-09-15 05:17:54 +08:00
|
|
|
/* Return the data in the hash. If there already was a match in the hash,
|
|
|
|
that data is returned. */
|
|
|
|
void *
|
2007-06-27 05:09:28 +08:00
|
|
|
Curl_hash_add(struct curl_hash *h, void *key, size_t key_len, void *p)
|
2002-01-03 18:22:59 +08:00
|
|
|
{
|
2005-01-25 08:06:29 +08:00
|
|
|
struct curl_hash_element *he;
|
|
|
|
struct curl_llist_element *le;
|
2007-06-27 05:09:28 +08:00
|
|
|
struct curl_llist *l = FETCH_LIST (h, key, key_len);
|
2002-04-13 07:40:19 +08:00
|
|
|
|
2003-09-15 05:17:54 +08:00
|
|
|
for (le = l->head; le; le = le->next) {
|
2005-01-25 08:06:29 +08:00
|
|
|
he = (struct curl_hash_element *) le->ptr;
|
2007-06-27 05:09:28 +08:00
|
|
|
if (h->comp_func(he->key, he->key_len, key, key_len)) {
|
2003-09-15 05:17:54 +08:00
|
|
|
h->dtor(p); /* remove the NEW entry */
|
|
|
|
return he->ptr; /* return the EXISTING entry */
|
2002-01-03 18:22:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-05 20:44:35 +08:00
|
|
|
he = mk_hash_element(key, key_len, p);
|
2003-12-15 23:21:13 +08:00
|
|
|
if (he) {
|
|
|
|
if(Curl_llist_insert_next(l, l->tail, he)) {
|
|
|
|
++h->size;
|
|
|
|
return p; /* return the new entry */
|
|
|
|
}
|
2004-05-10 17:17:50 +08:00
|
|
|
/*
|
|
|
|
* Couldn't insert it, destroy the 'he' element and the key again. We
|
|
|
|
* don't call hash_element_dtor() since that would also call the
|
|
|
|
* "destructor" for the actual data 'p'. When we fail, we shall not touch
|
|
|
|
* that data.
|
|
|
|
*/
|
|
|
|
free(he->key);
|
|
|
|
free(he);
|
2002-01-03 18:22:59 +08:00
|
|
|
}
|
2004-05-04 21:40:30 +08:00
|
|
|
|
2003-09-15 05:17:54 +08:00
|
|
|
return NULL; /* failure */
|
2002-01-03 18:22:59 +08:00
|
|
|
}
|
|
|
|
|
2006-04-10 23:00:53 +08:00
|
|
|
/* remove the identified hash entry, returns non-zero on failure */
|
2007-06-27 05:09:28 +08:00
|
|
|
int Curl_hash_delete(struct curl_hash *h, void *key, size_t key_len)
|
2006-04-10 23:00:53 +08:00
|
|
|
{
|
|
|
|
struct curl_llist_element *le;
|
|
|
|
struct curl_hash_element *he;
|
|
|
|
struct curl_llist *l = FETCH_LIST(h, key, key_len);
|
|
|
|
|
|
|
|
for (le = l->head; le; le = le->next) {
|
|
|
|
he = le->ptr;
|
2007-06-27 05:09:28 +08:00
|
|
|
if (h->comp_func(he->key, he->key_len, key, key_len)) {
|
2006-04-10 23:00:53 +08:00
|
|
|
Curl_llist_remove(l, le, (void *) h);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-11-05 18:51:41 +08:00
|
|
|
void *
|
2007-06-27 05:09:28 +08:00
|
|
|
Curl_hash_pick(struct curl_hash *h, void *key, size_t key_len)
|
2002-01-03 18:22:59 +08:00
|
|
|
{
|
2005-01-25 08:06:29 +08:00
|
|
|
struct curl_llist_element *le;
|
|
|
|
struct curl_hash_element *he;
|
|
|
|
struct curl_llist *l = FETCH_LIST(h, key, key_len);
|
2002-04-13 07:40:19 +08:00
|
|
|
|
2006-04-10 23:00:53 +08:00
|
|
|
for (le = l->head; le; le = le->next) {
|
2003-09-05 20:44:35 +08:00
|
|
|
he = le->ptr;
|
2007-06-27 05:09:28 +08:00
|
|
|
if (h->comp_func(he->key, he->key_len, key, key_len)) {
|
2002-11-05 18:51:41 +08:00
|
|
|
return he->ptr;
|
2002-01-03 18:22:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-05 18:51:41 +08:00
|
|
|
return NULL;
|
2002-01-03 18:22:59 +08:00
|
|
|
}
|
|
|
|
|
2003-09-05 20:44:35 +08:00
|
|
|
#if defined(CURLDEBUG) && defined(AGGRESIVE_TEST)
|
2004-06-24 15:43:48 +08:00
|
|
|
void
|
2002-04-27 21:06:40 +08:00
|
|
|
Curl_hash_apply(curl_hash *h, void *user,
|
2002-11-05 18:51:41 +08:00
|
|
|
void (*cb)(void *user, void *ptr))
|
2002-01-03 18:22:59 +08:00
|
|
|
{
|
2005-01-25 08:06:29 +08:00
|
|
|
struct curl_llist_element *le;
|
2002-01-03 18:22:59 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < h->slots; ++i) {
|
2003-09-05 20:44:35 +08:00
|
|
|
for (le = (h->table[i])->head;
|
|
|
|
le;
|
|
|
|
le = le->next) {
|
|
|
|
curl_hash_element *el = le->ptr;
|
2002-11-05 18:51:41 +08:00
|
|
|
cb(user, el->ptr);
|
2002-01-03 18:22:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-09-05 20:44:35 +08:00
|
|
|
#endif
|
2002-01-03 18:22:59 +08:00
|
|
|
|
|
|
|
void
|
2005-01-25 08:06:29 +08:00
|
|
|
Curl_hash_clean(struct curl_hash *h)
|
2002-01-03 18:22:59 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < h->slots; ++i) {
|
2002-04-27 21:06:40 +08:00
|
|
|
Curl_llist_destroy(h->table[i], (void *) h);
|
2002-01-03 18:22:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
free(h->table);
|
|
|
|
}
|
|
|
|
|
2002-04-18 04:13:55 +08:00
|
|
|
void
|
2005-01-25 08:06:29 +08:00
|
|
|
Curl_hash_clean_with_criterium(struct curl_hash *h, void *user,
|
2002-04-27 21:06:40 +08:00
|
|
|
int (*comp)(void *, void *))
|
2002-04-18 04:13:55 +08:00
|
|
|
{
|
2005-01-25 08:06:29 +08:00
|
|
|
struct curl_llist_element *le;
|
|
|
|
struct curl_llist_element *lnext;
|
|
|
|
struct curl_llist *list;
|
2002-04-18 04:13:55 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < h->slots; ++i) {
|
2003-09-05 20:44:35 +08:00
|
|
|
list = h->table[i];
|
|
|
|
le = list->head; /* get first list entry */
|
|
|
|
while(le) {
|
2005-01-25 08:06:29 +08:00
|
|
|
struct curl_hash_element *he = le->ptr;
|
2003-09-05 20:44:35 +08:00
|
|
|
lnext = le->next;
|
|
|
|
/* ask the callback function if we shall remove this entry or not */
|
|
|
|
if (comp(user, he->ptr)) {
|
|
|
|
Curl_llist_remove(list, le, (void *) h);
|
|
|
|
--h->size; /* one less entry in the hash now */
|
2002-04-18 04:13:55 +08:00
|
|
|
}
|
2003-09-05 20:44:35 +08:00
|
|
|
le = lnext;
|
|
|
|
}
|
2002-04-18 04:13:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-24 15:43:48 +08:00
|
|
|
void
|
2005-01-25 08:06:29 +08:00
|
|
|
Curl_hash_destroy(struct curl_hash *h)
|
2002-01-03 18:22:59 +08:00
|
|
|
{
|
2002-04-13 07:40:19 +08:00
|
|
|
if (!h)
|
2002-01-08 04:52:32 +08:00
|
|
|
return;
|
|
|
|
|
2002-04-27 21:06:40 +08:00
|
|
|
Curl_hash_clean(h);
|
2002-01-03 18:22:59 +08:00
|
|
|
free(h);
|
|
|
|
}
|
2003-01-29 18:12:06 +08:00
|
|
|
|
2007-06-27 05:09:28 +08:00
|
|
|
size_t Curl_hash_str(void* key, size_t key_length, size_t slots_num)
|
|
|
|
{
|
2007-08-29 13:36:53 +08:00
|
|
|
const char* key_str = (const char *) key;
|
|
|
|
const char *end = key_str + key_length;
|
2007-06-27 05:09:28 +08:00
|
|
|
unsigned long h = 5381;
|
|
|
|
|
|
|
|
while (key_str < end) {
|
|
|
|
h += h << 5;
|
|
|
|
h ^= (unsigned long) *key_str++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (h % slots_num);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Curl_str_key_compare(void*k1, size_t key1_len, void*k2, size_t key2_len)
|
|
|
|
{
|
|
|
|
char *key1 = (char *)k1;
|
|
|
|
char *key2 = (char *)k2;
|
|
|
|
|
|
|
|
if (key1_len == key2_len &&
|
|
|
|
*key1 == *key2 &&
|
|
|
|
memcmp(key1, key2, key1_len) == 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-11 06:12:24 +08:00
|
|
|
#if 0 /* useful function for debugging hashes and their contents */
|
|
|
|
void Curl_hash_print(struct curl_hash *h,
|
|
|
|
void (*func)(void *))
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct curl_llist_element *le;
|
|
|
|
struct curl_llist *list;
|
|
|
|
struct curl_hash_element *he;
|
|
|
|
if (!h)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fprintf(stderr, "=Hash dump=\n");
|
|
|
|
|
|
|
|
for (i = 0; i < h->slots; i++) {
|
|
|
|
list = h->table[i];
|
|
|
|
le = list->head; /* get first list entry */
|
|
|
|
if(le) {
|
|
|
|
fprintf(stderr, "index %d:", i);
|
|
|
|
while(le) {
|
|
|
|
he = le->ptr;
|
|
|
|
if(func)
|
|
|
|
func(he->ptr);
|
|
|
|
else
|
|
|
|
fprintf(stderr, " [%p]", he->ptr);
|
|
|
|
le = le->next;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|