- #define MMAP_FLAGS (MAP_PRIVATE | MAP_ANON | MAP_JIT)
-
-using namespace WTF;
-
-namespace JSC {
-
-#define TwoPow(n) (1ull << n)
-
-class AllocationTableSizeClass {
-public:
- AllocationTableSizeClass(size_t size, size_t blockSize, unsigned log2BlockSize)
- : m_blockSize(blockSize)
- {
- ASSERT(blockSize == TwoPow(log2BlockSize));
-
- // Calculate the number of blocks needed to hold size.
- size_t blockMask = blockSize - 1;
- m_blockCount = (size + blockMask) >> log2BlockSize;
-
- // Align to the smallest power of two >= m_blockCount.
- m_blockAlignment = 1;
- while (m_blockAlignment < m_blockCount)
- m_blockAlignment += m_blockAlignment;
- }
-
- size_t blockSize() const { return m_blockSize; }
- size_t blockCount() const { return m_blockCount; }
- size_t blockAlignment() const { return m_blockAlignment; }
-
- size_t size()
- {
- return m_blockSize * m_blockCount;
- }
-
-private:
- size_t m_blockSize;
- size_t m_blockCount;
- size_t m_blockAlignment;
-};
-
-template<unsigned log2Entries>
-class AllocationTableLeaf {
- typedef uint64_t BitField;
-
-public:
- static const unsigned log2SubregionSize = 12; // 2^12 == pagesize
- static const unsigned log2RegionSize = log2SubregionSize + log2Entries;
-
- static const size_t subregionSize = TwoPow(log2SubregionSize);
- static const size_t regionSize = TwoPow(log2RegionSize);
- static const unsigned entries = TwoPow(log2Entries);
- COMPILE_ASSERT(entries <= (sizeof(BitField) * 8), AllocationTableLeaf_entries_fit_in_BitField);
-
- AllocationTableLeaf()
- : m_allocated(0)
- {
- }
-
- ~AllocationTableLeaf()
- {
- ASSERT(isEmpty());
- }
-
- size_t allocate(AllocationTableSizeClass& sizeClass)
- {
- ASSERT(sizeClass.blockSize() == subregionSize);
- ASSERT(!isFull());
-
- size_t alignment = sizeClass.blockAlignment();
- size_t count = sizeClass.blockCount();
- // Use this mask to check for spans of free blocks.
- BitField mask = ((1ull << count) - 1) << (alignment - count);
-
- // Step in units of alignment size.
- for (unsigned i = 0; i < entries; i += alignment) {
- if (!(m_allocated & mask)) {
- m_allocated |= mask;
- return (i + (alignment - count)) << log2SubregionSize;
- }
- mask <<= alignment;
- }
- return notFound;
- }
-
- void free(size_t location, AllocationTableSizeClass& sizeClass)
- {
- ASSERT(sizeClass.blockSize() == subregionSize);
-
- size_t entry = location >> log2SubregionSize;
- size_t count = sizeClass.blockCount();
- BitField mask = ((1ull << count) - 1) << entry;
-
- ASSERT((m_allocated & mask) == mask);
- m_allocated &= ~mask;
- }
-
- bool isEmpty()
- {
- return !m_allocated;
- }
-
- bool isFull()
- {
- return !~m_allocated;
- }
-
- static size_t size()
- {
- return regionSize;
- }