intrusive_ptr.h 3.1 KB

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