hostcache: made all host caches use structs, not pointers

This avoids unnecessary dynamic allocs and as this also removed the last
users of *hash_alloc() and *hash_destroy(), those two functions are now
removed.
This commit is contained in:
Daniel Stenberg 2015-05-12 09:46:53 +02:00
parent d37e0160c2
commit b419e7ae0c
10 changed files with 31 additions and 82 deletions

View File

@ -89,32 +89,6 @@ Curl_hash_init(struct curl_hash *h,
}
}
struct curl_hash *
Curl_hash_alloc(int slots,
hash_function hfunc,
comp_function comparator,
curl_hash_dtor dtor)
{
struct curl_hash *h;
if(!slots || !hfunc || !comparator ||!dtor) {
return NULL; /* failure */
}
h = malloc(sizeof(struct curl_hash));
if(h) {
if(Curl_hash_init(h, slots, hfunc, comparator, dtor)) {
/* failure */
free(h);
h = NULL;
}
}
return h;
}
static struct curl_hash_element *
mk_hash_element(const void *key, size_t key_len, const void *p)
{
@ -281,17 +255,6 @@ Curl_hash_clean_with_criterium(struct curl_hash *h, void *user,
}
}
void
Curl_hash_destroy(struct curl_hash *h)
{
if(!h)
return;
Curl_hash_clean(h);
free(h);
}
size_t Curl_hash_str(void* key, size_t key_length, size_t slots_num)
{
const char* key_str = (const char *) key;

View File

@ -7,7 +7,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@ -74,11 +74,6 @@ int Curl_hash_init(struct curl_hash *h,
comp_function comparator,
curl_hash_dtor dtor);
struct curl_hash *Curl_hash_alloc(int slots,
hash_function hfunc,
comp_function comparator,
curl_hash_dtor dtor);
void *Curl_hash_add(struct curl_hash *h, void *key, size_t key_len, void *p);
int Curl_hash_delete(struct curl_hash *h, void *key, size_t key_len);
void *Curl_hash_pick(struct curl_hash *, void * key, size_t key_len);
@ -88,8 +83,6 @@ int Curl_hash_count(struct curl_hash *h);
void Curl_hash_clean(struct curl_hash *h);
void Curl_hash_clean_with_criterium(struct curl_hash *h, void *user,
int (*comp)(void *, void *));
void Curl_hash_destroy(struct curl_hash *h);
size_t Curl_hash_str(void* key, size_t key_length, size_t slots_num);
size_t Curl_str_key_compare(void*k1, size_t key1_len, void*k2,
size_t key2_len);

View File

@ -742,11 +742,12 @@ static void freednsentry(void *freethis)
}
/*
* Curl_mk_dnscache() creates a new DNS cache and returns the handle for it.
* Curl_mk_dnscache() inits a new DNS cache and returns success/failure.
*/
struct curl_hash *Curl_mk_dnscache(void)
int Curl_mk_dnscache(struct curl_hash *hash)
{
return Curl_hash_alloc(7, Curl_hash_str, Curl_str_key_compare, freednsentry);
return Curl_hash_init(hash, 7, Curl_hash_str, Curl_str_key_compare,
freednsentry);
}
/*

View File

@ -124,8 +124,8 @@ void Curl_resolv_unlock(struct SessionHandle *data,
/* for debugging purposes only: */
void Curl_scan_cache_used(void *user, void *ptr);
/* make a new dns cache and return the handle */
struct curl_hash *Curl_mk_dnscache(void);
/* init a new dns cache and return success */
int Curl_mk_dnscache(struct curl_hash *hash);
/* prune old entries from the DNS cache */
void Curl_hostcache_prune(struct SessionHandle *data);

View File

