2 * Copyright (C) 2012, 2016 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "WeakBlock.h"
29 #include "CellContainerInlines.h"
31 #include "HeapRootVisitor.h"
32 #include "JSCInlines.h"
34 #include "WeakHandleOwner.h"
38 WeakBlock* WeakBlock::create(Heap& heap, CellContainer container)
40 heap.didAllocateBlock(WeakBlock::blockSize);
41 return new (NotNull, fastMalloc(blockSize)) WeakBlock(container);
44 void WeakBlock::destroy(Heap& heap, WeakBlock* block)
48 heap.didFreeBlock(WeakBlock::blockSize);
51 WeakBlock::WeakBlock(CellContainer container)
52 : DoublyLinkedListNode<WeakBlock>()
53 , m_container(container)
55 for (size_t i = 0; i < weakImplCount(); ++i) {
56 WeakImpl* weakImpl = &weakImpls()[i];
57 new (NotNull, weakImpl) WeakImpl;
58 addToFreeList(&m_sweepResult.freeList, weakImpl);
64 void WeakBlock::lastChanceToFinalize()
66 for (size_t i = 0; i < weakImplCount(); ++i) {
67 WeakImpl* weakImpl = &weakImpls()[i];
68 if (weakImpl->state() >= WeakImpl::Finalized)
70 weakImpl->setState(WeakImpl::Dead);
75 void WeakBlock::sweep()
77 // If a block is completely empty, a sweep won't have any effect.
81 SweepResult sweepResult;
82 for (size_t i = 0; i < weakImplCount(); ++i) {
83 WeakImpl* weakImpl = &weakImpls()[i];
84 if (weakImpl->state() == WeakImpl::Dead)
86 if (weakImpl->state() == WeakImpl::Deallocated)
87 addToFreeList(&sweepResult.freeList, weakImpl);
89 sweepResult.blockIsFree = false;
90 if (weakImpl->state() == WeakImpl::Live)
91 sweepResult.blockIsLogicallyEmpty = false;
95 m_sweepResult = sweepResult;
96 ASSERT(!m_sweepResult.isNull());
99 template<typename ContainerType>
100 void WeakBlock::specializedVisit(ContainerType& container, HeapRootVisitor& heapRootVisitor)
102 SlotVisitor& visitor = heapRootVisitor.visitor();
104 HeapVersion markingVersion = visitor.markingVersion();
106 for (size_t i = 0; i < weakImplCount(); ++i) {
107 WeakImpl* weakImpl = &weakImpls()[i];
108 if (weakImpl->state() != WeakImpl::Live)
111 WeakHandleOwner* weakHandleOwner = weakImpl->weakHandleOwner();
112 if (!weakHandleOwner)
115 const JSValue& jsValue = weakImpl->jsValue();
116 if (container.isMarkedConcurrently(markingVersion, jsValue.asCell()))
119 if (!weakHandleOwner->isReachableFromOpaqueRoots(Handle<Unknown>::wrapSlot(&const_cast<JSValue&>(jsValue)), weakImpl->context(), visitor))
122 heapRootVisitor.visit(&const_cast<JSValue&>(jsValue));
126 void WeakBlock::visit(HeapRootVisitor& heapRootVisitor)
128 // If a block is completely empty, a visit won't have any effect.
132 // If this WeakBlock doesn't belong to a CellContainer, we won't even be here.
135 if (m_container.isLargeAllocation())
136 specializedVisit(m_container.largeAllocation(), heapRootVisitor);
138 specializedVisit(m_container.markedBlock(), heapRootVisitor);
141 void WeakBlock::reap()
143 // If a block is completely empty, a reaping won't have any effect.
147 // If this WeakBlock doesn't belong to a CellContainer, we won't even be here.
150 HeapVersion markingVersion = m_container.heap()->objectSpace().markingVersion();
152 for (size_t i = 0; i < weakImplCount(); ++i) {
153 WeakImpl* weakImpl = &weakImpls()[i];
154 if (weakImpl->state() > WeakImpl::Dead)
157 if (m_container.isMarked(markingVersion, weakImpl->jsValue().asCell())) {
158 ASSERT(weakImpl->state() == WeakImpl::Live);
162 weakImpl->setState(WeakImpl::Dead);