From: Ondřej Surý Date: Wed, 5 Oct 2022 09:19:52 +0000 (+0200) Subject: Add extra set of ISC_REFCOUNT_TRACE_{IMPL,DECL} macros X-Git-Tag: v9.19.8~35^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=118ae66976e72b5b8c12a7ed20091813cc24b246;p=thirdparty%2Fbind9.git Add extra set of ISC_REFCOUNT_TRACE_{IMPL,DECL} macros The new ISC_REFCOUNT_TRACE_{IMPL,DECL} macros can be used to add a reference tracing capability to any unit using the reference counting. It requires a little bit of extra work in each header as you can't have a define from inside a define (see rpz.h), but it's fairly easy to add tracing to any struct using reference counting with these macros. --- diff --git a/lib/isc/include/isc/refcount.h b/lib/isc/include/isc/refcount.h index e33cd7df25b..80ba888d06d 100644 --- a/lib/isc/include/isc/refcount.h +++ b/lib/isc/include/isc/refcount.h @@ -149,6 +149,53 @@ isc_refcount_decrement(isc_refcount_t *target) { ISC_INSIST(_refs > 0); \ } while (0) +#define ISC_REFCOUNT_TRACE_DECL(name) \ + void name##__ref(name##_t *ptr, const char *func, const char *file, \ + unsigned int line); \ + void name##__unref(name##_t *ptr, const char *func, const char *file, \ + unsigned int line); \ + void name##__attach(name##_t *ptr, name##_t **ptrp, const char *func, \ + const char *file, unsigned int line); \ + void name##__detach(name##_t **ptrp, const char *func, \ + const char *file, unsigned int line) + +#define ISC_REFCOUNT_TRACE_IMPL(name, destroy) \ + void name##__ref(name##_t *ptr, const char *func, const char *file, \ + unsigned int line) { \ + uint_fast32_t refs; \ + REQUIRE(ptr != NULL); \ + refs = isc_refcount_increment(&ptr->references); \ + fprintf(stderr, \ + "%s:%s:%s:%u:%p->references = %" PRIuFAST32 "\n", \ + __func__, func, file, line, ptr, refs + 1); \ + } \ + \ + void name##__unref(name##_t *ptr, const char *func, const char *file, \ + unsigned int line) { \ + uint_fast32_t refs; \ + REQUIRE(ptr != NULL); \ + if ((refs = isc_refcount_decrement(&ptr->references)) == 1) { \ + destroy(ptr); \ + } \ + fprintf(stderr, \ + "%s:%s:%s:%u:%p->references = %" PRIuFAST32 "\n", \ + __func__, func, file, line, ptr, refs - 1); \ + } \ + void name##__attach(name##_t *ptr, name##_t **ptrp, const char *func, \ + const char *file, unsigned int line) { \ + REQUIRE(ptrp != NULL && *ptrp == NULL); \ + name##__ref(ptr, func, file, line); \ + *ptrp = ptr; \ + } \ + \ + void name##__detach(name##_t **ptrp, const char *func, \ + const char *file, unsigned int line) { \ + REQUIRE(ptrp != NULL && *ptrp != NULL); \ + name##_t *ptr = *ptrp; \ + *ptrp = NULL; \ + name##__unref(ptr, func, file, line); \ + } + #define ISC_REFCOUNT_DECL(name) \ void name##_ref(name##_t *ptr); \ void name##_unref(name##_t *ptr); \