]> git.saurik.com Git - apple/javascriptcore.git/blame - heap/MarkedAllocator.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / heap / MarkedAllocator.h
CommitLineData
6fe7ccc8
A
1#ifndef MarkedAllocator_h
2#define MarkedAllocator_h
3
4#include "MarkedBlock.h"
5#include <wtf/DoublyLinkedList.h>
6
7namespace JSC {
8
9class Heap;
10class MarkedSpace;
11class LLIntOffsetsExtractor;
12
13namespace DFG {
14class SpeculativeJIT;
15}
16
17class MarkedAllocator {
93a37866 18 friend class LLIntOffsetsExtractor;
6fe7ccc8
A
19
20public:
93a37866
A
21 static ptrdiff_t offsetOfFreeListHead();
22
6fe7ccc8 23 MarkedAllocator();
81345200 24 void lastChanceToFinalize();
6fe7ccc8 25 void reset();
81345200
A
26 void stopAllocating();
27 void resumeAllocating();
6fe7ccc8 28 size_t cellSize() { return m_cellSize; }
ed1e77d3 29 bool needsDestruction() { return m_needsDestruction; }
93a37866 30 void* allocate(size_t);
6fe7ccc8 31 Heap* heap() { return m_heap; }
81345200
A
32 MarkedBlock* takeLastActiveBlock()
33 {
34 MarkedBlock* block = m_lastActiveBlock;
35 m_lastActiveBlock = 0;
36 return block;
37 }
6fe7ccc8
A
38
39 template<typename Functor> void forEachBlock(Functor&);
40
41 void addBlock(MarkedBlock*);
42 void removeBlock(MarkedBlock*);
ed1e77d3 43 void init(Heap*, MarkedSpace*, size_t cellSize, bool needsDestruction);
6fe7ccc8
A
44
45 bool isPagedOut(double deadline);
46
47private:
93a37866
A
48 JS_EXPORT_PRIVATE void* allocateSlowCase(size_t);
49 void* tryAllocate(size_t);
50 void* tryAllocateHelper(size_t);
81345200 51 void* tryPopFreeList(size_t);
93a37866 52 MarkedBlock* allocateBlock(size_t);
81345200 53 ALWAYS_INLINE void doTestCollectionsIfNeeded();
6fe7ccc8
A
54
55 MarkedBlock::FreeList m_freeList;
56 MarkedBlock* m_currentBlock;
81345200
A
57 MarkedBlock* m_lastActiveBlock;
58 MarkedBlock* m_nextBlockToSweep;
93a37866 59 DoublyLinkedList<MarkedBlock> m_blockList;
81345200 60 DoublyLinkedList<MarkedBlock> m_retiredBlocks;
6fe7ccc8 61 size_t m_cellSize;
ed1e77d3 62 bool m_needsDestruction { false };
6fe7ccc8
A
63 Heap* m_heap;
64 MarkedSpace* m_markedSpace;
65};
66
93a37866
A
67inline ptrdiff_t MarkedAllocator::offsetOfFreeListHead()
68{
69 return OBJECT_OFFSETOF(MarkedAllocator, m_freeList) + OBJECT_OFFSETOF(MarkedBlock::FreeList, head);
70}
71
6fe7ccc8
A
72inline MarkedAllocator::MarkedAllocator()
73 : m_currentBlock(0)
81345200
A
74 , m_lastActiveBlock(0)
75 , m_nextBlockToSweep(0)
6fe7ccc8 76 , m_cellSize(0)
6fe7ccc8
A
77 , m_heap(0)
78 , m_markedSpace(0)
79{
80}
81
ed1e77d3 82inline void MarkedAllocator::init(Heap* heap, MarkedSpace* markedSpace, size_t cellSize, bool needsDestruction)
6fe7ccc8
A
83{
84 m_heap = heap;
85 m_markedSpace = markedSpace;
86 m_cellSize = cellSize;
ed1e77d3 87 m_needsDestruction = needsDestruction;
6fe7ccc8
A
88}
89
93a37866 90inline void* MarkedAllocator::allocate(size_t bytes)
6fe7ccc8
A
91{
92 MarkedBlock::FreeCell* head = m_freeList.head;
93a37866
A
93 if (UNLIKELY(!head)) {
94 void* result = allocateSlowCase(bytes);
95#ifndef NDEBUG
96 memset(result, 0xCD, bytes);
97#endif
98 return result;
99 }
6fe7ccc8
A
100
101 m_freeList.head = head->next;
93a37866
A
102#ifndef NDEBUG
103 memset(head, 0xCD, bytes);
104#endif
6fe7ccc8
A
105 return head;
106}
107
81345200 108inline void MarkedAllocator::stopAllocating()
6fe7ccc8 109{
81345200 110 ASSERT(!m_lastActiveBlock);
6fe7ccc8
A
111 if (!m_currentBlock) {
112 ASSERT(!m_freeList.head);
113 return;
114 }
115
81345200
A
116 m_currentBlock->stopAllocating(m_freeList);
117 m_lastActiveBlock = m_currentBlock;
93a37866 118 m_currentBlock = 0;
6fe7ccc8
A
119 m_freeList = MarkedBlock::FreeList();
120}
121
81345200
A
122inline void MarkedAllocator::resumeAllocating()
123{
124 if (!m_lastActiveBlock)
125 return;
126
127 m_freeList = m_lastActiveBlock->resumeAllocating();
128 m_currentBlock = m_lastActiveBlock;
129 m_lastActiveBlock = 0;
130}
131
6fe7ccc8
A
132template <typename Functor> inline void MarkedAllocator::forEachBlock(Functor& functor)
133{
93a37866
A
134 MarkedBlock* next;
135 for (MarkedBlock* block = m_blockList.head(); block; block = next) {
6fe7ccc8 136 next = block->next();
93a37866 137 functor(block);
6fe7ccc8 138 }
81345200
A
139
140 for (MarkedBlock* block = m_retiredBlocks.head(); block; block = next) {
141 next = block->next();
142 functor(block);
143 }
6fe7ccc8 144}
81345200 145
6fe7ccc8
A
146} // namespace JSC
147
148#endif