Prevent use of the implicit copy constructor on SmallPtrSetImpl. An accidental copy...
[oota-llvm.git] / include / llvm / ADT / IntrusiveRefCntPtr.h
index 1a27d6cfce202c1f505e26c6d1624ba3fde552e7..f9df3781257e4678df616010d561ace064d081ca 100644 (file)
@@ -1,4 +1,4 @@
-//== llvm/ADT/IntrusiveRefCntPtr.h - Smart Refcounting Pointer ----*- C++ -*-==//
+//== llvm/ADT/IntrusiveRefCntPtr.h - Smart Refcounting Pointer ---*- C++ -*-==//
 //
 //                     The LLVM Compiler Infrastructure
 //
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LLVM_ADT_INTRUSIVE_REF_CNT_PTR
-#define LLVM_ADT_INTRUSIVE_REF_CNT_PTR
-
-#include <cassert>
-#include <cstddef>
+#ifndef LLVM_ADT_INTRUSIVEREFCNTPTR_H
+#define LLVM_ADT_INTRUSIVEREFCNTPTR_H
 
 #include "llvm/Support/Casting.h"
-
-// Forward declarations
+#include "llvm/Support/Compiler.h"
+#include <atomic>
+#include <memory>
 
 namespace llvm {
-  template <class T>
-  class RefCountedBase;
 
   template <class T>
-  class RefCountedBaseVPTR;
-}
-
-template <class T>
-void IntrusivePtrAddRef(llvm::RefCountedBase<T>*);
-
-template <class T>
-void IntrusivePtrRelease(llvm::RefCountedBase<T>*);
-
-template <class T>
-void IntrusivePtrAddRef(llvm::RefCountedBaseVPTR<T>*);
-
-template <class T>
-void IntrusivePtrRelease(llvm::RefCountedBaseVPTR<T>*);
-
-
-namespace llvm {
+  class IntrusiveRefCntPtr;
 
 //===----------------------------------------------------------------------===//
 /// RefCountedBase - A generic base class for objects that wish to
 ///  have their lifetimes managed using reference counts. Classes
 ///  subclass RefCountedBase to obtain such functionality, and are
-///  typically handled with IntrusivePtr "smart pointers" (see below)
+///  typically handled with IntrusiveRefCntPtr "smart pointers" (see below)
 ///  which automatically handle the management of reference counts.
 ///  Objects that subclass RefCountedBase should not be allocated on
 ///  the stack, as invoking "delete" (which is called when the
@@ -63,69 +43,78 @@ namespace llvm {
 //===----------------------------------------------------------------------===//
   template <class Derived>
   class RefCountedBase {
-    unsigned ref_cnt;
+    mutable unsigned ref_cnt;
 
-  protected:
+  public:
     RefCountedBase() : ref_cnt(0) {}
+    RefCountedBase(const RefCountedBase &) : ref_cnt(0) {}
 
-    void Retain() { ++ref_cnt; }
-    void Release() {
+    void Retain() const { ++ref_cnt; }
+    void Release() const {
       assert (ref_cnt > 0 && "Reference count is already zero.");
-      if (--ref_cnt == 0) delete static_cast<Derived*>(this);
+      if (--ref_cnt == 0) delete static_cast<const Derived*>(this);
     }
-
-    friend void IntrusivePtrAddRef<Derived>(RefCountedBase<Derived>*);
-    friend void IntrusivePtrRelease<Derived>(RefCountedBase<Derived>*);
   };
 
 //===----------------------------------------------------------------------===//
 /// RefCountedBaseVPTR - A class that has the same function as
 ///  RefCountedBase, but with a virtual destructor. Should be used
-///  instead of RefCountedBase for classes that have virtual
-///  destructors. Classes that inherit from RefCountedBaseVPTR can't
-///  be allocated on stack.
+///  instead of RefCountedBase for classes that already have virtual
+///  methods to enforce dynamic allocation via 'new'. Classes that
+///  inherit from RefCountedBaseVPTR can't be allocated on stack -
+///  attempting to do this will produce a compile error.
 //===----------------------------------------------------------------------===//
-  template <class Derived>
   class RefCountedBaseVPTR {
-    unsigned ref_cnt;
+    mutable unsigned ref_cnt;
+    virtual void anchor();
 
   protected:
     RefCountedBaseVPTR() : ref_cnt(0) {}
+    RefCountedBaseVPTR(const RefCountedBaseVPTR &) : ref_cnt(0) {}
+
     virtual ~RefCountedBaseVPTR() {}
 
-    void Retain() { ++ref_cnt; }
-    void Release() {
+    void Retain() const { ++ref_cnt; }
+    void Release() const {
       assert (ref_cnt > 0 && "Reference count is already zero.");
       if (--ref_cnt == 0) delete this;
     }
 
-    friend void IntrusivePtrAddRef<Derived>(RefCountedBaseVPTR<Derived>*);
-    friend void IntrusivePtrRelease<Derived>(RefCountedBaseVPTR<Derived>*);
+    template <typename T>
+    friend struct IntrusiveRefCntPtrInfo;
   };
 
-}
-
-//===----------------------------------------------------------------------===//
-/// IntrusivePtrAddRef - A utility function used by IntrusiveRefCntPtr
-///  to increment the reference count of an RefCountedBase-derived object.
-//===----------------------------------------------------------------------===//
-template <class T>
-void IntrusivePtrAddRef(llvm::RefCountedBase<T>* O) {
-  O->Retain();
-}
-
-//===----------------------------------------------------------------------===//
-/// IntrusivePtrRelease - The complement of IntrusivePtrAddRef;
-///  decrements the reference count of a RefCounted object.
-//===----------------------------------------------------------------------===//
-template <class T>
-void IntrusivePtrRelease(llvm::RefCountedBase<T>* O) {
-  O->Release();
-}
-
-
-namespace llvm {
+  
+  template <typename T> struct IntrusiveRefCntPtrInfo {
+    static void retain(T *obj) { obj->Retain(); }
+    static void release(T *obj) { obj->Release(); }
+  };
 
+/// \brief A thread-safe version of \c llvm::RefCountedBase.
+///
+/// A generic base class for objects that wish to have their lifetimes managed
+/// using reference counts. Classes subclass \c ThreadSafeRefCountedBase to
+/// obtain such functionality, and are typically handled with
+/// \c IntrusiveRefCntPtr "smart pointers" which automatically handle the
+/// management of reference counts.
+template <class Derived>
+class ThreadSafeRefCountedBase {
+  mutable std::atomic<int> RefCount;
+
+protected:
+  ThreadSafeRefCountedBase() : RefCount(0) {}
+
+public:
+  void Retain() const { ++RefCount; }
+
+  void Release() const {
+    int NewRefCount = --RefCount;
+    assert(NewRefCount >= 0 && "Reference count was already zero.");
+    if (NewRefCount == 0)
+      delete static_cast<const Derived*>(this);
+  }
+};
+  
 //===----------------------------------------------------------------------===//
 /// IntrusiveRefCntPtr - A template class that implements a "smart pointer"
 ///  that assumes the wrapped object has a reference count associated
@@ -136,17 +125,23 @@ namespace llvm {
 ///  incremented and upon destruction of the smart pointer the
 ///  reference count is decremented.  This class also safely handles
 ///  wrapping NULL pointers.
+///
+/// Reference counting is implemented via calls to
+///  Obj->Retain()/Obj->Release(). Release() is required to destroy
+///  the object when the reference count reaches zero. Inheriting from
+///  RefCountedBase/RefCountedBaseVPTR takes care of this
+///  automatically.
 //===----------------------------------------------------------------------===//
   template <typename T>
   class IntrusiveRefCntPtr {
     T* Obj;
-    typedef IntrusiveRefCntPtr this_type;
+
   public:
     typedef T element_type;
 
-    explicit IntrusiveRefCntPtr() : Obj(NULL) {}
+    explicit IntrusiveRefCntPtr() : Obj(nullptr) {}
 
-    explicit IntrusiveRefCntPtr(T* obj) : Obj(obj) {
+    IntrusiveRefCntPtr(T* obj) : Obj(obj) {
       retain();
     }
 
@@ -154,20 +149,23 @@ namespace llvm {
       retain();
     }
 
+    IntrusiveRefCntPtr(IntrusiveRefCntPtr&& S) : Obj(S.Obj) {
+      S.Obj = nullptr;
+    }
+
     template <class X>
-    IntrusiveRefCntPtr(const IntrusiveRefCntPtr<X>& S)
-      : Obj(S.getPtr()) {
-      retain();
+    IntrusiveRefCntPtr(IntrusiveRefCntPtr<X>&& S) : Obj(S.get()) {
+      S.Obj = 0;
     }
 
     template <class X>
-    IntrusiveRefCntPtr& operator=(const IntrusiveRefCntPtr<X>& S) {
-      replace(S.getPtr());
-      return *this;
+    IntrusiveRefCntPtr(const IntrusiveRefCntPtr<X>& S)
+      : Obj(S.get()) {
+      retain();
     }
 
-    IntrusiveRefCntPtr& operator=(T * S) {
-      replace(S);
+    IntrusiveRefCntPtr& operator=(IntrusiveRefCntPtr S) {
+      swap(S);
       return *this;
     }
 
@@ -177,12 +175,9 @@ namespace llvm {
 
     T* operator->() const { return Obj; }
 
-    T* getPtr() const { return Obj; }
+    T* get() const { return Obj; }
 
-    typedef T * IntrusiveRefCntPtr::*unspecified_bool_type;
-    operator unspecified_bool_type() const {
-      return Obj == NULL ? NULL : &IntrusiveRefCntPtr::getPtr;
-    }
+    LLVM_EXPLICIT operator bool() const { return Obj; }
 
     void swap(IntrusiveRefCntPtr& other) {
       T* tmp = other.Obj;
@@ -190,55 +185,80 @@ namespace llvm {
       Obj = tmp;
     }
 
-  private:
-    void retain() { if (Obj) IntrusivePtrAddRef(Obj); }
-    void release() { if (Obj) IntrusivePtrRelease(Obj); }
+    void reset() {
+      release();
+      Obj = nullptr;
+    }
 
-    void replace(T* S) {
-      this_type(S).swap(this);
+    void resetWithoutRelease() {
+      Obj = 0;
     }
+
+  private:
+    void retain() { if (Obj) IntrusiveRefCntPtrInfo<T>::retain(Obj); }
+    void release() { if (Obj) IntrusiveRefCntPtrInfo<T>::release(Obj); }
   };
 
   template<class T, class U>
   inline bool operator==(const IntrusiveRefCntPtr<T>& A,
                          const IntrusiveRefCntPtr<U>& B)
   {
-    return A.getPtr() == B.getPtr();
+    return A.get() == B.get();
   }
 
   template<class T, class U>
   inline bool operator!=(const IntrusiveRefCntPtr<T>& A,
                          const IntrusiveRefCntPtr<U>& B)
   {
-    return A.getPtr() != B.getPtr();
+    return A.get() != B.get();
   }
 
   template<class T, class U>
   inline bool operator==(const IntrusiveRefCntPtr<T>& A,
                          U* B)
   {
-    return A.getPtr() == B;
+    return A.get() == B;
   }
 
   template<class T, class U>
   inline bool operator!=(const IntrusiveRefCntPtr<T>& A,
                          U* B)
   {
-    return A.getPtr() != B;
+    return A.get() != B;
   }
 
   template<class T, class U>
   inline bool operator==(T* A,
                          const IntrusiveRefCntPtr<U>& B)
   {
-    return A == B.getPtr();
+    return A == B.get();
   }
 
   template<class T, class U>
   inline bool operator!=(T* A,
                          const IntrusiveRefCntPtr<U>& B)
   {
-    return A != B.getPtr();
+    return A != B.get();
+  }
+
+  template <class T>
+  bool operator==(std::nullptr_t A, const IntrusiveRefCntPtr<T> &B) {
+    return !B;
+  }
+
+  template <class T>
+  bool operator==(const IntrusiveRefCntPtr<T> &A, std::nullptr_t B) {
+    return B == A;
+  }
+
+  template <class T>
+  bool operator!=(std::nullptr_t A, const IntrusiveRefCntPtr<T> &B) {
+    return !(A == B);
+  }
+
+  template <class T>
+  bool operator!=(const IntrusiveRefCntPtr<T> &A, std::nullptr_t B) {
+    return !(A == B);
   }
 
 //===----------------------------------------------------------------------===//
@@ -247,18 +267,18 @@ namespace llvm {
 
   template<class T> struct simplify_type<IntrusiveRefCntPtr<T> > {
     typedef T* SimpleType;
-    static SimpleType getSimplifiedValue(const IntrusiveRefCntPtr<T>& Val) {
-      return Val.getPtr();
+    static SimpleType getSimplifiedValue(IntrusiveRefCntPtr<T>& Val) {
+      return Val.get();
     }
   };
 
   template<class T> struct simplify_type<const IntrusiveRefCntPtr<T> > {
-    typedef T* SimpleType;
+    typedef /*const*/ T* SimpleType;
     static SimpleType getSimplifiedValue(const IntrusiveRefCntPtr<T>& Val) {
-      return Val.getPtr();
+      return Val.get();
     }
   };
 
 } // end namespace llvm
 
-#endif // LLVM_ADT_INTRUSIVE_REF_CNT_PTR
\ No newline at end of file
+#endif // LLVM_ADT_INTRUSIVEREFCNTPTR_H