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.
29 #include <heap/Weak.h>
30 #include <heap/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 RawMappedArg
, typename HashArg
= typename DefaultHash
<KeyArg
>::Hash
,
38 typename KeyTraitsArg
= HashTraits
<KeyArg
> >
39 class WeakGCMap
: public HashMap
<KeyArg
, Weak
<RawMappedArg
>, HashArg
, KeyTraitsArg
> {
40 typedef Weak
<RawMappedArg
> MappedType
;
41 typedef HashMap
<KeyArg
, MappedType
, HashArg
, KeyTraitsArg
> Base
;
42 typedef WeakGCMap
<KeyArg
, RawMappedArg
, HashArg
, KeyTraitsArg
> Self
;
43 typedef HashTraits
<MappedType
> MappedTraits
;
44 typedef typename
MappedTraits::PassInType MappedPassInType
;
47 typedef typename
Base::KeyType KeyType
;
48 typedef typename
Base::AddResult AddResult
;
49 typedef typename
Base::iterator iterator
;
50 typedef typename
Base::const_iterator const_iterator
;
57 : m_gcThreshold(minGCThreshold
)
61 AddResult
set(const KeyType
& key
, MappedPassInType value
)
64 return Base::set(key
, value
);
67 AddResult
add(const KeyType
& key
, MappedPassInType value
)
70 AddResult addResult
= Base::add(key
, nullptr);
71 if (!addResult
.iterator
->value
) { // New value or found a zombie value.
72 addResult
.isNewEntry
= true;
73 addResult
.iterator
->value
= value
;
78 iterator
find(const KeyType
& key
)
80 iterator it
= Base::find(key
);
81 iterator end
= Base::end();
82 if (it
!= end
&& !it
->value
) // Found a zombie value.
87 const_iterator
find(const KeyType
& key
) const
89 return const_cast<Self
*>(this)->find(key
);
92 bool contains(const KeyType
& key
) const
94 return find(key
) != end();
98 static const int minGCThreshold
= 3;
102 Vector
<KeyType
, 4> zombies
;
103 iterator end
= this->end();
104 for (iterator it
= begin(); it
!= end
; ++it
) {
106 zombies
.append(it
->key
);
108 for (size_t i
= 0; i
< zombies
.size(); ++i
)
114 if (size() < m_gcThreshold
)
118 m_gcThreshold
= std::max(minGCThreshold
, size() * 2 - 1);
124 template<typename KeyArg
, typename RawMappedArg
, typename HashArg
, typename KeyTraitsArg
>
125 const int WeakGCMap
<KeyArg
, RawMappedArg
, HashArg
, KeyTraitsArg
>::minGCThreshold
;
129 #endif // WeakGCMap_h