#include "config.h"
#include "MarkedBlock.h"
#include "IncrementalSweeper.h"
#include "JSCell.h"
#include "JSDestructibleObject.h"
#include "JSCInlines.h"
namespace JSC {
MarkedBlock* MarkedBlock::create(MarkedAllocator* allocator, size_t capacity, size_t cellSize, bool needsDestruction)
{
return new (NotNull, fastAlignedMalloc(blockSize, capacity)) MarkedBlock(allocator, capacity, cellSize, needsDestruction);
}
void MarkedBlock::destroy(MarkedBlock* block)
{
block->~MarkedBlock();
fastAlignedFree(block);
}
MarkedBlock::MarkedBlock(MarkedAllocator* allocator, size_t capacity, size_t cellSize, bool needsDestruction)
: DoublyLinkedListNode<MarkedBlock>()
, m_atomsPerCell((cellSize + atomSize - 1) / atomSize)
, m_endAtom((allocator->cellSize() ? atomsPerBlock - m_atomsPerCell : firstAtom()) + 1)
, m_capacity(capacity)
, m_needsDestruction(needsDestruction)
, m_allocator(allocator)
, m_state(New) , m_weakSet(allocator->heap()->vm(), *this)
{
ASSERT(allocator);
HEAP_LOG_BLOCK_STATE_TRANSITION(this);
}
inline void MarkedBlock::callDestructor(JSCell* cell)
{
if (cell->isZapped())
return;
ASSERT(cell->structureID());
if (cell->inlineTypeFlags() & StructureIsImmortal)
cell->structure(*vm())->classInfo()->methodTable.destroy(cell);
else
jsCast<JSDestructibleObject*>(cell)->classInfo()->methodTable.destroy(cell);
cell->zap();
}
template<MarkedBlock::BlockState blockState, MarkedBlock::SweepMode sweepMode, bool callDestructors>
MarkedBlock::FreeList MarkedBlock::specializedSweep()
{
ASSERT(blockState != Allocated && blockState != FreeListed);
ASSERT(!(!callDestructors && sweepMode == SweepOnly));
SamplingRegion samplingRegion((!callDestructors && blockState != New) ? "Calling destructors" : "sweeping");
FreeCell* head = 0;
size_t count = 0;
for (size_t i = firstAtom(); i < m_endAtom; i += m_atomsPerCell) {
if (blockState == Marked && (m_marks.get(i) || (m_newlyAllocated && m_newlyAllocated->get(i))))
continue;
JSCell* cell = reinterpret_cast_ptr<JSCell*>(&atoms()[i]);
if (callDestructors && blockState != New)
callDestructor(cell);
if (sweepMode == SweepToFreeList) {
FreeCell* freeCell = reinterpret_cast<FreeCell*>(cell);
freeCell->next = head;
head = freeCell;
++count;
}
}
if (sweepMode == SweepToFreeList && m_newlyAllocated)
m_newlyAllocated = nullptr;
m_state = ((sweepMode == SweepToFreeList) ? FreeListed : Marked);
return FreeList(head, count * cellSize());
}
MarkedBlock::FreeList MarkedBlock::sweep(SweepMode sweepMode)
{
HEAP_LOG_BLOCK_STATE_TRANSITION(this);
m_weakSet.sweep();
if (sweepMode == SweepOnly && !m_needsDestruction)
return FreeList();
if (m_needsDestruction)
return sweepHelper<true>(sweepMode);
return sweepHelper<false>(sweepMode);
}
template<bool callDestructors>
MarkedBlock::FreeList MarkedBlock::sweepHelper(SweepMode sweepMode)
{
switch (m_state) {
case New:
ASSERT(sweepMode == SweepToFreeList);
return specializedSweep<New, SweepToFreeList, callDestructors>();
case FreeListed:
ASSERT(sweepMode == SweepToFreeList);
return FreeList();
case Retired:
case Allocated:
RELEASE_ASSERT_NOT_REACHED();
return FreeList();
case Marked:
return sweepMode == SweepToFreeList
? specializedSweep<Marked, SweepToFreeList, callDestructors>()
: specializedSweep<Marked, SweepOnly, callDestructors>();
}
RELEASE_ASSERT_NOT_REACHED();
return FreeList();
}
class SetNewlyAllocatedFunctor : public MarkedBlock::VoidFunctor {
public:
SetNewlyAllocatedFunctor(MarkedBlock* block)
: m_block(block)
{
}
IterationStatus operator()(JSCell* cell)
{
ASSERT(MarkedBlock::blockFor(cell) == m_block);
m_block->setNewlyAllocated(cell);
return IterationStatus::Continue;
}
private:
MarkedBlock* m_block;
};
void MarkedBlock::stopAllocating(const FreeList& freeList)
{
HEAP_LOG_BLOCK_STATE_TRANSITION(this);
FreeCell* head = freeList.head;
if (m_state == Marked) {
ASSERT(!head);
return;
}
ASSERT(m_state == FreeListed);
ASSERT(!m_newlyAllocated);
m_newlyAllocated = std::make_unique<WTF::Bitmap<atomsPerBlock>>();
SetNewlyAllocatedFunctor functor(this);
forEachCell(functor);
FreeCell* next;
for (FreeCell* current = head; current; current = next) {
next = current->next;
reinterpret_cast<JSCell*>(current)->zap();
clearNewlyAllocated(current);
}
m_state = Marked;
}
void MarkedBlock::clearMarks()
{
#if ENABLE(GGC)
if (heap()->operationInProgress() == JSC::EdenCollection)
this->clearMarksWithCollectionType<EdenCollection>();
else
this->clearMarksWithCollectionType<FullCollection>();
#else
this->clearMarksWithCollectionType<FullCollection>();
#endif
}
template <HeapOperation collectionType>
void MarkedBlock::clearMarksWithCollectionType()
{
ASSERT(collectionType == FullCollection || collectionType == EdenCollection);
HEAP_LOG_BLOCK_STATE_TRANSITION(this);
ASSERT(m_state != New && m_state != FreeListed);
if (collectionType == FullCollection) {
m_marks.clearAll();
m_state = Marked;
return;
}
ASSERT(collectionType == EdenCollection);
if (m_state != Retired)
m_state = Marked;
}
void MarkedBlock::lastChanceToFinalize()
{
m_weakSet.lastChanceToFinalize();
clearNewlyAllocated();
clearMarksWithCollectionType<FullCollection>();
sweep();
}
MarkedBlock::FreeList MarkedBlock::resumeAllocating()
{
HEAP_LOG_BLOCK_STATE_TRANSITION(this);
ASSERT(m_state == Marked);
if (!m_newlyAllocated) {
return FreeList();
}
return sweep(SweepToFreeList);
}
void MarkedBlock::didRetireBlock(const FreeList& freeList)
{
HEAP_LOG_BLOCK_STATE_TRANSITION(this);
FreeCell* head = freeList.head;
FreeCell* next;
for (FreeCell* current = head; current; current = next) {
next = current->next;
reinterpret_cast<JSCell*>(current)->zap();
}
ASSERT(m_state == FreeListed);
m_state = Retired;
}
}