| 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 | 
| 7 namespace base { | 9 namespace base { | 
| 8 namespace internal { | 10 namespace internal { | 
| 9 | 11 | 
|  | 12 static constexpr uintptr_t kTrueMask = ~static_cast<uintptr_t>(0); | 
|  | 13 | 
|  | 14 WeakReference::Flag::Flag() : is_valid_(kTrueMask) { | 
| 10 #if DCHECK_IS_ON() | 15 #if DCHECK_IS_ON() | 
| 11 WeakReference::NewFlag::NewFlag(WeakReference::NewFlag::NullFlagTag) |  | 
| 12     : is_valid_(false), ref_count_(1) { |  | 
| 13   // Flags only become bound when checked for validity, or invalidated, | 16   // Flags only become bound when checked for validity, or invalidated, | 
| 14   // so that we can check that later validity/invalidation operations on | 17   // so that we can check that later validity/invalidation operations on | 
| 15   // the same Flag take place on the same sequenced thread. | 18   // the same Flag take place on the same sequenced thread. | 
| 16   sequence_checker_.DetachFromSequence(); | 19   sequence_checker_.DetachFromSequence(); | 
| 17 } |  | 
| 18 #else |  | 
| 19 constexpr WeakReference::NewFlag::NewFlag(WeakReference::NewFlag::NullFlagTag) |  | 
| 20     : is_valid_(false), ref_count_(1) {} |  | 
| 21 #endif | 20 #endif | 
| 22 |  | 
| 23 WeakReference::NewFlag WeakReference::NewFlag::g_null_flag( |  | 
| 24     WeakReference::NewFlag::kNullFlagTag); |  | 
| 25 |  | 
| 26 #if 0 |  | 
| 27 WeakReference::Flag::Flag() : is_valid_(true) { |  | 
| 28   // Flags only become bound when checked for validity, or invalidated, |  | 
| 29   // so that we can check that later validity/invalidation operations on |  | 
| 30   // the same Flag take place on the same sequenced thread. |  | 
| 31   sequence_checker_.DetachFromSequence(); |  | 
| 32 } | 21 } | 
| 33 | 22 | 
| 34 void WeakReference::Flag::Invalidate() { | 23 WeakReference::Flag::Flag(WeakReference::Flag::NullFlagTag) : is_valid_(false) { | 
| 35   // The flag being invalidated with a single ref implies that there are no | 24   // There is no need for sequence_checker_.DetachFromSequence() because the | 
| 36   // weak pointers in existence. Allow deletion on other thread in this case. | 25   // null flag doesn't participate in the sequence checks. | 
| 37   DCHECK(sequence_checker_.CalledOnValidSequence() || HasOneRef()) | 26 | 
| 38       << "WeakPtrs must be invalidated on the same sequenced thread."; | 27   AddRef();  // Stayin' alive. | 
| 39   is_valid_ = false; |  | 
| 40 } | 28 } | 
| 41 | 29 | 
| 42 bool WeakReference::Flag::IsValid() const { | 30 WeakReference::Flag* WeakReference::Flag::nullFlag() { | 
| 43   DCHECK(sequence_checker_.CalledOnValidSequence()) | 31   ANNOTATE_SCOPED_MEMORY_LEAK; | 
| 44       << "WeakPtrs must be checked on the same sequenced thread."; | 32   static Flag* g_null_flag = new Flag(kNullFlagTag); | 
| 45   return is_valid_; | 33   return g_null_flag; | 
| 46 } | 34 } | 
| 47 | 35 | 
| 48 WeakReference::Flag::~Flag() { | 36 WeakReference::Flag::~Flag() {} | 
| 49 } |  | 
| 50 #endif |  | 
| 51 | 37 | 
| 52 WeakReference::WeakReference() : new_flag_(NewFlag::NullFlag()) { | 38 WeakReference::WeakReference() : flag_(Flag::nullFlag()) {} | 
| 53 } |  | 
| 54 |  | 
| 55 WeakReference::WeakReference(/*const Flag* flag,*/ const NewFlag* newFlag) |  | 
| 56     : /*flag_(flag),*/ new_flag_(newFlag) {} |  | 
| 57 | 39 | 
| 58 WeakReference::~WeakReference() { | 40 WeakReference::~WeakReference() { | 
| 59 } | 41 } | 
| 60 | 42 | 
| 61 WeakReference::WeakReference(WeakReference&& other) : new_flag_(std::move(other.
     new_flag_)) { | 43 WeakReference::WeakReference(const Flag* flag) : flag_(flag) {} | 
| 62   other.new_flag_ = NewFlag::NullFlag(); | 44 | 
|  | 45 WeakReference::WeakReference(WeakReference&& other) | 
|  | 46     : flag_(std::move(other.flag_)) { | 
|  | 47   other.flag_ = Flag::nullFlag(); | 
| 63 } | 48 } | 
| 64 | 49 | 
| 65 WeakReference::WeakReference(const WeakReference& other) = default; | 50 WeakReference::WeakReference(const WeakReference& other) = default; | 
| 66 | 51 | 
| 67 #if 0 | 52 WeakReferenceOwner::WeakReferenceOwner() | 
| 68 bool WeakReference::is_valid() const { | 53     : flag_(WeakReference::Flag::nullFlag()) {} | 
| 69   //return flag_.get() && flag_->IsValid(); |  | 
| 70   return new_flag_->IsValid(); |  | 
| 71 } |  | 
| 72 #endif |  | 
| 73 |  | 
| 74 WeakReferenceOwner::WeakReferenceOwner() : new_flag_(WeakReference::NewFlag::Nul
     lFlag()) { |  | 
| 75 } |  | 
| 76 | 54 | 
| 77 WeakReferenceOwner::~WeakReferenceOwner() { | 55 WeakReferenceOwner::~WeakReferenceOwner() { | 
| 78   Invalidate(); | 56   Invalidate(); | 
| 79 } | 57 } | 
| 80 | 58 | 
| 81 WeakReference WeakReferenceOwner::GetRef() const { | 59 WeakReference WeakReferenceOwner::GetRef() const { | 
| 82   // If we hold the last reference to the Flag then create a new one. | 60   // If we hold the last reference to the Flag then create a new one. | 
| 83   if (!HasRefs()) { | 61   if (!HasRefs()) | 
| 84     //flag_ = new WeakReference::Flag(); | 62     flag_ = new WeakReference::Flag(); | 
| 85     new_flag_ = new WeakReference::NewFlag(); |  | 
| 86   } |  | 
| 87 | 63 | 
| 88   return WeakReference(/*flag_.get(),*/ new_flag_.get()); | 64   return WeakReference(flag_.get()); | 
| 89 } | 65 } | 
| 90 | 66 | 
| 91 void WeakReferenceOwner::Invalidate() { | 67 void WeakReferenceOwner::Invalidate() { | 
| 92 #if 0 | 68   flag_->Invalidate(); | 
| 93   if (flag_.get()) { | 69   flag_ = WeakReference::Flag::nullFlag(); | 
| 94     flag_->Invalidate(); |  | 
| 95     flag_ = NULL; |  | 
| 96   } |  | 
| 97 #endif |  | 
| 98   new_flag_->Invalidate(); |  | 
| 99   new_flag_ = WeakReference::NewFlag::NullFlag(); |  | 
| 100 } | 70 } | 
| 101 | 71 | 
| 102 WeakPtrBase::WeakPtrBase() : ptr_(0) { } | 72 WeakPtrBase::WeakPtrBase() : ptr_(0) {} | 
| 103 | 73 | 
| 104 WeakPtrBase::~WeakPtrBase() { } | 74 WeakPtrBase::~WeakPtrBase() {} | 
| 105 | 75 | 
| 106 WeakPtrBase::WeakPtrBase(const WeakReference& ref) : ref_(ref) { | 76 WeakPtrBase::WeakPtrBase(const WeakReference& ref) : ref_(ref) { | 
| 107 } | 77 } | 
| 108 | 78 | 
| 109 }  // namespace internal |  | 
| 110 |  | 
| 111 WeakPtrFactoryBase::WeakPtrFactoryBase(uintptr_t ptr) : ptr_(ptr) {} | 79 WeakPtrFactoryBase::WeakPtrFactoryBase(uintptr_t ptr) : ptr_(ptr) {} | 
| 112 | 80 | 
| 113 WeakPtrFactoryBase::~WeakPtrFactoryBase() { ptr_ = 0; } | 81 WeakPtrFactoryBase::~WeakPtrFactoryBase() { | 
|  | 82   ptr_ = 0; | 
|  | 83 } | 
| 114 | 84 | 
|  | 85 }  // namespace internal | 
| 115 }  // namespace base | 86 }  // namespace base | 
| OLD | NEW | 
|---|