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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 mutable bool in_dtor_; | 80 mutable bool in_dtor_; |
81 #endif | 81 #endif |
82 | 82 |
83 DFAKE_MUTEX(add_release_); | 83 DFAKE_MUTEX(add_release_); |
84 | 84 |
85 DISALLOW_COPY_AND_ASSIGN(RefCountedBase); | 85 DISALLOW_COPY_AND_ASSIGN(RefCountedBase); |
86 }; | 86 }; |
87 | 87 |
88 class BASE_EXPORT RefCountedThreadSafeBase { | 88 class BASE_EXPORT RefCountedThreadSafeBase { |
89 public: | 89 public: |
90 bool HasOneRef() const; | 90 bool HasOneRef() const { |
| 91 return AtomicRefCountIsOne( |
| 92 &const_cast<RefCountedThreadSafeBase*>(this)->ref_count_); |
| 93 } |
91 | 94 |
92 protected: | 95 protected: |
93 RefCountedThreadSafeBase(); | 96 RefCountedThreadSafeBase() : ref_count_(0) { |
94 ~RefCountedThreadSafeBase(); | 97 #ifndef NDEBUG |
| 98 in_dtor_ = false; |
| 99 #endif |
| 100 } |
95 | 101 |
96 void AddRef() const; | 102 ~RefCountedThreadSafeBase() { |
| 103 #ifndef NDEBUG |
| 104 DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without " |
| 105 "calling Release()"; |
| 106 #endif |
| 107 } |
| 108 |
| 109 void AddRef() const { |
| 110 #ifndef NDEBUG |
| 111 DCHECK(!in_dtor_); |
| 112 #endif |
| 113 AtomicRefCountInc(&ref_count_); |
| 114 } |
97 | 115 |
98 // Returns true if the object should self-delete. | 116 // Returns true if the object should self-delete. |
99 bool Release() const; | 117 bool Release() const { |
| 118 #ifndef NDEBUG |
| 119 DCHECK(!in_dtor_); |
| 120 DCHECK(!AtomicRefCountIsZero(&ref_count_)); |
| 121 #endif |
| 122 if (!AtomicRefCountDec(&ref_count_)) { |
| 123 #ifndef NDEBUG |
| 124 in_dtor_ = true; |
| 125 #endif |
| 126 return true; |
| 127 } |
| 128 return false; |
| 129 } |
100 | 130 |
101 private: | 131 private: |
102 mutable AtomicRefCount ref_count_; | 132 mutable AtomicRefCount ref_count_; |
103 #ifndef NDEBUG | 133 #ifndef NDEBUG |
104 mutable bool in_dtor_; | 134 mutable bool in_dtor_; |
105 #endif | 135 #endif |
106 | 136 |
107 DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafeBase); | 137 DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafeBase); |
108 }; | 138 }; |
109 | 139 |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 bool operator!=(std::nullptr_t null, const scoped_refptr<T>& rhs) { | 483 bool operator!=(std::nullptr_t null, const scoped_refptr<T>& rhs) { |
454 return !operator==(null, rhs); | 484 return !operator==(null, rhs); |
455 } | 485 } |
456 | 486 |
457 template <typename T> | 487 template <typename T> |
458 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { | 488 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { |
459 return out << p.get(); | 489 return out << p.get(); |
460 } | 490 } |
461 | 491 |
462 #endif // BASE_MEMORY_REF_COUNTED_H_ | 492 #endif // BASE_MEMORY_REF_COUNTED_H_ |
OLD | NEW |