Uses less macros. #ifdef'ed out unused functions. Edited slightly to be

more in the same style as other curl source code. The only actual code change
is an added check after a malloc() call.
This commit is contained in:
Daniel Stenberg 2003-09-05 12:44:35 +00:00
parent 359c0eac0d
commit adcbe03aeb
3 changed files with 62 additions and 58 deletions

View File

@ -36,7 +36,7 @@
static unsigned long static unsigned long
_hash_str (const char *key, size_t key_length) hash_str(const char *key, size_t key_length)
{ {
char *end = (char *) key + key_length; char *end = (char *) key + key_length;
unsigned long h = 5381; unsigned long h = 5381;
@ -50,7 +50,7 @@ _hash_str (const char *key, size_t key_length)
} }
static void static void
_hash_element_dtor (void *user, void *element) hash_element_dtor(void *user, void *element)
{ {
curl_hash *h = (curl_hash *) user; curl_hash *h = (curl_hash *) user;
curl_hash_element *e = (curl_hash_element *) element; curl_hash_element *e = (curl_hash_element *) element;
@ -77,7 +77,7 @@ Curl_hash_init(curl_hash *h, int slots, curl_hash_dtor dtor)
h->table = (curl_llist **) malloc(slots * sizeof(curl_llist *)); h->table = (curl_llist **) malloc(slots * sizeof(curl_llist *));
if(h->table) { if(h->table) {
for (i = 0; i < slots; ++i) { for (i = 0; i < slots; ++i) {
h->table[i] = Curl_llist_alloc((curl_llist_dtor) _hash_element_dtor); h->table[i] = Curl_llist_alloc((curl_llist_dtor) hash_element_dtor);
if(!h->table[i]) { if(!h->table[i]) {
while(i--) while(i--)
Curl_llist_destroy(h->table[i], NULL); Curl_llist_destroy(h->table[i], NULL);
@ -109,7 +109,7 @@ Curl_hash_alloc(int slots, curl_hash_dtor dtor)
} }
static int static int
_hash_key_compare (char *key1, size_t key1_len, char *key2, size_t key2_len) hash_key_compare(char *key1, size_t key1_len, char *key2, size_t key2_len)
{ {
if (key1_len == key2_len && if (key1_len == key2_len &&
*key1 == *key2 && *key1 == *key2 &&
@ -120,44 +120,47 @@ _hash_key_compare (char *key1, size_t key1_len, char *key2, size_t key2_len)
return 0; return 0;
} }
static int static curl_hash_element *
_mk_hash_element (curl_hash_element **e, char *key, size_t key_len, mk_hash_element(char *key, size_t key_len, const void *p)
const void *p)
{ {
*e = (curl_hash_element *) malloc(sizeof(curl_hash_element)); curl_hash_element *he =
(*e)->key = strdup(key); (curl_hash_element *) malloc(sizeof(curl_hash_element));
(*e)->key_len = key_len;
(*e)->ptr = (void *) p; if(he) {
return 0; he->key = strdup(key);
he->key_len = key_len;
he->ptr = (void *) p;
}
return he;
} }
#define find_slot(__h, __k, __k_len) (_hash_str(__k, __k_len) % (__h)->slots) #define find_slot(__h, __k, __k_len) (hash_str(__k, __k_len) % (__h)->slots)
#define FETCH_LIST \ #define FETCH_LIST(x,y,z) x->table[find_slot(x, y, z)]
curl_llist *l = h->table[find_slot(h, key, key_len)]
int int
Curl_hash_add (curl_hash *h, char *key, size_t key_len, const void *p) Curl_hash_add(curl_hash *h, char *key, size_t key_len, const void *p)
{ {
curl_hash_element *he; curl_hash_element *he;
curl_llist_element *le; curl_llist_element *le;
FETCH_LIST; curl_llist *l = FETCH_LIST(h, key, key_len);
for (le = CURL_LLIST_HEAD(l); for (le = l->head;
le != NULL; le;
le = CURL_LLIST_NEXT(le)) { le = le->next) {
he = (curl_hash_element *) CURL_LLIST_VALP(le); he = (curl_hash_element *) le->ptr;
if (_hash_key_compare(he->key, he->key_len, key, key_len)) { if (hash_key_compare(he->key, he->key_len, key, key_len)) {
h->dtor(he->ptr); h->dtor(he->ptr);
he->ptr = (void *) p; he->ptr = (void *) p;
return 1; return 1;
} }
} }
if (_mk_hash_element(&he, key, key_len, p) != 0) he = mk_hash_element(key, key_len, p);
if (!he)
return 0; return 0;
if (Curl_llist_insert_next(l, CURL_LLIST_TAIL(l), he)) { if (Curl_llist_insert_next(l, l->tail, he)) {
++h->size; ++h->size;
return 1; return 1;
} }
@ -165,18 +168,19 @@ Curl_hash_add (curl_hash *h, char *key, size_t key_len, const void *p)
return 0; return 0;
} }
#if 0
int int
Curl_hash_delete(curl_hash *h, char *key, size_t key_len) Curl_hash_delete(curl_hash *h, char *key, size_t key_len)
{ {
curl_hash_element *he; curl_hash_element *he;
curl_llist_element *le; curl_llist_element *le;
FETCH_LIST; curl_llist *l = FETCH_LIST(h, key, key_len);
for (le = CURL_LLIST_HEAD(l); for (le = l->head;
le != NULL; le;
le = CURL_LLIST_NEXT(le)) { le = le->next) {
he = CURL_LLIST_VALP(le); he = le->ptr;
if (_hash_key_compare(he->key, he->key_len, key, key_len)) { if (hash_key_compare(he->key, he->key_len, key, key_len)) {
Curl_llist_remove(l, le, (void *) h); Curl_llist_remove(l, le, (void *) h);
--h->size; --h->size;
return 1; return 1;
@ -185,19 +189,20 @@ Curl_hash_delete(curl_hash *h, char *key, size_t key_len)
return 0; return 0;
} }
#endif
void * void *
Curl_hash_pick(curl_hash *h, char *key, size_t key_len) Curl_hash_pick(curl_hash *h, char *key, size_t key_len)
{ {
curl_llist_element *le; curl_llist_element *le;
curl_hash_element *he; curl_hash_element *he;
FETCH_LIST; curl_llist *l = FETCH_LIST(h, key, key_len);
for (le = CURL_LLIST_HEAD(l); for (le = l->head;
le != NULL; le;
le = CURL_LLIST_NEXT(le)) { le = le->next) {
he = CURL_LLIST_VALP(le); he = le->ptr;
if (_hash_key_compare(he->key, he->key_len, key, key_len)) { if (hash_key_compare(he->key, he->key_len, key, key_len)) {
return he->ptr; return he->ptr;
} }
} }
@ -205,6 +210,7 @@ Curl_hash_pick(curl_hash *h, char *key, size_t key_len)
return NULL; return NULL;
} }
#if defined(CURLDEBUG) && defined(AGGRESIVE_TEST)
void void
Curl_hash_apply(curl_hash *h, void *user, Curl_hash_apply(curl_hash *h, void *user,
void (*cb)(void *user, void *ptr)) void (*cb)(void *user, void *ptr))
@ -213,14 +219,15 @@ Curl_hash_apply(curl_hash *h, void *user,
int i; int i;
for (i = 0; i < h->slots; ++i) { for (i = 0; i < h->slots; ++i) {
for (le = CURL_LLIST_HEAD(h->table[i]); for (le = (h->table[i])->head;
le != NULL; le;
le = CURL_LLIST_NEXT(le)) { le = le->next) {
curl_hash_element *el = CURL_LLIST_VALP(le); curl_hash_element *el = le->ptr;
cb(user, el->ptr); cb(user, el->ptr);
} }
} }
} }
#endif
void void
Curl_hash_clean(curl_hash *h) Curl_hash_clean(curl_hash *h)
@ -240,27 +247,32 @@ Curl_hash_clean_with_criterium(curl_hash *h, void *user,
{ {
curl_llist_element *le; curl_llist_element *le;
curl_llist_element *lnext; curl_llist_element *lnext;
curl_llist *list;
int i; int i;
for (i = 0; i < h->slots; ++i) { for (i = 0; i < h->slots; ++i) {
le = CURL_LLIST_HEAD(h->table[i]); list = h->table[i];
while(le != NULL) le = list->head; /* get first list entry */
if (comp(user, ((curl_hash_element *) CURL_LLIST_VALP(le))->ptr)) { while(le) {
lnext = CURL_LLIST_NEXT(le); curl_hash_element *he = le->ptr;
Curl_llist_remove(h->table[i], le, (void *) h); lnext = le->next;
--h->size; /* ask the callback function if we shall remove this entry or not */
le = lnext; if (comp(user, he->ptr)) {
Curl_llist_remove(list, le, (void *) h);
--h->size; /* one less entry in the hash now */
} }
else le = lnext;
le = CURL_LLIST_NEXT(le); }
} }
} }
#if 0
int int
Curl_hash_count(curl_hash *h) Curl_hash_count(curl_hash *h)
{ {
return h->size; return h->size;
} }
#endif
void void
Curl_hash_destroy(curl_hash *h) Curl_hash_destroy(curl_hash *h)

View File

@ -161,7 +161,7 @@ Curl_llist_destroy(curl_llist *list, void *user)
{ {
if(list) { if(list) {
while (list->size > 0) while (list->size > 0)
Curl_llist_remove(list, CURL_LLIST_TAIL(list), user); Curl_llist_remove(list, list->tail, user);
free(list); free(list);
} }

View File

@ -53,12 +53,4 @@ int Curl_llist_remove_next(curl_llist *, curl_llist_element *, void *);
size_t Curl_llist_count(curl_llist *); size_t Curl_llist_count(curl_llist *);
void Curl_llist_destroy(curl_llist *, void *); void Curl_llist_destroy(curl_llist *, void *);
#define CURL_LLIST_HEAD(__l) ((__l)->head)
#define CURL_LLIST_TAIL(__l) ((__l)->tail)
#define CURL_LLIST_NEXT(__e) ((__e)->next)
#define CURL_LLIST_PREV(__e) ((__e)->prev)
#define CURL_LLIST_VALP(__e) ((__e)->ptr)
#define CURL_LLIST_IS_TAIL(__e) ((__e)->next ? 0 : 1)
#define CURL_LLIST_IS_HEAD(__e) ((__e)->prev ? 0 : 1)
#endif #endif