Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 BASE_MEMORY_REF_COUNTED_H_ | 5 #ifndef BASE_MEMORY_REF_COUNTED_H_ |
| 6 #define BASE_MEMORY_REF_COUNTED_H_ | 6 #define BASE_MEMORY_REF_COUNTED_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <cassert> | 10 #include <cassert> |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 protected: | 135 protected: |
| 136 explicit RefCountedThreadSafeBase(StartRefCountFromZeroTag) {} | 136 explicit RefCountedThreadSafeBase(StartRefCountFromZeroTag) {} |
| 137 explicit RefCountedThreadSafeBase(StartRefCountFromOneTag) : ref_count_(1) { | 137 explicit RefCountedThreadSafeBase(StartRefCountFromOneTag) : ref_count_(1) { |
| 138 #if DCHECK_IS_ON() | 138 #if DCHECK_IS_ON() |
| 139 needs_adopt_ref_ = true; | 139 needs_adopt_ref_ = true; |
| 140 #endif | 140 #endif |
| 141 } | 141 } |
| 142 | 142 |
| 143 ~RefCountedThreadSafeBase(); | 143 ~RefCountedThreadSafeBase(); |
| 144 | 144 |
| 145 void AddRef() const { | 145 // Release and AddRef are suitable for inlining on X86 because they generate |
| 146 // very small code sequences. On other platforms (ARM), it causes a size | |
| 147 // regression and is probably not worth it. | |
| 148 #if defined(ARCH_CPU_X86_FAMILY) | |
| 149 // Returns true if the object should self-delete. | |
| 150 bool Release() const { return ReleaseImpl(); } | |
| 151 void AddRef() const { AddRefImpl(); } | |
| 152 #else | |
| 153 // Returns true if the object should self-delete. | |
| 154 bool Release() const; | |
| 155 void AddRef() const; | |
| 156 #endif | |
| 157 | |
| 158 private: | |
| 159 template <typename U> | |
| 160 friend scoped_refptr<U> base::AdoptRef(U*); | |
| 161 | |
| 162 void Adopted() const { | |
| 163 #if DCHECK_IS_ON() | |
| 164 DCHECK(needs_adopt_ref_); | |
| 165 needs_adopt_ref_ = false; | |
| 166 #endif | |
| 167 } | |
| 168 | |
| 169 ALWAYS_INLINE void AddRefImpl() const { | |
| 146 #if DCHECK_IS_ON() | 170 #if DCHECK_IS_ON() |
| 147 DCHECK(!in_dtor_); | 171 DCHECK(!in_dtor_); |
| 148 DCHECK(!needs_adopt_ref_) | 172 DCHECK(!needs_adopt_ref_) |
| 149 << "This RefCounted object is created with non-zero reference count." | 173 << "This RefCounted object is created with non-zero reference count." |
| 150 << " The first reference to such a object has to be made by AdoptRef or" | 174 << " The first reference to such a object has to be made by AdoptRef or" |
| 151 << " MakeRefCounted."; | 175 << " MakeRefCounted."; |
| 152 #endif | 176 #endif |
| 153 AtomicRefCountInc(&ref_count_); | 177 AtomicRefCountInc(&ref_count_); |
| 154 } | 178 } |
| 155 | 179 |
| 156 // Returns true if the object should self-delete. | 180 ALWAYS_INLINE bool ReleaseImpl() const { |
|
(unused - use chromium)
2017/06/30 16:19:06
Is the ALWAYS_INLINE needed? Seems like the compil
hans
2017/06/30 16:21:13
It should.
But I think the macro also expresses o
| |
| 157 bool Release() const { | |
| 158 #if DCHECK_IS_ON() | 181 #if DCHECK_IS_ON() |
| 159 DCHECK(!in_dtor_); | 182 DCHECK(!in_dtor_); |
| 160 DCHECK(!AtomicRefCountIsZero(&ref_count_)); | 183 DCHECK(!AtomicRefCountIsZero(&ref_count_)); |
| 161 #endif | 184 #endif |
| 162 if (!AtomicRefCountDec(&ref_count_)) { | 185 if (!AtomicRefCountDec(&ref_count_)) { |
| 163 #if DCHECK_IS_ON() | 186 #if DCHECK_IS_ON() |
| 164 in_dtor_ = true; | 187 in_dtor_ = true; |
| 165 #endif | 188 #endif |
| 166 return true; | 189 return true; |
| 167 } | 190 } |
| 168 return false; | 191 return false; |
| 169 } | 192 } |
| 170 | 193 |
| 171 private: | |
| 172 template <typename U> | |
| 173 friend scoped_refptr<U> base::AdoptRef(U*); | |
| 174 | |
| 175 void Adopted() const { | |
| 176 #if DCHECK_IS_ON() | |
| 177 DCHECK(needs_adopt_ref_); | |
| 178 needs_adopt_ref_ = false; | |
| 179 #endif | |
| 180 } | |
| 181 | |
| 182 mutable AtomicRefCount ref_count_{0}; | 194 mutable AtomicRefCount ref_count_{0}; |
| 183 #if DCHECK_IS_ON() | 195 #if DCHECK_IS_ON() |
| 184 mutable bool needs_adopt_ref_ = false; | 196 mutable bool needs_adopt_ref_ = false; |
| 185 mutable bool in_dtor_ = false; | 197 mutable bool in_dtor_ = false; |
| 186 #endif | 198 #endif |
| 187 | 199 |
| 188 DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafeBase); | 200 DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafeBase); |
| 189 }; | 201 }; |
| 190 | 202 |
| 191 } // namespace subtle | 203 } // namespace subtle |
| (...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 651 bool operator!=(std::nullptr_t null, const scoped_refptr<T>& rhs) { | 663 bool operator!=(std::nullptr_t null, const scoped_refptr<T>& rhs) { |
| 652 return !operator==(null, rhs); | 664 return !operator==(null, rhs); |
| 653 } | 665 } |
| 654 | 666 |
| 655 template <typename T> | 667 template <typename T> |
| 656 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { | 668 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { |
| 657 return out << p.get(); | 669 return out << p.get(); |
| 658 } | 670 } |
| 659 | 671 |
| 660 #endif // BASE_MEMORY_REF_COUNTED_H_ | 672 #endif // BASE_MEMORY_REF_COUNTED_H_ |
| OLD | NEW |