SharedPtr.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_SHAREDPTR_HPP
  14. #define ZT_SHAREDPTR_HPP
  15. #include "Constants.hpp"
  16. #include "TriviallyCopyable.hpp"
  17. namespace ZeroTier {
  18. /**
  19. * Simple zero-overhead introspective reference counted pointer
  20. *
  21. * This is an introspective shared pointer. Classes that need to be reference
  22. * counted must list this as a 'friend' and must have a private instance of
  23. * atomic<int> called __refCount.
  24. */
  25. template<typename T>
  26. class SharedPtr : public TriviallyCopyable
  27. {
  28. public:
  29. ZT_INLINE SharedPtr() noexcept : m_ptr((T *)0) {}
  30. explicit ZT_INLINE SharedPtr(T *obj) noexcept : m_ptr(obj) { ++obj->__refCount; }
  31. ZT_INLINE SharedPtr(const SharedPtr &sp) noexcept : m_ptr(sp._getAndInc()) {}
  32. ZT_INLINE ~SharedPtr()
  33. {
  34. if (m_ptr) {
  35. if (--m_ptr->__refCount <= 0)
  36. delete m_ptr;
  37. }
  38. }
  39. ZT_INLINE SharedPtr &operator=(const SharedPtr &sp)
  40. {
  41. if (m_ptr != sp.m_ptr) {
  42. T *p = sp._getAndInc();
  43. if (m_ptr) {
  44. if (--m_ptr->__refCount <= 0)
  45. delete m_ptr;
  46. }
  47. m_ptr = p;
  48. }
  49. return *this;
  50. }
  51. /**
  52. * Set to a naked pointer and increment its reference count
  53. *
  54. * This assumes this SharedPtr is NULL and that ptr is not a 'zombie.' No
  55. * checks are performed.
  56. *
  57. * @param ptr Naked pointer to assign
  58. */
  59. ZT_INLINE void set(T *ptr) noexcept
  60. {
  61. zero();
  62. ++ptr->__refCount;
  63. m_ptr = ptr;
  64. }
  65. /**
  66. * Stupidly set this SharedPtr to 'ptr', ignoring current value and not incrementing reference counter
  67. *
  68. * This must only be used in code that knows what it's doing. :)
  69. *
  70. * @param ptr Pointer to set
  71. */
  72. ZT_INLINE void unsafeSet(T *ptr) noexcept { m_ptr = ptr; }
  73. /**
  74. * Swap with another pointer 'for free' without ref count overhead
  75. *
  76. * @param with Pointer to swap with
  77. */
  78. ZT_INLINE void swap(SharedPtr &with) noexcept
  79. {
  80. T *tmp = m_ptr;
  81. m_ptr = with.m_ptr;
  82. with.m_ptr = tmp;
  83. }
  84. /**
  85. * Set this value to one from another pointer and set that pointer to zero (take ownership from)
  86. *
  87. * This is faster than setting and zeroing the source pointer since it
  88. * avoids a synchronized reference count change.
  89. *
  90. * @param from Origin pointer; will be zeroed
  91. */
  92. ZT_INLINE void move(SharedPtr &from)
  93. {
  94. if (m_ptr) {
  95. if (--m_ptr->__refCount <= 0)
  96. delete m_ptr;
  97. }
  98. m_ptr = from.m_ptr;
  99. from.m_ptr = nullptr;
  100. }
  101. ZT_INLINE operator bool() const noexcept { return (m_ptr != nullptr); } // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
  102. ZT_INLINE T &operator*() const noexcept { return *m_ptr; }
  103. ZT_INLINE T *operator->() const noexcept { return m_ptr; }
  104. /**
  105. * @return Raw pointer to held object
  106. */
  107. ZT_INLINE T *ptr() const noexcept { return m_ptr; }
  108. /**
  109. * Set this pointer to NULL
  110. */
  111. ZT_INLINE void zero()
  112. {
  113. if (m_ptr) {
  114. if (--m_ptr->__refCount <= 0)
  115. delete m_ptr;
  116. m_ptr = nullptr;
  117. }
  118. }
  119. /**
  120. * Set pointer to NULL and delete object if reference count is only 1
  121. *
  122. * This can be called periodically to implement something like a weak
  123. * reference as it exists in other more managed languages like Java,
  124. * but with the caveat that it only works if there is only one remaining
  125. * SharedPtr to be treated as weak.
  126. *
  127. * @return True if object was in fact deleted or was already zero/NULL
  128. */
  129. ZT_INLINE bool weakGC()
  130. {
  131. if (m_ptr) {
  132. if (m_ptr->__refCount.compare_exchange_strong(1,0)) {
  133. delete m_ptr;
  134. m_ptr = nullptr;
  135. return true;
  136. }
  137. return false;
  138. } else {
  139. return true;
  140. }
  141. }
  142. /**
  143. * @return Number of references according to this object's ref count or 0 if NULL
  144. */
  145. ZT_INLINE int references() noexcept
  146. {
  147. if (m_ptr)
  148. return m_ptr->__refCount;
  149. return 0;
  150. }
  151. ZT_INLINE bool operator==(const SharedPtr &sp) const noexcept { return (m_ptr == sp.m_ptr); }
  152. ZT_INLINE bool operator!=(const SharedPtr &sp) const noexcept { return (m_ptr != sp.m_ptr); }
  153. ZT_INLINE bool operator>(const SharedPtr &sp) const noexcept { return (m_ptr > sp.m_ptr); }
  154. ZT_INLINE bool operator<(const SharedPtr &sp) const noexcept { return (m_ptr < sp.m_ptr); }
  155. ZT_INLINE bool operator>=(const SharedPtr &sp) const noexcept { return (m_ptr >= sp.m_ptr); }
  156. ZT_INLINE bool operator<=(const SharedPtr &sp) const noexcept { return (m_ptr <= sp.m_ptr); }
  157. private:
  158. ZT_INLINE T *_getAndInc() const noexcept
  159. {
  160. if (m_ptr)
  161. ++m_ptr->__refCount;
  162. return m_ptr;
  163. }
  164. T *m_ptr;
  165. };
  166. } // namespace ZeroTier
  167. namespace std {
  168. template<typename T>
  169. ZT_INLINE void swap(ZeroTier::SharedPtr<T> &a,ZeroTier::SharedPtr<T> &b) noexcept { a.swap(b); }
  170. }
  171. #endif