SharedPtr.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 : _ptr((T *)0) {}
  30. explicit ZT_INLINE SharedPtr(T *obj) noexcept : _ptr(obj) { ++obj->__refCount; }
  31. ZT_INLINE SharedPtr(const SharedPtr &sp) noexcept : _ptr(sp._getAndInc()) {}
  32. ZT_INLINE ~SharedPtr()
  33. {
  34. if (_ptr) {
  35. if (--_ptr->__refCount <= 0)
  36. delete _ptr;
  37. }
  38. }
  39. ZT_INLINE SharedPtr &operator=(const SharedPtr &sp)
  40. {
  41. if (_ptr != sp._ptr) {
  42. T *p = sp._getAndInc();
  43. if (_ptr) {
  44. if (--_ptr->__refCount <= 0)
  45. delete _ptr;
  46. }
  47. _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. _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 { _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 = _ptr;
  81. _ptr = with._ptr;
  82. with._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 (_ptr) {
  95. if (--_ptr->__refCount <= 0)
  96. delete _ptr;
  97. }
  98. _ptr = from._ptr;
  99. from._ptr = nullptr;
  100. }
  101. ZT_INLINE operator bool() const noexcept { return (_ptr != nullptr); }
  102. ZT_INLINE T &operator*() const noexcept { return *_ptr; }
  103. ZT_INLINE T *operator->() const noexcept { return _ptr; }
  104. /**
  105. * @return Raw pointer to held object
  106. */
  107. ZT_INLINE T *ptr() const noexcept { return _ptr; }
  108. /**
  109. * Set this pointer to NULL
  110. */
  111. ZT_INLINE void zero()
  112. {
  113. if (_ptr) {
  114. if (--_ptr->__refCount <= 0)
  115. delete _ptr;
  116. _ptr = (T *)0;
  117. }
  118. }
  119. /**
  120. * @return Number of references according to this object's ref count or 0 if NULL
  121. */
  122. ZT_INLINE int references() noexcept
  123. {
  124. if (_ptr)
  125. return _ptr->__refCount;
  126. return 0;
  127. }
  128. ZT_INLINE bool operator==(const SharedPtr &sp) const noexcept { return (_ptr == sp._ptr); }
  129. ZT_INLINE bool operator!=(const SharedPtr &sp) const noexcept { return (_ptr != sp._ptr); }
  130. ZT_INLINE bool operator>(const SharedPtr &sp) const noexcept { return (_ptr > sp._ptr); }
  131. ZT_INLINE bool operator<(const SharedPtr &sp) const noexcept { return (_ptr < sp._ptr); }
  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. private:
  135. ZT_INLINE T *_getAndInc() const noexcept
  136. {
  137. if (_ptr)
  138. ++_ptr->__refCount;
  139. return _ptr;
  140. }
  141. T *_ptr;
  142. };
  143. } // namespace ZeroTier
  144. namespace std {
  145. template<typename T>
  146. ZT_INLINE void swap(ZeroTier::SharedPtr<T> &a,ZeroTier::SharedPtr<T> &b) noexcept { a.swap(b); }
  147. }
  148. #endif