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 10 matching lines...) Expand all Loading... |
21 | 21 |
22 namespace base { | 22 namespace base { |
23 | 23 |
24 namespace subtle { | 24 namespace subtle { |
25 | 25 |
26 class BASE_EXPORT RefCountedBase { | 26 class BASE_EXPORT RefCountedBase { |
27 public: | 27 public: |
28 bool HasOneRef() const { return ref_count_ == 1; } | 28 bool HasOneRef() const { return ref_count_ == 1; } |
29 | 29 |
30 protected: | 30 protected: |
31 RefCountedBase() | 31 RefCountedBase() {} |
32 : ref_count_(0) | |
33 #if DCHECK_IS_ON() | |
34 , in_dtor_(false) | |
35 #endif | |
36 { | |
37 } | |
38 | 32 |
39 ~RefCountedBase() { | 33 ~RefCountedBase() { |
40 #if DCHECK_IS_ON() | 34 #if DCHECK_IS_ON() |
41 DCHECK(in_dtor_) << "RefCounted object deleted without calling Release()"; | 35 DCHECK(in_dtor_) << "RefCounted object deleted without calling Release()"; |
42 #endif | 36 #endif |
43 } | 37 } |
44 | 38 |
45 | |
46 void AddRef() const { | 39 void AddRef() const { |
47 // TODO(maruel): Add back once it doesn't assert 500 times/sec. | 40 // TODO(maruel): Add back once it doesn't assert 500 times/sec. |
48 // Current thread books the critical section "AddRelease" | 41 // Current thread books the critical section "AddRelease" |
49 // without release it. | 42 // without release it. |
50 // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_); | 43 // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_); |
51 #if DCHECK_IS_ON() | 44 #if DCHECK_IS_ON() |
52 DCHECK(!in_dtor_); | 45 DCHECK(!in_dtor_); |
53 #endif | 46 #endif |
| 47 |
54 ++ref_count_; | 48 ++ref_count_; |
55 } | 49 } |
56 | 50 |
57 // Returns true if the object should self-delete. | 51 // Returns true if the object should self-delete. |
58 bool Release() const { | 52 bool Release() const { |
| 53 --ref_count_; |
| 54 |
59 // TODO(maruel): Add back once it doesn't assert 500 times/sec. | 55 // TODO(maruel): Add back once it doesn't assert 500 times/sec. |
60 // Current thread books the critical section "AddRelease" | 56 // Current thread books the critical section "AddRelease" |
61 // without release it. | 57 // without release it. |
62 // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_); | 58 // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_); |
| 59 |
63 #if DCHECK_IS_ON() | 60 #if DCHECK_IS_ON() |
64 DCHECK(!in_dtor_); | 61 DCHECK(!in_dtor_); |
65 #endif | 62 if (ref_count_ == 0) |
66 if (--ref_count_ == 0) { | |
67 #if DCHECK_IS_ON() | |
68 in_dtor_ = true; | 63 in_dtor_ = true; |
69 #endif | 64 #endif |
70 return true; | 65 |
71 } | 66 return ref_count_ == 0; |
72 return false; | |
73 } | 67 } |
74 | 68 |
75 private: | 69 private: |
76 mutable size_t ref_count_; | 70 mutable size_t ref_count_ = 0; |
77 #if DCHECK_IS_ON() | 71 #if DCHECK_IS_ON() |
78 mutable bool in_dtor_; | 72 mutable bool in_dtor_ = false; |
79 #endif | 73 #endif |
80 | 74 |
81 DFAKE_MUTEX(add_release_); | 75 DFAKE_MUTEX(add_release_); |
82 | 76 |
83 DISALLOW_COPY_AND_ASSIGN(RefCountedBase); | 77 DISALLOW_COPY_AND_ASSIGN(RefCountedBase); |
84 }; | 78 }; |
85 | 79 |
86 class BASE_EXPORT RefCountedThreadSafeBase { | 80 class BASE_EXPORT RefCountedThreadSafeBase { |
87 public: | 81 public: |
88 bool HasOneRef() const; | 82 bool HasOneRef() const; |
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 bool operator!=(std::nullptr_t null, const scoped_refptr<T>& rhs) { | 445 bool operator!=(std::nullptr_t null, const scoped_refptr<T>& rhs) { |
452 return !operator==(null, rhs); | 446 return !operator==(null, rhs); |
453 } | 447 } |
454 | 448 |
455 template <typename T> | 449 template <typename T> |
456 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { | 450 std::ostream& operator<<(std::ostream& out, const scoped_refptr<T>& p) { |
457 return out << p.get(); | 451 return out << p.get(); |
458 } | 452 } |
459 | 453 |
460 #endif // BASE_MEMORY_REF_COUNTED_H_ | 454 #endif // BASE_MEMORY_REF_COUNTED_H_ |
OLD | NEW |