Removed some public data and casting from the Heap
[WebKit-https.git] / Source / JavaScriptCore / heap / MarkedAllocator.cpp
1 #include "config.h"
2 #include "MarkedAllocator.h"
3
4 #include "GCActivityCallback.h"
5 #include "Heap.h"
6 #include "JSGlobalData.h"
7 #include <wtf/CurrentTime.h>
8
9 namespace JSC {
10
11 bool MarkedAllocator::isPagedOut(double deadline)
12 {
13     unsigned itersSinceLastTimeCheck = 0;
14     MarkedBlock* block = m_blockList.head();
15     while (block) {
16         block = block->next();
17         ++itersSinceLastTimeCheck;
18         if (itersSinceLastTimeCheck >= Heap::s_timeCheckResolution) {
19             double currentTime = WTF::monotonicallyIncreasingTime();
20             if (currentTime > deadline)
21                 return true;
22             itersSinceLastTimeCheck = 0;
23         }
24     }
25
26     return false;
27 }
28
29 inline void* MarkedAllocator::tryAllocateHelper()
30 {
31     if (!m_freeList.head) {
32         for (MarkedBlock*& block = m_blocksToSweep; block; block = block->next()) {
33             m_freeList = block->sweep(MarkedBlock::SweepToFreeList);
34             if (m_freeList.head) {
35                 m_currentBlock = block;
36                 break;
37             }
38             block->didConsumeFreeList();
39         }
40         
41         if (!m_freeList.head) {
42             m_currentBlock = 0;
43             return 0;
44         }
45     }
46     
47     MarkedBlock::FreeCell* head = m_freeList.head;
48     m_freeList.head = head->next;
49     ASSERT(head);
50     return head;
51 }
52     
53 inline void* MarkedAllocator::tryAllocate()
54 {
55     ASSERT(!m_heap->isBusy());
56     m_heap->m_operationInProgress = Allocation;
57     void* result = tryAllocateHelper();
58     m_heap->m_operationInProgress = NoOperation;
59     return result;
60 }
61     
62 void* MarkedAllocator::allocateSlowCase()
63 {
64     ASSERT(m_heap->globalData()->apiLock().currentThreadIsHoldingLock());
65 #if COLLECT_ON_EVERY_ALLOCATION
66     m_heap->collectAllGarbage();
67     ASSERT(m_heap->m_operationInProgress == NoOperation);
68 #endif
69     
70     ASSERT(!m_freeList.head);
71     m_heap->didAllocate(m_freeList.bytes);
72     
73     void* result = tryAllocate();
74     
75     if (LIKELY(result != 0))
76         return result;
77     
78     if (m_heap->shouldCollect()) {
79         m_heap->collect(Heap::DoNotSweep);
80
81         result = tryAllocate();
82         if (result)
83             return result;
84     }
85
86     ASSERT(!m_heap->shouldCollect());
87     
88     MarkedBlock* block = allocateBlock();
89     ASSERT(block);
90     addBlock(block);
91         
92     result = tryAllocate();
93     ASSERT(result);
94     return result;
95 }
96
97 MarkedBlock* MarkedAllocator::allocateBlock()
98 {
99     MarkedBlock* block = MarkedBlock::create(m_heap->blockAllocator().allocate(), m_heap, m_cellSize, m_cellsNeedDestruction, m_onlyContainsStructures);
100     m_markedSpace->didAddBlock(block);
101     return block;
102 }
103
104 void MarkedAllocator::addBlock(MarkedBlock* block)
105 {
106     ASSERT(!m_currentBlock);
107     ASSERT(!m_blocksToSweep);
108     ASSERT(!m_freeList.head);
109     
110     m_blockList.append(block);
111     m_blocksToSweep = m_currentBlock = block;
112     m_freeList = block->sweep(MarkedBlock::SweepToFreeList);
113 }
114
115 void MarkedAllocator::removeBlock(MarkedBlock* block)
116 {
117     if (m_currentBlock == block) {
118         m_currentBlock = m_currentBlock->next();
119         m_freeList = MarkedBlock::FreeList();
120     }
121     if (m_blocksToSweep == block)
122         m_blocksToSweep = m_blocksToSweep->next();
123     m_blockList.remove(block);
124 }
125
126 } // namespace JSC