1 //==- llvm/ADT/IntrusiveRefCntPtr.h - Smart Refcounting Pointer --*- C++ -*-==//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines the RefCountedBase, ThreadSafeRefCountedBase, and
10 // IntrusiveRefCntPtr classes.
12 // IntrusiveRefCntPtr is a smart pointer to an object which maintains a
13 // reference count. (ThreadSafe)RefCountedBase is a mixin class that adds a
14 // refcount member variable and methods for updating the refcount. An object
15 // that inherits from (ThreadSafe)RefCountedBase deletes itself when its
16 // refcount hits zero.
20 // class MyClass : public RefCountedBase<MyClass> {};
23 // // Constructing an IntrusiveRefCntPtr increases the pointee's refcount by
24 // // 1 (from 0 in this case).
25 // IntrusiveRefCntPtr<MyClass> Ptr1(new MyClass());
27 // // Copying an IntrusiveRefCntPtr increases the pointee's refcount by 1.
28 // IntrusiveRefCntPtr<MyClass> Ptr2(Ptr1);
30 // // Constructing an IntrusiveRefCntPtr has no effect on the object's
31 // // refcount. After a move, the moved-from pointer is null.
32 // IntrusiveRefCntPtr<MyClass> Ptr3(std::move(Ptr1));
33 // assert(Ptr1 == nullptr);
35 // // Clearing an IntrusiveRefCntPtr decreases the pointee's refcount by 1.
38 // // The object deletes itself when we return from the function, because
39 // // Ptr3's destructor decrements its refcount to 0.
42 // You can use IntrusiveRefCntPtr with isa<T>(), dyn_cast<T>(), etc.:
44 // IntrusiveRefCntPtr<MyClass> Ptr(new MyClass());
45 // OtherClass *Other = dyn_cast<OtherClass>(Ptr); // Ptr.get() not required
47 // IntrusiveRefCntPtr works with any class that
49 // - inherits from (ThreadSafe)RefCountedBase,
50 // - has Retain() and Release() methods, or
51 // - specializes IntrusiveRefCntPtrInfo.
53 //===----------------------------------------------------------------------===//
55 #ifndef LLVM_ADT_INTRUSIVEREFCNTPTR_H
56 #define LLVM_ADT_INTRUSIVEREFCNTPTR_H
64 /// A CRTP mixin class that adds reference counting to a type.
66 /// The lifetime of an object which inherits from RefCountedBase is managed by
67 /// calls to Release() and Retain(), which increment and decrement the object's
68 /// refcount, respectively. When a Release() call decrements the refcount to 0,
69 /// the object deletes itself.
70 template <class Derived
> class RefCountedBase
{
71 mutable unsigned RefCount
= 0;
74 RefCountedBase() = default;
75 RefCountedBase(const RefCountedBase
&) {}
77 void Retain() const { ++RefCount
; }
79 void Release() const {
80 assert(RefCount
> 0 && "Reference count is already zero.");
82 delete static_cast<const Derived
*>(this);
86 /// A thread-safe version of \c RefCountedBase.
87 template <class Derived
> class ThreadSafeRefCountedBase
{
88 mutable std::atomic
<int> RefCount
;
91 ThreadSafeRefCountedBase() : RefCount(0) {}
94 void Retain() const { RefCount
.fetch_add(1, std::memory_order_relaxed
); }
96 void Release() const {
97 int NewRefCount
= RefCount
.fetch_sub(1, std::memory_order_acq_rel
) - 1;
98 assert(NewRefCount
>= 0 && "Reference count was already zero.");
100 delete static_cast<const Derived
*>(this);
104 /// Class you can specialize to provide custom retain/release functionality for
107 /// Usually specializing this class is not necessary, as IntrusiveRefCntPtr
108 /// works with any type which defines Retain() and Release() functions -- you
109 /// can define those functions yourself if RefCountedBase doesn't work for you.
111 /// One case when you might want to specialize this type is if you have
112 /// - Foo.h defines type Foo and includes Bar.h, and
113 /// - Bar.h uses IntrusiveRefCntPtr<Foo> in inline functions.
115 /// Because Foo.h includes Bar.h, Bar.h can't include Foo.h in order to pull in
116 /// the declaration of Foo. Without the declaration of Foo, normally Bar.h
117 /// wouldn't be able to use IntrusiveRefCntPtr<Foo>, which wants to call
118 /// T::Retain and T::Release.
120 /// To resolve this, Bar.h could include a third header, FooFwd.h, which
121 /// forward-declares Foo and specializes IntrusiveRefCntPtrInfo<Foo>. Then
122 /// Bar.h could use IntrusiveRefCntPtr<Foo>, although it still couldn't call any
123 /// functions on Foo itself, because Foo would be an incomplete type.
124 template <typename T
> struct IntrusiveRefCntPtrInfo
{
125 static void retain(T
*obj
) { obj
->Retain(); }
126 static void release(T
*obj
) { obj
->Release(); }
129 /// A smart pointer to a reference-counted object that inherits from
130 /// RefCountedBase or ThreadSafeRefCountedBase.
132 /// This class increments its pointee's reference count when it is created, and
133 /// decrements its refcount when it's destroyed (or is changed to point to a
134 /// different object).
135 template <typename T
> class IntrusiveRefCntPtr
{
139 using element_type
= T
;
141 explicit IntrusiveRefCntPtr() = default;
142 IntrusiveRefCntPtr(T
*obj
) : Obj(obj
) { retain(); }
143 IntrusiveRefCntPtr(const IntrusiveRefCntPtr
&S
) : Obj(S
.Obj
) { retain(); }
144 IntrusiveRefCntPtr(IntrusiveRefCntPtr
&&S
) : Obj(S
.Obj
) { S
.Obj
= nullptr; }
147 IntrusiveRefCntPtr(IntrusiveRefCntPtr
<X
> &&S
) : Obj(S
.get()) {
152 IntrusiveRefCntPtr(const IntrusiveRefCntPtr
<X
> &S
) : Obj(S
.get()) {
156 ~IntrusiveRefCntPtr() { release(); }
158 IntrusiveRefCntPtr
&operator=(IntrusiveRefCntPtr S
) {
163 T
&operator*() const { return *Obj
; }
164 T
*operator->() const { return Obj
; }
165 T
*get() const { return Obj
; }
166 explicit operator bool() const { return Obj
; }
168 void swap(IntrusiveRefCntPtr
&other
) {
179 void resetWithoutRelease() { Obj
= nullptr; }
184 IntrusiveRefCntPtrInfo
<T
>::retain(Obj
);
189 IntrusiveRefCntPtrInfo
<T
>::release(Obj
);
192 template <typename X
> friend class IntrusiveRefCntPtr
;
195 template <class T
, class U
>
196 inline bool operator==(const IntrusiveRefCntPtr
<T
> &A
,
197 const IntrusiveRefCntPtr
<U
> &B
) {
198 return A
.get() == B
.get();
201 template <class T
, class U
>
202 inline bool operator!=(const IntrusiveRefCntPtr
<T
> &A
,
203 const IntrusiveRefCntPtr
<U
> &B
) {
204 return A
.get() != B
.get();
207 template <class T
, class U
>
208 inline bool operator==(const IntrusiveRefCntPtr
<T
> &A
, U
*B
) {
212 template <class T
, class U
>
213 inline bool operator!=(const IntrusiveRefCntPtr
<T
> &A
, U
*B
) {
217 template <class T
, class U
>
218 inline bool operator==(T
*A
, const IntrusiveRefCntPtr
<U
> &B
) {
222 template <class T
, class U
>
223 inline bool operator!=(T
*A
, const IntrusiveRefCntPtr
<U
> &B
) {
228 bool operator==(std::nullptr_t A
, const IntrusiveRefCntPtr
<T
> &B
) {
233 bool operator==(const IntrusiveRefCntPtr
<T
> &A
, std::nullptr_t B
) {
238 bool operator!=(std::nullptr_t A
, const IntrusiveRefCntPtr
<T
> &B
) {
243 bool operator!=(const IntrusiveRefCntPtr
<T
> &A
, std::nullptr_t B
) {
247 // Make IntrusiveRefCntPtr work with dyn_cast, isa, and the other idioms from
249 template <typename From
> struct simplify_type
;
251 template <class T
> struct simplify_type
<IntrusiveRefCntPtr
<T
>> {
252 using SimpleType
= T
*;
254 static SimpleType
getSimplifiedValue(IntrusiveRefCntPtr
<T
> &Val
) {
259 template <class T
> struct simplify_type
<const IntrusiveRefCntPtr
<T
>> {
260 using SimpleType
= /*const*/ T
*;
262 static SimpleType
getSimplifiedValue(const IntrusiveRefCntPtr
<T
> &Val
) {
267 } // end namespace llvm
269 #endif // LLVM_ADT_INTRUSIVEREFCNTPTR_H