1 //== llvm/ADT/IntrusiveRefCntPtr.h - Smart Refcounting Pointer ---*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
16 // IntrusiveRefCntPtr is similar to Boost's intrusive_ptr with added
17 // LLVM-style casting.
19 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_ADT_INTRUSIVEREFCNTPTR_H
22 #define LLVM_ADT_INTRUSIVEREFCNTPTR_H
24 #include "llvm/Support/Casting.h"
25 #include "llvm/Support/Compiler.h"
32 class IntrusiveRefCntPtr;
34 //===----------------------------------------------------------------------===//
35 /// RefCountedBase - A generic base class for objects that wish to
36 /// have their lifetimes managed using reference counts. Classes
37 /// subclass RefCountedBase to obtain such functionality, and are
38 /// typically handled with IntrusiveRefCntPtr "smart pointers" (see below)
39 /// which automatically handle the management of reference counts.
40 /// Objects that subclass RefCountedBase should not be allocated on
41 /// the stack, as invoking "delete" (which is called when the
42 /// reference count hits 0) on such objects is an error.
43 //===----------------------------------------------------------------------===//
44 template <class Derived>
45 class RefCountedBase {
46 mutable unsigned ref_cnt;
49 RefCountedBase() : ref_cnt(0) {}
50 RefCountedBase(const RefCountedBase &) : ref_cnt(0) {}
52 void Retain() const { ++ref_cnt; }
53 void Release() const {
54 assert (ref_cnt > 0 && "Reference count is already zero.");
55 if (--ref_cnt == 0) delete static_cast<const Derived*>(this);
59 //===----------------------------------------------------------------------===//
60 /// RefCountedBaseVPTR - A class that has the same function as
61 /// RefCountedBase, but with a virtual destructor. Should be used
62 /// instead of RefCountedBase for classes that already have virtual
63 /// methods to enforce dynamic allocation via 'new'. Classes that
64 /// inherit from RefCountedBaseVPTR can't be allocated on stack -
65 /// attempting to do this will produce a compile error.
66 //===----------------------------------------------------------------------===//
67 class RefCountedBaseVPTR {
68 mutable unsigned ref_cnt;
69 virtual void anchor();
72 RefCountedBaseVPTR() : ref_cnt(0) {}
73 RefCountedBaseVPTR(const RefCountedBaseVPTR &) : ref_cnt(0) {}
75 virtual ~RefCountedBaseVPTR() {}
77 void Retain() const { ++ref_cnt; }
78 void Release() const {
79 assert (ref_cnt > 0 && "Reference count is already zero.");
80 if (--ref_cnt == 0) delete this;
84 friend struct IntrusiveRefCntPtrInfo;
88 template <typename T> struct IntrusiveRefCntPtrInfo {
89 static void retain(T *obj) { obj->Retain(); }
90 static void release(T *obj) { obj->Release(); }
93 /// \brief A thread-safe version of \c llvm::RefCountedBase.
95 /// A generic base class for objects that wish to have their lifetimes managed
96 /// using reference counts. Classes subclass \c ThreadSafeRefCountedBase to
97 /// obtain such functionality, and are typically handled with
98 /// \c IntrusiveRefCntPtr "smart pointers" which automatically handle the
99 /// management of reference counts.
100 template <class Derived>
101 class ThreadSafeRefCountedBase {
102 mutable std::atomic<int> RefCount;
105 ThreadSafeRefCountedBase() : RefCount(0) {}
108 void Retain() const { ++RefCount; }
110 void Release() const {
111 int NewRefCount = --RefCount;
112 assert(NewRefCount >= 0 && "Reference count was already zero.");
113 if (NewRefCount == 0)
114 delete static_cast<const Derived*>(this);
118 //===----------------------------------------------------------------------===//
119 /// IntrusiveRefCntPtr - A template class that implements a "smart pointer"
120 /// that assumes the wrapped object has a reference count associated
121 /// with it that can be managed via calls to
122 /// IntrusivePtrAddRef/IntrusivePtrRelease. The smart pointers
123 /// manage reference counts via the RAII idiom: upon creation of
124 /// smart pointer the reference count of the wrapped object is
125 /// incremented and upon destruction of the smart pointer the
126 /// reference count is decremented. This class also safely handles
127 /// wrapping NULL pointers.
129 /// Reference counting is implemented via calls to
130 /// Obj->Retain()/Obj->Release(). Release() is required to destroy
131 /// the object when the reference count reaches zero. Inheriting from
132 /// RefCountedBase/RefCountedBaseVPTR takes care of this
134 //===----------------------------------------------------------------------===//
135 template <typename T>
136 class IntrusiveRefCntPtr {
138 typedef IntrusiveRefCntPtr this_type;
140 typedef T element_type;
142 explicit IntrusiveRefCntPtr() : Obj(0) {}
144 IntrusiveRefCntPtr(T* obj) : Obj(obj) {
148 IntrusiveRefCntPtr(const IntrusiveRefCntPtr& S) : Obj(S.Obj) {
152 IntrusiveRefCntPtr(IntrusiveRefCntPtr&& S) : Obj(S.Obj) {
157 IntrusiveRefCntPtr(IntrusiveRefCntPtr<X>&& S) : Obj(S.getPtr()) {
162 IntrusiveRefCntPtr(const IntrusiveRefCntPtr<X>& S)
167 IntrusiveRefCntPtr& operator=(IntrusiveRefCntPtr S) {
172 ~IntrusiveRefCntPtr() { release(); }
174 T& operator*() const { return *Obj; }
176 T* operator->() const { return Obj; }
178 T* getPtr() const { return Obj; }
180 typedef T* (IntrusiveRefCntPtr::*unspecified_bool_type) () const;
181 operator unspecified_bool_type() const {
182 return Obj == 0 ? 0 : &IntrusiveRefCntPtr::getPtr;
185 void swap(IntrusiveRefCntPtr& other) {
196 void resetWithoutRelease() {
201 void retain() { if (Obj) IntrusiveRefCntPtrInfo<T>::retain(Obj); }
202 void release() { if (Obj) IntrusiveRefCntPtrInfo<T>::release(Obj); }
205 template<class T, class U>
206 inline bool operator==(const IntrusiveRefCntPtr<T>& A,
207 const IntrusiveRefCntPtr<U>& B)
209 return A.getPtr() == B.getPtr();
212 template<class T, class U>
213 inline bool operator!=(const IntrusiveRefCntPtr<T>& A,
214 const IntrusiveRefCntPtr<U>& B)
216 return A.getPtr() != B.getPtr();
219 template<class T, class U>
220 inline bool operator==(const IntrusiveRefCntPtr<T>& A,
223 return A.getPtr() == B;
226 template<class T, class U>
227 inline bool operator!=(const IntrusiveRefCntPtr<T>& A,
230 return A.getPtr() != B;
233 template<class T, class U>
234 inline bool operator==(T* A,
235 const IntrusiveRefCntPtr<U>& B)
237 return A == B.getPtr();
240 template<class T, class U>
241 inline bool operator!=(T* A,
242 const IntrusiveRefCntPtr<U>& B)
244 return A != B.getPtr();
247 //===----------------------------------------------------------------------===//
248 // LLVM-style downcasting support for IntrusiveRefCntPtr objects
249 //===----------------------------------------------------------------------===//
251 template<class T> struct simplify_type<IntrusiveRefCntPtr<T> > {
252 typedef T* SimpleType;
253 static SimpleType getSimplifiedValue(IntrusiveRefCntPtr<T>& Val) {
258 template<class T> struct simplify_type<const IntrusiveRefCntPtr<T> > {
259 typedef /*const*/ T* SimpleType;
260 static SimpleType getSimplifiedValue(const IntrusiveRefCntPtr<T>& Val) {
265 } // end namespace llvm
267 #endif // LLVM_ADT_INTRUSIVEREFCNTPTR_H