intrusive_ptr.h 3.0 KB

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