| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 Persistent_h | 5 #ifndef Persistent_h |
| 6 #define Persistent_h | 6 #define Persistent_h |
| 7 | 7 |
| 8 #include "platform/heap/Member.h" | 8 #include "platform/heap/Member.h" |
| 9 #include "platform/heap/PersistentNode.h" | 9 #include "platform/heap/PersistentNode.h" |
| 10 #include "wtf/Allocator.h" | 10 #include "wtf/Allocator.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 checkPointer(); | 70 checkPointer(); |
| 71 } | 71 } |
| 72 | 72 |
| 73 template<typename U> | 73 template<typename U> |
| 74 PersistentBase(const Member<U>& other) : m_raw(other) | 74 PersistentBase(const Member<U>& other) : m_raw(other) |
| 75 { | 75 { |
| 76 initialize(); | 76 initialize(); |
| 77 checkPointer(); | 77 checkPointer(); |
| 78 } | 78 } |
| 79 | 79 |
| 80 PersistentBase(WTF::HashTableDeletedValueType) : m_raw(reinterpret_cast<T*>(
-1)) |
| 81 { |
| 82 initialize(); |
| 83 checkPointer(); |
| 84 } |
| 85 |
| 80 ~PersistentBase() | 86 ~PersistentBase() |
| 81 { | 87 { |
| 82 uninitialize(); | 88 uninitialize(); |
| 83 m_raw = nullptr; | 89 m_raw = nullptr; |
| 84 } | 90 } |
| 85 | 91 |
| 92 bool isHashTableDeletedValue() const { return m_raw == reinterpret_cast<T*>(
-1); } |
| 93 |
| 86 template<typename VisitorDispatcher> | 94 template<typename VisitorDispatcher> |
| 87 void trace(VisitorDispatcher visitor) | 95 void trace(VisitorDispatcher visitor) |
| 88 { | 96 { |
| 89 static_assert(sizeof(T), "T must be fully defined"); | 97 static_assert(sizeof(T), "T must be fully defined"); |
| 90 static_assert(IsGarbageCollectedType<T>::value, "T needs to be a garbage
collected object"); | 98 static_assert(IsGarbageCollectedType<T>::value, "T needs to be a garbage
collected object"); |
| 91 if (weaknessConfiguration == WeakPersistentConfiguration) { | 99 if (weaknessConfiguration == WeakPersistentConfiguration) { |
| 92 visitor->registerWeakCell(&m_raw); | 100 visitor->registerWeakCell(&m_raw); |
| 93 } else { | 101 } else { |
| 94 visitor->mark(m_raw); | 102 visitor->mark(m_raw); |
| 95 } | 103 } |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 initialize(); | 185 initialize(); |
| 178 return; | 186 return; |
| 179 } | 187 } |
| 180 uninitialize(); | 188 uninitialize(); |
| 181 } | 189 } |
| 182 | 190 |
| 183 NO_LAZY_SWEEP_SANITIZE_ADDRESS | 191 NO_LAZY_SWEEP_SANITIZE_ADDRESS |
| 184 void initialize() | 192 void initialize() |
| 185 { | 193 { |
| 186 ASSERT(!m_persistentNode); | 194 ASSERT(!m_persistentNode); |
| 187 if (!m_raw) | 195 if (!m_raw || isHashTableDeletedValue()) |
| 188 return; | 196 return; |
| 189 | 197 |
| 190 TraceCallback traceCallback = TraceMethodDelegate<PersistentBase<T, weak
nessConfiguration, crossThreadnessConfiguration>, &PersistentBase<T, weaknessCon
figuration, crossThreadnessConfiguration>::trace>::trampoline; | 198 TraceCallback traceCallback = TraceMethodDelegate<PersistentBase<T, weak
nessConfiguration, crossThreadnessConfiguration>, &PersistentBase<T, weaknessCon
figuration, crossThreadnessConfiguration>::trace>::trampoline; |
| 191 if (crossThreadnessConfiguration == CrossThreadPersistentConfiguration)
{ | 199 if (crossThreadnessConfiguration == CrossThreadPersistentConfiguration)
{ |
| 192 ProcessHeap::crossThreadPersistentRegion().allocatePersistentNode(m_
persistentNode, this, traceCallback); | 200 ProcessHeap::crossThreadPersistentRegion().allocatePersistentNode(m_
persistentNode, this, traceCallback); |
| 193 return; | 201 return; |
| 194 } | 202 } |
| 195 ThreadState* state = ThreadStateFor<ThreadingTrait<T>::Affinity>::state(
); | 203 ThreadState* state = ThreadStateFor<ThreadingTrait<T>::Affinity>::state(
); |
| 196 ASSERT(state->checkThread()); | 204 ASSERT(state->checkThread()); |
| 197 m_persistentNode = state->getPersistentRegion()->allocatePersistentNode(
this, traceCallback); | 205 m_persistentNode = state->getPersistentRegion()->allocatePersistentNode(
this, traceCallback); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 214 ASSERT(state->checkThread()); | 222 ASSERT(state->checkThread()); |
| 215 // Persistent handle must be created and destructed in the same thread. | 223 // Persistent handle must be created and destructed in the same thread. |
| 216 ASSERT(m_state == state); | 224 ASSERT(m_state == state); |
| 217 state->freePersistentNode(m_persistentNode); | 225 state->freePersistentNode(m_persistentNode); |
| 218 m_persistentNode = nullptr; | 226 m_persistentNode = nullptr; |
| 219 } | 227 } |
| 220 | 228 |
| 221 void checkPointer() | 229 void checkPointer() |
| 222 { | 230 { |
| 223 #if ENABLE(ASSERT) && defined(ADDRESS_SANITIZER) | 231 #if ENABLE(ASSERT) && defined(ADDRESS_SANITIZER) |
| 224 if (!m_raw) | 232 if (!m_raw || isHashTableDeletedValue()) |
| 225 return; | 233 return; |
| 226 | 234 |
| 227 // ThreadHeap::isHeapObjectAlive(m_raw) checks that m_raw is a traceable | 235 // ThreadHeap::isHeapObjectAlive(m_raw) checks that m_raw is a traceable |
| 228 // object. In other words, it checks that the pointer is either of: | 236 // object. In other words, it checks that the pointer is either of: |
| 229 // | 237 // |
| 230 // (a) a pointer to the head of an on-heap object. | 238 // (a) a pointer to the head of an on-heap object. |
| 231 // (b) a pointer to the head of an on-heap mixin object. | 239 // (b) a pointer to the head of an on-heap mixin object. |
| 232 // | 240 // |
| 233 // Otherwise, ThreadHeap::isHeapObjectAlive will crash when it calls | 241 // Otherwise, ThreadHeap::isHeapObjectAlive will crash when it calls |
| 234 // header->checkHeader(). | 242 // header->checkHeader(). |
| (...skipping 21 matching lines...) Expand all Loading... |
| 256 public: | 264 public: |
| 257 Persistent() : Parent() { } | 265 Persistent() : Parent() { } |
| 258 Persistent(std::nullptr_t) : Parent(nullptr) { } | 266 Persistent(std::nullptr_t) : Parent(nullptr) { } |
| 259 Persistent(T* raw) : Parent(raw) { } | 267 Persistent(T* raw) : Parent(raw) { } |
| 260 Persistent(T& raw) : Parent(raw) { } | 268 Persistent(T& raw) : Parent(raw) { } |
| 261 Persistent(const Persistent& other) : Parent(other) { } | 269 Persistent(const Persistent& other) : Parent(other) { } |
| 262 template<typename U> | 270 template<typename U> |
| 263 Persistent(const Persistent<U>& other) : Parent(other) { } | 271 Persistent(const Persistent<U>& other) : Parent(other) { } |
| 264 template<typename U> | 272 template<typename U> |
| 265 Persistent(const Member<U>& other) : Parent(other) { } | 273 Persistent(const Member<U>& other) : Parent(other) { } |
| 274 Persistent(WTF::HashTableDeletedValueType x) : Parent(x) { } |
| 266 | 275 |
| 267 template<typename U> | 276 template<typename U> |
| 268 Persistent& operator=(U* other) | 277 Persistent& operator=(U* other) |
| 269 { | 278 { |
| 270 Parent::operator=(other); | 279 Parent::operator=(other); |
| 271 return *this; | 280 return *this; |
| 272 } | 281 } |
| 273 | 282 |
| 274 Persistent& operator=(std::nullptr_t) | 283 Persistent& operator=(std::nullptr_t) |
| 275 { | 284 { |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 public: | 374 public: |
| 366 CrossThreadPersistent() : Parent() { } | 375 CrossThreadPersistent() : Parent() { } |
| 367 CrossThreadPersistent(std::nullptr_t) : Parent(nullptr) { } | 376 CrossThreadPersistent(std::nullptr_t) : Parent(nullptr) { } |
| 368 CrossThreadPersistent(T* raw) : Parent(raw) { } | 377 CrossThreadPersistent(T* raw) : Parent(raw) { } |
| 369 CrossThreadPersistent(T& raw) : Parent(raw) { } | 378 CrossThreadPersistent(T& raw) : Parent(raw) { } |
| 370 CrossThreadPersistent(const CrossThreadPersistent& other) : Parent(other) {
} | 379 CrossThreadPersistent(const CrossThreadPersistent& other) : Parent(other) {
} |
| 371 template<typename U> | 380 template<typename U> |
| 372 CrossThreadPersistent(const CrossThreadPersistent<U>& other) : Parent(other)
{ } | 381 CrossThreadPersistent(const CrossThreadPersistent<U>& other) : Parent(other)
{ } |
| 373 template<typename U> | 382 template<typename U> |
| 374 CrossThreadPersistent(const Member<U>& other) : Parent(other) { } | 383 CrossThreadPersistent(const Member<U>& other) : Parent(other) { } |
| 384 CrossThreadPersistent(WTF::HashTableDeletedValueType x) : Parent(x) { } |
| 375 | 385 |
| 376 T* atomicGet() { return Parent::atomicGet(); } | 386 T* atomicGet() { return Parent::atomicGet(); } |
| 377 | 387 |
| 378 template<typename U> | 388 template<typename U> |
| 379 CrossThreadPersistent& operator=(U* other) | 389 CrossThreadPersistent& operator=(U* other) |
| 380 { | 390 { |
| 381 Parent::operator=(other); | 391 Parent::operator=(other); |
| 382 return *this; | 392 return *this; |
| 383 } | 393 } |
| 384 | 394 |
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 675 | 685 |
| 676 template<typename T, typename U> inline bool operator==(const Member<T>& a, cons
t Persistent<U>& b) { return a.get() == b.get(); } | 686 template<typename T, typename U> inline bool operator==(const Member<T>& a, cons
t Persistent<U>& b) { return a.get() == b.get(); } |
| 677 template<typename T, typename U> inline bool operator!=(const Member<T>& a, cons
t Persistent<U>& b) { return a.get() != b.get(); } | 687 template<typename T, typename U> inline bool operator!=(const Member<T>& a, cons
t Persistent<U>& b) { return a.get() != b.get(); } |
| 678 template<typename T, typename U> inline bool operator==(const Persistent<T>& a,
const Member<U>& b) { return a.get() == b.get(); } | 688 template<typename T, typename U> inline bool operator==(const Persistent<T>& a,
const Member<U>& b) { return a.get() == b.get(); } |
| 679 template<typename T, typename U> inline bool operator!=(const Persistent<T>& a,
const Member<U>& b) { return a.get() != b.get(); } | 689 template<typename T, typename U> inline bool operator!=(const Persistent<T>& a,
const Member<U>& b) { return a.get() != b.get(); } |
| 680 | 690 |
| 681 } // namespace blink | 691 } // namespace blink |
| 682 | 692 |
| 683 namespace WTF { | 693 namespace WTF { |
| 684 | 694 |
| 695 template <typename T> |
| 696 struct PersistentHash : MemberHash<T> { |
| 697 STATIC_ONLY(PersistentHash); |
| 698 }; |
| 699 |
| 700 template <typename T> |
| 701 struct CrossThreadPersistentHash : MemberHash<T> { |
| 702 STATIC_ONLY(CrossThreadPersistentHash); |
| 703 }; |
| 704 |
| 705 template <typename T> |
| 706 struct DefaultHash<blink::Persistent<T>> { |
| 707 STATIC_ONLY(DefaultHash); |
| 708 using Hash = PersistentHash<T>; |
| 709 }; |
| 710 |
| 711 template <typename T> |
| 712 struct DefaultHash<blink::CrossThreadPersistent<T>> { |
| 713 STATIC_ONLY(DefaultHash); |
| 714 using Hash = CrossThreadPersistentHash<T>; |
| 715 }; |
| 716 |
| 685 template<typename T> | 717 template<typename T> |
| 686 struct ParamStorageTraits<blink::WeakPersistentThisPointer<T>> { | 718 struct ParamStorageTraits<blink::WeakPersistentThisPointer<T>> { |
| 687 STATIC_ONLY(ParamStorageTraits); | 719 STATIC_ONLY(ParamStorageTraits); |
| 688 static_assert(sizeof(T), "T must be fully defined"); | 720 static_assert(sizeof(T), "T must be fully defined"); |
| 689 using StorageType = blink::WeakPersistent<T>; | 721 using StorageType = blink::WeakPersistent<T>; |
| 690 | 722 |
| 691 static StorageType wrap(const blink::WeakPersistentThisPointer<T>& value) {
return value.value(); } | 723 static StorageType wrap(const blink::WeakPersistentThisPointer<T>& value) {
return value.value(); } |
| 692 | 724 |
| 693 // WTF::FunctionWrapper<> handles WeakPtr<>, so recast this weak persistent | 725 // WTF::FunctionWrapper<> handles WeakPtr<>, so recast this weak persistent |
| 694 // into it. | 726 // into it. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 710 // into it. | 742 // into it. |
| 711 // | 743 // |
| 712 // TODO(sof): remove this hack once wtf/Functional.h can also work with a ty
pe like | 744 // TODO(sof): remove this hack once wtf/Functional.h can also work with a ty
pe like |
| 713 // CrossThreadWeakPersistent<>. | 745 // CrossThreadWeakPersistent<>. |
| 714 static WeakPtr<T> unwrap(const StorageType& value) { return WeakPtr<T>(WeakR
eference<T>::create(value.get())); } | 746 static WeakPtr<T> unwrap(const StorageType& value) { return WeakPtr<T>(WeakR
eference<T>::create(value.get())); } |
| 715 }; | 747 }; |
| 716 | 748 |
| 717 } // namespace WTF | 749 } // namespace WTF |
| 718 | 750 |
| 719 #endif // Persistent_h | 751 #endif // Persistent_h |
| OLD | NEW |