]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/WeakGCMap.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / WeakGCMap.h
1 /*
2 * Copyright (C) 2009, 2015 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 "Weak.h"
30 #include "WeakInlines.h"
31 #include <wtf/HashMap.h>
32
33 namespace JSC {
34
35 // A HashMap with Weak<JSCell> values, which automatically removes values once they're garbage collected.
36
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;
43
44 public:
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;
49
50 explicit WeakGCMap(VM&);
51 ~WeakGCMap();
52
53 ValueArg* get(const KeyType& key) const
54 {
55 return m_map.get(key);
56 }
57
58 AddResult set(const KeyType& key, ValueType value)
59 {
60 return m_map.set(key, WTF::move(value));
61 }
62
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
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
84 iterator find(const KeyType& key)
85 {
86 iterator it = m_map.find(key);
87 iterator end = m_map.end();
88 if (it != end && !it->value) // Found a zombie value.
89 return end;
90 return it;
91 }
92
93 const_iterator find(const KeyType& key) const
94 {
95 return const_cast<WeakGCMap*>(this)->find(key);
96 }
97
98 template<typename Functor>
99 void forEach(Functor functor)
100 {
101 for (auto& pair : m_map) {
102 if (pair.value)
103 functor(pair.key, pair.value.get());
104 }
105 }
106
107 bool contains(const KeyType& key) const
108 {
109 return find(key) != m_map.end();
110 }
111
112 void pruneStaleEntries();
113
114 private:
115 HashMapType m_map;
116 VM& m_vm;
117 };
118
119 } // namespace JSC
120
121 #endif // WeakGCMap_h