#include "config.h"
#include "MarkStack.h"
#include "MarkStackInlines.h"
#include "ConservativeRoots.h"
#include "CopiedSpace.h"
#include "CopiedSpaceInlines.h"
#include "Heap.h"
#include "JSArray.h"
#include "JSCell.h"
#include "JSObject.h"
#include "SlotVisitorInlines.h"
#include "Structure.h"
#include "WriteBarrier.h"
#include <wtf/Atomics.h>
#include <wtf/DataLog.h>
#include <wtf/MainThread.h>
namespace JSC {
COMPILE_ASSERT(MarkStackSegment::blockSize == WeakBlock::blockSize, blockSizeMatch);
MarkStackArray::MarkStackArray(BlockAllocator& blockAllocator)
: m_blockAllocator(blockAllocator)
, m_top(0)
, m_numberOfSegments(0)
{
m_segments.push(MarkStackSegment::create(m_blockAllocator.allocate<MarkStackSegment>()));
m_numberOfSegments++;
}
MarkStackArray::~MarkStackArray()
{
ASSERT(m_numberOfSegments == 1 && m_segments.size() == 1);
m_blockAllocator.deallocate(MarkStackSegment::destroy(m_segments.removeHead()));
}
void MarkStackArray::expand()
{
ASSERT(m_segments.head()->m_top == s_segmentCapacity);
MarkStackSegment* nextSegment = MarkStackSegment::create(m_blockAllocator.allocate<MarkStackSegment>());
m_numberOfSegments++;
#if !ASSERT_DISABLED
nextSegment->m_top = 0;
#endif
m_segments.push(nextSegment);
setTopForEmptySegment();
validatePrevious();
}
bool MarkStackArray::refill()
{
validatePrevious();
if (top())
return true;
m_blockAllocator.deallocate(MarkStackSegment::destroy(m_segments.removeHead()));
ASSERT(m_numberOfSegments > 1);
m_numberOfSegments--;
setTopForFullSegment();
validatePrevious();
return true;
}
void MarkStackArray::donateSomeCellsTo(MarkStackArray& other)
{
size_t segmentsToDonate = m_numberOfSegments / 2;
if (!segmentsToDonate) {
size_t cellsToDonate = m_top / 2; while (cellsToDonate--) {
ASSERT(m_top);
other.append(removeLast());
}
return;
}
validatePrevious();
other.validatePrevious();
MarkStackSegment* myHead = m_segments.removeHead();
MarkStackSegment* otherHead = other.m_segments.removeHead();
while (segmentsToDonate--) {
MarkStackSegment* current = m_segments.removeHead();
ASSERT(current);
ASSERT(m_numberOfSegments > 1);
other.m_segments.push(current);
m_numberOfSegments--;
other.m_numberOfSegments++;
}
m_segments.push(myHead);
other.m_segments.push(otherHead);
validatePrevious();
other.validatePrevious();
}
void MarkStackArray::stealSomeCellsFrom(MarkStackArray& other, size_t idleThreadCount)
{
validatePrevious();
other.validatePrevious();
if (other.m_numberOfSegments > 1) {
MarkStackSegment* otherHead = other.m_segments.removeHead();
MarkStackSegment* myHead = m_segments.removeHead();
ASSERT(other.m_segments.head()->m_top == s_segmentCapacity);
m_segments.push(other.m_segments.removeHead());
m_numberOfSegments++;
other.m_numberOfSegments--;
m_segments.push(myHead);
other.m_segments.push(otherHead);
validatePrevious();
other.validatePrevious();
return;
}
size_t numberOfCellsToSteal = (other.size() + idleThreadCount - 1) / idleThreadCount; while (numberOfCellsToSteal-- > 0 && other.canRemoveLast())
append(other.removeLast());
}
}