From 3ec39aa29508fad3b0ff141d4b788ea3e19d0dce Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Fri, 14 Jun 2024 15:21:49 -0400 Subject: [PATCH] xo-refcnt: exclude logging unless XO_INTRUSIVE_PTR_ENABLE_LOGGING --- include/xo/refcnt/Refcounted.hpp | 32 ++++++++++++++++++++++++++++++-- src/Refcounted.cpp | 2 ++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/include/xo/refcnt/Refcounted.hpp b/include/xo/refcnt/Refcounted.hpp index 3db621a..b54d5b5 100644 --- a/include/xo/refcnt/Refcounted.hpp +++ b/include/xo/refcnt/Refcounted.hpp @@ -28,7 +28,9 @@ namespace xo { public: intrusive_ptr() : ptr_(nullptr) {} intrusive_ptr(T * x) : ptr_(x) { +#ifdef XO_INTRUSIVE_PTR_ENABLE_LOGGING intrusive_ptr_log_ctor(sc_self_type, this, x); +#endif intrusive_ptr_add_ref(ptr_); } /*ctor*/ @@ -38,20 +40,26 @@ namespace xo { * instrusive_ptr. */ intrusive_ptr(intrusive_ptr const & x) : ptr_(x.get()) { +#ifdef XO_INTRUSIVE_PTR_ENABLE_LOGGING intrusive_ptr_log_cctor(sc_self_type, this, x.get()); +#endif intrusive_ptr_add_ref(ptr_); } /*cctor*/ /* create from instrusive pointer to some related type S */ template intrusive_ptr(intrusive_ptr const & x) : ptr_(x.get()) { +#ifdef XO_INTRUSIVE_PTR_ENABLE_LOGGING intrusive_ptr_log_cctor(sc_self_type, this, x.get()); +#endif intrusive_ptr_add_ref(ptr_); } /*cctor*/ /* move ctor -- in this case don't need to update refcount */ intrusive_ptr(intrusive_ptr && x) : ptr_{std::move(x.ptr_)} { +#ifdef XO_INTRUSIVE_PTR_ENABLE_LOGGING intrusive_ptr_log_mctor(sc_self_type, this, ptr_); +#endif /* since we're moving from x, need to make sure x dtor * doesn't decrement refcount */ @@ -66,7 +74,9 @@ namespace xo { template intrusive_ptr(intrusive_ptr const & /*x*/, element_type * y) : ptr_{y} { if (std::is_same()) { +#ifdef XO_INTRUSIVE_PTR_ENABLE_LOGGING intrusive_ptr_log_actor(sc_self_type, this, y); +#endif intrusive_ptr_add_ref(ptr_); ; /* trivial aliasing, proceed */ } else { @@ -80,7 +90,9 @@ namespace xo { ~intrusive_ptr() { T * x = this->ptr_; +#ifdef XO_INTRUSIVE_PTR_ENABLE_LOGGING intrusive_ptr_log_dtor(sc_self_type, this, x); +#endif this->ptr_ = nullptr; @@ -103,7 +115,9 @@ namespace xo { intrusive_ptr & operator=(intrusive_ptr const & rhs) { T * x = rhs.get(); +#ifdef XO_INTRUSIVE_PTR_ENABLE_LOGGING intrusive_ptr_log_assign(sc_self_type, this, x); +#endif T * old = this->ptr_; this->ptr_ = x; @@ -115,7 +129,9 @@ namespace xo { } /*operator=*/ intrusive_ptr & operator=(intrusive_ptr && rhs) { +#ifdef XO_INTRUSIVE_PTR_ENABLE_LOGGING intrusive_ptr_log_massign(sc_self_type, this, rhs.get()); +#endif std::swap(this->ptr_, rhs.ptr_); @@ -134,7 +150,8 @@ namespace xo { }; /*intrusive_ptr*/ template - inline bool operator==(intrusive_ptr const & x, intrusive_ptr const & y) { return intrusive_ptr::compare(x, y) == 0; } + inline bool operator==(intrusive_ptr const & x, + intrusive_ptr const & y) { return intrusive_ptr::compare(x, y) == 0; } template using rp = intrusive_ptr; @@ -171,6 +188,7 @@ namespace xo { return 0; } /*intrusive_ptr_refcount*/ +#ifdef XO_INTRUSIVE_PTR_ENABLE_LOGGING extern void intrusive_ptr_set_debug(bool x); extern void intrusive_ptr_log_ctor(std::string_view const & self_type, void * this_ptr, @@ -193,7 +211,8 @@ namespace xo { Refcount * x); extern void intrusive_ptr_log_massign(std::string_view const & self_type, void *this_ptr, - Refcount * x); + Refcount * x); +#endif extern void intrusive_ptr_add_ref(Refcount * x); extern void intrusive_ptr_release(Refcount * x); @@ -208,6 +227,15 @@ namespace xo { return os; } /*operator<<*/ + /** Wrap a (presumably non-reference-counted) class T so that it has a refcount. + **/ + template + class RefcountWrapper : public Refcount, public T { + public: + template + RefcountWrapper(Args... args) : Refcount(), T(std::forward(args...)) {} + }; + /* borrow a reference-counted pointer to pass down the stack * 1. borrowed pointer intended to replace: * a. code like diff --git a/src/Refcounted.cpp b/src/Refcounted.cpp index eade7a5..38accc8 100644 --- a/src/Refcounted.cpp +++ b/src/Refcounted.cpp @@ -4,6 +4,7 @@ namespace xo { namespace ref { +#ifdef XO_INTRUSIVE_PTR_ENABLE_LOGGING namespace { /* verbose logging for intrusive_ptr */ static bool s_logging_enabled = false; @@ -89,6 +90,7 @@ namespace xo { if (s_logging_enabled) intrusive_ptr_log_aux(self_type, "::m=", this_ptr, x); } /*intrusive_ptr_log_massign*/ +#endif void intrusive_ptr_add_ref(Refcount * x)