diff --git a/modules/beast_asio/async/beast_SharedHandler.h b/modules/beast_asio/async/beast_SharedHandler.h index e6c573eb31..756b036d5b 100644 --- a/modules/beast_asio/async/beast_SharedHandler.h +++ b/modules/beast_asio/async/beast_SharedHandler.h @@ -77,7 +77,6 @@ public: virtual void* allocate (std::size_t size) = 0; virtual void deallocate (void* p, std::size_t size) = 0; virtual bool is_continuation () = 0; - virtual void destroy () = 0; static void pure_virtual_called (char const* fileName, int lineNumber); @@ -87,22 +86,6 @@ private: friend void* asio_handler_allocate (std::size_t, SharedHandler*); friend void asio_handler_deallocate (void*, std::size_t, SharedHandler*); friend bool asio_handler_is_continuation (SharedHandler*); - friend struct ContainerDeletePolicy ; -}; - -//-------------------------------------------------------------------------- - -// For SharedPtr -template <> -struct ContainerDeletePolicy -{ - // SharedPtr will use this when - // the reference count drops to zero. - // - inline static void destroy (SharedHandler* handler) - { - handler->destroy (); - } }; //-------------------------------------------------------------------------- diff --git a/modules/beast_asio/async/beast_SharedHandlerType.h b/modules/beast_asio/async/beast_SharedHandlerType.h index 90ffc068e5..5e993ca147 100644 --- a/modules/beast_asio/async/beast_SharedHandlerType.h +++ b/modules/beast_asio/async/beast_SharedHandlerType.h @@ -69,9 +69,9 @@ protected: #endif } - // Called by our ContainerDeletePolicy hook to destroy the - // object. We need this because we allocated it using a custom - // allocator. Destruction is tricky, the algorithm is as follows: + // Called by SharedObject hook to destroy the object. We need + // this because we allocated it using a custom allocator. + // Destruction is tricky, the algorithm is as follows: // // First we move-assign the handler to our stack. If the build // doesn't support move-assignment it will be a copy, still ok. diff --git a/modules/beast_core/memory/SharedObject.h b/modules/beast_core/memory/SharedObject.h index 68147654df..c2853db0a6 100644 --- a/modules/beast_core/memory/SharedObject.h +++ b/modules/beast_core/memory/SharedObject.h @@ -79,20 +79,18 @@ public: The return value indicates if the reference count dropped to zero, so callers who know the derived type can use the ContainerDeletePolicy. */ - inline bool decReferenceCount (bool doDelete = true) noexcept + void decReferenceCount () { bassert (getReferenceCount() > 0); if (--refCount == 0) - { - if (doDelete) - delete this; - return true; - } - return false; + destroy (); } /** Returns the object's current reference count. */ - inline int getReferenceCount() const noexcept { return refCount.get(); } + inline int getReferenceCount() const noexcept + { + return refCount.get(); + } protected: //============================================================================== @@ -108,6 +106,14 @@ protected: bassert (getReferenceCount() == 0); } + /** Destroy the object. + Derived classes can override this for different behaviors. + */ + virtual void destroy () + { + delete this; + } + /** Resets the reference count to zero without deleting the object. You should probably never need to use this! */ @@ -155,17 +161,11 @@ public: The return value indicates if the reference count dropped to zero, so callers who know the derived type can use the ContainerDeletePolicy. */ - inline bool decReferenceCount (bool doDelete = true) noexcept + inline void decReferenceCount () { bassert (getReferenceCount() > 0); if (--refCount == 0) - { - if (doDelete) - delete this; - else - return true; - } - return false; + destroy (); } /** Returns the object's current reference count. */ @@ -184,6 +184,11 @@ protected: bassert (getReferenceCount() == 0); } + virtual void destroy () + { + delete this; + } + private: //============================================================================== int refCount; diff --git a/modules/beast_core/memory/SharedPtr.h b/modules/beast_core/memory/SharedPtr.h index a8e10bbaa3..68333e93c4 100644 --- a/modules/beast_core/memory/SharedPtr.h +++ b/modules/beast_core/memory/SharedPtr.h @@ -238,8 +238,7 @@ private: static void release (T* t) { if (t != nullptr) - if (t->decReferenceCount (false)) - ContainerDeletePolicy ::destroy (t); + t->decReferenceCount (); } // Swap ownership of the currently referenced object. diff --git a/modules/beast_core/memory/beast_SharedSingleton.h b/modules/beast_core/memory/beast_SharedSingleton.h index e5eefdc9e6..093976a757 100644 --- a/modules/beast_core/memory/beast_SharedSingleton.h +++ b/modules/beast_core/memory/beast_SharedSingleton.h @@ -139,11 +139,10 @@ public: ++m_refCount; } - inline bool decReferenceCount (bool = false) noexcept + inline void decReferenceCount () noexcept { if (--m_refCount == 0) destroySingleton (); - return false; } // Caller must synchronize.