#include "config.h"
#include "WeakSet.h"
#include "Heap.h"
#include "VM.h"
namespace JSC {
WeakSet::~WeakSet()
{
WeakBlock* next = 0;
for (WeakBlock* block = m_blocks.head(); block; block = next) {
next = block->next();
heap()->blockAllocator().deallocate(WeakBlock::destroy(block));
}
m_blocks.clear();
}
void WeakSet::sweep()
{
for (WeakBlock* block = m_blocks.head(); block; block = block->next())
block->sweep();
resetAllocator();
}
WeakBlock::FreeCell* WeakSet::findAllocator()
{
if (WeakBlock::FreeCell* allocator = tryFindAllocator())
return allocator;
return addAllocator();
}
WeakBlock::FreeCell* WeakSet::tryFindAllocator()
{
while (m_nextAllocator) {
WeakBlock* block = m_nextAllocator;
m_nextAllocator = m_nextAllocator->next();
WeakBlock::SweepResult sweepResult = block->takeSweepResult();
if (sweepResult.freeList)
return sweepResult.freeList;
}
return 0;
}
WeakBlock::FreeCell* WeakSet::addAllocator()
{
WeakBlock* block = WeakBlock::create(heap()->blockAllocator().allocate<WeakBlock>());
heap()->didAllocate(WeakBlock::blockSize);
m_blocks.append(block);
WeakBlock::SweepResult sweepResult = block->takeSweepResult();
ASSERT(!sweepResult.isNull() && sweepResult.freeList);
return sweepResult.freeList;
}
void WeakSet::removeAllocator(WeakBlock* block)
{
m_blocks.remove(block);
heap()->blockAllocator().deallocate(WeakBlock::destroy(block));
}
}