LCOV - code coverage report
Current view: top level - include/llvm/ADT - IntrusiveRefCntPtr.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 148 312 47.4 %
Date: 2018-10-16 05:50:02 Functions: 13 219 5.9 %
Legend: Lines: hit not hit

          Line data    Source code
       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 the RefCountedBase, ThreadSafeRefCountedBase, and
      11             : // IntrusiveRefCntPtr classes.
      12             : //
      13             : // IntrusiveRefCntPtr is a smart pointer to an object which maintains a
      14             : // reference count.  (ThreadSafe)RefCountedBase is a mixin class that adds a
      15             : // refcount member variable and methods for updating the refcount.  An object
      16             : // that inherits from (ThreadSafe)RefCountedBase deletes itself when its
      17             : // refcount hits zero.
      18             : //
      19             : // For example:
      20             : //
      21             : //   class MyClass : public RefCountedBase<MyClass> {};
      22             : //
      23             : //   void foo() {
      24             : //     // Constructing an IntrusiveRefCntPtr increases the pointee's refcount by
      25             : //     // 1 (from 0 in this case).
      26             : //     IntrusiveRefCntPtr<MyClass> Ptr1(new MyClass());
      27             : //
      28             : //     // Copying an IntrusiveRefCntPtr increases the pointee's refcount by 1.
      29             : //     IntrusiveRefCntPtr<MyClass> Ptr2(Ptr1);
      30             : //
      31             : //     // Constructing an IntrusiveRefCntPtr has no effect on the object's
      32             : //     // refcount.  After a move, the moved-from pointer is null.
      33             : //     IntrusiveRefCntPtr<MyClass> Ptr3(std::move(Ptr1));
      34             : //     assert(Ptr1 == nullptr);
      35             : //
      36             : //     // Clearing an IntrusiveRefCntPtr decreases the pointee's refcount by 1.
      37             : //     Ptr2.reset();
      38             : //
      39             : //     // The object deletes itself when we return from the function, because
      40             : //     // Ptr3's destructor decrements its refcount to 0.
      41             : //   }
      42             : //
      43             : // You can use IntrusiveRefCntPtr with isa<T>(), dyn_cast<T>(), etc.:
      44             : //
      45             : //   IntrusiveRefCntPtr<MyClass> Ptr(new MyClass());
      46             : //   OtherClass *Other = dyn_cast<OtherClass>(Ptr);  // Ptr.get() not required
      47             : //
      48             : // IntrusiveRefCntPtr works with any class that
      49             : //
      50             : //  - inherits from (ThreadSafe)RefCountedBase,
      51             : //  - has Retain() and Release() methods, or
      52             : //  - specializes IntrusiveRefCntPtrInfo.
      53             : //
      54             : //===----------------------------------------------------------------------===//
      55             : 
      56             : #ifndef LLVM_ADT_INTRUSIVEREFCNTPTR_H
      57             : #define LLVM_ADT_INTRUSIVEREFCNTPTR_H
      58             : 
      59             : #include <atomic>
      60             : #include <cassert>
      61             : #include <cstddef>
      62             : 
      63             : namespace llvm {
      64             : 
      65             : /// A CRTP mixin class that adds reference counting to a type.
      66             : ///
      67             : /// The lifetime of an object which inherits from RefCountedBase is managed by
      68             : /// calls to Release() and Retain(), which increment and decrement the object's
      69             : /// refcount, respectively.  When a Release() call decrements the refcount to 0,
      70             : /// the object deletes itself.
      71             : template <class Derived> class RefCountedBase {
      72             :   mutable unsigned RefCount = 0;
      73             : 
      74             : public:
      75      692008 :   RefCountedBase() = default;
      76             :   RefCountedBase(const RefCountedBase &) {}
      77             : 
      78      568339 :   void Retain() const { ++RefCount; }
      79             : 
      80      956116 :   void Release() const {
      81             :     assert(RefCount > 0 && "Reference count is already zero.");
      82     1004815 :     if (--RefCount == 0)
      83      509310 :       delete static_cast<const Derived *>(this);
      84      956116 :   }
      85      234269 : };
      86             : 
      87      234269 : /// A thread-safe version of \c RefCountedBase.
      88      145216 : template <class Derived> class ThreadSafeRefCountedBase {
      89      234269 :   mutable std::atomic<int> RefCount;
      90      276550 : 
      91             : protected:
      92      276550 :   ThreadSafeRefCountedBase() : RefCount(0) {}
      93      141601 : 
      94      276550 : public:
      95       46408 :   void Retain() const { RefCount.fetch_add(1, std::memory_order_relaxed); }
      96             : 
      97     3727826 :   void Release() const {
      98       41678 :     int NewRefCount = RefCount.fetch_sub(1, std::memory_order_acq_rel) - 1;
      99       46408 :     assert(NewRefCount >= 0 && "Reference count was already zero.");
     100     3721422 :     if (NewRefCount == 0)
     101      743266 :       delete static_cast<const Derived *>(this);
     102     3721422 :   }
     103      886655 : };
     104       40004 : 
     105       38205 : /// Class you can specialize to provide custom retain/release functionality for
     106      857497 : /// a type.
     107      393590 : ///
     108      885096 : /// Usually specializing this class is not necessary, as IntrusiveRefCntPtr
     109       48124 : /// works with any type which defines Retain() and Release() functions -- you
     110      227215 : /// can define those functions yourself if RefCountedBase doesn't work for you.
     111       32550 : ///
     112      237339 : /// One case when you might want to specialize this type is if you have
     113       96803 : ///  - Foo.h defines type Foo and includes Bar.h, and
     114       43572 : ///  - Bar.h uses IntrusiveRefCntPtr<Foo> in inline functions.
     115       31047 : ///
     116       66810 : /// Because Foo.h includes Bar.h, Bar.h can't include Foo.h in order to pull in
     117       44371 : /// the declaration of Foo.  Without the declaration of Foo, normally Bar.h
     118       93075 : /// wouldn't be able to use IntrusiveRefCntPtr<Foo>, which wants to call
     119       33111 : /// T::Retain and T::Release.
     120       32051 : ///
     121           0 : /// To resolve this, Bar.h could include a third header, FooFwd.h, which
     122       44939 : /// forward-declares Foo and specializes IntrusiveRefCntPtrInfo<Foo>.  Then
     123       31532 : /// Bar.h could use IntrusiveRefCntPtr<Foo>, although it still couldn't call any
     124       34320 : /// functions on Foo itself, because Foo would be an incomplete type.
     125       10619 : template <typename T> struct IntrusiveRefCntPtrInfo {
     126        3528 :   static void retain(T *obj) { obj->Retain(); }
     127     3824550 :   static void release(T *obj) { obj->Release(); }
     128           0 : };
     129           0 : 
     130       13946 : /// A smart pointer to a reference-counted object that inherits from
     131           0 : /// RefCountedBase or ThreadSafeRefCountedBase.
     132       13946 : ///
     133             : /// This class increments its pointee's reference count when it is created, and
     134             : /// decrements its refcount when it's destroyed (or is changed to point to a
     135             : /// different object).
     136             : template <typename T> class IntrusiveRefCntPtr {
     137      510428 :   T *Obj = nullptr;
     138             : 
     139      179885 : public:
     140       32981 :   using element_type = T;
     141          94 : 
     142      116683 :   explicit IntrusiveRefCntPtr() = default;
     143     1886400 :   IntrusiveRefCntPtr(T *obj) : Obj(obj) { retain(); }
     144    22728728 :   IntrusiveRefCntPtr(const IntrusiveRefCntPtr &S) : Obj(S.Obj) { retain(); }
     145     4726499 :   IntrusiveRefCntPtr(IntrusiveRefCntPtr &&S) : Obj(S.Obj) { S.Obj = nullptr; }
     146             : 
     147         374 :   template <class X>
     148          87 :   IntrusiveRefCntPtr(IntrusiveRefCntPtr<X> &&S) : Obj(S.get()) {
     149       14051 :     S.Obj = nullptr;
     150             :   }
     151             : 
     152       24868 :   template <class X>
     153      205991 :   IntrusiveRefCntPtr(const IntrusiveRefCntPtr<X> &S) : Obj(S.get()) {
     154       51690 :     retain();
     155      174046 :   }
     156       32793 : 
     157    17967276 :   ~IntrusiveRefCntPtr() { release(); }
     158        3947 : 
     159        4307 :   IntrusiveRefCntPtr &operator=(IntrusiveRefCntPtr S) {
     160           1 :     swap(S);
     161             :     return *this;
     162             :   }
     163       45486 : 
     164         405 :   T &operator*() const { return *Obj; }
     165        1954 :   T *operator->() const { return Obj; }
     166         637 :   T *get() const { return Obj; }
     167      825451 :   explicit operator bool() const { return Obj; }
     168       18197 : 
     169       52220 :   void swap(IntrusiveRefCntPtr &other) {
     170     1825852 :     T *tmp = other.Obj;
     171     2639515 :     other.Obj = Obj;
     172     2257319 :     Obj = tmp;
     173         718 :   }
     174          79 : 
     175         730 :   void reset() {
     176           0 :     release();
     177        1294 :     Obj = nullptr;
     178        8870 :   }
     179         888 : 
     180          43 :   void resetWithoutRelease() { Obj = nullptr; }
     181          43 : 
     182      115912 : private:
     183       40888 :   void retain() {
     184    29758967 :     if (Obj)
     185        2366 :       IntrusiveRefCntPtrInfo<T>::retain(Obj);
     186           0 :   }
     187       13242 : 
     188           0 :   void release() {
     189    24661109 :     if (Obj)
     190           0 :       IntrusiveRefCntPtrInfo<T>::release(Obj);
     191          23 :   }
     192          38 : 
     193        2490 :   template <typename X> friend class IntrusiveRefCntPtr;
     194      107651 : };
     195        7428 : 
     196       63268 : template <class T, class U>
     197     2336938 : inline bool operator==(const IntrusiveRefCntPtr<T> &A,
     198           0 :                        const IntrusiveRefCntPtr<U> &B) {
     199      736304 :   return A.get() == B.get();
     200        5502 : }
     201     1920028 : 
     202       27691 : template <class T, class U>
     203           0 : inline bool operator!=(const IntrusiveRefCntPtr<T> &A,
     204          76 :                        const IntrusiveRefCntPtr<U> &B) {
     205         774 :   return A.get() != B.get();
     206        1367 : }
     207       90841 : 
     208           0 : template <class T, class U>
     209       57021 : inline bool operator==(const IntrusiveRefCntPtr<T> &A, U *B) {
     210       36236 :   return A.get() == B;
     211      334266 : }
     212      272714 : 
     213     1000137 : template <class T, class U>
     214         786 : inline bool operator!=(const IntrusiveRefCntPtr<T> &A, U *B) {
     215      272569 :   return A.get() != B;
     216       11710 : }
     217        2406 : 
     218           0 : template <class T, class U>
     219       10394 : inline bool operator==(T *A, const IntrusiveRefCntPtr<U> &B) {
     220          12 :   return A == B.get();
     221        2691 : }
     222           0 : 
     223      504531 : template <class T, class U>
     224           0 : inline bool operator!=(T *A, const IntrusiveRefCntPtr<U> &B) {
     225          60 :   return A != B.get();
     226           0 : }
     227          46 : 
     228        3204 : template <class T>
     229         629 : bool operator==(std::nullptr_t A, const IntrusiveRefCntPtr<T> &B) {
     230           8 :   return !B;
     231         364 : }
     232           0 : 
     233           0 : template <class T>
     234           0 : bool operator==(const IntrusiveRefCntPtr<T> &A, std::nullptr_t B) {
     235       17944 :   return B == A;
     236        2994 : }
     237           0 : 
     238          54 : template <class T>
     239           0 : bool operator!=(std::nullptr_t A, const IntrusiveRefCntPtr<T> &B) {
     240           0 :   return !(A == B);
     241           0 : }
     242       33569 : 
     243        4170 : template <class T>
     244           0 : bool operator!=(const IntrusiveRefCntPtr<T> &A, std::nullptr_t B) {
     245           7 :   return !(A == B);
     246       11226 : }
     247       11226 : 
     248           0 : // Make IntrusiveRefCntPtr work with dyn_cast, isa, and the other idioms from
     249           0 : // Casting.h.
     250           0 : template <typename From> struct simplify_type;
     251         132 : 
     252           0 : template <class T> struct simplify_type<IntrusiveRefCntPtr<T>> {
     253       15398 :   using SimpleType = T *;
     254       19582 : 
     255           0 :   static SimpleType getSimplifiedValue(IntrusiveRefCntPtr<T> &Val) {
     256       32240 :     return Val.get();
     257        3653 :   }
     258           0 : };
     259           0 : 
     260           0 : template <class T> struct simplify_type<const IntrusiveRefCntPtr<T>> {
     261           0 :   using SimpleType = /*const*/ T *;
     262         117 : 
     263           0 :   static SimpleType getSimplifiedValue(const IntrusiveRefCntPtr<T> &Val) {
     264      282551 :     return Val.get();
     265           0 :   }
     266           0 : };
     267           0 : 
     268           0 : } // end namespace llvm
     269           0 : 
     270           0 : #endif // LLVM_ADT_INTRUSIVEREFCNTPTR_H

Generated by: LCOV version 1.13