1/*
2 * Copyright (C) 2011-2016 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 "HandleSet.h"
28
29#include "HandleBlock.h"
30#include "HandleBlockInlines.h"
31#include "JSObject.h"
32#include "JSCInlines.h"
33#include <wtf/DataLog.h>
34
35namespace JSC {
36
37HandleSet::HandleSet(VM* vm)
38 : m_vm(vm)
39{
40 grow();
41}
42
43HandleSet::~HandleSet()
44{
45 while (!m_blockList.isEmpty())
46 HandleBlock::destroy(m_blockList.removeHead());
47}
48
49void HandleSet::grow()
50{
51 HandleBlock* newBlock = HandleBlock::create(this);
52 m_blockList.append(newBlock);
53
54 for (int i = newBlock->nodeCapacity() - 1; i >= 0; --i) {
55 Node* node = newBlock->nodeAtIndex(i);
56 new (NotNull, node) Node;
57 m_freeList.push(node);
58 }
59}
60
61void HandleSet::visitStrongHandles(SlotVisitor& visitor)
62{
63 Node* end = m_strongList.end();
64 for (Node* node = m_strongList.begin(); node != end; node = node->next()) {
65#if ENABLE(GC_VALIDATION)
66 RELEASE_ASSERT(isLiveNode(node));
67#endif
68 visitor.appendUnbarriered(*node->slot());
69 }
70}
71
72void HandleSet::writeBarrier(HandleSlot slot, const JSValue& value)
73{
74 if (!value == !*slot && slot->isCell() == value.isCell())
75 return;
76
77 Node* node = toNode(slot);
78#if ENABLE(GC_VALIDATION)
79 RELEASE_ASSERT(isLiveNode(node));
80#endif
81 SentinelLinkedList<Node>::remove(node);
82 if (!value || !value.isCell()) {
83 m_immediateList.push(node);
84 return;
85 }
86
87 m_strongList.push(node);
88#if ENABLE(GC_VALIDATION)
89 RELEASE_ASSERT(isLiveNode(node));
90#endif
91}
92
93unsigned HandleSet::protectedGlobalObjectCount()
94{
95 unsigned count = 0;
96 Node* end = m_strongList.end();
97 for (Node* node = m_strongList.begin(); node != end; node = node->next()) {
98 JSValue value = *node->slot();
99 if (value.isObject() && asObject(value.asCell())->isGlobalObject())
100 count++;
101 }
102 return count;
103}
104
105#if ENABLE(GC_VALIDATION) || !ASSERT_DISABLED
106bool HandleSet::isLiveNode(Node* node)
107{
108 if (node->prev()->next() != node)
109 return false;
110 if (node->next()->prev() != node)
111 return false;
112
113 return true;
114}
115#endif
116
117} // namespace JSC
118