intrusive_ptr.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #ifndef INTRUSIVE_PTR_H
  2. #define INTRUSIVE_PTR_H
  3. #include "atomic.h"
  4. #include "opthelpers.h"
  5. namespace al {
  6. template<typename T>
  7. class intrusive_ref {
  8. RefCount mRef{1u};
  9. public:
  10. unsigned int add_ref() noexcept { return IncrementRef(mRef); }
  11. unsigned int release() noexcept
  12. {
  13. auto ref = DecrementRef(mRef);
  14. if UNLIKELY(ref == 0)
  15. delete static_cast<T*>(this);
  16. return ref;
  17. }
  18. /**
  19. * Release only if doing so would not bring the object to 0 references and
  20. * delete it. Returns false if the object could not be released.
  21. *
  22. * NOTE: The caller is responsible for handling a failed release, as it
  23. * means the object has no other references and needs to be be deleted
  24. * somehow.
  25. */
  26. bool releaseIfNoDelete() noexcept
  27. {
  28. auto val = mRef.load(std::memory_order_acquire);
  29. while(val > 1 && !mRef.compare_exchange_strong(val, val-1, std::memory_order_acq_rel))
  30. {
  31. /* val was updated with the current value on failure, so just try
  32. * again.
  33. */
  34. }
  35. return val >= 2;
  36. }
  37. };
  38. template<typename T>
  39. class intrusive_ptr {
  40. T *mPtr{nullptr};
  41. public:
  42. intrusive_ptr() noexcept = default;
  43. intrusive_ptr(const intrusive_ptr &rhs) noexcept : mPtr{rhs.mPtr}
  44. { if(mPtr) mPtr->add_ref(); }
  45. intrusive_ptr(intrusive_ptr&& rhs) noexcept : mPtr{rhs.mPtr}
  46. { rhs.mPtr = nullptr; }
  47. intrusive_ptr(std::nullptr_t) noexcept { }
  48. explicit intrusive_ptr(T *ptr) noexcept : mPtr{ptr} { }
  49. ~intrusive_ptr() { if(mPtr) mPtr->release(); }
  50. intrusive_ptr& operator=(const intrusive_ptr &rhs) noexcept
  51. {
  52. if(rhs.mPtr) rhs.mPtr->add_ref();
  53. if(mPtr) mPtr->release();
  54. mPtr = rhs.mPtr;
  55. return *this;
  56. }
  57. intrusive_ptr& operator=(intrusive_ptr&& rhs) noexcept
  58. {
  59. if(mPtr)
  60. mPtr->release();
  61. mPtr = rhs.mPtr;
  62. rhs.mPtr = nullptr;
  63. return *this;
  64. }
  65. operator bool() const noexcept { return mPtr != nullptr; }
  66. T& operator*() const noexcept { return *mPtr; }
  67. T* operator->() const noexcept { return mPtr; }
  68. T* get() const noexcept { return mPtr; }
  69. void reset(T *ptr=nullptr) noexcept
  70. {
  71. if(mPtr)
  72. mPtr->release();
  73. mPtr = ptr;
  74. }
  75. T* release() noexcept
  76. {
  77. T *ret{mPtr};
  78. mPtr = nullptr;
  79. return ret;
  80. }
  81. void swap(intrusive_ptr &rhs) noexcept { std::swap(mPtr, rhs.mPtr); }
  82. void swap(intrusive_ptr&& rhs) noexcept { std::swap(mPtr, rhs.mPtr); }
  83. };
  84. #define AL_DECL_OP(op) \
  85. template<typename T> \
  86. inline bool operator op(const intrusive_ptr<T> &lhs, const T *rhs) noexcept \
  87. { return lhs.get() op rhs; } \
  88. template<typename T> \
  89. inline bool operator op(const T *lhs, const intrusive_ptr<T> &rhs) noexcept \
  90. { return lhs op rhs.get(); }
  91. AL_DECL_OP(==)
  92. AL_DECL_OP(!=)
  93. AL_DECL_OP(<=)
  94. AL_DECL_OP(>=)
  95. AL_DECL_OP(<)
  96. AL_DECL_OP(>)
  97. #undef AL_DECL_OP
  98. } // namespace al
  99. #endif /* INTRUSIVE_PTR_H */