Visiting a WeakBlock should report bytes visited, since we reported them allocated.
[WebKit-https.git] / Source / JavaScriptCore / heap / WeakBlock.cpp
1 /*
2  * Copyright (C) 2012 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 "Heap.h"
30 #include "HeapRootVisitor.h"
31 #include "JSCInlines.h"
32 #include "JSObject.h"
33 #include "WeakHandleOwner.h"
34
35 namespace JSC {
36
37 WeakBlock* WeakBlock::create(Heap& heap, MarkedBlock& markedBlock)
38 {
39     heap.didAllocateBlock(WeakBlock::blockSize);
40     return new (NotNull, fastMalloc(blockSize)) WeakBlock(markedBlock);
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(MarkedBlock& markedBlock)
51     : DoublyLinkedListNode<WeakBlock>()
52     , m_markedBlock(&markedBlock)
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 void WeakBlock::visit(HeapRootVisitor& heapRootVisitor)
99 {
100     // If a block is completely empty, a visit won't have any effect.
101     if (isEmpty())
102         return;
103
104     // If this WeakBlock doesn't belong to a MarkedBlock, we won't even be here.
105     ASSERT(m_markedBlock);
106
107     // We only visit after marking.
108     ASSERT(m_markedBlock->isMarkedOrRetired());
109
110     SlotVisitor& visitor = heapRootVisitor.visitor();
111
112     visitor.reportMemoryVisited(WeakBlock::blockSize);
113
114     for (size_t i = 0; i < weakImplCount(); ++i) {
115         WeakImpl* weakImpl = &weakImpls()[i];
116         if (weakImpl->state() != WeakImpl::Live)
117             continue;
118
119         const JSValue& jsValue = weakImpl->jsValue();
120         if (m_markedBlock->isMarkedOrNewlyAllocated(jsValue.asCell()))
121             continue;
122
123         WeakHandleOwner* weakHandleOwner = weakImpl->weakHandleOwner();
124         if (!weakHandleOwner)
125             continue;
126
127         if (!weakHandleOwner->isReachableFromOpaqueRoots(Handle<Unknown>::wrapSlot(&const_cast<JSValue&>(jsValue)), weakImpl->context(), visitor))
128             continue;
129
130         heapRootVisitor.visit(&const_cast<JSValue&>(jsValue));
131     }
132 }
133
134 void WeakBlock::reap()
135 {
136     // If a block is completely empty, a reaping won't have any effect.
137     if (isEmpty())
138         return;
139
140     // If this WeakBlock doesn't belong to a MarkedBlock, we won't even be here.
141     ASSERT(m_markedBlock);
142
143     // We only reap after marking.
144     ASSERT(m_markedBlock->isMarkedOrRetired());
145
146     for (size_t i = 0; i < weakImplCount(); ++i) {
147         WeakImpl* weakImpl = &weakImpls()[i];
148         if (weakImpl->state() > WeakImpl::Dead)
149             continue;
150
151         if (m_markedBlock->isMarkedOrNewlyAllocated(weakImpl->jsValue().asCell())) {
152             ASSERT(weakImpl->state() == WeakImpl::Live);
153             continue;
154         }
155
156         weakImpl->setState(WeakImpl::Dead);
157     }
158 }
159
160 } // namespace JSC