Fix the ringbuffer write size
[openal-soft.git] / common / intrusive_ptr.h
blob9e206a6b6270ebfb37ce33b19de9312a203bb0ca
1 #ifndef INTRUSIVE_PTR_H
2 #define INTRUSIVE_PTR_H
4 #include <utility>
6 #include "atomic.h"
7 #include "opthelpers.h"
10 namespace al {
12 template<typename T>
13 class intrusive_ref {
14 RefCount mRef{1u};
16 public:
17 unsigned int add_ref() noexcept { return IncrementRef(mRef); }
18 unsigned int release() noexcept
20 auto ref = DecrementRef(mRef);
21 if UNLIKELY(ref == 0)
22 delete static_cast<T*>(this);
23 return ref;
26 /**
27 * Release only if doing so would not bring the object to 0 references and
28 * delete it. Returns false if the object could not be released.
30 * NOTE: The caller is responsible for handling a failed release, as it
31 * means the object has no other references and needs to be be deleted
32 * somehow.
34 bool releaseIfNoDelete() noexcept
36 auto val = mRef.load(std::memory_order_acquire);
37 while(val > 1 && !mRef.compare_exchange_strong(val, val-1, std::memory_order_acq_rel))
39 /* val was updated with the current value on failure, so just try
40 * again.
44 return val >= 2;
49 template<typename T>
50 class intrusive_ptr {
51 T *mPtr{nullptr};
53 public:
54 intrusive_ptr() noexcept = default;
55 intrusive_ptr(const intrusive_ptr &rhs) noexcept : mPtr{rhs.mPtr}
56 { if(mPtr) mPtr->add_ref(); }
57 intrusive_ptr(intrusive_ptr&& rhs) noexcept : mPtr{rhs.mPtr}
58 { rhs.mPtr = nullptr; }
59 intrusive_ptr(std::nullptr_t) noexcept { }
60 explicit intrusive_ptr(T *ptr) noexcept : mPtr{ptr} { }
61 ~intrusive_ptr() { if(mPtr) mPtr->release(); }
63 intrusive_ptr& operator=(const intrusive_ptr &rhs) noexcept
65 static_assert(noexcept(std::declval<T*>()->release()), "release must be noexcept");
67 if(rhs.mPtr) rhs.mPtr->add_ref();
68 if(mPtr) mPtr->release();
69 mPtr = rhs.mPtr;
70 return *this;
72 intrusive_ptr& operator=(intrusive_ptr&& rhs) noexcept
74 if(likely(&rhs != this))
76 if(mPtr) mPtr->release();
77 mPtr = std::exchange(rhs.mPtr, nullptr);
79 return *this;
82 explicit operator bool() const noexcept { return mPtr != nullptr; }
84 T& operator*() const noexcept { return *mPtr; }
85 T* operator->() const noexcept { return mPtr; }
86 T* get() const noexcept { return mPtr; }
88 void reset(T *ptr=nullptr) noexcept
90 if(mPtr)
91 mPtr->release();
92 mPtr = ptr;
95 T* release() noexcept { return std::exchange(mPtr, nullptr); }
97 void swap(intrusive_ptr &rhs) noexcept { std::swap(mPtr, rhs.mPtr); }
98 void swap(intrusive_ptr&& rhs) noexcept { std::swap(mPtr, rhs.mPtr); }
101 #define AL_DECL_OP(op) \
102 template<typename T> \
103 inline bool operator op(const intrusive_ptr<T> &lhs, const T *rhs) noexcept \
104 { return lhs.get() op rhs; } \
105 template<typename T> \
106 inline bool operator op(const T *lhs, const intrusive_ptr<T> &rhs) noexcept \
107 { return lhs op rhs.get(); }
109 AL_DECL_OP(==)
110 AL_DECL_OP(!=)
111 AL_DECL_OP(<=)
112 AL_DECL_OP(>=)
113 AL_DECL_OP(<)
114 AL_DECL_OP(>)
116 #undef AL_DECL_OP
118 } // namespace al
120 #endif /* INTRUSIVE_PTR_H */