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 <wtf/HashMap.h>
36 // A HashMap whose get() function returns emptyValue() for cells awaiting destruction.
37 template<typename KeyType, typename MappedType>
39 WTF_MAKE_FAST_ALLOCATED;
42 * A value enters the WeakGCMap marked. (Guaranteed by set().)
43 * A value that becomes unmarked leaves the WeakGCMap before being recycled. (Guaranteed by the value's destructor removing it from the WeakGCMap.)
44 * A value that becomes unmarked leaves the WeakGCMap before becoming marked again. (Guaranteed by all destructors running before the mark phase begins.)
45 * During the mark phase, all values in the WeakGCMap are valid. (Guaranteed by all destructors running before the mark phase begins.)
49 typedef typename HashMap<KeyType, MappedType>::iterator iterator;
50 typedef typename HashMap<KeyType, MappedType>::const_iterator const_iterator;
52 bool isEmpty() { return m_map.isEmpty(); }
53 void clear() { m_map.clear(); }
55 MappedType get(const KeyType& key) const;
56 pair<iterator, bool> set(const KeyType&, const MappedType&);
57 MappedType take(const KeyType& key);
59 // These unchecked functions provide access to a value even if the value's
60 // mark bit is not set. This is used, among other things, to retrieve values
61 // during the GC mark phase, which begins by clearing all mark bits.
63 MappedType uncheckedGet(const KeyType& key) const { return m_map.get(key); }
64 bool uncheckedRemove(const KeyType&, const MappedType&);
66 iterator uncheckedBegin() { return m_map.begin(); }
67 iterator uncheckedEnd() { return m_map.end(); }
69 const_iterator uncheckedBegin() const { return m_map.begin(); }
70 const_iterator uncheckedEnd() const { return m_map.end(); }
73 HashMap<KeyType, MappedType> m_map;
76 template<typename KeyType, typename MappedType>
77 inline MappedType WeakGCMap<KeyType, MappedType>::get(const KeyType& key) const
79 MappedType result = m_map.get(key);
80 if (result == HashTraits<MappedType>::emptyValue())
82 if (!Heap::isCellMarked(result))
83 return HashTraits<MappedType>::emptyValue();
87 template<typename KeyType, typename MappedType>
88 MappedType WeakGCMap<KeyType, MappedType>::take(const KeyType& key)
90 MappedType result = m_map.take(key);
91 if (result == HashTraits<MappedType>::emptyValue())
93 if (!Heap::isCellMarked(result))
94 return HashTraits<MappedType>::emptyValue();
98 template<typename KeyType, typename MappedType>
99 pair<typename HashMap<KeyType, MappedType>::iterator, bool> WeakGCMap<KeyType, MappedType>::set(const KeyType& key, const MappedType& value)
101 Heap::markCell(value); // If value is newly allocated, it's not marked, so mark it now.
102 pair<iterator, bool> result = m_map.add(key, value);
103 if (!result.second) { // pre-existing entry
104 result.second = !Heap::isCellMarked(result.first->second);
105 result.first->second = value;
110 template<typename KeyType, typename MappedType>
111 bool WeakGCMap<KeyType, MappedType>::uncheckedRemove(const KeyType& key, const MappedType& value)
113 iterator it = m_map.find(key);
114 if (it == m_map.end())
116 if (it->second != value)
124 #endif // WeakGCMap_h