#include "config.h"
#include "WeakBlock.h"
#include "Heap.h"
#include "HeapRootVisitor.h"
#include "JSObject.h"
#include "JSCInlines.h"
#include "Structure.h"
namespace JSC {
WeakBlock* WeakBlock::create(MarkedBlock& markedBlock)
{
return new (NotNull, fastMalloc(blockSize)) WeakBlock(markedBlock);
}
void WeakBlock::destroy(WeakBlock* block)
{
block->~WeakBlock();
fastFree(block);
}
WeakBlock::WeakBlock(MarkedBlock& markedBlock)
: DoublyLinkedListNode<WeakBlock>()
, m_markedBlock(&markedBlock)
{
for (size_t i = 0; i < weakImplCount(); ++i) {
WeakImpl* weakImpl = &weakImpls()[i];
new (NotNull, weakImpl) WeakImpl;
addToFreeList(&m_sweepResult.freeList, weakImpl);
}
ASSERT(isEmpty());
}
void WeakBlock::lastChanceToFinalize()
{
for (size_t i = 0; i < weakImplCount(); ++i) {
WeakImpl* weakImpl = &weakImpls()[i];
if (weakImpl->state() >= WeakImpl::Finalized)
continue;
weakImpl->setState(WeakImpl::Dead);
finalize(weakImpl);
}
}
void WeakBlock::sweep()
{
if (isEmpty())
return;
SweepResult sweepResult;
for (size_t i = 0; i < weakImplCount(); ++i) {
WeakImpl* weakImpl = &weakImpls()[i];
if (weakImpl->state() == WeakImpl::Dead)
finalize(weakImpl);
if (weakImpl->state() == WeakImpl::Deallocated)
addToFreeList(&sweepResult.freeList, weakImpl);
else {
sweepResult.blockIsFree = false;
if (weakImpl->state() == WeakImpl::Live)
sweepResult.blockIsLogicallyEmpty = false;
}
}
m_sweepResult = sweepResult;
ASSERT(!m_sweepResult.isNull());
}
void WeakBlock::visit(HeapRootVisitor& heapRootVisitor)
{
if (isEmpty())
return;
ASSERT(m_markedBlock);
if (m_markedBlock->isAllocated())
return;
SlotVisitor& visitor = heapRootVisitor.visitor();
for (size_t i = 0; i < weakImplCount(); ++i) {
WeakImpl* weakImpl = &weakImpls()[i];
if (weakImpl->state() != WeakImpl::Live)
continue;
const JSValue& jsValue = weakImpl->jsValue();
if (m_markedBlock->isMarkedOrNewlyAllocated(jsValue.asCell()))
continue;
WeakHandleOwner* weakHandleOwner = weakImpl->weakHandleOwner();
if (!weakHandleOwner)
continue;
if (!weakHandleOwner->isReachableFromOpaqueRoots(Handle<Unknown>::wrapSlot(&const_cast<JSValue&>(jsValue)), weakImpl->context(), visitor))
continue;
heapRootVisitor.visit(&const_cast<JSValue&>(jsValue));
}
}
void WeakBlock::reap()
{
if (isEmpty())
return;
ASSERT(m_markedBlock);
if (m_markedBlock->isAllocated())
return;
for (size_t i = 0; i < weakImplCount(); ++i) {
WeakImpl* weakImpl = &weakImpls()[i];
if (weakImpl->state() > WeakImpl::Dead)
continue;
if (m_markedBlock->isMarkedOrNewlyAllocated(weakImpl->jsValue().asCell())) {
ASSERT(weakImpl->state() == WeakImpl::Live);
continue;
}
weakImpl->setState(WeakImpl::Dead);
}
}
}