diff --git a/bin/named/server.c b/bin/named/server.c index b7fbccdfec..06c98abb17 100644 --- a/bin/named/server.c +++ b/bin/named/server.c @@ -9143,7 +9143,6 @@ view_loaded(void *arg) { ns_zoneload_t *zl = (ns_zoneload_t *) arg; named_server_t *server = zl->server; bool reconfig = zl->reconfig; - unsigned int refs; /* @@ -9154,35 +9153,34 @@ view_loaded(void *arg) { * We use the zoneload reference counter to let us * know when all views are finished. */ - isc_refcount_decrement(&zl->refs, &refs); - if (refs != 0) - return (ISC_R_SUCCESS); + if (isc_refcount_decrement(&zl->refs) == 1) { - isc_refcount_destroy(&zl->refs); - isc_mem_put(server->mctx, zl, sizeof (*zl)); + isc_refcount_destroy(&zl->refs); + isc_mem_put(server->mctx, zl, sizeof (*zl)); - /* - * To maintain compatibility with log parsing tools that might - * be looking for this string after "rndc reconfig", we keep it - * as it is - */ - if (reconfig) { + /* + * To maintain compatibility with log parsing tools that might + * be looking for this string after "rndc reconfig", we keep it + * as it is + */ + if (reconfig) { + isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL, + NAMED_LOGMODULE_SERVER, ISC_LOG_INFO, + "any newly configured zones are now loaded"); + } else { + isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL, + NAMED_LOGMODULE_SERVER, ISC_LOG_NOTICE, + "all zones loaded"); + } + + CHECKFATAL(dns_zonemgr_forcemaint(server->zonemgr), + "forcing zone maintenance"); + + named_os_started(); isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL, - NAMED_LOGMODULE_SERVER, ISC_LOG_INFO, - "any newly configured zones are now loaded"); - } else { - isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL, - NAMED_LOGMODULE_SERVER, ISC_LOG_NOTICE, - "all zones loaded"); + NAMED_LOGMODULE_SERVER, ISC_LOG_NOTICE, "running"); } - CHECKFATAL(dns_zonemgr_forcemaint(server->zonemgr), - "forcing zone maintenance"); - - named_os_started(); - isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL, - NAMED_LOGMODULE_SERVER, ISC_LOG_NOTICE, "running"); - return (ISC_R_SUCCESS); } @@ -9191,7 +9189,6 @@ load_zones(named_server_t *server, bool init, bool reconfig) { isc_result_t result; dns_view_t *view; ns_zoneload_t *zl; - unsigned int refs = 0; zl = isc_mem_get(server->mctx, sizeof (*zl)); if (zl == NULL) @@ -9230,13 +9227,12 @@ load_zones(named_server_t *server, bool init, bool reconfig) { * 'dns_view_asyncload' calls view_loaded if there are no * zones. */ - isc_refcount_increment(&zl->refs, NULL); + isc_refcount_increment(&zl->refs); CHECK(dns_view_asyncload(view, view_loaded, zl)); } cleanup: - isc_refcount_decrement(&zl->refs, &refs); - if (refs == 0) { + if (isc_refcount_decrement(&zl->refs) == 1) { isc_refcount_destroy(&zl->refs); isc_mem_put(server->mctx, zl, sizeof (*zl)); } else if (init) { diff --git a/bin/tests/system/dyndb/driver/db.c b/bin/tests/system/dyndb/driver/db.c index e9bdf27a25..8df5154d65 100644 --- a/bin/tests/system/dyndb/driver/db.c +++ b/bin/tests/system/dyndb/driver/db.c @@ -72,7 +72,7 @@ attach(dns_db_t *source, dns_db_t **targetp) { REQUIRE(VALID_SAMPLEDB(sampledb)); - isc_refcount_increment(&sampledb->refs, NULL); + isc_refcount_increment(&sampledb->refs); *targetp = source; } @@ -88,13 +88,12 @@ free_sampledb(sampledb_t *sampledb) { static void detach(dns_db_t **dbp) { sampledb_t *sampledb = (sampledb_t *)(*dbp); - unsigned int refs; REQUIRE(VALID_SAMPLEDB(sampledb)); - isc_refcount_decrement(&sampledb->refs, &refs); - if (refs == 0) + if (isc_refcount_decrement(&sampledb->refs) == 1) { free_sampledb(sampledb); + } *dbp = NULL; } diff --git a/lib/dns/acl.c b/lib/dns/acl.c index f2f5532aaf..2502a14f3f 100644 --- a/lib/dns/acl.c +++ b/lib/dns/acl.c @@ -450,7 +450,7 @@ void dns_acl_attach(dns_acl_t *source, dns_acl_t **target) { REQUIRE(DNS_ACL_VALID(source)); - isc_refcount_increment(&source->refcount, NULL); + isc_refcount_increment(&source->refcount); *target = source; } @@ -483,13 +483,13 @@ destroy(dns_acl_t *dacl) { void dns_acl_detach(dns_acl_t **aclp) { dns_acl_t *acl = *aclp; - unsigned int refs; REQUIRE(DNS_ACL_VALID(acl)); - isc_refcount_decrement(&acl->refcount, &refs); - if (refs == 0) + if (isc_refcount_decrement(&acl->refcount) == 1) { destroy(acl); + } + *aclp = NULL; } diff --git a/lib/dns/catz.c b/lib/dns/catz.c index d112284700..82c6a70646 100644 --- a/lib/dns/catz.c +++ b/lib/dns/catz.c @@ -233,7 +233,7 @@ dns_catz_entry_copy(dns_catz_zone_t *zone, const dns_catz_entry_t *entry, void dns_catz_entry_attach(dns_catz_entry_t *entry, dns_catz_entry_t **entryp) { REQUIRE(entryp != NULL && *entryp == NULL); - isc_refcount_increment(&entry->refs, NULL); + isc_refcount_increment(&entry->refs); *entryp = entry; } @@ -241,23 +241,22 @@ void dns_catz_entry_detach(dns_catz_zone_t *zone, dns_catz_entry_t **entryp) { dns_catz_entry_t *entry; isc_mem_t *mctx; - unsigned int refs; REQUIRE(entryp != NULL && *entryp != NULL); entry = *entryp; - *entryp = NULL; mctx = zone->catzs->mctx; - isc_refcount_decrement(&entry->refs, &refs); - if (refs == 0) { + if (isc_refcount_decrement(&entry->refs) == 1) { dns_catz_options_free(&entry->opts, mctx); if (dns_name_dynamic(&entry->name)) dns_name_free(&entry->name, mctx); isc_refcount_destroy(&entry->refs); isc_mem_put(mctx, entry, sizeof(dns_catz_entry_t)); } + + *entryp = NULL; } bool @@ -728,7 +727,7 @@ void dns_catz_catzs_attach(dns_catz_zones_t *catzs, dns_catz_zones_t **catzsp) { REQUIRE(catzsp != NULL && *catzsp == NULL); - isc_refcount_increment(&catzs->refs, NULL); + isc_refcount_increment(&catzs->refs); *catzsp = catzs; } @@ -736,7 +735,7 @@ void dns_catz_zone_attach(dns_catz_zone_t *zone, dns_catz_zone_t **zonep) { REQUIRE(zonep != NULL && *zonep == NULL); - isc_refcount_increment(&zone->refs, NULL); + isc_refcount_increment(&zone->refs); *zonep = zone; } @@ -746,14 +745,13 @@ dns_catz_zone_detach(dns_catz_zone_t **zonep) { dns_catz_zone_t *zone; isc_ht_iter_t *iter = NULL; isc_mem_t *mctx; - unsigned int refs; REQUIRE(zonep != NULL && *zonep != NULL); zone = *zonep; - *zonep = NULL; - isc_refcount_decrement(&zone->refs, &refs); - if (refs == 0) { + + if (isc_refcount_decrement(&zone->refs) == 1) { + isc_refcount_destroy(&zone->refs); if (zone->entries != NULL) { result = isc_ht_iter_create(zone->entries, &iter); INSIST(result == ISC_R_SUCCESS); @@ -775,7 +773,6 @@ dns_catz_zone_detach(dns_catz_zone_t **zonep) { } mctx = zone->catzs->mctx; isc_timer_detach(&zone->updatetimer); - isc_refcount_destroy(&zone->refs); if (zone->db_registered == true) { result = dns_db_updatenotify_unregister(zone->db, dns_catz_dbupdate_callback, @@ -795,6 +792,8 @@ dns_catz_zone_detach(dns_catz_zone_t **zonep) { zone->catzs = NULL; isc_mem_put(mctx, zone, sizeof(dns_catz_zone_t)); } + + *zonep = NULL; } void @@ -802,7 +801,6 @@ dns_catz_catzs_detach(dns_catz_zones_t ** catzsp) { dns_catz_zones_t *catzs; isc_ht_iter_t *iter = NULL; isc_result_t result; - unsigned int refs; dns_catz_zone_t *zone; @@ -811,9 +809,8 @@ dns_catz_catzs_detach(dns_catz_zones_t ** catzsp) { REQUIRE(catzs != NULL); *catzsp = NULL; - isc_refcount_decrement(&catzs->refs, &refs); - - if (refs == 0) { + if (isc_refcount_decrement(&catzs->refs) == 1) { + isc_refcount_destroy(&catzs->refs); DESTROYLOCK(&catzs->lock); if (catzs->zones != NULL) { result = isc_ht_iter_create(catzs->zones, &iter); @@ -830,7 +827,6 @@ dns_catz_catzs_detach(dns_catz_zones_t ** catzsp) { INSIST(isc_ht_count(catzs->zones) == 0); isc_ht_destroy(&catzs->zones); } - isc_refcount_destroy(&catzs->refs); isc_task_destroy(&catzs->updater); isc_mem_putanddetach(&catzs->mctx, catzs, sizeof(*catzs)); } diff --git a/lib/dns/dnstap.c b/lib/dns/dnstap.c index 2fdc43c230..e50ad13435 100644 --- a/lib/dns/dnstap.c +++ b/lib/dns/dnstap.c @@ -531,7 +531,7 @@ dns_dt_attach(dns_dtenv_t *source, dns_dtenv_t **destp) { REQUIRE(VALID_DTENV(source)); REQUIRE(destp != NULL && *destp == NULL); - isc_refcount_increment(&source->refcount, NULL); + isc_refcount_increment(&source->refcount); *destp = source; } @@ -579,17 +579,17 @@ destroy(dns_dtenv_t *env) { void dns_dt_detach(dns_dtenv_t **envp) { - unsigned int refs; dns_dtenv_t *env; REQUIRE(envp != NULL && VALID_DTENV(*envp)); env = *envp; - *envp = NULL; - isc_refcount_decrement(&env->refcount, &refs); - if (refs == 0) + if (isc_refcount_decrement(&env->refcount) == 1) { destroy(env); + } + + *envp = NULL; } static isc_result_t diff --git a/lib/dns/dst_api.c b/lib/dns/dst_api.c index e4b5220def..2407c49504 100644 --- a/lib/dns/dst_api.c +++ b/lib/dns/dst_api.c @@ -1124,7 +1124,7 @@ dst_key_attach(dst_key_t *source, dst_key_t **target) { REQUIRE(target != NULL && *target == NULL); REQUIRE(VALID_KEY(source)); - isc_refcount_increment(&source->refs, NULL); + isc_refcount_increment(&source->refs); *target = source; } @@ -1132,7 +1132,6 @@ void dst_key_free(dst_key_t **keyp) { isc_mem_t *mctx; dst_key_t *key; - unsigned int refs; REQUIRE(dst_initialized == true); REQUIRE(keyp != NULL && VALID_KEY(*keyp)); @@ -1140,26 +1139,24 @@ dst_key_free(dst_key_t **keyp) { key = *keyp; mctx = key->mctx; - isc_refcount_decrement(&key->refs, &refs); - if (refs != 0) - return; - - isc_refcount_destroy(&key->refs); - if (key->keydata.generic != NULL) { - INSIST(key->func->destroy != NULL); - key->func->destroy(key); + if (isc_refcount_decrement(&key->refs) == 1) { + isc_refcount_destroy(&key->refs); + if (key->keydata.generic != NULL) { + INSIST(key->func->destroy != NULL); + key->func->destroy(key); + } + if (key->engine != NULL) + isc_mem_free(mctx, key->engine); + if (key->label != NULL) + isc_mem_free(mctx, key->label); + dns_name_free(key->key_name, mctx); + isc_mem_put(mctx, key->key_name, sizeof(dns_name_t)); + if (key->key_tkeytoken) { + isc_buffer_free(&key->key_tkeytoken); + } + isc_safe_memwipe(key, sizeof(*key)); + isc_mem_putanddetach(&mctx, key, sizeof(*key)); } - if (key->engine != NULL) - isc_mem_free(mctx, key->engine); - if (key->label != NULL) - isc_mem_free(mctx, key->label); - dns_name_free(key->key_name, mctx); - isc_mem_put(mctx, key->key_name, sizeof(dns_name_t)); - if (key->key_tkeytoken) { - isc_buffer_free(&key->key_tkeytoken); - } - isc_safe_memwipe(key, sizeof(*key)); - isc_mem_putanddetach(&mctx, key, sizeof(*key)); *keyp = NULL; } diff --git a/lib/dns/include/dns/rbt.h b/lib/dns/include/dns/rbt.h index ffd2445bd7..c7f6a7c992 100644 --- a/lib/dns/include/dns/rbt.h +++ b/lib/dns/include/dns/rbt.h @@ -1041,36 +1041,6 @@ dns_rbtnodechain_nextflat(dns_rbtnodechain_t *chain, dns_name_t *name); * Find the next node at the current depth in DNSSEC order. */ -/* - * Wrapper macros for manipulating the rbtnode reference counter: - * Since we selectively use isc_refcount_t for the reference counter of - * a rbtnode, operations on the counter depend on the actual type of it. - * The following macros provide a common interface to these operations, - * hiding the back-end. The usage is the same as that of isc_refcount_xxx(). - */ -#define dns_rbtnode_refinit(node, n) \ - do { \ - isc_refcount_init(&(node)->references, (n)); \ - } while (0) -#define dns_rbtnode_refdestroy(node) \ - do { \ - isc_refcount_destroy(&(node)->references); \ - } while (0) -#define dns_rbtnode_refcurrent(node) \ - isc_refcount_current(&(node)->references) -#define dns_rbtnode_refincrement0(node, refs) \ - do { \ - isc_refcount_increment0(&(node)->references, (refs)); \ - } while (0) -#define dns_rbtnode_refincrement(node, refs) \ - do { \ - isc_refcount_increment(&(node)->references, (refs)); \ - } while (0) -#define dns_rbtnode_refdecrement(node, refs) \ - do { \ - isc_refcount_decrement(&(node)->references, (refs)); \ - } while (0) - void dns_rbtnode_nodename(dns_rbtnode_t *node, dns_name_t *name); diff --git a/lib/dns/iptable.c b/lib/dns/iptable.c index 5850de25fc..daaf3f85a3 100644 --- a/lib/dns/iptable.c +++ b/lib/dns/iptable.c @@ -142,18 +142,19 @@ dns_iptable_merge(dns_iptable_t *tab, dns_iptable_t *source, bool pos) void dns_iptable_attach(dns_iptable_t *source, dns_iptable_t **target) { REQUIRE(DNS_IPTABLE_VALID(source)); - isc_refcount_increment(&source->refcount, NULL); + isc_refcount_increment(&source->refcount); *target = source; } void dns_iptable_detach(dns_iptable_t **tabp) { dns_iptable_t *tab = *tabp; - unsigned int refs; REQUIRE(DNS_IPTABLE_VALID(tab)); - isc_refcount_decrement(&tab->refcount, &refs); - if (refs == 0) + + if (isc_refcount_decrement(&tab->refcount) == 1) { destroy_iptable(tab); + } + *tabp = NULL; } diff --git a/lib/dns/keytable.c b/lib/dns/keytable.c index 75cbf4e1f3..dfe7d35c2b 100644 --- a/lib/dns/keytable.c +++ b/lib/dns/keytable.c @@ -118,7 +118,7 @@ dns_keytable_attach(dns_keytable_t *source, dns_keytable_t **targetp) { REQUIRE(VALID_KEYTABLE(source)); REQUIRE(targetp != NULL && *targetp == NULL); - isc_refcount_increment(&source->references, NULL); + isc_refcount_increment(&source->references); *targetp = source; } @@ -126,7 +126,6 @@ dns_keytable_attach(dns_keytable_t *source, dns_keytable_t **targetp) { void dns_keytable_detach(dns_keytable_t **keytablep) { dns_keytable_t *keytable; - unsigned int refs; /* * Detach *keytablep from its keytable. @@ -135,10 +134,8 @@ dns_keytable_detach(dns_keytable_t **keytablep) { REQUIRE(keytablep != NULL && VALID_KEYTABLE(*keytablep)); keytable = *keytablep; - *keytablep = NULL; - isc_refcount_decrement(&keytable->references, &refs); - if (refs == 0) { + if (isc_refcount_decrement(&keytable->references) == 1) { INSIST(isc_refcount_current(&keytable->active_nodes) == 0); isc_refcount_destroy(&keytable->active_nodes); isc_refcount_destroy(&keytable->references); @@ -148,6 +145,7 @@ dns_keytable_detach(dns_keytable_t **keytablep) { isc_mem_putanddetach(&keytable->mctx, keytable, sizeof(*keytable)); } + *keytablep = NULL; } /*% @@ -411,7 +409,7 @@ dns_keytable_find(dns_keytable_t *keytable, const dns_name_t *keyname, DNS_RBTFIND_NOOPTIONS, NULL, NULL); if (result == ISC_R_SUCCESS) { if (node->data != NULL) { - isc_refcount_increment0(&keytable->active_nodes, NULL); + isc_refcount_increment0(&keytable->active_nodes); dns_keynode_attach(node->data, keynodep); } else result = ISC_R_NOTFOUND; @@ -439,7 +437,7 @@ dns_keytable_nextkeynode(dns_keytable_t *keytable, dns_keynode_t *keynode, return (ISC_R_NOTFOUND); dns_keynode_attach(keynode->next, nextnodep); - isc_refcount_increment(&keytable->active_nodes, NULL); + isc_refcount_increment(&keytable->active_nodes); return (ISC_R_SUCCESS); } @@ -487,7 +485,7 @@ dns_keytable_findkeynode(dns_keytable_t *keytable, const dns_name_t *name, break; } if (knode != NULL) { - isc_refcount_increment0(&keytable->active_nodes, NULL); + isc_refcount_increment0(&keytable->active_nodes); dns_keynode_attach(knode, keynodep); } else result = DNS_R_PARTIALMATCH; @@ -525,7 +523,7 @@ dns_keytable_findnextkeynode(dns_keytable_t *keytable, dns_keynode_t *keynode, break; } if (knode != NULL) { - isc_refcount_increment(&keytable->active_nodes, NULL); + isc_refcount_increment(&keytable->active_nodes); result = ISC_R_SUCCESS; dns_keynode_attach(knode, nextnodep); } else @@ -574,7 +572,7 @@ dns_keytable_attachkeynode(dns_keytable_t *keytable, dns_keynode_t *source, REQUIRE(VALID_KEYNODE(source)); REQUIRE(target != NULL && *target == NULL); - isc_refcount_increment(&keytable->active_nodes, NULL); + isc_refcount_increment(&keytable->active_nodes); dns_keynode_attach(source, target); } @@ -589,7 +587,7 @@ dns_keytable_detachkeynode(dns_keytable_t *keytable, dns_keynode_t **keynodep) REQUIRE(VALID_KEYTABLE(keytable)); REQUIRE(keynodep != NULL && VALID_KEYNODE(*keynodep)); - isc_refcount_decrement(&keytable->active_nodes, NULL); + (void)isc_refcount_decrement(&keytable->active_nodes); dns_keynode_detach(keytable->mctx, keynodep); } @@ -735,7 +733,7 @@ dns_keytable_forall(dns_keytable_t *keytable, result = ISC_R_SUCCESS; goto cleanup; } - isc_refcount_increment0(&keytable->active_nodes, NULL); + isc_refcount_increment0(&keytable->active_nodes); for (;;) { dns_rbtnodechain_current(&chain, NULL, NULL, &node); if (node->data != NULL) @@ -747,7 +745,7 @@ dns_keytable_forall(dns_keytable_t *keytable, break; } } - isc_refcount_decrement(&keytable->active_nodes, NULL); + (void)isc_refcount_decrement(&keytable->active_nodes); cleanup: dns_rbtnodechain_invalidate(&chain); @@ -808,17 +806,15 @@ dns_keynode_create(isc_mem_t *mctx, dns_keynode_t **target) { void dns_keynode_attach(dns_keynode_t *source, dns_keynode_t **target) { REQUIRE(VALID_KEYNODE(source)); - isc_refcount_increment(&source->refcount, NULL); + isc_refcount_increment(&source->refcount); *target = source; } void dns_keynode_detach(isc_mem_t *mctx, dns_keynode_t **keynode) { - unsigned int refs; dns_keynode_t *node = *keynode; REQUIRE(VALID_KEYNODE(node)); - isc_refcount_decrement(&node->refcount, &refs); - if (refs == 0) { + if (isc_refcount_decrement(&node->refcount) == 1) { if (node->key != NULL) dst_key_free(&node->key); isc_refcount_destroy(&node->refcount); diff --git a/lib/dns/nta.c b/lib/dns/nta.c index b1299d5de5..8f32ae6229 100644 --- a/lib/dns/nta.c +++ b/lib/dns/nta.c @@ -61,19 +61,16 @@ struct dns_nta { */ static void nta_ref(dns_nta_t *nta) { - isc_refcount_increment(&nta->refcount, NULL); + isc_refcount_increment(&nta->refcount); } static void nta_detach(isc_mem_t *mctx, dns_nta_t **ntap) { - unsigned int refs; dns_nta_t *nta = *ntap; REQUIRE(VALID_NTA(nta)); - *ntap = NULL; - isc_refcount_decrement(&nta->refcount, &refs); - if (refs == 0) { + if (isc_refcount_decrement(&nta->refcount) == 1) { nta->magic = 0; if (nta->timer != NULL) { (void) isc_timer_reset(nta->timer, @@ -92,6 +89,7 @@ nta_detach(isc_mem_t *mctx, dns_nta_t **ntap) { } isc_mem_put(mctx, nta, sizeof(dns_nta_t)); } + *ntap = NULL; } static void diff --git a/lib/dns/order.c b/lib/dns/order.c index f3fb3c51e9..26563af8b3 100644 --- a/lib/dns/order.c +++ b/lib/dns/order.c @@ -128,7 +128,7 @@ void dns_order_attach(dns_order_t *source, dns_order_t **target) { REQUIRE(DNS_ORDER_VALID(source)); REQUIRE(target != NULL && *target == NULL); - isc_refcount_increment(&source->references, NULL); + isc_refcount_increment(&source->references); *target = source; } @@ -136,21 +136,20 @@ void dns_order_detach(dns_order_t **orderp) { dns_order_t *order; dns_order_ent_t *ent; - unsigned int references; REQUIRE(orderp != NULL); order = *orderp; REQUIRE(DNS_ORDER_VALID(order)); - isc_refcount_decrement(&order->references, &references); - *orderp = NULL; - if (references != 0) - return; - order->magic = 0; - while ((ent = ISC_LIST_HEAD(order->ents)) != NULL) { - ISC_LIST_UNLINK(order->ents, ent, link); - isc_mem_put(order->mctx, ent, sizeof(*ent)); + if (isc_refcount_decrement(&order->references) == 1) { + order->magic = 0; + while ((ent = ISC_LIST_HEAD(order->ents)) != NULL) { + ISC_LIST_UNLINK(order->ents, ent, link); + isc_mem_put(order->mctx, ent, sizeof(*ent)); + } + isc_refcount_destroy(&order->references); + isc_mem_putanddetach(&order->mctx, order, sizeof(*order)); } - isc_refcount_destroy(&order->references); - isc_mem_putanddetach(&order->mctx, order, sizeof(*order)); + + *orderp = NULL; } diff --git a/lib/dns/portlist.c b/lib/dns/portlist.c index 0a31ea30c8..92315b7a3e 100644 --- a/lib/dns/portlist.c +++ b/lib/dns/portlist.c @@ -228,21 +228,19 @@ dns_portlist_attach(dns_portlist_t *portlist, dns_portlist_t **portlistp) { REQUIRE(DNS_VALID_PORTLIST(portlist)); REQUIRE(portlistp != NULL && *portlistp == NULL); - isc_refcount_increment(&portlist->refcount, NULL); + isc_refcount_increment(&portlist->refcount); *portlistp = portlist; } void dns_portlist_detach(dns_portlist_t **portlistp) { dns_portlist_t *portlist; - unsigned int count; REQUIRE(portlistp != NULL); portlist = *portlistp; REQUIRE(DNS_VALID_PORTLIST(portlist)); - *portlistp = NULL; - isc_refcount_decrement(&portlist->refcount, &count); - if (count == 0) { + + if (isc_refcount_decrement(&portlist->refcount) == 1) { portlist->magic = 0; isc_refcount_destroy(&portlist->refcount); if (portlist->list != NULL) @@ -253,4 +251,5 @@ dns_portlist_detach(dns_portlist_t **portlistp) { isc_mem_putanddetach(&portlist->mctx, portlist, sizeof(*portlist)); } + *portlistp = NULL; } diff --git a/lib/dns/rbt.c b/lib/dns/rbt.c index aee033070c..bb7c46d1eb 100644 --- a/lib/dns/rbt.c +++ b/lib/dns/rbt.c @@ -2105,7 +2105,7 @@ dns_rbt_deletenode(dns_rbt_t *rbt, dns_rbtnode_t *node, bool recurse) #if DNS_RBT_USEMAGIC node->magic = 0; #endif - dns_rbtnode_refdestroy(node); + isc_refcount_destroy(&node->references); freenode(rbt, &node); @@ -2217,7 +2217,7 @@ create_node(isc_mem_t *mctx, const dns_name_t *name, dns_rbtnode_t **nodep) { LOCKNUM(node) = 0; WILD(node) = 0; DIRTY(node) = 0; - dns_rbtnode_refinit(node, 0); + isc_refcount_init(&node->references, 0); node->find_callback = 0; node->nsec = DNS_RBT_NSEC_NORMAL; diff --git a/lib/dns/rbtdb.c b/lib/dns/rbtdb.c index ab21465b61..d34efe2ea7 100644 --- a/lib/dns/rbtdb.c +++ b/lib/dns/rbtdb.c @@ -773,7 +773,7 @@ attach(dns_db_t *source, dns_db_t **targetp) { REQUIRE(VALID_RBTDB(rbtdb)); - isc_refcount_increment(&rbtdb->references, NULL); + isc_refcount_increment(&rbtdb->references); *targetp = source; } @@ -972,11 +972,9 @@ free_rbtdb(dns_rbtdb_t *rbtdb, bool log, isc_event_t *event) { REQUIRE(rbtdb->future_version == NULL); if (rbtdb->current_version != NULL) { - unsigned int refs; - isc_refcount_decrement(&rbtdb->current_version->references, - &refs); - INSIST(refs == 0); + INSIST(isc_refcount_decrement(&rbtdb->current_version->references) == 1); + UNLINK(rbtdb->open_versions, rbtdb->current_version, link); isc_rwlock_destroy(&rbtdb->current_version->glue_rwlock); isc_refcount_destroy(&rbtdb->current_version->references); @@ -1187,14 +1185,12 @@ maybe_free_rbtdb(dns_rbtdb_t *rbtdb) { static void detach(dns_db_t **dbp) { dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)(*dbp); - unsigned int refs; REQUIRE(VALID_RBTDB(rbtdb)); - isc_refcount_decrement(&rbtdb->references, &refs); - - if (refs == 0) + if (isc_refcount_decrement(&rbtdb->references) == 1) { maybe_free_rbtdb(rbtdb); + } *dbp = NULL; } @@ -1203,13 +1199,13 @@ static void currentversion(dns_db_t *db, dns_dbversion_t **versionp) { dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db; rbtdb_version_t *version; - unsigned int refs; REQUIRE(VALID_RBTDB(rbtdb)); + /* XXXOND: Is the lock needed here? */ RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_read); version = rbtdb->current_version; - isc_refcount_increment(&version->references, &refs); + isc_refcount_increment(&version->references); RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_read); *versionp = (dns_dbversion_t *)version; @@ -1332,13 +1328,11 @@ attachversion(dns_db_t *db, dns_dbversion_t *source, { dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db; rbtdb_version_t *rbtversion = source; - unsigned int refs; REQUIRE(VALID_RBTDB(rbtdb)); INSIST(rbtversion != NULL && rbtversion->rbtdb == rbtdb); - isc_refcount_increment(&rbtversion->references, &refs); - INSIST(refs > 1); + isc_refcount_increment(&rbtversion->references); *targetp = rbtversion; } @@ -1348,7 +1342,6 @@ add_changed(dns_rbtdb_t *rbtdb, rbtdb_version_t *version, dns_rbtnode_t *node) { rbtdb_changed_t *changed; - unsigned int refs; /* * Caller must be holding the node lock if its reference must be @@ -1362,8 +1355,7 @@ add_changed(dns_rbtdb_t *rbtdb, rbtdb_version_t *version, REQUIRE(version->writer); if (changed != NULL) { - dns_rbtnode_refincrement(node, &refs); - INSIST(refs != 0); + isc_refcount_increment(&node->references); changed->node = node; changed->dirty = false; ISC_LIST_INITANDAPPEND(version->changed_list, changed, link); @@ -1792,96 +1784,16 @@ delete_node(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node) { } } -#if 0 -static void -clean_now_or_later(dns_rbtnode_t *node, dns_rbtdb_t *rbtdb, - rdatasetheader_t *header, rdatasetheader_t **header_prevp) -{ - if (dns_rbtnode_refcurrent(node) == 0) { - isc_mem_t *mctx; - - /* - * header->down can be non-NULL if the refcount has just - * decremented to 0 but decrement_reference() has not performed - * clean_cache_node(), in which case we need to purge the stale - * headers first. - */ - mctx = rbtdb->common.mctx; - clean_stale_headers(rbtdb, mctx, header); - if (*header_prevp != NULL) - (*header_prevp)->next = header->next; - else - node->data = header->next; - free_rdataset(rbtdb, mctx, header); - } else { - header->attributes |= RDATASET_ATTR_STALE | - RDATASET_ATTR_ANCIENT; - node->dirty = 1; - *header_prevp = header; - } -} - -static rdataset_ttl_t -check_ttl(dns_rbtnode_t *node, rbtdb_search_t *search, - rdatasetheader_t *header, rdatasetheader_t **header_prevp, - nodelock_t *lock, isc_rwlocktype_t *locktype) -{ - dns_rbtdb_t *rbtdb = search->rbtdb; - - if (header->rdh_ttl > search->now) - return rdataset_ttl_fresh; - - /* - * This rdataset is stale, but perhaps still usable. - */ - if (KEEPSTALE(rbtdb) && - header->rdh_ttl + rbtdb->serve_stale_ttl > search->now) { - header->attributes |= RDATASET_ATTR_STALE; - /* Doesn't set dirty because it doesn't need removal. */ - return rdataset_ttl_stale; - } - - /* - * This rdataset is so stale it is no longer usable, even with - * KEEPSTALE. If no one else is using the node, we can clean it up - * right now, otherwise we mark it as ancient, and the node as dirty, - * so it will get cleaned up later. - */ - if ((header->rdh_ttl <= search->now - RBTDB_VIRTUAL) && - (*locktype == isc_rwlocktype_write || - NODE_TRYUPGRADE(lock) == ISC_R_SUCCESS)) { - /* - * We update the node's status only when we can get write - * access; otherwise, we leave others to this work. Periodical - * cleaning will eventually take the job as the last resort. - * We won't downgrade the lock, since other rdatasets are - * probably stale, too. - */ - *locktype = isc_rwlocktype_write; - clean_now_or_later(node, rbtdb, header, header_prevp); - } else - *header_prevp = header; - - return rdataset_ttl_ancient; -} -#endif - /* * Caller must be holding the node lock. */ static inline void new_reference(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node) { - unsigned int lockrefs, noderefs; - isc_refcount_t *lockref; - INSIST(!ISC_LINK_LINKED(node, deadlink)); - dns_rbtnode_refincrement0(node, &noderefs); - if (noderefs == 1) { /* this is the first reference to the node */ - lockref = &rbtdb->node_locks[node->locknum].references; - isc_refcount_increment0(lockref, &lockrefs); - INSIST(lockrefs != 0); + if (isc_refcount_increment(&node->references) == 0) { + /* this is the first reference to the node */ + isc_refcount_increment0(&rbtdb->node_locks[node->locknum].references); } - INSIST(noderefs != 0); } /*% @@ -1905,7 +1817,7 @@ cleanup_dead_nodes(dns_rbtdb_t *rbtdb, int bucketnum) { * Since we're holding a tree write lock, it should be * impossible for this node to be referenced by others. */ - INSIST(dns_rbtnode_refcurrent(node) == 0 && + INSIST(isc_refcount_current(&node->references) == 0 && node->data == NULL); if (node->parent != NULL && @@ -2013,7 +1925,6 @@ decrement_reference(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node, isc_result_t result; bool write_locked; rbtdb_nodelock_t *nodelock; - unsigned int refs, nrefs; int bucket = node->locknum; bool no_reference = true; @@ -2025,13 +1936,12 @@ decrement_reference(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node, /* Handle easy and typical case first. */ if (!node->dirty && KEEP_NODE(node, rbtdb)) { - dns_rbtnode_refdecrement(node, &nrefs); - INSIST((int)nrefs >= 0); - if (nrefs == 0) { - isc_refcount_decrement(&nodelock->references, &refs); - INSIST((int)refs >= 0); + if (isc_refcount_decrement(&node->references) == 1) { + isc_refcount_decrement(&nodelock->references); + return (true); + } else { + return (false); } - return ((nrefs == 0) ? true : false); } /* Upgrade the lock? */ @@ -2040,9 +1950,7 @@ decrement_reference(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node, NODE_WEAKLOCK(&nodelock->lock, isc_rwlocktype_write); } - dns_rbtnode_refdecrement(node, &nrefs); - INSIST((int)nrefs >= 0); - if (nrefs > 0) { + if (isc_refcount_decrement(&node->references) > 1) { /* Restore the lock? */ if (nlock == isc_rwlocktype_read) NODE_WEAKDOWNGRADE(&nodelock->lock); @@ -2090,8 +1998,7 @@ decrement_reference(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node, } else write_locked = true; - isc_refcount_decrement(&nodelock->references, &refs); - INSIST((int)refs >= 0); + isc_refcount_decrement(&nodelock->references); if (KEEP_NODE(node, rbtdb)) goto restore_locks; @@ -2446,7 +2353,6 @@ cleanup_dead_nodes_callback(isc_task_t *task, isc_event_t *event) { dns_rbtdb_t *rbtdb = event->ev_arg; bool again = false; unsigned int locknum; - unsigned int refs; RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_write); for (locknum = 0; locknum < rbtdb->node_lock_count; locknum++) { @@ -2463,9 +2369,9 @@ cleanup_dead_nodes_callback(isc_task_t *task, isc_event_t *event) { isc_task_send(task, &event); else { isc_event_free(&event); - isc_refcount_decrement(&rbtdb->references, &refs); - if (refs == 0) + if (isc_refcount_decrement(&rbtdb->references) == 1) { maybe_free_rbtdb(rbtdb); + } } } @@ -2479,7 +2385,6 @@ closeversion(dns_db_t *db, dns_dbversion_t **versionp, bool commit) { rbtdb_changed_t *changed, *next_changed; rbtdb_serial_t serial, least_serial; dns_rbtnode_t *rbtnode; - unsigned int refs; rdatasetheader_t *header; REQUIRE(VALID_RBTDB(rbtdb)); @@ -2490,8 +2395,8 @@ closeversion(dns_db_t *db, dns_dbversion_t **versionp, bool commit) { ISC_LIST_INIT(cleanup_list); ISC_LIST_INIT(resigned_list); - isc_refcount_decrement(&version->references, &refs); - if (refs > 0) { /* typical and easy case first */ + if (isc_refcount_decrement(&version->references) > 1) { + /* typical and easy case first */ if (commit) { RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_read); INSIST(!version->writer); @@ -2522,9 +2427,8 @@ closeversion(dns_db_t *db, dns_dbversion_t **versionp, bool commit) { * DB itself and unlink it from the open list. */ cur_version = rbtdb->current_version; - isc_refcount_decrement(&cur_version->references, - &cur_ref); - if (cur_ref == 0) { + cur_ref = isc_refcount_decrement(&cur_version->references); + if (cur_ref == 1) { if (cur_version->serial == rbtdb->least_serial) INSIST(EMPTY(cur_version->changed_list)); UNLINK(rbtdb->open_versions, @@ -2558,7 +2462,7 @@ closeversion(dns_db_t *db, dns_dbversion_t **versionp, bool commit) { * isn't being used by anyone, we can clean * it up. */ - if (cur_ref == 0) { + if (cur_ref == 1) { cleanup_version = cur_version; APPENDLIST(version->changed_list, cleanup_version->changed_list, @@ -2579,9 +2483,7 @@ closeversion(dns_db_t *db, dns_dbversion_t **versionp, bool commit) { * case where we need to increment the counter from * zero and need to use isc_refcount_increment0(). */ - isc_refcount_increment0(&version->references, - &cur_ref); - INSIST(cur_ref == 1); + INSIST(isc_refcount_increment0(&version->references) == 0); PREPEND(rbtdb->open_versions, rbtdb->current_version, link); resigned_list = version->resigned_list; @@ -2733,7 +2635,7 @@ closeversion(dns_db_t *db, dns_dbversion_t **versionp, bool commit) { sizeof(*changed)); } if (event != NULL) { - isc_refcount_increment(&rbtdb->references, NULL); + isc_refcount_increment(&rbtdb->references); isc_task_send(rbtdb->task, &event); } else RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write); @@ -4467,7 +4369,7 @@ check_stale_header(dns_rbtnode_t *node, rdatasetheader_t *header, */ *locktype = isc_rwlocktype_write; - if (dns_rbtnode_refcurrent(node) == 0) { + if (isc_refcount_current(&node->references) == 0) { isc_mem_t *mctx; /* @@ -5280,15 +5182,11 @@ static void attachnode(dns_db_t *db, dns_dbnode_t *source, dns_dbnode_t **targetp) { dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db; dns_rbtnode_t *node = (dns_rbtnode_t *)source; - unsigned int refs; REQUIRE(VALID_RBTDB(rbtdb)); REQUIRE(targetp != NULL && *targetp == NULL); - NODE_STRONGLOCK(&rbtdb->node_locks[node->locknum].lock); - dns_rbtnode_refincrement(node, &refs); - INSIST(refs != 0); - NODE_STRONGUNLOCK(&rbtdb->node_locks[node->locknum].lock); + isc_refcount_increment(&node->references); *targetp = source; } @@ -5451,7 +5349,7 @@ printnode(dns_db_t *db, dns_dbnode_t *node, FILE *out) { NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock, isc_rwlocktype_read); - refs = dns_rbtnode_refcurrent(rbtnode); + refs = isc_refcount_current(&rbtnode->references); fprintf(out, "node %p, %" PRIu32 " references, locknum = %u\n", rbtnode, refs, rbtnode->locknum); @@ -5718,7 +5616,6 @@ allrdatasets(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version, dns_rbtnode_t *rbtnode = (dns_rbtnode_t *)node; rbtdb_version_t *rbtversion = version; rbtdb_rdatasetiter_t *iterator; - unsigned int refs; REQUIRE(VALID_RBTDB(rbtdb)); @@ -5734,9 +5631,7 @@ allrdatasets(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version, else { INSIST(rbtversion->rbtdb == rbtdb); - isc_refcount_increment(&rbtversion->references, - &refs); - INSIST(refs > 1); + (void)isc_refcount_increment(&rbtversion->references); } } else { if (now == 0) @@ -5753,8 +5648,7 @@ allrdatasets(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version, NODE_STRONGLOCK(&rbtdb->node_locks[rbtnode->locknum].lock); - dns_rbtnode_refincrement(rbtnode, &refs); - INSIST(refs != 0); + isc_refcount_increment(&rbtnode->references); iterator->current = NULL; @@ -8437,7 +8331,7 @@ dns_rbtdb_create(isc_mem_t *mctx, const dns_name_t *origin, dns_dbtype_t type, rbtdb->next_serial = 2; rbtdb->current_version = allocate_version(mctx, 1, 1, false); if (rbtdb->current_version == NULL) { - isc_refcount_decrement(&rbtdb->references, NULL); + isc_refcount_decrement(&rbtdb->references); free_rbtdb(rbtdb, false, NULL); return (ISC_R_NOMEMORY); } @@ -8458,7 +8352,7 @@ dns_rbtdb_create(isc_mem_t *mctx, const dns_name_t *origin, dns_dbtype_t type, isc_mem_put(mctx, rbtdb->current_version, sizeof(*rbtdb->current_version)); rbtdb->current_version = NULL; - isc_refcount_decrement(&rbtdb->references, NULL); + isc_refcount_decrement(&rbtdb->references); free_rbtdb(rbtdb, false, NULL); return (result); } @@ -9424,13 +9318,8 @@ dbiterator_current(dns_dbiterator_t *iterator, dns_dbnode_t **nodep, * expirenode() currently always returns success. */ if (expire_result == ISC_R_SUCCESS && node->down == NULL) { - unsigned int refs; - rbtdbiter->deletions[rbtdbiter->delcnt++] = node; - NODE_STRONGLOCK(&rbtdb->node_locks[node->locknum].lock); - dns_rbtnode_refincrement(node, &refs); - INSIST(refs != 0); - NODE_STRONGUNLOCK(&rbtdb->node_locks[node->locknum].lock); + isc_refcount_increment(&node->references); } } @@ -9725,7 +9614,7 @@ free_gluetable(rbtdb_version_t *version) { cur = version->glue_table[i]; while (cur != NULL) { cur_next = cur->next; - /* dns_rbtnode_refdecrement(cur->node, NULL); */ + /* isc_refcount_decrement(&cur->node->references); */ cur->node = NULL; free_gluelist(cur->glue_list, rbtdb); cur->glue_list = NULL; @@ -10153,7 +10042,7 @@ no_glue: * when named is terminated by a keyboard break. This doesn't * cleanup the node reference and keeps the process dangling. */ - /* dns_rbtnode_refincrement0(node, NULL); */ + /* isc_refcount_increment0(&node->references); */ cur->node = node; if (ctx.glue_list == NULL) { @@ -10328,7 +10217,7 @@ expire_header(dns_rbtdb_t *rbtdb, rdatasetheader_t *header, * Caller must hold the node (write) lock. */ - if (dns_rbtnode_refcurrent(header->node) == 0) { + if (isc_refcount_current(&header->node->references) == 0) { /* * If no one else is using the node, we can clean it up now. * We first need to gain a new reference to the node to meet a diff --git a/lib/dns/rpz.c b/lib/dns/rpz.c index e6afcbbf4a..030365cd0e 100644 --- a/lib/dns/rpz.c +++ b/lib/dns/rpz.c @@ -1484,7 +1484,7 @@ cleanup_task: dns_rbt_destroy(&zones->rbt); cleanup_rbt: - isc_refcount_decrement(&zones->refs, NULL); + (void)isc_refcount_decrement(&zones->refs); isc_refcount_destroy(&zones->refs); DESTROYLOCK(&zones->maint_lock); @@ -1567,7 +1567,7 @@ cleanup_ht: isc_timer_detach(&zone->updatetimer); cleanup_timer: - isc_refcount_decrement(&zone->refs, NULL); + isc_refcount_decrement(&zone->refs); isc_refcount_destroy(&zone->refs); isc_mem_put(zone->rpzs->mctx, zone, sizeof(*zone)); @@ -2049,51 +2049,51 @@ cidr_free(dns_rpz_zones_t *rpzs) { static void rpz_detach(dns_rpz_zone_t **rpzp, dns_rpz_zones_t *rpzs) { dns_rpz_zone_t *rpz; - unsigned int refs; rpz = *rpzp; + + if (isc_refcount_decrement(&rpz->refs) == 1) { + isc_refcount_destroy(&rpz->refs); + + if (dns_name_dynamic(&rpz->origin)) + dns_name_free(&rpz->origin, rpzs->mctx); + if (dns_name_dynamic(&rpz->client_ip)) + dns_name_free(&rpz->client_ip, rpzs->mctx); + if (dns_name_dynamic(&rpz->ip)) + dns_name_free(&rpz->ip, rpzs->mctx); + if (dns_name_dynamic(&rpz->nsdname)) + dns_name_free(&rpz->nsdname, rpzs->mctx); + if (dns_name_dynamic(&rpz->nsip)) + dns_name_free(&rpz->nsip, rpzs->mctx); + if (dns_name_dynamic(&rpz->passthru)) + dns_name_free(&rpz->passthru, rpzs->mctx); + if (dns_name_dynamic(&rpz->drop)) + dns_name_free(&rpz->drop, rpzs->mctx); + if (dns_name_dynamic(&rpz->tcp_only)) + dns_name_free(&rpz->tcp_only, rpzs->mctx); + if (dns_name_dynamic(&rpz->cname)) + dns_name_free(&rpz->cname, rpzs->mctx); + if (rpz->db_registered) + dns_db_updatenotify_unregister(rpz->db, + dns_rpz_dbupdate_callback, rpz); + if (rpz->dbversion != NULL) + dns_db_closeversion(rpz->db, &rpz->dbversion, + false); + if (rpz->db) + dns_db_detach(&rpz->db); + isc_ht_destroy(&rpz->nodes); + isc_timer_detach(&rpz->updatetimer); + + isc_mem_put(rpzs->mctx, rpz, sizeof(*rpz)); + } + *rpzp = NULL; - isc_refcount_decrement(&rpz->refs, &refs); - if (refs != 0) - return; - isc_refcount_destroy(&rpz->refs); - - if (dns_name_dynamic(&rpz->origin)) - dns_name_free(&rpz->origin, rpzs->mctx); - if (dns_name_dynamic(&rpz->client_ip)) - dns_name_free(&rpz->client_ip, rpzs->mctx); - if (dns_name_dynamic(&rpz->ip)) - dns_name_free(&rpz->ip, rpzs->mctx); - if (dns_name_dynamic(&rpz->nsdname)) - dns_name_free(&rpz->nsdname, rpzs->mctx); - if (dns_name_dynamic(&rpz->nsip)) - dns_name_free(&rpz->nsip, rpzs->mctx); - if (dns_name_dynamic(&rpz->passthru)) - dns_name_free(&rpz->passthru, rpzs->mctx); - if (dns_name_dynamic(&rpz->drop)) - dns_name_free(&rpz->drop, rpzs->mctx); - if (dns_name_dynamic(&rpz->tcp_only)) - dns_name_free(&rpz->tcp_only, rpzs->mctx); - if (dns_name_dynamic(&rpz->cname)) - dns_name_free(&rpz->cname, rpzs->mctx); - if (rpz->db_registered) - dns_db_updatenotify_unregister(rpz->db, - dns_rpz_dbupdate_callback, rpz); - if (rpz->dbversion != NULL) - dns_db_closeversion(rpz->db, &rpz->dbversion, - false); - if (rpz->db) - dns_db_detach(&rpz->db); - isc_ht_destroy(&rpz->nodes); - isc_timer_detach(&rpz->updatetimer); - - isc_mem_put(rpzs->mctx, rpz, sizeof(*rpz)); } void dns_rpz_attach_rpzs(dns_rpz_zones_t *rpzs, dns_rpz_zones_t **rpzsp) { REQUIRE(rpzsp != NULL && *rpzsp == NULL); - isc_refcount_increment(&rpzs->refs, NULL); + isc_refcount_increment(&rpzs->refs); *rpzsp = rpzs; } @@ -2105,46 +2105,43 @@ dns_rpz_detach_rpzs(dns_rpz_zones_t **rpzsp) { dns_rpz_zones_t *rpzs; dns_rpz_zone_t *rpz; dns_rpz_num_t rpz_num; - unsigned int refs; REQUIRE(rpzsp != NULL); rpzs = *rpzsp; REQUIRE(rpzs != NULL); *rpzsp = NULL; - isc_refcount_decrement(&rpzs->refs, &refs); - if (refs != 0) { - return; - } + if (isc_refcount_decrement(&rpzs->refs) == 1) { + isc_refcount_destroy(&rpzs->refs); - /* - * Forget the last of view's rpz machinery after the last reference. - */ - for (rpz_num = 0; rpz_num < DNS_RPZ_MAX_ZONES; ++rpz_num) { - rpz = rpzs->zones[rpz_num]; - rpzs->zones[rpz_num] = NULL; - if (rpz != NULL) { - rpz_detach(&rpz, rpzs); + /* + * Forget the last of view's rpz machinery after the last reference. + */ + for (rpz_num = 0; rpz_num < DNS_RPZ_MAX_ZONES; ++rpz_num) { + rpz = rpzs->zones[rpz_num]; + rpzs->zones[rpz_num] = NULL; + if (rpz != NULL) { + rpz_detach(&rpz, rpzs); + } } - } - if (rpzs->rps_cstr_size != 0) { + if (rpzs->rps_cstr_size != 0) { #ifdef USE_DNSRPS - librpz->client_detach(&rpzs->rps_client); + librpz->client_detach(&rpzs->rps_client); #endif - isc_mem_put(rpzs->mctx, rpzs->rps_cstr, - rpzs->rps_cstr_size); - } + isc_mem_put(rpzs->mctx, rpzs->rps_cstr, + rpzs->rps_cstr_size); + } - cidr_free(rpzs); - if (rpzs->rbt != NULL) { - dns_rbt_destroy(&rpzs->rbt); + cidr_free(rpzs); + if (rpzs->rbt != NULL) { + dns_rbt_destroy(&rpzs->rbt); + } + DESTROYLOCK(&rpzs->maint_lock); + isc_rwlock_destroy(&rpzs->search_lock); + isc_task_destroy(&rpzs->updater); + isc_mem_putanddetach(&rpzs->mctx, rpzs, sizeof(*rpzs)); } - DESTROYLOCK(&rpzs->maint_lock); - isc_rwlock_destroy(&rpzs->search_lock); - isc_refcount_destroy(&rpzs->refs); - isc_task_destroy(&rpzs->updater); - isc_mem_putanddetach(&rpzs->mctx, rpzs, sizeof(*rpzs)); } /* diff --git a/lib/dns/tsig.c b/lib/dns/tsig.c index 921ba94279..529efdfe84 100644 --- a/lib/dns/tsig.c +++ b/lib/dns/tsig.c @@ -369,8 +369,9 @@ dns_tsigkey_createfromkey(const dns_name_t *name, const dns_name_t *algorithm, cleanup_refs: tkey->magic = 0; - while (refs-- > 0) - isc_refcount_decrement(&tkey->refs, NULL); + while (refs-- > 0) { + (void)isc_refcount_decrement(&tkey->refs); + } isc_refcount_destroy(&tkey->refs); if (tkey->key != NULL) @@ -706,7 +707,7 @@ dns_tsigkey_attach(dns_tsigkey_t *source, dns_tsigkey_t **targetp) { REQUIRE(VALID_TSIG_KEY(source)); REQUIRE(targetp != NULL && *targetp == NULL); - isc_refcount_increment(&source->refs, NULL); + isc_refcount_increment(&source->refs); *targetp = source; } @@ -735,16 +736,15 @@ tsigkey_free(dns_tsigkey_t *key) { void dns_tsigkey_detach(dns_tsigkey_t **keyp) { dns_tsigkey_t *key; - unsigned int refs; REQUIRE(keyp != NULL); REQUIRE(VALID_TSIG_KEY(*keyp)); key = *keyp; - isc_refcount_decrement(&key->refs, &refs); - if (refs == 0) + if (isc_refcount_decrement(&key->refs) == 1) { tsigkey_free(key); + } *keyp = NULL; } @@ -1784,7 +1784,7 @@ dns_tsigkey_find(dns_tsigkey_t **tsigkey, const dns_name_t *name, return (ISC_R_NOTFOUND); } #endif - isc_refcount_increment(&key->refs, NULL); + isc_refcount_increment(&key->refs); RWUNLOCK(&ring->lock, isc_rwlocktype_read); adjust_lru(key); *tsigkey = key; @@ -1854,7 +1854,7 @@ dns_tsigkeyring_add(dns_tsig_keyring_t *ring, const dns_name_t *name, result = keyring_add(ring, name, tkey); if (result == ISC_R_SUCCESS) - isc_refcount_increment(&tkey->refs, NULL); + isc_refcount_increment(&tkey->refs); return (result); } diff --git a/lib/dns/view.c b/lib/dns/view.c index e0fe3ca955..751b0d18af 100644 --- a/lib/dns/view.c +++ b/lib/dns/view.c @@ -311,7 +311,7 @@ dns_view_create(isc_mem_t *mctx, dns_rdataclass_t rdclass, dns_tsigkeyring_detach(&view->dynamickeys); cleanup_references: - isc_refcount_decrement(&view->references, NULL); + (void)isc_refcount_decrement(&view->references); isc_refcount_destroy(&view->references); if (view->fwdtable != NULL) @@ -569,7 +569,7 @@ dns_view_attach(dns_view_t *source, dns_view_t **targetp) { REQUIRE(DNS_VIEW_VALID(source)); REQUIRE(targetp != NULL && *targetp == NULL); - isc_refcount_increment(&source->references, NULL); + isc_refcount_increment(&source->references); *targetp = source; } @@ -577,7 +577,6 @@ dns_view_attach(dns_view_t *source, dns_view_t **targetp) { static void view_flushanddetach(dns_view_t **viewp, bool flush) { dns_view_t *view; - unsigned int refs; bool done = false; REQUIRE(viewp != NULL); @@ -586,8 +585,7 @@ view_flushanddetach(dns_view_t **viewp, bool flush) { if (flush) view->flush = true; - isc_refcount_decrement(&view->references, &refs); - if (refs == 0) { + if (isc_refcount_decrement(&view->references) == 1) { dns_zone_t *mkzone = NULL, *rdzone = NULL; LOCK(&view->lock); diff --git a/lib/dns/zone.c b/lib/dns/zone.c index 3dc5231702..b9e2b83e3e 100644 --- a/lib/dns/zone.c +++ b/lib/dns/zone.c @@ -1083,7 +1083,7 @@ dns_zone_create(dns_zone_t **zonep, isc_mem_t *mctx) { isc_stats_detach(&zone->gluecachestats); free_erefs: - isc_refcount_decrement(&zone->erefs, NULL); + (void)isc_refcount_decrement(&zone->erefs); isc_refcount_destroy(&zone->erefs); ZONEDB_DESTROYLOCK(&zone->dblock); @@ -5252,7 +5252,7 @@ void dns_zone_attach(dns_zone_t *source, dns_zone_t **target) { REQUIRE(DNS_ZONE_VALID(source)); REQUIRE(target != NULL && *target == NULL); - isc_refcount_increment(&source->erefs, NULL); + isc_refcount_increment(&source->erefs); *target = source; } @@ -5261,16 +5261,13 @@ dns_zone_detach(dns_zone_t **zonep) { dns_zone_t *zone; dns_zone_t *raw = NULL; dns_zone_t *secure = NULL; - unsigned int refs; bool free_now = false; REQUIRE(zonep != NULL && DNS_ZONE_VALID(*zonep)); zone = *zonep; - isc_refcount_decrement(&zone->erefs, &refs); - - if (refs == 0) { + if (isc_refcount_decrement(&zone->erefs) == 1) { LOCK_ZONE(zone); INSIST(zone != zone->raw); /* @@ -18638,7 +18635,7 @@ dns_zone_link(dns_zone_t *zone, dns_zone_t *raw) { /* dns_zone_attach(raw, &zone->raw); */ - isc_refcount_increment(&raw->erefs, NULL); + isc_refcount_increment(&raw->erefs); zone->raw = raw; /* dns_zone_iattach(zone, &raw->secure); */ diff --git a/lib/isc/include/isc/refcount.h b/lib/isc/include/isc/refcount.h index 4562208837..09a87d7ea4 100644 --- a/lib/isc/include/isc/refcount.h +++ b/lib/isc/include/isc/refcount.h @@ -89,49 +89,24 @@ ISC_LANG_BEGINDECLS * Sample implementations */ -typedef struct isc_refcount { - atomic_int_fast32_t refs; -} isc_refcount_t; +typedef atomic_uint_fast32_t isc_refcount_t; -#define isc_refcount_init(rp, n) \ - atomic_init(&(rp)->refs, n) +#define isc_refcount_init(target, value) \ + atomic_init(target, value) -#define isc_refcount_current(rp) \ - atomic_load_explicit(&(rp)->refs, memory_order_relaxed) +#define isc_refcount_current(target) \ + atomic_load_explicit(target, memory_order_acquire) -#define isc_refcount_destroy(rp) \ - ISC_REQUIRE(isc_refcount_current(rp) == 0) +#define isc_refcount_destroy(target) \ + ISC_REQUIRE(isc_refcount_current(target) == 0) -#define isc_refcount_increment0(rp, tp) \ - do { \ - unsigned int *_tmp = (unsigned int *)(tp); \ - int32_t prev; \ - prev = atomic_fetch_add_explicit \ - (&(rp)->refs, 1, memory_order_relaxed); \ - if (_tmp != NULL) \ - *_tmp = prev + 1; \ - } while (0) +#define isc_refcount_increment0(target) \ + atomic_fetch_add_explicit(target, 1, memory_order_relaxed) -#define isc_refcount_increment(rp, tp) \ - do { \ - unsigned int *_tmp = (unsigned int *)(tp); \ - int32_t prev; \ - prev = atomic_fetch_add_explicit \ - (&(rp)->refs, 1, memory_order_relaxed); \ - ISC_REQUIRE(prev > 0); \ - if (_tmp != NULL) \ - *_tmp = prev + 1; \ - } while (0) +#define isc_refcount_increment(target) \ + atomic_fetch_add_explicit(target, 1, memory_order_relaxed) -#define isc_refcount_decrement(rp, tp) \ - do { \ - unsigned int *_tmp = (unsigned int *)(tp); \ - int32_t prev; \ - prev = atomic_fetch_sub_explicit \ - (&(rp)->refs, 1, memory_order_relaxed); \ - ISC_REQUIRE(prev > 0); \ - if (_tmp != NULL) \ - *_tmp = prev - 1; \ - } while (0) +#define isc_refcount_decrement(target) \ + atomic_fetch_sub_explicit(target, 1, memory_order_release) ISC_LANG_ENDDECLS diff --git a/lib/isc/mem.c b/lib/isc/mem.c index 99a53ed88d..371b2e3f08 100644 --- a/lib/isc/mem.c +++ b/lib/isc/mem.c @@ -1095,12 +1095,11 @@ destroy(isc__mem_t *ctx) { void isc__mem_attach(isc_mem_t *source0, isc_mem_t **targetp) { isc__mem_t *source = (isc__mem_t *)source0; - int_fast32_t refs; REQUIRE(VALID_CONTEXT(source)); REQUIRE(targetp != NULL && *targetp == NULL); - isc_refcount_increment(&source->references, &refs); + isc_refcount_increment(&source->references); *targetp = (isc_mem_t *)source; } @@ -1108,15 +1107,12 @@ isc__mem_attach(isc_mem_t *source0, isc_mem_t **targetp) { void isc__mem_detach(isc_mem_t **ctxp) { isc__mem_t *ctx; - int_fast32_t refs; REQUIRE(ctxp != NULL); ctx = (isc__mem_t *)*ctxp; REQUIRE(VALID_CONTEXT(ctx)); - isc_refcount_decrement(&ctx->references, &refs); - - if (refs == 0) { + if (isc_refcount_decrement(&ctx->references) == 1) { destroy(ctx); } @@ -1138,7 +1134,6 @@ isc___mem_putanddetach(isc_mem_t **ctxp, void *ptr, size_t size FLARG) { isc__mem_t *ctx; size_info *si; size_t oldsize; - int_fast32_t refs; REQUIRE(ctxp != NULL); ctx = (isc__mem_t *)*ctxp; @@ -1163,9 +1158,7 @@ isc___mem_putanddetach(isc_mem_t **ctxp, void *ptr, size_t size FLARG) { } isc__mem_free((isc_mem_t *)ctx, ptr FLARG_PASS); - isc_refcount_decrement(&ctx->references, &refs); - - if (refs == 0) { + if (isc_refcount_decrement(&ctx->references) == 1) { destroy(ctx); } @@ -1184,9 +1177,7 @@ isc___mem_putanddetach(isc_mem_t **ctxp, void *ptr, size_t size FLARG) { } MCTXUNLOCK(ctx, &ctx->lock); - isc_refcount_decrement(&ctx->references, &refs); - - if (refs == 0) { + if (isc_refcount_decrement(&ctx->references) == 1) { destroy(ctx); } } @@ -1194,7 +1185,6 @@ isc___mem_putanddetach(isc_mem_t **ctxp, void *ptr, size_t size FLARG) { void isc__mem_destroy(isc_mem_t **ctxp) { isc__mem_t *ctx; - int_fast32_t refs; /* * This routine provides legacy support for callers who use mctxs @@ -1205,11 +1195,12 @@ isc__mem_destroy(isc_mem_t **ctxp) { ctx = (isc__mem_t *)*ctxp; REQUIRE(VALID_CONTEXT(ctx)); - isc_refcount_decrement(&ctx->references, &refs); #if ISC_MEM_TRACKLINES - if (refs >= 1) { + if (isc_refcount_decrement(&ctx->references) != 1) { print_active(ctx, stderr); } +#else + (void)isc_refcount_decrement(&ctx->references); #endif isc_refcount_destroy(&ctx->references); destroy(ctx); diff --git a/lib/isc/radix.c b/lib/isc/radix.c index 3fae918273..040a386f69 100644 --- a/lib/isc/radix.c +++ b/lib/isc/radix.c @@ -76,14 +76,10 @@ _new_prefix(isc_mem_t *mctx, isc_prefix_t **target, int family, void *dest, static void _deref_prefix(isc_prefix_t *prefix) { - int refs; - if (prefix == NULL) return; - isc_refcount_decrement(&prefix->refcount, &refs); - - if (refs <= 0) { + if (isc_refcount_decrement(&prefix->refcount) == 1) { isc_refcount_destroy(&prefix->refcount); isc_mem_putanddetach(&prefix->mctx, prefix, sizeof(isc_prefix_t)); @@ -110,7 +106,7 @@ _ref_prefix(isc_mem_t *mctx, isc_prefix_t **target, isc_prefix_t *prefix) { return (ret); } - isc_refcount_increment(&prefix->refcount, NULL); + isc_refcount_increment(&prefix->refcount); *target = prefix; return (ISC_R_SUCCESS); diff --git a/lib/isccfg/aclconf.c b/lib/isccfg/aclconf.c index e713f11981..4d43fabce1 100644 --- a/lib/isccfg/aclconf.c +++ b/lib/isccfg/aclconf.c @@ -64,7 +64,7 @@ cfg_aclconfctx_attach(cfg_aclconfctx_t *src, cfg_aclconfctx_t **dest) { REQUIRE(src != NULL); REQUIRE(dest != NULL && *dest == NULL); - isc_refcount_increment(&src->references, NULL); + isc_refcount_increment(&src->references); *dest = src; } @@ -72,14 +72,12 @@ void cfg_aclconfctx_detach(cfg_aclconfctx_t **actxp) { cfg_aclconfctx_t *actx; dns_acl_t *dacl, *next; - unsigned int refs; REQUIRE(actxp != NULL && *actxp != NULL); actx = *actxp; - isc_refcount_decrement(&actx->references, &refs); - if (refs == 0) { + if (isc_refcount_decrement(&actx->references) == 1) { for (dacl = ISC_LIST_HEAD(actx->named_acl_cache); dacl != NULL; dacl = next) diff --git a/lib/isccfg/parser.c b/lib/isccfg/parser.c index f5425efafc..656a0f683b 100644 --- a/lib/isccfg/parser.c +++ b/lib/isccfg/parser.c @@ -666,22 +666,20 @@ cfg_parser_attach(cfg_parser_t *src, cfg_parser_t **dest) { REQUIRE(src != NULL); REQUIRE(dest != NULL && *dest == NULL); - isc_refcount_increment(&src->references, NULL); + isc_refcount_increment(&src->references); *dest = src; } void cfg_parser_destroy(cfg_parser_t **pctxp) { cfg_parser_t *pctx; - unsigned int refs; REQUIRE(pctxp != NULL && *pctxp != NULL); pctx = *pctxp; *pctxp = NULL; - isc_refcount_decrement(&pctx->references, &refs); - if (refs == 0) { + if (isc_refcount_decrement(&pctx->references) == 1) { isc_lex_destroy(&pctx->lexer); /* * Cleaning up open_files does not @@ -3135,15 +3133,13 @@ cfg_obj_istype(const cfg_obj_t *obj, const cfg_type_t *type) { void cfg_obj_destroy(cfg_parser_t *pctx, cfg_obj_t **objp) { cfg_obj_t *obj; - unsigned int refs; REQUIRE(objp != NULL && *objp != NULL); REQUIRE(pctx != NULL); obj = *objp; - isc_refcount_decrement(&obj->references, &refs); - if (refs == 0) { + if (isc_refcount_decrement(&obj->references) == 1) { obj->type->rep->free(pctx, obj); isc_refcount_destroy(&obj->references); isc_mem_put(pctx->mctx, obj, sizeof(cfg_obj_t)); @@ -3156,7 +3152,7 @@ cfg_obj_attach(cfg_obj_t *src, cfg_obj_t **dest) { REQUIRE(src != NULL); REQUIRE(dest != NULL && *dest == NULL); - isc_refcount_increment(&src->references, NULL); + isc_refcount_increment(&src->references); *dest = src; } diff --git a/lib/ns/server.c b/lib/ns/server.c index 1278dd6267..4a133f3c51 100644 --- a/lib/ns/server.c +++ b/lib/ns/server.c @@ -118,7 +118,7 @@ ns_server_attach(ns_server_t *src, ns_server_t **dest) { REQUIRE(SCTX_VALID(src)); REQUIRE(dest != NULL && *dest == NULL); - isc_refcount_increment(&src->references, NULL); + isc_refcount_increment(&src->references); *dest = src; } @@ -126,14 +126,12 @@ ns_server_attach(ns_server_t *src, ns_server_t **dest) { void ns_server_detach(ns_server_t **sctxp) { ns_server_t *sctx; - unsigned int refs; REQUIRE(sctxp != NULL); sctx = *sctxp; REQUIRE(SCTX_VALID(sctx)); - isc_refcount_decrement(&sctx->references, &refs); - if (refs == 0) { + if (isc_refcount_decrement(&sctx->references) == 1) { ns_altsecret_t *altsecret; sctx->magic = 0;