MarkedAllocator.h 2.69 KB
Newer Older
mhahnenberg@apple.com's avatar
mhahnenberg@apple.com committed
1 2 3 4 5 6 7 8 9 10
#ifndef MarkedAllocator_h
#define MarkedAllocator_h

#include "MarkedBlock.h"
#include <wtf/DoublyLinkedList.h>

namespace JSC {

class Heap;
class MarkedSpace;
11
class LLIntOffsetsExtractor;
mhahnenberg@apple.com's avatar
mhahnenberg@apple.com committed
12 13 14 15 16 17 18 19 20 21 22 23

namespace DFG {
class SpeculativeJIT;
}

class MarkedAllocator {
    friend class JIT;
    friend class DFG::SpeculativeJIT;

public:
    MarkedAllocator();
    void reset();
24
    void canonicalizeCellLivenessData();
mhahnenberg@apple.com's avatar
mhahnenberg@apple.com committed
25
    size_t cellSize() { return m_cellSize; }
26
    MarkedBlock::DestructorType destructorType() { return m_destructorType; }
27
    void* allocate(size_t);
mhahnenberg@apple.com's avatar
mhahnenberg@apple.com committed
28 29 30 31 32 33
    Heap* heap() { return m_heap; }
    
    template<typename Functor> void forEachBlock(Functor&);
    
    void addBlock(MarkedBlock*);
    void removeBlock(MarkedBlock*);
34
    void init(Heap*, MarkedSpace*, size_t cellSize, MarkedBlock::DestructorType);
35 36 37

    bool isPagedOut(double deadline);
   
mhahnenberg@apple.com's avatar
mhahnenberg@apple.com committed
38
private:
39 40
    friend class LLIntOffsetsExtractor;
    
41 42 43 44
    JS_EXPORT_PRIVATE void* allocateSlowCase(size_t);
    void* tryAllocate(size_t);
    void* tryAllocateHelper(size_t);
    MarkedBlock* allocateBlock(size_t);
mhahnenberg@apple.com's avatar
mhahnenberg@apple.com committed
45
    
46
    MarkedBlock::FreeList m_freeList;
mhahnenberg@apple.com's avatar
mhahnenberg@apple.com committed
47
    MarkedBlock* m_currentBlock;
48
    MarkedBlock* m_blocksToSweep;
49
    DoublyLinkedList<MarkedBlock> m_blockList;
mhahnenberg@apple.com's avatar
mhahnenberg@apple.com committed
50
    size_t m_cellSize;
51
    MarkedBlock::DestructorType m_destructorType;
mhahnenberg@apple.com's avatar
mhahnenberg@apple.com committed
52 53 54 55 56
    Heap* m_heap;
    MarkedSpace* m_markedSpace;
};

inline MarkedAllocator::MarkedAllocator()
57
    : m_currentBlock(0)
58
    , m_blocksToSweep(0)
mhahnenberg@apple.com's avatar
mhahnenberg@apple.com committed
59
    , m_cellSize(0)
60
    , m_destructorType(MarkedBlock::None)
mhahnenberg@apple.com's avatar
mhahnenberg@apple.com committed
61 62 63 64
    , m_heap(0)
    , m_markedSpace(0)
{
}
65

66
inline void MarkedAllocator::init(Heap* heap, MarkedSpace* markedSpace, size_t cellSize, MarkedBlock::DestructorType destructorType)
67 68 69 70
{
    m_heap = heap;
    m_markedSpace = markedSpace;
    m_cellSize = cellSize;
71
    m_destructorType = destructorType;
72 73
}

74
inline void* MarkedAllocator::allocate(size_t bytes)
mhahnenberg@apple.com's avatar
mhahnenberg@apple.com committed
75
{
76 77
    MarkedBlock::FreeCell* head = m_freeList.head;
    if (UNLIKELY(!head))
78
        return allocateSlowCase(bytes);
mhahnenberg@apple.com's avatar
mhahnenberg@apple.com committed
79
    
80 81
    m_freeList.head = head->next;
    return head;
mhahnenberg@apple.com's avatar
mhahnenberg@apple.com committed
82 83 84 85
}

inline void MarkedAllocator::reset()
{
86 87
    m_currentBlock = 0;
    m_freeList = MarkedBlock::FreeList();
88
    m_blocksToSweep = m_blockList.head();
mhahnenberg@apple.com's avatar
mhahnenberg@apple.com committed
89 90
}

91
inline void MarkedAllocator::canonicalizeCellLivenessData()
mhahnenberg@apple.com's avatar
mhahnenberg@apple.com committed
92 93
{
    if (!m_currentBlock) {
94
        ASSERT(!m_freeList.head);
mhahnenberg@apple.com's avatar
mhahnenberg@apple.com committed
95 96 97
        return;
    }
    
98
    m_currentBlock->canonicalizeCellLivenessData(m_freeList);
99
    m_currentBlock = 0;
100
    m_freeList = MarkedBlock::FreeList();
mhahnenberg@apple.com's avatar
mhahnenberg@apple.com committed
101 102 103 104
}

template <typename Functor> inline void MarkedAllocator::forEachBlock(Functor& functor)
{
105 106
    MarkedBlock* next;
    for (MarkedBlock* block = m_blockList.head(); block; block = next) {
mhahnenberg@apple.com's avatar
mhahnenberg@apple.com committed
107
        next = block->next();
108
        functor(block);
mhahnenberg@apple.com's avatar
mhahnenberg@apple.com committed
109 110 111 112 113 114
    }
}
    
} // namespace JSC

#endif