Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
7.5 KiB

  1. //== llvm/ADT/IntrusiveRefCntPtr.h - Smart Refcounting Pointer ---*- C++ -*-==//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines IntrusiveRefCntPtr, a template class that
  11. // implements a "smart" pointer for objects that maintain their own
  12. // internal reference count, and RefCountedBase/RefCountedBaseVPTR, two
  13. // generic base classes for objects that wish to have their lifetimes
  14. // managed using reference counting.
  15. //
  16. // IntrusiveRefCntPtr is similar to Boost's intrusive_ptr with added
  17. // LLVM-style casting.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #ifndef LLVM_ADT_INTRUSIVEREFCNTPTR_H
  21. #define LLVM_ADT_INTRUSIVEREFCNTPTR_H
  22. #include "llvm/Support/Casting.h"
  23. #include "llvm/Support/Compiler.h"
  24. #include <memory>
  25. namespace llvm {
  26. template <class T>
  27. class IntrusiveRefCntPtr;
  28. //===----------------------------------------------------------------------===//
  29. /// RefCountedBase - A generic base class for objects that wish to
  30. /// have their lifetimes managed using reference counts. Classes
  31. /// subclass RefCountedBase to obtain such functionality, and are
  32. /// typically handled with IntrusiveRefCntPtr "smart pointers" (see below)
  33. /// which automatically handle the management of reference counts.
  34. /// Objects that subclass RefCountedBase should not be allocated on
  35. /// the stack, as invoking "delete" (which is called when the
  36. /// reference count hits 0) on such objects is an error.
  37. //===----------------------------------------------------------------------===//
  38. template <class Derived>
  39. class RefCountedBase {
  40. mutable unsigned ref_cnt;
  41. public:
  42. RefCountedBase() : ref_cnt(0) {}
  43. RefCountedBase(const RefCountedBase &) : ref_cnt(0) {}
  44. void Retain() const { ++ref_cnt; }
  45. void Release() const {
  46. assert (ref_cnt > 0 && "Reference count is already zero.");
  47. if (--ref_cnt == 0) delete static_cast<const Derived*>(this);
  48. }
  49. };
  50. //===----------------------------------------------------------------------===//
  51. /// RefCountedBaseVPTR - A class that has the same function as
  52. /// RefCountedBase, but with a virtual destructor. Should be used
  53. /// instead of RefCountedBase for classes that already have virtual
  54. /// methods to enforce dynamic allocation via 'new'. Classes that
  55. /// inherit from RefCountedBaseVPTR can't be allocated on stack -
  56. /// attempting to do this will produce a compile error.
  57. //===----------------------------------------------------------------------===//
  58. class RefCountedBaseVPTR {
  59. mutable unsigned ref_cnt;
  60. virtual void anchor();
  61. protected:
  62. RefCountedBaseVPTR() : ref_cnt(0) {}
  63. RefCountedBaseVPTR(const RefCountedBaseVPTR &) : ref_cnt(0) {}
  64. virtual ~RefCountedBaseVPTR() {}
  65. void Retain() const { ++ref_cnt; }
  66. void Release() const {
  67. assert (ref_cnt > 0 && "Reference count is already zero.");
  68. if (--ref_cnt == 0) delete this;
  69. }
  70. template <typename T>
  71. friend struct IntrusiveRefCntPtrInfo;
  72. };
  73. template <typename T> struct IntrusiveRefCntPtrInfo {
  74. static void retain(T *obj) { obj->Retain(); }
  75. static void release(T *obj) { obj->Release(); }
  76. };
  77. //===----------------------------------------------------------------------===//
  78. /// IntrusiveRefCntPtr - A template class that implements a "smart pointer"
  79. /// that assumes the wrapped object has a reference count associated
  80. /// with it that can be managed via calls to
  81. /// IntrusivePtrAddRef/IntrusivePtrRelease. The smart pointers
  82. /// manage reference counts via the RAII idiom: upon creation of
  83. /// smart pointer the reference count of the wrapped object is
  84. /// incremented and upon destruction of the smart pointer the
  85. /// reference count is decremented. This class also safely handles
  86. /// wrapping NULL pointers.
  87. ///
  88. /// Reference counting is implemented via calls to
  89. /// Obj->Retain()/Obj->Release(). Release() is required to destroy
  90. /// the object when the reference count reaches zero. Inheriting from
  91. /// RefCountedBase/RefCountedBaseVPTR takes care of this
  92. /// automatically.
  93. //===----------------------------------------------------------------------===//
  94. template <typename T>
  95. class IntrusiveRefCntPtr {
  96. T* Obj;
  97. typedef IntrusiveRefCntPtr this_type;
  98. public:
  99. typedef T element_type;
  100. explicit IntrusiveRefCntPtr() : Obj(0) {}
  101. IntrusiveRefCntPtr(T* obj) : Obj(obj) {
  102. retain();
  103. }
  104. IntrusiveRefCntPtr(const IntrusiveRefCntPtr& S) : Obj(S.Obj) {
  105. retain();
  106. }
  107. #if LLVM_HAS_RVALUE_REFERENCES
  108. IntrusiveRefCntPtr(IntrusiveRefCntPtr&& S) : Obj(S.Obj) {
  109. S.Obj = 0;
  110. }
  111. template <class X>
  112. IntrusiveRefCntPtr(IntrusiveRefCntPtr<X>&& S) : Obj(S.getPtr()) {
  113. S.Obj = 0;
  114. }
  115. #endif
  116. template <class X>
  117. IntrusiveRefCntPtr(const IntrusiveRefCntPtr<X>& S)
  118. : Obj(S.getPtr()) {
  119. retain();
  120. }
  121. IntrusiveRefCntPtr& operator=(IntrusiveRefCntPtr S) {
  122. swap(S);
  123. return *this;
  124. }
  125. ~IntrusiveRefCntPtr() { release(); }
  126. T& operator*() const { return *Obj; }
  127. T* operator->() const { return Obj; }
  128. T* getPtr() const { return Obj; }
  129. typedef T* (IntrusiveRefCntPtr::*unspecified_bool_type) () const;
  130. operator unspecified_bool_type() const {
  131. return Obj == 0 ? 0 : &IntrusiveRefCntPtr::getPtr;
  132. }
  133. void swap(IntrusiveRefCntPtr& other) {
  134. T* tmp = other.Obj;
  135. other.Obj = Obj;
  136. Obj = tmp;
  137. }
  138. void reset() {
  139. release();
  140. Obj = 0;
  141. }
  142. void resetWithoutRelease() {
  143. Obj = 0;
  144. }
  145. private:
  146. void retain() { if (Obj) IntrusiveRefCntPtrInfo<T>::retain(Obj); }
  147. void release() { if (Obj) IntrusiveRefCntPtrInfo<T>::release(Obj); }
  148. };
  149. template<class T, class U>
  150. inline bool operator==(const IntrusiveRefCntPtr<T>& A,
  151. const IntrusiveRefCntPtr<U>& B)
  152. {
  153. return A.getPtr() == B.getPtr();
  154. }
  155. template<class T, class U>
  156. inline bool operator!=(const IntrusiveRefCntPtr<T>& A,
  157. const IntrusiveRefCntPtr<U>& B)
  158. {
  159. return A.getPtr() != B.getPtr();
  160. }
  161. template<class T, class U>
  162. inline bool operator==(const IntrusiveRefCntPtr<T>& A,
  163. U* B)
  164. {
  165. return A.getPtr() == B;
  166. }
  167. template<class T, class U>
  168. inline bool operator!=(const IntrusiveRefCntPtr<T>& A,
  169. U* B)
  170. {
  171. return A.getPtr() != B;
  172. }
  173. template<class T, class U>
  174. inline bool operator==(T* A,
  175. const IntrusiveRefCntPtr<U>& B)
  176. {
  177. return A == B.getPtr();
  178. }
  179. template<class T, class U>
  180. inline bool operator!=(T* A,
  181. const IntrusiveRefCntPtr<U>& B)
  182. {
  183. return A != B.getPtr();
  184. }
  185. //===----------------------------------------------------------------------===//
  186. // LLVM-style downcasting support for IntrusiveRefCntPtr objects
  187. //===----------------------------------------------------------------------===//
  188. template<class T> struct simplify_type<IntrusiveRefCntPtr<T> > {
  189. typedef T* SimpleType;
  190. static SimpleType getSimplifiedValue(IntrusiveRefCntPtr<T>& Val) {
  191. return Val.getPtr();
  192. }
  193. };
  194. template<class T> struct simplify_type<const IntrusiveRefCntPtr<T> > {
  195. typedef /*const*/ T* SimpleType;
  196. static SimpleType getSimplifiedValue(const IntrusiveRefCntPtr<T>& Val) {
  197. return Val.getPtr();
  198. }
  199. };
  200. } // end namespace llvm
  201. #endif // LLVM_ADT_INTRUSIVEREFCNTPTR_H