OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef RUNTIME_BIN_REFERENCE_COUNTING_H_ | 5 #ifndef RUNTIME_BIN_REFERENCE_COUNTING_H_ |
6 #define RUNTIME_BIN_REFERENCE_COUNTING_H_ | 6 #define RUNTIME_BIN_REFERENCE_COUNTING_H_ |
7 | 7 |
8 #include "vm/atomic.h" | 8 #include "vm/atomic.h" |
9 | 9 |
10 namespace dart { | 10 namespace dart { |
11 namespace bin { | 11 namespace bin { |
12 | 12 |
13 // Forward declaration. | 13 // Forward declaration. |
14 template <class Target> class RefCntReleaseScope; | 14 template <class Target> |
| 15 class RefCntReleaseScope; |
15 | 16 |
16 // Inherit from this class where instances of the derived class should be | 17 // Inherit from this class where instances of the derived class should be |
17 // reference counted. Reference counts on instances are incremented and | 18 // reference counted. Reference counts on instances are incremented and |
18 // decremented explicitly with calls to Retain() and Release(). E.g.: | 19 // decremented explicitly with calls to Retain() and Release(). E.g.: |
19 // | 20 // |
20 // class Foo : public ReferenceCounted<Foo> { | 21 // class Foo : public ReferenceCounted<Foo> { |
21 // public: | 22 // public: |
22 // Foo() : ReferenceCounted() {} | 23 // Foo() : ReferenceCounted() {} |
23 // ... | 24 // ... |
24 // }; | 25 // }; |
25 // | 26 // |
26 // void DoStuffWithAFoo() { | 27 // void DoStuffWithAFoo() { |
27 // Foo* foo = new Foo(); // Reference count starts at 1, so no explicit | 28 // Foo* foo = new Foo(); // Reference count starts at 1, so no explicit |
28 // // call to Retain is needed after allocation. | 29 // // call to Retain is needed after allocation. |
29 // ... | 30 // ... |
30 // foo->Release(); | 31 // foo->Release(); |
31 // } | 32 // } |
32 template <class Derived> | 33 template <class Derived> |
33 class ReferenceCounted { | 34 class ReferenceCounted { |
34 public: | 35 public: |
35 ReferenceCounted() : | 36 ReferenceCounted() : ref_count_(1) {} |
36 ref_count_(1) { | |
37 } | |
38 | 37 |
39 ~ReferenceCounted() { | 38 ~ReferenceCounted() { ASSERT(ref_count_ == 0); } |
40 ASSERT(ref_count_ == 0); | |
41 } | |
42 | 39 |
43 void Retain() { | 40 void Retain() { |
44 uintptr_t old = AtomicOperations::FetchAndIncrement(&ref_count_); | 41 uintptr_t old = AtomicOperations::FetchAndIncrement(&ref_count_); |
45 ASSERT(old > 0); | 42 ASSERT(old > 0); |
46 } | 43 } |
47 | 44 |
48 void Release() { | 45 void Release() { |
49 uintptr_t old = AtomicOperations::FetchAndDecrement(&ref_count_); | 46 uintptr_t old = AtomicOperations::FetchAndDecrement(&ref_count_); |
50 ASSERT(old > 0); | 47 ASSERT(old > 0); |
51 if (old == 1) { | 48 if (old == 1) { |
(...skipping 19 matching lines...) Expand all Loading... |
71 // RefCntReleaseScope<Foo> rs(foo); | 68 // RefCntReleaseScope<Foo> rs(foo); |
72 // ... | 69 // ... |
73 // } | 70 // } |
74 template <class Target> | 71 template <class Target> |
75 class RefCntReleaseScope { | 72 class RefCntReleaseScope { |
76 public: | 73 public: |
77 explicit RefCntReleaseScope(ReferenceCounted<Target>* t) : target_(t) { | 74 explicit RefCntReleaseScope(ReferenceCounted<Target>* t) : target_(t) { |
78 ASSERT(target_ != NULL); | 75 ASSERT(target_ != NULL); |
79 ASSERT(target_->ref_count() > 0); | 76 ASSERT(target_->ref_count() > 0); |
80 } | 77 } |
81 ~RefCntReleaseScope() { | 78 ~RefCntReleaseScope() { target_->Release(); } |
82 target_->Release(); | |
83 } | |
84 | 79 |
85 private: | 80 private: |
86 ReferenceCounted<Target>* target_; | 81 ReferenceCounted<Target>* target_; |
87 | 82 |
88 DISALLOW_ALLOCATION(); | 83 DISALLOW_ALLOCATION(); |
89 DISALLOW_COPY_AND_ASSIGN(RefCntReleaseScope); | 84 DISALLOW_COPY_AND_ASSIGN(RefCntReleaseScope); |
90 }; | 85 }; |
91 | 86 |
92 // Instances of RetainedPointer manage Retaining and Releasing reference counted | 87 // Instances of RetainedPointer manage Retaining and Releasing reference counted |
93 // objects. There are two ways to use it. First, it can be used as a field in | 88 // objects. There are two ways to use it. First, it can be used as a field in |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 void set(ReferenceCounted<Target>* t) { | 130 void set(ReferenceCounted<Target>* t) { |
136 if (target_ != NULL) { | 131 if (target_ != NULL) { |
137 target_->Release(); | 132 target_->Release(); |
138 } | 133 } |
139 target_ = t; | 134 target_ = t; |
140 if (target_ != NULL) { | 135 if (target_ != NULL) { |
141 target_->Retain(); | 136 target_->Retain(); |
142 } | 137 } |
143 } | 138 } |
144 | 139 |
145 Target* get() const { | 140 Target* get() const { return static_cast<Target*>(target_); } |
146 return static_cast<Target*>(target_); | |
147 } | |
148 | 141 |
149 private: | 142 private: |
150 ReferenceCounted<Target>* target_; | 143 ReferenceCounted<Target>* target_; |
151 | 144 |
152 DISALLOW_ALLOCATION(); | 145 DISALLOW_ALLOCATION(); |
153 DISALLOW_COPY_AND_ASSIGN(RetainedPointer); | 146 DISALLOW_COPY_AND_ASSIGN(RetainedPointer); |
154 }; | 147 }; |
155 | 148 |
156 } // namespace bin | 149 } // namespace bin |
157 } // namespace dart | 150 } // namespace dart |
158 | 151 |
159 #endif // RUNTIME_BIN_REFERENCE_COUNTING_H_ | 152 #endif // RUNTIME_BIN_REFERENCE_COUNTING_H_ |
OLD | NEW |