PreciseAllocation.cpp [plain text]
#include "config.h"
#include "PreciseAllocation.h"
#include "AlignedMemoryAllocator.h"
#include "IsoCellSetInlines.h"
#include "JSCInlines.h"
#include "Scribble.h"
#include "SubspaceInlines.h"
namespace JSC {
static inline bool isAlignedForPreciseAllocation(void* memory)
{
uintptr_t allocatedPointer = bitwise_cast<uintptr_t>(memory);
return !(allocatedPointer & (PreciseAllocation::alignment - 1));
}
PreciseAllocation* PreciseAllocation::tryCreate(Heap& heap, size_t size, Subspace* subspace, unsigned indexInSpace)
{
if constexpr (validateDFGDoesGC)
heap.verifyCanGC();
size_t adjustedAlignmentAllocationSize = headerSize() + size + halfAlignment;
static_assert(halfAlignment == 8, "We assume that memory returned by malloc has alignment >= 8.");
void* space = subspace->alignedMemoryAllocator()->tryAllocateMemory(adjustedAlignmentAllocationSize);
if (!space)
return nullptr;
bool adjustedAlignment = false;
if (!isAlignedForPreciseAllocation(space)) {
space = bitwise_cast<void*>(bitwise_cast<uintptr_t>(space) + halfAlignment);
adjustedAlignment = true;
ASSERT(isAlignedForPreciseAllocation(space));
}
if (scribbleFreeCells())
scribble(space, size);
return new (NotNull, space) PreciseAllocation(heap, size, subspace, indexInSpace, adjustedAlignment);
}
PreciseAllocation* PreciseAllocation::tryReallocate(size_t size, Subspace* subspace)
{
ASSERT(!isLowerTier());
size_t adjustedAlignmentAllocationSize = headerSize() + size + halfAlignment;
static_assert(halfAlignment == 8, "We assume that memory returned by malloc has alignment >= 8.");
ASSERT(subspace == m_subspace);
unsigned oldCellSize = m_cellSize;
bool oldAdjustedAlignment = m_adjustedAlignment;
void* oldBasePointer = basePointer();
void* newBasePointer = subspace->alignedMemoryAllocator()->tryReallocateMemory(oldBasePointer, adjustedAlignmentAllocationSize);
if (!newBasePointer)
return nullptr;
PreciseAllocation* newAllocation = bitwise_cast<PreciseAllocation*>(newBasePointer);
bool newAdjustedAlignment = false;
if (!isAlignedForPreciseAllocation(newBasePointer)) {
newAdjustedAlignment = true;
newAllocation = bitwise_cast<PreciseAllocation*>(bitwise_cast<uintptr_t>(newBasePointer) + halfAlignment);
ASSERT(isAlignedForPreciseAllocation(static_cast<void*>(newAllocation)));
}
if (oldAdjustedAlignment != newAdjustedAlignment) {
if (oldAdjustedAlignment) {
ASSERT(!newAdjustedAlignment);
ASSERT(newAllocation == newBasePointer);
memmove(newBasePointer, bitwise_cast<char*>(newBasePointer) + halfAlignment, oldCellSize + PreciseAllocation::headerSize());
} else {
ASSERT(newAdjustedAlignment);
ASSERT(newAllocation != newBasePointer);
ASSERT(newAllocation == bitwise_cast<void*>(bitwise_cast<char*>(newBasePointer) + halfAlignment));
memmove(bitwise_cast<char*>(newBasePointer) + halfAlignment, newBasePointer, oldCellSize + PreciseAllocation::headerSize());
}
}
newAllocation->m_cellSize = size;
newAllocation->m_adjustedAlignment = newAdjustedAlignment;
return newAllocation;
}
PreciseAllocation* PreciseAllocation::createForLowerTier(Heap& heap, size_t size, Subspace* subspace, uint8_t lowerTierIndex)
{
if constexpr (validateDFGDoesGC)
heap.verifyCanGC();
size_t adjustedAlignmentAllocationSize = headerSize() + size + halfAlignment;
static_assert(halfAlignment == 8, "We assume that memory returned by malloc has alignment >= 8.");
void* space = subspace->alignedMemoryAllocator()->tryAllocateMemory(adjustedAlignmentAllocationSize);
RELEASE_ASSERT(space);
bool adjustedAlignment = false;
if (!isAlignedForPreciseAllocation(space)) {
space = bitwise_cast<void*>(bitwise_cast<uintptr_t>(space) + halfAlignment);
adjustedAlignment = true;
ASSERT(isAlignedForPreciseAllocation(space));
}
if (scribbleFreeCells())
scribble(space, size);
PreciseAllocation* preciseAllocation = new (NotNull, space) PreciseAllocation(heap, size, subspace, 0, adjustedAlignment);
preciseAllocation->m_lowerTierIndex = lowerTierIndex;
return preciseAllocation;
}
PreciseAllocation* PreciseAllocation::reuseForLowerTier()
{
Heap& heap = *this->heap();
size_t size = m_cellSize;
Subspace* subspace = m_subspace;
bool adjustedAlignment = m_adjustedAlignment;
uint8_t lowerTierIndex = m_lowerTierIndex;
void* basePointer = this->basePointer();
this->~PreciseAllocation();
void* space = basePointer;
ASSERT((!isAlignedForPreciseAllocation(basePointer)) == adjustedAlignment);
if (adjustedAlignment)
space = bitwise_cast<void*>(bitwise_cast<uintptr_t>(basePointer) + halfAlignment);
PreciseAllocation* preciseAllocation = new (NotNull, space) PreciseAllocation(heap, size, subspace, 0, adjustedAlignment);
preciseAllocation->m_lowerTierIndex = lowerTierIndex;
preciseAllocation->m_hasValidCell = false;
return preciseAllocation;
}
PreciseAllocation::PreciseAllocation(Heap& heap, size_t size, Subspace* subspace, unsigned indexInSpace, bool adjustedAlignment)
: m_indexInSpace(indexInSpace)
, m_cellSize(size)
, m_isNewlyAllocated(true)
, m_hasValidCell(true)
, m_adjustedAlignment(adjustedAlignment)
, m_attributes(subspace->attributes())
, m_subspace(subspace)
, m_weakSet(heap.vm())
{
m_isMarked.store(0);
ASSERT(cell()->isPreciseAllocation());
}
PreciseAllocation::~PreciseAllocation()
{
if (isOnList())
remove();
}
void PreciseAllocation::lastChanceToFinalize()
{
m_weakSet.lastChanceToFinalize();
clearMarked();
clearNewlyAllocated();
sweep();
}
void PreciseAllocation::shrink()
{
m_weakSet.shrink();
}
void PreciseAllocation::visitWeakSet(SlotVisitor& visitor)
{
m_weakSet.visit(visitor);
}
void PreciseAllocation::reapWeakSet()
{
return m_weakSet.reap();
}
void PreciseAllocation::flip()
{
ASSERT(heap()->collectionScope() == CollectionScope::Full);
m_isNewlyAllocated |= isMarked();
m_isMarked.store(false, std::memory_order_relaxed);
}
bool PreciseAllocation::isEmpty()
{
return !isMarked() && m_weakSet.isEmpty() && !isNewlyAllocated();
}
void PreciseAllocation::sweep()
{
m_weakSet.sweep();
if (m_hasValidCell && !isLive()) {
if (m_attributes.destruction == NeedsDestruction)
m_subspace->destroy(vm(), static_cast<JSCell*>(cell()));
if (isLowerTier())
static_cast<IsoSubspace*>(m_subspace)->clearIsoCellSetBit(this);
m_hasValidCell = false;
}
}
void PreciseAllocation::destroy()
{
AlignedMemoryAllocator* allocator = m_subspace->alignedMemoryAllocator();
void* basePointer = this->basePointer();
this->~PreciseAllocation();
allocator->freeMemory(basePointer);
}
void PreciseAllocation::dump(PrintStream& out) const
{
out.print(RawPointer(this), ":(cell at ", RawPointer(cell()), " with size ", m_cellSize, " and attributes ", m_attributes, ")");
}
#if ASSERT_ENABLED
void PreciseAllocation::assertValidCell(VM& vm, HeapCell* cell) const
{
ASSERT(&vm == &this->vm());
ASSERT(cell == this->cell());
ASSERT(m_hasValidCell);
}
#endif
}