From 759a977a67bb3f5e4437a356d9430fc79ffd0237 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Fri, 22 Sep 2023 13:59:10 +0200 Subject: [PATCH] Convert dns_message reference counting to ISC_REFCOUNT macros Unify the dns_message reference counting to use ISC_REFCOUNT_{IMPL,DECL} macros to reduce the code duplicity and add reference count tracing. --- lib/dns/include/dns/message.h | 37 ++++++++-------- lib/dns/message.c | 82 +++++++++++++++-------------------- 2 files changed, 52 insertions(+), 67 deletions(-) diff --git a/lib/dns/include/dns/message.h b/lib/dns/include/dns/message.h index 637675fd9a..5a4a1365ad 100644 --- a/lib/dns/include/dns/message.h +++ b/lib/dns/include/dns/message.h @@ -30,6 +30,9 @@ #include +/* Define to 1 for detailed reference tracing */ +#undef DNS_MESSAGE_TRACE + /*! \file dns/message.h * \brief Message Handling Module * @@ -250,7 +253,7 @@ typedef struct dns_minttl { struct dns_message { /* public from here down */ unsigned int magic; - isc_refcount_t refcount; + isc_refcount_t references; dns_messageid_t id; unsigned int flags; @@ -396,24 +399,20 @@ dns_message_reset(dns_message_t *msg, unsigned int intent); *\li 'intent' is DNS_MESSAGE_INTENTPARSE or DNS_MESSAGE_INTENTRENDER */ -void -dns_message_attach(dns_message_t *source, dns_message_t **target); -/*%< - * Attach to message 'source'. - * - * Requires: - *\li 'source' to be a valid message. - *\li 'target' to be non NULL and '*target' to be NULL. - */ - -void -dns_message_detach(dns_message_t **messagep); -/*%< - * Detach *messagep from its message. - * list. - * - * Requires: - *\li '*messagep' to be a valid message. +#if DNS_NTA_TRACE +#define dns_message_ref(ptr) dns_message__ref(ptr, __func__, __FILE__, __LINE__) +#define dns_message_unref(ptr) \ + dns_message__unref(ptr, __func__, __FILE__, __LINE__) +#define dns_message_attach(ptr, ptrp) \ + dns_message__attach(ptr, ptrp, __func__, __FILE__, __LINE__) +#define dns_message_detach(ptrp) \ + dns_message__detach(ptrp, __func__, __FILE__, __LINE__) +ISC_REFCOUNT_TRACE_DECL(dns_message); +#else +ISC_REFCOUNT_DECL(dns_message); +#endif +/* + * Reference counting for dns_message */ isc_result_t diff --git a/lib/dns/message.c b/lib/dns/message.c index 6477253352..45657f61dd 100644 --- a/lib/dns/message.c +++ b/lib/dns/message.c @@ -704,9 +704,8 @@ spacefortsig(dns_tsigkey_t *key, int otherlen) { void dns_message_create(isc_mem_t *mctx, unsigned int intent, dns_message_t **msgp) { - dns_message_t *m = NULL; + dns_message_t *msg = NULL; isc_buffer_t *dynbuf = NULL; - unsigned int i; REQUIRE(mctx != NULL); REQUIRE(msgp != NULL); @@ -714,40 +713,41 @@ dns_message_create(isc_mem_t *mctx, unsigned int intent, dns_message_t **msgp) { REQUIRE(intent == DNS_MESSAGE_INTENTPARSE || intent == DNS_MESSAGE_INTENTRENDER); - m = isc_mem_get(mctx, sizeof(dns_message_t)); - *m = (dns_message_t){ .from_to_wire = intent }; - isc_mem_attach(mctx, &m->mctx); - msginit(m); + msg = isc_mem_get(mctx, sizeof(dns_message_t)); + *msg = (dns_message_t){ + .from_to_wire = intent, + .references = ISC_REFCOUNT_INITIALIZER(1), + .scratchpad = ISC_LIST_INITIALIZER, + .cleanup = ISC_LIST_INITIALIZER, + .rdatas = ISC_LIST_INITIALIZER, + .rdatalists = ISC_LIST_INITIALIZER, + .offsets = ISC_LIST_INITIALIZER, + .freerdata = ISC_LIST_INITIALIZER, + .freerdatalist = ISC_LIST_INITIALIZER, + .magic = DNS_MESSAGE_MAGIC, + }; - for (i = 0; i < DNS_SECTION_MAX; i++) { - ISC_LIST_INIT(m->sections[i]); + isc_mem_attach(mctx, &msg->mctx); + msginit(msg); + + for (size_t i = 0; i < DNS_SECTION_MAX; i++) { + ISC_LIST_INIT(msg->sections[i]); } - ISC_LIST_INIT(m->scratchpad); - ISC_LIST_INIT(m->cleanup); - ISC_LIST_INIT(m->rdatas); - ISC_LIST_INIT(m->rdatalists); - ISC_LIST_INIT(m->offsets); - ISC_LIST_INIT(m->freerdata); - ISC_LIST_INIT(m->freerdatalist); + isc_mempool_create(msg->mctx, sizeof(dns_fixedname_t), &msg->namepool); + isc_mempool_setfillcount(msg->namepool, NAME_FILLCOUNT); + isc_mempool_setfreemax(msg->namepool, NAME_FREEMAX); + isc_mempool_setname(msg->namepool, "dns_fixedname_pool"); - isc_mempool_create(m->mctx, sizeof(dns_fixedname_t), &m->namepool); - isc_mempool_setfillcount(m->namepool, NAME_FILLCOUNT); - isc_mempool_setfreemax(m->namepool, NAME_FREEMAX); - isc_mempool_setname(m->namepool, "msg:names"); - - isc_mempool_create(m->mctx, sizeof(dns_rdataset_t), &m->rdspool); - isc_mempool_setfillcount(m->rdspool, RDATASET_FILLCOUNT); - isc_mempool_setfreemax(m->rdspool, RDATASET_FREEMAX); - isc_mempool_setname(m->rdspool, "msg:rdataset"); + isc_mempool_create(msg->mctx, sizeof(dns_rdataset_t), &msg->rdspool); + isc_mempool_setfillcount(msg->rdspool, RDATASET_FILLCOUNT); + isc_mempool_setfreemax(msg->rdspool, RDATASET_FREEMAX); + isc_mempool_setname(msg->rdspool, "dns_rdataset_pool"); isc_buffer_allocate(mctx, &dynbuf, SCRATCHPAD_SIZE); - ISC_LIST_APPEND(m->scratchpad, dynbuf, link); + ISC_LIST_APPEND(msg->scratchpad, dynbuf, link); - isc_refcount_init(&m->refcount, 1); - m->magic = DNS_MESSAGE_MAGIC; - - *msgp = m; + *msgp = msg; } void @@ -768,29 +768,15 @@ dns__message_destroy(dns_message_t *msg) { msgreset(msg, true); isc_mempool_destroy(&msg->namepool); isc_mempool_destroy(&msg->rdspool); - isc_refcount_destroy(&msg->refcount); msg->magic = 0; isc_mem_putanddetach(&msg->mctx, msg, sizeof(dns_message_t)); } -void -dns_message_attach(dns_message_t *source, dns_message_t **target) { - REQUIRE(DNS_MESSAGE_VALID(source)); - - isc_refcount_increment(&source->refcount); - *target = source; -} - -void -dns_message_detach(dns_message_t **messagep) { - REQUIRE(messagep != NULL && DNS_MESSAGE_VALID(*messagep)); - dns_message_t *msg = *messagep; - *messagep = NULL; - - if (isc_refcount_decrement(&msg->refcount) == 1) { - dns__message_destroy(msg); - } -} +#if DNS_MESSAGE_TRACE +ISC_REFCOUNT_TRACE_IMPL(dns_message, dns__message_destroy); +#else +ISC_REFCOUNT_IMPL(dns_message, dns__message_destroy); +#endif static isc_result_t findname(dns_name_t **foundname, const dns_name_t *target,