diff options
Diffstat (limited to 'lib/dns/cache.c')
| -rw-r--r-- | lib/dns/cache.c | 105 |
1 files changed, 77 insertions, 28 deletions
diff --git a/lib/dns/cache.c b/lib/dns/cache.c index 28ead664f9e7..5ecbc26c0b5b 100644 --- a/lib/dns/cache.c +++ b/lib/dns/cache.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2004-2009 Internet Systems Consortium, Inc. ("ISC") + * Copyright (C) 2004-2009, 2011 Internet Systems Consortium, Inc. ("ISC") * Copyright (C) 1999-2003 Internet Software Consortium. * * Permission to use, copy, modify, and/or distribute this software for any @@ -15,7 +15,7 @@ * PERFORMANCE OF THIS SOFTWARE. */ -/* $Id: cache.c,v 1.80.50.3 2009-05-06 23:34:30 jinmei Exp $ */ +/* $Id: cache.c,v 1.80.50.5 2011-03-03 23:46:01 tbox Exp $ */ /*! \file */ @@ -40,6 +40,8 @@ #include <dns/rdatasetiter.h> #include <dns/result.h> +#include "rbtdb.h" + #define CACHE_MAGIC ISC_MAGIC('$', '$', '$', '$') #define VALID_CACHE(cache) ISC_MAGIC_VALID(cache, CACHE_MAGIC) @@ -121,7 +123,8 @@ struct dns_cache { unsigned int magic; isc_mutex_t lock; isc_mutex_t filelock; - isc_mem_t *mctx; + isc_mem_t *mctx; /* Main cache memory */ + isc_mem_t *hmctx; /* Heap memory */ /* Locked by 'lock'. */ int references; @@ -166,26 +169,51 @@ cache_create_db(dns_cache_t *cache, dns_db_t **db) { } isc_result_t -dns_cache_create(isc_mem_t *mctx, isc_taskmgr_t *taskmgr, +dns_cache_create(isc_mem_t *cmctx, isc_taskmgr_t *taskmgr, isc_timermgr_t *timermgr, dns_rdataclass_t rdclass, const char *db_type, unsigned int db_argc, char **db_argv, dns_cache_t **cachep) { + return (dns_cache_create3(cmctx, cmctx, taskmgr, timermgr, rdclass, + NULL, db_type, db_argc, db_argv, cachep)); +} + +isc_result_t +dns_cache_create2(isc_mem_t *cmctx, isc_taskmgr_t *taskmgr, + isc_timermgr_t *timermgr, dns_rdataclass_t rdclass, + const char *cachename, const char *db_type, + unsigned int db_argc, char **db_argv, dns_cache_t **cachep) +{ + return (dns_cache_create3(cmctx, cmctx, taskmgr, timermgr, rdclass, + cachename, db_type, db_argc, db_argv, + cachep)); +} + +isc_result_t +dns_cache_create3(isc_mem_t *cmctx, isc_mem_t *hmctx, isc_taskmgr_t *taskmgr, + isc_timermgr_t *timermgr, dns_rdataclass_t rdclass, + const char *cachename, const char *db_type, + unsigned int db_argc, char **db_argv, dns_cache_t **cachep) +{ isc_result_t result; dns_cache_t *cache; - int i; + int i, extra = 0; isc_task_t *dbtask; REQUIRE(cachep != NULL); REQUIRE(*cachep == NULL); - REQUIRE(mctx != NULL); + REQUIRE(cmctx != NULL); + REQUIRE(hmctx != NULL); + + UNUSED(cachename); - cache = isc_mem_get(mctx, sizeof(*cache)); + cache = isc_mem_get(cmctx, sizeof(*cache)); if (cache == NULL) return (ISC_R_NOMEMORY); - cache->mctx = NULL; - isc_mem_attach(mctx, &cache->mctx); + cache->mctx = cache->hmctx = NULL; + isc_mem_attach(cmctx, &cache->mctx); + isc_mem_attach(hmctx, &cache->hmctx); result = isc_mutex_init(&cache->lock); if (result != ISC_R_SUCCESS) @@ -199,26 +227,38 @@ dns_cache_create(isc_mem_t *mctx, isc_taskmgr_t *taskmgr, cache->live_tasks = 0; cache->rdclass = rdclass; - cache->db_type = isc_mem_strdup(mctx, db_type); + cache->db_type = isc_mem_strdup(cmctx, db_type); if (cache->db_type == NULL) { result = ISC_R_NOMEMORY; goto cleanup_filelock; } - cache->db_argc = db_argc; - if (cache->db_argc == 0) - cache->db_argv = NULL; - else { - cache->db_argv = isc_mem_get(mctx, + /* + * For databases of type "rbt" we pass hmctx to dns_db_create() + * via cache->db_argv, followed by the rest of the arguments in + * db_argv (of which there really shouldn't be any). + */ + if (strcmp(cache->db_type, "rbt") == 0) + extra = 1; + + cache->db_argc = db_argc + extra; + cache->db_argv = NULL; + + if (cache->db_argc != 0) { + cache->db_argv = isc_mem_get(cmctx, cache->db_argc * sizeof(char *)); if (cache->db_argv == NULL) { result = ISC_R_NOMEMORY; goto cleanup_dbtype; } + for (i = 0; i < cache->db_argc; i++) cache->db_argv[i] = NULL; - for (i = 0; i < cache->db_argc; i++) { - cache->db_argv[i] = isc_mem_strdup(mctx, db_argv[i]); + + cache->db_argv[0] = (char *) hmctx; + for (i = extra; i < cache->db_argc; i++) { + cache->db_argv[i] = isc_mem_strdup(cmctx, + db_argv[i - extra]); if (cache->db_argv[i] == NULL) { result = ISC_R_NOMEMORY; goto cleanup_dbargv; @@ -226,6 +266,9 @@ dns_cache_create(isc_mem_t *mctx, isc_taskmgr_t *taskmgr, } } + /* + * Create the database + */ cache->db = NULL; result = cache_create_db(cache, &cache->db); if (result != ISC_R_SUCCESS) @@ -262,27 +305,26 @@ dns_cache_create(isc_mem_t *mctx, isc_taskmgr_t *taskmgr, cleanup_db: dns_db_detach(&cache->db); cleanup_dbargv: - for (i = 0; i < cache->db_argc; i++) + for (i = extra; i < cache->db_argc; i++) if (cache->db_argv[i] != NULL) - isc_mem_free(mctx, cache->db_argv[i]); + isc_mem_free(cmctx, cache->db_argv[i]); if (cache->db_argv != NULL) - isc_mem_put(mctx, cache->db_argv, + isc_mem_put(cmctx, cache->db_argv, cache->db_argc * sizeof(char *)); cleanup_dbtype: - isc_mem_free(mctx, cache->db_type); + isc_mem_free(cmctx, cache->db_type); cleanup_filelock: DESTROYLOCK(&cache->filelock); cleanup_lock: DESTROYLOCK(&cache->lock); cleanup_mem: - isc_mem_put(mctx, cache, sizeof(*cache)); - isc_mem_detach(&mctx); + isc_mem_detach(&cache->hmctx); + isc_mem_putanddetach(&cache->mctx, cache, sizeof(*cache)); return (result); } static void cache_free(dns_cache_t *cache) { - isc_mem_t *mctx; int i; REQUIRE(VALID_CACHE(cache)); @@ -313,7 +355,14 @@ cache_free(dns_cache_t *cache) { dns_db_detach(&cache->db); if (cache->db_argv != NULL) { - for (i = 0; i < cache->db_argc; i++) + /* + * We don't free db_argv[0] in "rbt" cache databases + * as it's a pointer to hmctx + */ + int extra = 0; + if (strcmp(cache->db_type, "rbt") == 0) + extra = 1; + for (i = extra; i < cache->db_argc; i++) if (cache->db_argv[i] != NULL) isc_mem_free(cache->mctx, cache->db_argv[i]); isc_mem_put(cache->mctx, cache->db_argv, @@ -325,10 +374,10 @@ cache_free(dns_cache_t *cache) { DESTROYLOCK(&cache->lock); DESTROYLOCK(&cache->filelock); + cache->magic = 0; - mctx = cache->mctx; - isc_mem_put(cache->mctx, cache, sizeof(*cache)); - isc_mem_detach(&mctx); + isc_mem_detach(&cache->hmctx); + isc_mem_putanddetach(&cache->mctx, cache, sizeof(*cache)); } |
