Skip to content

Commit 9eeaae5

Browse files
committed
Update per review. Patch by Mikhail Glushenkov!
llvm-svn: 47628
1 parent 01a72ff commit 9eeaae5

File tree

1 file changed

+17
-56
lines changed

1 file changed

+17
-56
lines changed

llvm/include/llvm/ADT/IntrusiveRefCntPtr.h

Lines changed: 17 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,13 @@
2222
#define LLVM_ADT_INTRUSIVE_REF_CNT_PTR
2323

2424
#include <cassert>
25-
#include <cstddef>
2625

2726
#include "llvm/Support/Casting.h"
2827

29-
// Forward declarations
30-
3128
namespace llvm {
32-
template <class T>
33-
class RefCountedBase;
3429

3530
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;
5332

5433
//===----------------------------------------------------------------------===//
5534
/// RefCountedBase - A generic base class for objects that wish to
@@ -74,16 +53,16 @@ namespace llvm {
7453
if (--ref_cnt == 0) delete static_cast<Derived*>(this);
7554
}
7655

77-
friend void IntrusivePtrAddRef<Derived>(RefCountedBase<Derived>*);
78-
friend void IntrusivePtrRelease<Derived>(RefCountedBase<Derived>*);
56+
friend class IntrusiveRefCntPtr<Derived>;
7957
};
8058

8159
//===----------------------------------------------------------------------===//
8260
/// RefCountedBaseVPTR - A class that has the same function as
8361
/// 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.
8766
//===----------------------------------------------------------------------===//
8867
template <class Derived>
8968
class RefCountedBaseVPTR {
@@ -99,33 +78,9 @@ namespace llvm {
9978
if (--ref_cnt == 0) delete this;
10079
}
10180

102-
friend void IntrusivePtrAddRef<Derived>(RefCountedBaseVPTR<Derived>*);
103-
friend void IntrusivePtrRelease<Derived>(RefCountedBaseVPTR<Derived>*);
81+
friend class IntrusiveRefCntPtr<Derived>;
10482
};
10583

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-
12984
//===----------------------------------------------------------------------===//
13085
/// IntrusiveRefCntPtr - A template class that implements a "smart pointer"
13186
/// that assumes the wrapped object has a reference count associated
@@ -136,6 +91,12 @@ namespace llvm {
13691
/// incremented and upon destruction of the smart pointer the
13792
/// reference count is decremented. This class also safely handles
13893
/// 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.
139100
//===----------------------------------------------------------------------===//
140101
template <typename T>
141102
class IntrusiveRefCntPtr {
@@ -144,7 +105,7 @@ namespace llvm {
144105
public:
145106
typedef T element_type;
146107

147-
explicit IntrusiveRefCntPtr() : Obj(NULL) {}
108+
explicit IntrusiveRefCntPtr() : Obj(0) {}
148109

149110
explicit IntrusiveRefCntPtr(T* obj) : Obj(obj) {
150111
retain();
@@ -181,7 +142,7 @@ namespace llvm {
181142

182143
typedef T * IntrusiveRefCntPtr::*unspecified_bool_type;
183144
operator unspecified_bool_type() const {
184-
return Obj == NULL ? NULL : &IntrusiveRefCntPtr::getPtr;
145+
return Obj == 0 ? 0 : &IntrusiveRefCntPtr::getPtr;
185146
}
186147

187148
void swap(IntrusiveRefCntPtr& other) {
@@ -191,8 +152,8 @@ namespace llvm {
191152
}
192153

193154
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(); }
196157

197158
void replace(T* S) {
198159
this_type(S).swap(this);

0 commit comments

Comments
 (0)