StructureIDTable.cpp [plain text]
#include "config.h"
#include "StructureIDTable.h"
#include <limits.h>
#include <wtf/Atomics.h>
namespace JSC {
StructureIDTable::StructureIDTable()
: m_firstFreeOffset(0)
, m_table(std::make_unique<StructureOrOffset[]>(s_initialSize))
, m_size(0)
, m_capacity(s_initialSize)
{
allocateID(0);
}
void StructureIDTable::resize(size_t newCapacity)
{
auto newTable = std::make_unique<StructureOrOffset[]>(newCapacity);
memcpy(newTable.get(), table(), m_capacity * sizeof(StructureOrOffset));
WTF::storeStoreFence();
swap(m_table, newTable);
m_oldTables.append(WTFMove(newTable));
m_capacity = newCapacity;
}
void StructureIDTable::flushOldTables()
{
m_oldTables.clear();
}
StructureID StructureIDTable::allocateID(Structure* structure)
{
#if USE(JSVALUE64)
if (!m_firstFreeOffset) {
RELEASE_ASSERT(m_capacity <= UINT_MAX);
if (m_size == m_capacity)
resize(m_capacity * 2);
ASSERT(m_size < m_capacity);
StructureOrOffset newEntry;
newEntry.structure = structure;
if (m_size == s_unusedID) {
m_size++;
return allocateID(structure);
}
StructureID result = m_size;
table()[result] = newEntry;
m_size++;
ASSERT(!isNuked(result));
return result;
}
ASSERT(m_firstFreeOffset != s_unusedID);
StructureID result = m_firstFreeOffset;
m_firstFreeOffset = table()[m_firstFreeOffset].offset;
table()[result].structure = structure;
ASSERT(!isNuked(result));
return result;
#else
ASSERT(!isNuked(structure));
return structure;
#endif
}
void StructureIDTable::deallocateID(Structure* structure, StructureID structureID)
{
#if USE(JSVALUE64)
ASSERT(structureID != s_unusedID);
RELEASE_ASSERT(table()[structureID].structure == structure);
table()[structureID].offset = m_firstFreeOffset;
m_firstFreeOffset = structureID;
#else
UNUSED_PARAM(structure);
UNUSED_PARAM(structureID);
#endif
}
}