2009-02-20 22:20:18 +00:00
|
|
|
//== llvm/ADT/IntrusiveRefCntPtr.h - Smart Refcounting Pointer ---*- C++ -*-==//
|
2008-02-22 17:26:05 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines IntrusiveRefCntPtr, a template class that
|
|
|
|
// implements a "smart" pointer for objects that maintain their own
|
|
|
|
// internal reference count, and RefCountedBase/RefCountedBaseVPTR, two
|
|
|
|
// generic base classes for objects that wish to have their lifetimes
|
|
|
|
// managed using reference counting.
|
|
|
|
//
|
|
|
|
// IntrusiveRefCntPtr is similar to Boost's intrusive_ptr with added
|
|
|
|
// LLVM-style casting.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-10 00:45:19 +00:00
|
|
|
#ifndef LLVM_ADT_INTRUSIVEREFCNTPTR_H
|
|
|
|
#define LLVM_ADT_INTRUSIVEREFCNTPTR_H
|
2008-02-22 17:26:05 +00:00
|
|
|
|
|
|
|
#include "llvm/Support/Casting.h"
|
2012-05-27 16:22:08 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2012-05-27 20:46:04 +00:00
|
|
|
#include <memory>
|
2014-03-05 15:24:33 +00:00
|
|
|
#include <atomic>
|
2008-02-22 17:26:05 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
template <class T>
|
2008-02-26 21:44:24 +00:00
|
|
|
class IntrusiveRefCntPtr;
|
2008-02-22 17:26:05 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// 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
|
2012-08-01 14:53:13 +00:00
|
|
|
/// typically handled with IntrusiveRefCntPtr "smart pointers" (see below)
|
2008-02-22 17:26:05 +00:00
|
|
|
/// 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
|
|
|
|
/// reference count hits 0) on such objects is an error.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
template <class Derived>
|
|
|
|
class RefCountedBase {
|
2011-04-04 00:57:03 +00:00
|
|
|
mutable unsigned ref_cnt;
|
2008-02-22 17:26:05 +00:00
|
|
|
|
2011-03-22 01:15:07 +00:00
|
|
|
public:
|
2008-02-22 17:26:05 +00:00
|
|
|
RefCountedBase() : ref_cnt(0) {}
|
2011-11-17 23:02:14 +00:00
|
|
|
RefCountedBase(const RefCountedBase &) : ref_cnt(0) {}
|
2008-02-22 17:26:05 +00:00
|
|
|
|
2011-04-04 00:57:03 +00:00
|
|
|
void Retain() const { ++ref_cnt; }
|
|
|
|
void Release() const {
|
2008-02-22 17:26:05 +00:00
|
|
|
assert (ref_cnt > 0 && "Reference count is already zero.");
|
2011-04-04 00:57:03 +00:00
|
|
|
if (--ref_cnt == 0) delete static_cast<const Derived*>(this);
|
2008-02-22 17:26:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// RefCountedBaseVPTR - A class that has the same function as
|
|
|
|
/// RefCountedBase, but with a virtual destructor. Should be used
|
2008-02-26 21:44:24 +00:00
|
|
|
/// 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.
|
2008-02-22 17:26:05 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
class RefCountedBaseVPTR {
|
2011-04-04 00:57:03 +00:00
|
|
|
mutable unsigned ref_cnt;
|
2011-12-20 02:50:00 +00:00
|
|
|
virtual void anchor();
|
2008-02-22 17:26:05 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
RefCountedBaseVPTR() : ref_cnt(0) {}
|
2011-11-17 23:02:14 +00:00
|
|
|
RefCountedBaseVPTR(const RefCountedBaseVPTR &) : ref_cnt(0) {}
|
|
|
|
|
2008-02-22 17:26:05 +00:00
|
|
|
virtual ~RefCountedBaseVPTR() {}
|
|
|
|
|
2011-04-04 00:57:03 +00:00
|
|
|
void Retain() const { ++ref_cnt; }
|
|
|
|
void Release() const {
|
2008-02-22 17:26:05 +00:00
|
|
|
assert (ref_cnt > 0 && "Reference count is already zero.");
|
|
|
|
if (--ref_cnt == 0) delete this;
|
|
|
|
}
|
|
|
|
|
2011-03-20 06:14:56 +00:00
|
|
|
template <typename T>
|
2012-01-31 19:58:34 +00:00
|
|
|
friend struct IntrusiveRefCntPtrInfo;
|
2008-02-22 17:26:05 +00:00
|
|
|
};
|
|
|
|
|
2012-01-31 00:57:04 +00:00
|
|
|
|
|
|
|
template <typename T> struct IntrusiveRefCntPtrInfo {
|
|
|
|
static void retain(T *obj) { obj->Retain(); }
|
|
|
|
static void release(T *obj) { obj->Release(); }
|
|
|
|
};
|
2014-03-05 15:24:33 +00:00
|
|
|
|
|
|
|
/// \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 {
|
2014-03-05 16:26:04 +00:00
|
|
|
mutable std::atomic<int> RefCount;
|
2014-03-05 15:24:33 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
2012-01-31 00:57:04 +00:00
|
|
|
|
2008-02-22 17:26:05 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// IntrusiveRefCntPtr - A template class that implements a "smart pointer"
|
|
|
|
/// that assumes the wrapped object has a reference count associated
|
|
|
|
/// with it that can be managed via calls to
|
|
|
|
/// IntrusivePtrAddRef/IntrusivePtrRelease. The smart pointers
|
|
|
|
/// manage reference counts via the RAII idiom: upon creation of
|
|
|
|
/// smart pointer the reference count of the wrapped object is
|
|
|
|
/// incremented and upon destruction of the smart pointer the
|
|
|
|
/// reference count is decremented. This class also safely handles
|
|
|
|
/// wrapping NULL pointers.
|
2008-02-26 21:44:24 +00:00
|
|
|
///
|
|
|
|
/// 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.
|
2008-02-22 17:26:05 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
template <typename T>
|
|
|
|
class IntrusiveRefCntPtr {
|
|
|
|
T* Obj;
|
|
|
|
typedef IntrusiveRefCntPtr this_type;
|
|
|
|
public:
|
|
|
|
typedef T element_type;
|
|
|
|
|
2008-02-26 21:44:24 +00:00
|
|
|
explicit IntrusiveRefCntPtr() : Obj(0) {}
|
2008-02-22 17:26:05 +00:00
|
|
|
|
2012-01-31 00:57:08 +00:00
|
|
|
IntrusiveRefCntPtr(T* obj) : Obj(obj) {
|
2008-02-22 17:26:05 +00:00
|
|
|
retain();
|
|
|
|
}
|
|
|
|
|
|
|
|
IntrusiveRefCntPtr(const IntrusiveRefCntPtr& S) : Obj(S.Obj) {
|
|
|
|
retain();
|
|
|
|
}
|
|
|
|
|
2012-05-27 16:22:08 +00:00
|
|
|
IntrusiveRefCntPtr(IntrusiveRefCntPtr&& S) : Obj(S.Obj) {
|
|
|
|
S.Obj = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class X>
|
|
|
|
IntrusiveRefCntPtr(IntrusiveRefCntPtr<X>&& S) : Obj(S.getPtr()) {
|
|
|
|
S.Obj = 0;
|
|
|
|
}
|
|
|
|
|
2008-02-22 17:26:05 +00:00
|
|
|
template <class X>
|
|
|
|
IntrusiveRefCntPtr(const IntrusiveRefCntPtr<X>& S)
|
|
|
|
: Obj(S.getPtr()) {
|
|
|
|
retain();
|
|
|
|
}
|
|
|
|
|
2012-05-31 22:25:25 +00:00
|
|
|
IntrusiveRefCntPtr& operator=(IntrusiveRefCntPtr S) {
|
|
|
|
swap(S);
|
2008-02-22 17:26:05 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
~IntrusiveRefCntPtr() { release(); }
|
|
|
|
|
|
|
|
T& operator*() const { return *Obj; }
|
|
|
|
|
|
|
|
T* operator->() const { return Obj; }
|
|
|
|
|
|
|
|
T* getPtr() const { return Obj; }
|
|
|
|
|
2008-04-11 16:42:06 +00:00
|
|
|
typedef T* (IntrusiveRefCntPtr::*unspecified_bool_type) () const;
|
2008-02-22 17:26:05 +00:00
|
|
|
operator unspecified_bool_type() const {
|
2008-02-26 21:44:24 +00:00
|
|
|
return Obj == 0 ? 0 : &IntrusiveRefCntPtr::getPtr;
|
2008-02-22 17:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void swap(IntrusiveRefCntPtr& other) {
|
|
|
|
T* tmp = other.Obj;
|
|
|
|
other.Obj = Obj;
|
|
|
|
Obj = tmp;
|
|
|
|
}
|
2011-12-08 22:50:09 +00:00
|
|
|
|
|
|
|
void reset() {
|
|
|
|
release();
|
|
|
|
Obj = 0;
|
|
|
|
}
|
|
|
|
|
2011-03-21 18:37:59 +00:00
|
|
|
void resetWithoutRelease() {
|
|
|
|
Obj = 0;
|
|
|
|
}
|
2008-02-22 17:26:05 +00:00
|
|
|
|
|
|
|
private:
|
2012-01-31 00:57:04 +00:00
|
|
|
void retain() { if (Obj) IntrusiveRefCntPtrInfo<T>::retain(Obj); }
|
|
|
|
void release() { if (Obj) IntrusiveRefCntPtrInfo<T>::release(Obj); }
|
2008-02-22 17:26:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<class T, class U>
|
|
|
|
inline bool operator==(const IntrusiveRefCntPtr<T>& A,
|
|
|
|
const IntrusiveRefCntPtr<U>& B)
|
|
|
|
{
|
|
|
|
return A.getPtr() == B.getPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T, class U>
|
|
|
|
inline bool operator!=(const IntrusiveRefCntPtr<T>& A,
|
|
|
|
const IntrusiveRefCntPtr<U>& B)
|
|
|
|
{
|
|
|
|
return A.getPtr() != B.getPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T, class U>
|
|
|
|
inline bool operator==(const IntrusiveRefCntPtr<T>& A,
|
|
|
|
U* B)
|
|
|
|
{
|
|
|
|
return A.getPtr() == B;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T, class U>
|
|
|
|
inline bool operator!=(const IntrusiveRefCntPtr<T>& A,
|
|
|
|
U* B)
|
|
|
|
{
|
|
|
|
return A.getPtr() != B;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T, class U>
|
|
|
|
inline bool operator==(T* A,
|
|
|
|
const IntrusiveRefCntPtr<U>& B)
|
|
|
|
{
|
|
|
|
return A == B.getPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T, class U>
|
|
|
|
inline bool operator!=(T* A,
|
|
|
|
const IntrusiveRefCntPtr<U>& B)
|
|
|
|
{
|
|
|
|
return A != B.getPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// LLVM-style downcasting support for IntrusiveRefCntPtr objects
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
template<class T> struct simplify_type<IntrusiveRefCntPtr<T> > {
|
|
|
|
typedef T* SimpleType;
|
2013-03-27 16:43:11 +00:00
|
|
|
static SimpleType getSimplifiedValue(IntrusiveRefCntPtr<T>& Val) {
|
2008-02-22 17:26:05 +00:00
|
|
|
return Val.getPtr();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T> struct simplify_type<const IntrusiveRefCntPtr<T> > {
|
2013-03-27 16:43:11 +00:00
|
|
|
typedef /*const*/ T* SimpleType;
|
2008-02-22 17:26:05 +00:00
|
|
|
static SimpleType getSimplifiedValue(const IntrusiveRefCntPtr<T>& Val) {
|
|
|
|
return Val.getPtr();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
2013-01-10 00:45:19 +00:00
|
|
|
#endif // LLVM_ADT_INTRUSIVEREFCNTPTR_H
|