2013-07-24 07:41:39 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-06-04 03:36:43 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 11:12:37 +00:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2011-12-28 16:48:54 +00:00
|
|
|
/* Helpers for defining and using refcounted objects. */
|
|
|
|
|
2013-07-24 07:41:39 +00:00
|
|
|
#ifndef mozilla_RefPtr_h
|
|
|
|
#define mozilla_RefPtr_h
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2011-12-19 19:28:35 +00:00
|
|
|
#include "mozilla/Assertions.h"
|
2013-05-18 07:52:53 +00:00
|
|
|
#include "mozilla/Atomics.h"
|
2011-11-20 20:22:51 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-07-11 02:10:17 +00:00
|
|
|
#include "mozilla/NullPtr.h"
|
2014-02-21 19:45:50 +00:00
|
|
|
#include "mozilla/RefCountType.h"
|
2013-04-19 21:59:01 +00:00
|
|
|
#include "mozilla/TypeTraits.h"
|
2014-03-09 18:36:36 +00:00
|
|
|
#if defined(MOZILLA_INTERNAL_API)
|
|
|
|
#include "nsXPCOM.h"
|
|
|
|
#endif
|
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
#if defined(MOZILLA_INTERNAL_API) && \
|
|
|
|
(defined(DEBUG) || defined(FORCE_BUILD_REFCNT_LOGGING))
|
2014-03-09 18:36:36 +00:00
|
|
|
#define MOZ_REFCOUNTED_LEAK_CHECKING
|
|
|
|
#endif
|
2011-06-17 03:40:20 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
template<typename T> class RefCounted;
|
|
|
|
template<typename T> class RefPtr;
|
|
|
|
template<typename T> class TemporaryRef;
|
|
|
|
template<typename T> class OutParamRef;
|
|
|
|
template<typename T> OutParamRef<T> byRef(RefPtr<T>&);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RefCounted<T> is a sort of a "mixin" for a class T. RefCounted
|
|
|
|
* manages, well, refcounting for T, and because RefCounted is
|
|
|
|
* parameterized on T, RefCounted<T> can call T's destructor directly.
|
|
|
|
* This means T doesn't need to have a virtual dtor and so doesn't
|
|
|
|
* need a vtable.
|
|
|
|
*
|
|
|
|
* RefCounted<T> is created with refcount == 0. Newly-allocated
|
|
|
|
* RefCounted<T> must immediately be assigned to a RefPtr to make the
|
|
|
|
* refcount > 0. It's an error to allocate and free a bare
|
|
|
|
* RefCounted<T>, i.e. outside of the RefPtr machinery. Attempts to
|
|
|
|
* do so will abort DEBUG builds.
|
|
|
|
*
|
|
|
|
* Live RefCounted<T> have refcount > 0. The lifetime (refcounts) of
|
|
|
|
* live RefCounted<T> are controlled by RefPtr<T> and
|
|
|
|
* RefPtr<super/subclass of T>. Upon a transition from refcounted==1
|
|
|
|
* to 0, the RefCounted<T> "dies" and is destroyed. The "destroyed"
|
2013-01-29 08:35:16 +00:00
|
|
|
* state is represented in DEBUG builds by refcount==0xffffdead. This
|
2011-06-17 03:40:20 +00:00
|
|
|
* state distinguishes use-before-ref (refcount==0) from
|
2013-01-29 08:35:16 +00:00
|
|
|
* use-after-destroy (refcount==0xffffdead).
|
2014-02-21 02:33:49 +00:00
|
|
|
*
|
|
|
|
* Note that when deriving from RefCounted or AtomicRefCounted, you
|
|
|
|
* should add MOZ_DECLARE_REFCOUNTED_TYPENAME(ClassName) to the public
|
|
|
|
* section of your class, where ClassName is the name of your class.
|
2011-06-17 03:40:20 +00:00
|
|
|
*/
|
2013-02-05 03:53:57 +00:00
|
|
|
namespace detail {
|
2013-05-18 07:52:53 +00:00
|
|
|
#ifdef DEBUG
|
2014-02-25 02:08:51 +00:00
|
|
|
const MozRefCountType DEAD = 0xffffdead;
|
2013-02-05 03:53:57 +00:00
|
|
|
#endif
|
|
|
|
|
2014-03-09 18:36:36 +00:00
|
|
|
// When building code that gets compiled into Gecko, try to use the
|
|
|
|
// trace-refcount leak logging facilities.
|
|
|
|
#ifdef MOZ_REFCOUNTED_LEAK_CHECKING
|
|
|
|
class RefCountLogger
|
|
|
|
{
|
2014-07-11 02:10:17 +00:00
|
|
|
public:
|
|
|
|
static void logAddRef(const void* aPointer, MozRefCountType aRefCount,
|
|
|
|
const char* aTypeName, uint32_t aInstanceSize)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aRefCount != DEAD);
|
|
|
|
NS_LogAddRef(const_cast<void*>(aPointer), aRefCount, aTypeName,
|
|
|
|
aInstanceSize);
|
|
|
|
}
|
2014-03-09 18:36:36 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
static void logRelease(const void* aPointer, MozRefCountType aRefCount,
|
|
|
|
const char* aTypeName)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aRefCount != DEAD);
|
|
|
|
NS_LogRelease(const_cast<void*>(aPointer), aRefCount, aTypeName);
|
|
|
|
}
|
2014-03-09 18:36:36 +00:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2013-05-18 07:52:53 +00:00
|
|
|
// This is used WeakPtr.h as well as this file.
|
|
|
|
enum RefCountAtomicity
|
|
|
|
{
|
|
|
|
AtomicRefCount,
|
|
|
|
NonAtomicRefCount
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, RefCountAtomicity Atomicity>
|
2011-06-17 03:40:20 +00:00
|
|
|
class RefCounted
|
|
|
|
{
|
2014-07-11 02:10:17 +00:00
|
|
|
friend class RefPtr<T>;
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
protected:
|
|
|
|
RefCounted() : mRefCnt(0) {}
|
|
|
|
~RefCounted() { MOZ_ASSERT(mRefCnt == detail::DEAD); }
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
public:
|
|
|
|
// Compatibility with nsRefPtr.
|
|
|
|
void AddRef() const
|
|
|
|
{
|
|
|
|
// Note: this method must be thread safe for AtomicRefCounted.
|
|
|
|
MOZ_ASSERT(int32_t(mRefCnt) >= 0);
|
2014-03-25 13:02:26 +00:00
|
|
|
#ifndef MOZ_REFCOUNTED_LEAK_CHECKING
|
2014-07-11 02:10:17 +00:00
|
|
|
++mRefCnt;
|
2014-03-25 13:02:26 +00:00
|
|
|
#else
|
2014-07-11 02:10:17 +00:00
|
|
|
const char* type = static_cast<const T*>(this)->typeName();
|
|
|
|
uint32_t size = static_cast<const T*>(this)->typeSize();
|
|
|
|
const void* ptr = static_cast<const T*>(this);
|
|
|
|
MozRefCountType cnt = ++mRefCnt;
|
|
|
|
detail::RefCountLogger::logAddRef(ptr, cnt, type, size);
|
2014-03-09 18:36:36 +00:00
|
|
|
#endif
|
2014-07-11 02:10:17 +00:00
|
|
|
}
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
void Release() const
|
|
|
|
{
|
|
|
|
// Note: this method must be thread safe for AtomicRefCounted.
|
|
|
|
MOZ_ASSERT(int32_t(mRefCnt) > 0);
|
2014-03-25 13:02:26 +00:00
|
|
|
#ifndef MOZ_REFCOUNTED_LEAK_CHECKING
|
2014-07-11 02:10:17 +00:00
|
|
|
MozRefCountType cnt = --mRefCnt;
|
2014-03-25 13:02:26 +00:00
|
|
|
#else
|
2014-07-11 02:10:17 +00:00
|
|
|
const char* type = static_cast<const T*>(this)->typeName();
|
|
|
|
const void* ptr = static_cast<const T*>(this);
|
|
|
|
MozRefCountType cnt = --mRefCnt;
|
|
|
|
// Note: it's not safe to touch |this| after decrementing the refcount,
|
|
|
|
// except for below.
|
|
|
|
detail::RefCountLogger::logRelease(ptr, cnt, type);
|
2014-03-09 18:36:36 +00:00
|
|
|
#endif
|
2014-07-11 02:10:17 +00:00
|
|
|
if (0 == cnt) {
|
|
|
|
// Because we have atomically decremented the refcount above, only
|
|
|
|
// one thread can get a 0 count here, so as long as we can assume that
|
|
|
|
// everything else in the system is accessing this object through
|
|
|
|
// RefPtrs, it's safe to access |this| here.
|
2011-06-17 03:40:20 +00:00
|
|
|
#ifdef DEBUG
|
2014-07-11 02:10:17 +00:00
|
|
|
mRefCnt = detail::DEAD;
|
2011-06-17 03:40:20 +00:00
|
|
|
#endif
|
2014-07-11 02:10:17 +00:00
|
|
|
delete static_cast<const T*>(this);
|
2011-06-17 03:40:20 +00:00
|
|
|
}
|
2014-07-11 02:10:17 +00:00
|
|
|
}
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
// Compatibility with wtf::RefPtr.
|
|
|
|
void ref() { AddRef(); }
|
|
|
|
void deref() { Release(); }
|
|
|
|
MozRefCountType refCount() const { return mRefCnt; }
|
|
|
|
bool hasOneRef() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mRefCnt > 0);
|
|
|
|
return mRefCnt == 1;
|
|
|
|
}
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
private:
|
|
|
|
mutable typename Conditional<Atomicity == AtomicRefCount,
|
|
|
|
Atomic<MozRefCountType>,
|
|
|
|
MozRefCountType>::Type mRefCnt;
|
2013-05-18 07:52:53 +00:00
|
|
|
};
|
|
|
|
|
2014-03-09 18:36:36 +00:00
|
|
|
#ifdef MOZ_REFCOUNTED_LEAK_CHECKING
|
2014-02-24 13:23:37 +00:00
|
|
|
#define MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(T) \
|
2014-02-26 15:13:48 +00:00
|
|
|
virtual const char* typeName() const { return #T; } \
|
|
|
|
virtual size_t typeSize() const { return sizeof(*this); }
|
|
|
|
#else
|
|
|
|
#define MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(T)
|
|
|
|
#endif
|
2014-02-24 13:23:37 +00:00
|
|
|
|
2014-06-19 00:11:21 +00:00
|
|
|
// Note that this macro is expanded unconditionally because it declares only
|
|
|
|
// two small inline functions which will hopefully get eliminated by the linker
|
|
|
|
// in non-leak-checking builds.
|
|
|
|
#define MOZ_DECLARE_REFCOUNTED_TYPENAME(T) \
|
|
|
|
const char* typeName() const { return #T; } \
|
|
|
|
size_t typeSize() const { return sizeof(*this); }
|
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
} // namespace detail
|
2013-05-18 07:52:53 +00:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class RefCounted : public detail::RefCounted<T, detail::NonAtomicRefCount>
|
|
|
|
{
|
2014-07-11 02:10:17 +00:00
|
|
|
public:
|
|
|
|
~RefCounted()
|
|
|
|
{
|
|
|
|
static_assert(IsBaseOf<RefCounted, T>::value,
|
|
|
|
"T must derive from RefCounted<T>");
|
|
|
|
}
|
2013-05-18 07:52:53 +00:00
|
|
|
};
|
|
|
|
|
2014-05-01 18:33:20 +00:00
|
|
|
namespace external {
|
|
|
|
|
2013-05-18 07:52:53 +00:00
|
|
|
/**
|
|
|
|
* AtomicRefCounted<T> is like RefCounted<T>, with an atomically updated
|
|
|
|
* reference counter.
|
2014-05-01 18:33:20 +00:00
|
|
|
*
|
|
|
|
* NOTE: Please do not use this class, use NS_INLINE_DECL_THREADSAFE_REFCOUNTING
|
|
|
|
* instead.
|
2013-05-18 07:52:53 +00:00
|
|
|
*/
|
|
|
|
template<typename T>
|
2014-07-11 02:10:17 +00:00
|
|
|
class AtomicRefCounted :
|
|
|
|
public mozilla::detail::RefCounted<T, mozilla::detail::AtomicRefCount>
|
2013-05-18 07:52:53 +00:00
|
|
|
{
|
2014-07-11 02:10:17 +00:00
|
|
|
public:
|
|
|
|
~AtomicRefCounted()
|
|
|
|
{
|
|
|
|
static_assert(IsBaseOf<AtomicRefCounted, T>::value,
|
|
|
|
"T must derive from AtomicRefCounted<T>");
|
|
|
|
}
|
2011-06-17 03:40:20 +00:00
|
|
|
};
|
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
} // namespace external
|
2014-05-01 18:33:20 +00:00
|
|
|
|
2011-06-17 03:40:20 +00:00
|
|
|
/**
|
|
|
|
* RefPtr points to a refcounted thing that has AddRef and Release
|
|
|
|
* methods to increase/decrease the refcount, respectively. After a
|
|
|
|
* RefPtr<T> is assigned a T*, the T* can be used through the RefPtr
|
|
|
|
* as if it were a T*.
|
|
|
|
*
|
|
|
|
* A RefPtr can forget its underlying T*, which results in the T*
|
|
|
|
* being wrapped in a temporary object until the T* is either
|
|
|
|
* re-adopted from or released by the temporary.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
class RefPtr
|
|
|
|
{
|
2014-07-11 02:10:17 +00:00
|
|
|
// To allow them to use unref()
|
|
|
|
friend class TemporaryRef<T>;
|
|
|
|
friend class OutParamRef<T>;
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
struct DontRef {};
|
2011-06-22 04:44:00 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
public:
|
|
|
|
RefPtr() : mPtr(0) {}
|
|
|
|
RefPtr(const RefPtr& aOther) : mPtr(ref(aOther.mPtr)) {}
|
|
|
|
MOZ_IMPLICIT RefPtr(const TemporaryRef<T>& aOther) : mPtr(aOther.drop()) {}
|
|
|
|
MOZ_IMPLICIT RefPtr(T* aVal) : mPtr(ref(aVal)) {}
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
template<typename U>
|
|
|
|
RefPtr(const RefPtr<U>& aOther) : mPtr(ref(aOther.get())) {}
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
~RefPtr() { unref(mPtr); }
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
RefPtr& operator=(const RefPtr& aOther)
|
|
|
|
{
|
|
|
|
assign(ref(aOther.mPtr));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
RefPtr& operator=(const TemporaryRef<T>& aOther)
|
|
|
|
{
|
|
|
|
assign(aOther.drop());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
RefPtr& operator=(T* aVal)
|
|
|
|
{
|
|
|
|
assign(ref(aVal));
|
|
|
|
return *this;
|
|
|
|
}
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
template<typename U>
|
|
|
|
RefPtr& operator=(const RefPtr<U>& aOther)
|
|
|
|
{
|
|
|
|
assign(ref(aOther.get()));
|
|
|
|
return *this;
|
|
|
|
}
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
TemporaryRef<T> forget()
|
|
|
|
{
|
|
|
|
T* tmp = mPtr;
|
|
|
|
mPtr = nullptr;
|
|
|
|
return TemporaryRef<T>(tmp, DontRef());
|
|
|
|
}
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
T* get() const { return mPtr; }
|
|
|
|
operator T*() const { return mPtr; }
|
|
|
|
T* operator->() const { return mPtr; }
|
|
|
|
T& operator*() const { return *mPtr; }
|
|
|
|
template<typename U>
|
|
|
|
operator TemporaryRef<U>() { return TemporaryRef<U>(mPtr); }
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
private:
|
|
|
|
void assign(T* aVal)
|
|
|
|
{
|
|
|
|
unref(mPtr);
|
|
|
|
mPtr = aVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* mPtr;
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
static MOZ_ALWAYS_INLINE T* ref(T* aVal)
|
|
|
|
{
|
|
|
|
if (aVal) {
|
|
|
|
aVal->AddRef();
|
2011-06-17 03:40:20 +00:00
|
|
|
}
|
2014-07-11 02:10:17 +00:00
|
|
|
return aVal;
|
|
|
|
}
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
static MOZ_ALWAYS_INLINE void unref(T* aVal)
|
|
|
|
{
|
|
|
|
if (aVal) {
|
|
|
|
aVal->Release();
|
2011-06-17 03:40:20 +00:00
|
|
|
}
|
2014-07-11 02:10:17 +00:00
|
|
|
}
|
2011-06-17 03:40:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TemporaryRef<T> represents an object that holds a temporary
|
|
|
|
* reference to a T. TemporaryRef objects can't be manually ref'd or
|
|
|
|
* unref'd (being temporaries, not lvalues), so can only relinquish
|
|
|
|
* references to other objects, or unref on destruction.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
class TemporaryRef
|
|
|
|
{
|
2014-07-11 02:10:17 +00:00
|
|
|
// To allow it to construct TemporaryRef from a bare T*
|
|
|
|
friend class RefPtr<T>;
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
typedef typename RefPtr<T>::DontRef DontRef;
|
2011-06-22 04:44:00 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
public:
|
|
|
|
MOZ_IMPLICIT TemporaryRef(T* aVal) : mPtr(RefPtr<T>::ref(aVal)) {}
|
|
|
|
TemporaryRef(const TemporaryRef& aOther) : mPtr(aOther.drop()) {}
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
template<typename U>
|
|
|
|
TemporaryRef(const TemporaryRef<U>& aOther) : mPtr(aOther.drop()) {}
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
~TemporaryRef() { RefPtr<T>::unref(mPtr); }
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
T* drop() const
|
|
|
|
{
|
|
|
|
T* tmp = mPtr;
|
|
|
|
mPtr = nullptr;
|
|
|
|
return tmp;
|
|
|
|
}
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
private:
|
|
|
|
TemporaryRef(T* aVal, const DontRef&) : mPtr(aVal) {}
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
mutable T* mPtr;
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
TemporaryRef() MOZ_DELETE;
|
|
|
|
void operator=(const TemporaryRef&) MOZ_DELETE;
|
2011-06-17 03:40:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OutParamRef is a wrapper that tracks a refcounted pointer passed as
|
2011-06-22 04:44:00 +00:00
|
|
|
* an outparam argument to a function. OutParamRef implements COM T**
|
|
|
|
* outparam semantics: this requires the callee to AddRef() the T*
|
|
|
|
* returned through the T** outparam on behalf of the caller. This
|
|
|
|
* means the caller (through OutParamRef) must Release() the old
|
|
|
|
* object contained in the tracked RefPtr. It's OK if the callee
|
|
|
|
* returns the same T* passed to it through the T** outparam, as long
|
|
|
|
* as the callee obeys the COM discipline.
|
2011-06-17 03:40:20 +00:00
|
|
|
*
|
|
|
|
* Prefer returning TemporaryRef<T> from functions over creating T**
|
|
|
|
* outparams and passing OutParamRef<T> to T**. Prefer RefPtr<T>*
|
|
|
|
* outparams over T** outparams.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
class OutParamRef
|
|
|
|
{
|
2014-07-11 02:10:17 +00:00
|
|
|
friend OutParamRef byRef<T>(RefPtr<T>&);
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
public:
|
|
|
|
~OutParamRef()
|
|
|
|
{
|
|
|
|
RefPtr<T>::unref(mRefPtr.mPtr);
|
|
|
|
mRefPtr.mPtr = mTmp;
|
|
|
|
}
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
operator T**() { return &mTmp; }
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
private:
|
|
|
|
explicit OutParamRef(RefPtr<T>& p) : mRefPtr(p), mTmp(p.get()) {}
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
RefPtr<T>& mRefPtr;
|
|
|
|
T* mTmp;
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
OutParamRef() MOZ_DELETE;
|
|
|
|
OutParamRef& operator=(const OutParamRef&) MOZ_DELETE;
|
2011-06-17 03:40:20 +00:00
|
|
|
};
|
|
|
|
|
2011-06-22 04:44:00 +00:00
|
|
|
/**
|
|
|
|
* byRef cooperates with OutParamRef to implement COM outparam semantics.
|
|
|
|
*/
|
2011-06-17 03:40:20 +00:00
|
|
|
template<typename T>
|
|
|
|
OutParamRef<T>
|
2014-07-11 02:10:17 +00:00
|
|
|
byRef(RefPtr<T>& aPtr)
|
2011-06-17 03:40:20 +00:00
|
|
|
{
|
2014-07-11 02:10:17 +00:00
|
|
|
return OutParamRef<T>(aPtr);
|
2011-06-17 03:40:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
|
|
|
// Command line that builds these tests
|
|
|
|
//
|
|
|
|
// cp RefPtr.h test.cc && g++ -g -Wall -pedantic -DDEBUG -o test test.cc && ./test
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
struct Foo : public RefCounted<Foo>
|
|
|
|
{
|
2014-02-21 02:33:49 +00:00
|
|
|
MOZ_DECLARE_REFCOUNTED_TYPENAME(Foo)
|
2014-07-11 02:10:17 +00:00
|
|
|
Foo() : mDead(false) {}
|
|
|
|
~Foo()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mDead);
|
|
|
|
mDead = true;
|
|
|
|
sNumDestroyed++;
|
2012-06-04 03:36:43 +00:00
|
|
|
}
|
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
bool mDead;
|
|
|
|
static int sNumDestroyed;
|
2011-06-17 03:40:20 +00:00
|
|
|
};
|
2014-07-11 02:10:17 +00:00
|
|
|
int Foo::sNumDestroyed;
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
struct Bar : public Foo {};
|
2011-06-17 03:40:20 +00:00
|
|
|
|
|
|
|
TemporaryRef<Foo>
|
|
|
|
NewFoo()
|
|
|
|
{
|
2012-06-04 03:36:43 +00:00
|
|
|
return RefPtr<Foo>(new Foo());
|
2011-06-17 03:40:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TemporaryRef<Foo>
|
|
|
|
NewBar()
|
|
|
|
{
|
2012-06-04 03:36:43 +00:00
|
|
|
return new Bar();
|
2011-06-17 03:40:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GetNewFoo(Foo** f)
|
|
|
|
{
|
2012-06-04 03:36:43 +00:00
|
|
|
*f = new Bar();
|
|
|
|
// Kids, don't try this at home
|
|
|
|
(*f)->AddRef();
|
2011-06-17 03:40:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GetPassedFoo(Foo** f)
|
2011-06-22 04:44:00 +00:00
|
|
|
{
|
2012-06-04 03:36:43 +00:00
|
|
|
// Kids, don't try this at home
|
|
|
|
(*f)->AddRef();
|
2011-06-22 04:44:00 +00:00
|
|
|
}
|
2011-06-17 03:40:20 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
GetNewFoo(RefPtr<Foo>* f)
|
|
|
|
{
|
2012-06-04 03:36:43 +00:00
|
|
|
*f = new Bar();
|
2011-06-17 03:40:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GetPassedFoo(RefPtr<Foo>* f)
|
|
|
|
{}
|
|
|
|
|
2011-06-22 04:44:00 +00:00
|
|
|
TemporaryRef<Foo>
|
|
|
|
GetNullFoo()
|
|
|
|
{
|
2012-06-04 03:36:43 +00:00
|
|
|
return 0;
|
2011-06-22 04:44:00 +00:00
|
|
|
}
|
|
|
|
|
2011-06-17 03:40:20 +00:00
|
|
|
int
|
|
|
|
main(int argc, char** argv)
|
|
|
|
{
|
2012-06-04 03:36:43 +00:00
|
|
|
// This should blow up
|
2011-06-17 03:40:20 +00:00
|
|
|
// Foo* f = new Foo(); delete f;
|
|
|
|
|
2014-07-11 02:10:17 +00:00
|
|
|
MOZ_ASSERT(0 == Foo::sNumDestroyed);
|
2012-06-04 03:36:43 +00:00
|
|
|
{
|
|
|
|
RefPtr<Foo> f = new Foo();
|
|
|
|
MOZ_ASSERT(f->refCount() == 1);
|
|
|
|
}
|
2014-07-11 02:10:17 +00:00
|
|
|
MOZ_ASSERT(1 == Foo::sNumDestroyed);
|
2012-06-04 03:36:43 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
RefPtr<Foo> f1 = NewFoo();
|
|
|
|
RefPtr<Foo> f2(NewFoo());
|
2014-07-11 02:10:17 +00:00
|
|
|
MOZ_ASSERT(1 == Foo::sNumDestroyed);
|
2012-06-04 03:36:43 +00:00
|
|
|
}
|
2014-07-11 02:10:17 +00:00
|
|
|
MOZ_ASSERT(3 == Foo::sNumDestroyed);
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2012-06-04 03:36:43 +00:00
|
|
|
{
|
|
|
|
RefPtr<Foo> b = NewBar();
|
2014-07-11 02:10:17 +00:00
|
|
|
MOZ_ASSERT(3 == Foo::sNumDestroyed);
|
2012-06-04 03:36:43 +00:00
|
|
|
}
|
2014-07-11 02:10:17 +00:00
|
|
|
MOZ_ASSERT(4 == Foo::sNumDestroyed);
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2012-06-04 03:36:43 +00:00
|
|
|
{
|
|
|
|
RefPtr<Foo> f1;
|
2011-06-17 03:40:20 +00:00
|
|
|
{
|
2012-06-04 03:36:43 +00:00
|
|
|
f1 = new Foo();
|
|
|
|
RefPtr<Foo> f2(f1);
|
|
|
|
RefPtr<Foo> f3 = f2;
|
2014-07-11 02:10:17 +00:00
|
|
|
MOZ_ASSERT(4 == Foo::sNumDestroyed);
|
2011-06-17 03:40:20 +00:00
|
|
|
}
|
2014-07-11 02:10:17 +00:00
|
|
|
MOZ_ASSERT(4 == Foo::sNumDestroyed);
|
2012-06-04 03:36:43 +00:00
|
|
|
}
|
2014-07-11 02:10:17 +00:00
|
|
|
MOZ_ASSERT(5 == Foo::sNumDestroyed);
|
2012-06-04 03:36:43 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
RefPtr<Foo> f = new Foo();
|
|
|
|
f.forget();
|
2014-07-11 02:10:17 +00:00
|
|
|
MOZ_ASSERT(6 == Foo::sNumDestroyed);
|
2012-06-04 03:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
RefPtr<Foo> f = new Foo();
|
|
|
|
GetNewFoo(byRef(f));
|
2014-07-11 02:10:17 +00:00
|
|
|
MOZ_ASSERT(7 == Foo::sNumDestroyed);
|
2012-06-04 03:36:43 +00:00
|
|
|
}
|
2014-07-11 02:10:17 +00:00
|
|
|
MOZ_ASSERT(8 == Foo::sNumDestroyed);
|
2012-06-04 03:36:43 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
RefPtr<Foo> f = new Foo();
|
|
|
|
GetPassedFoo(byRef(f));
|
2014-07-11 02:10:17 +00:00
|
|
|
MOZ_ASSERT(8 == Foo::sNumDestroyed);
|
2012-06-04 03:36:43 +00:00
|
|
|
}
|
2014-07-11 02:10:17 +00:00
|
|
|
MOZ_ASSERT(9 == Foo::sNumDestroyed);
|
2012-06-04 03:36:43 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
RefPtr<Foo> f = new Foo();
|
|
|
|
GetNewFoo(&f);
|
2014-07-11 02:10:17 +00:00
|
|
|
MOZ_ASSERT(10 == Foo::sNumDestroyed);
|
2012-06-04 03:36:43 +00:00
|
|
|
}
|
2014-07-11 02:10:17 +00:00
|
|
|
MOZ_ASSERT(11 == Foo::sNumDestroyed);
|
2012-06-04 03:36:43 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
RefPtr<Foo> f = new Foo();
|
|
|
|
GetPassedFoo(&f);
|
2014-07-11 02:10:17 +00:00
|
|
|
MOZ_ASSERT(11 == Foo::sNumDestroyed);
|
2012-06-04 03:36:43 +00:00
|
|
|
}
|
2014-07-11 02:10:17 +00:00
|
|
|
MOZ_ASSERT(12 == Foo::sNumDestroyed);
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2012-06-04 03:36:43 +00:00
|
|
|
{
|
|
|
|
RefPtr<Foo> f1 = new Bar();
|
|
|
|
}
|
2014-07-11 02:10:17 +00:00
|
|
|
MOZ_ASSERT(13 == Foo::sNumDestroyed);
|
2011-06-17 03:40:20 +00:00
|
|
|
|
2012-06-04 03:36:43 +00:00
|
|
|
{
|
|
|
|
RefPtr<Foo> f = GetNullFoo();
|
2014-07-11 02:10:17 +00:00
|
|
|
MOZ_ASSERT(13 == Foo::sNumDestroyed);
|
2012-06-04 03:36:43 +00:00
|
|
|
}
|
2014-07-11 02:10:17 +00:00
|
|
|
MOZ_ASSERT(13 == Foo::sNumDestroyed);
|
2011-06-22 04:44:00 +00:00
|
|
|
|
2012-06-04 03:36:43 +00:00
|
|
|
return 0;
|
2011-06-17 03:40:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2013-07-24 07:41:39 +00:00
|
|
|
|
|
|
|
#endif /* mozilla_RefPtr_h */
|