Changeset 245972 in webkit
- Timestamp:
- May 31, 2019, 10:11:58 AM (7 years ago)
- Location:
- trunk
- Files:
-
- 8 edited
-
Source/WTF/ChangeLog (modified) (1 diff)
-
Source/WTF/wtf/WeakHashSet.h (modified) (9 diffs)
-
Source/WTF/wtf/WeakPtr.h (modified) (11 diffs)
-
Source/WebCore/ChangeLog (modified) (1 diff)
-
Source/WebCore/Modules/indexeddb/shared/InProcessIDBServer.cpp (modified) (1 diff)
-
Source/WebCore/Modules/indexeddb/shared/InProcessIDBServer.h (modified) (2 diffs)
-
Tools/ChangeLog (modified) (1 diff)
-
Tools/TestWebKitAPI/Tests/WTF/WeakPtr.cpp (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WTF/ChangeLog
r245968 r245972 1 2019-05-31 Geoffrey Garen <ggaren@apple.com> 2 3 Some WeakPtr cleanup 4 https://bugs.webkit.org/show_bug.cgi?id=198390 5 6 Reviewed by Chris Dumez. 7 8 * wtf/WeakHashSet.h: 9 (WTF::HashTraits<Ref<WeakPtrImpl>>::isReleasedWeakValue): 10 (WTF::WeakHashSet::WeakHashSetConstIterator::WeakHashSetConstIterator): 11 Updated for rename to WeakPtrImpl. 12 13 (WTF::WeakHashSet::WeakHashSetConstIterator::get const): Updated for new 14 get() interface. Also, switched to iterator operator* to help clarify 15 the double dereference here. 16 17 (WTF::WeakHashSet::add): 18 (WTF::WeakHashSet::remove): 19 (WTF::WeakHashSet::contains const): 20 (WTF::WeakHashSet::computeSize const): 21 (WTF::HashTraits<Ref<WeakReference>>::isReleasedWeakValue): Deleted. 22 Updated for rename to WeakPtrImpl. 23 24 * wtf/WeakPtr.h: 25 (WTF::WeakPtrImpl::create): 26 (WTF::WeakPtrImpl::~WeakPtrImpl): Renamed WeakReference to WeakPtrImpl. 27 Now we don't need a comment explaining that this class is the backing 28 implementation of WeakPtr. 29 30 (WTF::WeakPtrImpl::get): Return the pointer type we stored, rather than 31 the pointer type requested by our client. It's a little too surprising 32 for a field to store one pointer type and load another. 33 34 (WTF::WeakPtrImpl::WeakPtrImpl): Fixed a theoretical type safety bug. 35 Make sure to store T::WeakValueType* instead of T*, since they might 36 not be the same pointer value. (In practice, T and T::WeakValueType* 37 are always the same type in this constructor because WeakPtrFactory 38 makes them so, but it's best not to depend on implementation details 39 across classes.) 40 41 (WTF::WeakPtr::get const): Updated for new get() interface. 42 43 (WTF::WeakPtr::operator bool const): 44 (WTF::WeakPtr::operator=): 45 (WTF::WeakPtr::clear): 46 (WTF::WeakPtr::WeakPtr): Updated for WeakPtrImpl rename. 47 48 (WTF::WeakPtrFactory::~WeakPtrFactory): Updated for WeakPtrImpl rename. 49 50 (WTF::WeakPtrFactory::createWeakPtr const): ASSERT that the passed-in 51 pointer is equal to the stored pointer. As a space optimization, we 52 require our client to remind us what we point to each time a weak 53 pointer is created -- but nothing guarantees that our client will do 54 this correctly. 55 56 (WTF::WeakPtrFactory::revokeAll): Updated for WeakPtrImpl rename. 57 58 (WTF::CanMakeWeakPtr::weakPtrFactory const): 59 (WTF::CanMakeWeakPtr::weakPtrFactory): Use idiomatic accessor naming. 60 61 (WTF::weak_ptr_impl_cast): Fixed a theoretical type safety bug. 62 Previously, if Base and Derived both inherited CanMakeWeakPtr, and 63 you casted WeakPtr<Base> to WeakPtr<Derived> (or vice versa), and 64 casting Base <-> Derived required pointer fixup, the previous 65 compile-time check would accept the cast, even though the stored pointer 66 would be wrong. 67 68 (WTF::WeakPtr<T>::WeakPtr): 69 (WTF::=): 70 (WTF::makeWeakPtr): 71 (WTF::WeakReference::create): Deleted. 72 (WTF::WeakReference::~WeakReference): Deleted. 73 (WTF::WeakReference::get const): Deleted. 74 (WTF::WeakReference::operator bool const): Deleted. 75 (WTF::WeakReference::clear): Deleted. 76 (WTF::WeakReference::WeakReference): Deleted. 77 (WTF::weak_reference_cast): Deleted. Updated for rename to WeakPtrImpl. 78 79 Don't export WeakPtrImpl because it's an implmenetation detail and 80 it shouldn't be easy to use outside WTF. 81 1 82 2019-05-31 Don Olmstead <don.olmstead@sony.com> 2 83 -
trunk/Source/WTF/wtf/WeakHashSet.h
r245868 r245972 33 33 namespace WTF { 34 34 35 template<> struct HashTraits<Ref<Weak Reference>> : RefHashTraits<WeakReference> {35 template<> struct HashTraits<Ref<WeakPtrImpl>> : RefHashTraits<WeakPtrImpl> { 36 36 static const bool hasIsReleasedWeakValueFunction = true; 37 static bool isReleasedWeakValue(const Ref<Weak Reference>& value)37 static bool isReleasedWeakValue(const Ref<WeakPtrImpl>& value) 38 38 { 39 39 return !value.isHashTableDeletedValue() && !value.isHashTableEmptyValue() && !value.get(); … … 44 44 class WeakHashSet { 45 45 public: 46 typedef HashSet<Ref<Weak Reference>> WeakReferenceSet;46 typedef HashSet<Ref<WeakPtrImpl>> WeakPtrImplSet; 47 47 48 48 class WeakHashSetConstIterator : public std::iterator<std::forward_iterator_tag, T, std::ptrdiff_t, const T*, const T&> { 49 49 private: 50 WeakHashSetConstIterator(const Weak ReferenceSet& set, typename WeakReferenceSet::const_iterator position)50 WeakHashSetConstIterator(const WeakPtrImplSet& set, typename WeakPtrImplSet::const_iterator position) 51 51 : m_position(position), m_endPosition(set.end()) 52 52 { … … 55 55 56 56 public: 57 T* get() const { return m_position->get().template get<T, typename T::WeakValueType>(); }57 T* get() const { return static_cast<T*>((*m_position)->template get<T>()); } 58 58 T& operator*() const { return *get(); } 59 59 T* operator->() const { return get(); } … … 86 86 template <typename> friend class WeakHashSet; 87 87 88 typename Weak ReferenceSet::const_iterator m_position;89 typename Weak ReferenceSet::const_iterator m_endPosition;88 typename WeakPtrImplSet::const_iterator m_position; 89 typename WeakPtrImplSet::const_iterator m_endPosition; 90 90 }; 91 91 typedef WeakHashSetConstIterator const_iterator; … … 99 99 void add(const U& value) 100 100 { 101 m_set.add(*makeWeakPtr<T>(const_cast<U&>(value)).m_ ref);101 m_set.add(*makeWeakPtr<T>(const_cast<U&>(value)).m_impl); 102 102 } 103 103 … … 105 105 bool remove(const U& value) 106 106 { 107 auto& weak Reference = value.weakPtrFactory().m_ref;108 if (!weak Reference || !*weakReference)107 auto& weakPtrImpl = value.weakPtrFactory().m_impl; 108 if (!weakPtrImpl || !*weakPtrImpl) 109 109 return false; 110 return m_set.remove(*weak Reference);110 return m_set.remove(*weakPtrImpl); 111 111 } 112 112 … … 114 114 bool contains(const U& value) const 115 115 { 116 auto& weak Reference = value.weakPtrFactory().m_ref;117 if (!weak Reference || !*weakReference)116 auto& weakPtrImpl = value.weakPtrFactory().m_impl; 117 if (!weakPtrImpl || !*weakPtrImpl) 118 118 return false; 119 return m_set.contains(*weak Reference);119 return m_set.contains(*weakPtrImpl); 120 120 } 121 121 … … 131 131 unsigned computeSize() const 132 132 { 133 const_cast<Weak ReferenceSet&>(m_set).removeIf([] (auto& value) { return !value.get(); });133 const_cast<WeakPtrImplSet&>(m_set).removeIf([] (auto& value) { return !value.get(); }); 134 134 return m_set.size(); 135 135 } … … 142 142 143 143 private: 144 Weak ReferenceSet m_set;144 WeakPtrImplSet m_set; 145 145 }; 146 146 -
trunk/Source/WTF/wtf/WeakPtr.h
r245868 r245972 35 35 36 36 // Testing interface for TestWebKitAPI 37 #ifndef DID_CREATE_WEAK_ REFERENCE38 #define DID_CREATE_WEAK_ REFERENCE(p)37 #ifndef DID_CREATE_WEAK_PTR_IMPL 38 #define DID_CREATE_WEAK_PTR_IMPL(p) 39 39 #endif 40 #ifndef WILL_DESTROY_WEAK_ REFERENCE41 #define WILL_DESTROY_WEAK_ REFERENCE(p)40 #ifndef WILL_DESTROY_WEAK_PTR_IMPL 41 #define WILL_DESTROY_WEAK_PTR_IMPL(p) 42 42 #endif 43 43 … … 46 46 template<typename> class WeakPtrFactory; 47 47 48 // Note: WeakReference is an implementation detail, and should not be used directly. 49 class WeakReference : public ThreadSafeRefCounted<WeakReference> { 50 WTF_MAKE_NONCOPYABLE(WeakReference); 48 class WeakPtrImpl : public ThreadSafeRefCounted<WeakPtrImpl> { 49 WTF_MAKE_NONCOPYABLE(WeakPtrImpl); 51 50 WTF_MAKE_FAST_ALLOCATED; 52 51 public: 53 template<typename T> static Ref<WeakReference> create(T* ptr) { return adoptRef(*new WeakReference(ptr)); } 54 55 ~WeakReference() 56 { 57 WILL_DESTROY_WEAK_REFERENCE(m_ptr); 58 } 59 60 template<typename T, typename WeakValueType> T* get() const { return static_cast<T*>(static_cast<WeakValueType*>(m_ptr)); } 52 template<typename T> static Ref<WeakPtrImpl> create(T* ptr) 53 { 54 return adoptRef(*new WeakPtrImpl(ptr)); 55 } 56 57 ~WeakPtrImpl() 58 { 59 WILL_DESTROY_WEAK_PTR_IMPL(m_ptr); 60 } 61 62 template<typename T> typename T::WeakValueType* get() 63 { 64 return static_cast<typename T::WeakValueType*>(m_ptr); 65 } 66 61 67 explicit operator bool() const { return m_ptr; } 62 63 68 void clear() { m_ptr = nullptr; } 64 69 65 70 private: 66 template<typename T> explicit Weak Reference(T* ptr)67 : m_ptr( ptr)68 { 69 DID_CREATE_WEAK_ REFERENCE(ptr);71 template<typename T> explicit WeakPtrImpl(T* ptr) 72 : m_ptr(static_cast<typename T::WeakValueType*>(ptr)) 73 { 74 DID_CREATE_WEAK_PTR_IMPL(ptr); 70 75 } 71 76 … … 82 87 template<typename U> WeakPtr(WeakPtr<U>&&); 83 88 84 T* get() const { return m_ ref ? m_ref->template get<T, typename T::WeakValueType>() : nullptr; }85 explicit operator bool() const { return m_ ref && *m_ref; }86 87 WeakPtr& operator=(std::nullptr_t) { m_ ref= nullptr; return *this; }89 T* get() const { return m_impl ? static_cast<T*>(m_impl->get<T>()) : nullptr; } 90 explicit operator bool() const { return m_impl && *m_impl; } 91 92 WeakPtr& operator=(std::nullptr_t) { m_impl = nullptr; return *this; } 88 93 template<typename U> WeakPtr& operator=(const WeakPtr<U>&); 89 94 template<typename U> WeakPtr& operator=(WeakPtr<U>&&); … … 92 97 T& operator*() const { return *get(); } 93 98 94 void clear() { m_ ref= nullptr; }95 96 private: 97 explicit WeakPtr(Ref<Weak Reference>&& ref) : m_ref(std::move(ref)) { }99 void clear() { m_impl = nullptr; } 100 101 private: 102 explicit WeakPtr(Ref<WeakPtrImpl>&& ref) : m_impl(WTFMove(ref)) { } 98 103 template<typename> friend class WeakHashSet; 99 104 template<typename> friend class WeakPtr; … … 101 106 template<typename U> friend WeakPtr<U> makeWeakPtr(U&); 102 107 103 RefPtr<Weak Reference> m_ref;108 RefPtr<WeakPtrImpl> m_impl; 104 109 }; 105 110 … … 113 118 ~WeakPtrFactory() 114 119 { 115 if (!m_ ref)120 if (!m_impl) 116 121 return; 117 m_ref->clear(); 118 } 119 120 WeakPtr<T> createWeakPtr(T& ptr) const 121 { 122 if (!m_ref) 123 m_ref = WeakReference::create(&ptr); 124 return WeakPtr<T>(makeRef(*m_ref)); 125 } 126 127 WeakPtr<const T> createWeakPtr(const T& ptr) const 128 { 129 if (!m_ref) 130 m_ref = WeakReference::create(const_cast<T*>(&ptr)); 131 return WeakPtr<T>(makeRef(*m_ref)); 122 m_impl->clear(); 123 } 124 125 WeakPtr<T> createWeakPtr(T& object) const 126 { 127 if (!m_impl) 128 m_impl = WeakPtrImpl::create(&object); 129 130 ASSERT(&object == m_impl->get<T>()); 131 return WeakPtr<T>(makeRef(*m_impl)); 132 } 133 134 WeakPtr<const T> createWeakPtr(const T& object) const 135 { 136 if (!m_impl) 137 m_impl = WeakPtrImpl::create(const_cast<T*>(&object)); 138 139 ASSERT(&object == m_impl->get<T>()); 140 return WeakPtr<T>(makeRef(*m_impl)); 132 141 } 133 142 134 143 void revokeAll() 135 144 { 136 if (!m_ ref)145 if (!m_impl) 137 146 return; 138 147 139 m_ ref->clear();140 m_ ref= nullptr;148 m_impl->clear(); 149 m_impl = nullptr; 141 150 } 142 151 … … 144 153 template<typename> friend class WeakHashSet; 145 154 146 mutable RefPtr<Weak Reference> m_ref;155 mutable RefPtr<WeakPtrImpl> m_impl; 147 156 }; 148 157 … … 151 160 typedef T WeakValueType; 152 161 153 const WeakPtrFactory<T>& weakPtrFactory() const { return m_weak Factory; }154 WeakPtrFactory<T>& weakPtrFactory() { return m_weak Factory; }155 156 private: 157 WeakPtrFactory<T> m_weak Factory;158 }; 159 160 template<typename T, typename U> inline Weak Reference* weak_reference_cast(WeakReference* weakReference)161 { 162 UNUSED_VARIABLE(static_cast<T*>(static_cast<typename U::WeakValueType*>(nullptr))); // Verify that casting is valid.163 return weakReference;162 const WeakPtrFactory<T>& weakPtrFactory() const { return m_weakPtrFactory; } 163 WeakPtrFactory<T>& weakPtrFactory() { return m_weakPtrFactory; } 164 165 private: 166 WeakPtrFactory<T> m_weakPtrFactory; 167 }; 168 169 template<typename T, typename U> inline WeakPtrImpl* weak_ptr_impl_cast(WeakPtrImpl* impl) 170 { 171 static_assert(std::is_same<typename T::WeakValueType, typename U::WeakValueType>::value, "Invalid weak pointer cast"); 172 return impl; 164 173 } 165 174 166 175 template<typename T> template<typename U> inline WeakPtr<T>::WeakPtr(const WeakPtr<U>& o) 167 : m_ ref(weak_reference_cast<T, U>(o.m_ref.get()))176 : m_impl(weak_ptr_impl_cast<T, U>(o.m_impl.get())) 168 177 { 169 178 } 170 179 171 180 template<typename T> template<typename U> inline WeakPtr<T>::WeakPtr(WeakPtr<U>&& o) 172 : m_ ref(adoptRef(weak_reference_cast<T, U>(o.m_ref.leakRef())))181 : m_impl(adoptRef(weak_ptr_impl_cast<T, U>(o.m_impl.leakRef()))) 173 182 { 174 183 } … … 176 185 template<typename T> template<typename U> inline WeakPtr<T>& WeakPtr<T>::operator=(const WeakPtr<U>& o) 177 186 { 178 m_ ref = weak_reference_cast<T, U>(o.m_ref.get());187 m_impl = weak_ptr_impl_cast<T, U>(o.m_impl.get()); 179 188 return *this; 180 189 } … … 182 191 template<typename T> template<typename U> inline WeakPtr<T>& WeakPtr<T>::operator=(WeakPtr<U>&& o) 183 192 { 184 m_ ref = adoptRef(weak_reference_cast<T, U>(o.m_ref.leakRef()));193 m_impl = adoptRef(weak_ptr_impl_cast<T, U>(o.m_impl.leakRef())); 185 194 return *this; 186 195 } 187 196 188 template<typename T> inline WeakPtr<T> makeWeakPtr(T& ref)189 { 190 return { ref.weakPtrFactory().createWeakPtr(ref) };197 template<typename T> inline WeakPtr<T> makeWeakPtr(T& object) 198 { 199 return { object.weakPtrFactory().createWeakPtr(object) }; 191 200 } 192 201 … … 233 242 using WTF::WeakPtr; 234 243 using WTF::WeakPtrFactory; 235 using WTF::WeakReference;236 244 using WTF::makeWeakPtr; -
trunk/Source/WebCore/ChangeLog
r245970 r245972 1 2019-05-31 Geoffrey Garen <ggaren@apple.com> 2 3 Some WeakPtr cleanup 4 https://bugs.webkit.org/show_bug.cgi?id=198390 5 6 Reviewed by Chris Dumez. 7 8 * Modules/indexeddb/shared/InProcessIDBServer.cpp: 9 (WebCore::storageQuotaManagerGetter): Dereference the weak pointer 10 directly instead of using a weak pointer to guard a raw pointer. It's 11 safer and more idiomatic to use weak pointers directly. 12 13 * Modules/indexeddb/shared/InProcessIDBServer.h: Use our base clase 14 weakPtrFactory() definition instead of writing our own. Declare 15 WeakValueType so we can dereference the weak pointer we create (above). 16 1 17 2019-05-31 Zalan Bujtas <zalan@apple.com> 2 18 -
trunk/Source/WebCore/Modules/indexeddb/shared/InProcessIDBServer.cpp
r242911 r245972 70 70 static inline IDBServer::IDBServer::QuotaManagerGetter storageQuotaManagerGetter(InProcessIDBServer& server) 71 71 { 72 return [ &server,weakServer = makeWeakPtr(server)](PAL::SessionID, const auto& origin) {73 return weakServer ? server.quotaManager(origin) : nullptr;72 return [weakServer = makeWeakPtr(server)](PAL::SessionID, const auto& origin) { 73 return weakServer ? weakServer->quotaManager(origin) : nullptr; 74 74 }; 75 75 } -
trunk/Source/WebCore/Modules/indexeddb/shared/InProcessIDBServer.h
r242911 r245972 54 54 class InProcessIDBServer final : public IDBClient::IDBConnectionToServerDelegate, public IDBServer::IDBConnectionToClientDelegate, public RefCounted<InProcessIDBServer>, public IDBServer::IDBBackingStoreTemporaryFileHandler { 55 55 public: 56 using IDBClient::IDBConnectionToServerDelegate::weakPtrFactory; 57 typedef IDBClient::IDBConnectionToServerDelegate::WeakValueType WeakValueType; 58 56 59 WEBCORE_EXPORT static Ref<InProcessIDBServer> create(PAL::SessionID); 57 60 WEBCORE_EXPORT static Ref<InProcessIDBServer> create(PAL::SessionID, const String& databaseDirectoryPath); … … 125 128 StorageQuotaManager* quotaManager(const ClientOrigin&); 126 129 127 const WeakPtrFactory<IDBClient::IDBConnectionToServerDelegate>& weakPtrFactory() const { return IDBClient::IDBConnectionToServerDelegate::weakPtrFactory(); }128 129 130 private: 130 131 explicit InProcessIDBServer(PAL::SessionID); -
trunk/Tools/ChangeLog
r245968 r245972 1 2019-05-31 Geoffrey Garen <ggaren@apple.com> 2 3 Some WeakPtr cleanup 4 https://bugs.webkit.org/show_bug.cgi?id=198390 5 6 Reviewed by Chris Dumez. 7 8 * TestWebKitAPI/Tests/WTF/WeakPtr.cpp: Updated for rename. 9 1 10 2019-05-31 Don Olmstead <don.olmstead@sony.com> 2 11 -
trunk/Tools/TestWebKitAPI/Tests/WTF/WeakPtr.cpp
r245868 r245972 28 28 static unsigned s_baseWeakReferences = 0; 29 29 30 #define DID_CREATE_WEAK_ REFERENCE(p) do { \30 #define DID_CREATE_WEAK_PTR_IMPL(p) do { \ 31 31 ++s_baseWeakReferences; \ 32 32 } while (0); 33 33 34 #define WILL_DESTROY_WEAK_ REFERENCE(p) do { \34 #define WILL_DESTROY_WEAK_PTR_IMPL(p) do { \ 35 35 --s_baseWeakReferences; \ 36 36 } while (0);
Note:
See TracChangeset
for help on using the changeset viewer.