mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 1 | #include "config.h" |
| 2 | #include "MarkedAllocator.h" |
| 3 | |
mhahnenberg@apple.com | 2b64eec0 | 2012-04-18 16:18:32 +0000 | [diff] [blame] | 4 | #include "GCActivityCallback.h" |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 5 | #include "Heap.h" |
mhahnenberg@apple.com | 59c64f1 | 2012-07-31 23:05:12 +0000 | [diff] [blame] | 6 | #include "IncrementalSweeper.h" |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 7 | #include "VM.h" |
mhahnenberg@apple.com | 2e132e4 | 2012-05-03 00:14:05 +0000 | [diff] [blame] | 8 | #include <wtf/CurrentTime.h> |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 9 | |
| 10 | namespace JSC { |
| 11 | |
mhahnenberg@apple.com | 2e132e4 | 2012-05-03 00:14:05 +0000 | [diff] [blame] | 12 | bool MarkedAllocator::isPagedOut(double deadline) |
| 13 | { |
| 14 | unsigned itersSinceLastTimeCheck = 0; |
ggaren@apple.com | dd7793a | 2012-07-31 21:26:38 +0000 | [diff] [blame] | 15 | MarkedBlock* block = m_blockList.head(); |
mhahnenberg@apple.com | 2e132e4 | 2012-05-03 00:14:05 +0000 | [diff] [blame] | 16 | while (block) { |
| 17 | block = block->next(); |
| 18 | ++itersSinceLastTimeCheck; |
| 19 | if (itersSinceLastTimeCheck >= Heap::s_timeCheckResolution) { |
| 20 | double currentTime = WTF::monotonicallyIncreasingTime(); |
| 21 | if (currentTime > deadline) |
| 22 | return true; |
| 23 | itersSinceLastTimeCheck = 0; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | return false; |
| 28 | } |
| 29 | |
ggaren@apple.com | 6159e5f | 2012-09-11 03:02:46 +0000 | [diff] [blame] | 30 | inline void* MarkedAllocator::tryAllocateHelper(size_t bytes) |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 31 | { |
mhahnenberg@apple.com | 8b5cfd3 | 2012-04-20 00:05:37 +0000 | [diff] [blame] | 32 | if (!m_freeList.head) { |
ggaren@apple.com | dd7793a | 2012-07-31 21:26:38 +0000 | [diff] [blame] | 33 | for (MarkedBlock*& block = m_blocksToSweep; block; block = block->next()) { |
ggaren@apple.com | 6159e5f | 2012-09-11 03:02:46 +0000 | [diff] [blame] | 34 | MarkedBlock::FreeList freeList = block->sweep(MarkedBlock::SweepToFreeList); |
| 35 | if (!freeList.head) { |
| 36 | block->didConsumeFreeList(); |
| 37 | continue; |
mhahnenberg@apple.com | 7f5b959 | 2012-07-27 22:59:14 +0000 | [diff] [blame] | 38 | } |
ggaren@apple.com | 6159e5f | 2012-09-11 03:02:46 +0000 | [diff] [blame] | 39 | |
| 40 | if (bytes > block->cellSize()) { |
mhahnenberg@apple.com | 76e50b1 | 2012-09-14 07:06:23 +0000 | [diff] [blame] | 41 | block->canonicalizeCellLivenessData(freeList); |
ggaren@apple.com | 6159e5f | 2012-09-11 03:02:46 +0000 | [diff] [blame] | 42 | continue; |
| 43 | } |
| 44 | |
| 45 | m_currentBlock = block; |
| 46 | m_freeList = freeList; |
| 47 | break; |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 48 | } |
| 49 | |
mhahnenberg@apple.com | 7f5b959 | 2012-07-27 22:59:14 +0000 | [diff] [blame] | 50 | if (!m_freeList.head) { |
| 51 | m_currentBlock = 0; |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 52 | return 0; |
mhahnenberg@apple.com | 7f5b959 | 2012-07-27 22:59:14 +0000 | [diff] [blame] | 53 | } |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 54 | } |
| 55 | |
mhahnenberg@apple.com | 8b5cfd3 | 2012-04-20 00:05:37 +0000 | [diff] [blame] | 56 | MarkedBlock::FreeCell* head = m_freeList.head; |
| 57 | m_freeList.head = head->next; |
| 58 | ASSERT(head); |
| 59 | return head; |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 60 | } |
| 61 | |
ggaren@apple.com | 6159e5f | 2012-09-11 03:02:46 +0000 | [diff] [blame] | 62 | inline void* MarkedAllocator::tryAllocate(size_t bytes) |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 63 | { |
mhahnenberg@apple.com | 47c9c53 | 2012-06-05 20:38:21 +0000 | [diff] [blame] | 64 | ASSERT(!m_heap->isBusy()); |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 65 | m_heap->m_operationInProgress = Allocation; |
ggaren@apple.com | 6159e5f | 2012-09-11 03:02:46 +0000 | [diff] [blame] | 66 | void* result = tryAllocateHelper(bytes); |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 67 | m_heap->m_operationInProgress = NoOperation; |
| 68 | return result; |
| 69 | } |
| 70 | |
ggaren@apple.com | 6159e5f | 2012-09-11 03:02:46 +0000 | [diff] [blame] | 71 | void* MarkedAllocator::allocateSlowCase(size_t bytes) |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 72 | { |
andersca@apple.com | b987aae | 2013-07-26 00:13:13 +0000 | [diff] [blame^] | 73 | ASSERT(m_heap->vm()->currentThreadIsHoldingAPILock()); |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 74 | #if COLLECT_ON_EVERY_ALLOCATION |
| 75 | m_heap->collectAllGarbage(); |
| 76 | ASSERT(m_heap->m_operationInProgress == NoOperation); |
| 77 | #endif |
| 78 | |
mhahnenberg@apple.com | 8b5cfd3 | 2012-04-20 00:05:37 +0000 | [diff] [blame] | 79 | ASSERT(!m_freeList.head); |
| 80 | m_heap->didAllocate(m_freeList.bytes); |
mhahnenberg@apple.com | 2b64eec0 | 2012-04-18 16:18:32 +0000 | [diff] [blame] | 81 | |
ggaren@apple.com | 6159e5f | 2012-09-11 03:02:46 +0000 | [diff] [blame] | 82 | void* result = tryAllocate(bytes); |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 83 | |
| 84 | if (LIKELY(result != 0)) |
| 85 | return result; |
| 86 | |
oliver@apple.com | 284cc3d | 2013-07-25 04:00:33 +0000 | [diff] [blame] | 87 | if (m_heap->collectIfNecessaryOrDefer()) { |
ggaren@apple.com | 6159e5f | 2012-09-11 03:02:46 +0000 | [diff] [blame] | 88 | result = tryAllocate(bytes); |
mhahnenberg@apple.com | 3e7e4e0 | 2012-05-09 02:44:39 +0000 | [diff] [blame] | 89 | if (result) |
| 90 | return result; |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 91 | } |
mhahnenberg@apple.com | 3e7e4e0 | 2012-05-09 02:44:39 +0000 | [diff] [blame] | 92 | |
mhahnenberg@apple.com | 8b5cfd3 | 2012-04-20 00:05:37 +0000 | [diff] [blame] | 93 | ASSERT(!m_heap->shouldCollect()); |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 94 | |
ggaren@apple.com | 6159e5f | 2012-09-11 03:02:46 +0000 | [diff] [blame] | 95 | MarkedBlock* block = allocateBlock(bytes); |
mhahnenberg@apple.com | 3e7e4e0 | 2012-05-09 02:44:39 +0000 | [diff] [blame] | 96 | ASSERT(block); |
| 97 | addBlock(block); |
| 98 | |
ggaren@apple.com | 6159e5f | 2012-09-11 03:02:46 +0000 | [diff] [blame] | 99 | result = tryAllocate(bytes); |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 100 | ASSERT(result); |
| 101 | return result; |
| 102 | } |
ggaren@apple.com | a68a650 | 2012-05-22 23:59:51 +0000 | [diff] [blame] | 103 | |
ggaren@apple.com | 6159e5f | 2012-09-11 03:02:46 +0000 | [diff] [blame] | 104 | MarkedBlock* MarkedAllocator::allocateBlock(size_t bytes) |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 105 | { |
ggaren@apple.com | 6159e5f | 2012-09-11 03:02:46 +0000 | [diff] [blame] | 106 | size_t minBlockSize = MarkedBlock::blockSize; |
| 107 | size_t minAllocationSize = WTF::roundUpToMultipleOf(WTF::pageSize(), sizeof(MarkedBlock) + bytes); |
| 108 | size_t blockSize = std::max(minBlockSize, minAllocationSize); |
| 109 | |
| 110 | size_t cellSize = m_cellSize ? m_cellSize : WTF::roundUpToMultipleOf<MarkedBlock::atomSize>(bytes); |
| 111 | |
mhahnenberg@apple.com | 5c018e7 | 2012-10-12 03:10:30 +0000 | [diff] [blame] | 112 | if (blockSize == MarkedBlock::blockSize) |
| 113 | return MarkedBlock::create(m_heap->blockAllocator().allocate<MarkedBlock>(), this, cellSize, m_destructorType); |
| 114 | return MarkedBlock::create(m_heap->blockAllocator().allocateCustomSize(blockSize, MarkedBlock::blockSize), this, cellSize, m_destructorType); |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void MarkedAllocator::addBlock(MarkedBlock* block) |
| 118 | { |
| 119 | ASSERT(!m_currentBlock); |
mhahnenberg@apple.com | 8b5cfd3 | 2012-04-20 00:05:37 +0000 | [diff] [blame] | 120 | ASSERT(!m_freeList.head); |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 121 | |
| 122 | m_blockList.append(block); |
mhahnenberg@apple.com | 7f5b959 | 2012-07-27 22:59:14 +0000 | [diff] [blame] | 123 | m_blocksToSweep = m_currentBlock = block; |
mhahnenberg@apple.com | 8b5cfd3 | 2012-04-20 00:05:37 +0000 | [diff] [blame] | 124 | m_freeList = block->sweep(MarkedBlock::SweepToFreeList); |
ggaren@apple.com | 6159e5f | 2012-09-11 03:02:46 +0000 | [diff] [blame] | 125 | m_markedSpace->didAddBlock(block); |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void MarkedAllocator::removeBlock(MarkedBlock* block) |
| 129 | { |
ggaren@apple.com | 4b67d0d | 2012-06-21 02:00:08 +0000 | [diff] [blame] | 130 | if (m_currentBlock == block) { |
ggaren@apple.com | dd7793a | 2012-07-31 21:26:38 +0000 | [diff] [blame] | 131 | m_currentBlock = m_currentBlock->next(); |
ggaren@apple.com | 4b67d0d | 2012-06-21 02:00:08 +0000 | [diff] [blame] | 132 | m_freeList = MarkedBlock::FreeList(); |
| 133 | } |
mhahnenberg@apple.com | 7f5b959 | 2012-07-27 22:59:14 +0000 | [diff] [blame] | 134 | if (m_blocksToSweep == block) |
ggaren@apple.com | dd7793a | 2012-07-31 21:26:38 +0000 | [diff] [blame] | 135 | m_blocksToSweep = m_blocksToSweep->next(); |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 136 | m_blockList.remove(block); |
| 137 | } |
| 138 | |
| 139 | } // namespace JSC |