2002-09-03 07:52:59 -04:00
|
|
|
/***************************************************************************
|
2004-06-24 03:43:48 -04:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
2002-01-03 05:22:59 -05:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2011-04-20 09:17:42 -04:00
|
|
|
* Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2002-01-03 05:22:59 -05:00
|
|
|
*
|
2002-09-03 07:52:59 -04: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 03:43:48 -04:00
|
|
|
*
|
2002-01-03 05:22:59 -05: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 07:52:59 -04:00
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
2002-01-03 05:22:59 -05:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
2002-09-03 07:52:59 -04:00
|
|
|
***************************************************************************/
|
2002-01-03 05:22:59 -05:00
|
|
|
|
|
|
|
#include "setup.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
2002-02-17 09:55:35 -05:00
|
|
|
#include <stdlib.h>
|
2002-11-05 05:51:41 -05:00
|
|
|
|
2002-01-03 05:22:59 -05:00
|
|
|
#include "hash.h"
|
|
|
|
#include "llist.h"
|
|
|
|
|
2008-10-25 12:15:21 -04:00
|
|
|
#define _MPRINTF_REPLACE /* use our functions only */
|
|
|
|
#include <curl/mprintf.h>
|
|
|
|
|
2009-04-21 07:46:16 -04:00
|
|
|
#include "curl_memory.h"
|
2008-10-25 12:15:21 -04:00
|
|
|
/* The last #include file should be: */
|
2002-01-03 05:22:59 -05:00
|
|
|
#include "memdebug.h"
|
|
|
|
|
2004-06-24 03:43:48 -04:00
|
|
|
static void
|
2003-09-05 08:44:35 -04:00
|
|
|
hash_element_dtor(void *user, void *element)
|
2002-01-03 05:22:59 -05:00
|
|
|
{
|
2005-01-24 19:06:29 -05:00
|
|
|
struct curl_hash *h = (struct curl_hash *) user;
|
|
|
|
struct curl_hash_element *e = (struct curl_hash_element *) element;
|
2002-04-12 19:40:19 -04:00
|
|
|
|
2007-11-07 04:21:35 -05:00
|
|
|
if(e->key)
|
2002-04-12 19:40:19 -04:00
|
|
|
free(e->key);
|
|
|
|
|
2008-10-27 01:29:17 -04:00
|
|
|
if(e->ptr)
|
|
|
|
h->dtor(e->ptr);
|
2002-01-03 05:22:59 -05:00
|
|
|
|
|
|
|
free(e);
|
|
|
|
}
|
|
|
|
|
2003-08-14 11:05:13 -04:00
|
|
|
/* return 1 on error, 0 is fine */
|
|
|
|
int
|
2007-06-26 17:09:28 -04:00
|
|
|
Curl_hash_init(struct curl_hash *h,
|
|
|
|
int slots,
|
|
|
|
hash_function hfunc,
|
|
|
|
comp_function comparator,
|
|
|
|
curl_hash_dtor dtor)
|
2002-01-03 05:22:59 -05:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2007-11-07 04:21:35 -05:00
|
|
|
if(!slots || !hfunc || !comparator ||!dtor) {
|
2007-06-26 17:09:28 -04:00
|
|
|
return 1; /* failure */
|
|
|
|
}
|
|
|
|
|
|
|
|
h->hash_func = hfunc;
|
|
|
|
h->comp_func = comparator;
|
2002-01-03 05:22:59 -05:00
|
|
|
h->dtor = dtor;
|
|
|
|
h->size = 0;
|
2004-06-24 03:43:48 -04:00
|
|
|
h->slots = slots;
|
2002-01-03 05:22:59 -05:00
|
|
|
|
2008-09-06 01:29:05 -04:00
|
|
|
h->table = malloc(slots * sizeof(struct curl_llist *));
|
2003-08-14 11:05:13 -04:00
|
|
|
if(h->table) {
|
2011-04-20 09:17:42 -04:00
|
|
|
for(i = 0; i < slots; ++i) {
|
2003-09-05 08:44:35 -04:00
|
|
|
h->table[i] = Curl_llist_alloc((curl_llist_dtor) hash_element_dtor);
|
2003-08-14 11:05:13 -04: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 05:22:59 -05:00
|
|
|
}
|
2003-08-14 11:05:13 -04:00
|
|
|
else
|
|
|
|
return 1; /* failure */
|
2002-01-03 05:22:59 -05:00
|
|
|
}
|
|
|
|
|
2005-01-24 19:06:29 -05:00
|
|
|
struct curl_hash *
|
2007-06-26 17:09:28 -04:00
|
|
|
Curl_hash_alloc(int slots,
|
|
|
|
hash_function hfunc,
|
|
|
|
comp_function comparator,
|
|
|
|
curl_hash_dtor dtor)
|
2002-01-03 05:22:59 -05:00
|
|
|
{
|
2005-01-24 19:06:29 -05:00
|
|
|
struct curl_hash *h;
|
2002-01-03 05:22:59 -05:00
|
|
|
|
2007-11-07 04:21:35 -05:00
|
|
|
if(!slots || !hfunc || !comparator ||!dtor) {
|
2007-06-26 17:09:28 -04:00
|
|
|
return NULL; /* failure */
|
|
|
|
}
|
|
|
|
|
2008-09-06 01:29:05 -04:00
|
|
|
h = malloc(sizeof(struct curl_hash));
|
2007-11-07 04:21:35 -05:00
|
|
|
if(h) {
|
2007-06-26 17:09:28 -04:00
|
|
|
if(Curl_hash_init(h, slots, hfunc, comparator, dtor)) {
|
2003-08-14 11:05:13 -04:00
|
|
|
/* failure */
|
|
|
|
free(h);
|
|
|
|
h = NULL;
|
|
|
|
}
|
|
|
|
}
|
2002-01-03 05:22:59 -05:00
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-01-24 19:06:29 -05:00
|
|
|
static struct curl_hash_element *
|
2007-08-29 01:36:53 -04:00
|
|
|
mk_hash_element(const void *key, size_t key_len, const void *p)
|
2002-04-12 19:40:19 -04:00
|
|
|
{
|
2008-09-06 01:29:05 -04:00
|
|
|
struct curl_hash_element *he = malloc(sizeof(struct curl_hash_element));
|
2003-09-05 08:44:35 -04:00
|
|
|
|
|
|
|
if(he) {
|
2007-09-27 14:12:03 -04:00
|
|
|
void *dupkey = malloc(key_len);
|
|
|
|
if(dupkey) {
|
2006-04-10 11:00:53 -04:00
|
|
|
/* copy the key */
|
2007-09-27 14:12:03 -04:00
|
|
|
memcpy(dupkey, key, key_len);
|
2006-04-10 11:00:53 -04:00
|
|
|
|
2007-09-27 14:12:03 -04:00
|
|
|
he->key = dupkey;
|
2004-05-10 04:57:18 -04:00
|
|
|
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 08:44:35 -04:00
|
|
|
}
|
|
|
|
return he;
|
2002-01-03 05:22:59 -05:00
|
|
|
}
|
2002-04-12 19:40:19 -04:00
|
|
|
|
2007-06-26 17:09:28 -04:00
|
|
|
#define FETCH_LIST(x,y,z) x->table[x->hash_func(y, z, x->slots)]
|
2002-04-12 19:40:19 -04:00
|
|
|
|
2009-11-11 04:31:37 -05:00
|
|
|
/* Insert the data in the hash. If there already was a match in the hash,
|
|
|
|
that data is replaced. */
|
2003-09-14 17:17:54 -04:00
|
|
|
void *
|
2007-06-26 17:09:28 -04:00
|
|
|
Curl_hash_add(struct curl_hash *h, void *key, size_t key_len, void *p)
|
2002-01-03 05:22:59 -05:00
|
|
|
{
|
2005-01-24 19:06:29 -05:00
|
|
|
struct curl_hash_element *he;
|
|
|
|
struct curl_llist_element *le;
|
2007-06-26 17:09:28 -04:00
|
|
|
struct curl_llist *l = FETCH_LIST (h, key, key_len);
|
2002-04-12 19:40:19 -04:00
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
for(le = l->head; le; le = le->next) {
|
2005-01-24 19:06:29 -05:00
|
|
|
he = (struct curl_hash_element *) le->ptr;
|
2007-11-07 04:21:35 -05:00
|
|
|
if(h->comp_func(he->key, he->key_len, key, key_len)) {
|
2009-11-11 04:31:37 -05:00
|
|
|
Curl_llist_remove(l, le, (void *)h);
|
|
|
|
--h->size;
|
|
|
|
break;
|
2002-01-03 05:22:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-05 08:44:35 -04:00
|
|
|
he = mk_hash_element(key, key_len, p);
|
2007-11-07 04:21:35 -05:00
|
|
|
if(he) {
|
2003-12-15 10:21:13 -05:00
|
|
|
if(Curl_llist_insert_next(l, l->tail, he)) {
|
|
|
|
++h->size;
|
|
|
|
return p; /* return the new entry */
|
|
|
|
}
|
2004-05-10 05:17:50 -04: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 05:22:59 -05:00
|
|
|
}
|
2004-05-04 09:40:30 -04:00
|
|
|
|
2003-09-14 17:17:54 -04:00
|
|
|
return NULL; /* failure */
|
2002-01-03 05:22:59 -05:00
|
|
|
}
|
|
|
|
|
2006-04-10 11:00:53 -04:00
|
|
|
/* remove the identified hash entry, returns non-zero on failure */
|
2007-06-26 17:09:28 -04:00
|
|
|
int Curl_hash_delete(struct curl_hash *h, void *key, size_t key_len)
|
2006-04-10 11:00:53 -04:00
|
|
|
{
|
|
|
|
struct curl_llist_element *le;
|
|
|
|
struct curl_hash_element *he;
|
|
|
|
struct curl_llist *l = FETCH_LIST(h, key, key_len);
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
for(le = l->head; le; le = le->next) {
|
2006-04-10 11:00:53 -04:00
|
|
|
he = le->ptr;
|
2007-11-07 04:21:35 -05:00
|
|
|
if(h->comp_func(he->key, he->key_len, key, key_len)) {
|
2006-04-10 11:00:53 -04:00
|
|
|
Curl_llist_remove(l, le, (void *) h);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-11-05 05:51:41 -05:00
|
|
|
void *
|
2007-06-26 17:09:28 -04:00
|
|
|
Curl_hash_pick(struct curl_hash *h, void *key, size_t key_len)
|
2002-01-03 05:22:59 -05:00
|
|
|
{
|
2005-01-24 19:06:29 -05:00
|
|
|
struct curl_llist_element *le;
|
|
|
|
struct curl_hash_element *he;
|
|
|
|
struct curl_llist *l = FETCH_LIST(h, key, key_len);
|
2002-04-12 19:40:19 -04:00
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
for(le = l->head; le; le = le->next) {
|
2003-09-05 08:44:35 -04:00
|
|
|
he = le->ptr;
|
2007-11-07 04:21:35 -05:00
|
|
|
if(h->comp_func(he->key, he->key_len, key, key_len)) {
|
2002-11-05 05:51:41 -05:00
|
|
|
return he->ptr;
|
2002-01-03 05:22:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-05 05:51:41 -05:00
|
|
|
return NULL;
|
2002-01-03 05:22:59 -05:00
|
|
|
}
|
|
|
|
|
2009-06-09 22:49:42 -04:00
|
|
|
#if defined(DEBUGBUILD) && defined(AGGRESIVE_TEST)
|
2004-06-24 03:43:48 -04:00
|
|
|
void
|
2002-04-27 09:06:40 -04:00
|
|
|
Curl_hash_apply(curl_hash *h, void *user,
|
2002-11-05 05:51:41 -05:00
|
|
|
void (*cb)(void *user, void *ptr))
|
2002-01-03 05:22:59 -05:00
|
|
|
{
|
2005-01-24 19:06:29 -05:00
|
|
|
struct curl_llist_element *le;
|
2002-01-03 05:22:59 -05:00
|
|
|
int i;
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
for(i = 0; i < h->slots; ++i) {
|
|
|
|
for(le = (h->table[i])->head;
|
|
|
|
le;
|
|
|
|
le = le->next) {
|
2003-09-05 08:44:35 -04:00
|
|
|
curl_hash_element *el = le->ptr;
|
2002-11-05 05:51:41 -05:00
|
|
|
cb(user, el->ptr);
|
2002-01-03 05:22:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-09-05 08:44:35 -04:00
|
|
|
#endif
|
2002-01-03 05:22:59 -05:00
|
|
|
|
|
|
|
void
|
2005-01-24 19:06:29 -05:00
|
|
|
Curl_hash_clean(struct curl_hash *h)
|
2002-01-03 05:22:59 -05:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
for(i = 0; i < h->slots; ++i) {
|
2002-04-27 09:06:40 -04:00
|
|
|
Curl_llist_destroy(h->table[i], (void *) h);
|
2008-10-19 16:17:16 -04:00
|
|
|
h->table[i] = NULL;
|
2002-01-03 05:22:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
free(h->table);
|
|
|
|
}
|
|
|
|
|
2002-04-17 16:13:55 -04:00
|
|
|
void
|
2005-01-24 19:06:29 -05:00
|
|
|
Curl_hash_clean_with_criterium(struct curl_hash *h, void *user,
|
2002-04-27 09:06:40 -04:00
|
|
|
int (*comp)(void *, void *))
|
2002-04-17 16:13:55 -04:00
|
|
|
{
|
2005-01-24 19:06:29 -05:00
|
|
|
struct curl_llist_element *le;
|
|
|
|
struct curl_llist_element *lnext;
|
|
|
|
struct curl_llist *list;
|
2002-04-17 16:13:55 -04:00
|
|
|
int i;
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
for(i = 0; i < h->slots; ++i) {
|
2003-09-05 08:44:35 -04:00
|
|
|
list = h->table[i];
|
|
|
|
le = list->head; /* get first list entry */
|
|
|
|
while(le) {
|
2005-01-24 19:06:29 -05:00
|
|
|
struct curl_hash_element *he = le->ptr;
|
2003-09-05 08:44:35 -04:00
|
|
|
lnext = le->next;
|
|
|
|
/* ask the callback function if we shall remove this entry or not */
|
2007-11-07 04:21:35 -05:00
|
|
|
if(comp(user, he->ptr)) {
|
2003-09-05 08:44:35 -04:00
|
|
|
Curl_llist_remove(list, le, (void *) h);
|
|
|
|
--h->size; /* one less entry in the hash now */
|
2002-04-17 16:13:55 -04:00
|
|
|
}
|
2003-09-05 08:44:35 -04:00
|
|
|
le = lnext;
|
|
|
|
}
|
2002-04-17 16:13:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-24 03:43:48 -04:00
|
|
|
void
|
2005-01-24 19:06:29 -05:00
|
|
|
Curl_hash_destroy(struct curl_hash *h)
|
2002-01-03 05:22:59 -05:00
|
|
|
{
|
2007-11-07 04:21:35 -05:00
|
|
|
if(!h)
|
2002-01-07 15:52:32 -05:00
|
|
|
return;
|
|
|
|
|
2002-04-27 09:06:40 -04:00
|
|
|
Curl_hash_clean(h);
|
2008-10-19 16:17:16 -04:00
|
|
|
|
2002-01-03 05:22:59 -05:00
|
|
|
free(h);
|
|
|
|
}
|
2003-01-29 05:12:06 -05:00
|
|
|
|
2007-06-26 17:09:28 -04:00
|
|
|
size_t Curl_hash_str(void* key, size_t key_length, size_t slots_num)
|
|
|
|
{
|
2007-08-29 01:36:53 -04:00
|
|
|
const char* key_str = (const char *) key;
|
|
|
|
const char *end = key_str + key_length;
|
2007-06-26 17:09:28 -04:00
|
|
|
unsigned long h = 5381;
|
|
|
|
|
2007-11-07 04:21:35 -05:00
|
|
|
while(key_str < end) {
|
2007-06-26 17:09:28 -04:00
|
|
|
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;
|
|
|
|
|
2007-11-07 04:21:35 -05:00
|
|
|
if(key1_len == key2_len &&
|
2007-06-26 17:09:28 -04:00
|
|
|
*key1 == *key2 &&
|
|
|
|
memcmp(key1, key2, key1_len) == 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-10 18:12:24 -04: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;
|
2007-11-07 04:21:35 -05:00
|
|
|
if(!h)
|
2006-09-10 18:12:24 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
fprintf(stderr, "=Hash dump=\n");
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
for(i = 0; i < h->slots; i++) {
|
2006-09-10 18:12:24 -04:00
|
|
|
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
|