CMake/Source/CTest/Curl/hash.c

309 lines
6.6 KiB
C
Raw Normal View History

2003-01-10 07:26:37 +03:00
/***************************************************************************
2003-01-07 05:13:39 +03:00
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
2003-01-10 07:26:37 +03:00
* Copyright (C) 1998 - 2002, Daniel Stenberg, <daniel@haxx.se>, et al.
2003-01-07 05:13:39 +03:00
*
2003-01-10 07:26:37 +03: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.
*
2003-01-07 05:13:39 +03: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
2003-01-10 07:26:37 +03:00
* furnished to do so, under the terms of the COPYING file.
2003-01-07 05:13:39 +03:00
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* $Id$
2003-01-10 07:26:37 +03:00
***************************************************************************/
2003-01-07 05:13:39 +03:00
#include "setup.h"
#include <string.h>
#include <stdlib.h>
2003-01-10 07:26:37 +03:00
2003-01-07 05:13:39 +03:00
#include "hash.h"
#include "llist.h"
#ifdef MALLOCDEBUG
/* this must be the last include file */
#include "memdebug.h"
#endif
2003-01-10 07:26:37 +03:00
/* {{{ static unsigned long _hash_str (const char *, size_t)
*/
static unsigned long
_hash_str (const char *key, size_t key_length)
2003-01-07 05:13:39 +03:00
{
2003-01-10 07:26:37 +03:00
char *end = (char *) key + key_length;
unsigned long h = 5381;
while (key < end) {
h += h << 5;
h ^= (unsigned long) *key++;
2003-01-07 05:13:39 +03:00
}
return h;
}
2003-01-10 07:26:37 +03:00
/* }}} */
2003-01-07 05:13:39 +03:00
2003-01-10 07:26:37 +03:00
/* {{{ static void _hash_element_dtor (void *, void *)
*/
2003-01-07 05:13:39 +03:00
static void
2003-01-10 07:26:37 +03:00
_hash_element_dtor (void *user, void *element)
2003-01-07 05:13:39 +03:00
{
2003-01-10 07:26:37 +03:00
curl_hash *h = (curl_hash *) user;
curl_hash_element *e = (curl_hash_element *) element;
if (e->key) {
free(e->key);
2003-01-07 05:13:39 +03:00
}
2003-01-10 07:26:37 +03:00
2003-01-07 05:13:39 +03:00
h->dtor(e->ptr);
free(e);
}
2003-01-10 07:26:37 +03:00
/* }}} */
2003-01-07 05:13:39 +03:00
2003-01-10 07:26:37 +03:00
/* {{{ void curl_hash_init (curl_hash *, int, curl_hash_dtor)
*/
2003-01-07 05:13:39 +03:00
void
2003-01-10 07:26:37 +03:00
Curl_hash_init (curl_hash *h, int slots, curl_hash_dtor dtor)
2003-01-07 05:13:39 +03:00
{
int i;
h->dtor = dtor;
h->size = 0;
h->slots = slots;
h->table = (curl_llist **) malloc(slots * sizeof(curl_llist *));
2003-01-10 07:26:37 +03:00
for (i = 0; i < slots; ++i) {
h->table[i] = Curl_llist_alloc((curl_llist_dtor) _hash_element_dtor);
2003-01-07 05:13:39 +03:00
}
}
2003-01-10 07:26:37 +03:00
/* }}} */
2003-01-07 05:13:39 +03:00
2003-01-10 07:26:37 +03:00
/* {{{ curl_hash *curl_hash_alloc (int, curl_hash_dtor)
*/
2003-01-07 05:13:39 +03:00
curl_hash *
2003-01-10 07:26:37 +03:00
Curl_hash_alloc (int slots, curl_hash_dtor dtor)
2003-01-07 05:13:39 +03:00
{
curl_hash *h;
2003-01-10 07:26:37 +03:00
h = (curl_hash *) malloc(sizeof(curl_hash));
if (NULL == h)
2003-01-07 05:13:39 +03:00
return NULL;
2003-01-10 07:26:37 +03:00
Curl_hash_init(h, slots, dtor);
2003-01-07 05:13:39 +03:00
return h;
}
2003-01-10 07:26:37 +03:00
/* }}} */
2003-01-07 05:13:39 +03:00
2003-01-10 07:26:37 +03:00
/* {{{ static int _hash_key_compare (char *, size_t, char *, size_t)
*/
static int
_hash_key_compare (char *key1, size_t key1_len, char *key2, size_t key2_len)
{
if (key1_len == key2_len &&
*key1 == *key2 &&
memcmp(key1, key2, key1_len) == 0) {
return 1;
2003-01-07 05:13:39 +03:00
}
2003-01-10 07:26:37 +03:00
return 0;
}
/* }}} */
2003-01-07 05:13:39 +03:00
2003-01-10 07:26:37 +03:00
/* {{{ static int _mk_hash_element (curl_hash_element **, char *, size_t, const void *)
*/
static int
_mk_hash_element (curl_hash_element **e, char *key, size_t key_len, const void *p)
2003-01-07 05:13:39 +03:00
{
2003-01-10 07:26:37 +03:00
*e = (curl_hash_element *) malloc(sizeof(curl_hash_element));
(*e)->key = strdup(key);
(*e)->key_len = key_len;
(*e)->ptr = (void *) p;
return 0;
}
/* }}} */
2003-01-07 05:13:39 +03:00
2003-01-10 07:26:37 +03:00
#define find_slot(__h, __k, __k_len) (_hash_str(__k, __k_len) % (__h)->slots)
2003-01-07 05:13:39 +03:00
2003-01-10 07:26:37 +03:00
#define FETCH_LIST \
curl_llist *l = h->table[find_slot(h, key, key_len)]
2003-01-07 05:13:39 +03:00
2003-01-10 07:26:37 +03:00
/* {{{ int curl_hash_add (curl_hash *, char *, size_t, const void *)
*/
2003-01-07 05:13:39 +03:00
int
2003-01-10 07:26:37 +03:00
Curl_hash_add (curl_hash *h, char *key, size_t key_len, const void *p)
2003-01-07 05:13:39 +03:00
{
2003-01-10 07:26:37 +03:00
curl_hash_element *he;
2003-01-07 05:13:39 +03:00
curl_llist_element *le;
2003-01-10 07:26:37 +03:00
FETCH_LIST;
for (le = CURL_LLIST_HEAD(l);
le != NULL;
le = CURL_LLIST_NEXT(le)) {
he = (curl_hash_element *) CURL_LLIST_VALP(le);
if (_hash_key_compare(he->key, he->key_len, key, key_len)) {
h->dtor(he->ptr);
he->ptr = (void *) p;
2003-01-07 05:13:39 +03:00
return 1;
}
}
2003-01-10 07:26:37 +03:00
if (_mk_hash_element(&he, key, key_len, p) != 0)
return 0;
2003-01-07 05:13:39 +03:00
2003-01-10 07:26:37 +03:00
if (Curl_llist_insert_next(l, CURL_LLIST_TAIL(l), he)) {
2003-01-07 05:13:39 +03:00
++h->size;
return 1;
}
2003-01-10 07:26:37 +03:00
return 0;
2003-01-07 05:13:39 +03:00
}
2003-01-10 07:26:37 +03:00
/* }}} */
2003-01-07 05:13:39 +03:00
2003-01-10 07:26:37 +03:00
/* {{{ int curl_hash_delete (curl_hash *, char *, size_t)
*/
2003-01-07 05:13:39 +03:00
int
2003-01-10 07:26:37 +03:00
Curl_hash_delete(curl_hash *h, char *key, size_t key_len)
2003-01-07 05:13:39 +03:00
{
2003-01-10 07:26:37 +03:00
curl_hash_element *he;
2003-01-07 05:13:39 +03:00
curl_llist_element *le;
2003-01-10 07:26:37 +03:00
FETCH_LIST;
for (le = CURL_LLIST_HEAD(l);
le != NULL;
le = CURL_LLIST_NEXT(le)) {
he = CURL_LLIST_VALP(le);
if (_hash_key_compare(he->key, he->key_len, key, key_len)) {
Curl_llist_remove(l, le, (void *) h);
2003-01-07 05:13:39 +03:00
--h->size;
return 1;
}
}
return 0;
}
2003-01-10 07:26:37 +03:00
/* }}} */
2003-01-07 05:13:39 +03:00
2003-01-10 07:26:37 +03:00
/* {{{ int curl_hash_pick (curl_hash *, char *, size_t, void **)
*/
void *
Curl_hash_pick(curl_hash *h, char *key, size_t key_len)
2003-01-07 05:13:39 +03:00
{
curl_llist_element *le;
2003-01-10 07:26:37 +03:00
curl_hash_element *he;
FETCH_LIST;
for (le = CURL_LLIST_HEAD(l);
le != NULL;
le = CURL_LLIST_NEXT(le)) {
he = CURL_LLIST_VALP(le);
if (_hash_key_compare(he->key, he->key_len, key, key_len)) {
return he->ptr;
2003-01-07 05:13:39 +03:00
}
}
2003-01-10 07:26:37 +03:00
return NULL;
2003-01-07 05:13:39 +03:00
}
2003-01-10 07:26:37 +03:00
/* }}} */
2003-01-07 05:13:39 +03:00
2003-01-10 07:26:37 +03:00
/* {{{ void curl_hash_apply (curl_hash *, void *, void (*)(void *, curl_hash_element *))
*/
2003-01-07 05:13:39 +03:00
void
2003-01-10 07:26:37 +03:00
Curl_hash_apply(curl_hash *h, void *user,
void (*cb)(void *user, void *ptr))
2003-01-07 05:13:39 +03:00
{
curl_llist_element *le;
int i;
for (i = 0; i < h->slots; ++i) {
2003-01-10 07:26:37 +03:00
for (le = CURL_LLIST_HEAD(h->table[i]);
le != NULL;
le = CURL_LLIST_NEXT(le)) {
curl_hash_element *el = CURL_LLIST_VALP(le);
cb(user, el->ptr);
2003-01-07 05:13:39 +03:00
}
}
}
2003-01-10 07:26:37 +03:00
/* }}} */
2003-01-07 05:13:39 +03:00
2003-01-10 07:26:37 +03:00
/* {{{ void curl_hash_clean (curl_hash *)
*/
2003-01-07 05:13:39 +03:00
void
2003-01-10 07:26:37 +03:00
Curl_hash_clean(curl_hash *h)
2003-01-07 05:13:39 +03:00
{
int i;
for (i = 0; i < h->slots; ++i) {
2003-01-10 07:26:37 +03:00
Curl_llist_destroy(h->table[i], (void *) h);
2003-01-07 05:13:39 +03:00
}
free(h->table);
}
2003-01-10 07:26:37 +03:00
/* }}} */
2003-01-07 05:13:39 +03:00
2003-01-10 07:26:37 +03:00
/* {{{ void curl_hash_clean_with_criterium (curl_hash *, void *,
int (*)(void *, void *))
*/
void
Curl_hash_clean_with_criterium(curl_hash *h, void *user,
int (*comp)(void *, void *))
{
curl_llist_element *le;
curl_llist_element *lnext;
int i;
for (i = 0; i < h->slots; ++i) {
le = CURL_LLIST_HEAD(h->table[i]);
while(le != NULL)
if (comp(user, ((curl_hash_element *) CURL_LLIST_VALP(le))->ptr)) {
lnext = CURL_LLIST_NEXT(le);
Curl_llist_remove(h->table[i], le, (void *) h);
--h->size;
le = lnext;
}
else
le = CURL_LLIST_NEXT(le);
}
}
/* {{{ int curl_hash_count (curl_hash *)
*/
int
Curl_hash_count(curl_hash *h)
2003-01-07 05:13:39 +03:00
{
return (int)h->size;
2003-01-07 05:13:39 +03:00
}
2003-01-10 07:26:37 +03:00
/* }}} */
2003-01-07 05:13:39 +03:00
2003-01-10 07:26:37 +03:00
/* {{{ void curl_hash_destroy (curl_hash *)
*/
2003-01-07 05:13:39 +03:00
void
2003-01-10 07:26:37 +03:00
Curl_hash_destroy(curl_hash *h)
2003-01-07 05:13:39 +03:00
{
2003-01-10 07:26:37 +03:00
if (!h)
2003-01-07 05:13:39 +03:00
return;
2003-01-10 07:26:37 +03:00
Curl_hash_clean(h);
2003-01-07 05:13:39 +03:00
free(h);
}
2003-01-10 07:26:37 +03:00
/* }}} */
2003-01-07 05:13:39 +03:00
/*
* local variables:
* eval: (load-file "../curl-mode.el")
* end:
* vim600: fdm=marker
* vim: et sw=2 ts=2 sts=2 tw=78
*/