]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Convert manual dns_iptable_{attach,detach} to ISC_REFCOUNT_IMPL
authorOndřej Surý <ondrej@isc.org>
Fri, 13 Oct 2023 06:12:51 +0000 (08:12 +0200)
committerOndřej Surý <ondrej@isc.org>
Fri, 13 Oct 2023 12:44:40 +0000 (14:44 +0200)
Instead of having a manual set of functions, use ISC_REFCOUNT_IMPL macro
to implement the attach, detach, ref and unref functions.

lib/dns/include/dns/iptable.h
lib/dns/iptable.c

index 60bc7030784cd679bbeb83fdcf46a50f72b7af7c..9c709141ca8481f4bc16a965c0ef0f55b947e804 100644 (file)
 #include <isc/lang.h>
 #include <isc/magic.h>
 #include <isc/radix.h>
+#include <isc/refcount.h>
 
 #include <dns/types.h>
 
 struct dns_iptable {
        unsigned int      magic;
        isc_mem_t        *mctx;
-       isc_refcount_t    refcount;
+       isc_refcount_t    references;
        isc_radix_tree_t *radix;
        ISC_LINK(dns_iptable_t) nextincache;
 };
 
+/* Add -DDNS_IPTABLE_TRACE=1 to CFLAGS for detailed reference tracing */
+
 #define DNS_IPTABLE_MAGIC    ISC_MAGIC('T', 'a', 'b', 'l')
 #define DNS_IPTABLE_VALID(a) ISC_MAGIC_VALID(a, DNS_IPTABLE_MAGIC)
 
@@ -58,10 +61,17 @@ dns_iptable_merge(dns_iptable_t *tab, dns_iptable_t *source, bool pos);
  * Merge one IP table into another one.
  */
 
-void
-dns_iptable_attach(dns_iptable_t *source, dns_iptable_t **target);
-
-void
-dns_iptable_detach(dns_iptable_t **tabp);
+#if DNS_IPTABLE_TRACE
+#define dns_iptable_ref(ptr) dns_iptable__ref(ptr, __func__, __FILE__, __LINE__)
+#define dns_iptable_unref(ptr) \
+       dns_iptable__unref(ptr, __func__, __FILE__, __LINE__)
+#define dns_iptable_attach(ptr, ptrp) \
+       dns_iptable__attach(ptr, ptrp, __func__, __FILE__, __LINE__)
+#define dns_iptable_detach(ptrp) \
+       dns_iptable__detach(ptrp, __func__, __FILE__, __LINE__)
+ISC_REFCOUNT_TRACE_DECL(dns_iptable);
+#else
+ISC_REFCOUNT_DECL(dns_iptable);
+#endif
 
 ISC_LANG_ENDDECLS
index f0ebef7ac8f9219be338167e88a696bcf2354455..2a783a1b082e7532efe32f26267f87a262ee51b5 100644 (file)
@@ -20,9 +20,6 @@
 
 #include <dns/acl.h>
 
-static void
-destroy_iptable(dns_iptable_t *dtab);
-
 /*
  * Create a new IP table and the underlying radix structure
  */
@@ -30,7 +27,7 @@ void
 dns_iptable_create(isc_mem_t *mctx, dns_iptable_t **target) {
        dns_iptable_t *tab = isc_mem_get(mctx, sizeof(*tab));
        *tab = (dns_iptable_t){
-               .refcount = 1,
+               .references = ISC_REFCOUNT_INITIALIZER(1),
                .magic = DNS_IPTABLE_MAGIC,
        };
        isc_mem_attach(mctx, &tab->mctx);
@@ -130,34 +127,22 @@ dns_iptable_merge(dns_iptable_t *tab, dns_iptable_t *source, bool pos) {
        return (ISC_R_SUCCESS);
 }
 
-void
-dns_iptable_attach(dns_iptable_t *source, dns_iptable_t **target) {
-       REQUIRE(DNS_IPTABLE_VALID(source));
-       isc_refcount_increment(&source->refcount);
-       *target = source;
-}
-
-void
-dns_iptable_detach(dns_iptable_t **tabp) {
-       REQUIRE(tabp != NULL && DNS_IPTABLE_VALID(*tabp));
-       dns_iptable_t *tab = *tabp;
-       *tabp = NULL;
-
-       if (isc_refcount_decrement(&tab->refcount) == 1) {
-               isc_refcount_destroy(&tab->refcount);
-               destroy_iptable(tab);
-       }
-}
-
 static void
-destroy_iptable(dns_iptable_t *dtab) {
+dns__iptable_destroy(dns_iptable_t *dtab) {
        REQUIRE(DNS_IPTABLE_VALID(dtab));
 
+       dtab->magic = 0;
+
        if (dtab->radix != NULL) {
                isc_radix_destroy(dtab->radix, NULL);
                dtab->radix = NULL;
        }
 
-       dtab->magic = 0;
        isc_mem_putanddetach(&dtab->mctx, dtab, sizeof(*dtab));
 }
+
+#if DNS_IPTABLE_TRACE
+ISC_REFCOUNT_TRACE_IMPL(dns_iptable, dns__iptable_destroy);
+#else
+ISC_REFCOUNT_IMPL(dns_iptable, dns__iptable_destroy);
+#endif