IsoAlignedMemoryAllocator.cpp [plain text]
#include "config.h"
#include "IsoAlignedMemoryAllocator.h"
namespace JSC {
IsoAlignedMemoryAllocator::IsoAlignedMemoryAllocator()
{
}
IsoAlignedMemoryAllocator::~IsoAlignedMemoryAllocator()
{
for (unsigned i = 0; i < m_blocks.size(); ++i) {
void* block = m_blocks[i];
if (!m_committed[i])
WTF::fastCommitAlignedMemory(block, MarkedBlock::blockSize);
fastAlignedFree(block);
}
}
void* IsoAlignedMemoryAllocator::tryAllocateAlignedMemory(size_t alignment, size_t size)
{
RELEASE_ASSERT(alignment == MarkedBlock::blockSize);
RELEASE_ASSERT(size == MarkedBlock::blockSize);
auto locker = holdLock(m_lock);
m_firstUncommitted = m_committed.findBit(m_firstUncommitted, false);
if (m_firstUncommitted < m_blocks.size()) {
m_committed[m_firstUncommitted] = true;
void* result = m_blocks[m_firstUncommitted];
WTF::fastCommitAlignedMemory(result, MarkedBlock::blockSize);
return result;
}
void* result = tryFastAlignedMalloc(MarkedBlock::blockSize, MarkedBlock::blockSize);
if (!result)
return nullptr;
unsigned index = m_blocks.size();
m_blocks.append(result);
m_blockIndices.add(result, index);
if (m_blocks.capacity() != m_committed.size())
m_committed.resize(m_blocks.capacity());
m_committed[index] = true;
return result;
}
void IsoAlignedMemoryAllocator::freeAlignedMemory(void* basePtr)
{
auto locker = holdLock(m_lock);
auto iter = m_blockIndices.find(basePtr);
RELEASE_ASSERT(iter != m_blockIndices.end());
unsigned index = iter->value;
m_committed[index] = false;
m_firstUncommitted = std::min(index, m_firstUncommitted);
WTF::fastDecommitAlignedMemory(basePtr, MarkedBlock::blockSize);
}
void IsoAlignedMemoryAllocator::dump(PrintStream& out) const
{
out.print("Iso(", RawPointer(this), ")");
}
}