Improve isc_refcount with initializer and implicit destroy

Add ISC_REFCOUNT_INITIALIZER(x) macro and implicitly call
isc_refcount_destroy() in the ISC_REFCOUNT_IMPL() macros
to reduce code duplicities.
This commit is contained in:
Ondřej Surý 2023-09-22 13:57:26 +02:00
parent 6adc0be124
commit 3340c82b99
No known key found for this signature in database
GPG key ID: 2820F37E873DEA41
17 changed files with 5 additions and 36 deletions

View file

@ -173,8 +173,6 @@ free_listener(controllistener_t *listener) {
INSIST(listener->exiting);
INSIST(ISC_LIST_EMPTY(listener->connections));
isc_refcount_destroy(&listener->references);
REQUIRE(listener->sock == NULL);
free_controlkeylist(&listener->keys, listener->mctx);

View file

@ -1179,7 +1179,6 @@ destroy_adbentry(dns_adbentry_t *entry) {
}
isc_mutex_destroy(&entry->lock);
isc_refcount_destroy(&entry->references);
isc_mem_put(adb->mctx, entry, sizeof(*entry));
dec_adbstats(adb, dns_adbstats_entriescnt);
@ -1963,7 +1962,6 @@ destroy(dns_adb_t *adb) {
isc_mem_detach(&adb->hmctx);
isc_mutex_destroy(&adb->lock);
isc_refcount_destroy(&adb->references);
isc_stats_detach(&adb->stats);
dns_resolver_detach(&adb->res);

View file

@ -1020,7 +1020,6 @@ dns__catz_zone_destroy(dns_catz_zone_t *catz) {
dns_catz_options_free(&catz->zoneoptions, mctx);
dns_catz_zones_detach(&catz->catzs);
isc_refcount_destroy(&catz->references);
isc_mem_put(mctx, catz, sizeof(*catz));
}
@ -1032,7 +1031,6 @@ dns__catz_zones_destroy(dns_catz_zones_t *catzs) {
catzs->magic = 0;
isc_mutex_destroy(&catzs->lock);
isc_refcount_destroy(&catzs->references);
isc_mem_putanddetach(&catzs->mctx, catzs, sizeof(*catzs));
}

View file

@ -414,8 +414,6 @@ dispentry_destroy(dns_dispentry_t *resp) {
INSIST(disp->requests > 0);
disp->requests--;
isc_refcount_destroy(&resp->references);
resp->magic = 0;
INSIST(!ISC_LINK_LINKED(resp, plink));
@ -1363,7 +1361,6 @@ dispatch_destroy(dns_dispatch_t *disp) {
dns_dispatchmgr_t *mgr = disp->mgr;
uint32_t tid = isc_tid();
isc_refcount_destroy(&disp->references);
disp->magic = 0;
if (disp->socktype == isc_socktype_tcp) {

View file

@ -104,7 +104,6 @@ static void
destroy_keynode(dns_keynode_t *knode) {
dns_rdata_t *rdata = NULL;
isc_refcount_destroy(&knode->references);
isc_rwlock_destroy(&knode->rwlock);
if (knode->dslist != NULL) {
for (rdata = ISC_LIST_HEAD(knode->dslist->rdata); rdata != NULL;
@ -163,7 +162,6 @@ destroy_keytable(dns_keytable_t *keytable) {
dns_qpread_destroy(keytable->table, &qpr);
dns_qpmulti_destroy(&keytable->table);
isc_refcount_destroy(&keytable->references);
isc_mem_putanddetach(&keytable->mctx, keytable, sizeof(*keytable));
}

View file

@ -66,7 +66,6 @@ static dns_qpmethods_t qpmethods = {
static void
destroy_ntnode(dns_ntnode_t *node) {
isc_refcount_destroy(&node->references);
if (node->bits != NULL) {
isc_mem_cput(node->mctx, node->bits, node->bits[0],
sizeof(char));
@ -120,7 +119,6 @@ destroy_nametree(dns_nametree_t *nametree) {
/* dns_qpread_destroy(nametree->table, &qpr); */
dns_qpmulti_destroy(&nametree->table);
isc_refcount_destroy(&nametree->references);
isc_mem_putanddetach(&nametree->mctx, nametree, sizeof(*nametree));
}

View file

@ -93,7 +93,6 @@ dns__nta_destroy(dns__nta_t *nta) {
REQUIRE(nta->timer == NULL);
nta->magic = 0;
isc_refcount_destroy(&nta->references);
if (dns_rdataset_isassociated(&nta->rdataset)) {
dns_rdataset_disassociate(&nta->rdataset);
}
@ -140,7 +139,6 @@ dns_ntatable_create(dns_view_t *view, isc_loopmgr_t *loopmgr,
static void
dns__ntatable_destroy(dns_ntatable_t *ntatable) {
isc_refcount_destroy(&ntatable->references);
ntatable->magic = 0;
isc_rwlock_destroy(&ntatable->rwlock);
dns_qpmulti_destroy(&ntatable->table);

View file

@ -227,8 +227,6 @@ requestmgr_destroy(dns_requestmgr_t *requestmgr) {
INSIST(atomic_load(&requestmgr->shuttingdown));
isc_refcount_destroy(&requestmgr->references);
size_t nloops = isc_loopmgr_nloops(requestmgr->loopmgr);
for (size_t i = 0; i < nloops; i++) {
INSIST(ISC_LIST_EMPTY(requestmgr->requests[i]));
@ -973,8 +971,6 @@ req_destroy(dns_request_t *request) {
req_log(ISC_LOG_DEBUG(3), "%s: request %p", __func__, request);
isc_refcount_destroy(&request->references);
/*
* These should have been cleaned up before the
* completion event was sent.

View file

@ -998,8 +998,6 @@ resquery_destroy(resquery_t *query) {
dns_dispatch_detach(&query->dispatch);
}
isc_refcount_destroy(&query->references);
LOCK(&fctx->lock);
atomic_fetch_sub_release(&fctx->nqueries, 1);
UNLOCK(&fctx->lock);
@ -4309,8 +4307,6 @@ fctx_destroy(fetchctx_t *fctx) {
FCTXTRACE("destroy");
isc_refcount_destroy(&fctx->references);
fctx->magic = 0;
res = fctx->res;
@ -9850,7 +9846,6 @@ static void
dns_resolver__destroy(dns_resolver_t *res) {
alternate_t *a = NULL;
isc_refcount_destroy(&res->references);
REQUIRE(!atomic_load_acquire(&res->priming));
REQUIRE(res->primefetch == NULL);

View file

@ -2088,8 +2088,6 @@ static void
dns__rpz_zones_destroy(dns_rpz_zones_t *rpzs) {
REQUIRE(rpzs->shuttingdown);
isc_refcount_destroy(&rpzs->references);
for (dns_rpz_num_t rpz_num = 0; rpz_num < DNS_RPZ_MAX_ZONES; ++rpz_num)
{
if (rpzs->zones[rpz_num] == NULL) {

View file

@ -332,7 +332,6 @@ destroyring(dns_tsigkeyring_t *ring) {
ring->magic = 0;
isc_refcount_destroy(&ring->references);
isc_rwlock_destroy(&ring->lock);
isc_mem_putanddetach(&ring->mctx, ring, sizeof(dns_tsigkeyring_t));
}

View file

@ -1859,7 +1859,6 @@ xfrin_destroy(dns_xfrin_t *xfr) {
/* Safe-guards */
REQUIRE(atomic_load(&xfr->shuttingdown));
isc_refcount_destroy(&xfr->references);
INSIST(xfr->shutdown_result != ISC_R_UNSET);

View file

@ -5601,8 +5601,6 @@ closeversion:
static void
zone_destroy(dns_zone_t *zone) {
isc_refcount_destroy(&zone->references);
/*
* Stop things being restarted after we cancel them below.
*/

View file

@ -34,6 +34,8 @@ ISC_LANG_BEGINDECLS
typedef atomic_uint_fast32_t isc_refcount_t;
#define ISC_REFCOUNT_INITIALIZER(a) (a)
/** \def isc_refcount_init(ref, n)
* \brief Initialize the reference counter.
* \param[in] ref pointer to reference counter.
@ -145,6 +147,7 @@ typedef atomic_uint_fast32_t isc_refcount_t;
uint_fast32_t refs = \
isc_refcount_decrement(&ptr->references) - 1; \
if (refs == 0) { \
isc_refcount_destroy(&ptr->references); \
destroy(ptr); \
} \
fprintf(stderr, \
@ -170,6 +173,7 @@ typedef atomic_uint_fast32_t isc_refcount_t;
uint_fast32_t refs = \
isc_refcount_decrement(&ptr->references) - 1; \
if (refs == 0) { \
isc_refcount_destroy(&ptr->references); \
destroy(ptr); \
} \
fprintf(stderr, \
@ -193,6 +197,7 @@ typedef atomic_uint_fast32_t isc_refcount_t;
void name##_unref(name##_t *ptr) { \
REQUIRE(ptr != NULL); \
if (isc_refcount_decrement(&ptr->references) == 1) { \
isc_refcount_destroy(&ptr->references); \
destroy(ptr); \
} \
} \

View file

@ -880,8 +880,6 @@ isc_nmhandle_is_stream(isc_nmhandle_t *handle) {
static void
nmhandle_free(isc_nmsocket_t *sock, isc_nmhandle_t *handle) {
isc_refcount_destroy(&handle->references);
handle->magic = 0;
if (handle->dofree != NULL) {

View file

@ -313,8 +313,6 @@ isc_ratelimiter_shutdown(isc_ratelimiter_t *restrict rl) {
static void
ratelimiter_destroy(isc_ratelimiter_t *restrict rl) {
isc_refcount_destroy(&rl->references);
LOCK(&rl->lock);
REQUIRE(rl->state == isc_ratelimiter_shuttingdown);
UNLOCK(&rl->lock);

View file

@ -2383,8 +2383,6 @@ clientmgr_destroy_cb(void *arg) {
manager->magic = 0;
isc_refcount_destroy(&manager->references);
isc_loop_detach(&manager->loop);
dns_aclenv_detach(&manager->aclenv);