]>
Commit | Line | Data |
---|---|---|
f9bf01c6 A |
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 | ||
14957cd0 A |
29 | #include "Handle.h" |
30 | #include "JSGlobalData.h" | |
f9bf01c6 A |
31 | #include <wtf/HashMap.h> |
32 | ||
33 | namespace JSC { | |
34 | ||
14957cd0 A |
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 | }; | |
f9bf01c6 | 48 | |
14957cd0 A |
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 | ||
6fe7ccc8 | 54 | typedef HashMap<KeyType, WeakImpl*, HashArg, KeyTraitsArg> MapType; |
14957cd0 A |
55 | typedef typename HandleTypes<MappedType>::ExternalType ExternalType; |
56 | typedef typename MapType::iterator map_iterator; | |
f9bf01c6 A |
57 | |
58 | public: | |
14957cd0 A |
59 | |
60 | struct iterator { | |
61 | friend class WeakGCMap; | |
62 | iterator(map_iterator iter) | |
63 | : m_iterator(iter) | |
64 | { | |
65 | } | |
66 | ||
6fe7ccc8 | 67 | std::pair<KeyType, ExternalType> get() const { return std::make_pair(m_iterator->first, HandleTypes<MappedType>::getFromSlot(const_cast<JSValue*>(&m_iterator->second->jsValue()))); } |
14957cd0 A |
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 | ||
6fe7ccc8 A |
81 | typedef WTF::HashTableAddResult<iterator> AddResult; |
82 | ||
14957cd0 A |
83 | WeakGCMap() |
84 | { | |
85 | } | |
86 | ||
f9bf01c6 | 87 | bool isEmpty() { return m_map.isEmpty(); } |
14957cd0 A |
88 | void clear() |
89 | { | |
90 | map_iterator end = m_map.end(); | |
91 | for (map_iterator ptr = m_map.begin(); ptr != end; ++ptr) | |
6fe7ccc8 | 92 | WeakSet::deallocate(ptr->second); |
14957cd0 A |
93 | m_map.clear(); |
94 | } | |
f9bf01c6 | 95 | |
14957cd0 A |
96 | bool contains(const KeyType& key) const |
97 | { | |
98 | return m_map.contains(key); | |
99 | } | |
f9bf01c6 | 100 | |
14957cd0 A |
101 | iterator find(const KeyType& key) |
102 | { | |
103 | return m_map.find(key); | |
104 | } | |
f9bf01c6 | 105 | |
14957cd0 A |
106 | void remove(iterator iter) |
107 | { | |
108 | ASSERT(iter.m_iterator != m_map.end()); | |
6fe7ccc8 A |
109 | WeakImpl* impl = iter.m_iterator->second; |
110 | ASSERT(impl); | |
111 | WeakSet::deallocate(impl); | |
14957cd0 A |
112 | m_map.remove(iter.m_iterator); |
113 | } | |
f9bf01c6 | 114 | |
14957cd0 A |
115 | ExternalType get(const KeyType& key) const |
116 | { | |
6fe7ccc8 | 117 | return HandleTypes<MappedType>::getFromSlot(const_cast<JSValue*>(&m_map.get(key)->jsValue())); |
14957cd0 | 118 | } |
f9bf01c6 | 119 | |
6fe7ccc8 | 120 | AddResult add(JSGlobalData&, const KeyType& key, ExternalType value) |
14957cd0 | 121 | { |
6fe7ccc8 A |
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)); | |
f9bf01c6 | 125 | |
6fe7ccc8 A |
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); | |
14957cd0 | 128 | } |
f9bf01c6 | 129 | |
14957cd0 A |
130 | void set(JSGlobalData& globalData, const KeyType& key, ExternalType value) |
131 | { | |
6fe7ccc8 A |
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)); | |
14957cd0 A |
137 | } |
138 | ||
139 | ExternalType take(const KeyType& key) | |
140 | { | |
6fe7ccc8 A |
141 | WeakImpl* impl = m_map.take(key); |
142 | if (!impl) | |
14957cd0 | 143 | return HashTraits<ExternalType>::emptyValue(); |
6fe7ccc8 A |
144 | ExternalType result = HandleTypes<MappedType>::getFromSlot(const_cast<JSValue*>(&impl->jsValue())); |
145 | WeakSet::deallocate(impl); | |
f9bf01c6 | 146 | return result; |
f9bf01c6 | 147 | } |
14957cd0 A |
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 | { | |
6fe7ccc8 A |
162 | WeakImpl* impl = m_map.take(FinalizerCallback::keyForFinalizer(context, HandleTypes<MappedType>::getFromSlot(handle.slot()))); |
163 | ASSERT(impl); | |
164 | WeakSet::deallocate(impl); | |
14957cd0 A |
165 | } |
166 | ||
167 | MapType m_map; | |
168 | }; | |
f9bf01c6 A |
169 | |
170 | } // namespace JSC | |
171 | ||
172 | #endif // WeakGCMap_h |