22
22
#define LLVM_ADT_INTRUSIVE_REF_CNT_PTR
23
23
24
24
#include < cassert>
25
- #include < cstddef>
26
25
27
26
#include " llvm/Support/Casting.h"
28
27
29
- // Forward declarations
30
-
31
28
namespace llvm {
32
- template <class T >
33
- class RefCountedBase ;
34
29
35
30
template <class T >
36
- class RefCountedBaseVPTR ;
37
- }
38
-
39
- template <class T >
40
- void IntrusivePtrAddRef (llvm::RefCountedBase<T>*);
41
-
42
- template <class T >
43
- void IntrusivePtrRelease (llvm::RefCountedBase<T>*);
44
-
45
- template <class T >
46
- void IntrusivePtrAddRef (llvm::RefCountedBaseVPTR<T>*);
47
-
48
- template <class T >
49
- void IntrusivePtrRelease (llvm::RefCountedBaseVPTR<T>*);
50
-
51
-
52
- namespace llvm {
31
+ class IntrusiveRefCntPtr ;
53
32
54
33
// ===----------------------------------------------------------------------===//
55
34
// / RefCountedBase - A generic base class for objects that wish to
@@ -74,16 +53,16 @@ namespace llvm {
74
53
if (--ref_cnt == 0 ) delete static_cast <Derived*>(this );
75
54
}
76
55
77
- friend void IntrusivePtrAddRef<Derived>(RefCountedBase<Derived>*);
78
- friend void IntrusivePtrRelease<Derived>(RefCountedBase<Derived>*);
56
+ friend class IntrusiveRefCntPtr <Derived>;
79
57
};
80
58
81
59
// ===----------------------------------------------------------------------===//
82
60
// / RefCountedBaseVPTR - A class that has the same function as
83
61
// / RefCountedBase, but with a virtual destructor. Should be used
84
- // / instead of RefCountedBase for classes that have virtual
85
- // / destructors. Classes that inherit from RefCountedBaseVPTR can't
86
- // / be allocated on stack.
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.
87
66
// ===----------------------------------------------------------------------===//
88
67
template <class Derived >
89
68
class RefCountedBaseVPTR {
@@ -99,33 +78,9 @@ namespace llvm {
99
78
if (--ref_cnt == 0 ) delete this ;
100
79
}
101
80
102
- friend void IntrusivePtrAddRef<Derived>(RefCountedBaseVPTR<Derived>*);
103
- friend void IntrusivePtrRelease<Derived>(RefCountedBaseVPTR<Derived>*);
81
+ friend class IntrusiveRefCntPtr <Derived>;
104
82
};
105
83
106
- }
107
-
108
- // ===----------------------------------------------------------------------===//
109
- // / IntrusivePtrAddRef - A utility function used by IntrusiveRefCntPtr
110
- // / to increment the reference count of an RefCountedBase-derived object.
111
- // ===----------------------------------------------------------------------===//
112
- template <class T >
113
- void IntrusivePtrAddRef (llvm::RefCountedBase<T>* O) {
114
- O->Retain ();
115
- }
116
-
117
- // ===----------------------------------------------------------------------===//
118
- // / IntrusivePtrRelease - The complement of IntrusivePtrAddRef;
119
- // / decrements the reference count of a RefCounted object.
120
- // ===----------------------------------------------------------------------===//
121
- template <class T >
122
- void IntrusivePtrRelease (llvm::RefCountedBase<T>* O) {
123
- O->Release ();
124
- }
125
-
126
-
127
- namespace llvm {
128
-
129
84
// ===----------------------------------------------------------------------===//
130
85
// / IntrusiveRefCntPtr - A template class that implements a "smart pointer"
131
86
// / that assumes the wrapped object has a reference count associated
@@ -136,6 +91,12 @@ namespace llvm {
136
91
// / incremented and upon destruction of the smart pointer the
137
92
// / reference count is decremented. This class also safely handles
138
93
// / wrapping NULL pointers.
94
+ // /
95
+ // / Reference counting is implemented via calls to
96
+ // / Obj->Retain()/Obj->Release(). Release() is required to destroy
97
+ // / the object when the reference count reaches zero. Inheriting from
98
+ // / RefCountedBase/RefCountedBaseVPTR takes care of this
99
+ // / automatically.
139
100
// ===----------------------------------------------------------------------===//
140
101
template <typename T>
141
102
class IntrusiveRefCntPtr {
@@ -144,7 +105,7 @@ namespace llvm {
144
105
public:
145
106
typedef T element_type;
146
107
147
- explicit IntrusiveRefCntPtr () : Obj(NULL ) {}
108
+ explicit IntrusiveRefCntPtr () : Obj(0 ) {}
148
109
149
110
explicit IntrusiveRefCntPtr (T* obj) : Obj(obj) {
150
111
retain ();
@@ -181,7 +142,7 @@ namespace llvm {
181
142
182
143
typedef T * IntrusiveRefCntPtr::*unspecified_bool_type;
183
144
operator unspecified_bool_type () const {
184
- return Obj == NULL ? NULL : &IntrusiveRefCntPtr::getPtr;
145
+ return Obj == 0 ? 0 : &IntrusiveRefCntPtr::getPtr;
185
146
}
186
147
187
148
void swap (IntrusiveRefCntPtr& other) {
@@ -191,8 +152,8 @@ namespace llvm {
191
152
}
192
153
193
154
private:
194
- void retain () { if (Obj) IntrusivePtrAddRef ( Obj); }
195
- void release () { if (Obj) IntrusivePtrRelease ( Obj); }
155
+ void retain () { if (Obj) Obj-> Retain ( ); }
156
+ void release () { if (Obj) Obj-> Release ( ); }
196
157
197
158
void replace (T* S) {
198
159
this_type (S).swap (this );
0 commit comments