]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Convert manual dns_{acl,aclenv}_{attach,detach} to ISC_REFCOUNT_IMPL
authorOndřej Surý <ondrej@isc.org>
Fri, 13 Oct 2023 06:22:27 +0000 (08:22 +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/acl.c
lib/dns/include/dns/acl.h

index 45d19a563a62e1f182a3b2a10323a9f9b3afb738..85c3cdbd2bd573216e1cb2f9ff76c2320b48378b 100644 (file)
@@ -38,7 +38,7 @@ dns_acl_create(isc_mem_t *mctx, int n, dns_acl_t **target) {
 
        dns_acl_t *acl = isc_mem_get(mctx, sizeof(*acl));
        *acl = (dns_acl_t){
-               .refcount = 1,
+               .references = ISC_REFCOUNT_INITIALIZER(1),
                .nextincache = ISC_LINK_INITIALIZER,
                .elements = isc_mem_cget(mctx, n, sizeof(acl->elements[0])),
                .alloc = n,
@@ -464,16 +464,8 @@ dns_aclelement_match(const isc_netaddr_t *reqaddr, const dns_name_t *reqsigner,
        return (false);
 }
 
-void
-dns_acl_attach(dns_acl_t *source, dns_acl_t **target) {
-       REQUIRE(DNS_ACL_VALID(source));
-
-       isc_refcount_increment(&source->refcount);
-       *target = source;
-}
-
 static void
-destroy(dns_acl_t *dacl) {
+dns__acl_destroy(dns_acl_t *dacl) {
        unsigned int i;
        dns_acl_port_transports_t *port_proto;
 
@@ -508,21 +500,16 @@ destroy(dns_acl_t *dacl) {
                port_proto = next;
        }
 
-       isc_refcount_destroy(&dacl->refcount);
+       isc_refcount_destroy(&dacl->references);
        dacl->magic = 0;
        isc_mem_putanddetach(&dacl->mctx, dacl, sizeof(*dacl));
 }
 
-void
-dns_acl_detach(dns_acl_t **aclp) {
-       REQUIRE(aclp != NULL && DNS_ACL_VALID(*aclp));
-       dns_acl_t *acl = *aclp;
-       *aclp = NULL;
-
-       if (isc_refcount_decrement(&acl->refcount) == 1) {
-               destroy(acl);
-       }
-}
+#if DNS_ACL_TRACE
+ISC_REFCOUNT_TRACE_IMPL(dns_acl, dns__acl_destroy);
+#else
+ISC_REFCOUNT_IMPL(dns_acl, dns__acl_destroy);
+#endif
 
 static isc_once_t insecure_prefix_once = ISC_ONCE_INIT;
 static isc_mutex_t insecure_prefix_lock;
@@ -659,7 +646,7 @@ void
 dns_aclenv_create(isc_mem_t *mctx, dns_aclenv_t **envp) {
        dns_aclenv_t *env = isc_mem_get(mctx, sizeof(*env));
        *env = (dns_aclenv_t){
-               .references = 1,
+               .references = ISC_REFCOUNT_INITIALIZER(1),
                .magic = DNS_ACLENV_MAGIC,
        };
 
@@ -720,28 +707,11 @@ dns__aclenv_destroy(dns_aclenv_t *aclenv) {
        isc_mem_putanddetach(&aclenv->mctx, aclenv, sizeof(*aclenv));
 }
 
-void
-dns_aclenv_attach(dns_aclenv_t *source, dns_aclenv_t **targetp) {
-       REQUIRE(VALID_ACLENV(source));
-       REQUIRE(targetp != NULL && *targetp == NULL);
-
-       isc_refcount_increment(&source->references);
-       *targetp = source;
-}
-
-void
-dns_aclenv_detach(dns_aclenv_t **aclenvp) {
-       dns_aclenv_t *aclenv = NULL;
-
-       REQUIRE(aclenvp != NULL && VALID_ACLENV(*aclenvp));
-
-       aclenv = *aclenvp;
-       *aclenvp = NULL;
-
-       if (isc_refcount_decrement(&aclenv->references) == 1) {
-               dns__aclenv_destroy(aclenv);
-       }
-}
+#if DNS_ACL_TRACE
+ISC_REFCOUNT_TRACE_IMPL(dns_aclenv, dns__aclenv_destroy);
+#else
+ISC_REFCOUNT_IMPL(dns_aclenv, dns__aclenv_destroy);
+#endif
 
 void
 dns_acl_add_port_transports(dns_acl_t *acl, const in_port_t port,
index 580fd415bdebb8f3ee49f681c87bf5f90ef5fc6d..d1be3a3d4304edc1fee9435a26c0f96f35f8e3fd 100644 (file)
@@ -22,6 +22,8 @@
  * Address match list handling.
  */
 
+/* Add -DDNS_ACL_TRACE=1 to CFLAGS for detailed reference tracing */
+
 /***
  *** Imports
  ***/
@@ -84,7 +86,7 @@ struct dns_aclelement {
 struct dns_acl {
        unsigned int      magic;
        isc_mem_t        *mctx;
-       isc_refcount_t    refcount;
+       isc_refcount_t    references;
        dns_iptable_t    *iptable;
        dns_aclelement_t *elements;
        bool              has_negatives;
@@ -164,28 +166,16 @@ dns_acl_merge(dns_acl_t *dest, dns_acl_t *source, bool pos);
  * an unexpected positive match in the parent ACL.
  */
 
-void
-dns_acl_attach(dns_acl_t *source, dns_acl_t **target);
-/*%<
- * Attach to acl 'source'.
- *
- * Requires:
- *\li  'source' to be a valid acl.
- *\li  'target' to be non NULL and '*target' to be NULL.
- */
-
-void
-dns_acl_detach(dns_acl_t **aclp);
-/*%<
- * Detach the acl. On final detach the acl must not be linked on any
- * list.
- *
- * Requires:
- *\li  '*aclp' to be a valid acl.
- *
- * Insists:
- *\li  '*aclp' is not linked on final detach.
- */
+#if DNS_ACL_TRACE
+#define dns_acl_ref(ptr)   dns_acl__ref(ptr, __func__, __FILE__, __LINE__)
+#define dns_acl_unref(ptr) dns_acl__unref(ptr, __func__, __FILE__, __LINE__)
+#define dns_acl_attach(ptr, ptrp) \
+       dns_acl__attach(ptr, ptrp, __func__, __FILE__, __LINE__)
+#define dns_acl_detach(ptrp) dns_acl__detach(ptrp, __func__, __FILE__, __LINE__)
+ISC_REFCOUNT_TRACE_DECL(dns_acl);
+#else
+ISC_REFCOUNT_DECL(dns_acl);
+#endif
 
 bool
 dns_acl_isinsecure(const dns_acl_t *a);
@@ -227,24 +217,18 @@ dns_aclenv_set(dns_aclenv_t *env, dns_acl_t *localhost, dns_acl_t *localnets);
  * Attach the 'localhost' and 'localnets' arguments to 'env' ACL environment
  */
 
-void
-dns_aclenv_attach(dns_aclenv_t *source, dns_aclenv_t **targetp);
-/*%<
- * Attach '*targetp' to ACL environment 'source'.
- *
- * Requires:
- *\li  'source' is a valid ACL environment.
- *\li  'targetp' is not NULL and '*targetp' is NULL.
- */
-
-void
-dns_aclenv_detach(dns_aclenv_t **aclenvp);
-/*%<
- * Detach an ACL environment; on final detach, destroy it.
- *
- * Requires:
- *\li  '*aclenvp' to be a valid ACL environment
- */
+#if DNS_ACL_TRACE
+#define dns_aclenv_ref(ptr) dns_aclenv__ref(ptr, __func__, __FILE__, __LINE__)
+#define dns_aclenv_unref(ptr) \
+       dns_aclenv__unref(ptr, __func__, __FILE__, __LINE__)
+#define dns_aclenv_attach(ptr, ptrp) \
+       dns_aclenv__attach(ptr, ptrp, __func__, __FILE__, __LINE__)
+#define dns_aclenv_detach(ptrp) \
+       dns_aclenv__detach(ptrp, __func__, __FILE__, __LINE__)
+ISC_REFCOUNT_TRACE_DECL(dns_aclenv);
+#else
+ISC_REFCOUNT_DECL(dns_aclenv);
+#endif
 
 isc_result_t
 dns_acl_match(const isc_netaddr_t *reqaddr, const dns_name_t *reqsigner,