| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #ifndef BIN_REFERENCE_COUNTING_H_ |
| 6 #define BIN_REFERENCE_COUNTING_H_ |
| 7 |
| 8 #include "vm/atomic.h" |
| 9 |
| 10 namespace dart { |
| 11 namespace bin { |
| 12 |
| 13 // Forward declaration. |
| 14 template <class Target> class RefCntReleaseScope; |
| 15 |
| 16 // Inherit from this class where instances of the derived class should be |
| 17 // reference counted. Reference counts on instances are incremented and |
| 18 // decremented explicitly with calls to Retain() and Release(). E.g.: |
| 19 // |
| 20 // class Foo : public ReferenceCounted<Foo> { |
| 21 // public: |
| 22 // Foo() : ReferenceCounted() {} |
| 23 // ... |
| 24 // }; |
| 25 // |
| 26 // void DoStuffWithAFoo() { |
| 27 // Foo* foo = new Foo(); // Reference count starts at 1, so no explicit |
| 28 // // call to Retain is needed after allocation. |
| 29 // ... |
| 30 // foo->Release(); |
| 31 // } |
| 32 template <class Derived> |
| 33 class ReferenceCounted { |
| 34 public: |
| 35 ReferenceCounted() : |
| 36 ref_count_(1) { |
| 37 } |
| 38 |
| 39 ~ReferenceCounted() { |
| 40 ASSERT(ref_count_ == 0); |
| 41 } |
| 42 |
| 43 void Retain() { |
| 44 uintptr_t old = AtomicOperations::FetchAndIncrement(&ref_count_); |
| 45 ASSERT(old > 0); |
| 46 } |
| 47 |
| 48 void Release() { |
| 49 uintptr_t old = AtomicOperations::FetchAndDecrement(&ref_count_); |
| 50 ASSERT(old > 0); |
| 51 if (old == 1) { |
| 52 delete static_cast<Derived*>(this); |
| 53 } |
| 54 } |
| 55 |
| 56 private: |
| 57 uintptr_t ref_count_; |
| 58 |
| 59 // These are used only in the ASSERT below in RefCntReleaseScope. |
| 60 uintptr_t ref_count() const { return ref_count_; } |
| 61 friend class RefCntReleaseScope<Derived>; |
| 62 DISALLOW_COPY_AND_ASSIGN(ReferenceCounted); |
| 63 }; |
| 64 |
| 65 // Creates a scope at the end of which a reference counted object is |
| 66 // Released. This is useful for reference counted objects recieved by the IO |
| 67 // Service, which have already been Retained E.g.: |
| 68 // |
| 69 // CObject* Foo::FooRequest(const CObjectArray& request) { |
| 70 // Foo* foo = CObjectToFoo(request[0]); |
| 71 // RefCntReleaseScope<Foo> rs(foo); |
| 72 // ... |
| 73 // } |
| 74 template <class Target> |
| 75 class RefCntReleaseScope { |
| 76 public: |
| 77 explicit RefCntReleaseScope(ReferenceCounted<Target>* t) : target_(t) { |
| 78 ASSERT(target_ != NULL); |
| 79 ASSERT(target_->ref_count() > 0); |
| 80 } |
| 81 ~RefCntReleaseScope() { |
| 82 target_->Release(); |
| 83 } |
| 84 |
| 85 private: |
| 86 ReferenceCounted<Target>* target_; |
| 87 |
| 88 DISALLOW_ALLOCATION(); |
| 89 DISALLOW_COPY_AND_ASSIGN(RefCntReleaseScope); |
| 90 }; |
| 91 |
| 92 // 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 |
| 94 // a class, e.g.: |
| 95 // |
| 96 // class Foo { |
| 97 // private: |
| 98 // RetainedPointer<Bar> bar_; |
| 99 // public: |
| 100 // explicit Foo(Bar* b) : bar_(b) {} |
| 101 // } |
| 102 // |
| 103 // In this case, b will be Retained in Foo's constructor, and Released |
| 104 // automatically during Foo's destructor. |
| 105 // |
| 106 // RetainedPointer can also be used as a scope, as with RefCntReleaseScope, |
| 107 // with the difference that entering the scope also Retains the pointer, e.g.: |
| 108 // |
| 109 // void RetainAndDoStuffWithFoo(Foo* foo) { |
| 110 // RetainedPointer<Foo> retained(foo); |
| 111 // .. |
| 112 // } |
| 113 // |
| 114 // This Retains foo on entry and Releases foo at every exit from the scope. |
| 115 // |
| 116 // The underlying pointer can be accessed with the get() and set() methods. |
| 117 // Overwriting a non-NULL pointer with set causes that pointer to be Released. |
| 118 template <class Target> |
| 119 class RetainedPointer { |
| 120 public: |
| 121 RetainedPointer() : target_(NULL) {} |
| 122 |
| 123 explicit RetainedPointer(ReferenceCounted<Target>* t) : target_(t) { |
| 124 if (target_ != NULL) { |
| 125 target_->Retain(); |
| 126 } |
| 127 } |
| 128 |
| 129 ~RetainedPointer() { |
| 130 if (target_ != NULL) { |
| 131 target_->Release(); |
| 132 } |
| 133 } |
| 134 |
| 135 void set(ReferenceCounted<Target>* t) { |
| 136 if (target_ != NULL) { |
| 137 target_->Release(); |
| 138 } |
| 139 target_ = t; |
| 140 if (target_ != NULL) { |
| 141 target_->Retain(); |
| 142 } |
| 143 } |
| 144 |
| 145 Target* get() const { |
| 146 return static_cast<Target*>(target_); |
| 147 } |
| 148 |
| 149 private: |
| 150 ReferenceCounted<Target>* target_; |
| 151 |
| 152 DISALLOW_ALLOCATION(); |
| 153 DISALLOW_COPY_AND_ASSIGN(RetainedPointer); |
| 154 }; |
| 155 |
| 156 } // namespace bin |
| 157 } // namespace dart |
| 158 |
| 159 #endif // BIN_REFERENCE_COUNTING_H_ |
| OLD | NEW |