2 * Copyright (C) 2009 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
;
51 : m_gcThreshold(minGCThreshold
)
55 ValueArg
* get(const KeyType
& key
) const
57 return m_map
.get(key
);
60 AddResult
set(const KeyType
& key
, ValueType value
)
63 return m_map
.set(key
, WTF::move(value
));
66 ALWAYS_INLINE AddResult
add(const KeyType
& key
, ValueType value
)
69 AddResult addResult
= m_map
.fastAdd(key
, nullptr);
70 if (!addResult
.iterator
->value
) { // New value or found a zombie value.
71 addResult
.isNewEntry
= true;
72 addResult
.iterator
->value
= WTF::move(value
);
77 bool remove(const KeyType
& key
)
79 return m_map
.remove(key
);
87 iterator
find(const KeyType
& key
)
89 iterator it
= m_map
.find(key
);
90 iterator end
= m_map
.end();
91 if (it
!= end
&& !it
->value
) // Found a zombie value.
96 const_iterator
find(const KeyType
& key
) const
98 return const_cast<WeakGCMap
*>(this)->find(key
);
101 bool contains(const KeyType
& key
) const
103 return find(key
) != m_map
.end();
107 static const int minGCThreshold
= 3;
109 NEVER_INLINE
void gcMap()
111 Vector
<KeyType
, 4> zombies
;
113 for (iterator it
= m_map
.begin(), end
= m_map
.end(); it
!= end
; ++it
) {
115 zombies
.append(it
->key
);
118 for (size_t i
= 0; i
< zombies
.size(); ++i
)
119 m_map
.remove(zombies
[i
]);
124 if (m_map
.size() < m_gcThreshold
)
128 m_gcThreshold
= std::max(minGCThreshold
, m_map
.size() * 2 - 1);
135 template<typename KeyArg
, typename RawMappedArg
, typename HashArg
, typename KeyTraitsArg
>
136 const int WeakGCMap
<KeyArg
, RawMappedArg
, HashArg
, KeyTraitsArg
>::minGCThreshold
;
140 #endif // WeakGCMap_h