]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/MapData.cpp
b9e73ae31817d4b9f162351a25eff0148d43e06c
[apple/javascriptcore.git] / runtime / MapData.cpp
1 /*
2 * Copyright (C) 2013 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 "MapData.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/CryptographicallyRandomNumber.h>
36 #include <wtf/MathExtras.h>
37
38
39 namespace JSC {
40
41 const ClassInfo MapData::s_info = { "MapData", 0, 0, 0, CREATE_METHOD_TABLE(MapData) };
42
43 static const int32_t minimumMapSize = 8;
44
45 MapData::MapData(VM& vm)
46 : Base(vm, vm.mapDataStructure.get())
47 , m_capacity(0)
48 , m_size(0)
49 , m_deletedCount(0)
50 , m_iteratorCount(0)
51 , m_entries(0)
52 {
53 }
54
55 MapData::Entry* MapData::find(CallFrame* callFrame, KeyType key)
56 {
57 if (key.value.isString()) {
58 auto iter = m_stringKeyedTable.find(asString(key.value)->value(callFrame).impl());
59 if (iter == m_stringKeyedTable.end())
60 return 0;
61 return &m_entries[iter->value];
62 }
63 if (key.value.isCell()) {
64 auto iter = m_cellKeyedTable.find(key.value.asCell());
65 if (iter == m_cellKeyedTable.end())
66 return 0;
67 return &m_entries[iter->value];
68 }
69
70 auto iter = m_valueKeyedTable.find(JSValue::encode(key.value));
71 if (iter == m_valueKeyedTable.end())
72 return 0;
73 return &m_entries[iter->value];
74 }
75
76 bool MapData::contains(CallFrame* callFrame, KeyType key)
77 {
78 return find(callFrame, key);
79 }
80
81 template <typename Map, typename Key> MapData::Entry* MapData::add(CallFrame* callFrame, Map& map, Key key, KeyType keyValue)
82 {
83 typename Map::iterator location = map.find(key);
84 if (location != map.end())
85 return &m_entries[location->value];
86
87 if (!ensureSpaceForAppend(callFrame))
88 return 0;
89
90 auto result = map.add(key, m_size);
91 RELEASE_ASSERT(result.isNewEntry);
92 Entry* entry = &m_entries[m_size++];
93 new (entry) Entry();
94 entry->key.set(callFrame->vm(), this, keyValue.value);
95 return entry;
96 }
97
98 void MapData::set(CallFrame* callFrame, KeyType key, JSValue value)
99 {
100 Entry* location = add(callFrame, key);
101 if (!location)
102 return;
103 location->value.set(callFrame->vm(), this, value);
104 }
105
106 MapData::Entry* MapData::add(CallFrame* callFrame, KeyType key)
107 {
108 if (key.value.isString())
109 return add(callFrame, m_stringKeyedTable, asString(key.value)->value(callFrame).impl(), key);
110 if (key.value.isCell())
111 return add(callFrame, m_cellKeyedTable, key.value.asCell(), key);
112 return add(callFrame, m_valueKeyedTable, JSValue::encode(key.value), key);
113 }
114
115 JSValue MapData::get(CallFrame* callFrame, KeyType key)
116 {
117 if (Entry* entry = find(callFrame, key))
118 return entry->value.get();
119 return JSValue();
120 }
121
122 bool MapData::remove(CallFrame* callFrame, KeyType key)
123 {
124 int32_t location;
125 if (key.value.isString()) {
126 auto iter = m_stringKeyedTable.find(asString(key.value)->value(callFrame).impl());
127 if (iter == m_stringKeyedTable.end())
128 return false;
129 location = iter->value;
130 m_stringKeyedTable.remove(iter);
131 } else if (key.value.isCell()) {
132 auto iter = m_cellKeyedTable.find(key.value.asCell());
133 if (iter == m_cellKeyedTable.end())
134 return false;
135 location = iter->value;
136 m_cellKeyedTable.remove(iter);
137 } else {
138 auto iter = m_valueKeyedTable.find(JSValue::encode(key.value));
139 if (iter == m_valueKeyedTable.end())
140 return false;
141 location = iter->value;
142 m_valueKeyedTable.remove(iter);
143 }
144 m_entries[location].key.clear();
145 m_entries[location].value.clear();
146 m_deletedCount++;
147 return true;
148 }
149
150 void MapData::replaceAndPackBackingStore(Entry* destination, int32_t newCapacity)
151 {
152 ASSERT(shouldPack());
153 int32_t newEnd = 0;
154 RELEASE_ASSERT(newCapacity > 0);
155 for (int32_t i = 0; i < m_size; i++) {
156 Entry& entry = m_entries[i];
157 if (!entry.key)
158 continue;
159 ASSERT(newEnd < newCapacity);
160 destination[newEnd] = entry;
161
162 // We overwrite the old entry with a forwarding index for the new entry,
163 // so that we can fix up our hash tables below without doing additional
164 // hash lookups
165 entry.value.setWithoutWriteBarrier(jsNumber(newEnd));
166 newEnd++;
167 }
168
169 // Fixup for the hashmaps
170 for (auto ptr = m_valueKeyedTable.begin(); ptr != m_valueKeyedTable.end(); ++ptr)
171 ptr->value = m_entries[ptr->value].value.get().asInt32();
172 for (auto ptr = m_stringKeyedTable.begin(); ptr != m_stringKeyedTable.end(); ++ptr)
173 ptr->value = m_entries[ptr->value].value.get().asInt32();
174
175 ASSERT((m_size - newEnd) == m_deletedCount);
176 m_deletedCount = 0;
177
178 m_capacity = newCapacity;
179 m_size = newEnd;
180 m_entries = destination;
181
182 }
183
184 void MapData::replaceBackingStore(Entry* destination, int32_t newCapacity)
185 {
186 ASSERT(!shouldPack());
187 RELEASE_ASSERT(newCapacity > 0);
188 ASSERT(newCapacity >= m_capacity);
189 memcpy(destination, m_entries, sizeof(Entry) * m_size);
190 m_capacity = newCapacity;
191 m_entries = destination;
192 }
193
194 CheckedBoolean MapData::ensureSpaceForAppend(CallFrame* callFrame)
195 {
196 if (m_capacity > m_size)
197 return true;
198
199 size_t requiredSize = std::max(m_capacity + (m_capacity / 2) + 1, minimumMapSize);
200 void* newStorage = 0;
201 DeferGC defer(*callFrame->heap());
202 if (!callFrame->heap()->tryAllocateStorage(this, requiredSize * sizeof(Entry), &newStorage)) {
203 throwOutOfMemoryError(callFrame);
204 return false;
205 }
206 Entry* newEntries = static_cast<Entry*>(newStorage);
207 if (shouldPack())
208 replaceAndPackBackingStore(newEntries, requiredSize);
209 else
210 replaceBackingStore(newEntries, requiredSize);
211 callFrame->heap()->writeBarrier(this);
212 return true;
213 }
214
215 void MapData::visitChildren(JSCell* cell, SlotVisitor& visitor)
216 {
217 Base::visitChildren(cell, visitor);
218 MapData* thisObject = jsCast<MapData*>(cell);
219 Entry* entries = thisObject->m_entries;
220 if (!entries)
221 return;
222 size_t size = thisObject->m_size;
223 if (thisObject->m_deletedCount) {
224 for (size_t i = 0; i < size; i++) {
225 if (!entries[i].key)
226 continue;
227 visitor.append(&entries[i].key);
228 visitor.append(&entries[i].value);
229 }
230 } else
231 visitor.appendValues(&entries[0].key, size * (sizeof(Entry) / sizeof(WriteBarrier<Unknown>)));
232
233 visitor.copyLater(thisObject, MapBackingStoreCopyToken, entries, thisObject->capacityInBytes());
234 }
235
236 void MapData::copyBackingStore(JSCell* cell, CopyVisitor& visitor, CopyToken token)
237 {
238 MapData* thisObject = jsCast<MapData*>(cell);
239 if (token == MapBackingStoreCopyToken && visitor.checkIfShouldCopy(thisObject->m_entries)) {
240 Entry* oldEntries = thisObject->m_entries;
241 Entry* newEntries = static_cast<Entry*>(visitor.allocateNewSpace(thisObject->capacityInBytes()));
242 if (thisObject->shouldPack())
243 thisObject->replaceAndPackBackingStore(newEntries, thisObject->m_capacity);
244 else
245 thisObject->replaceBackingStore(newEntries, thisObject->m_capacity);
246 visitor.didCopy(oldEntries, thisObject->capacityInBytes());
247 }
248 Base::copyBackingStore(cell, visitor, token);
249 }
250
251 void MapData::destroy(JSCell* cell)
252 {
253 static_cast<MapData*>(cell)->~MapData();
254 }
255
256 }