1 | /*
|
---|
2 | * Copyright (C) 2009 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 <wtf/HashMap.h>
|
---|
30 |
|
---|
31 | namespace JSC {
|
---|
32 |
|
---|
33 | class JSCell;
|
---|
34 |
|
---|
35 | // A HashMap whose get() function returns emptyValue() for cells awaiting destruction.
|
---|
36 | template<typename KeyType, typename MappedType>
|
---|
37 | class WeakGCMap : public FastAllocBase {
|
---|
38 | /*
|
---|
39 | Invariants:
|
---|
40 | * A value enters the WeakGCMap marked. (Guaranteed by set().)
|
---|
41 | * A value that becomes unmarked leaves the WeakGCMap before being recycled. (Guaranteed by the value's destructor removing it from the WeakGCMap.)
|
---|
42 | * A value that becomes unmarked leaves the WeakGCMap before becoming marked again. (Guaranteed by all destructors running before the mark phase begins.)
|
---|
43 | * During the mark phase, all values in the WeakGCMap are valid. (Guaranteed by all destructors running before the mark phase begins.)
|
---|
44 | */
|
---|
45 |
|
---|
46 | public:
|
---|
47 | typedef typename HashMap<KeyType, MappedType>::iterator iterator;
|
---|
48 | typedef typename HashMap<KeyType, MappedType>::const_iterator const_iterator;
|
---|
49 |
|
---|
50 | bool isEmpty() { return m_map.isEmpty(); }
|
---|
51 |
|
---|
52 | MappedType get(const KeyType& key) const;
|
---|
53 | pair<iterator, bool> set(const KeyType&, const MappedType&);
|
---|
54 | MappedType take(const KeyType& key);
|
---|
55 |
|
---|
56 | // These unchecked functions provide access to a value even if the value's
|
---|
57 | // mark bit is not set. This is used, among other things, to retrieve values
|
---|
58 | // during the GC mark phase, which begins by clearing all mark bits.
|
---|
59 |
|
---|
60 | MappedType uncheckedGet(const KeyType& key) const { return m_map.get(key); }
|
---|
61 | bool uncheckedRemove(const KeyType&, const MappedType&);
|
---|
62 |
|
---|
63 | iterator uncheckedBegin() { return m_map.begin(); }
|
---|
64 | iterator uncheckedEnd() { return m_map.end(); }
|
---|
65 |
|
---|
66 | const_iterator uncheckedBegin() const { return m_map.begin(); }
|
---|
67 | const_iterator uncheckedEnd() const { return m_map.end(); }
|
---|
68 |
|
---|
69 | private:
|
---|
70 | HashMap<KeyType, MappedType> m_map;
|
---|
71 | };
|
---|
72 |
|
---|
73 | template<typename KeyType, typename MappedType>
|
---|
74 | MappedType WeakGCMap<KeyType, MappedType>::get(const KeyType& key) const
|
---|
75 | {
|
---|
76 | MappedType result = m_map.get(key);
|
---|
77 | if (result == HashTraits<MappedType>::emptyValue())
|
---|
78 | return result;
|
---|
79 | if (!Heap::isCellMarked(result))
|
---|
80 | return HashTraits<MappedType>::emptyValue();
|
---|
81 | return result;
|
---|
82 | }
|
---|
83 |
|
---|
84 | template<typename KeyType, typename MappedType>
|
---|
85 | MappedType WeakGCMap<KeyType, MappedType>::take(const KeyType& key)
|
---|
86 | {
|
---|
87 | MappedType result = m_map.take(key);
|
---|
88 | if (result == HashTraits<MappedType>::emptyValue())
|
---|
89 | return result;
|
---|
90 | if (!Heap::isCellMarked(result))
|
---|
91 | return HashTraits<MappedType>::emptyValue();
|
---|
92 | return result;
|
---|
93 | }
|
---|
94 |
|
---|
95 | template<typename KeyType, typename MappedType>
|
---|
96 | pair<typename HashMap<KeyType, MappedType>::iterator, bool> WeakGCMap<KeyType, MappedType>::set(const KeyType& key, const MappedType& value)
|
---|
97 | {
|
---|
98 | Heap::markCell(value); // If value is newly allocated, it's not marked, so mark it now.
|
---|
99 | pair<iterator, bool> result = m_map.add(key, value);
|
---|
100 | if (!result.second) { // pre-existing entry
|
---|
101 | result.second = !Heap::isCellMarked(result.first->second);
|
---|
102 | result.first->second = value;
|
---|
103 | }
|
---|
104 | return result;
|
---|
105 | }
|
---|
106 |
|
---|
107 | template<typename KeyType, typename MappedType>
|
---|
108 | bool WeakGCMap<KeyType, MappedType>::uncheckedRemove(const KeyType& key, const MappedType& value)
|
---|
109 | {
|
---|
110 | iterator it = m_map.find(key);
|
---|
111 | if (it == m_map.end())
|
---|
112 | return false;
|
---|
113 | if (it->second != value)
|
---|
114 | return false;
|
---|
115 | m_map.remove(it);
|
---|
116 | return true;
|
---|
117 | }
|
---|
118 |
|
---|
119 | } // namespace JSC
|
---|
120 |
|
---|
121 | #endif // WeakGCMap_h
|
---|