]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Convert dns_message reference counting to ISC_REFCOUNT macros
authorOndřej Surý <ondrej@isc.org>
Fri, 22 Sep 2023 11:59:10 +0000 (13:59 +0200)
committerOndřej Surý <ondrej@isc.org>
Sun, 24 Sep 2023 08:09:04 +0000 (10:09 +0200)
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
lib/dns/message.c

index 637675fd9ae8a0ddf76c86e7082b990a064c741e..5a4a1365add745c8fed898e2aa73bd02669e6922 100644 (file)
@@ -30,6 +30,9 @@
 
 #include <dst/dst.h>
 
+/* 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
index 64772533524c364e397c0be05e67e5da6d57aa60..45657f61dd489c354d00bbee9b58aec3c3a1f340 100644 (file)
@@ -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);
-
-       for (i = 0; i < DNS_SECTION_MAX; i++) {
-               ISC_LIST_INIT(m->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(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");
+       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,
+       };
+
+       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_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(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_refcount_init(&m->refcount, 1);
-       m->magic = DNS_MESSAGE_MAGIC;
+       ISC_LIST_APPEND(msg->scratchpad, dynbuf, link);
 
-       *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,