• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2005 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ANDROID_STRONG_POINTER_H
18 #define ANDROID_STRONG_POINTER_H
19 
20 #include <functional>
21 #include <type_traits>  // for common_type.
22 
23 // ---------------------------------------------------------------------------
24 namespace android {
25 
26 template<typename T> class wp;
27 
28 // ---------------------------------------------------------------------------
29 
30 // TODO: Maybe remove sp<> ? wp<> comparison? These are dangerous: If the wp<>
31 // was created before the sp<>, and they point to different objects, they may
32 // compare equal even if they are entirely unrelated. E.g. CameraService
33 // currently performa such comparisons.
34 
35 #define COMPARE_STRONG(_op_)                                           \
36 template<typename U>                                            \
37 inline bool operator _op_ (const sp<U>& o) const {              \
38     return m_ptr _op_ o.m_ptr;                                  \
39 }                                                               \
40 template<typename U>                                            \
41 inline bool operator _op_ (const U* o) const {                  \
42     return m_ptr _op_ o;                                        \
43 }                                                               \
44 /* Needed to handle type inference for nullptr: */              \
45 inline bool operator _op_ (const T* o) const {                  \
46     return m_ptr _op_ o;                                        \
47 }
48 
49 template<template<typename C> class comparator, typename T, typename U>
_sp_compare_(T * a,U * b)50 static inline bool _sp_compare_(T* a, U* b) {
51     return comparator<typename std::common_type<T*, U*>::type>()(a, b);
52 }
53 
54 // Use std::less and friends to avoid undefined behavior when ordering pointers
55 // to different objects.
56 #define COMPARE_STRONG_FUNCTIONAL(_op_, _compare_)               \
57 template<typename U>                                             \
58 inline bool operator _op_ (const sp<U>& o) const {               \
59     return _sp_compare_<_compare_>(m_ptr, o.m_ptr);              \
60 }                                                                \
61 template<typename U>                                             \
62 inline bool operator _op_ (const U* o) const {                   \
63     return _sp_compare_<_compare_>(m_ptr, o);                    \
64 }
65 // ---------------------------------------------------------------------------
66 
67 template<typename T>
68 class sp {
69 public:
sp()70     inline sp() : m_ptr(nullptr) { }
71 
72     sp(T* other);  // NOLINT(implicit)
73     sp(const sp<T>& other);
74     sp(sp<T>&& other) noexcept;
75     template<typename U> sp(U* other);  // NOLINT(implicit)
76     template<typename U> sp(const sp<U>& other);  // NOLINT(implicit)
77     template<typename U> sp(sp<U>&& other);  // NOLINT(implicit)
78 
79     ~sp();
80 
81     // Assignment
82 
83     sp& operator = (T* other);
84     sp& operator = (const sp<T>& other);
85     sp& operator=(sp<T>&& other) noexcept;
86 
87     template<typename U> sp& operator = (const sp<U>& other);
88     template<typename U> sp& operator = (sp<U>&& other);
89     template<typename U> sp& operator = (U* other);
90 
91     //! Special optimization for use by ProcessState (and nobody else).
92     void force_set(T* other);
93 
94     // Reset
95 
96     void clear();
97 
98     // Accessors
99 
100     inline T&       operator* () const     { return *m_ptr; }
101     inline T*       operator-> () const    { return m_ptr;  }
get()102     inline T*       get() const            { return m_ptr; }
103     inline explicit operator bool () const { return m_ptr != nullptr; }
104 
105     // Operators
106 
107     COMPARE_STRONG(==)
108     COMPARE_STRONG(!=)
109     COMPARE_STRONG_FUNCTIONAL(>, std::greater)
110     COMPARE_STRONG_FUNCTIONAL(<, std::less)
111     COMPARE_STRONG_FUNCTIONAL(<=, std::less_equal)
112     COMPARE_STRONG_FUNCTIONAL(>=, std::greater_equal)
113 
114     // Punt these to the wp<> implementation.
115     template<typename U>
116     inline bool operator == (const wp<U>& o) const {
117         return o == *this;
118     }
119 
120     template<typename U>
121     inline bool operator != (const wp<U>& o) const {
122         return o != *this;
123     }
124 
125 private:
126     template<typename Y> friend class sp;
127     template<typename Y> friend class wp;
128     void set_pointer(T* ptr);
129     T* m_ptr;
130 };
131 
132 // For code size reasons, we do not want this inlined or templated.
133 void sp_report_race();
134 
135 #undef COMPARE
136 
137 // ---------------------------------------------------------------------------
138 // No user serviceable parts below here.
139 
140 template<typename T>
sp(T * other)141 sp<T>::sp(T* other)
142         : m_ptr(other) {
143     if (other)
144         other->incStrong(this);
145 }
146 
147 template<typename T>
sp(const sp<T> & other)148 sp<T>::sp(const sp<T>& other)
149         : m_ptr(other.m_ptr) {
150     if (m_ptr)
151         m_ptr->incStrong(this);
152 }
153 
154 template <typename T>
sp(sp<T> && other)155 sp<T>::sp(sp<T>&& other) noexcept : m_ptr(other.m_ptr) {
156     other.m_ptr = nullptr;
157 }
158 
159 template<typename T> template<typename U>
sp(U * other)160 sp<T>::sp(U* other)
161         : m_ptr(other) {
162     if (other)
163         (static_cast<T*>(other))->incStrong(this);
164 }
165 
166 template<typename T> template<typename U>
sp(const sp<U> & other)167 sp<T>::sp(const sp<U>& other)
168         : m_ptr(other.m_ptr) {
169     if (m_ptr)
170         m_ptr->incStrong(this);
171 }
172 
173 template<typename T> template<typename U>
sp(sp<U> && other)174 sp<T>::sp(sp<U>&& other)
175         : m_ptr(other.m_ptr) {
176     other.m_ptr = nullptr;
177 }
178 
179 template<typename T>
~sp()180 sp<T>::~sp() {
181     if (m_ptr)
182         m_ptr->decStrong(this);
183 }
184 
185 template<typename T>
186 sp<T>& sp<T>::operator =(const sp<T>& other) {
187     // Force m_ptr to be read twice, to heuristically check for data races.
188     T* oldPtr(*const_cast<T* volatile*>(&m_ptr));
189     T* otherPtr(other.m_ptr);
190     if (otherPtr) otherPtr->incStrong(this);
191     if (oldPtr) oldPtr->decStrong(this);
192     if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race();
193     m_ptr = otherPtr;
194     return *this;
195 }
196 
197 template <typename T>
198 sp<T>& sp<T>::operator=(sp<T>&& other) noexcept {
199     T* oldPtr(*const_cast<T* volatile*>(&m_ptr));
200     if (oldPtr) oldPtr->decStrong(this);
201     if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race();
202     m_ptr = other.m_ptr;
203     other.m_ptr = nullptr;
204     return *this;
205 }
206 
207 template<typename T>
208 sp<T>& sp<T>::operator =(T* other) {
209     T* oldPtr(*const_cast<T* volatile*>(&m_ptr));
210     if (other) other->incStrong(this);
211     if (oldPtr) oldPtr->decStrong(this);
212     if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race();
213     m_ptr = other;
214     return *this;
215 }
216 
217 template<typename T> template<typename U>
218 sp<T>& sp<T>::operator =(const sp<U>& other) {
219     T* oldPtr(*const_cast<T* volatile*>(&m_ptr));
220     T* otherPtr(other.m_ptr);
221     if (otherPtr) otherPtr->incStrong(this);
222     if (oldPtr) oldPtr->decStrong(this);
223     if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race();
224     m_ptr = otherPtr;
225     return *this;
226 }
227 
228 template<typename T> template<typename U>
229 sp<T>& sp<T>::operator =(sp<U>&& other) {
230     T* oldPtr(*const_cast<T* volatile*>(&m_ptr));
231     if (m_ptr) m_ptr->decStrong(this);
232     if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race();
233     m_ptr = other.m_ptr;
234     other.m_ptr = nullptr;
235     return *this;
236 }
237 
238 template<typename T> template<typename U>
239 sp<T>& sp<T>::operator =(U* other) {
240     T* oldPtr(*const_cast<T* volatile*>(&m_ptr));
241     if (other) (static_cast<T*>(other))->incStrong(this);
242     if (oldPtr) oldPtr->decStrong(this);
243     if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race();
244     m_ptr = other;
245     return *this;
246 }
247 
248 template<typename T>
force_set(T * other)249 void sp<T>::force_set(T* other) {
250     other->forceIncStrong(this);
251     m_ptr = other;
252 }
253 
254 template<typename T>
clear()255 void sp<T>::clear() {
256     T* oldPtr(*const_cast<T* volatile*>(&m_ptr));
257     if (oldPtr) {
258         oldPtr->decStrong(this);
259         if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race();
260         m_ptr = nullptr;
261     }
262 }
263 
264 template<typename T>
set_pointer(T * ptr)265 void sp<T>::set_pointer(T* ptr) {
266     m_ptr = ptr;
267 }
268 
269 }  // namespace android
270 
271 // ---------------------------------------------------------------------------
272 
273 #endif // ANDROID_STRONG_POINTER_H
274