#include "config.h"
#include "GCController.h"
#include "CommonVM.h"
#include <runtime/VM.h>
#include <runtime/JSLock.h>
#include <heap/Heap.h>
#include <wtf/StdLibExtras.h>
#include <wtf/FastMalloc.h>
#include <wtf/NeverDestroyed.h>
using namespace JSC;
namespace WebCore {
static void collect()
{
JSLockHolder lock(commonVM());
commonVM().heap.collectNow(Async, CollectionScope::Full);
}
GCController& GCController::singleton()
{
static NeverDestroyed<GCController> controller;
return controller;
}
GCController::GCController()
: m_GCTimer(*this, &GCController::gcTimerFired)
{
}
void GCController::garbageCollectSoon()
{
#if USE(CF) || USE(GLIB)
JSLockHolder lock(commonVM());
commonVM().heap.reportAbandonedObjectGraph();
#else
garbageCollectOnNextRunLoop();
#endif
}
void GCController::garbageCollectOnNextRunLoop()
{
if (!m_GCTimer.isActive())
m_GCTimer.startOneShot(0_s);
}
void GCController::gcTimerFired()
{
collect();
}
void GCController::garbageCollectNow()
{
JSLockHolder lock(commonVM());
if (!commonVM().heap.isCurrentThreadBusy()) {
commonVM().heap.collectNow(Sync, CollectionScope::Full);
WTF::releaseFastMallocFreeMemory();
}
}
void GCController::garbageCollectNowIfNotDoneRecently()
{
#if USE(CF) || USE(GLIB)
JSLockHolder lock(commonVM());
if (!commonVM().heap.isCurrentThreadBusy())
commonVM().heap.collectNowFullIfNotDoneRecently(Async);
#else
garbageCollectSoon();
#endif
}
void GCController::garbageCollectOnAlternateThreadForDebugging(bool waitUntilDone)
{
RefPtr<Thread> thread = Thread::create("WebCore: GCController", &collect);
if (waitUntilDone) {
thread->waitForCompletion();
return;
}
thread->detach();
}
void GCController::setJavaScriptGarbageCollectorTimerEnabled(bool enable)
{
commonVM().heap.setGarbageCollectionTimerEnabled(enable);
}
void GCController::deleteAllCode(DeleteAllCodeEffort effort)
{
JSLockHolder lock(commonVM());
commonVM().deleteAllCode(effort);
}
void GCController::deleteAllLinkedCode(DeleteAllCodeEffort effort)
{
JSLockHolder lock(commonVM());
commonVM().deleteAllLinkedCode(effort);
}
}