#pragma once
#include <wtf/Assertions.h>
#include <wtf/FastMalloc.h>
#include <wtf/MainThread.h>
#include <wtf/Noncopyable.h>
namespace WTF {
#if ASSERT_ENABLED || ENABLE(SECURITY_ASSERTIONS)
#define CHECK_REF_COUNTED_LIFECYCLE 1
#else
#define CHECK_REF_COUNTED_LIFECYCLE 0
#endif
class RefCountedBase {
public:
void ref() const
{
applyRefDerefThreadingCheck();
#if CHECK_REF_COUNTED_LIFECYCLE
ASSERT_WITH_SECURITY_IMPLICATION(!m_deletionHasBegun);
ASSERT(!m_adoptionIsRequired);
#endif
++m_refCount;
}
bool hasOneRef() const
{
#if CHECK_REF_COUNTED_LIFECYCLE
ASSERT(!m_deletionHasBegun);
#endif
return m_refCount == 1;
}
unsigned refCount() const
{
return m_refCount;
}
void relaxAdoptionRequirement()
{
#if CHECK_REF_COUNTED_LIFECYCLE
ASSERT_WITH_SECURITY_IMPLICATION(!m_deletionHasBegun);
ASSERT(m_adoptionIsRequired);
m_adoptionIsRequired = false;
#endif
}
void disableThreadingChecks()
{
#if ASSERT_ENABLED
m_areThreadingChecksEnabled = false;
#endif
}
static void enableThreadingChecksGlobally()
{
#if ASSERT_ENABLED
areThreadingChecksEnabledGlobally = true;
#endif
}
protected:
RefCountedBase()
: m_refCount(1)
#if ASSERT_ENABLED
, m_isOwnedByMainThread(isMainThread())
#endif
#if CHECK_REF_COUNTED_LIFECYCLE
, m_deletionHasBegun(false)
, m_adoptionIsRequired(true)
#endif
{
}
void applyRefDerefThreadingCheck() const
{
#if ASSERT_ENABLED
if (hasOneRef()) {
m_isOwnedByMainThread = isMainThread();
} else if (areThreadingChecksEnabledGlobally && m_areThreadingChecksEnabled) {
ASSERT_WITH_MESSAGE(m_isOwnedByMainThread == isMainThread(), "Unsafe to ref/deref from different threads");
}
#endif
}
~RefCountedBase()
{
#if CHECK_REF_COUNTED_LIFECYCLE
ASSERT(m_deletionHasBegun);
ASSERT(!m_adoptionIsRequired);
#endif
}
bool derefBase() const
{
applyRefDerefThreadingCheck();
#if CHECK_REF_COUNTED_LIFECYCLE
ASSERT_WITH_SECURITY_IMPLICATION(!m_deletionHasBegun);
ASSERT(!m_adoptionIsRequired);
#endif
ASSERT(m_refCount);
unsigned tempRefCount = m_refCount - 1;
if (!tempRefCount) {
#if CHECK_REF_COUNTED_LIFECYCLE
m_deletionHasBegun = true;
#endif
return true;
}
m_refCount = tempRefCount;
return false;
}
#if CHECK_REF_COUNTED_LIFECYCLE
bool deletionHasBegun() const
{
return m_deletionHasBegun;
}
#endif
private:
#if CHECK_REF_COUNTED_LIFECYCLE
friend void adopted(RefCountedBase*);
#endif
mutable unsigned m_refCount;
#if ASSERT_ENABLED
mutable bool m_isOwnedByMainThread;
bool m_areThreadingChecksEnabled { true };
#endif
WTF_EXPORT_PRIVATE static bool areThreadingChecksEnabledGlobally;
#if CHECK_REF_COUNTED_LIFECYCLE
mutable bool m_deletionHasBegun;
mutable bool m_adoptionIsRequired;
#endif
};
#if CHECK_REF_COUNTED_LIFECYCLE
inline void adopted(RefCountedBase* object)
{
if (!object)
return;
ASSERT_WITH_SECURITY_IMPLICATION(!object->m_deletionHasBegun);
object->m_adoptionIsRequired = false;
}
#endif
template<typename T, typename Deleter = std::default_delete<T>> class RefCounted : public RefCountedBase {
WTF_MAKE_NONCOPYABLE(RefCounted); WTF_MAKE_FAST_ALLOCATED;
public:
void deref() const
{
if (derefBase())
Deleter()(const_cast<T*>(static_cast<const T*>(this)));
}
protected:
RefCounted() { }
~RefCounted()
{
}
};
}
using WTF::RefCounted;