StructureIDTable.cpp [plain text]
#include "config.h"
#include "StructureIDTable.h"
#include <wtf/Atomics.h>
#include <wtf/DataLog.h>
#include <wtf/RawPointer.h>
namespace JSC {
#if USE(JSVALUE64)
namespace StructureIDTableInternal {
static constexpr bool verbose = false;
}
StructureIDTable::StructureIDTable()
: m_table(makeUniqueArray<StructureOrOffset>(s_initialSize))
, m_size(1)
, m_capacity(s_initialSize)
{
table()[0].encodedStructureBits = 0;
makeFreeListFromRange(1, m_capacity - 1);
}
void StructureIDTable::makeFreeListFromRange(uint32_t first, uint32_t last)
{
ASSERT(!m_firstFreeOffset);
ASSERT(!m_lastFreeOffset);
uint32_t head = first;
uint32_t tail = last;
for (uint32_t i = first; i < last; ++i)
table()[i].offset = i + 1;
table()[last].offset = 0;
uint32_t size = last - first + 1;
uint32_t maxIterations = (size * 2) / 3;
for (uint32_t count = 0; count < maxIterations; ++count) {
uint32_t random = m_weakRandom.getUint32();
uint32_t nodeBefore = first + (random % size);
uint32_t pick = table()[nodeBefore].offset;
if (pick) {
uint32_t nodeAfter = table()[pick].offset;
table()[nodeBefore].offset = nodeAfter;
if ((random & 1) || !nodeAfter) {
table()[pick].offset = head;
head = pick;
if (!nodeAfter)
tail = nodeBefore;
} else {
table()[pick].offset = 0;
table()[tail].offset = pick;
tail = pick;
}
}
}
uint32_t cut = first + (m_weakRandom.getUint32() % size);
uint32_t afterCut = table()[cut].offset;
if (afterCut) {
table()[tail].offset = head;
tail = cut;
head = afterCut;
table()[cut].offset = 0;
}
m_firstFreeOffset = head;
m_lastFreeOffset = tail;
}
void StructureIDTable::resize(size_t newCapacity)
{
if (newCapacity > s_maximumNumberOfStructures)
newCapacity = s_maximumNumberOfStructures;
RELEASE_ASSERT_WITH_MESSAGE(m_size < newCapacity, "Crash intentionally because of exhaust of StructureIDs.");
auto newTable = makeUniqueArray<StructureOrOffset>(newCapacity);
memcpy(newTable.get(), table(), m_capacity * sizeof(StructureOrOffset));
WTF::storeStoreFence();
swap(m_table, newTable);
m_oldTables.append(WTFMove(newTable));
m_capacity = newCapacity;
makeFreeListFromRange(m_size, m_capacity - 1);
}
void StructureIDTable::flushOldTables()
{
m_oldTables.clear();
}
StructureID StructureIDTable::allocateID(Structure* structure)
{
if (UNLIKELY(!m_firstFreeOffset)) {
RELEASE_ASSERT(m_capacity <= s_maximumNumberOfStructures);
ASSERT(m_size == m_capacity);
resize(m_capacity * 2);
ASSERT(m_size < m_capacity);
RELEASE_ASSERT(m_firstFreeOffset);
}
constexpr uint32_t entropyBitsMask = (1 << s_numberOfEntropyBits) - 1;
uint32_t entropyBits = m_weakRandom.getUint32() & entropyBitsMask;
if (UNLIKELY(!entropyBits)) {
constexpr uint32_t numberOfValuesToPickFrom = entropyBitsMask;
entropyBits = (m_weakRandom.getUint32() % numberOfValuesToPickFrom) + 1;
}
uint32_t structureIndex = m_firstFreeOffset;
m_firstFreeOffset = table()[m_firstFreeOffset].offset;
if (!m_firstFreeOffset)
m_lastFreeOffset = 0;
StructureID result = (structureIndex << s_numberOfEntropyBits) | entropyBits;
table()[structureIndex].encodedStructureBits = encode(structure, result);
m_size++;
ASSERT(!isNuked(result));
dataLogLnIf(StructureIDTableInternal::verbose, "Allocated StructureID ", result, " for Structure ", RawPointer(structure));
return result;
}
void StructureIDTable::deallocateID(Structure* structure, StructureID structureID)
{
dataLogLnIf(StructureIDTableInternal::verbose, "Deallocated StructureID ", structureID);
ASSERT(structureID != s_unusedID);
uint32_t structureIndex = structureID >> s_numberOfEntropyBits;
ASSERT(structureIndex && structureIndex < s_maximumNumberOfStructures);
RELEASE_ASSERT(table()[structureIndex].encodedStructureBits == encode(structure, structureID));
m_size--;
if (!m_firstFreeOffset) {
table()[structureIndex].offset = 0;
m_firstFreeOffset = structureIndex;
m_lastFreeOffset = structureIndex;
return;
}
bool insertAtHead = m_weakRandom.getUint32() & 1;
if (insertAtHead) {
table()[structureIndex].offset = m_firstFreeOffset;
m_firstFreeOffset = structureIndex;
} else {
table()[structureIndex].offset = 0;
table()[m_lastFreeOffset].offset = structureIndex;
m_lastFreeOffset = structureIndex;
}
}
#endif // USE(JSVALUE64)
}