]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/WeakGCMap.h
e7e9447ab5d2fb53b95c4462f103a54a9dce437b
[apple/javascriptcore.git] / runtime / WeakGCMap.h
1 /*
2 * Copyright (C) 2009 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 #ifndef WeakGCMap_h
27 #define WeakGCMap_h
28
29 #include "Handle.h"
30 #include "JSGlobalData.h"
31 #include <wtf/HashMap.h>
32
33 namespace JSC {
34
35 // A HashMap for GC'd values that removes entries when the associated value
36 // dies.
37 template <typename KeyType, typename MappedType> struct DefaultWeakGCMapFinalizerCallback {
38 static void* finalizerContextFor(KeyType key)
39 {
40 return reinterpret_cast<void*>(key);
41 }
42
43 static KeyType keyForFinalizer(void* context, typename HandleTypes<MappedType>::ExternalType)
44 {
45 return reinterpret_cast<KeyType>(context);
46 }
47 };
48
49 template<typename KeyType, typename MappedType, typename FinalizerCallback = DefaultWeakGCMapFinalizerCallback<KeyType, MappedType>, typename HashArg = typename DefaultHash<KeyType>::Hash, typename KeyTraitsArg = HashTraits<KeyType> >
50 class WeakGCMap : private WeakHandleOwner {
51 WTF_MAKE_FAST_ALLOCATED;
52 WTF_MAKE_NONCOPYABLE(WeakGCMap);
53
54 typedef HashMap<KeyType, WeakImpl*, HashArg, KeyTraitsArg> MapType;
55 typedef typename HandleTypes<MappedType>::ExternalType ExternalType;
56 typedef typename MapType::iterator map_iterator;
57
58 public:
59
60 struct iterator {
61 friend class WeakGCMap;
62 iterator(map_iterator iter)
63 : m_iterator(iter)
64 {
65 }
66
67 std::pair<KeyType, ExternalType> get() const { return std::make_pair(m_iterator->first, HandleTypes<MappedType>::getFromSlot(const_cast<JSValue*>(&m_iterator->second->jsValue()))); }
68
69 iterator& operator++() { ++m_iterator; return *this; }
70
71 // postfix ++ intentionally omitted
72
73 // Comparison.
74 bool operator==(const iterator& other) const { return m_iterator == other.m_iterator; }
75 bool operator!=(const iterator& other) const { return m_iterator != other.m_iterator; }
76
77 private:
78 map_iterator m_iterator;
79 };
80
81 typedef WTF::HashTableAddResult<iterator> AddResult;
82
83 WeakGCMap()
84 {
85 }
86
87 bool isEmpty() { return m_map.isEmpty(); }
88 void clear()
89 {
90 map_iterator end = m_map.end();
91 for (map_iterator ptr = m_map.begin(); ptr != end; ++ptr)
92 WeakSet::deallocate(ptr->second);
93 m_map.clear();
94 }
95
96 bool contains(const KeyType& key) const
97 {
98 return m_map.contains(key);
99 }
100
101 iterator find(const KeyType& key)
102 {
103 return m_map.find(key);
104 }
105
106 void remove(iterator iter)
107 {
108 ASSERT(iter.m_iterator != m_map.end());
109 WeakImpl* impl = iter.m_iterator->second;
110 ASSERT(impl);
111 WeakSet::deallocate(impl);
112 m_map.remove(iter.m_iterator);
113 }
114
115 ExternalType get(const KeyType& key) const
116 {
117 return HandleTypes<MappedType>::getFromSlot(const_cast<JSValue*>(&m_map.get(key)->jsValue()));
118 }
119
120 AddResult add(JSGlobalData&, const KeyType& key, ExternalType value)
121 {
122 typename MapType::AddResult result = m_map.add(key, 0);
123 if (result.isNewEntry)
124 result.iterator->second = WeakSet::allocate(value, this, FinalizerCallback::finalizerContextFor(key));
125
126 // WeakGCMap exposes a different iterator, so we need to wrap it and create our own AddResult.
127 return AddResult(iterator(result.iterator), result.isNewEntry);
128 }
129
130 void set(JSGlobalData& globalData, const KeyType& key, ExternalType value)
131 {
132 ASSERT_UNUSED(globalData, globalData.apiLock().currentThreadIsHoldingLock());
133 typename MapType::AddResult result = m_map.add(key, 0);
134 if (!result.isNewEntry)
135 WeakSet::deallocate(result.iterator->second);
136 result.iterator->second = WeakSet::allocate(value, this, FinalizerCallback::finalizerContextFor(key));
137 }
138
139 ExternalType take(const KeyType& key)
140 {
141 WeakImpl* impl = m_map.take(key);
142 if (!impl)
143 return HashTraits<ExternalType>::emptyValue();
144 ExternalType result = HandleTypes<MappedType>::getFromSlot(const_cast<JSValue*>(&impl->jsValue()));
145 WeakSet::deallocate(impl);
146 return result;
147 }
148
149 size_t size() { return m_map.size(); }
150
151 iterator begin() { return iterator(m_map.begin()); }
152 iterator end() { return iterator(m_map.end()); }
153
154 ~WeakGCMap()
155 {
156 clear();
157 }
158
159 private:
160 virtual void finalize(Handle<Unknown> handle, void* context)
161 {
162 WeakImpl* impl = m_map.take(FinalizerCallback::keyForFinalizer(context, HandleTypes<MappedType>::getFromSlot(handle.slot())));
163 ASSERT(impl);
164 WeakSet::deallocate(impl);
165 }
166
167 MapType m_map;
168 };
169
170 } // namespace JSC
171
172 #endif // WeakGCMap_h