#pragma once
#include "DisallowScope.h"
#include "Heap.h"
#include <wtf/NeverDestroyed.h>
#include <wtf/ThreadSpecific.h>
namespace JSC {
class DeferGC {
WTF_MAKE_NONCOPYABLE(DeferGC);
WTF_FORBID_HEAP_ALLOCATION;
public:
DeferGC(Heap& heap)
: m_heap(heap)
{
m_heap.incrementDeferralDepth();
}
~DeferGC()
{
if (validateDFGDoesGC)
RELEASE_ASSERT(m_heap.expectDoesGC());
m_heap.decrementDeferralDepthAndGCIfNeeded();
}
private:
Heap& m_heap;
};
class DeferGCForAWhile {
WTF_MAKE_NONCOPYABLE(DeferGCForAWhile);
WTF_FORBID_HEAP_ALLOCATION;
public:
DeferGCForAWhile(Heap& heap)
: m_heap(heap)
{
m_heap.incrementDeferralDepth();
}
~DeferGCForAWhile()
{
m_heap.decrementDeferralDepth();
}
private:
Heap& m_heap;
};
class DisallowGC : public DisallowScope<DisallowGC> {
WTF_MAKE_NONCOPYABLE(DisallowGC);
WTF_FORBID_HEAP_ALLOCATION;
typedef DisallowScope<DisallowGC> Base;
public:
#ifdef NDEBUG
ALWAYS_INLINE DisallowGC(bool = false) { }
ALWAYS_INLINE static void initialize() { }
#else // not NDEBUG
DisallowGC(bool enabled = true)
: Base(enabled)
{ }
static void initialize()
{
s_scopeReentryCount.construct();
}
private:
static unsigned scopeReentryCount()
{
return *s_scopeReentryCount.get();
}
static void setScopeReentryCount(unsigned value)
{
*s_scopeReentryCount.get() = value;
}
JS_EXPORT_PRIVATE static LazyNeverDestroyed<ThreadSpecific<unsigned, WTF::CanBeGCThread::True>> s_scopeReentryCount;
#endif // NDEBUG
friend class DisallowScope<DisallowGC>;
};
}