#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 constexpr (validateDFGDoesGC)
m_heap.verifyCanGC();
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:
#if ASSERT_ENABLED
DisallowGC() = default;
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;
#else
ALWAYS_INLINE DisallowGC() { } ALWAYS_INLINE static void initialize() { }
#endif // ASSERT_ENABLED
friend class DisallowScope<DisallowGC>;
};
}