SharedPtr.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. * This is technically TriviallyCopyable but extreme care must be taken if
  26. * one wishes to handle it in this manner. A memcpy must be followed by a
  27. * memset of the source to 0 so as to achieve 'move' semantics.
  28. */
  29. template<typename T>
  30. class SharedPtr : public TriviallyCopyable
  31. {
  32. public:
  33. ZT_INLINE SharedPtr() noexcept : _ptr((T *)0) {}
  34. explicit ZT_INLINE SharedPtr(T *obj) noexcept : _ptr(obj) { ++obj->__refCount; }
  35. ZT_INLINE SharedPtr(const SharedPtr &sp) noexcept : _ptr(sp._getAndInc()) {}
  36. ZT_INLINE ~SharedPtr()
  37. {
  38. if (_ptr) {
  39. if (--_ptr->__refCount <= 0)
  40. delete _ptr;
  41. }
  42. }
  43. ZT_INLINE SharedPtr &operator=(const SharedPtr &sp)
  44. {
  45. if (_ptr != sp._ptr) {
  46. T *p = sp._getAndInc();
  47. if (_ptr) {
  48. if (--_ptr->__refCount <= 0)
  49. delete _ptr;
  50. }
  51. _ptr = p;
  52. }
  53. return *this;
  54. }
  55. /**
  56. * Set to a naked pointer and increment its reference count
  57. *
  58. * This assumes this SharedPtr is NULL and that ptr is not a 'zombie.' No
  59. * checks are performed.
  60. *
  61. * @param ptr Naked pointer to assign
  62. */
  63. ZT_INLINE void set(T *ptr) noexcept
  64. {
  65. zero();
  66. ++ptr->__refCount;
  67. _ptr = ptr;
  68. }
  69. /**
  70. * Stupidly set this SharedPtr to 'ptr', ignoring current value and not incrementing reference counter
  71. *
  72. * This must only be used in code that knows what it's doing. :)
  73. *
  74. * @param ptr Pointer to set
  75. */
  76. ZT_INLINE void unsafeSet(T *ptr) noexcept { _ptr = ptr; }
  77. /**
  78. * Swap with another pointer 'for free' without ref count overhead
  79. *
  80. * @param with Pointer to swap with
  81. */
  82. ZT_INLINE void swap(SharedPtr &with) noexcept
  83. {
  84. T *tmp = _ptr;
  85. _ptr = with._ptr;
  86. with._ptr = tmp;
  87. }
  88. /**
  89. * Set this value to one from another pointer and set that pointer to zero (take ownership from)
  90. *
  91. * This is faster than setting and zeroing the source pointer since it
  92. * avoids a synchronized reference count change.
  93. *
  94. * @param from Origin pointer; will be zeroed
  95. */
  96. ZT_INLINE void move(SharedPtr &from)
  97. {
  98. if (_ptr) {
  99. if (--_ptr->__refCount <= 0)
  100. delete _ptr;
  101. }
  102. _ptr = from._ptr;
  103. from._ptr = nullptr;
  104. }
  105. ZT_INLINE operator bool() const noexcept { return (_ptr != nullptr); }
  106. ZT_INLINE T &operator*() const noexcept { return *_ptr; }
  107. ZT_INLINE T *operator->() const noexcept { return _ptr; }
  108. /**
  109. * @return Raw pointer to held object
  110. */
  111. ZT_INLINE T *ptr() const noexcept { return _ptr; }
  112. /**
  113. * Set this pointer to NULL
  114. */
  115. ZT_INLINE void zero()
  116. {
  117. if (_ptr) {
  118. if (--_ptr->__refCount <= 0)
  119. delete _ptr;
  120. _ptr = (T *)0;
  121. }
  122. }
  123. /**
  124. * @return Number of references according to this object's ref count or 0 if NULL
  125. */
  126. ZT_INLINE int references() noexcept
  127. {
  128. if (_ptr)
  129. return _ptr->__refCount;
  130. return 0;
  131. }
  132. ZT_INLINE bool operator==(const SharedPtr &sp) const noexcept { return (_ptr == sp._ptr); }
  133. ZT_INLINE bool operator!=(const SharedPtr &sp) const noexcept { return (_ptr != sp._ptr); }
  134. ZT_INLINE bool operator>(const SharedPtr &sp) const noexcept { return (_ptr > sp._ptr); }
  135. ZT_INLINE bool operator<(const SharedPtr &sp) const noexcept { return (_ptr < sp._ptr); }
  136. ZT_INLINE bool operator>=(const SharedPtr &sp) const noexcept { return (_ptr >= sp._ptr); }
  137. ZT_INLINE bool operator<=(const SharedPtr &sp) const noexcept { return (_ptr <= sp._ptr); }
  138. private:
  139. ZT_INLINE T *_getAndInc() const noexcept
  140. {
  141. if (_ptr)
  142. ++_ptr->__refCount;
  143. return _ptr;
  144. }
  145. T *_ptr;
  146. };
  147. } // namespace ZeroTier
  148. namespace std {
  149. template<typename T>
  150. ZT_INLINE void swap(ZeroTier::SharedPtr<T> &a,ZeroTier::SharedPtr<T> &b) noexcept { a.swap(b); }
  151. }
  152. #endif