2002-09-03 13:52:59 +02:00
|
|
|
/***************************************************************************
|
2002-01-03 11:22:59 +01:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2004-01-07 10:19:33 +01:00
|
|
|
* Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2002-01-03 11:22:59 +01:00
|
|
|
*
|
2002-09-03 13:52:59 +02: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.
|
|
|
|
*
|
2002-01-03 11:22:59 +01: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 13:52:59 +02:00
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
2002-01-03 11:22:59 +01:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
* $Id$
|
2002-09-03 13:52:59 +02:00
|
|
|
***************************************************************************/
|
2002-01-03 11:22:59 +01:00
|
|
|
|
|
|
|
#include "setup.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
2002-02-17 15:55:35 +01:00
|
|
|
#include <stdlib.h>
|
2002-11-05 11:51:41 +01:00
|
|
|
|
2002-01-03 11:22:59 +01:00
|
|
|
#include "hash.h"
|
|
|
|
#include "llist.h"
|
2004-05-11 13:30:23 +02:00
|
|
|
#include "memory.h"
|
2002-01-03 11:22:59 +01:00
|
|
|
|
|
|
|
/* this must be the last include file */
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
2002-04-13 01:40:19 +02:00
|
|
|
static unsigned long
|
2003-09-05 14:44:35 +02:00
|
|
|
hash_str(const char *key, size_t key_length)
|
2002-01-03 11:22:59 +01:00
|
|
|
{
|
2002-04-13 01:40:19 +02:00
|
|
|
char *end = (char *) key + key_length;
|
|
|
|
unsigned long h = 5381;
|
|
|
|
|
|
|
|
while (key < end) {
|
|
|
|
h += h << 5;
|
|
|
|
h ^= (unsigned long) *key++;
|
2002-01-03 11:22:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2003-09-05 14:44:35 +02:00
|
|
|
hash_element_dtor(void *user, void *element)
|
2002-01-03 11:22:59 +01:00
|
|
|
{
|
2002-04-13 01:40:19 +02:00
|
|
|
curl_hash *h = (curl_hash *) user;
|
|
|
|
curl_hash_element *e = (curl_hash_element *) element;
|
|
|
|
|
|
|
|
if (e->key) {
|
|
|
|
free(e->key);
|
2002-01-03 11:22:59 +01:00
|
|
|
}
|
2002-04-13 01:40:19 +02:00
|
|
|
|
2002-01-03 11:22:59 +01:00
|
|
|
h->dtor(e->ptr);
|
|
|
|
|
|
|
|
free(e);
|
|
|
|
}
|
|
|
|
|
2003-08-14 17:05:13 +02:00
|
|
|
/* return 1 on error, 0 is fine */
|
|
|
|
int
|
|
|
|
Curl_hash_init(curl_hash *h, int slots, curl_hash_dtor dtor)
|
2002-01-03 11:22:59 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
h->dtor = dtor;
|
|
|
|
h->size = 0;
|
|
|
|
h->slots = slots;
|
|
|
|
|
|
|
|
h->table = (curl_llist **) malloc(slots * sizeof(curl_llist *));
|
2003-08-14 17:05:13 +02:00
|
|
|
if(h->table) {
|
|
|
|
for (i = 0; i < slots; ++i) {
|
2003-09-05 14:44:35 +02:00
|
|
|
h->table[i] = Curl_llist_alloc((curl_llist_dtor) hash_element_dtor);
|
2003-08-14 17:05:13 +02: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 11:22:59 +01:00
|
|
|
}
|
2003-08-14 17:05:13 +02:00
|
|
|
else
|
|
|
|
return 1; /* failure */
|
2002-01-03 11:22:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
curl_hash *
|
2003-08-14 17:05:13 +02:00
|
|
|
Curl_hash_alloc(int slots, curl_hash_dtor dtor)
|
2002-01-03 11:22:59 +01:00
|
|
|
{
|
|
|
|
curl_hash *h;
|
|
|
|
|
2002-04-13 01:40:19 +02:00
|
|
|
h = (curl_hash *) malloc(sizeof(curl_hash));
|
2003-08-14 17:05:13 +02:00
|
|
|
if (h) {
|
|
|
|
if(Curl_hash_init(h, slots, dtor)) {
|
|
|
|
/* failure */
|
|
|
|
free(h);
|
|
|
|
h = NULL;
|
|
|
|
}
|
|
|
|
}
|
2002-01-03 11:22:59 +01:00
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-09-05 14:44:35 +02:00
|
|
|
hash_key_compare(char *key1, size_t key1_len, char *key2, size_t key2_len)
|
2002-01-03 11:22:59 +01:00
|
|
|
{
|
2002-04-13 01:40:19 +02:00
|
|
|
if (key1_len == key2_len &&
|
|
|
|
*key1 == *key2 &&
|
|
|
|
memcmp(key1, key2, key1_len) == 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
2002-01-03 11:22:59 +01:00
|
|
|
|
2002-04-13 01:40:19 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2002-01-03 11:22:59 +01:00
|
|
|
|
2003-09-05 14:44:35 +02:00
|
|
|
static curl_hash_element *
|
|
|
|
mk_hash_element(char *key, size_t key_len, const void *p)
|
2002-04-13 01:40:19 +02:00
|
|
|
{
|
2003-09-05 14:44:35 +02:00
|
|
|
curl_hash_element *he =
|
|
|
|
(curl_hash_element *) malloc(sizeof(curl_hash_element));
|
|
|
|
|
|
|
|
if(he) {
|
2004-05-10 10:57:18 +02:00
|
|
|
char *dup = strdup(key);
|
|
|
|
if(dup) {
|
|
|
|
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 14:44:35 +02:00
|
|
|
}
|
|
|
|
return he;
|
2002-01-03 11:22:59 +01:00
|
|
|
}
|
2002-04-13 01:40:19 +02:00
|
|
|
|
2003-09-05 14:44:35 +02:00
|
|
|
#define find_slot(__h, __k, __k_len) (hash_str(__k, __k_len) % (__h)->slots)
|
2002-04-13 01:40:19 +02:00
|
|
|
|
2003-09-05 14:44:35 +02:00
|
|
|
#define FETCH_LIST(x,y,z) x->table[find_slot(x, y, z)]
|
2002-04-13 01:40:19 +02:00
|
|
|
|
2003-09-14 23:17:54 +02:00
|
|
|
/* Return the data in the hash. If there already was a match in the hash,
|
|
|
|
that data is returned. */
|
|
|
|
void *
|
|
|
|
Curl_hash_add(curl_hash *h, char *key, size_t key_len, void *p)
|
2002-01-03 11:22:59 +01:00
|
|
|
{
|
2002-04-13 01:40:19 +02:00
|
|
|
curl_hash_element *he;
|
2002-01-03 11:22:59 +01:00
|
|
|
curl_llist_element *le;
|
2003-09-05 14:44:35 +02:00
|
|
|
curl_llist *l = FETCH_LIST(h, key, key_len);
|
2002-04-13 01:40:19 +02:00
|
|
|
|
2003-09-14 23:17:54 +02:00
|
|
|
for (le = l->head; le; le = le->next) {
|
2003-09-05 14:44:35 +02:00
|
|
|
he = (curl_hash_element *) le->ptr;
|
|
|
|
if (hash_key_compare(he->key, he->key_len, key, key_len)) {
|
2003-09-14 23:17:54 +02:00
|
|
|
h->dtor(p); /* remove the NEW entry */
|
|
|
|
return he->ptr; /* return the EXISTING entry */
|
2002-01-03 11:22:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-05 14:44:35 +02:00
|
|
|
he = mk_hash_element(key, key_len, p);
|
2003-12-15 16:21:13 +01:00
|
|
|
if (he) {
|
|
|
|
if(Curl_llist_insert_next(l, l->tail, he)) {
|
|
|
|
++h->size;
|
|
|
|
return p; /* return the new entry */
|
|
|
|
}
|
2004-05-10 11:17:50 +02: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 11:22:59 +01:00
|
|
|
}
|
2004-05-04 15:40:30 +02:00
|
|
|
|
2003-09-14 23:17:54 +02:00
|
|
|
return NULL; /* failure */
|
2002-01-03 11:22:59 +01:00
|
|
|
}
|
|
|
|
|
2003-09-05 14:44:35 +02:00
|
|
|
#if 0
|
2002-01-03 11:22:59 +01:00
|
|
|
int
|
2002-04-27 15:06:40 +02:00
|
|
|
Curl_hash_delete(curl_hash *h, char *key, size_t key_len)
|
2002-01-03 11:22:59 +01:00
|
|
|
{
|
2002-04-13 01:40:19 +02:00
|
|
|
curl_hash_element *he;
|
2002-01-03 11:22:59 +01:00
|
|
|
curl_llist_element *le;
|
2003-09-05 14:44:35 +02:00
|
|
|
curl_llist *l = FETCH_LIST(h, key, key_len);
|
2002-01-03 11:22:59 +01:00
|
|
|
|
2003-09-05 14:44:35 +02:00
|
|
|
for (le = l->head;
|
|
|
|
le;
|
|
|
|
le = le->next) {
|
|
|
|
he = le->ptr;
|
|
|
|
if (hash_key_compare(he->key, he->key_len, key, key_len)) {
|
2002-04-27 15:06:40 +02:00
|
|
|
Curl_llist_remove(l, le, (void *) h);
|
2002-01-03 11:22:59 +01:00
|
|
|
--h->size;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2003-09-05 14:44:35 +02:00
|
|
|
#endif
|
2002-01-03 11:22:59 +01:00
|
|
|
|
2002-11-05 11:51:41 +01:00
|
|
|
void *
|
|
|
|
Curl_hash_pick(curl_hash *h, char *key, size_t key_len)
|
2002-01-03 11:22:59 +01:00
|
|
|
{
|
|
|
|
curl_llist_element *le;
|
2002-04-13 01:40:19 +02:00
|
|
|
curl_hash_element *he;
|
2003-09-05 14:44:35 +02:00
|
|
|
curl_llist *l = FETCH_LIST(h, key, key_len);
|
2002-04-13 01:40:19 +02:00
|
|
|
|
2003-09-05 14:44:35 +02:00
|
|
|
for (le = l->head;
|
|
|
|
le;
|
|
|
|
le = le->next) {
|
|
|
|
he = le->ptr;
|
|
|
|
if (hash_key_compare(he->key, he->key_len, key, key_len)) {
|
2002-11-05 11:51:41 +01:00
|
|
|
return he->ptr;
|
2002-01-03 11:22:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-05 11:51:41 +01:00
|
|
|
return NULL;
|
2002-01-03 11:22:59 +01:00
|
|
|
}
|
|
|
|
|
2003-09-05 14:44:35 +02:00
|
|
|
#if defined(CURLDEBUG) && defined(AGGRESIVE_TEST)
|
2002-01-03 11:22:59 +01:00
|
|
|
void
|
2002-04-27 15:06:40 +02:00
|
|
|
Curl_hash_apply(curl_hash *h, void *user,
|
2002-11-05 11:51:41 +01:00
|
|
|
void (*cb)(void *user, void *ptr))
|
2002-01-03 11:22:59 +01:00
|
|
|
{
|
|
|
|
curl_llist_element *le;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < h->slots; ++i) {
|
2003-09-05 14:44:35 +02:00
|
|
|
for (le = (h->table[i])->head;
|
|
|
|
le;
|
|
|
|
le = le->next) {
|
|
|
|
curl_hash_element *el = le->ptr;
|
2002-11-05 11:51:41 +01:00
|
|
|
cb(user, el->ptr);
|
2002-01-03 11:22:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-09-05 14:44:35 +02:00
|
|
|
#endif
|
2002-01-03 11:22:59 +01:00
|
|
|
|
|
|
|
void
|
2002-04-27 15:06:40 +02:00
|
|
|
Curl_hash_clean(curl_hash *h)
|
2002-01-03 11:22:59 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < h->slots; ++i) {
|
2002-04-27 15:06:40 +02:00
|
|
|
Curl_llist_destroy(h->table[i], (void *) h);
|
2002-01-03 11:22:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
free(h->table);
|
|
|
|
}
|
|
|
|
|
2002-04-17 22:13:55 +02:00
|
|
|
void
|
2002-04-27 15:06:40 +02:00
|
|
|
Curl_hash_clean_with_criterium(curl_hash *h, void *user,
|
|
|
|
int (*comp)(void *, void *))
|
2002-04-17 22:13:55 +02:00
|
|
|
{
|
|
|
|
curl_llist_element *le;
|
2002-04-27 15:06:40 +02:00
|
|
|
curl_llist_element *lnext;
|
2003-09-05 14:44:35 +02:00
|
|
|
curl_llist *list;
|
2002-04-17 22:13:55 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < h->slots; ++i) {
|
2003-09-05 14:44:35 +02:00
|
|
|
list = h->table[i];
|
|
|
|
le = list->head; /* get first list entry */
|
|
|
|
while(le) {
|
|
|
|
curl_hash_element *he = le->ptr;
|
|
|
|
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-17 22:13:55 +02:00
|
|
|
}
|
2003-09-05 14:44:35 +02:00
|
|
|
le = lnext;
|
|
|
|
}
|
2002-04-17 22:13:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-05 14:44:35 +02:00
|
|
|
#if 0
|
2003-01-29 11:12:06 +01:00
|
|
|
int
|
2002-04-27 15:06:40 +02:00
|
|
|
Curl_hash_count(curl_hash *h)
|
2002-01-07 21:52:32 +01:00
|
|
|
{
|
|
|
|
return h->size;
|
|
|
|
}
|
2003-09-05 14:44:35 +02:00
|
|
|
#endif
|
2002-01-07 21:52:32 +01:00
|
|
|
|
2002-01-03 11:22:59 +01:00
|
|
|
void
|
2002-04-27 15:06:40 +02:00
|
|
|
Curl_hash_destroy(curl_hash *h)
|
2002-01-03 11:22:59 +01:00
|
|
|
{
|
2002-04-13 01:40:19 +02:00
|
|
|
if (!h)
|
2002-01-07 21:52:32 +01:00
|
|
|
return;
|
|
|
|
|
2002-04-27 15:06:40 +02:00
|
|
|
Curl_hash_clean(h);
|
2002-01-03 11:22:59 +01:00
|
|
|
free(h);
|
|
|
|
}
|
2003-01-29 11:12:06 +01:00
|
|
|
|