2 * Copyright (C) 2011 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 "HandleHeap.h"
29 #include "HeapRootVisitor.h"
34 WeakHandleOwner::~WeakHandleOwner()
38 bool WeakHandleOwner::isReachableFromOpaqueRoots(Handle<Unknown>, void*, SlotVisitor&)
43 void WeakHandleOwner::finalize(Handle<Unknown>, void*)
47 HandleHeap::HandleHeap(JSGlobalData* globalData)
48 : m_globalData(globalData)
54 void HandleHeap::grow()
56 Node* block = m_blockStack.grow();
57 for (int i = m_blockStack.blockLength - 1; i >= 0; --i) {
58 Node* node = &block[i];
59 new (node) Node(this);
60 m_freeList.push(node);
64 void HandleHeap::visitStrongHandles(HeapRootVisitor& heapRootVisitor)
66 Node* end = m_strongList.end();
67 for (Node* node = m_strongList.begin(); node != end; node = node->next()) {
68 #if ENABLE(GC_VALIDATION)
69 if (!isLiveNode(node))
72 heapRootVisitor.visit(node->slot());
76 void HandleHeap::visitWeakHandles(HeapRootVisitor& heapRootVisitor)
78 SlotVisitor& visitor = heapRootVisitor.visitor();
80 Node* end = m_weakList.end();
81 for (Node* node = m_weakList.begin(); node != end; node = node->next()) {
82 #if ENABLE(GC_VALIDATION)
83 if (!isValidWeakNode(node))
86 JSCell* cell = node->slot()->asCell();
87 if (Heap::isMarked(cell))
90 WeakHandleOwner* weakOwner = node->weakOwner();
94 if (!weakOwner->isReachableFromOpaqueRoots(Handle<Unknown>::wrapSlot(node->slot()), node->weakOwnerContext(), visitor))
97 heapRootVisitor.visit(node->slot());
101 void HandleHeap::finalizeWeakHandles()
103 Node* end = m_weakList.end();
104 for (Node* node = m_weakList.begin(); node != end; node = m_nextToFinalize) {
105 m_nextToFinalize = node->next();
106 #if ENABLE(GC_VALIDATION)
107 if (!isValidWeakNode(node))
111 JSCell* cell = node->slot()->asCell();
112 if (Heap::isMarked(cell))
115 if (WeakHandleOwner* weakOwner = node->weakOwner()) {
116 weakOwner->finalize(Handle<Unknown>::wrapSlot(node->slot()), node->weakOwnerContext());
117 if (m_nextToFinalize != node->next()) // Owner deallocated node.
120 #if ENABLE(GC_VALIDATION)
121 if (!isLiveNode(node))
124 *node->slot() = JSValue();
125 SentinelLinkedList<Node>::remove(node);
126 m_immediateList.push(node);
129 m_nextToFinalize = 0;
132 void HandleHeap::writeBarrier(HandleSlot slot, const JSValue& value)
134 // Forbid assignment to handles during the finalization phase, since it would violate many GC invariants.
135 // File a bug with stack trace if you hit this.
136 if (m_nextToFinalize)
139 if (!value == !*slot && slot->isCell() == value.isCell())
142 Node* node = toNode(slot);
143 #if ENABLE(GC_VALIDATION)
144 if (!isLiveNode(node))
147 SentinelLinkedList<Node>::remove(node);
148 if (!value || !value.isCell()) {
149 m_immediateList.push(node);
153 if (node->isWeak()) {
154 m_weakList.push(node);
155 #if ENABLE(GC_VALIDATION)
156 if (!isLiveNode(node))
162 m_strongList.push(node);
163 #if ENABLE(GC_VALIDATION)
164 if (!isLiveNode(node))
169 unsigned HandleHeap::protectedGlobalObjectCount()
172 Node* end = m_strongList.end();
173 for (Node* node = m_strongList.begin(); node != end; node = node->next()) {
174 JSValue value = *node->slot();
175 if (value.isObject() && asObject(value.asCell())->isGlobalObject())
181 #if ENABLE(GC_VALIDATION) || !ASSERT_DISABLED
182 bool HandleHeap::isLiveNode(Node* node)
184 if (node->prev()->next() != node)
186 if (node->next()->prev() != node)
192 bool HandleHeap::isValidWeakNode(Node* node)
194 if (!isLiveNode(node))
199 JSValue value = *node->slot();
200 if (!value || !value.isCell())
203 JSCell* cell = value.asCell();
204 if (!cell || !cell->structure())