diff options
Diffstat (limited to 'contrib/llvm-project/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h')
-rw-r--r-- | contrib/llvm-project/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h | 45 |
1 files changed, 42 insertions, 3 deletions
diff --git a/contrib/llvm-project/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h b/contrib/llvm-project/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h index 6d97fe15db8b..ca4c40db48b9 100644 --- a/contrib/llvm-project/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h +++ b/contrib/llvm-project/llvm/include/llvm/ADT/IntrusiveRefCntPtr.h @@ -58,6 +58,7 @@ #include <atomic> #include <cassert> #include <cstddef> +#include <memory> namespace llvm { @@ -70,10 +71,23 @@ namespace llvm { template <class Derived> class RefCountedBase { mutable unsigned RefCount = 0; -public: +protected: RefCountedBase() = default; RefCountedBase(const RefCountedBase &) {} + RefCountedBase &operator=(const RefCountedBase &) = delete; + +#ifndef NDEBUG + ~RefCountedBase() { + assert(RefCount == 0 && + "Destruction occured when there are still references to this."); + } +#else + // Default the destructor in release builds, A trivial destructor may enable + // better codegen. + ~RefCountedBase() = default; +#endif +public: void Retain() const { ++RefCount; } void Release() const { @@ -85,10 +99,24 @@ public: /// A thread-safe version of \c RefCountedBase. template <class Derived> class ThreadSafeRefCountedBase { - mutable std::atomic<int> RefCount; + mutable std::atomic<int> RefCount{0}; protected: - ThreadSafeRefCountedBase() : RefCount(0) {} + ThreadSafeRefCountedBase() = default; + ThreadSafeRefCountedBase(const ThreadSafeRefCountedBase &) {} + ThreadSafeRefCountedBase & + operator=(const ThreadSafeRefCountedBase &) = delete; + +#ifndef NDEBUG + ~ThreadSafeRefCountedBase() { + assert(RefCount == 0 && + "Destruction occured when there are still references to this."); + } +#else + // Default the destructor in release builds, A trivial destructor may enable + // better codegen. + ~ThreadSafeRefCountedBase() = default; +#endif public: void Retain() const { RefCount.fetch_add(1, std::memory_order_relaxed); } @@ -149,6 +177,11 @@ public: } template <class X> + IntrusiveRefCntPtr(std::unique_ptr<X> S) : Obj(S.release()) { + retain(); + } + + template <class X> IntrusiveRefCntPtr(const IntrusiveRefCntPtr<X> &S) : Obj(S.get()) { retain(); } @@ -264,6 +297,12 @@ template <class T> struct simplify_type<const IntrusiveRefCntPtr<T>> { } }; +/// Factory function for creating intrusive ref counted pointers. +template <typename T, typename... Args> +IntrusiveRefCntPtr<T> makeIntrusiveRefCnt(Args &&...A) { + return IntrusiveRefCntPtr<T>(new T(std::forward<Args>(A)...)); +} + } // end namespace llvm #endif // LLVM_ADT_INTRUSIVEREFCNTPTR_H |