| 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 <cassert> | 8 #include <cassert> |
| 9 #include <iosfwd> | 9 #include <iosfwd> |
| 10 | 10 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 { | 35 { |
| 36 } | 36 } |
| 37 | 37 |
| 38 ~RefCountedBase() { | 38 ~RefCountedBase() { |
| 39 #ifndef NDEBUG | 39 #ifndef NDEBUG |
| 40 DCHECK(in_dtor_) << "RefCounted object deleted without calling Release()"; | 40 DCHECK(in_dtor_) << "RefCounted object deleted without calling Release()"; |
| 41 #endif | 41 #endif |
| 42 } | 42 } |
| 43 | 43 |
| 44 | 44 |
| 45 void AddRef() const { | 45 void AddRef() const; |
| 46 // TODO(maruel): Add back once it doesn't assert 500 times/sec. | |
| 47 // Current thread books the critical section "AddRelease" | |
| 48 // without release it. | |
| 49 // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_); | |
| 50 #ifndef NDEBUG | |
| 51 DCHECK(!in_dtor_); | |
| 52 #endif | |
| 53 ++ref_count_; | |
| 54 } | |
| 55 | 46 |
| 56 // Returns true if the object should self-delete. | 47 // Returns true if the object should self-delete. |
| 57 bool Release() const { | 48 bool Release() const; |
| 58 // TODO(maruel): Add back once it doesn't assert 500 times/sec. | |
| 59 // Current thread books the critical section "AddRelease" | |
| 60 // without release it. | |
| 61 // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_); | |
| 62 #ifndef NDEBUG | |
| 63 DCHECK(!in_dtor_); | |
| 64 #endif | |
| 65 if (--ref_count_ == 0) { | |
| 66 #ifndef NDEBUG | |
| 67 in_dtor_ = true; | |
| 68 #endif | |
| 69 return true; | |
| 70 } | |
| 71 return false; | |
| 72 } | |
| 73 | 49 |
| 74 private: | 50 private: |
| 75 mutable int ref_count_; | 51 mutable int ref_count_; |
| 76 #ifndef NDEBUG | 52 #ifndef NDEBUG |
| 77 mutable bool in_dtor_; | 53 mutable bool in_dtor_; |
| 78 #endif | 54 #endif |
| 79 | 55 |
| 80 DFAKE_MUTEX(add_release_); | 56 DFAKE_MUTEX(add_release_); |
| 81 | 57 |
| 82 DISALLOW_COPY_AND_ASSIGN(RefCountedBase); | 58 DISALLOW_COPY_AND_ASSIGN(RefCountedBase); |
| (...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) { | 421 bool operator!=(const T* lhs, const scoped_refptr<U>& rhs) { |
| 446 return !operator==(lhs, rhs); | 422 return !operator==(lhs, rhs); |
| 447 } | 423 } |
| 448 | 424 |
| 449 template <typename T> | 425 template <typename T> |
| 450 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { | 426 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { |
| 451 return out << p.get(); | 427 return out << p.get(); |
| 452 } | 428 } |
| 453 | 429 |
| 454 #endif // BASE_MEMORY_REF_COUNTED_H_ | 430 #endif // BASE_MEMORY_REF_COUNTED_H_ |
| OLD | NEW |