blob: ddc029e79166a51e84354911afaf786b48c40cf1 [file] [log] [blame]
oliver@apple.combfcc0482013-09-10 21:16:42 +00001/*
fpizlo@apple.com3cb36ea2015-10-05 19:35:32 +00002 * Copyright (C) 2013, 2015 Apple Inc. All rights reserved.
oliver@apple.combfcc0482013-09-10 21:16:42 +00003 *
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 "WeakMapData.h"
28
29#include "CopiedAllocator.h"
30#include "CopyVisitorInlines.h"
31#include "ExceptionHelpers.h"
32#include "JSCJSValueInlines.h"
33#include "SlotVisitorInlines.h"
34
35#include <wtf/MathExtras.h>
36
oliver@apple.combfcc0482013-09-10 21:16:42 +000037namespace JSC {
38
akling@apple.com2de49b72014-07-30 22:26:22 +000039const ClassInfo WeakMapData::s_info = { "WeakMapData", 0, 0, CREATE_METHOD_TABLE(WeakMapData) };
oliver@apple.combfcc0482013-09-10 21:16:42 +000040
commit-queue@webkit.orgb1511432013-09-11 20:34:09 +000041WeakMapData::WeakMapData(VM& vm)
42 : Base(vm, vm.weakMapDataStructure.get())
oliver@apple.combfcc0482013-09-10 21:16:42 +000043 , m_deadKeyCleaner(this)
44{
45}
46
47void WeakMapData::finishCreation(VM& vm)
48{
49 Base::finishCreation(vm);
50}
51
52void WeakMapData::destroy(JSCell* cell)
53{
54 static_cast<WeakMapData*>(cell)->~WeakMapData();
55}
56
57void WeakMapData::visitChildren(JSCell* cell, SlotVisitor& visitor)
58{
59 Base::visitChildren(cell, visitor);
60 WeakMapData* thisObj = jsCast<WeakMapData*>(cell);
61 visitor.addUnconditionalFinalizer(&thisObj->m_deadKeyCleaner);
62 visitor.addWeakReferenceHarvester(&thisObj->m_deadKeyCleaner);
63
64 // Rough approximation of the external storage needed for the hashtable.
65 // This isn't exact, but it is close enough, and proportional to the actual
66 // external mermory usage.
fpizlo@apple.com3cb36ea2015-10-05 19:35:32 +000067 visitor.reportExtraMemoryVisited(thisObj->m_map.capacity() * (sizeof(JSObject*) + sizeof(WriteBarrier<Unknown>)));
oliver@apple.combfcc0482013-09-10 21:16:42 +000068}
69
commit-queue@webkit.orgb1511432013-09-11 20:34:09 +000070void WeakMapData::set(VM& vm, JSObject* key, JSValue value)
oliver@apple.combfcc0482013-09-10 21:16:42 +000071{
72 // Here we force the write barrier on the key.
commit-queue@webkit.orgb1511432013-09-11 20:34:09 +000073 auto result = m_map.add(WriteBarrier<JSObject>(vm, this, key).get(), WriteBarrier<Unknown>());
74 result.iterator->value.set(vm, this, value);
oliver@apple.combfcc0482013-09-10 21:16:42 +000075}
76
77JSValue WeakMapData::get(JSObject* key)
78{
79 auto iter = m_map.find(key);
80 if (iter == m_map.end())
81 return jsUndefined();
82 return iter->value.get();
83}
84
85bool WeakMapData::remove(JSObject* key)
86{
87 auto iter = m_map.find(key);
88 if (iter == m_map.end())
89 return false;
90
91 m_map.remove(iter);
92 return true;
93}
94
95bool WeakMapData::contains(JSObject* key)
96{
97 return m_map.contains(key);
98}
99
100void WeakMapData::clear()
101{
102 m_map.clear();
103}
104
105void WeakMapData::DeadKeyCleaner::visitWeakReferences(SlotVisitor& visitor)
106{
107 m_liveKeyCount = 0;
108 for (auto it = m_target->m_map.begin(), end = m_target->m_map.end(); it != end; ++it) {
109 if (!Heap::isMarked(it->key))
110 continue;
111 m_liveKeyCount++;
112 visitor.append(&it->value);
113 }
114 RELEASE_ASSERT(m_liveKeyCount <= m_target->m_map.size());
115}
116
117void WeakMapData::DeadKeyCleaner::finalizeUnconditionally()
118{
119 if (m_liveKeyCount > m_target->m_map.size() / 2) {
120 RELEASE_ASSERT(m_liveKeyCount <= m_target->m_map.size());
121 int deadCount = m_target->m_map.size() - m_liveKeyCount;
122 if (!deadCount)
123 return;
124 Vector<JSObject*> deadEntries;
125 deadEntries.reserveCapacity(deadCount);
126 for (auto it = m_target->m_map.begin(), end = m_target->m_map.end(); it != end; ++it) {
127 if (Heap::isMarked(it->key))
128 continue;
129 deadEntries.uncheckedAppend(it->key);
130 }
131 for (size_t i = 0; i < deadEntries.size(); i++)
132 m_target->m_map.remove(deadEntries[i]);
133 } else {
134 MapType newMap;
135 for (auto it = m_target->m_map.begin(), end = m_target->m_map.end(); it != end; ++it) {
136 if (!Heap::isMarked(it->key))
137 continue;
138 newMap.add(it->key, it->value);
139 }
140 m_target->m_map.swap(newMap);
141 }
142}
143
144}