[Alignment][NFC] Use Align with TargetLowering::setMinFunctionAlignment
[llvm-core.git] / include / llvm / ADT / IntrusiveRefCntPtr.h
blob6d97fe15db8b9ac502393eecd5ef7822a12f986e
1 //==- llvm/ADT/IntrusiveRefCntPtr.h - Smart Refcounting Pointer --*- C++ -*-==//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
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.
18 // For example:
20 // class MyClass : public RefCountedBase<MyClass> {};
22 // void foo() {
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.
36 // Ptr2.reset();
38 // // The object deletes itself when we return from the function, because
39 // // Ptr3's destructor decrements its refcount to 0.
40 // }
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
58 #include <atomic>
59 #include <cassert>
60 #include <cstddef>
62 namespace llvm {
64 /// A CRTP mixin class that adds reference counting to a type.
65 ///
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;
73 public:
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.");
81 if (--RefCount == 0)
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;
90 protected:
91 ThreadSafeRefCountedBase() : RefCount(0) {}
93 public:
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.");
99 if (NewRefCount == 0)
100 delete static_cast<const Derived *>(this);
104 /// Class you can specialize to provide custom retain/release functionality for
105 /// a type.
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 {
136 T *Obj = nullptr;
138 public:
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; }
146 template <class X>
147 IntrusiveRefCntPtr(IntrusiveRefCntPtr<X> &&S) : Obj(S.get()) {
148 S.Obj = nullptr;
151 template <class X>
152 IntrusiveRefCntPtr(const IntrusiveRefCntPtr<X> &S) : Obj(S.get()) {
153 retain();
156 ~IntrusiveRefCntPtr() { release(); }
158 IntrusiveRefCntPtr &operator=(IntrusiveRefCntPtr S) {
159 swap(S);
160 return *this;
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) {
169 T *tmp = other.Obj;
170 other.Obj = Obj;
171 Obj = tmp;
174 void reset() {
175 release();
176 Obj = nullptr;
179 void resetWithoutRelease() { Obj = nullptr; }
181 private:
182 void retain() {
183 if (Obj)
184 IntrusiveRefCntPtrInfo<T>::retain(Obj);
187 void release() {
188 if (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) {
209 return A.get() == B;
212 template <class T, class U>
213 inline bool operator!=(const IntrusiveRefCntPtr<T> &A, U *B) {
214 return A.get() != B;
217 template <class T, class U>
218 inline bool operator==(T *A, const IntrusiveRefCntPtr<U> &B) {
219 return A == B.get();
222 template <class T, class U>
223 inline bool operator!=(T *A, const IntrusiveRefCntPtr<U> &B) {
224 return A != B.get();
227 template <class T>
228 bool operator==(std::nullptr_t A, const IntrusiveRefCntPtr<T> &B) {
229 return !B;
232 template <class T>
233 bool operator==(const IntrusiveRefCntPtr<T> &A, std::nullptr_t B) {
234 return B == A;
237 template <class T>
238 bool operator!=(std::nullptr_t A, const IntrusiveRefCntPtr<T> &B) {
239 return !(A == B);
242 template <class T>
243 bool operator!=(const IntrusiveRefCntPtr<T> &A, std::nullptr_t B) {
244 return !(A == B);
247 // Make IntrusiveRefCntPtr work with dyn_cast, isa, and the other idioms from
248 // Casting.h.
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) {
255 return Val.get();
259 template <class T> struct simplify_type<const IntrusiveRefCntPtr<T>> {
260 using SimpleType = /*const*/ T *;
262 static SimpleType getSimplifiedValue(const IntrusiveRefCntPtr<T> &Val) {
263 return Val.get();
267 } // end namespace llvm
269 #endif // LLVM_ADT_INTRUSIVEREFCNTPTR_H