| 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 "platform/heap/Handle.h" |
| 9 #include "wtf/HashMap.h" | 9 #include "wtf/HashMap.h" |
| 10 #include "wtf/Vector.h" | 10 #include "wtf/Vector.h" |
| 11 | 11 |
| 12 namespace blink { | 12 namespace blink { |
| 13 | 13 |
| 14 #if !ENABLE(OILPAN) | 14 template<typename T> struct IdentifierGenerator; |
| 15 |
| 16 template<> struct IdentifierGenerator<int> { |
| 17 using IdentifierType = int; |
| 18 static IdentifierType next() |
| 19 { |
| 20 static int s_lastId = 0; |
| 21 return ++s_lastId; |
| 22 } |
| 23 }; |
| 24 |
| 15 template<typename T> struct WeakIdentifierMapTraits { | 25 template<typename T> struct WeakIdentifierMapTraits { |
| 16 static void removedFromIdentifierMap(T*) { } | 26 static void removedFromIdentifierMap(T*) { } |
| 17 static void addedToIdentifierMap(T*) { } | 27 static void addedToIdentifierMap(T*) { } |
| 18 }; | 28 }; |
| 19 | 29 |
| 20 template<typename T, typename Traits = WeakIdentifierMapTraits<T>> class WeakIde
ntifierMap { | 30 template<typename T, |
| 31 typename Generator = IdentifierGenerator<int>, |
| 32 typename Traits = WeakIdentifierMapTraits<T>, |
| 33 bool isGarbageCollected = IsGarbageCollectedType<T>::value> class WeakIdenti
fierMap; |
| 34 |
| 35 template<typename T, typename Generator, typename Traits> class WeakIdentifierMa
p<T, Generator, Traits, false> { |
| 21 public: | 36 public: |
| 37 using IdentifierType = typename Generator::IdentifierType; |
| 38 using ReferenceType = RawPtr<WeakIdentifierMap<T, Generator, Traits, false>>
; |
| 39 |
| 22 ~WeakIdentifierMap() | 40 ~WeakIdentifierMap() |
| 23 { | 41 { |
| 24 ObjectToWeakIdentifierMaps& allMaps = ObjectToWeakIdentifierMaps::instan
ce(); | 42 ObjectToWeakIdentifierMaps& allMaps = ObjectToWeakIdentifierMaps::instan
ce(); |
| 25 for (auto& map : m_objectToIdentifier) { | 43 for (auto& map : m_objectToIdentifier) { |
| 26 T* object = map.key; | 44 T* object = map.key; |
| 27 if (allMaps.removedFromMap(object, this)) | 45 if (allMaps.removedFromMap(object, this)) |
| 28 Traits::removedFromIdentifierMap(object); | 46 Traits::removedFromIdentifierMap(object); |
| 29 } | 47 } |
| 30 } | 48 } |
| 31 | 49 |
| 32 int identifier(T* object) | 50 IdentifierType identifier(T* object) |
| 33 { | 51 { |
| 34 int result = m_objectToIdentifier.get(object); | 52 IdentifierType result = m_objectToIdentifier.get(object); |
| 35 if (!result) { | 53 |
| 36 static int s_lastId = 0; | 54 if (WTF::isHashTraitsEmptyValue<HashTraits<IdentifierType>>(result)) { |
| 37 result = ++s_lastId; | 55 result = Generator::next(); |
| 38 put(object, result); | 56 put(object, result); |
| 39 } | 57 } |
| 40 return result; | 58 return result; |
| 41 } | 59 } |
| 42 | 60 |
| 43 T* lookup(int identifier) | 61 T* lookup(IdentifierType identifier) |
| 44 { | 62 { |
| 45 return m_identifierToObject.get(identifier); | 63 return m_identifierToObject.get(identifier); |
| 46 } | 64 } |
| 47 | 65 |
| 48 static void notifyObjectDestroyed(T* object) | 66 static void notifyObjectDestroyed(T* object) |
| 49 { | 67 { |
| 50 ObjectToWeakIdentifierMaps::instance().objectDestroyed(object); | 68 ObjectToWeakIdentifierMaps::instance().objectDestroyed(object); |
| 51 } | 69 } |
| 52 | 70 |
| 53 private: | 71 private: |
| 54 void put(T* object, int identifier) | 72 void put(T* object, IdentifierType identifier) |
| 55 { | 73 { |
| 56 ASSERT(object && !m_objectToIdentifier.contains(object)); | 74 ASSERT(object && !m_objectToIdentifier.contains(object)); |
| 57 m_objectToIdentifier.set(object, identifier); | 75 m_objectToIdentifier.set(object, identifier); |
| 58 m_identifierToObject.set(identifier, object); | 76 m_identifierToObject.set(identifier, object); |
| 59 | 77 |
| 60 ObjectToWeakIdentifierMaps& maps = ObjectToWeakIdentifierMaps::instance(
); | 78 ObjectToWeakIdentifierMaps& maps = ObjectToWeakIdentifierMaps::instance(
); |
| 61 if (maps.addedToMap(object, this)) | 79 if (maps.addedToMap(object, this)) |
| 62 Traits::addedToIdentifierMap(object); | 80 Traits::addedToIdentifierMap(object); |
| 63 } | 81 } |
| 64 | 82 |
| 65 class ObjectToWeakIdentifierMaps { | 83 class ObjectToWeakIdentifierMaps { |
| 66 typedef WeakIdentifierMap<T> IdentifierMap; | 84 using IdentifierMap = WeakIdentifierMap<T, Generator, Traits>; |
| 67 public: | 85 public: |
| 68 bool addedToMap(T* object, IdentifierMap* map) | 86 bool addedToMap(T* object, IdentifierMap* map) |
| 69 { | 87 { |
| 70 typename ObjectToMapList::AddResult result = m_objectToMapList.add(o
bject, nullptr); | 88 typename ObjectToMapList::AddResult result = m_objectToMapList.add(o
bject, nullptr); |
| 71 if (result.isNewEntry) | 89 if (result.isNewEntry) |
| 72 result.storedValue->value = adoptPtr(new MapList()); | 90 result.storedValue->value = adoptPtr(new MapList()); |
| 73 result.storedValue->value->append(map); | 91 result.storedValue->value->append(map); |
| 74 return result.isNewEntry; | 92 return result.isNewEntry; |
| 75 } | 93 } |
| 76 | 94 |
| 77 bool removedFromMap(T* object, IdentifierMap* map) | 95 bool removedFromMap(T* object, IdentifierMap* map) |
| 78 { | 96 { |
| 79 typename ObjectToMapList::iterator it = m_objectToMapList.find(objec
t); | 97 typename ObjectToMapList::iterator it = m_objectToMapList.find(objec
t); |
| 80 ASSERT(it != m_objectToMapList.end()); | 98 ASSERT(it != m_objectToMapList.end()); |
| 81 MapList* mapList = it->value.get(); | 99 MapList* mapList = it->value.get(); |
| 82 size_t position = mapList->find(map); | 100 size_t position = mapList->find(map); |
| 83 ASSERT(position != kNotFound); | 101 ASSERT(position != kNotFound); |
| 84 mapList->remove(position); | 102 mapList->remove(position); |
| 85 if (mapList->isEmpty()) { | 103 if (mapList->isEmpty()) { |
| 86 m_objectToMapList.remove(it); | 104 m_objectToMapList.remove(it); |
| 87 return true; | 105 return true; |
| 88 } | 106 } |
| 89 return false; | 107 return false; |
| 90 } | 108 } |
| 91 | 109 |
| 92 void objectDestroyed(T* object) | 110 void objectDestroyed(T* object) |
| 93 { | 111 { |
| 94 OwnPtr<MapList> maps = m_objectToMapList.take(object); | 112 if (OwnPtr<MapList> maps = m_objectToMapList.take(object)) { |
| 95 for (auto& map : *maps) | 113 for (auto& map : *maps) |
| 96 map->objectDestroyed(object); | 114 map->objectDestroyed(object); |
| 115 } |
| 97 } | 116 } |
| 98 | 117 |
| 99 static ObjectToWeakIdentifierMaps& instance() | 118 static ObjectToWeakIdentifierMaps& instance() |
| 100 { | 119 { |
| 101 DEFINE_STATIC_LOCAL(ObjectToWeakIdentifierMaps, self, ()); | 120 DEFINE_STATIC_LOCAL(ObjectToWeakIdentifierMaps, self, ()); |
| 102 return self; | 121 return self; |
| 103 } | 122 } |
| 104 | 123 |
| 105 private: | 124 private: |
| 106 typedef Vector<IdentifierMap*, 1> MapList; | 125 using MapList = Vector<IdentifierMap*, 1>; |
| 107 typedef HashMap<T*, OwnPtr<MapList>> ObjectToMapList; | 126 using ObjectToMapList = HashMap<T*, OwnPtr<MapList>>; |
| 108 ObjectToMapList m_objectToMapList; | 127 ObjectToMapList m_objectToMapList; |
| 109 }; | 128 }; |
| 110 | 129 |
| 111 void objectDestroyed(T* object) | 130 void objectDestroyed(T* object) |
| 112 { | 131 { |
| 113 int identifier = m_objectToIdentifier.take(object); | 132 int identifier = m_objectToIdentifier.take(object); |
| 114 ASSERT(identifier); | 133 ASSERT(identifier); |
| 115 m_identifierToObject.remove(identifier); | 134 m_identifierToObject.remove(identifier); |
| 116 } | 135 } |
| 117 | 136 |
| 118 typedef HashMap<T*, int> ObjectToIdentifier; | 137 using ObjectToIdentifier = HashMap<T*, IdentifierType>; |
| 119 typedef HashMap<int, T*> IdentifierToObject; | 138 using IdentifierToObject = HashMap<IdentifierType, T*>; |
| 120 | 139 |
| 121 ObjectToIdentifier m_objectToIdentifier; | 140 ObjectToIdentifier m_objectToIdentifier; |
| 122 IdentifierToObject m_identifierToObject; | 141 IdentifierToObject m_identifierToObject; |
| 123 }; | 142 }; |
| 124 | 143 |
| 125 #else // ENABLE(OILPAN) | 144 template<typename T, typename Generator, typename Traits> class WeakIdentifierMa
p<T, Generator, Traits, true> |
| 145 : public GarbageCollected<WeakIdentifierMap<T, Generator, Traits, true>> { |
| 146 public: |
| 147 using IdentifierType = typename Generator::IdentifierType; |
| 148 using ReferenceType = Persistent<WeakIdentifierMap<T, Generator, Traits, tru
e>>; |
| 126 | 149 |
| 127 template<typename T> class WeakIdentifierMap : public GarbageCollected<WeakIdent
ifierMap<T>> { | |
| 128 public: | |
| 129 WeakIdentifierMap() | 150 WeakIdentifierMap() |
| 130 : m_objectToIdentifier(new ObjectToIdentifier()) | 151 : m_objectToIdentifier(new ObjectToIdentifier()) |
| 131 , m_identifierToObject(new IdentifierToObject()) | 152 , m_identifierToObject(new IdentifierToObject()) |
| 132 { | 153 { |
| 133 } | 154 } |
| 134 | 155 |
| 135 int identifier(T* object) | 156 IdentifierType identifier(T* object) |
| 136 { | 157 { |
| 137 int result = m_objectToIdentifier->get(object); | 158 IdentifierType result = m_objectToIdentifier->get(object); |
| 138 if (!result) { | 159 |
| 139 static int s_lastId = 0; | 160 if (WTF::isHashTraitsEmptyValue<HashTraits<IdentifierType>>(result)) { |
| 140 result = ++s_lastId; | 161 result = Generator::next(); |
| 141 put(object, result); | 162 put(object, result); |
| 142 } | 163 } |
| 143 return result; | 164 return result; |
| 144 } | 165 } |
| 145 | 166 |
| 146 T* lookup(int identifier) | 167 T* lookup(IdentifierType identifier) |
| 147 { | 168 { |
| 148 return m_identifierToObject->get(identifier); | 169 return m_identifierToObject->get(identifier); |
| 149 } | 170 } |
| 150 | 171 |
| 151 static void notifyObjectDestroyed(T* object) { } | 172 static void notifyObjectDestroyed(T* object) { } |
| 152 | 173 |
| 153 DEFINE_INLINE_TRACE() | 174 DEFINE_INLINE_TRACE() |
| 154 { | 175 { |
| 155 visitor->trace(m_objectToIdentifier); | 176 visitor->trace(m_objectToIdentifier); |
| 156 visitor->trace(m_identifierToObject); | 177 visitor->trace(m_identifierToObject); |
| 157 } | 178 } |
| 158 | 179 |
| 159 private: | 180 private: |
| 160 void put(T* object, int identifier) | 181 void put(T* object, IdentifierType identifier) |
| 161 { | 182 { |
| 162 ASSERT(object && !m_objectToIdentifier->contains(object)); | 183 ASSERT(object && !m_objectToIdentifier->contains(object)); |
| 163 m_objectToIdentifier->set(object, identifier); | 184 m_objectToIdentifier->set(object, identifier); |
| 164 m_identifierToObject->set(identifier, object); | 185 m_identifierToObject->set(identifier, object); |
| 165 } | 186 } |
| 166 | 187 |
| 167 typedef HeapHashMap<WeakMember<T>, int> ObjectToIdentifier; | 188 using ObjectToIdentifier = HeapHashMap<WeakMember<T>, IdentifierType>; |
| 168 typedef HeapHashMap<int, WeakMember<T>> IdentifierToObject; | 189 using IdentifierToObject = HeapHashMap<IdentifierType, WeakMember<T>>; |
| 169 | 190 |
| 170 Member<ObjectToIdentifier> m_objectToIdentifier; | 191 Member<ObjectToIdentifier> m_objectToIdentifier; |
| 171 Member<IdentifierToObject> m_identifierToObject; | 192 Member<IdentifierToObject> m_identifierToObject; |
| 172 }; | 193 }; |
| 173 | 194 |
| 174 #endif // ENABLE(OILPAN) | |
| 175 | |
| 176 } | 195 } |
| 177 | 196 |
| 178 #endif // WeakIdentifierMap_h | 197 #endif // WeakIdentifierMap_h |
| OLD | NEW |