fpizlo@apple.com | b426f86 | 2014-02-10 02:51:13 +0000 | [diff] [blame] | 1 | /* |
fpizlo@apple.com | aeddff9 | 2016-07-18 21:33:45 +0000 | [diff] [blame] | 2 | * Copyright (C) 2012, 2013, 2016 Apple Inc. All rights reserved. |
fpizlo@apple.com | b426f86 | 2014-02-10 02:51:13 +0000 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 26 | #include "config.h" |
| 27 | #include "MarkedAllocator.h" |
| 28 | |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame^] | 29 | #include "AllocatingScope.h" |
mhahnenberg@apple.com | 2b64eec0 | 2012-04-18 16:18:32 +0000 | [diff] [blame] | 30 | #include "GCActivityCallback.h" |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 31 | #include "Heap.h" |
mhahnenberg@apple.com | 59c64f1 | 2012-07-31 23:05:12 +0000 | [diff] [blame] | 32 | #include "IncrementalSweeper.h" |
fpizlo@apple.com | fb7eff2 | 2014-02-11 01:45:50 +0000 | [diff] [blame] | 33 | #include "JSCInlines.h" |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 34 | #include "MarkedBlockInlines.h" |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 35 | #include "SuperSampler.h" |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 36 | #include "VM.h" |
mhahnenberg@apple.com | 2e132e4 | 2012-05-03 00:14:05 +0000 | [diff] [blame] | 37 | #include <wtf/CurrentTime.h> |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 38 | |
| 39 | namespace JSC { |
| 40 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 41 | MarkedAllocator::MarkedAllocator(Heap* heap, MarkedSpace* markedSpace, size_t cellSize, const AllocatorAttributes& attributes) |
| 42 | : m_currentBlock(0) |
| 43 | , m_lastActiveBlock(0) |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 44 | , m_cellSize(static_cast<unsigned>(cellSize)) |
| 45 | , m_attributes(attributes) |
| 46 | , m_heap(heap) |
| 47 | , m_markedSpace(markedSpace) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | bool MarkedAllocator::isPagedOut(double deadline) |
mhahnenberg@apple.com | 2e132e4 | 2012-05-03 00:14:05 +0000 | [diff] [blame] | 52 | { |
| 53 | unsigned itersSinceLastTimeCheck = 0; |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 54 | for (size_t index = 0; index < m_blocks.size(); ++index) { |
| 55 | MarkedBlock::Handle* block = m_blocks[index]; |
fpizlo@apple.com | 7b23164 | 2016-10-11 23:52:02 +0000 | [diff] [blame] | 56 | if (block) |
| 57 | block->block().updateNeedsDestruction(); |
mhahnenberg@apple.com | 2e132e4 | 2012-05-03 00:14:05 +0000 | [diff] [blame] | 58 | ++itersSinceLastTimeCheck; |
| 59 | if (itersSinceLastTimeCheck >= Heap::s_timeCheckResolution) { |
| 60 | double currentTime = WTF::monotonicallyIncreasingTime(); |
| 61 | if (currentTime > deadline) |
| 62 | return true; |
| 63 | itersSinceLastTimeCheck = 0; |
| 64 | } |
| 65 | } |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 66 | return false; |
| 67 | } |
mhahnenberg@apple.com | 2e132e4 | 2012-05-03 00:14:05 +0000 | [diff] [blame] | 68 | |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 69 | bool MarkedAllocator::shouldStealEmptyBlocksFromOtherAllocators() const |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 70 | { |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 71 | return !needsDestruction(); |
ggaren@apple.com | 7535d92 | 2016-06-21 23:06:08 +0000 | [diff] [blame] | 72 | } |
| 73 | |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 74 | MarkedBlock::Handle* MarkedAllocator::findEmptyBlockToSteal() |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 75 | { |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 76 | // Don't allow others to steal from us, if we wouldn't steal from others. |
| 77 | if (!shouldStealEmptyBlocksFromOtherAllocators()) |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 78 | return nullptr; |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 79 | |
| 80 | m_emptyCursor = m_empty.findBit(m_emptyCursor, true); |
| 81 | if (m_emptyCursor >= m_blocks.size()) |
| 82 | return nullptr; |
| 83 | return m_blocks[m_emptyCursor]; |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 84 | } |
| 85 | |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 86 | void MarkedAllocator::didConsumeFreeList() |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 87 | { |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 88 | if (m_currentBlock) |
| 89 | m_currentBlock->didConsumeFreeList(); |
| 90 | |
| 91 | setFreeList(FreeList()); |
| 92 | m_currentBlock = nullptr; |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 93 | } |
| 94 | |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 95 | void* MarkedAllocator::tryAllocateWithoutCollecting() |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 96 | { |
| 97 | SuperSamplerScope superSamplerScope(false); |
| 98 | |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 99 | ASSERT(!m_currentBlock); |
| 100 | ASSERT(!m_freeList); |
| 101 | |
| 102 | for (;;) { |
| 103 | m_allocationCursor = (m_canAllocateButNotEmpty | m_empty).findBit(m_allocationCursor, true); |
| 104 | if (m_allocationCursor >= m_blocks.size()) |
| 105 | break; |
| 106 | |
| 107 | setIsCanAllocateButNotEmpty(m_allocationCursor, false); |
| 108 | |
| 109 | if (void* result = tryAllocateIn(m_blocks[m_allocationCursor])) |
| 110 | return result; |
msaboff@apple.com | 84946d4 | 2015-02-06 01:12:00 +0000 | [diff] [blame] | 111 | } |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 112 | |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 113 | if (Options::stealEmptyBlocksFromOtherAllocators() |
| 114 | && shouldStealEmptyBlocksFromOtherAllocators()) { |
| 115 | if (MarkedBlock::Handle* block = m_markedSpace->findEmptyBlockToSteal()) { |
| 116 | block->sweep(); |
| 117 | |
| 118 | // It's good that this clears canAllocateButNotEmpty as well as all other bits, |
| 119 | // because there is a remote chance that a block may have both canAllocateButNotEmpty |
| 120 | // and empty set at the same time. |
| 121 | block->removeFromAllocator(); |
| 122 | addBlock(block); |
| 123 | return allocateIn(block); |
msaboff@apple.com | 84946d4 | 2015-02-06 01:12:00 +0000 | [diff] [blame] | 124 | } |
msaboff@apple.com | 84946d4 | 2015-02-06 01:12:00 +0000 | [diff] [blame] | 125 | } |
| 126 | |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 127 | return nullptr; |
| 128 | } |
mhahnenberg@apple.com | 3f62e72 | 2013-12-19 04:30:02 +0000 | [diff] [blame] | 129 | |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 130 | void* MarkedAllocator::allocateIn(MarkedBlock::Handle* block) |
| 131 | { |
| 132 | void* result = tryAllocateIn(block); |
| 133 | RELEASE_ASSERT(result); |
| 134 | return result; |
| 135 | } |
| 136 | |
| 137 | void* MarkedAllocator::tryAllocateIn(MarkedBlock::Handle* block) |
| 138 | { |
| 139 | ASSERT(block); |
| 140 | ASSERT(!block->hasAnyNewlyAllocated()); |
| 141 | ASSERT(!block->isFreeListed()); |
| 142 | |
| 143 | FreeList freeList = block->sweep(MarkedBlock::Handle::SweepToFreeList); |
| 144 | |
| 145 | // It's possible to stumble on a completely full block. Marking tries to retire these, but |
| 146 | // that algorithm is racy and may forget to do it sometimes. |
| 147 | if (freeList.allocationWillFail()) { |
| 148 | ASSERT(block->isFreeListed()); |
| 149 | block->unsweepWithNoNewlyAllocated(); |
| 150 | ASSERT(!block->isFreeListed()); |
| 151 | ASSERT(!isEmpty(block)); |
| 152 | ASSERT(!isCanAllocateButNotEmpty(block)); |
| 153 | return nullptr; |
| 154 | } |
| 155 | |
| 156 | m_currentBlock = block; |
| 157 | setFreeList(freeList); |
| 158 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 159 | void* result; |
| 160 | if (m_freeList.remaining) { |
| 161 | unsigned cellSize = m_cellSize; |
| 162 | m_freeList.remaining -= cellSize; |
| 163 | result = m_freeList.payloadEnd - m_freeList.remaining - cellSize; |
| 164 | } else { |
| 165 | FreeCell* head = m_freeList.head; |
| 166 | m_freeList.head = head->next; |
| 167 | result = head; |
| 168 | } |
| 169 | RELEASE_ASSERT(result); |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 170 | setIsEden(m_currentBlock, true); |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 171 | m_markedSpace->didAllocateInBlock(m_currentBlock); |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 172 | return result; |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 173 | } |
mhahnenberg@apple.com | fa811df | 2014-02-13 04:24:41 +0000 | [diff] [blame] | 174 | |
fpizlo@apple.com | 9a17595 | 2016-09-28 21:55:53 +0000 | [diff] [blame] | 175 | ALWAYS_INLINE void MarkedAllocator::doTestCollectionsIfNeeded(GCDeferralContext* deferralContext) |
mark.lam@apple.com | a2a9f66 | 2014-04-24 21:12:56 +0000 | [diff] [blame] | 176 | { |
| 177 | if (!Options::slowPathAllocsBetweenGCs()) |
| 178 | return; |
| 179 | |
| 180 | static unsigned allocationCount = 0; |
| 181 | if (!allocationCount) { |
fpizlo@apple.com | 9a17595 | 2016-09-28 21:55:53 +0000 | [diff] [blame] | 182 | if (!m_heap->isDeferred()) { |
| 183 | if (deferralContext) |
| 184 | deferralContext->m_shouldGC = true; |
| 185 | else |
| 186 | m_heap->collectAllGarbage(); |
| 187 | } |
mark.lam@apple.com | a2a9f66 | 2014-04-24 21:12:56 +0000 | [diff] [blame] | 188 | } |
| 189 | if (++allocationCount >= Options::slowPathAllocsBetweenGCs()) |
| 190 | allocationCount = 0; |
| 191 | } |
| 192 | |
fpizlo@apple.com | 9a17595 | 2016-09-28 21:55:53 +0000 | [diff] [blame] | 193 | void* MarkedAllocator::allocateSlowCase(GCDeferralContext* deferralContext) |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 194 | { |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 195 | bool crashOnFailure = true; |
fpizlo@apple.com | 9a17595 | 2016-09-28 21:55:53 +0000 | [diff] [blame] | 196 | return allocateSlowCaseImpl(deferralContext, crashOnFailure); |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 197 | } |
| 198 | |
fpizlo@apple.com | 9a17595 | 2016-09-28 21:55:53 +0000 | [diff] [blame] | 199 | void* MarkedAllocator::tryAllocateSlowCase(GCDeferralContext* deferralContext) |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 200 | { |
| 201 | bool crashOnFailure = false; |
fpizlo@apple.com | 9a17595 | 2016-09-28 21:55:53 +0000 | [diff] [blame] | 202 | return allocateSlowCaseImpl(deferralContext, crashOnFailure); |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 203 | } |
| 204 | |
fpizlo@apple.com | 9a17595 | 2016-09-28 21:55:53 +0000 | [diff] [blame] | 205 | void* MarkedAllocator::allocateSlowCaseImpl(GCDeferralContext* deferralContext, bool crashOnFailure) |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 206 | { |
| 207 | SuperSamplerScope superSamplerScope(false); |
andersca@apple.com | b987aae | 2013-07-26 00:13:13 +0000 | [diff] [blame] | 208 | ASSERT(m_heap->vm()->currentThreadIsHoldingAPILock()); |
fpizlo@apple.com | 9a17595 | 2016-09-28 21:55:53 +0000 | [diff] [blame] | 209 | doTestCollectionsIfNeeded(deferralContext); |
mark.lam@apple.com | a2a9f66 | 2014-04-24 21:12:56 +0000 | [diff] [blame] | 210 | |
mhahnenberg@apple.com | bee96a3 | 2013-09-16 19:48:48 +0000 | [diff] [blame] | 211 | ASSERT(!m_markedSpace->isIterating()); |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 212 | m_heap->didAllocate(m_freeList.originalSize); |
mhahnenberg@apple.com | 2b64eec0 | 2012-04-18 16:18:32 +0000 | [diff] [blame] | 213 | |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 214 | didConsumeFreeList(); |
| 215 | |
fpizlo@apple.com | ea379af | 2016-10-21 02:17:35 +0000 | [diff] [blame^] | 216 | AllocatingScope healpingHeap(*m_heap); |
| 217 | |
fpizlo@apple.com | 9a17595 | 2016-09-28 21:55:53 +0000 | [diff] [blame] | 218 | m_heap->collectIfNecessaryOrDefer(deferralContext); |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 219 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 220 | void* result = tryAllocateWithoutCollecting(); |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 221 | |
| 222 | if (LIKELY(result != 0)) |
| 223 | return result; |
| 224 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 225 | MarkedBlock::Handle* block = tryAllocateBlock(); |
| 226 | if (!block) { |
| 227 | if (crashOnFailure) |
| 228 | RELEASE_ASSERT_NOT_REACHED(); |
| 229 | else |
| 230 | return nullptr; |
| 231 | } |
mhahnenberg@apple.com | 3e7e4e0 | 2012-05-09 02:44:39 +0000 | [diff] [blame] | 232 | addBlock(block); |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 233 | result = allocateIn(block); |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 234 | ASSERT(result); |
| 235 | return result; |
| 236 | } |
ggaren@apple.com | a68a650 | 2012-05-22 23:59:51 +0000 | [diff] [blame] | 237 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 238 | static size_t blockHeaderSize() |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 239 | { |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 240 | return WTF::roundUpToMultipleOf<MarkedBlock::atomSize>(sizeof(MarkedBlock)); |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 241 | } |
| 242 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 243 | size_t MarkedAllocator::blockSizeForBytes(size_t bytes) |
| 244 | { |
| 245 | size_t minBlockSize = MarkedBlock::blockSize; |
| 246 | size_t minAllocationSize = blockHeaderSize() + WTF::roundUpToMultipleOf<MarkedBlock::atomSize>(bytes); |
| 247 | minAllocationSize = WTF::roundUpToMultipleOf(WTF::pageSize(), minAllocationSize); |
| 248 | return std::max(minBlockSize, minAllocationSize); |
| 249 | } |
| 250 | |
| 251 | MarkedBlock::Handle* MarkedAllocator::tryAllocateBlock() |
| 252 | { |
| 253 | SuperSamplerScope superSamplerScope(false); |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 254 | |
| 255 | MarkedBlock::Handle* handle = MarkedBlock::tryCreate(*m_heap); |
| 256 | if (!handle) |
| 257 | return nullptr; |
| 258 | |
| 259 | m_markedSpace->didAddBlock(handle); |
| 260 | |
| 261 | return handle; |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | void MarkedAllocator::addBlock(MarkedBlock::Handle* block) |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 265 | { |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 266 | size_t index; |
| 267 | if (m_freeBlockIndices.isEmpty()) { |
| 268 | index = m_blocks.size(); |
| 269 | |
| 270 | size_t oldCapacity = m_blocks.capacity(); |
| 271 | m_blocks.append(block); |
| 272 | if (m_blocks.capacity() != oldCapacity) { |
| 273 | forEachBitVector( |
| 274 | [&] (FastBitVector& vector) { |
| 275 | ASSERT_UNUSED(vector, vector.numBits() == oldCapacity); |
| 276 | }); |
| 277 | |
| 278 | ASSERT(m_blocks.capacity() > oldCapacity); |
| 279 | |
| 280 | forEachBitVector( |
| 281 | [&] (FastBitVector& vector) { |
| 282 | vector.resize(m_blocks.capacity()); |
| 283 | }); |
| 284 | } |
| 285 | } else { |
| 286 | index = m_freeBlockIndices.takeLast(); |
| 287 | ASSERT(!m_blocks[index]); |
| 288 | m_blocks[index] = block; |
| 289 | } |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 290 | |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 291 | forEachBitVector( |
| 292 | [&] (FastBitVector& vector) { |
| 293 | ASSERT_UNUSED(vector, !vector[index]); |
| 294 | }); |
| 295 | |
| 296 | // This is the point at which the block learns of its cellSize() and attributes(). |
| 297 | block->didAddToAllocator(this, index); |
| 298 | |
| 299 | setIsLive(index, true); |
| 300 | setIsEmpty(index, true); |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 301 | } |
| 302 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 303 | void MarkedAllocator::removeBlock(MarkedBlock::Handle* block) |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 304 | { |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 305 | ASSERT(block->allocator() == this); |
| 306 | ASSERT(m_blocks[block->index()] == block); |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 307 | |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 308 | m_blocks[block->index()] = nullptr; |
| 309 | m_freeBlockIndices.append(block->index()); |
| 310 | |
| 311 | forEachBitVector( |
| 312 | [&] (FastBitVector& vector) { |
| 313 | vector[block->index()] = false; |
| 314 | }); |
| 315 | |
| 316 | block->didRemoveFromAllocator(); |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 317 | } |
| 318 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 319 | void MarkedAllocator::stopAllocating() |
| 320 | { |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 321 | if (false) |
| 322 | dataLog(RawPointer(this), ": MarkedAllocator::stopAllocating!\n"); |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 323 | ASSERT(!m_lastActiveBlock); |
| 324 | if (!m_currentBlock) { |
| 325 | ASSERT(!m_freeList); |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | m_currentBlock->stopAllocating(m_freeList); |
| 330 | m_lastActiveBlock = m_currentBlock; |
| 331 | m_currentBlock = 0; |
| 332 | m_freeList = FreeList(); |
| 333 | } |
| 334 | |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 335 | void MarkedAllocator::prepareForAllocation() |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 336 | { |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 337 | m_lastActiveBlock = nullptr; |
| 338 | m_currentBlock = nullptr; |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 339 | setFreeList(FreeList()); |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 340 | |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 341 | m_allocationCursor = 0; |
| 342 | m_emptyCursor = 0; |
| 343 | m_unsweptCursor = 0; |
| 344 | |
| 345 | m_eden.clearAll(); |
ggaren@apple.com | 7535d92 | 2016-06-21 23:06:08 +0000 | [diff] [blame] | 346 | |
| 347 | if (UNLIKELY(Options::useImmortalObjects())) { |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 348 | // FIXME: Make this work again. |
| 349 | // https://bugs.webkit.org/show_bug.cgi?id=162296 |
| 350 | RELEASE_ASSERT_NOT_REACHED(); |
ggaren@apple.com | 7535d92 | 2016-06-21 23:06:08 +0000 | [diff] [blame] | 351 | } |
mhahnenberg@apple.com | 3ddd7ac | 2014-01-10 02:28:27 +0000 | [diff] [blame] | 352 | } |
| 353 | |
mhahnenberg@apple.com | 96b8cd1 | 2014-03-12 18:17:43 +0000 | [diff] [blame] | 354 | void MarkedAllocator::lastChanceToFinalize() |
| 355 | { |
fpizlo@apple.com | aeddff9 | 2016-07-18 21:33:45 +0000 | [diff] [blame] | 356 | forEachBlock( |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 357 | [&] (MarkedBlock::Handle* block) { |
fpizlo@apple.com | aeddff9 | 2016-07-18 21:33:45 +0000 | [diff] [blame] | 358 | block->lastChanceToFinalize(); |
| 359 | }); |
mhahnenberg@apple.com | 96b8cd1 | 2014-03-12 18:17:43 +0000 | [diff] [blame] | 360 | } |
| 361 | |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 362 | void MarkedAllocator::setFreeList(const FreeList& freeList) |
| 363 | { |
| 364 | m_freeList = freeList; |
| 365 | } |
| 366 | |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 367 | void MarkedAllocator::resumeAllocating() |
| 368 | { |
| 369 | if (!m_lastActiveBlock) |
| 370 | return; |
| 371 | |
| 372 | m_freeList = m_lastActiveBlock->resumeAllocating(); |
| 373 | m_currentBlock = m_lastActiveBlock; |
| 374 | m_lastActiveBlock = nullptr; |
| 375 | } |
| 376 | |
| 377 | void MarkedAllocator::beginMarkingForFullCollection() |
| 378 | { |
| 379 | // Mark bits are sticky and so is our summary of mark bits. We only clear these during full |
| 380 | // collections, so if you survived the last collection you will survive the next one so long |
| 381 | // as the next one is eden. |
| 382 | m_markingNotEmpty.clearAll(); |
| 383 | m_markingRetired.clearAll(); |
| 384 | } |
| 385 | |
| 386 | void MarkedAllocator::endMarking() |
| 387 | { |
| 388 | m_allocated.clearAll(); |
| 389 | |
| 390 | // It's surprising and frustrating to comprehend, but the end-of-marking flip does not need to |
| 391 | // know what kind of collection it is. That knowledge is already encoded in the m_markingXYZ |
| 392 | // vectors. |
| 393 | |
| 394 | if (needsDestruction()) { |
| 395 | // If blocks need destruction then nothing is empty! This is a correct assertion but may |
| 396 | // become wrong once we go full concurrent: when we create a new block, it will flicker |
| 397 | // into the empty set for a tiny moment. On the other hand, this code is likely to be run |
| 398 | // in stopTheWorld. |
| 399 | ASSERT(m_empty.isEmpty()); |
| 400 | m_canAllocateButNotEmpty = m_live & ~m_markingRetired; |
| 401 | return; |
| 402 | } |
| 403 | |
| 404 | m_empty = m_live & ~m_markingNotEmpty; |
| 405 | m_canAllocateButNotEmpty = m_live & m_markingNotEmpty & ~m_markingRetired; |
| 406 | |
| 407 | if (false) { |
| 408 | dataLog("Bits for ", m_cellSize, ", ", m_attributes, " after endMarking:\n"); |
| 409 | dumpBits(WTF::dataFile()); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | void MarkedAllocator::snapshotUnsweptForEdenCollection() |
| 414 | { |
| 415 | m_unswept |= m_eden; |
| 416 | } |
| 417 | |
| 418 | void MarkedAllocator::snapshotUnsweptForFullCollection() |
| 419 | { |
| 420 | m_unswept = m_live; |
| 421 | } |
| 422 | |
| 423 | MarkedBlock::Handle* MarkedAllocator::findBlockToSweep() |
| 424 | { |
| 425 | m_unsweptCursor = m_unswept.findBit(m_unsweptCursor, true); |
| 426 | if (m_unsweptCursor >= m_blocks.size()) |
| 427 | return nullptr; |
| 428 | return m_blocks[m_unsweptCursor]; |
| 429 | } |
| 430 | |
| 431 | void MarkedAllocator::sweep() |
| 432 | { |
| 433 | m_unswept.forEachSetBit( |
| 434 | [&] (size_t index) { |
fpizlo@apple.com | 7b23164 | 2016-10-11 23:52:02 +0000 | [diff] [blame] | 435 | MarkedBlock::Handle* block = m_blocks[index]; |
| 436 | block->sweep(); |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 437 | }); |
| 438 | } |
| 439 | |
| 440 | void MarkedAllocator::shrink() |
| 441 | { |
| 442 | m_empty.forEachSetBit( |
| 443 | [&] (size_t index) { |
| 444 | m_markedSpace->freeBlock(m_blocks[index]); |
| 445 | }); |
| 446 | } |
| 447 | |
| 448 | void MarkedAllocator::assertNoUnswept() |
| 449 | { |
| 450 | if (ASSERT_DISABLED) |
| 451 | return; |
| 452 | |
| 453 | if (m_unswept.isEmpty()) |
| 454 | return; |
| 455 | |
| 456 | dataLog("Assertion failed: unswept not empty in ", *this, ".\n"); |
| 457 | dumpBits(); |
| 458 | ASSERT_NOT_REACHED(); |
| 459 | } |
| 460 | |
| 461 | void MarkedAllocator::dump(PrintStream& out) const |
| 462 | { |
| 463 | out.print(RawPointer(this), ":", m_cellSize, "/", m_attributes); |
| 464 | } |
| 465 | |
| 466 | void MarkedAllocator::dumpBits(PrintStream& out) |
| 467 | { |
| 468 | unsigned maxNameLength = 0; |
| 469 | forEachBitVectorWithName( |
| 470 | [&] (FastBitVector&, const char* name) { |
| 471 | unsigned length = strlen(name); |
| 472 | maxNameLength = std::max(maxNameLength, length); |
| 473 | }); |
| 474 | |
| 475 | forEachBitVectorWithName( |
| 476 | [&] (FastBitVector& vector, const char* name) { |
| 477 | out.print(" ", name, ": "); |
| 478 | for (unsigned i = maxNameLength - strlen(name); i--;) |
| 479 | out.print(" "); |
| 480 | out.print(vector, "\n"); |
| 481 | }); |
| 482 | } |
| 483 | |
mhahnenberg@apple.com | ce85b93 | 2012-02-03 19:27:57 +0000 | [diff] [blame] | 484 | } // namespace JSC |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 485 | |