From: Witold Kręcicki Date: Fri, 17 May 2019 11:47:00 +0000 (+0200) Subject: lib/dns/cache.c: use isc_refcount_t X-Git-Tag: v9.15.2~3^2~21 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=757cff6644f9ad2b9dd615e2c794427b07651174;p=thirdparty%2Fbind9.git lib/dns/cache.c: use isc_refcount_t --- diff --git a/lib/dns/cache.c b/lib/dns/cache.c index 3fddd9c830b..731b79a5605 100644 --- a/lib/dns/cache.c +++ b/lib/dns/cache.c @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -130,9 +131,9 @@ struct dns_cache { isc_mem_t *mctx; /* Main cache memory */ isc_mem_t *hmctx; /* Heap memory */ char *name; + isc_refcount_t references; /* Locked by 'lock'. */ - int references; int live_tasks; dns_rdataclass_t rdclass; dns_db_t *db; @@ -214,7 +215,7 @@ dns_cache_create(isc_mem_t *cmctx, isc_mem_t *hmctx, isc_taskmgr_t *taskmgr, isc_mutex_init(&cache->lock); isc_mutex_init(&cache->filelock); - cache->references = 1; + isc_refcount_init(&cache->references, 1); cache->live_tasks = 0; cache->rdclass = rdclass; cache->serve_stale_ttl = 0; @@ -337,7 +338,7 @@ cache_free(dns_cache_t *cache) { int i; REQUIRE(VALID_CACHE(cache)); - REQUIRE(cache->references == 0); + REQUIRE(isc_refcount_current(&cache->references) == 0); isc_mem_setwater(cache->mctx, NULL, NULL, 0, 0); @@ -402,9 +403,7 @@ dns_cache_attach(dns_cache_t *cache, dns_cache_t **targetp) { REQUIRE(VALID_CACHE(cache)); REQUIRE(targetp != NULL && *targetp == NULL); - LOCK(&cache->lock); - cache->references++; - UNLOCK(&cache->lock); + isc_refcount_increment(&cache->references); *targetp = cache; } @@ -417,18 +416,12 @@ dns_cache_detach(dns_cache_t **cachep) { REQUIRE(cachep != NULL); cache = *cachep; REQUIRE(VALID_CACHE(cache)); - - LOCK(&cache->lock); - REQUIRE(cache->references > 0); - cache->references--; - if (cache->references == 0) { - cache->cleaner.overmem = false; - free_cache = true; - } - *cachep = NULL; - if (free_cache) { + if (isc_refcount_decrement(&cache->references) == 1) { + LOCK(&cache->lock); + free_cache = true; + cache->cleaner.overmem = false; /* * When the cache is shut down, dump it to a file if one is * specified. @@ -447,12 +440,13 @@ dns_cache_detach(dns_cache_t **cachep) { isc_task_shutdown(cache->cleaner.task); free_cache = false; } + UNLOCK(&cache->lock); } - UNLOCK(&cache->lock); - if (free_cache) + if (free_cache) { cache_free(cache); + } } void @@ -1030,8 +1024,9 @@ cleaner_shutdown_action(isc_task_t *task, isc_event_t *event) { cache->live_tasks--; INSIST(cache->live_tasks == 0); - if (cache->references == 0) + if (isc_refcount_current(&cache->references) == 0) { should_free = true; + } /* Make sure we don't reschedule anymore. */ (void)isc_task_purge(task, NULL, DNS_EVENT_CACHECLEAN, NULL);