The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
Hans Boehm | 9ba7192 | 2016-07-21 18:56:55 -0700 | [diff] [blame^] | 2 | * Copyright (C) 2016 The Android Open Source Project |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Hans Boehm | 9ba7192 | 2016-07-21 18:56:55 -0700 | [diff] [blame^] | 17 | |
| 18 | // SOME COMMENTS ABOUT USAGE: |
| 19 | |
| 20 | // This provides primarily wp<> weak pointer types and RefBase, which work |
| 21 | // together with sp<> from <StrongPointer.h>. |
| 22 | |
| 23 | // sp<> (and wp<>) are a type of smart pointer that use a well defined protocol |
| 24 | // to operate. As long as the object they are templated with implements that |
| 25 | // protocol, these smart pointers work. In several places the platform |
| 26 | // instantiates sp<> with non-RefBase objects; the two are not tied to each |
| 27 | // other. |
| 28 | |
| 29 | // RefBase is such an implementation and it supports strong pointers, weak |
| 30 | // pointers and some magic features for the binder. |
| 31 | |
| 32 | // So, when using RefBase objects, you have the ability to use strong and weak |
| 33 | // pointers through sp<> and wp<>. |
| 34 | |
| 35 | // Normally, when the last strong pointer goes away, the object is destroyed, |
| 36 | // i.e. it's destructor is called. HOWEVER, parts of its associated memory is not |
| 37 | // freed until the last weak pointer is released. |
| 38 | |
| 39 | // Weak pointers are essentially "safe" pointers. They are always safe to |
| 40 | // access through promote(). They may return nullptr if the object was |
| 41 | // destroyed because it ran out of strong pointers. This makes them good candidates |
| 42 | // for keys in a cache for instance. |
| 43 | |
| 44 | // Weak pointers remain valid for comparison purposes even after the underlying |
| 45 | // object has been destroyed. Even if object A is destroyed and its memory reused |
| 46 | // for B, A remaining weak pointer to A will not compare equal to one to B. |
| 47 | // This again makes them attractive for use as keys. |
| 48 | |
| 49 | // How is this supposed / intended to be used? |
| 50 | |
| 51 | // Our recommendation is to use strong references (sp<>) when there is an |
| 52 | // ownership relation. e.g. when an object "owns" another one, use a strong |
| 53 | // ref. And of course use strong refs as arguments of functions (it's extremely |
| 54 | // rare that a function will take a wp<>). |
| 55 | |
| 56 | // Typically a newly allocated object will immediately be used to initialize |
| 57 | // a strong pointer, which may then be used to construct or assign to other |
| 58 | // strong and weak pointers. |
| 59 | |
| 60 | // Use weak references when there are no ownership relation. e.g. the keys in a |
| 61 | // cache (you cannot use plain pointers because there is no safe way to acquire |
| 62 | // a strong reference from a vanilla pointer). |
| 63 | |
| 64 | // This implies that two objects should never (or very rarely) have sp<> on |
| 65 | // each other, because they can't both own each other. |
| 66 | |
| 67 | |
| 68 | // Caveats with reference counting |
| 69 | |
| 70 | // Obviously, circular strong references are a big problem; this creates leaks |
| 71 | // and it's hard to debug -- except it's in fact really easy because RefBase has |
| 72 | // tons of debugging code for that. It can basically tell you exactly where the |
| 73 | // leak is. |
| 74 | |
| 75 | // Another problem has to do with destructors with side effects. You must |
| 76 | // assume that the destructor of reference counted objects can be called AT ANY |
| 77 | // TIME. For instance code as simple as this: |
| 78 | |
| 79 | // void setStuff(const sp<Stuff>& stuff) { |
| 80 | // std::lock_guard<std::mutex> lock(mMutex); |
| 81 | // mStuff = stuff; |
| 82 | // } |
| 83 | |
| 84 | // is very dangerous. This code WILL deadlock one day or another. |
| 85 | |
| 86 | // What isn't obvious is that ~Stuff() can be called as a result of the |
| 87 | // assignment. And it gets called with the lock held. First of all, the lock is |
| 88 | // protecting mStuff, not ~Stuff(). Secondly, if ~Stuff() uses its own internal |
| 89 | // mutex, now you have mutex ordering issues. Even worse, if ~Stuff() is |
| 90 | // virtual, now you're calling into "user" code (potentially), by that, I mean, |
| 91 | // code you didn't even write. |
| 92 | |
| 93 | // A correct way to write this code is something like: |
| 94 | |
| 95 | // void setStuff(const sp<Stuff>& stuff) { |
| 96 | // std::unique_lock<std::mutex> lock(mMutex); |
| 97 | // sp<Stuff> hold = mStuff; |
| 98 | // mStuff = stuff; |
| 99 | // lock.unlock(); |
| 100 | // } |
| 101 | |
| 102 | // More importantly, reference counted objects should do as little work as |
| 103 | // possible in their destructor, or at least be mindful that their destructor |
| 104 | // could be called from very weird and unintended places. |
| 105 | |
| 106 | // Other more specific restrictions for wp<> and sp<>: |
| 107 | |
| 108 | // Constructing a strong or weak pointer to "this" in its constructors is almost |
| 109 | // always wrong. In the case of strong pointers. it is always wrong with RefBase |
| 110 | // because the onFirstRef() callback will be mode on an incompletely constructed |
| 111 | // object. In either case, it is wrong if such a pointer does not outlive the |
| 112 | // constructor, since destruction of the smart pointer will attempt to destroy the |
| 113 | // object before construction is finished, normally resulting in a pointer to a |
| 114 | // destroyed object being returned from a new expression. |
| 115 | |
| 116 | // In the case of weak pointers, this occurs because an object that has never been |
| 117 | // referenced by a strong pointer is destroyed when the last weak pointer disappears. |
| 118 | |
| 119 | // Such strong or weak pointers can be safely created in the RefBase onFirstRef() |
| 120 | // callback. |
| 121 | |
| 122 | // Use of wp::unsafe_get() for any purpose other than debugging is almost |
| 123 | // always wrong. Unless you somehow know that there is a longer-lived sp<> to |
| 124 | // the same object, it may well return a pointer to a deallocated object that |
| 125 | // has since been reallocated for a different purpose. (And if you know there |
| 126 | // is a longer-lived sp<>, why not use an sp<> directly?) A wp<> should only be |
| 127 | // dereferenced by using promote(). |
| 128 | |
| 129 | // Explicitly deleting or otherwise destroying a RefBase object with outstanding |
| 130 | // wp<> or sp<> pointers to it will result in heap corruption. |
| 131 | |
| 132 | // Extra Features: |
| 133 | |
| 134 | // RefBase::extendObjectLifetime() can be used to prevent destruction of the |
| 135 | // object while there are still weak references. This is really special purpose |
| 136 | // functionality to support Binder. |
| 137 | |
| 138 | // Wp::promote(), implemented via the attemptIncStrong() member function, is |
| 139 | // used to try to convert a weak pointer back to a strong pointer. It's the |
| 140 | // normal way to try to access the fields of an object referenced only through |
| 141 | // a wp<>. Binder code also sometimes uses attemptIncStrong() directly. |
| 142 | |
| 143 | // RefBase provides a number of additional callbacks for certain reference count |
| 144 | // events, as well as some debugging facilities. |
| 145 | |
| 146 | // Debugging support can be enabled by turning on DEBUG_REFS in RefBase.cpp. |
| 147 | // Otherwise essentially no checking is provided. |
| 148 | |
| 149 | // Thread safety: |
| 150 | |
| 151 | // Like std::shared_ptr, sp<> and wp<> allow concurrent accesses to DIFFERENT |
| 152 | // sp<> and wp<> instances that happen to refer to the same underlying object. |
| 153 | // They do NOT support concurrent access (where at least one access is a write) |
| 154 | // to THE SAME sp<> or wp<>. In effect, their thread-safety properties are |
| 155 | // exactly like those of T*, NOT atomic<T*>. |
| 156 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | #ifndef ANDROID_REF_BASE_H |
| 158 | #define ANDROID_REF_BASE_H |
| 159 | |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 160 | #include <atomic> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | |
| 162 | #include <stdint.h> |
| 163 | #include <sys/types.h> |
| 164 | #include <stdlib.h> |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 165 | #include <string.h> |
| 166 | |
| 167 | #include <utils/StrongPointer.h> |
Jeff Brown | 9a0a76d | 2012-03-16 14:45:49 -0700 | [diff] [blame] | 168 | #include <utils/TypeHelpers.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | |
| 170 | // --------------------------------------------------------------------------- |
| 171 | namespace android { |
| 172 | |
Mathias Agopian | 84a23fa | 2011-02-16 15:23:08 -0800 | [diff] [blame] | 173 | class TextOutput; |
Mathias Agopian | 84a23fa | 2011-02-16 15:23:08 -0800 | [diff] [blame] | 174 | TextOutput& printWeakPointer(TextOutput& to, const void* val); |
| 175 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | // --------------------------------------------------------------------------- |
| 177 | |
Mathias Agopian | ff49de7 | 2011-02-09 18:38:55 -0800 | [diff] [blame] | 178 | #define COMPARE_WEAK(_op_) \ |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | inline bool operator _op_ (const sp<T>& o) const { \ |
| 180 | return m_ptr _op_ o.m_ptr; \ |
| 181 | } \ |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | inline bool operator _op_ (const T* o) const { \ |
| 183 | return m_ptr _op_ o; \ |
| 184 | } \ |
| 185 | template<typename U> \ |
| 186 | inline bool operator _op_ (const sp<U>& o) const { \ |
| 187 | return m_ptr _op_ o.m_ptr; \ |
| 188 | } \ |
| 189 | template<typename U> \ |
Mathias Agopian | ff49de7 | 2011-02-09 18:38:55 -0800 | [diff] [blame] | 190 | inline bool operator _op_ (const U* o) const { \ |
| 191 | return m_ptr _op_ o; \ |
| 192 | } |
| 193 | |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 194 | // --------------------------------------------------------------------------- |
Mathias Agopian | 6cd548c | 2013-03-18 22:27:41 -0700 | [diff] [blame] | 195 | |
| 196 | class ReferenceRenamer { |
| 197 | protected: |
| 198 | // destructor is purposedly not virtual so we avoid code overhead from |
| 199 | // subclasses; we have to make it protected to guarantee that it |
| 200 | // cannot be called from this base class (and to make strict compilers |
| 201 | // happy). |
| 202 | ~ReferenceRenamer() { } |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 203 | public: |
Mathias Agopian | 6cd548c | 2013-03-18 22:27:41 -0700 | [diff] [blame] | 204 | virtual void operator()(size_t i) const = 0; |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 205 | }; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | |
| 207 | // --------------------------------------------------------------------------- |
| 208 | |
| 209 | class RefBase |
| 210 | { |
| 211 | public: |
| 212 | void incStrong(const void* id) const; |
| 213 | void decStrong(const void* id) const; |
| 214 | |
| 215 | void forceIncStrong(const void* id) const; |
| 216 | |
| 217 | //! DEBUGGING ONLY: Get current strong ref count. |
| 218 | int32_t getStrongCount() const; |
| 219 | |
| 220 | class weakref_type |
| 221 | { |
| 222 | public: |
| 223 | RefBase* refBase() const; |
| 224 | |
| 225 | void incWeak(const void* id); |
| 226 | void decWeak(const void* id); |
| 227 | |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 228 | // acquires a strong reference if there is already one. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | bool attemptIncStrong(const void* id); |
| 230 | |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 231 | // acquires a weak reference if there is already one. |
| 232 | // This is not always safe. see ProcessState.cpp and BpBinder.cpp |
| 233 | // for proper use. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | bool attemptIncWeak(const void* id); |
| 235 | |
| 236 | //! DEBUGGING ONLY: Get current weak ref count. |
| 237 | int32_t getWeakCount() const; |
| 238 | |
| 239 | //! DEBUGGING ONLY: Print references held on object. |
| 240 | void printRefs() const; |
| 241 | |
| 242 | //! DEBUGGING ONLY: Enable tracking for this object. |
| 243 | // enable -- enable/disable tracking |
| 244 | // retain -- when tracking is enable, if true, then we save a stack trace |
| 245 | // for each reference and dereference; when retain == false, we |
| 246 | // match up references and dereferences and keep only the |
| 247 | // outstanding ones. |
| 248 | |
| 249 | void trackMe(bool enable, bool retain); |
| 250 | }; |
| 251 | |
| 252 | weakref_type* createWeak(const void* id) const; |
| 253 | |
| 254 | weakref_type* getWeakRefs() const; |
| 255 | |
| 256 | //! DEBUGGING ONLY: Print references held on object. |
| 257 | inline void printRefs() const { getWeakRefs()->printRefs(); } |
| 258 | |
| 259 | //! DEBUGGING ONLY: Enable tracking of object. |
| 260 | inline void trackMe(bool enable, bool retain) |
| 261 | { |
| 262 | getWeakRefs()->trackMe(enable, retain); |
| 263 | } |
| 264 | |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 265 | typedef RefBase basetype; |
| 266 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | protected: |
| 268 | RefBase(); |
| 269 | virtual ~RefBase(); |
Mathias Agopian | 7f57eac | 2011-06-16 17:15:51 -0700 | [diff] [blame] | 270 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | //! Flags for extendObjectLifetime() |
| 272 | enum { |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 273 | OBJECT_LIFETIME_STRONG = 0x0000, |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | OBJECT_LIFETIME_WEAK = 0x0001, |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 275 | OBJECT_LIFETIME_MASK = 0x0001 |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | }; |
| 277 | |
| 278 | void extendObjectLifetime(int32_t mode); |
| 279 | |
| 280 | //! Flags for onIncStrongAttempted() |
| 281 | enum { |
| 282 | FIRST_INC_STRONG = 0x0001 |
| 283 | }; |
| 284 | |
Hans Boehm | 9ba7192 | 2016-07-21 18:56:55 -0700 | [diff] [blame^] | 285 | // Invoked after creation of initial strong pointer/reference. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | virtual void onFirstRef(); |
Hans Boehm | 9ba7192 | 2016-07-21 18:56:55 -0700 | [diff] [blame^] | 287 | // Invoked when either the last strong reference goes away, or we need to undo |
| 288 | // the effect of an unnecessary onIncStrongAttempted. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | virtual void onLastStrongRef(const void* id); |
Hans Boehm | 9ba7192 | 2016-07-21 18:56:55 -0700 | [diff] [blame^] | 290 | // Only called in OBJECT_LIFETIME_WEAK case. Returns true if OK to promote to |
| 291 | // strong reference. May have side effects if it returns true. |
| 292 | // The first flags argument is always FIRST_INC_STRONG. |
| 293 | // TODO: Remove initial flag argument. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | virtual bool onIncStrongAttempted(uint32_t flags, const void* id); |
Hans Boehm | 9ba7192 | 2016-07-21 18:56:55 -0700 | [diff] [blame^] | 295 | // Invoked in the OBJECT_LIFETIME_WEAK case when the last reference of either |
| 296 | // kind goes away. Unused. |
| 297 | // TODO: Remove. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | virtual void onLastWeakRef(const void* id); |
| 299 | |
| 300 | private: |
| 301 | friend class weakref_type; |
| 302 | class weakref_impl; |
| 303 | |
| 304 | RefBase(const RefBase& o); |
| 305 | RefBase& operator=(const RefBase& o); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 306 | |
Mathias Agopian | 6cd548c | 2013-03-18 22:27:41 -0700 | [diff] [blame] | 307 | private: |
| 308 | friend class ReferenceMover; |
| 309 | |
| 310 | static void renameRefs(size_t n, const ReferenceRenamer& renamer); |
| 311 | |
| 312 | static void renameRefId(weakref_type* ref, |
| 313 | const void* old_id, const void* new_id); |
| 314 | |
| 315 | static void renameRefId(RefBase* ref, |
| 316 | const void* old_id, const void* new_id); |
| 317 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | weakref_impl* const mRefs; |
| 319 | }; |
| 320 | |
| 321 | // --------------------------------------------------------------------------- |
| 322 | |
| 323 | template <class T> |
| 324 | class LightRefBase |
| 325 | { |
| 326 | public: |
| 327 | inline LightRefBase() : mCount(0) { } |
Igor Murashkin | a27c1e0 | 2012-12-05 16:10:26 -0800 | [diff] [blame] | 328 | inline void incStrong(__attribute__((unused)) const void* id) const { |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 329 | mCount.fetch_add(1, std::memory_order_relaxed); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | } |
Igor Murashkin | a27c1e0 | 2012-12-05 16:10:26 -0800 | [diff] [blame] | 331 | inline void decStrong(__attribute__((unused)) const void* id) const { |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 332 | if (mCount.fetch_sub(1, std::memory_order_release) == 1) { |
| 333 | std::atomic_thread_fence(std::memory_order_acquire); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | delete static_cast<const T*>(this); |
| 335 | } |
| 336 | } |
Mathias Agopian | 019f8ed | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 337 | //! DEBUGGING ONLY: Get current strong ref count. |
| 338 | inline int32_t getStrongCount() const { |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 339 | return mCount.load(std::memory_order_relaxed); |
Mathias Agopian | 019f8ed | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 340 | } |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 341 | |
| 342 | typedef LightRefBase<T> basetype; |
| 343 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | protected: |
| 345 | inline ~LightRefBase() { } |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 346 | |
| 347 | private: |
| 348 | friend class ReferenceMover; |
Greg Kaiser | d9885e7 | 2016-07-08 17:18:07 -0700 | [diff] [blame] | 349 | inline static void renameRefs(size_t /*n*/, |
| 350 | const ReferenceRenamer& /*renamer*/) { } |
| 351 | inline static void renameRefId(T* /*ref*/, |
| 352 | const void* /*old_id*/ , const void* /*new_id*/) { } |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 353 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | private: |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 355 | mutable std::atomic<int32_t> mCount; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | }; |
| 357 | |
John Reck | d83186c | 2014-05-09 15:27:22 -0700 | [diff] [blame] | 358 | // This is a wrapper around LightRefBase that simply enforces a virtual |
| 359 | // destructor to eliminate the template requirement of LightRefBase |
| 360 | class VirtualLightRefBase : public LightRefBase<VirtualLightRefBase> { |
| 361 | public: |
| 362 | virtual ~VirtualLightRefBase() {} |
| 363 | }; |
| 364 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | // --------------------------------------------------------------------------- |
| 366 | |
| 367 | template <typename T> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | class wp |
| 369 | { |
| 370 | public: |
| 371 | typedef typename RefBase::weakref_type weakref_type; |
| 372 | |
| 373 | inline wp() : m_ptr(0) { } |
| 374 | |
| 375 | wp(T* other); |
| 376 | wp(const wp<T>& other); |
| 377 | wp(const sp<T>& other); |
| 378 | template<typename U> wp(U* other); |
| 379 | template<typename U> wp(const sp<U>& other); |
| 380 | template<typename U> wp(const wp<U>& other); |
| 381 | |
| 382 | ~wp(); |
| 383 | |
| 384 | // Assignment |
| 385 | |
| 386 | wp& operator = (T* other); |
| 387 | wp& operator = (const wp<T>& other); |
| 388 | wp& operator = (const sp<T>& other); |
| 389 | |
| 390 | template<typename U> wp& operator = (U* other); |
| 391 | template<typename U> wp& operator = (const wp<U>& other); |
| 392 | template<typename U> wp& operator = (const sp<U>& other); |
| 393 | |
| 394 | void set_object_and_refs(T* other, weakref_type* refs); |
| 395 | |
| 396 | // promotion to sp |
| 397 | |
| 398 | sp<T> promote() const; |
| 399 | |
| 400 | // Reset |
| 401 | |
| 402 | void clear(); |
| 403 | |
| 404 | // Accessors |
| 405 | |
| 406 | inline weakref_type* get_refs() const { return m_refs; } |
| 407 | |
| 408 | inline T* unsafe_get() const { return m_ptr; } |
| 409 | |
| 410 | // Operators |
Mathias Agopian | ff49de7 | 2011-02-09 18:38:55 -0800 | [diff] [blame] | 411 | |
| 412 | COMPARE_WEAK(==) |
| 413 | COMPARE_WEAK(!=) |
| 414 | COMPARE_WEAK(>) |
| 415 | COMPARE_WEAK(<) |
| 416 | COMPARE_WEAK(<=) |
| 417 | COMPARE_WEAK(>=) |
| 418 | |
| 419 | inline bool operator == (const wp<T>& o) const { |
| 420 | return (m_ptr == o.m_ptr) && (m_refs == o.m_refs); |
| 421 | } |
| 422 | template<typename U> |
| 423 | inline bool operator == (const wp<U>& o) const { |
| 424 | return m_ptr == o.m_ptr; |
| 425 | } |
| 426 | |
| 427 | inline bool operator > (const wp<T>& o) const { |
| 428 | return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr); |
| 429 | } |
| 430 | template<typename U> |
| 431 | inline bool operator > (const wp<U>& o) const { |
| 432 | return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr); |
| 433 | } |
| 434 | |
| 435 | inline bool operator < (const wp<T>& o) const { |
| 436 | return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr); |
| 437 | } |
| 438 | template<typename U> |
| 439 | inline bool operator < (const wp<U>& o) const { |
| 440 | return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr); |
| 441 | } |
| 442 | inline bool operator != (const wp<T>& o) const { return m_refs != o.m_refs; } |
| 443 | template<typename U> inline bool operator != (const wp<U>& o) const { return !operator == (o); } |
| 444 | inline bool operator <= (const wp<T>& o) const { return !operator > (o); } |
| 445 | template<typename U> inline bool operator <= (const wp<U>& o) const { return !operator > (o); } |
| 446 | inline bool operator >= (const wp<T>& o) const { return !operator < (o); } |
| 447 | template<typename U> inline bool operator >= (const wp<U>& o) const { return !operator < (o); } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 448 | |
| 449 | private: |
| 450 | template<typename Y> friend class sp; |
| 451 | template<typename Y> friend class wp; |
| 452 | |
| 453 | T* m_ptr; |
| 454 | weakref_type* m_refs; |
| 455 | }; |
| 456 | |
| 457 | template <typename T> |
| 458 | TextOutput& operator<<(TextOutput& to, const wp<T>& val); |
| 459 | |
Mathias Agopian | ff49de7 | 2011-02-09 18:38:55 -0800 | [diff] [blame] | 460 | #undef COMPARE_WEAK |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | |
| 462 | // --------------------------------------------------------------------------- |
| 463 | // No user serviceable parts below here. |
| 464 | |
| 465 | template<typename T> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 466 | wp<T>::wp(T* other) |
| 467 | : m_ptr(other) |
| 468 | { |
| 469 | if (other) m_refs = other->createWeak(this); |
| 470 | } |
| 471 | |
| 472 | template<typename T> |
| 473 | wp<T>::wp(const wp<T>& other) |
| 474 | : m_ptr(other.m_ptr), m_refs(other.m_refs) |
| 475 | { |
| 476 | if (m_ptr) m_refs->incWeak(this); |
| 477 | } |
| 478 | |
| 479 | template<typename T> |
| 480 | wp<T>::wp(const sp<T>& other) |
| 481 | : m_ptr(other.m_ptr) |
| 482 | { |
| 483 | if (m_ptr) { |
| 484 | m_refs = m_ptr->createWeak(this); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | template<typename T> template<typename U> |
| 489 | wp<T>::wp(U* other) |
| 490 | : m_ptr(other) |
| 491 | { |
| 492 | if (other) m_refs = other->createWeak(this); |
| 493 | } |
| 494 | |
| 495 | template<typename T> template<typename U> |
| 496 | wp<T>::wp(const wp<U>& other) |
| 497 | : m_ptr(other.m_ptr) |
| 498 | { |
| 499 | if (m_ptr) { |
| 500 | m_refs = other.m_refs; |
| 501 | m_refs->incWeak(this); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | template<typename T> template<typename U> |
| 506 | wp<T>::wp(const sp<U>& other) |
| 507 | : m_ptr(other.m_ptr) |
| 508 | { |
| 509 | if (m_ptr) { |
| 510 | m_refs = m_ptr->createWeak(this); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | template<typename T> |
| 515 | wp<T>::~wp() |
| 516 | { |
| 517 | if (m_ptr) m_refs->decWeak(this); |
| 518 | } |
| 519 | |
| 520 | template<typename T> |
| 521 | wp<T>& wp<T>::operator = (T* other) |
| 522 | { |
| 523 | weakref_type* newRefs = |
| 524 | other ? other->createWeak(this) : 0; |
| 525 | if (m_ptr) m_refs->decWeak(this); |
| 526 | m_ptr = other; |
| 527 | m_refs = newRefs; |
| 528 | return *this; |
| 529 | } |
| 530 | |
| 531 | template<typename T> |
| 532 | wp<T>& wp<T>::operator = (const wp<T>& other) |
| 533 | { |
Mathias Agopian | 7b15167 | 2010-06-24 21:49:02 -0700 | [diff] [blame] | 534 | weakref_type* otherRefs(other.m_refs); |
| 535 | T* otherPtr(other.m_ptr); |
| 536 | if (otherPtr) otherRefs->incWeak(this); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 537 | if (m_ptr) m_refs->decWeak(this); |
Mathias Agopian | 7b15167 | 2010-06-24 21:49:02 -0700 | [diff] [blame] | 538 | m_ptr = otherPtr; |
| 539 | m_refs = otherRefs; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | return *this; |
| 541 | } |
| 542 | |
| 543 | template<typename T> |
| 544 | wp<T>& wp<T>::operator = (const sp<T>& other) |
| 545 | { |
| 546 | weakref_type* newRefs = |
| 547 | other != NULL ? other->createWeak(this) : 0; |
Mathias Agopian | 7b15167 | 2010-06-24 21:49:02 -0700 | [diff] [blame] | 548 | T* otherPtr(other.m_ptr); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | if (m_ptr) m_refs->decWeak(this); |
Mathias Agopian | 7b15167 | 2010-06-24 21:49:02 -0700 | [diff] [blame] | 550 | m_ptr = otherPtr; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 551 | m_refs = newRefs; |
| 552 | return *this; |
| 553 | } |
| 554 | |
| 555 | template<typename T> template<typename U> |
| 556 | wp<T>& wp<T>::operator = (U* other) |
| 557 | { |
| 558 | weakref_type* newRefs = |
| 559 | other ? other->createWeak(this) : 0; |
| 560 | if (m_ptr) m_refs->decWeak(this); |
| 561 | m_ptr = other; |
| 562 | m_refs = newRefs; |
| 563 | return *this; |
| 564 | } |
| 565 | |
| 566 | template<typename T> template<typename U> |
| 567 | wp<T>& wp<T>::operator = (const wp<U>& other) |
| 568 | { |
Mathias Agopian | 7b15167 | 2010-06-24 21:49:02 -0700 | [diff] [blame] | 569 | weakref_type* otherRefs(other.m_refs); |
| 570 | U* otherPtr(other.m_ptr); |
| 571 | if (otherPtr) otherRefs->incWeak(this); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 572 | if (m_ptr) m_refs->decWeak(this); |
Mathias Agopian | 7b15167 | 2010-06-24 21:49:02 -0700 | [diff] [blame] | 573 | m_ptr = otherPtr; |
| 574 | m_refs = otherRefs; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | return *this; |
| 576 | } |
| 577 | |
| 578 | template<typename T> template<typename U> |
| 579 | wp<T>& wp<T>::operator = (const sp<U>& other) |
| 580 | { |
| 581 | weakref_type* newRefs = |
| 582 | other != NULL ? other->createWeak(this) : 0; |
Mathias Agopian | 7b15167 | 2010-06-24 21:49:02 -0700 | [diff] [blame] | 583 | U* otherPtr(other.m_ptr); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 584 | if (m_ptr) m_refs->decWeak(this); |
Mathias Agopian | 7b15167 | 2010-06-24 21:49:02 -0700 | [diff] [blame] | 585 | m_ptr = otherPtr; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | m_refs = newRefs; |
| 587 | return *this; |
| 588 | } |
| 589 | |
| 590 | template<typename T> |
| 591 | void wp<T>::set_object_and_refs(T* other, weakref_type* refs) |
| 592 | { |
| 593 | if (other) refs->incWeak(this); |
| 594 | if (m_ptr) m_refs->decWeak(this); |
| 595 | m_ptr = other; |
| 596 | m_refs = refs; |
| 597 | } |
| 598 | |
| 599 | template<typename T> |
| 600 | sp<T> wp<T>::promote() const |
| 601 | { |
Mathias Agopian | 3e0f875 | 2011-02-24 18:12:34 -0800 | [diff] [blame] | 602 | sp<T> result; |
| 603 | if (m_ptr && m_refs->attemptIncStrong(&result)) { |
| 604 | result.set_pointer(m_ptr); |
| 605 | } |
| 606 | return result; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | template<typename T> |
| 610 | void wp<T>::clear() |
| 611 | { |
| 612 | if (m_ptr) { |
| 613 | m_refs->decWeak(this); |
| 614 | m_ptr = 0; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | template <typename T> |
| 619 | inline TextOutput& operator<<(TextOutput& to, const wp<T>& val) |
| 620 | { |
Mathias Agopian | 84a23fa | 2011-02-16 15:23:08 -0800 | [diff] [blame] | 621 | return printWeakPointer(to, val.unsafe_get()); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 622 | } |
| 623 | |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 624 | // --------------------------------------------------------------------------- |
| 625 | |
| 626 | // this class just serves as a namespace so TYPE::moveReferences can stay |
| 627 | // private. |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 628 | class ReferenceMover { |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 629 | public: |
Mathias Agopian | 6cd548c | 2013-03-18 22:27:41 -0700 | [diff] [blame] | 630 | // it would be nice if we could make sure no extra code is generated |
| 631 | // for sp<TYPE> or wp<TYPE> when TYPE is a descendant of RefBase: |
| 632 | // Using a sp<RefBase> override doesn't work; it's a bit like we wanted |
| 633 | // a template<typename TYPE inherits RefBase> template... |
| 634 | |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 635 | template<typename TYPE> static inline |
| 636 | void move_references(sp<TYPE>* d, sp<TYPE> const* s, size_t n) { |
Mathias Agopian | 6cd548c | 2013-03-18 22:27:41 -0700 | [diff] [blame] | 637 | |
| 638 | class Renamer : public ReferenceRenamer { |
| 639 | sp<TYPE>* d; |
| 640 | sp<TYPE> const* s; |
| 641 | virtual void operator()(size_t i) const { |
| 642 | // The id are known to be the sp<>'s this pointer |
| 643 | TYPE::renameRefId(d[i].get(), &s[i], &d[i]); |
| 644 | } |
| 645 | public: |
Ukri Niemimuukko | 00e56a2 | 2014-04-29 06:25:28 +0800 | [diff] [blame] | 646 | Renamer(sp<TYPE>* d, sp<TYPE> const* s) : d(d), s(s) { } |
| 647 | virtual ~Renamer() { } |
Mathias Agopian | 6cd548c | 2013-03-18 22:27:41 -0700 | [diff] [blame] | 648 | }; |
| 649 | |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 650 | memmove(d, s, n*sizeof(sp<TYPE>)); |
Mathias Agopian | 6cd548c | 2013-03-18 22:27:41 -0700 | [diff] [blame] | 651 | TYPE::renameRefs(n, Renamer(d, s)); |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 652 | } |
Mathias Agopian | 6cd548c | 2013-03-18 22:27:41 -0700 | [diff] [blame] | 653 | |
| 654 | |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 655 | template<typename TYPE> static inline |
| 656 | void move_references(wp<TYPE>* d, wp<TYPE> const* s, size_t n) { |
Mathias Agopian | 6cd548c | 2013-03-18 22:27:41 -0700 | [diff] [blame] | 657 | |
| 658 | class Renamer : public ReferenceRenamer { |
| 659 | wp<TYPE>* d; |
| 660 | wp<TYPE> const* s; |
| 661 | virtual void operator()(size_t i) const { |
| 662 | // The id are known to be the wp<>'s this pointer |
| 663 | TYPE::renameRefId(d[i].get_refs(), &s[i], &d[i]); |
| 664 | } |
| 665 | public: |
Ukri Niemimuukko | 00e56a2 | 2014-04-29 06:25:28 +0800 | [diff] [blame] | 666 | Renamer(wp<TYPE>* d, wp<TYPE> const* s) : d(d), s(s) { } |
| 667 | virtual ~Renamer() { } |
Mathias Agopian | 6cd548c | 2013-03-18 22:27:41 -0700 | [diff] [blame] | 668 | }; |
| 669 | |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 670 | memmove(d, s, n*sizeof(wp<TYPE>)); |
Mathias Agopian | 6cd548c | 2013-03-18 22:27:41 -0700 | [diff] [blame] | 671 | TYPE::renameRefs(n, Renamer(d, s)); |
Mathias Agopian | b26ea8b | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 672 | } |
| 673 | }; |
| 674 | |
| 675 | // specialization for moving sp<> and wp<> types. |
| 676 | // these are used by the [Sorted|Keyed]Vector<> implementations |
| 677 | // sp<> and wp<> need to be handled specially, because they do not |
| 678 | // have trivial copy operation in the general case (see RefBase.cpp |
| 679 | // when DEBUG ops are enabled), but can be implemented very |
| 680 | // efficiently in most cases. |
| 681 | |
| 682 | template<typename TYPE> inline |
| 683 | void move_forward_type(sp<TYPE>* d, sp<TYPE> const* s, size_t n) { |
| 684 | ReferenceMover::move_references(d, s, n); |
| 685 | } |
| 686 | |
| 687 | template<typename TYPE> inline |
| 688 | void move_backward_type(sp<TYPE>* d, sp<TYPE> const* s, size_t n) { |
| 689 | ReferenceMover::move_references(d, s, n); |
| 690 | } |
| 691 | |
| 692 | template<typename TYPE> inline |
| 693 | void move_forward_type(wp<TYPE>* d, wp<TYPE> const* s, size_t n) { |
| 694 | ReferenceMover::move_references(d, s, n); |
| 695 | } |
| 696 | |
| 697 | template<typename TYPE> inline |
| 698 | void move_backward_type(wp<TYPE>* d, wp<TYPE> const* s, size_t n) { |
| 699 | ReferenceMover::move_references(d, s, n); |
| 700 | } |
| 701 | |
| 702 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 703 | }; // namespace android |
| 704 | |
| 705 | // --------------------------------------------------------------------------- |
| 706 | |
| 707 | #endif // ANDROID_REF_BASE_H |