#include "config.h"
#include "Subspace.h"
#include "AlignedMemoryAllocator.h"
#include "HeapCellType.h"
#include "MarkedSpaceInlines.h"
#include "ParallelSourceAdapter.h"
#include "SubspaceInlines.h"
namespace JSC {
Subspace::Subspace(CString name, Heap& heap)
: m_space(heap.objectSpace())
, m_name(name)
{
}
void Subspace::initialize(HeapCellType* heapCellType, AlignedMemoryAllocator* alignedMemoryAllocator)
{
m_heapCellType = heapCellType;
m_alignedMemoryAllocator = alignedMemoryAllocator;
m_directoryForEmptyAllocation = m_alignedMemoryAllocator->firstDirectory();
Heap& heap = m_space.heap();
heap.objectSpace().m_subspaces.append(this);
m_alignedMemoryAllocator->registerSubspace(this);
}
Subspace::~Subspace()
{
}
void Subspace::finishSweep(MarkedBlock::Handle& block, FreeList* freeList)
{
m_heapCellType->finishSweep(block, freeList);
}
void Subspace::destroy(VM& vm, JSCell* cell)
{
m_heapCellType->destroy(vm, cell);
}
void Subspace::prepareForAllocation()
{
forEachDirectory(
[&] (BlockDirectory& directory) {
directory.prepareForAllocation();
});
m_directoryForEmptyAllocation = m_alignedMemoryAllocator->firstDirectory();
}
MarkedBlock::Handle* Subspace::findEmptyBlockToSteal()
{
for (; m_directoryForEmptyAllocation; m_directoryForEmptyAllocation = m_directoryForEmptyAllocation->nextDirectoryInAlignedMemoryAllocator()) {
if (MarkedBlock::Handle* block = m_directoryForEmptyAllocation->findEmptyBlockToSteal())
return block;
}
return nullptr;
}
Ref<SharedTask<BlockDirectory*()>> Subspace::parallelDirectorySource()
{
class Task final : public SharedTask<BlockDirectory*()> {
public:
Task(BlockDirectory* directory)
: m_directory(directory)
{
}
BlockDirectory* run() final
{
auto locker = holdLock(m_lock);
BlockDirectory* result = m_directory;
if (result)
m_directory = result->nextDirectoryInSubspace();
return result;
}
private:
BlockDirectory* m_directory;
Lock m_lock;
};
return adoptRef(*new Task(m_firstDirectory));
}
Ref<SharedTask<MarkedBlock::Handle*()>> Subspace::parallelNotEmptyMarkedBlockSource()
{
return createParallelSourceAdapter<BlockDirectory*, MarkedBlock::Handle*>(
parallelDirectorySource(),
[] (BlockDirectory* directory) -> RefPtr<SharedTask<MarkedBlock::Handle*()>> {
if (!directory)
return nullptr;
return directory->parallelNotEmptyBlockSource();
});
}
void Subspace::sweepBlocks()
{
forEachDirectory(
[&] (BlockDirectory& directory) {
directory.sweep();
});
}
void Subspace::didResizeBits(unsigned)
{
}
void Subspace::didRemoveBlock(unsigned)
{
}
void Subspace::didBeginSweepingToFreeList(MarkedBlock::Handle*)
{
}
}