Build fix
[WebKit-https.git] / Source / JavaScriptCore / heap / MarkedAllocator.h
1 #ifndef MarkedAllocator_h
2 #define MarkedAllocator_h
3
4 #include "MarkedBlock.h"
5 #include <wtf/DoublyLinkedList.h>
6
7 namespace JSC {
8
9 class Heap;
10 class MarkedSpace;
11
12 namespace DFG {
13 class SpeculativeJIT;
14 }
15
16 class MarkedAllocator {
17     friend class JIT;
18     friend class DFG::SpeculativeJIT;
19
20 public:
21     MarkedAllocator();
22     void reset();
23     void zapFreeList();
24     size_t cellSize() { return m_cellSize; }
25     void* allocate();
26     Heap* heap() { return m_heap; }
27     
28     template<typename Functor> void forEachBlock(Functor&);
29     
30     void addBlock(MarkedBlock*);
31     void removeBlock(MarkedBlock*);
32     void setHeap(Heap* heap) { m_heap = heap; }
33     void setCellSize(size_t cellSize) { m_cellSize = cellSize; }
34     void setMarkedSpace(MarkedSpace* space) { m_markedSpace = space; }
35     
36 private:
37     JS_EXPORT_PRIVATE void* allocateSlowCase();
38     void* tryAllocate();
39     void* tryAllocateHelper();
40     MarkedBlock* allocateBlock(AllocationEffort);
41     
42     MarkedBlock::FreeCell* m_firstFreeCell;
43     MarkedBlock* m_currentBlock;
44     DoublyLinkedList<HeapBlock> m_blockList;
45     size_t m_cellSize;
46     Heap* m_heap;
47     MarkedSpace* m_markedSpace;
48 };
49
50 inline MarkedAllocator::MarkedAllocator()
51     : m_firstFreeCell(0)
52     , m_currentBlock(0)
53     , m_cellSize(0)
54     , m_heap(0)
55     , m_markedSpace(0)
56 {
57 }
58     
59 inline void* MarkedAllocator::allocate()
60 {
61     MarkedBlock::FreeCell* firstFreeCell = m_firstFreeCell;
62     // This is a light-weight fast path to cover the most common case.
63     if (UNLIKELY(!firstFreeCell))
64         return allocateSlowCase();
65     
66     m_firstFreeCell = firstFreeCell->next;
67     return firstFreeCell;
68 }
69
70 inline void MarkedAllocator::reset()
71 {
72     m_currentBlock = static_cast<MarkedBlock*>(m_blockList.head());
73 }
74
75 inline void MarkedAllocator::zapFreeList()
76 {
77     if (!m_currentBlock) {
78         ASSERT(!m_firstFreeCell);
79         return;
80     }
81     
82     m_currentBlock->zapFreeList(m_firstFreeCell);
83     m_firstFreeCell = 0;
84 }
85
86 template <typename Functor> inline void MarkedAllocator::forEachBlock(Functor& functor)
87 {
88     HeapBlock* next;
89     for (HeapBlock* block = m_blockList.head(); block; block = next) {
90         next = block->next();
91         functor(static_cast<MarkedBlock*>(block));
92     }
93 }
94     
95 } // namespace JSC
96
97 #endif