Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(415)

Side by Side Diff: runtime/bin/reference_counting.h

Issue 1852783003: Implements remaining SecurityContext calls for iOS (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Add RefCntReleaseScope Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/bin/io_impl_sources.gypi ('k') | runtime/bin/secure_socket.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 // Inherit from this class where instances of the derived class should be
14 // reference counted. Reference counts on instances are incremented and
15 // decremented explicitly with calls to Retain() and Release(). E.g.:
16 //
17 // class Foo : public ReferenceCounted<Foo> {
18 // public:
19 // Foo() : ReferenceCounted() {}
20 // ...
21 // };
22 //
23 // void DoStuffWithAFoo() {
24 // Foo* foo = new Foo();
25 // foo->Retain();
26 // ...
27 // foo->Release();
28 // }
29 template <class Derived>
30 class ReferenceCounted {
31 public:
32 ReferenceCounted() :
33 ref_count_(0) {
34 }
35
36 ~ReferenceCounted() {
37 ASSERT(ref_count_ == 0);
38 }
39
40 void Retain() {
41 uintptr_t old = AtomicOperations::FetchAndIncrement(&ref_count_);
42 ASSERT(old >= 0);
43 }
44
45 void Release() {
46 uintptr_t old = AtomicOperations::FetchAndDecrement(&ref_count_);
47 ASSERT(old > 0);
48 if (old == 1) {
49 delete reinterpret_cast<Derived*>(this);
50 }
51 }
52
53 private:
54 uintptr_t ref_count_;
55
56 DISALLOW_COPY_AND_ASSIGN(ReferenceCounted);
57 };
58
59 // Creates a scope at the end of which, a reference counted object is
60 // Released. This is useful for reference counted objects recieved by the IO
61 // Service, which have already been Retained E.g.:
62 //
63 // CObject* Foo::FooRequest(const CObjectArray& request) {
64 // Foo* foo = CObjectToFoo(request[0]);
65 // RefCntReleaseScope<Foo> rs(foo);
66 // ...
67 // }
68 template <class Target>
69 class RefCntReleaseScope {
70 public:
71 explicit RefCntReleaseScope(ReferenceCounted<Target>* t) : target_(t) {
72 ASSERT(target_ != NULL);
Ivan Posva 2016/04/08 13:18:21 ASSERT that ref_count_ is greater than 1?
zra 2016/04/08 16:07:48 Done.
73 }
74 ~RefCntReleaseScope() {
75 target_->Release();
76 }
77
78 private:
79 ReferenceCounted<Target>* target_;
80
81 DISALLOW_ALLOCATION();
82 DISALLOW_COPY_AND_ASSIGN(RefCntReleaseScope);
83 };
84
85 template <class Target>
Ivan Posva 2016/04/08 13:18:21 There is good documentation for the above classes,
zra 2016/04/08 16:07:48 Done.
86 class RetainedPointer {
87 public:
88 RetainedPointer() : target_(NULL) {}
89
90 explicit RetainedPointer(ReferenceCounted<Target>* t) : target_(t) {
91 if (target_ != NULL) {
92 target_->Retain();
93 }
94 }
95
96 ~RetainedPointer() {
97 if (target_ != NULL) {
98 target_->Release();
99 }
100 }
101
102 void set(ReferenceCounted<Target>* t) {
103 if (target_ != NULL) {
104 target_->Release();
105 }
106 target_ = t;
107 if (target_ != NULL) {
108 target_->Retain();
109 }
110 }
111
112 Target* get() const {
113 return reinterpret_cast<Target*>(target_);
114 }
115
116 private:
117 ReferenceCounted<Target>* target_;
118
119 DISALLOW_ALLOCATION();
120 DISALLOW_COPY_AND_ASSIGN(RetainedPointer);
121 };
122
123 } // namespace bin
124 } // namespace dart
125
126 #endif // BIN_REFERENCE_COUNTING_H_
OLDNEW
« no previous file with comments | « runtime/bin/io_impl_sources.gypi ('k') | runtime/bin/secure_socket.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698