ISC_LIST_INIT(l->elements);
l->mem = mem;
- l->refs = 1;
+ isc_refcount_init(&l->refs, 1);
l->magic = DNS_PEERLIST_MAGIC;
*list = l;
REQUIRE(target != NULL);
REQUIRE(*target == NULL);
- source->refs++;
-
- ENSURE(source->refs != 0xffffffffU);
+ isc_refcount_increment(&source->refs);
*target = source;
}
plist = *list;
*list = NULL;
- REQUIRE(plist->refs > 0);
-
- plist->refs--;
-
- if (plist->refs == 0)
+ if (isc_refcount_decrement(&plist->refs) == 1) {
peerlist_delete(&plist);
+ }
}
static void
l = *list;
- REQUIRE(l->refs == 0);
+ isc_refcount_destroy(&l->refs);
server = ISC_LIST_HEAD(l->elements);
while (server != NULL) {
{
dns_peer_t *peer;
- REQUIRE(peerptr != NULL);
+ REQUIRE(peerptr != NULL && *peerptr == NULL);
peer = isc_mem_get(mem, sizeof(*peer));
- peer->magic = DNS_PEER_MAGIC;
- peer->address = *addr;
- peer->prefixlen = prefixlen;
- peer->mem = mem;
- peer->bogus = false;
- peer->transfer_format = dns_one_answer;
- peer->transfers = 0;
- peer->request_ixfr = false;
- peer->provide_ixfr = false;
- peer->key = NULL;
- peer->refs = 1;
- peer->transfer_source = NULL;
- peer->notify_source = NULL;
- peer->query_source = NULL;
-
- memset(&peer->bitflags, 0x0, sizeof(peer->bitflags));
+ *peer = (dns_peer_t){
+ .magic = DNS_PEER_MAGIC,
+ .address = *addr,
+ .prefixlen = prefixlen,
+ .mem = mem,
+ .transfer_format = dns_one_answer,
+ };
+
+ isc_refcount_init(&peer->refs, 1);
ISC_LINK_INIT(peer, next);
REQUIRE(target != NULL);
REQUIRE(*target == NULL);
- source->refs++;
-
- ENSURE(source->refs != 0xffffffffU);
+ isc_refcount_increment(&source->refs);
*target = source;
}
REQUIRE(DNS_PEER_VALID(*peer));
p = *peer;
-
- REQUIRE(p->refs > 0);
-
*peer = NULL;
- p->refs--;
- if (p->refs == 0)
+ if (isc_refcount_decrement(&p->refs) == 1) {
peer_delete(&p);
+ }
}
static void
p = *peer;
- REQUIRE(p->refs == 0);
+ isc_refcount_destroy(&p->refs);
mem = p->mem;
p->mem = NULL;
dns_zonemgr_t *zmgr;
ISC_LINK(dns_zone_t) link; /* Used by zmgr. */
isc_timer_t *timer;
- unsigned int irefs;
+ isc_refcount_t irefs;
dns_name_t origin;
char *masterfile;
ISC_LIST(dns_include_t) includes; /* Include files */
struct dns_zonemgr {
unsigned int magic;
isc_mem_t * mctx;
- int refs; /* Locked by rwlock */
+ isc_refcount_t refs;
isc_taskmgr_t * taskmgr;
isc_timermgr_t * timermgr;
isc_socketmgr_t * socketmgr;
zone->db = NULL;
zone->zmgr = NULL;
ISC_LINK_INIT(zone, link);
- isc_refcount_init(&zone->erefs, 1); /* Implicit attach. */
- zone->irefs = 0;
+ isc_refcount_init(&zone->erefs, 1);
+ isc_refcount_init(&zone->irefs, 0);
dns_name_init(&zone->origin, NULL);
zone->strnamerd = NULL;
zone->strname = NULL;
result = isc_stats_create(mctx, &zone->gluecachestats,
dns_gluecachestatscounter_max);
if (result != ISC_R_SUCCESS) {
- goto free_erefs;
+ goto free_refs;
}
/* Must be after magic is set. */
*zonep = zone;
return (ISC_R_SUCCESS);
- free_erefs:
+ free_refs:
INSIST(isc_refcount_decrement(&zone->erefs) > 0);
isc_refcount_destroy(&zone->erefs);
+ isc_refcount_destroy(&zone->irefs);
ZONEDB_DESTROYLOCK(&zone->dblock);
REQUIRE(DNS_ZONE_VALID(zone));
REQUIRE(isc_refcount_current(&zone->erefs) == 0);
- REQUIRE(zone->irefs == 0);
+ REQUIRE(isc_refcount_current(&zone->irefs) == 0);
REQUIRE(!LOCKED_ZONE(zone));
REQUIRE(zone->timer == NULL);
REQUIRE(zone->zmgr == NULL);
exit_check(dns_zone_t *zone) {
REQUIRE(LOCKED_ZONE(zone));
- if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_SHUTDOWN) && zone->irefs == 0) {
+ if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_SHUTDOWN) &&
+ isc_refcount_current(&zone->irefs) == 0)
+ {
/*
* DNS_ZONEFLG_SHUTDOWN can only be set if erefs == 0.
*/
REQUIRE(DNS_ZONE_VALID(source));
REQUIRE(LOCKED_ZONE(source));
REQUIRE(target != NULL && *target == NULL);
- INSIST(source->irefs + isc_refcount_current(&source->erefs) > 0);
- source->irefs++;
- INSIST(source->irefs != 0);
+ INSIST(isc_refcount_increment0(&source->irefs) +
+ isc_refcount_current(&source->erefs) > 0);
*target = source;
}
REQUIRE(LOCKED_ZONE(*zonep));
*zonep = NULL;
- INSIST(zone->irefs > 0);
- zone->irefs--;
- INSIST(zone->irefs + isc_refcount_current(&zone->erefs) > 0);
+ INSIST(isc_refcount_decrement(&zone->irefs) - 1 +
+ isc_refcount_current(&zone->erefs) > 0);
}
void
zone = *zonep;
*zonep = NULL;
- LOCK_ZONE(zone);
- INSIST(zone->irefs > 0);
- zone->irefs--;
- free_needed = exit_check(zone);
- UNLOCK_ZONE(zone);
- if (free_needed)
- zone_free(zone);
+ if (isc_refcount_decrement(&zone->irefs) == 1) {
+ LOCK_ZONE(zone);
+ free_needed = exit_check(zone);
+ UNLOCK_ZONE(zone);
+ if (free_needed) {
+ zone_free(zone);
+ }
+ }
}
isc_mem_t *
cleanup:
dns_db_detach(&kfetch->db);
- INSIST(zone->irefs > 0);
- zone->irefs--;
+ isc_refcount_decrement(&zone->irefs);
+
kfetch->zone = NULL;
if (dns_rdataset_isassociated(keydataset)) {
zone->refreshkeycount++;
kfetch->zone = zone;
- zone->irefs++;
- INSIST(zone->irefs != 0);
+ isc_refcount_increment0(&zone->irefs);
kname = dns_fixedname_initname(&kfetch->name);
dns_name_dup(name, zone->mctx, kname);
dns_rdataset_init(&kfetch->dnskeyset);
fetching = true;
} else {
zone->refreshkeycount--;
- zone->irefs--;
+ isc_refcount_decrement(&zone->irefs);
dns_db_detach(&kfetch->db);
dns_rdataset_disassociate(&kfetch->keydataset);
dns_name_free(kname, zone->mctx);
LOCK_ZONE(zone);
INSIST(zone != zone->raw);
if (linked) {
- INSIST(zone->irefs > 0);
- zone->irefs--;
+ isc_refcount_decrement(&zone->irefs);
}
if (zone->request != NULL) {
dns_request_cancel(zone->request);
if (zone->timer != NULL) {
isc_timer_detach(&zone->timer);
- INSIST(zone->irefs > 0);
- zone->irefs--;
+ isc_refcount_decrement(&zone->irefs);
}
/*
if (event != NULL) {
LOCK_ZONE(zone);
- INSIST(zone->irefs > 1);
- zone->irefs--;
+ isc_refcount_decrement(&zone->irefs);
ISC_LIST_UNLINK(zone->rss_events, event, ev_link);
goto nextevent;
}
if (again && !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING))
queue_soa_query(zone);
- INSIST(zone->irefs > 0);
- zone->irefs--;
+ isc_refcount_decrement(&zone->irefs);
free_needed = exit_check(zone);
UNLOCK_ZONE(zone);
if (free_needed)
RWLOCK(&zmgr->rwlock, isc_rwlocktype_write);
ISC_LIST_APPEND(zmgr->waiting_for_xfrin, zone, statelink);
- LOCK_ZONE(zone);
- zone->irefs++;
- UNLOCK_ZONE(zone);
+ isc_refcount_increment0(&zone->irefs);
zone->statelist = &zmgr->waiting_for_xfrin;
result = zmgr_start_xfrin_ifquota(zmgr, zone);
RWUNLOCK(&zmgr->rwlock, isc_rwlocktype_write);
zmgr = isc_mem_get(mctx, sizeof(*zmgr));
zmgr->mctx = NULL;
- zmgr->refs = 1;
+ isc_refcount_init(&zmgr->refs, 1);
isc_mem_attach(mctx, &zmgr->mctx);
zmgr->taskmgr = taskmgr;
zmgr->timermgr = timermgr;
/*
* The timer "holds" a iref.
*/
- zone->irefs++;
- INSIST(zone->irefs != 0);
+ isc_refcount_increment0(&zone->irefs);
ISC_LIST_APPEND(zmgr->zones, zone, link);
zone->zmgr = zmgr;
- zmgr->refs++;
+ isc_refcount_increment(&zmgr->refs);
goto unlock;
ISC_LIST_UNLINK(zmgr->zones, zone, link);
zone->zmgr = NULL;
- zmgr->refs--;
- if (zmgr->refs == 0)
+
+ if (isc_refcount_decrement(&zmgr->refs) == 1) {
free_now = true;
+ }
UNLOCK_ZONE(zone);
RWUNLOCK(&zmgr->rwlock, isc_rwlocktype_write);
REQUIRE(DNS_ZONEMGR_VALID(source));
REQUIRE(target != NULL && *target == NULL);
- RWLOCK(&source->rwlock, isc_rwlocktype_write);
- REQUIRE(source->refs > 0);
- source->refs++;
- INSIST(source->refs > 0);
- RWUNLOCK(&source->rwlock, isc_rwlocktype_write);
+ isc_refcount_increment(&source->refs);
+
*target = source;
}
void
dns_zonemgr_detach(dns_zonemgr_t **zmgrp) {
dns_zonemgr_t *zmgr;
- bool free_now = false;
REQUIRE(zmgrp != NULL);
zmgr = *zmgrp;
REQUIRE(DNS_ZONEMGR_VALID(zmgr));
- RWLOCK(&zmgr->rwlock, isc_rwlocktype_write);
- zmgr->refs--;
- if (zmgr->refs == 0)
- free_now = true;
- RWUNLOCK(&zmgr->rwlock, isc_rwlocktype_write);
-
- if (free_now)
+ if (isc_refcount_decrement(&zmgr->refs) == 1) {
zonemgr_free(zmgr);
+ }
+
*zmgrp = NULL;
}
zonemgr_free(dns_zonemgr_t *zmgr) {
isc_mem_t *mctx;
- INSIST(zmgr->refs == 0);
INSIST(ISC_LIST_EMPTY(zmgr->zones));
zmgr->magic = 0;
+ isc_refcount_destroy(&zmgr->refs);
isc_mutex_destroy(&zmgr->iolock);
isc_ratelimiter_detach(&zmgr->notifyrl);
isc_ratelimiter_detach(&zmgr->refreshrl);
/*
* The timer "holds" a iref.
*/
- raw->irefs++;
- INSIST(raw->irefs != 0);
-
+ isc_refcount_increment0(&raw->irefs);
/* dns_zone_attach(raw, &zone->raw); */
isc_refcount_increment(&raw->erefs);
ISC_LIST_APPEND(zmgr->zones, raw, link);
raw->zmgr = zmgr;
- zmgr->refs++;
+ isc_refcount_increment(&zmgr->refs);
unlock:
UNLOCK_ZONE(raw);
#include <isc/mem.h>
#include <isc/ratelimiter.h>
+#include <isc/refcount.h>
#include <isc/task.h>
#include <isc/time.h>
#include <isc/timer.h>
struct isc_ratelimiter {
isc_mem_t * mctx;
isc_mutex_t lock;
- int refs;
+ isc_refcount_t references;
isc_task_t * task;
isc_timer_t * timer;
isc_interval_t interval;
INSIST(ratelimiterp != NULL && *ratelimiterp == NULL);
rl = isc_mem_get(mctx, sizeof(*rl));
- rl->mctx = mctx;
- rl->refs = 1;
- rl->task = task;
+ *rl = (isc_ratelimiter_t){
+ .mctx = mctx,
+ .task = task,
+ .pertic = 1,
+ .state = isc_ratelimiter_idle,
+ };
+
+ isc_refcount_init(&rl->references, 1);
isc_interval_set(&rl->interval, 0, 0);
- rl->timer = NULL;
- rl->pertic = 1;
- rl->pushpop = false;
- rl->state = isc_ratelimiter_idle;
ISC_LIST_INIT(rl->pending);
isc_mutex_init(&rl->lock);
* Increment the reference count to indicate that we may
* (soon) have events outstanding.
*/
- rl->refs++;
+ isc_refcount_increment(&rl->references);
ISC_EVENT_INIT(&rl->shutdownevent,
sizeof(isc_event_t),
*/
ISC_LIST_UNLINK(rl->pending, p, ev_ratelink);
} else {
- isc_result_t result;
/*
* No work left to do. Stop the timer so that we don't
* waste resources by having it fire periodically.
*/
- result = isc_timer_reset(rl->timer,
- isc_timertype_inactive,
- NULL, NULL, false);
+ isc_result_t result =
+ isc_timer_reset(rl->timer,
+ isc_timertype_inactive,
+ NULL, NULL, false);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
rl->state = isc_ratelimiter_idle;
pertic = 0; /* Force the loop to exit. */
void
isc_ratelimiter_shutdown(isc_ratelimiter_t *rl) {
isc_event_t *ev;
- isc_task_t *task;
REQUIRE(rl != NULL);
(void)isc_timer_reset(rl->timer, isc_timertype_inactive,
NULL, NULL, false);
while ((ev = ISC_LIST_HEAD(rl->pending)) != NULL) {
+ isc_task_t *task = ev->ev_sender;
ISC_LIST_UNLINK(rl->pending, ev, ev_ratelink);
ev->ev_attributes |= ISC_EVENTATTR_CANCELED;
- task = ev->ev_sender;
isc_task_send(task, &ev);
}
isc_timer_detach(&rl->timer);
void
isc_ratelimiter_attach(isc_ratelimiter_t *source, isc_ratelimiter_t **target) {
-
REQUIRE(source != NULL);
REQUIRE(target != NULL && *target == NULL);
- LOCK(&source->lock);
- REQUIRE(source->refs > 0);
- source->refs++;
- INSIST(source->refs > 0);
- UNLOCK(&source->lock);
+ isc_refcount_increment(&source->references);
+
*target = source;
}
void
isc_ratelimiter_detach(isc_ratelimiter_t **rlp) {
isc_ratelimiter_t *rl;
- bool free_now = false;
REQUIRE(rlp != NULL && *rlp != NULL);
rl = *rlp;
- LOCK(&rl->lock);
- REQUIRE(rl->refs > 0);
- rl->refs--;
- if (rl->refs == 0)
- free_now = true;
- UNLOCK(&rl->lock);
-
- if (free_now)
+ if (isc_refcount_decrement(&rl->references) == 1) {
ratelimiter_free(rl);
+ }
*rlp = NULL;
}