]>
git.saurik.com Git - apple/javascriptcore.git/blob - heap/HandleSet.h
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.
29 #include <wtf/BlockStack.h>
31 #include <wtf/HashCountedSet.h>
32 #include <wtf/SentinelLinkedList.h>
33 #include <wtf/SinglyLinkedList.h>
38 class HeapRootVisitor
;
45 static HandleSet
* heapFor(HandleSlot
);
47 HandleSet(JSGlobalData
*);
49 JSGlobalData
* globalData();
51 HandleSlot
allocate();
52 void deallocate(HandleSlot
);
54 void visitStrongHandles(HeapRootVisitor
&);
56 JS_EXPORT_PRIVATE
void writeBarrier(HandleSlot
, const JSValue
&);
58 unsigned protectedGlobalObjectCount();
60 template<typename Functor
> void forEachStrongHandle(Functor
&, const HashCountedSet
<JSCell
*>& skipSet
);
65 Node(WTF::SentinelTag
);
69 HandleSet
* handleSet();
79 HandleSet
* m_handleSet
;
84 static HandleSlot
toHandle(Node
*);
85 static Node
* toNode(HandleSlot
);
87 JS_EXPORT_PRIVATE
void grow();
89 #if ENABLE(GC_VALIDATION) || !ASSERT_DISABLED
90 bool isLiveNode(Node
*);
93 JSGlobalData
* m_globalData
;
94 BlockStack
<Node
> m_blockStack
;
96 SentinelLinkedList
<Node
> m_strongList
;
97 SentinelLinkedList
<Node
> m_immediateList
;
98 SinglyLinkedList
<Node
> m_freeList
;
99 Node
* m_nextToFinalize
;
102 inline HandleSet
* HandleSet::heapFor(HandleSlot handle
)
104 return toNode(handle
)->handleSet();
107 inline JSGlobalData
* HandleSet::globalData()
112 inline HandleSlot
HandleSet::toHandle(Node
* node
)
114 return reinterpret_cast<HandleSlot
>(node
);
117 inline HandleSet::Node
* HandleSet::toNode(HandleSlot handle
)
119 return reinterpret_cast<Node
*>(handle
);
122 inline HandleSlot
HandleSet::allocate()
124 // Forbid assignment to handles during the finalization phase, since it would violate many GC invariants.
125 // File a bug with stack trace if you hit this.
126 if (m_nextToFinalize
)
128 if (m_freeList
.isEmpty())
131 Node
* node
= m_freeList
.pop();
132 new (NotNull
, node
) Node(this);
133 m_immediateList
.push(node
);
134 return toHandle(node
);
137 inline void HandleSet::deallocate(HandleSlot handle
)
139 Node
* node
= toNode(handle
);
140 if (node
== m_nextToFinalize
) {
141 ASSERT(m_nextToFinalize
->next());
142 m_nextToFinalize
= m_nextToFinalize
->next();
145 SentinelLinkedList
<Node
>::remove(node
);
146 m_freeList
.push(node
);
149 inline HandleSet::Node::Node(HandleSet
* handleSet
)
150 : m_handleSet(handleSet
)
156 inline HandleSet::Node::Node(WTF::SentinelTag
)
163 inline HandleSlot
HandleSet::Node::slot()
168 inline HandleSet
* HandleSet::Node::handleSet()
173 inline void HandleSet::Node::setPrev(Node
* prev
)
178 inline HandleSet::Node
* HandleSet::Node::prev()
183 inline void HandleSet::Node::setNext(Node
* next
)
188 inline HandleSet::Node
* HandleSet::Node::next()
193 template<typename Functor
> void HandleSet::forEachStrongHandle(Functor
& functor
, const HashCountedSet
<JSCell
*>& skipSet
)
195 Node
* end
= m_strongList
.end();
196 for (Node
* node
= m_strongList
.begin(); node
!= end
; node
= node
->next()) {
197 JSValue value
= *node
->slot();
198 if (!value
|| !value
.isCell())
200 if (skipSet
.contains(value
.asCell()))
202 functor(value
.asCell());