| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "base/memory/weak_ptr.h" | 5 #include "base/memory/weak_ptr.h" |
| 6 | 6 |
| 7 #include "base/debug/leak_annotations.h" | |
| 8 | |
| 9 namespace base { | 7 namespace base { |
| 10 namespace internal { | 8 namespace internal { |
| 11 | 9 |
| 12 static constexpr uintptr_t kTrueMask = ~static_cast<uintptr_t>(0); | 10 WeakReference::Flag::Flag() : is_valid_(true) { |
| 13 | |
| 14 WeakReference::Flag::Flag() : is_valid_(kTrueMask) { | |
| 15 #if DCHECK_IS_ON() | |
| 16 // Flags only become bound when checked for validity, or invalidated, | 11 // Flags only become bound when checked for validity, or invalidated, |
| 17 // so that we can check that later validity/invalidation operations on | 12 // so that we can check that later validity/invalidation operations on |
| 18 // the same Flag take place on the same sequenced thread. | 13 // the same Flag take place on the same sequenced thread. |
| 19 sequence_checker_.DetachFromSequence(); | 14 sequence_checker_.DetachFromSequence(); |
| 20 #endif | |
| 21 } | 15 } |
| 22 | 16 |
| 23 WeakReference::Flag::Flag(WeakReference::Flag::NullFlagTag) : is_valid_(false) { | 17 void WeakReference::Flag::Invalidate() { |
| 24 // There is no need for sequence_checker_.DetachFromSequence() because the | 18 // The flag being invalidated with a single ref implies that there are no |
| 25 // null flag doesn't participate in the sequence checks. See DCHECK in | 19 // weak pointers in existence. Allow deletion on other thread in this case. |
| 26 // Invalidate() and IsValid(). | 20 DCHECK(sequence_checker_.CalledOnValidSequence() || HasOneRef()) |
| 27 | 21 << "WeakPtrs must be invalidated on the same sequenced thread."; |
| 28 // Keep the object alive perpetually, even when there are no references to it. | 22 is_valid_ = false; |
| 29 AddRef(); | |
| 30 } | 23 } |
| 31 | 24 |
| 32 WeakReference::Flag* WeakReference::Flag::NullFlag() { | 25 bool WeakReference::Flag::IsValid() const { |
| 33 ANNOTATE_SCOPED_MEMORY_LEAK; | 26 DCHECK(sequence_checker_.CalledOnValidSequence()) |
| 34 static Flag* g_null_flag = new Flag(kNullFlagTag); | 27 << "WeakPtrs must be checked on the same sequenced thread."; |
| 35 return g_null_flag; | 28 return is_valid_; |
| 36 } | 29 } |
| 37 | 30 |
| 38 WeakReference::Flag::~Flag() {} | 31 WeakReference::Flag::~Flag() { |
| 32 } |
| 39 | 33 |
| 40 WeakReference::WeakReference() : flag_(Flag::NullFlag()) {} | 34 WeakReference::WeakReference() { |
| 35 } |
| 36 |
| 37 WeakReference::WeakReference(const Flag* flag) : flag_(flag) { |
| 38 } |
| 41 | 39 |
| 42 WeakReference::~WeakReference() { | 40 WeakReference::~WeakReference() { |
| 43 } | 41 } |
| 44 | 42 |
| 45 WeakReference::WeakReference(const Flag* flag) : flag_(flag) {} | 43 WeakReference::WeakReference(WeakReference&& other) = default; |
| 46 | |
| 47 WeakReference::WeakReference(WeakReference&& other) | |
| 48 : flag_(std::move(other.flag_)) { | |
| 49 other.flag_ = Flag::NullFlag(); | |
| 50 } | |
| 51 | 44 |
| 52 WeakReference::WeakReference(const WeakReference& other) = default; | 45 WeakReference::WeakReference(const WeakReference& other) = default; |
| 53 | 46 |
| 54 WeakReferenceOwner::WeakReferenceOwner() | 47 bool WeakReference::is_valid() const { return flag_.get() && flag_->IsValid(); } |
| 55 : flag_(WeakReference::Flag::NullFlag()) {} | 48 |
| 49 WeakReferenceOwner::WeakReferenceOwner() { |
| 50 } |
| 56 | 51 |
| 57 WeakReferenceOwner::~WeakReferenceOwner() { | 52 WeakReferenceOwner::~WeakReferenceOwner() { |
| 58 Invalidate(); | 53 Invalidate(); |
| 59 } | 54 } |
| 60 | 55 |
| 61 WeakReference WeakReferenceOwner::GetRef() const { | 56 WeakReference WeakReferenceOwner::GetRef() const { |
| 62 // If we hold the last reference to the Flag then create a new one. | 57 // If we hold the last reference to the Flag then create a new one. |
| 63 if (!HasRefs()) | 58 if (!HasRefs()) |
| 64 flag_ = new WeakReference::Flag(); | 59 flag_ = new WeakReference::Flag(); |
| 65 | 60 |
| 66 return WeakReference(flag_.get()); | 61 return WeakReference(flag_.get()); |
| 67 } | 62 } |
| 68 | 63 |
| 69 void WeakReferenceOwner::Invalidate() { | 64 void WeakReferenceOwner::Invalidate() { |
| 70 flag_->Invalidate(); | 65 if (flag_.get()) { |
| 71 flag_ = WeakReference::Flag::NullFlag(); | 66 flag_->Invalidate(); |
| 67 flag_ = NULL; |
| 68 } |
| 72 } | 69 } |
| 73 | 70 |
| 74 WeakPtrBase::WeakPtrBase() : ptr_(0) {} | 71 WeakPtrBase::WeakPtrBase() : ptr_(0) {} |
| 75 | 72 |
| 76 WeakPtrBase::~WeakPtrBase() {} | 73 WeakPtrBase::~WeakPtrBase() {} |
| 77 | 74 |
| 78 WeakPtrBase::WeakPtrBase(const WeakReference& ref, uintptr_t ptr) | 75 WeakPtrBase::WeakPtrBase(const WeakReference& ref, uintptr_t ptr) |
| 79 : ref_(ref), ptr_(ptr) {} | 76 : ref_(ref), ptr_(ptr) {} |
| 80 | 77 |
| 81 WeakPtrFactoryBase::WeakPtrFactoryBase(uintptr_t ptr) : ptr_(ptr) {} | 78 WeakPtrFactoryBase::WeakPtrFactoryBase(uintptr_t ptr) : ptr_(ptr) {} |
| 82 | 79 |
| 83 WeakPtrFactoryBase::~WeakPtrFactoryBase() { | 80 WeakPtrFactoryBase::~WeakPtrFactoryBase() { |
| 84 ptr_ = 0; | 81 ptr_ = 0; |
| 85 } | 82 } |
| 86 | 83 |
| 87 } // namespace internal | 84 } // namespace internal |
| 88 } // namespace base | 85 } // namespace base |
| OLD | NEW |