OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef WeakIdentifierMap_h | 5 #ifndef WeakIdentifierMap_h |
6 #define WeakIdentifierMap_h | 6 #define WeakIdentifierMap_h |
7 | 7 |
| 8 #include "platform/heap/Handle.h" |
8 #include "wtf/HashMap.h" | 9 #include "wtf/HashMap.h" |
| 10 #include "wtf/Vector.h" |
9 | 11 |
10 namespace blink { | 12 namespace blink { |
| 13 |
| 14 #if !ENABLE(OILPAN) |
11 template<typename T> struct WeakIdentifierMapTraits { | 15 template<typename T> struct WeakIdentifierMapTraits { |
12 static void removedFromIdentifierMap(T*) { } | 16 static void removedFromIdentifierMap(T*) { } |
13 static void addedToIdentifierMap(T*) { } | 17 static void addedToIdentifierMap(T*) { } |
14 }; | 18 }; |
15 | 19 |
16 template<typename T, typename Traits = WeakIdentifierMapTraits<T>> class WeakIde
ntifierMap { | 20 template<typename T, typename Traits = WeakIdentifierMapTraits<T>> class WeakIde
ntifierMap { |
17 public: | 21 public: |
18 ~WeakIdentifierMap() | 22 ~WeakIdentifierMap() |
19 { | 23 { |
20 ObjectToWeakIdentifierMaps& allMaps = ObjectToWeakIdentifierMaps::instan
ce(); | 24 ObjectToWeakIdentifierMaps& allMaps = ObjectToWeakIdentifierMaps::instan
ce(); |
21 for (auto& map : m_objectToIdentifier) { | 25 for (auto& map : m_objectToIdentifier) { |
22 T* object = map.key; | 26 T* object = map.key; |
23 if (allMaps.removedFromMap(object, this)) | 27 if (allMaps.removedFromMap(object, this)) |
24 Traits::removedFromIdentifierMap(object); | 28 Traits::removedFromIdentifierMap(object); |
25 } | 29 } |
26 } | 30 } |
27 | 31 |
28 void put(T* object, int identifier) | |
29 { | |
30 ASSERT(object && !m_objectToIdentifier.contains(object)); | |
31 m_objectToIdentifier.set(object, identifier); | |
32 m_identifierToObject.set(identifier, object); | |
33 | |
34 ObjectToWeakIdentifierMaps& maps = ObjectToWeakIdentifierMaps::instance(
); | |
35 if (maps.addedToMap(object, this)) | |
36 Traits::addedToIdentifierMap(object); | |
37 } | |
38 | |
39 int identifier(T* object) | 32 int identifier(T* object) |
40 { | 33 { |
41 return m_objectToIdentifier.get(object); | 34 int result = m_objectToIdentifier.get(object); |
| 35 if (!result) { |
| 36 static int s_lastId = 0; |
| 37 result = ++s_lastId; |
| 38 put(object, result); |
| 39 } |
| 40 return result; |
42 } | 41 } |
43 | 42 |
44 T* lookup(int identifier) | 43 T* lookup(int identifier) |
45 { | 44 { |
46 return m_identifierToObject.get(identifier); | 45 return m_identifierToObject.get(identifier); |
47 } | 46 } |
48 | 47 |
49 static void notifyObjectDestroyed(T* object) | 48 static void notifyObjectDestroyed(T* object) |
50 { | 49 { |
51 ObjectToWeakIdentifierMaps::instance().objectDestroyed(object); | 50 ObjectToWeakIdentifierMaps::instance().objectDestroyed(object); |
52 } | 51 } |
53 | 52 |
54 private: | 53 private: |
| 54 void put(T* object, int identifier) |
| 55 { |
| 56 ASSERT(object && !m_objectToIdentifier.contains(object)); |
| 57 m_objectToIdentifier.set(object, identifier); |
| 58 m_identifierToObject.set(identifier, object); |
| 59 |
| 60 ObjectToWeakIdentifierMaps& maps = ObjectToWeakIdentifierMaps::instance(
); |
| 61 if (maps.addedToMap(object, this)) |
| 62 Traits::addedToIdentifierMap(object); |
| 63 } |
| 64 |
55 class ObjectToWeakIdentifierMaps { | 65 class ObjectToWeakIdentifierMaps { |
56 typedef WeakIdentifierMap<T> IdentifierMap; | 66 typedef WeakIdentifierMap<T> IdentifierMap; |
57 public: | 67 public: |
58 bool addedToMap(T* object, IdentifierMap* map) | 68 bool addedToMap(T* object, IdentifierMap* map) |
59 { | 69 { |
60 typename ObjectToMapList::AddResult result = m_objectToMapList.add(o
bject, nullptr); | 70 typename ObjectToMapList::AddResult result = m_objectToMapList.add(o
bject, nullptr); |
61 if (result.isNewEntry) | 71 if (result.isNewEntry) |
62 result.storedValue->value = adoptPtr(new MapList()); | 72 result.storedValue->value = adoptPtr(new MapList()); |
63 result.storedValue->value->append(map); | 73 result.storedValue->value->append(map); |
64 return result.isNewEntry; | 74 return result.isNewEntry; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 }; | 109 }; |
100 | 110 |
101 void objectDestroyed(T* object) | 111 void objectDestroyed(T* object) |
102 { | 112 { |
103 int identifier = m_objectToIdentifier.take(object); | 113 int identifier = m_objectToIdentifier.take(object); |
104 ASSERT(identifier); | 114 ASSERT(identifier); |
105 m_identifierToObject.remove(identifier); | 115 m_identifierToObject.remove(identifier); |
106 } | 116 } |
107 | 117 |
108 typedef HashMap<T*, int> ObjectToIdentifier; | 118 typedef HashMap<T*, int> ObjectToIdentifier; |
| 119 typedef HashMap<int, T*> IdentifierToObject; |
| 120 |
109 ObjectToIdentifier m_objectToIdentifier; | 121 ObjectToIdentifier m_objectToIdentifier; |
110 typedef HashMap<int, T*> IdentifierToObject; | |
111 IdentifierToObject m_identifierToObject; | 122 IdentifierToObject m_identifierToObject; |
112 }; | 123 }; |
113 | 124 |
| 125 #else // ENABLE(OILPAN) |
| 126 |
| 127 template<typename T> class WeakIdentifierMap : public GarbageCollected<WeakIdent
ifierMap<T>> { |
| 128 public: |
| 129 WeakIdentifierMap() |
| 130 : m_objectToIdentifier(new ObjectToIdentifier()) |
| 131 , m_identifierToObject(new IdentifierToObject()) |
| 132 { |
| 133 } |
| 134 |
| 135 int identifier(T* object) |
| 136 { |
| 137 int result = m_objectToIdentifier->get(object); |
| 138 if (!result) { |
| 139 static int s_lastId = 0; |
| 140 result = ++s_lastId; |
| 141 put(object, result); |
| 142 } |
| 143 return result; |
| 144 } |
| 145 |
| 146 T* lookup(int identifier) |
| 147 { |
| 148 return m_identifierToObject->get(identifier); |
| 149 } |
| 150 |
| 151 static void notifyObjectDestroyed(T* object) { } |
| 152 |
| 153 DEFINE_INLINE_TRACE() |
| 154 { |
| 155 visitor->trace(m_objectToIdentifier); |
| 156 visitor->trace(m_identifierToObject); |
| 157 } |
| 158 |
| 159 private: |
| 160 void put(T* object, int identifier) |
| 161 { |
| 162 ASSERT(object && !m_objectToIdentifier->contains(object)); |
| 163 m_objectToIdentifier->set(object, identifier); |
| 164 m_identifierToObject->set(identifier, object); |
| 165 } |
| 166 |
| 167 typedef HeapHashMap<WeakMember<T>, int> ObjectToIdentifier; |
| 168 typedef HeapHashMap<int, WeakMember<T>> IdentifierToObject; |
| 169 |
| 170 Member<ObjectToIdentifier> m_objectToIdentifier; |
| 171 Member<IdentifierToObject> m_identifierToObject; |
| 172 }; |
| 173 |
| 174 #endif // ENABLE(OILPAN) |
| 175 |
114 } | 176 } |
| 177 |
115 #endif // WeakIdentifierMap_h | 178 #endif // WeakIdentifierMap_h |
OLD | NEW |