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