aa7ccdcf9c7fb35e74e18067c9f9990b9d474bbe
[oota-llvm.git] / include / llvm / ADT / IntrusiveRefCntPtr.h
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
21 #ifndef LLVM_ADT_INTRUSIVEREFCNTPTR_H
22 #define LLVM_ADT_INTRUSIVEREFCNTPTR_H
23
24 #include "llvm/Support/Casting.h"
25 #include "llvm/Support/Compiler.h"
26 #include <memory>
27 #include <atomic>
28
29 namespace llvm {
30
31   template <class T>
32   class IntrusiveRefCntPtr;
33
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;
47
48   public:
49     RefCountedBase() : ref_cnt(0) {}
50     RefCountedBase(const RefCountedBase &) : ref_cnt(0) {}
51
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);
56     }
57   };
58
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();
70
71   protected:
72     RefCountedBaseVPTR() : ref_cnt(0) {}
73     RefCountedBaseVPTR(const RefCountedBaseVPTR &) : ref_cnt(0) {}
74
75     virtual ~RefCountedBaseVPTR() {}
76
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;
81     }
82
83     template <typename T>
84     friend struct IntrusiveRefCntPtrInfo;
85   };
86
87   
88   template <typename T> struct IntrusiveRefCntPtrInfo {
89     static void retain(T *obj) { obj->Retain(); }
90     static void release(T *obj) { obj->Release(); }
91   };
92
93 /// \brief A thread-safe version of \c llvm::RefCountedBase.
94 ///
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;
103
104 protected:
105   ThreadSafeRefCountedBase() : RefCount(0) {}
106
107 public:
108   void Retain() const { ++RefCount; }
109
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);
115   }
116 };
117   
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.
128 ///
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
133 ///  automatically.
134 //===----------------------------------------------------------------------===//
135   template <typename T>
136   class IntrusiveRefCntPtr {
137     T* Obj;
138     typedef IntrusiveRefCntPtr this_type;
139   public:
140     typedef T element_type;
141
142     explicit IntrusiveRefCntPtr() : Obj(0) {}
143
144     IntrusiveRefCntPtr(T* obj) : Obj(obj) {
145       retain();
146     }
147
148     IntrusiveRefCntPtr(const IntrusiveRefCntPtr& S) : Obj(S.Obj) {
149       retain();
150     }
151
152     IntrusiveRefCntPtr(IntrusiveRefCntPtr&& S) : Obj(S.Obj) {
153       S.Obj = 0;
154     }
155
156     template <class X>
157     IntrusiveRefCntPtr(IntrusiveRefCntPtr<X>&& S) : Obj(S.getPtr()) {
158       S.Obj = 0;
159     }
160
161     template <class X>
162     IntrusiveRefCntPtr(const IntrusiveRefCntPtr<X>& S)
163       : Obj(S.getPtr()) {
164       retain();
165     }
166
167     IntrusiveRefCntPtr& operator=(IntrusiveRefCntPtr S) {
168       swap(S);
169       return *this;
170     }
171
172     ~IntrusiveRefCntPtr() { release(); }
173
174     T& operator*() const { return *Obj; }
175
176     T* operator->() const { return Obj; }
177
178     T* getPtr() const { return Obj; }
179
180     typedef T* (IntrusiveRefCntPtr::*unspecified_bool_type) () const;
181     operator unspecified_bool_type() const {
182       return Obj == 0 ? 0 : &IntrusiveRefCntPtr::getPtr;
183     }
184
185     void swap(IntrusiveRefCntPtr& other) {
186       T* tmp = other.Obj;
187       other.Obj = Obj;
188       Obj = tmp;
189     }
190
191     void reset() {
192       release();
193       Obj = 0;
194     }
195
196     void resetWithoutRelease() {
197       Obj = 0;
198     }
199
200   private:
201     void retain() { if (Obj) IntrusiveRefCntPtrInfo<T>::retain(Obj); }
202     void release() { if (Obj) IntrusiveRefCntPtrInfo<T>::release(Obj); }
203   };
204
205   template<class T, class U>
206   inline bool operator==(const IntrusiveRefCntPtr<T>& A,
207                          const IntrusiveRefCntPtr<U>& B)
208   {
209     return A.getPtr() == B.getPtr();
210   }
211
212   template<class T, class U>
213   inline bool operator!=(const IntrusiveRefCntPtr<T>& A,
214                          const IntrusiveRefCntPtr<U>& B)
215   {
216     return A.getPtr() != B.getPtr();
217   }
218
219   template<class T, class U>
220   inline bool operator==(const IntrusiveRefCntPtr<T>& A,
221                          U* B)
222   {
223     return A.getPtr() == B;
224   }
225
226   template<class T, class U>
227   inline bool operator!=(const IntrusiveRefCntPtr<T>& A,
228                          U* B)
229   {
230     return A.getPtr() != B;
231   }
232
233   template<class T, class U>
234   inline bool operator==(T* A,
235                          const IntrusiveRefCntPtr<U>& B)
236   {
237     return A == B.getPtr();
238   }
239
240   template<class T, class U>
241   inline bool operator!=(T* A,
242                          const IntrusiveRefCntPtr<U>& B)
243   {
244     return A != B.getPtr();
245   }
246
247 //===----------------------------------------------------------------------===//
248 // LLVM-style downcasting support for IntrusiveRefCntPtr objects
249 //===----------------------------------------------------------------------===//
250
251   template<class T> struct simplify_type<IntrusiveRefCntPtr<T> > {
252     typedef T* SimpleType;
253     static SimpleType getSimplifiedValue(IntrusiveRefCntPtr<T>& Val) {
254       return Val.getPtr();
255     }
256   };
257
258   template<class T> struct simplify_type<const IntrusiveRefCntPtr<T> > {
259     typedef /*const*/ T* SimpleType;
260     static SimpleType getSimplifiedValue(const IntrusiveRefCntPtr<T>& Val) {
261       return Val.getPtr();
262     }
263   };
264
265 } // end namespace llvm
266
267 #endif // LLVM_ADT_INTRUSIVEREFCNTPTR_H