]>
Commit | Line | Data |
---|---|---|
f9bf01c6 | 1 | /* |
ed1e77d3 | 2 | * Copyright (C) 2009, 2015 Apple Inc. All rights reserved. |
f9bf01c6 A |
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 | ||
81345200 A |
29 | #include "Weak.h" |
30 | #include "WeakInlines.h" | |
f9bf01c6 A |
31 | #include <wtf/HashMap.h> |
32 | ||
33 | namespace JSC { | |
34 | ||
93a37866 | 35 | // A HashMap with Weak<JSCell> values, which automatically removes values once they're garbage collected. |
f9bf01c6 | 36 | |
81345200 A |
37 | template<typename KeyArg, typename ValueArg, typename HashArg = typename DefaultHash<KeyArg>::Hash, |
38 | typename KeyTraitsArg = HashTraits<KeyArg>> | |
39 | class WeakGCMap { | |
40 | WTF_MAKE_FAST_ALLOCATED; | |
41 | typedef Weak<ValueArg> ValueType; | |
42 | typedef HashMap<KeyArg, ValueType, HashArg, KeyTraitsArg> HashMapType; | |
f9bf01c6 A |
43 | |
44 | public: | |
81345200 A |
45 | typedef typename HashMapType::KeyType KeyType; |
46 | typedef typename HashMapType::AddResult AddResult; | |
47 | typedef typename HashMapType::iterator iterator; | |
48 | typedef typename HashMapType::const_iterator const_iterator; | |
6fe7ccc8 | 49 | |
ed1e77d3 A |
50 | explicit WeakGCMap(VM&); |
51 | ~WeakGCMap(); | |
14957cd0 | 52 | |
81345200 A |
53 | ValueArg* get(const KeyType& key) const |
54 | { | |
55 | return m_map.get(key); | |
56 | } | |
57 | ||
58 | AddResult set(const KeyType& key, ValueType value) | |
14957cd0 | 59 | { |
81345200 | 60 | return m_map.set(key, WTF::move(value)); |
14957cd0 | 61 | } |
f9bf01c6 | 62 | |
81345200 A |
63 | bool remove(const KeyType& key) |
64 | { | |
65 | return m_map.remove(key); | |
66 | } | |
67 | ||
68 | void clear() | |
69 | { | |
70 | m_map.clear(); | |
71 | } | |
72 | ||
ed1e77d3 A |
73 | bool isEmpty() const |
74 | { | |
75 | const_iterator it = m_map.begin(); | |
76 | const_iterator end = m_map.end(); | |
77 | while (it != end) { | |
78 | if (it->value) | |
79 | return true; | |
80 | } | |
81 | return false; | |
82 | } | |
83 | ||
14957cd0 A |
84 | iterator find(const KeyType& key) |
85 | { | |
81345200 A |
86 | iterator it = m_map.find(key); |
87 | iterator end = m_map.end(); | |
93a37866 A |
88 | if (it != end && !it->value) // Found a zombie value. |
89 | return end; | |
90 | return it; | |
14957cd0 | 91 | } |
f9bf01c6 | 92 | |
93a37866 | 93 | const_iterator find(const KeyType& key) const |
14957cd0 | 94 | { |
81345200 | 95 | return const_cast<WeakGCMap*>(this)->find(key); |
14957cd0 | 96 | } |
f9bf01c6 | 97 | |
ed1e77d3 A |
98 | template<typename Functor> |
99 | void forEach(Functor functor) | |
14957cd0 | 100 | { |
ed1e77d3 A |
101 | for (auto& pair : m_map) { |
102 | if (pair.value) | |
103 | functor(pair.key, pair.value.get()); | |
93a37866 | 104 | } |
14957cd0 A |
105 | } |
106 | ||
ed1e77d3 | 107 | bool contains(const KeyType& key) const |
14957cd0 | 108 | { |
ed1e77d3 | 109 | return find(key) != m_map.end(); |
14957cd0 A |
110 | } |
111 | ||
ed1e77d3 A |
112 | void pruneStaleEntries(); |
113 | ||
114 | private: | |
81345200 | 115 | HashMapType m_map; |
ed1e77d3 | 116 | VM& m_vm; |
14957cd0 | 117 | }; |
f9bf01c6 A |
118 | |
119 | } // namespace JSC | |
120 | ||
121 | #endif // WeakGCMap_h |