From 87e3b9dbf378c67a3e6822cedebbac0d57f0c64e Mon Sep 17 00:00:00 2001 From: Mark Andrews Date: Wed, 27 Mar 2024 11:32:25 +1100 Subject: [PATCH] Pass a memory context in to dns_cache_create --- bin/delv/delv.c | 2 +- bin/named/server.c | 2 +- lib/dns/cache.c | 13 +++---------- lib/dns/include/dns/cache.h | 4 +++- tests/libtest/dns.c | 2 +- 5 files changed, 9 insertions(+), 14 deletions(-) diff --git a/bin/delv/delv.c b/bin/delv/delv.c index a767c0cffa..e06322de11 100644 --- a/bin/delv/delv.c +++ b/bin/delv/delv.c @@ -2151,7 +2151,7 @@ run_server(void *arg) { CHECK(dns_view_create(mctx, dispatchmgr, dns_rdataclass_in, "_default", &view)); - CHECK(dns_cache_create(loopmgr, dns_rdataclass_in, "", &cache)); + CHECK(dns_cache_create(loopmgr, dns_rdataclass_in, "", mctx, &cache)); dns_view_setcache(view, cache, false); dns_cache_detach(&cache); dns_view_setdstport(view, destport); diff --git a/bin/named/server.c b/bin/named/server.c index f972dce906..a17375dbaa 100644 --- a/bin/named/server.c +++ b/bin/named/server.c @@ -4671,7 +4671,7 @@ configure_view(dns_view_t *view, dns_viewlist_t *viewlist, cfg_obj_t *config, * is simply a named cache that is not shared. */ CHECK(dns_cache_create(named_g_loopmgr, view->rdclass, - cachename, &cache)); + cachename, mctx, &cache)); } nsc = isc_mem_get(mctx, sizeof(*nsc)); nsc->cache = NULL; diff --git a/lib/dns/cache.c b/lib/dns/cache.c index 28bb6d297c..e9e677c40b 100644 --- a/lib/dns/cache.c +++ b/lib/dns/cache.c @@ -66,7 +66,7 @@ struct dns_cache { /* Unlocked. */ unsigned int magic; isc_mutex_t lock; - isc_mem_t *mctx; /* Main cache memory */ + isc_mem_t *mctx; /* Memory context for the dns_cache object */ isc_mem_t *hmctx; /* Heap memory */ isc_mem_t *tmctx; /* Tree memory */ isc_loop_t *loop; @@ -160,24 +160,16 @@ cache_destroy(dns_cache_t *cache) { isc_result_t dns_cache_create(isc_loopmgr_t *loopmgr, dns_rdataclass_t rdclass, - const char *cachename, dns_cache_t **cachep) { + const char *cachename, isc_mem_t *mctx, dns_cache_t **cachep) { isc_result_t result; dns_cache_t *cache = NULL; - isc_mem_t *mctx = NULL; REQUIRE(loopmgr != NULL); REQUIRE(cachename != NULL); REQUIRE(cachep != NULL && *cachep == NULL); - /* - * Self. - */ - isc_mem_create(&mctx); - isc_mem_setname(mctx, "cache-self"); - cache = isc_mem_get(mctx, sizeof(*cache)); *cache = (dns_cache_t){ - .mctx = mctx, .rdclass = rdclass, .name = isc_mem_strdup(mctx, cachename), .loop = isc_loop_ref(isc_loop_main(loopmgr)), @@ -186,6 +178,7 @@ dns_cache_create(isc_loopmgr_t *loopmgr, dns_rdataclass_t rdclass, }; isc_mutex_init(&cache->lock); + isc_mem_attach(mctx, &cache->mctx); isc_stats_create(mctx, &cache->stats, dns_cachestatscounter_max); diff --git a/lib/dns/include/dns/cache.h b/lib/dns/include/dns/cache.h index 103e5cbf7e..72cf80c3f4 100644 --- a/lib/dns/include/dns/cache.h +++ b/lib/dns/include/dns/cache.h @@ -73,7 +73,7 @@ ISC_REFCOUNT_DECL(dns_cache); isc_result_t dns_cache_create(isc_loopmgr_t *loopmgr, dns_rdataclass_t rdclass, - const char *cachename, dns_cache_t **cachep); + const char *cachename, isc_mem_t *mctx, dns_cache_t **cachep); /*%< * Create a new DNS cache. * @@ -84,6 +84,8 @@ dns_cache_create(isc_loopmgr_t *loopmgr, dns_rdataclass_t rdclass, *\li 'loopmgr' is a valid loop manager. * *\li 'cachename' is a valid string. This must not be NULL. + + *\li 'mctx' is a valid memory context. * *\li 'cachep' is a valid pointer, and *cachep == NULL * diff --git a/tests/libtest/dns.c b/tests/libtest/dns.c index e57a6820e8..8dd1bbc5e5 100644 --- a/tests/libtest/dns.c +++ b/tests/libtest/dns.c @@ -84,7 +84,7 @@ dns_test_makeview(const char *name, bool with_dispatchmgr, bool with_cache, } if (with_cache) { - result = dns_cache_create(loopmgr, dns_rdataclass_in, "", + result = dns_cache_create(loopmgr, dns_rdataclass_in, "", mctx, &cache); if (result != ISC_R_SUCCESS) { dns_view_detach(&view);