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 } | |
94 | 91 |
95 protected: | 92 protected: |
96 RefCountedThreadSafeBase() : ref_count_(0) { | 93 RefCountedThreadSafeBase(); |
97 #ifndef NDEBUG | 94 ~RefCountedThreadSafeBase(); |
98 in_dtor_ = false; | |
99 #endif | |
100 } | |
101 | 95 |
102 ~RefCountedThreadSafeBase() { | 96 void AddRef() const; |
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 } | |
115 | 97 |
116 // Returns true if the object should self-delete. | 98 // Returns true if the object should self-delete. |
117 bool Release() const { | 99 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 } | |
130 | 100 |
131 private: | 101 private: |
132 mutable AtomicRefCount ref_count_; | 102 mutable AtomicRefCount ref_count_; |
133 #ifndef NDEBUG | 103 #ifndef NDEBUG |
134 mutable bool in_dtor_; | 104 mutable bool in_dtor_; |
135 #endif | 105 #endif |
136 | 106 |
137 DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafeBase); | 107 DISALLOW_COPY_AND_ASSIGN(RefCountedThreadSafeBase); |
138 }; | 108 }; |
139 | 109 |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
483 bool operator!=(std::nullptr_t null, const scoped_refptr<T>& rhs) { | 453 bool operator!=(std::nullptr_t null, const scoped_refptr<T>& rhs) { |
484 return !operator==(null, rhs); | 454 return !operator==(null, rhs); |
485 } | 455 } |
486 | 456 |
487 template <typename T> | 457 template <typename T> |
488 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { | 458 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { |
489 return out << p.get(); | 459 return out << p.get(); |
490 } | 460 } |
491 | 461 |
492 #endif // BASE_MEMORY_REF_COUNTED_H_ | 462 #endif // BASE_MEMORY_REF_COUNTED_H_ |
OLD | NEW |