]>
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.
30 #include "HandleBlock.h"
31 #include <wtf/DoublyLinkedList.h>
32 #include <wtf/HashCountedSet.h>
33 #include <wtf/SentinelLinkedList.h>
34 #include <wtf/SinglyLinkedList.h>
40 class HeapRootVisitor
;
47 HandleNode(WTF::SentinelTag
);
51 HandleSet
* handleSet();
53 void setPrev(HandleNode
*);
56 void setNext(HandleNode
*);
66 friend class HandleBlock
;
68 static HandleSet
* heapFor(HandleSlot
);
75 HandleSlot
allocate();
76 void deallocate(HandleSlot
);
78 void visitStrongHandles(HeapRootVisitor
&);
80 JS_EXPORT_PRIVATE
void writeBarrier(HandleSlot
, const JSValue
&);
82 unsigned protectedGlobalObjectCount();
84 template<typename Functor
> void forEachStrongHandle(Functor
&, const HashCountedSet
<JSCell
*>& skipSet
);
87 typedef HandleNode Node
;
88 static HandleSlot
toHandle(Node
*);
89 static Node
* toNode(HandleSlot
);
91 JS_EXPORT_PRIVATE
void grow();
93 #if ENABLE(GC_VALIDATION) || !ASSERT_DISABLED
94 bool isLiveNode(Node
*);
98 DoublyLinkedList
<HandleBlock
> m_blockList
;
100 SentinelLinkedList
<Node
> m_strongList
;
101 SentinelLinkedList
<Node
> m_immediateList
;
102 SinglyLinkedList
<Node
> m_freeList
;
103 Node
* m_nextToFinalize
;
106 inline HandleSet
* HandleSet::heapFor(HandleSlot handle
)
108 return toNode(handle
)->handleSet();
111 inline VM
* HandleSet::vm()
116 inline HandleSlot
HandleSet::toHandle(HandleSet::Node
* node
)
118 return reinterpret_cast<HandleSlot
>(node
);
121 inline HandleSet::Node
* HandleSet::toNode(HandleSlot handle
)
123 return reinterpret_cast<HandleSet::Node
*>(handle
);
126 inline HandleSlot
HandleSet::allocate()
128 // Forbid assignment to handles during the finalization phase, since it would violate many GC invariants.
129 // File a bug with stack trace if you hit this.
130 RELEASE_ASSERT(!m_nextToFinalize
);
132 if (m_freeList
.isEmpty())
135 HandleSet::Node
* node
= m_freeList
.pop();
136 new (NotNull
, node
) HandleSet::Node();
137 m_immediateList
.push(node
);
138 return toHandle(node
);
141 inline void HandleSet::deallocate(HandleSlot handle
)
143 HandleSet::Node
* node
= toNode(handle
);
144 if (node
== m_nextToFinalize
) {
145 ASSERT(m_nextToFinalize
->next());
146 m_nextToFinalize
= m_nextToFinalize
->next();
149 SentinelLinkedList
<HandleSet::Node
>::remove(node
);
150 m_freeList
.push(node
);
153 inline HandleNode::HandleNode()
159 inline HandleNode::HandleNode(WTF::SentinelTag
)
165 inline HandleSlot
HandleNode::slot()
170 inline HandleSet
* HandleNode::handleSet()
172 return HandleBlock::blockFor(this)->handleSet();
175 inline void HandleNode::setPrev(HandleNode
* prev
)
180 inline HandleNode
* HandleNode::prev()
185 inline void HandleNode::setNext(HandleNode
* next
)
190 inline HandleNode
* HandleNode::next()
195 template<typename Functor
> void HandleSet::forEachStrongHandle(Functor
& functor
, const HashCountedSet
<JSCell
*>& skipSet
)
197 HandleSet::Node
* end
= m_strongList
.end();
198 for (HandleSet::Node
* node
= m_strongList
.begin(); node
!= end
; node
= node
->next()) {
199 JSValue value
= *node
->slot();
200 if (!value
|| !value
.isCell())
202 if (skipSet
.contains(value
.asCell()))
204 functor(value
.asCell());