2 * Copyright (C) 2009, 2015 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
30 #include "WeakInlines.h"
31 #include <wtf/HashMap.h>
35 // A HashMap with Weak<JSCell> values, which automatically removes values once they're garbage collected.
37 template<typename KeyArg
, typename ValueArg
, typename HashArg
= typename DefaultHash
<KeyArg
>::Hash
,
38 typename KeyTraitsArg
= HashTraits
<KeyArg
>>
40 WTF_MAKE_FAST_ALLOCATED
;
41 typedef Weak
<ValueArg
> ValueType
;
42 typedef HashMap
<KeyArg
, ValueType
, HashArg
, KeyTraitsArg
> HashMapType
;
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
;
50 explicit WeakGCMap(VM
&);
53 ValueArg
* get(const KeyType
& key
) const
55 return m_map
.get(key
);
58 AddResult
set(const KeyType
& key
, ValueType value
)
60 return m_map
.set(key
, WTF::move(value
));
63 bool remove(const KeyType
& key
)
65 return m_map
.remove(key
);
75 const_iterator it
= m_map
.begin();
76 const_iterator end
= m_map
.end();
84 iterator
find(const KeyType
& key
)
86 iterator it
= m_map
.find(key
);
87 iterator end
= m_map
.end();
88 if (it
!= end
&& !it
->value
) // Found a zombie value.
93 const_iterator
find(const KeyType
& key
) const
95 return const_cast<WeakGCMap
*>(this)->find(key
);
98 template<typename Functor
>
99 void forEach(Functor functor
)
101 for (auto& pair
: m_map
) {
103 functor(pair
.key
, pair
.value
.get());
107 bool contains(const KeyType
& key
) const
109 return find(key
) != m_map
.end();
112 void pruneStaleEntries();
121 #endif // WeakGCMap_h