From ae3417aab5c1d0da9684fe897f8ae294d0c9dfa5 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 1 Jan 2017 02:07:19 -0500 Subject: [PATCH] In OwnedPtrDeleter, store a vptr for a behavior function. Storing the behavior enum value results in more generated code, since the call operator needs to perform a comparison. --- zcpointer.h | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/zcpointer.h b/zcpointer.h index d77bb63..2301ef1 100644 --- a/zcpointer.h +++ b/zcpointer.h @@ -42,31 +42,34 @@ enum class OwnershipBehavior { template class OwnedPtrDeleter { public: - OwnedPtrDeleter() : refs_(), behavior_(OwnershipBehavior::DELETE_POINTER) {} + OwnedPtrDeleter() + : refs_(), + finalizer_(&OwnedPtrDeleter::HandleDeletePointer) {} + ~OwnedPtrDeleter() {} explicit OwnedPtrDeleter(OwnershipBehavior behavior) : refs_(), - behavior_(behavior) { + finalizer_(behavior == OwnershipBehavior::BORROW_POINTER + ? &OwnedPtrDeleter::HandleBorrowPointer + : &OwnedPtrDeleter::HandleDeletePointer) { } OwnedPtrDeleter(OwnedPtrDeleter&& other) : refs_(std::move(other.refs_)), - behavior_(other.behavior_) { + finalizer_(other.finalizer_) { } void operator=(const OwnedPtrDeleter& o) { refs_ = o.refs_; - behavior_ = o.behavior_; + finalizer_ = o.finalizer_; } void operator()(T* t) const { for (auto& ref : refs_) { ref->MarkDeleted(); } - if (behavior_ == OwnershipBehavior::DELETE_POINTER) { - delete t; - } + (this->finalizer_)(t); } protected: @@ -80,9 +83,15 @@ class OwnedPtrDeleter { refs_.remove(ref); } + static void HandleDeletePointer(T* t) { + delete t; + } + + static void HandleBorrowPointer(T* t) {} + private: + void (*finalizer_)(T*); std::forward_list*> refs_; - OwnershipBehavior behavior_; }; void RaiseUseAfterFree() __attribute__((noreturn)); -- 2.43.5