@ -294,8 +294,7 @@ struct Curl_multi *Curl_multi_handle(int hashsize, /* socket hash */
multi->type = CURL_MULTI_HANDLE;
multi->hostcache = Curl_mk_dnscache();
if(!multi->hostcache)
if(Curl_mk_dnscache(&multi->hostcache))
goto error;
if(sh_init(&multi->sockhash, hashsize))
@ -329,8 +328,7 @@ struct Curl_multi *Curl_multi_handle(int hashsize, /* socket hash */
error:
Curl_hash_clean(&multi->sockhash);
Curl_hash_destroy(multi->hostcache);
multi->hostcache = NULL;
Curl_hash_clean(&multi->hostcache);
Curl_conncache_destroy(&multi->conn_cache);
Curl_close(multi->closure_handle);
multi->closure_handle = NULL;
@ -400,7 +398,7 @@ CURLMcode curl_multi_add_handle(CURLM *multi_handle,
easy handle's one is currently not set. */
else if(!data->dns.hostcache ||
(data->dns.hostcachetype == HCACHE_NONE)) {
data->dns.hostcache = multi->hostcache;
data->dns.hostcache = &multi->hostcache;
data->dns.hostcachetype = HCACHE_MULTI;
}
@ -1861,7 +1859,7 @@ CURLMcode curl_multi_cleanup(CURLM *multi_handle)
sigpipe_ignore(multi->closure_handle, &pipe_st);
restore_pipe = TRUE;
multi->closure_handle->dns.hostcache = multi->hostcache;
multi->closure_handle->dns.hostcache = &multi->hostcache;
Curl_hostcache_clean(multi->closure_handle,
multi->closure_handle->dns.hostcache);
@ -1891,7 +1889,7 @@ CURLMcode curl_multi_cleanup(CURLM *multi_handle)
data = nextdata;
}
Curl_hash_destroy(multi->hostcache);
Curl_hash_clean(&multi->hostcache);
/* Free the blacklists by setting them to NULL */
Curl_pipeline_set_site_blacklist(NULL, &multi->pipelining_site_bl);

View File

@ -86,7 +86,7 @@ struct Curl_multi {
void *socket_userp;
/* Hostname cache */
struct curl_hash *hostcache;
struct curl_hash hostcache;
/* timetree points to the splay-tree of time nodes to figure out expire
times of all currently set timers */

View File

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@ -38,6 +38,11 @@ curl_share_init(void)
if(share)
share->specifier |= (1<<CURL_LOCK_DATA_SHARE);
if(Curl_mk_dnscache(&share->hostcache)) {
free(share);
return NULL;
}
return share;
}
@ -67,11 +72,6 @@ curl_share_setopt(CURLSH *sh, CURLSHoption option, ...)
share->specifier |= (1<<type);
switch( type ) {
case CURL_LOCK_DATA_DNS:
if(!share->hostcache) {
share->hostcache = Curl_mk_dnscache();
if(!share->hostcache)
res = CURLSHE_NOMEM;
}
break;
case CURL_LOCK_DATA_COOKIE:
@ -115,10 +115,6 @@ curl_share_setopt(CURLSH *sh, CURLSHoption option, ...)
share->specifier &= ~(1<<type);
switch( type ) {
case CURL_LOCK_DATA_DNS:
if(share->hostcache) {
Curl_hash_destroy(share->hostcache);
share->hostcache = NULL;
}
break;
case CURL_LOCK_DATA_COOKIE:
@ -192,10 +188,7 @@ curl_share_cleanup(CURLSH *sh)
return CURLSHE_IN_USE;
}
if(share->hostcache) {
Curl_hash_destroy(share->hostcache);
share->hostcache = NULL;
}
Curl_hash_clean(&share->hostcache);
#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
Curl_cookie_cleanup(share->cookies);

View File

@ -7,7 +7,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@ -44,7 +44,7 @@ struct Curl_share {
curl_unlock_function unlockfunc;
void *clientdata;
struct curl_hash *hostcache;
struct curl_hash hostcache;
#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
struct CookieInfo *cookies;
#endif

View File

@ -2187,9 +2187,9 @@ CURLcode Curl_setopt(struct SessionHandle *data, CURLoption option,
data->share->dirty++;
if(data->share->hostcache) {
if(data->share->specifier & (1<< CURL_LOCK_DATA_DNS)) {
/* use shared host cache */
data->dns.hostcache = data->share->hostcache;
data->dns.hostcache = &data->share->hostcache;
data->dns.hostcachetype = HCACHE_SHARED;
}
#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)

View File

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@ -40,18 +40,19 @@
#include "memdebug.h" /* LAST include file */
static struct SessionHandle *data;
static struct curl_hash *hp;
static struct curl_hash hp;
static char *data_key;
static struct Curl_dns_entry *data_node;
static CURLcode unit_setup( void )
{
int rc;
data = curl_easy_init();
if (!data)
return CURLE_OUT_OF_MEMORY;
hp = Curl_mk_dnscache();
if(!hp) {
rc = Curl_mk_dnscache(&hp);
if(rc) {
curl_easy_cleanup(data);
curl_global_cleanup();
return CURLE_OUT_OF_MEMORY;
@ -66,7 +67,7 @@ static void unit_stop( void )
free(data_node);
}
free(data_key);
Curl_hash_destroy(hp);
Curl_hash_clean(&hp);
curl_easy_cleanup(data);
curl_global_cleanup();
@ -129,7 +130,7 @@ UNITTEST_START
key_len = strlen(data_key);
data_node->inuse = 1; /* hash will hold the reference */
nodep = Curl_hash_add(hp, data_key, key_len+1, data_node);
nodep = Curl_hash_add(&hp, data_key, key_len+1, data_node);
abort_unless(nodep, "insertion into hash failed");
/* Freeing will now be done by Curl_hash_destroy */
data_node = NULL;