2011-02-07 Geoffrey Garen <ggaren@apple.com>
[WebKit-https.git] / Source / JavaScriptCore / runtime / MarkedSpace.cpp
1 /*
2  *  Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3  *  Copyright (C) 2007 Eric Seidel <eric@webkit.org>
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2 of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  */
20
21 #include "config.h"
22 #include "MarkedSpace.h"
23
24 #include "CollectorHeapIterator.h"
25 #include "JSCell.h"
26 #include "JSGlobalData.h"
27 #include "JSLock.h"
28
29 using std::max;
30
31 namespace JSC {
32
33 class Structure;
34
35 // tunable parameters
36
37 const size_t GROWTH_FACTOR = 2;
38 const size_t LOW_WATER_FACTOR = 4;
39 const size_t ALLOCATIONS_PER_COLLECTION = 3600;
40 // This value has to be a macro to be used in max() without introducing
41 // a PIC branch in Mach-O binaries, see <rdar://problem/5971391>.
42 #define MIN_ARRAY_SIZE (static_cast<size_t>(14))
43
44 MarkedSpace::MarkedSpace(JSGlobalData* globalData)
45     : m_globalData(globalData)
46 {
47     memset(&m_heap, 0, sizeof(CollectorHeap));
48     allocateBlock();
49 }
50
51 void MarkedSpace::destroy()
52 {
53     clearMarkBits(); // Make sure weak pointers appear dead during destruction.
54
55     while (m_heap.usedBlocks)
56         freeBlock(0);
57     fastFree(m_heap.blocks);
58
59     memset(&m_heap, 0, sizeof(CollectorHeap));
60 }
61
62 NEVER_INLINE MarkedBlock* MarkedSpace::allocateBlock()
63 {
64     PageAllocationAligned allocation = PageAllocationAligned::allocate(BLOCK_SIZE, BLOCK_SIZE, OSAllocator::JSGCHeapPages);
65     MarkedBlock* block = static_cast<MarkedBlock*>(allocation.base());
66     if (!block)
67         CRASH();
68
69     // Initialize block.
70
71     block->heap = &globalData()->heap;
72     clearMarkBits(block);
73
74     Structure* dummyMarkableCellStructure = globalData()->dummyMarkableCellStructure.get();
75     for (size_t i = 0; i < HeapConstants::cellsPerBlock; ++i)
76         new (&block->cells[i]) JSCell(dummyMarkableCellStructure);
77     
78     // Add block to blocks vector.
79
80     size_t numBlocks = m_heap.numBlocks;
81     if (m_heap.usedBlocks == numBlocks) {
82         static const size_t maxNumBlocks = ULONG_MAX / sizeof(PageAllocationAligned) / GROWTH_FACTOR;
83         if (numBlocks > maxNumBlocks)
84             CRASH();
85         numBlocks = max(MIN_ARRAY_SIZE, numBlocks * GROWTH_FACTOR);
86         m_heap.numBlocks = numBlocks;
87         m_heap.blocks = static_cast<PageAllocationAligned*>(fastRealloc(m_heap.blocks, numBlocks * sizeof(PageAllocationAligned)));
88     }
89     m_heap.blocks[m_heap.usedBlocks++] = allocation;
90
91     return block;
92 }
93
94 NEVER_INLINE void MarkedSpace::freeBlock(size_t block)
95 {
96     ObjectIterator it(m_heap, block, 0);
97     ObjectIterator end(m_heap, block + 1, 0);
98     for ( ; it != end; ++it)
99         (*it)->~JSCell();
100     m_heap.blocks[block].deallocate();
101
102     // swap with the last block so we compact as we go
103     m_heap.blocks[block] = m_heap.blocks[m_heap.usedBlocks - 1];
104     m_heap.usedBlocks--;
105
106     if (m_heap.numBlocks > MIN_ARRAY_SIZE && m_heap.usedBlocks < m_heap.numBlocks / LOW_WATER_FACTOR) {
107         m_heap.numBlocks = m_heap.numBlocks / GROWTH_FACTOR; 
108         m_heap.blocks = static_cast<PageAllocationAligned*>(fastRealloc(m_heap.blocks, m_heap.numBlocks * sizeof(PageAllocationAligned)));
109     }
110 }
111
112 void* MarkedSpace::allocate(size_t s)
113 {
114     ASSERT(globalData()->identifierTable == wtfThreadData().currentIdentifierTable());
115     typedef HeapConstants::Block Block;
116     typedef HeapConstants::Cell Cell;
117     
118     ASSERT(JSLock::lockCount() > 0);
119     ASSERT(JSLock::currentThreadIsHoldingLock());
120     ASSERT_UNUSED(s, s <= HeapConstants::cellSize);
121
122     // Fast case: find the next garbage cell and recycle it.
123
124     do {
125         ASSERT(m_heap.nextBlock < m_heap.usedBlocks);
126         Block* block = m_heap.collectorBlock(m_heap.nextBlock);
127         do {
128             ASSERT(m_heap.nextCell < HeapConstants::cellsPerBlock);
129             if (!block->marked.testAndSet(m_heap.nextCell)) { // Always false for the last cell in the block
130                 Cell* cell = &block->cells[m_heap.nextCell];
131
132                 JSCell* imp = reinterpret_cast<JSCell*>(cell);
133                 imp->~JSCell();
134
135                 ++m_heap.nextCell;
136                 return cell;
137             }
138             m_heap.nextCell = block->marked.nextPossiblyUnset(m_heap.nextCell);
139         } while (m_heap.nextCell != HeapConstants::cellsPerBlock);
140         m_heap.nextCell = 0;
141         m_heap.waterMark += BLOCK_SIZE;
142     } while (++m_heap.nextBlock != m_heap.usedBlocks);
143
144     if (m_heap.waterMark < m_heap.highWaterMark) {
145         MarkedBlock* block = allocateBlock();
146         ASSERT(!block->marked.get(m_heap.nextCell));
147         block->marked.set(m_heap.nextCell);
148         return &block->cells[m_heap.nextCell++];
149     }
150
151     return 0;
152 }
153
154 void MarkedSpace::resizeBlocks()
155 {
156     size_t usedCellCount = markedCells();
157     size_t minCellCount = usedCellCount + max(ALLOCATIONS_PER_COLLECTION, usedCellCount);
158     size_t minBlockCount = (minCellCount + HeapConstants::cellsPerBlock - 1) / HeapConstants::cellsPerBlock;
159
160     size_t maxCellCount = 1.25f * minCellCount;
161     size_t maxBlockCount = (maxCellCount + HeapConstants::cellsPerBlock - 1) / HeapConstants::cellsPerBlock;
162
163     if (m_heap.usedBlocks < minBlockCount)
164         growBlocks(minBlockCount);
165     else if (m_heap.usedBlocks > maxBlockCount)
166         shrinkBlocks(maxBlockCount);
167 }
168
169 void MarkedSpace::growBlocks(size_t neededBlocks)
170 {
171     ASSERT(m_heap.usedBlocks < neededBlocks);
172     while (m_heap.usedBlocks < neededBlocks)
173         allocateBlock();
174 }
175
176 void MarkedSpace::shrinkBlocks(size_t neededBlocks)
177 {
178     ASSERT(m_heap.usedBlocks > neededBlocks);
179     
180     // Clear the always-on last bit, so isEmpty() isn't fooled by it.
181     for (size_t i = 0; i < m_heap.usedBlocks; ++i)
182         m_heap.collectorBlock(i)->marked.clear(HeapConstants::cellsPerBlock - 1);
183
184     for (size_t i = 0; i != m_heap.usedBlocks && m_heap.usedBlocks != neededBlocks; ) {
185         if (m_heap.collectorBlock(i)->marked.isEmpty()) {
186             freeBlock(i);
187         } else
188             ++i;
189     }
190
191     // Reset the always-on last bit.
192     for (size_t i = 0; i < m_heap.usedBlocks; ++i)
193         m_heap.collectorBlock(i)->marked.set(HeapConstants::cellsPerBlock - 1);
194 }
195
196 bool MarkedSpace::containsSlowCase(const void* x)
197 {
198     uintptr_t xAsBits = reinterpret_cast<uintptr_t>(x);
199     xAsBits &= CELL_ALIGN_MASK;
200
201     uintptr_t offset = xAsBits & BLOCK_OFFSET_MASK;
202     const size_t lastCellOffset = sizeof(CollectorCell) * (CELLS_PER_BLOCK - 1);
203     if (offset > lastCellOffset)
204         return false;
205
206     MarkedBlock* blockAddr = reinterpret_cast<MarkedBlock*>(xAsBits - offset);
207     size_t usedBlocks = m_heap.usedBlocks;
208     for (size_t block = 0; block < usedBlocks; block++) {
209         if (m_heap.collectorBlock(block) != blockAddr)
210             continue;
211
212         // x is a pointer into the heap. Now, verify that the cell it
213         // points to is live. (If the cell is dead, we must not mark it,
214         // since that would revive it in a zombie state.)
215         size_t cellOffset = offset / CELL_SIZE;
216         return blockAddr->marked.get(cellOffset);
217     }
218     
219     return false;
220 }
221
222 void MarkedSpace::clearMarkBits()
223 {
224     for (size_t i = 0; i < m_heap.usedBlocks; ++i)
225         clearMarkBits(m_heap.collectorBlock(i));
226 }
227
228 void MarkedSpace::clearMarkBits(MarkedBlock* block)
229 {
230     // allocate assumes that the last cell in every block is marked.
231     block->marked.clearAll();
232     block->marked.set(HeapConstants::cellsPerBlock - 1);
233 }
234
235 size_t MarkedSpace::markedCells(size_t startBlock, size_t startCell) const
236 {
237     ASSERT(startBlock <= m_heap.usedBlocks);
238     ASSERT(startCell < HeapConstants::cellsPerBlock);
239
240     if (startBlock >= m_heap.usedBlocks)
241         return 0;
242
243     size_t result = 0;
244     result += m_heap.collectorBlock(startBlock)->marked.count(startCell);
245     for (size_t i = startBlock + 1; i < m_heap.usedBlocks; ++i)
246         result += m_heap.collectorBlock(i)->marked.count();
247
248     return result;
249 }
250
251 void MarkedSpace::sweep()
252 {
253 #if !ENABLE(JSC_ZOMBIES)
254     Structure* dummyMarkableCellStructure = globalData()->dummyMarkableCellStructure.get();
255 #endif
256
257     DeadObjectIterator it(m_heap, 0, 0);
258     DeadObjectIterator end(m_heap, m_heap.usedBlocks, 0);
259     for ( ; it != end; ++it) {
260         JSCell* cell = *it;
261 #if ENABLE(JSC_ZOMBIES)
262         if (!cell->isZombie()) {
263             const ClassInfo* info = cell->classInfo();
264             cell->~JSCell();
265             new (cell) JSZombie(info, JSZombie::leakedZombieStructure());
266             Heap::markCell(cell);
267         }
268 #else
269         cell->~JSCell();
270         // Callers of sweep assume it's safe to mark any cell in the heap.
271         new (cell) JSCell(dummyMarkableCellStructure);
272 #endif
273     }
274     
275     if (m_heap.usedBlocks > 1)
276         shrinkBlocks(1);
277 }
278
279 size_t MarkedSpace::objectCount() const
280 {
281     return m_heap.nextBlock * HeapConstants::cellsPerBlock // allocated full blocks
282            + m_heap.nextCell // allocated cells in current block
283            + markedCells(m_heap.nextBlock, m_heap.nextCell) // marked cells in remainder of m_heap
284            - m_heap.usedBlocks; // 1 cell per block is a dummy sentinel
285 }
286
287 size_t MarkedSpace::size() const
288 {
289     return objectCount() * HeapConstants::cellSize;
290 }
291
292 size_t MarkedSpace::capacity() const
293 {
294     return m_heap.usedBlocks * BLOCK_SIZE;
295 }
296
297 void MarkedSpace::reset()
298 {
299     m_heap.nextCell = 0;
300     m_heap.nextBlock = 0;
301     m_heap.waterMark = 0;
302 #if ENABLE(JSC_ZOMBIES)
303     sweep();
304 #endif
305 }
306
307 LiveObjectIterator MarkedSpace::primaryHeapBegin()
308 {
309     return LiveObjectIterator(m_heap, 0, 0);
310 }
311
312 LiveObjectIterator MarkedSpace::primaryHeapEnd()
313 {
314     return LiveObjectIterator(m_heap, m_heap.usedBlocks, 0);
315 }
316
317 } // namespace JSC