From b419e7ae0c9869ac3eb06d529efeb2c6dc1b4bc1 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 12 May 2015 09:46:53 +0200 Subject: [PATCH] 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. --- lib/hash.c | 37 ------------------------------------- lib/hash.h | 9 +-------- lib/hostip.c | 7 ++++--- lib/hostip.h | 4 ++-- lib/multi.c | 12 +++++------- lib/multihandle.h | 2 +- lib/share.c | 21 +++++++-------------- lib/share.h | 4 ++-- lib/url.c | 4 ++-- tests/unit/unit1305.c | 13 +++++++------ 10 files changed, 31 insertions(+), 82 deletions(-) diff --git a/lib/hash.c b/lib/hash.c index af2887758..b1aebd5b8 100644 --- a/lib/hash.c +++ b/lib/hash.c @@ -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; diff --git a/lib/hash.h b/lib/hash.h index aa935d4eb..bc7c20dec 100644 --- a/lib/hash.h +++ b/lib/hash.h @@ -7,7 +7,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2007, Daniel Stenberg, , et al. + * Copyright (C) 1998 - 2015, Daniel Stenberg, , 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); diff --git a/lib/hostip.c b/lib/hostip.c index 764e78026..338cf2de6 100644 --- a/lib/hostip.c +++ b/lib/hostip.c @@ -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); } /* diff --git a/lib/hostip.h b/lib/hostip.h index a79b89a44..d5b44bc9e 100644 --- a/lib/hostip.h +++ b/lib/hostip.h @@ -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); diff --git a/lib/multi.c b/lib/multi.c index c1467ad4d..bd16f9462 100644 --- a/lib/multi.c +++ b/lib/multi.c @@ -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); diff --git a/lib/multihandle.h b/lib/multihandle.h index 1ef28c9d6..640e3fb4b 100644 --- a/lib/multihandle.h +++ b/lib/multihandle.h @@ -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 */ diff --git a/lib/share.c b/lib/share.c index 3fc53119e..b61a86bc2 100644 --- a/lib/share.c +++ b/lib/share.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2013, Daniel Stenberg, , et al. + * Copyright (C) 1998 - 2015, Daniel Stenberg, , 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<hostcache)) { + free(share); + return NULL; + } + return share; } @@ -67,11 +72,6 @@ curl_share_setopt(CURLSH *sh, CURLSHoption option, ...) share->specifier |= (1<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<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); diff --git a/lib/share.h b/lib/share.h index 9a5128e93..8e6629b7b 100644 --- a/lib/share.h +++ b/lib/share.h @@ -7,7 +7,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2012, Daniel Stenberg, , et al. + * Copyright (C) 1998 - 2015, Daniel Stenberg, , 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 diff --git a/lib/url.c b/lib/url.c index c99066074..20b802ad1 100644 --- a/lib/url.c +++ b/lib/url.c @@ -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) diff --git a/tests/unit/unit1305.c b/tests/unit/unit1305.c index 4f9c609b0..b4e837762 100644 --- a/tests/unit/unit1305.c +++ b/tests/unit/unit1305.c @@ -5,7 +5,7 @@ * | (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * - * Copyright (C) 1998 - 2011, Daniel Stenberg, , et al. + * Copyright (C) 1998 - 2015, Daniel Stenberg, , 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;