| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#ifndef WeakGCMap_h |
| 27 |
#define WeakGCMap_h |
| 28 |
|
| 29 |
#include "Collector.h" |
| 30 |
#include <wtf/HashMap.h> |
| 31 |
|
| 32 |
namespace JSC { |
| 33 |
|
| 34 |
class JSCell; |
| 35 |
|
| 36 |
|
| 37 |
template<typename KeyType, typename MappedType> |
| 38 |
class WeakGCMap : public FastAllocBase { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public: |
| 48 |
typedef typename HashMap<KeyType, MappedType>::iterator iterator; |
| 49 |
typedef typename HashMap<KeyType, MappedType>::const_iterator const_iterator; |
| 50 |
|
| 51 |
bool isEmpty() { return m_map.isEmpty(); } |
| 52 |
|
| 53 |
MappedType get(const KeyType& key) const; |
| 54 |
pair<iterator, bool> set(const KeyType&, const MappedType&); |
| 55 |
MappedType take(const KeyType& key); |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
MappedType uncheckedGet(const KeyType& key) const { return m_map.get(key); } |
| 62 |
bool uncheckedRemove(const KeyType&, const MappedType&); |
| 63 |
|
| 64 |
iterator uncheckedBegin() { return m_map.begin(); } |
| 65 |
iterator uncheckedEnd() { return m_map.end(); } |
| 66 |
|
| 67 |
const_iterator uncheckedBegin() const { return m_map.begin(); } |
| 68 |
const_iterator uncheckedEnd() const { return m_map.end(); } |
| 69 |
|
| 70 |
private: |
| 71 |
HashMap<KeyType, MappedType> m_map; |
| 72 |
}; |
| 73 |
|
| 74 |
template<typename KeyType, typename MappedType> |
| 75 |
inline MappedType WeakGCMap<KeyType, MappedType>::get(const KeyType& key) const |
| 76 |
{ |
| 77 |
MappedType result = m_map.get(key); |
| 78 |
if (result == HashTraits<MappedType>::emptyValue()) |
| 79 |
return result; |
| 80 |
if (!Heap::isCellMarked(result)) |
| 81 |
return HashTraits<MappedType>::emptyValue(); |
| 82 |
return result; |
| 83 |
} |
| 84 |
|
| 85 |
template<typename KeyType, typename MappedType> |
| 86 |
MappedType WeakGCMap<KeyType, MappedType>::take(const KeyType& key) |
| 87 |
{ |
| 88 |
MappedType result = m_map.take(key); |
| 89 |
if (result == HashTraits<MappedType>::emptyValue()) |
| 90 |
return result; |
| 91 |
if (!Heap::isCellMarked(result)) |
| 92 |
return HashTraits<MappedType>::emptyValue(); |
| 93 |
return result; |
| 94 |
} |
| 95 |
|
| 96 |
template<typename KeyType, typename MappedType> |
| 97 |
pair<typename HashMap<KeyType, MappedType>::iterator, bool> WeakGCMap<KeyType, MappedType>::set(const KeyType& key, const MappedType& value) |
| 98 |
{ |
| 99 |
Heap::markCell(value); |
| 100 |
pair<iterator, bool> result = m_map.add(key, value); |
| 101 |
if (!result.second) { |
| 102 |
result.second = !Heap::isCellMarked(result.first->second); |
| 103 |
result.first->second = value; |
| 104 |
} |
| 105 |
return result; |
| 106 |
} |
| 107 |
|
| 108 |
template<typename KeyType, typename MappedType> |
| 109 |
bool WeakGCMap<KeyType, MappedType>::uncheckedRemove(const KeyType& key, const MappedType& value) |
| 110 |
{ |
| 111 |
iterator it = m_map.find(key); |
| 112 |
if (it == m_map.end()) |
| 113 |
return false; |
| 114 |
if (it->second != value) |
| 115 |
return false; |
| 116 |
m_map.remove(it); |
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
#endif |