initial import
[vuplus_webkit] / Source / JavaScriptCore / heap / HandleHeap.cpp
1 /*
2  * Copyright (C) 2011 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 "HandleHeap.h"
28
29 #include "HeapRootVisitor.h"
30 #include "JSObject.h"
31
32 namespace JSC {
33
34 WeakHandleOwner::~WeakHandleOwner()
35 {
36 }
37
38 bool WeakHandleOwner::isReachableFromOpaqueRoots(Handle<Unknown>, void*, SlotVisitor&)
39 {
40     return false;
41 }
42
43 void WeakHandleOwner::finalize(Handle<Unknown>, void*)
44 {
45 }
46
47 HandleHeap::HandleHeap(JSGlobalData* globalData)
48     : m_globalData(globalData)
49     , m_nextToFinalize(0)
50 {
51     grow();
52 }
53
54 void HandleHeap::grow()
55 {
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);
61     }
62 }
63
64 void HandleHeap::visitStrongHandles(HeapRootVisitor& heapRootVisitor)
65 {
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))
70             CRASH();
71 #endif
72         heapRootVisitor.visit(node->slot());
73     }
74 }
75
76 void HandleHeap::visitWeakHandles(HeapRootVisitor& heapRootVisitor)
77 {
78     SlotVisitor& visitor = heapRootVisitor.visitor();
79
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))
84             CRASH();
85 #endif
86         JSCell* cell = node->slot()->asCell();
87         if (Heap::isMarked(cell))
88             continue;
89
90         WeakHandleOwner* weakOwner = node->weakOwner();
91         if (!weakOwner)
92             continue;
93
94         if (!weakOwner->isReachableFromOpaqueRoots(Handle<Unknown>::wrapSlot(node->slot()), node->weakOwnerContext(), visitor))
95             continue;
96
97         heapRootVisitor.visit(node->slot());
98     }
99 }
100
101 void HandleHeap::finalizeWeakHandles()
102 {
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))
108             CRASH();
109 #endif
110
111         JSCell* cell = node->slot()->asCell();
112         if (Heap::isMarked(cell))
113             continue;
114
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.
118                 continue;
119         }
120 #if ENABLE(GC_VALIDATION)
121         if (!isLiveNode(node))
122             CRASH();
123 #endif
124         *node->slot() = JSValue();
125         SentinelLinkedList<Node>::remove(node);
126         m_immediateList.push(node);
127     }
128     
129     m_nextToFinalize = 0;
130 }
131
132 void HandleHeap::writeBarrier(HandleSlot slot, const JSValue& value)
133 {
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)
137         CRASH();
138
139     if (!value == !*slot && slot->isCell() == value.isCell())
140         return;
141
142     Node* node = toNode(slot);
143 #if ENABLE(GC_VALIDATION)
144     if (!isLiveNode(node))
145         CRASH();
146 #endif
147     SentinelLinkedList<Node>::remove(node);
148     if (!value || !value.isCell()) {
149         m_immediateList.push(node);
150         return;
151     }
152
153     if (node->isWeak()) {
154         m_weakList.push(node);
155 #if ENABLE(GC_VALIDATION)
156         if (!isLiveNode(node))
157             CRASH();
158 #endif
159         return;
160     }
161
162     m_strongList.push(node);
163 #if ENABLE(GC_VALIDATION)
164     if (!isLiveNode(node))
165         CRASH();
166 #endif
167 }
168
169 unsigned HandleHeap::protectedGlobalObjectCount()
170 {
171     unsigned count = 0;
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())
176             count++;
177     }
178     return count;
179 }
180
181 #if ENABLE(GC_VALIDATION) || !ASSERT_DISABLED
182 bool HandleHeap::isLiveNode(Node* node)
183 {
184     if (node->prev()->next() != node)
185         return false;
186     if (node->next()->prev() != node)
187         return false;
188         
189     return true;
190 }
191
192 bool HandleHeap::isValidWeakNode(Node* node)
193 {
194     if (!isLiveNode(node))
195         return false;
196     if (!node->isWeak())
197         return false;
198
199     JSValue value = *node->slot();
200     if (!value || !value.isCell())
201         return false;
202
203     JSCell* cell = value.asCell();
204     if (!cell || !cell->structure())
205         return false;
206
207     return true;
208 }
209 #endif
210
211 } // namespace JSC