aed147f0c7aff96c36cfa1289b5adfd29baae572
[WebKit-https.git] / Source / JavaScriptCore / heap / WeakBlock.cpp
1 /*
2  * Copyright (C) 2012, 2016 Apple Inc. All rights reserved.
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. 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.
24  */
25
26 #include "config.h"
27 #include "WeakBlock.h"
28
29 #include "CellContainerInlines.h"
30 #include "Heap.h"
31 #include "HeapRootVisitor.h"
32 #include "JSCInlines.h"
33 #include "JSObject.h"
34 #include "WeakHandleOwner.h"
35
36 namespace JSC {
37
38 WeakBlock* WeakBlock::create(Heap& heap, CellContainer container)
39 {
40     heap.didAllocateBlock(WeakBlock::blockSize);
41     return new (NotNull, fastMalloc(blockSize)) WeakBlock(container);
42 }
43
44 void WeakBlock::destroy(Heap& heap, WeakBlock* block)
45 {
46     block->~WeakBlock();
47     fastFree(block);
48     heap.didFreeBlock(WeakBlock::blockSize);
49 }
50
51 WeakBlock::WeakBlock(CellContainer container)
52     : DoublyLinkedListNode<WeakBlock>()
53     , m_container(container)
54 {
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);
59     }
60
61     ASSERT(isEmpty());
62 }
63
64 void WeakBlock::lastChanceToFinalize()
65 {
66     for (size_t i = 0; i < weakImplCount(); ++i) {
67         WeakImpl* weakImpl = &weakImpls()[i];
68         if (weakImpl->state() >= WeakImpl::Finalized)
69             continue;
70         weakImpl->setState(WeakImpl::Dead);
71         finalize(weakImpl);
72     }
73 }
74
75 void WeakBlock::sweep()
76 {
77     // If a block is completely empty, a sweep won't have any effect.
78     if (isEmpty())
79         return;
80
81     SweepResult sweepResult;
82     for (size_t i = 0; i < weakImplCount(); ++i) {
83         WeakImpl* weakImpl = &weakImpls()[i];
84         if (weakImpl->state() == WeakImpl::Dead)
85             finalize(weakImpl);
86         if (weakImpl->state() == WeakImpl::Deallocated)
87             addToFreeList(&sweepResult.freeList, weakImpl);
88         else {
89             sweepResult.blockIsFree = false;
90             if (weakImpl->state() == WeakImpl::Live)
91                 sweepResult.blockIsLogicallyEmpty = false;
92         }
93     }
94
95     m_sweepResult = sweepResult;
96     ASSERT(!m_sweepResult.isNull());
97 }
98
99 template<typename ContainerType>
100 void WeakBlock::specializedVisit(ContainerType& container, HeapRootVisitor& heapRootVisitor)
101 {
102     SlotVisitor& visitor = heapRootVisitor.visitor();
103     
104     HeapVersion version = visitor.version();
105
106     for (size_t i = 0; i < weakImplCount(); ++i) {
107         WeakImpl* weakImpl = &weakImpls()[i];
108         if (weakImpl->state() != WeakImpl::Live)
109             continue;
110
111         WeakHandleOwner* weakHandleOwner = weakImpl->weakHandleOwner();
112         if (!weakHandleOwner)
113             continue;
114
115         const JSValue& jsValue = weakImpl->jsValue();
116         if (container.isMarkedConcurrently(version, jsValue.asCell()))
117             continue;
118         
119         if (!weakHandleOwner->isReachableFromOpaqueRoots(Handle<Unknown>::wrapSlot(&const_cast<JSValue&>(jsValue)), weakImpl->context(), visitor))
120             continue;
121
122         heapRootVisitor.visit(&const_cast<JSValue&>(jsValue));
123     }
124 }
125
126 void WeakBlock::visit(HeapRootVisitor& heapRootVisitor)
127 {
128     // If a block is completely empty, a visit won't have any effect.
129     if (isEmpty())
130         return;
131
132     // If this WeakBlock doesn't belong to a CellContainer, we won't even be here.
133     ASSERT(m_container);
134     
135     if (m_container.isLargeAllocation())
136         specializedVisit(m_container.largeAllocation(), heapRootVisitor);
137     else
138         specializedVisit(m_container.markedBlock(), heapRootVisitor);
139 }
140
141 void WeakBlock::reap()
142 {
143     // If a block is completely empty, a reaping won't have any effect.
144     if (isEmpty())
145         return;
146
147     // If this WeakBlock doesn't belong to a CellContainer, we won't even be here.
148     ASSERT(m_container);
149     
150     HeapVersion version = m_container.heap()->objectSpace().version();
151
152     for (size_t i = 0; i < weakImplCount(); ++i) {
153         WeakImpl* weakImpl = &weakImpls()[i];
154         if (weakImpl->state() > WeakImpl::Dead)
155             continue;
156
157         if (m_container.isMarked(version, weakImpl->jsValue().asCell())) {
158             ASSERT(weakImpl->state() == WeakImpl::Live);
159             continue;
160         }
161
162         weakImpl->setState(WeakImpl::Dead);
163     }
164 }
165
166 } // namespace JSC