The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | #define LOG_TAG "RefBase" |
Mathias Agopian | da8ec4b | 2013-03-19 17:36:57 -0700 | [diff] [blame] | 18 | // #define LOG_NDEBUG 0 |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | |
Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 20 | #include <memory> |
| 21 | |
Chih-Hung Hsieh | 502f486 | 2018-09-13 11:08:41 -0700 | [diff] [blame] | 22 | #include <android-base/macros.h> |
| 23 | |
Steven Moreland | 377adea | 2022-10-08 05:06:52 +0000 | [diff] [blame] | 24 | #include <fcntl.h> |
Christopher Ferris | 0e69160 | 2020-10-14 14:13:58 -0700 | [diff] [blame] | 25 | #include <log/log.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
Christopher Ferris | 0e69160 | 2020-10-14 14:13:58 -0700 | [diff] [blame] | 27 | #include <utils/RefBase.h> |
Steven Moreland | 377adea | 2022-10-08 05:06:52 +0000 | [diff] [blame] | 28 | #include <utils/String8.h> |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 30 | #include <utils/Mutex.h> |
| 31 | |
Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 32 | #ifndef __unused |
| 33 | #define __unused __attribute__((__unused__)) |
| 34 | #endif |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 36 | // Compile with refcounting debugging enabled. |
Steven Moreland | 377adea | 2022-10-08 05:06:52 +0000 | [diff] [blame] | 37 | #ifndef DEBUG_REFS |
Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 38 | #define DEBUG_REFS 0 |
Steven Moreland | 377adea | 2022-10-08 05:06:52 +0000 | [diff] [blame] | 39 | #endif |
Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 40 | |
| 41 | // The following three are ignored unless DEBUG_REFS is set. |
Mathias Agopian | 6d4419d | 2013-03-18 20:31:18 -0700 | [diff] [blame] | 42 | |
| 43 | // whether ref-tracking is enabled by default, if not, trackMe(true, false) |
| 44 | // needs to be called explicitly |
Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 45 | #define DEBUG_REFS_ENABLED_BY_DEFAULT 0 |
Mathias Agopian | 6d4419d | 2013-03-18 20:31:18 -0700 | [diff] [blame] | 46 | |
| 47 | // whether callstack are collected (significantly slows things down) |
Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 48 | #define DEBUG_REFS_CALLSTACK_ENABLED 1 |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | |
Mathias Agopian | 6d4419d | 2013-03-18 20:31:18 -0700 | [diff] [blame] | 50 | // folder where stack traces are saved when DEBUG_REFS is enabled |
| 51 | // this folder needs to exist and be writable |
Steven Moreland | b7412c8 | 2022-10-08 05:07:52 +0000 | [diff] [blame] | 52 | #ifdef __ANDROID__ |
Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 53 | #define DEBUG_REFS_CALLSTACK_PATH "/data/debug" |
Steven Moreland | b7412c8 | 2022-10-08 05:07:52 +0000 | [diff] [blame] | 54 | #else |
| 55 | #define DEBUG_REFS_CALLSTACK_PATH "." |
| 56 | #endif |
Mathias Agopian | 6d4419d | 2013-03-18 20:31:18 -0700 | [diff] [blame] | 57 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | // log all reference counting operations |
Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 59 | #define PRINT_REFS 0 |
| 60 | |
Andrei Homescu | 5c15de2 | 2021-12-10 05:32:17 +0000 | [diff] [blame] | 61 | #if defined(__linux__) |
Christopher Ferris | 0e69160 | 2020-10-14 14:13:58 -0700 | [diff] [blame] | 62 | // CallStack is only supported on linux type platforms. |
| 63 | #define CALLSTACK_ENABLED 1 |
| 64 | #else |
| 65 | #define CALLSTACK_ENABLED 0 |
| 66 | #endif |
| 67 | |
| 68 | #if CALLSTACK_ENABLED |
| 69 | #include <utils/CallStack.h> |
| 70 | #endif |
| 71 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | // --------------------------------------------------------------------------- |
| 73 | |
| 74 | namespace android { |
| 75 | |
Hans Boehm | 9ba7192 | 2016-07-21 18:56:55 -0700 | [diff] [blame] | 76 | // Observations, invariants, etc: |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 77 | |
Hans Boehm | 9ba7192 | 2016-07-21 18:56:55 -0700 | [diff] [blame] | 78 | // By default, obects are destroyed when the last strong reference disappears |
| 79 | // or, if the object never had a strong reference, when the last weak reference |
| 80 | // disappears. |
| 81 | // |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 82 | // OBJECT_LIFETIME_WEAK changes this behavior to retain the object |
| 83 | // unconditionally until the last reference of either kind disappears. The |
| 84 | // client ensures that the extendObjectLifetime call happens before the dec |
| 85 | // call that would otherwise have deallocated the object, or before an |
| 86 | // attemptIncStrong call that might rely on it. We do not worry about |
| 87 | // concurrent changes to the object lifetime. |
Hans Boehm | 9ba7192 | 2016-07-21 18:56:55 -0700 | [diff] [blame] | 88 | // |
| 89 | // AttemptIncStrong will succeed if the object has a strong reference, or if it |
| 90 | // has a weak reference and has never had a strong reference. |
| 91 | // AttemptIncWeak really does succeed only if there is already a WEAK |
| 92 | // reference, and thus may fail when attemptIncStrong would succeed. |
| 93 | // |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 94 | // mStrong is the strong reference count. mWeak is the weak reference count. |
| 95 | // Between calls, and ignoring memory ordering effects, mWeak includes strong |
| 96 | // references, and is thus >= mStrong. |
| 97 | // |
Hans Boehm | 9ba7192 | 2016-07-21 18:56:55 -0700 | [diff] [blame] | 98 | // A weakref_impl holds all the information, including both reference counts, |
| 99 | // required to perform wp<> operations. Thus these can continue to be performed |
| 100 | // after the RefBase object has been destroyed. |
| 101 | // |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 102 | // A weakref_impl is allocated as the value of mRefs in a RefBase object on |
| 103 | // construction. |
Hans Boehm | 23c857e | 2016-08-02 18:39:30 -0700 | [diff] [blame] | 104 | // In the OBJECT_LIFETIME_STRONG case, it is normally deallocated in decWeak, |
| 105 | // and hence lives as long as the last weak reference. (It can also be |
| 106 | // deallocated in the RefBase destructor iff the strong reference count was |
| 107 | // never incremented and the weak count is zero, e.g. if the RefBase object is |
| 108 | // explicitly destroyed without decrementing the strong count. This should be |
| 109 | // avoided.) In this case, the RefBase destructor should be invoked from |
| 110 | // decStrong. |
| 111 | // In the OBJECT_LIFETIME_WEAK case, the weakref_impl is always deallocated in |
| 112 | // the RefBase destructor, which is always invoked by decWeak. DecStrong |
| 113 | // explicitly avoids the deletion in this case. |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 114 | // |
| 115 | // Memory ordering: |
| 116 | // The client must ensure that every inc() call, together with all other |
| 117 | // accesses to the object, happens before the corresponding dec() call. |
| 118 | // |
| 119 | // We try to keep memory ordering constraints on atomics as weak as possible, |
| 120 | // since memory fences or ordered memory accesses are likely to be a major |
| 121 | // performance cost for this code. All accesses to mStrong, mWeak, and mFlags |
| 122 | // explicitly relax memory ordering in some way. |
| 123 | // |
| 124 | // The only operations that are not memory_order_relaxed are reference count |
| 125 | // decrements. All reference count decrements are release operations. In |
| 126 | // addition, the final decrement leading the deallocation is followed by an |
| 127 | // acquire fence, which we can view informally as also turning it into an |
| 128 | // acquire operation. (See 29.8p4 [atomics.fences] for details. We could |
| 129 | // alternatively use acq_rel operations for all decrements. This is probably |
| 130 | // slower on most current (2016) hardware, especially on ARMv7, but that may |
| 131 | // not be true indefinitely.) |
| 132 | // |
| 133 | // This convention ensures that the second-to-last decrement synchronizes with |
| 134 | // (in the language of 1.10 in the C++ standard) the final decrement of a |
| 135 | // reference count. Since reference counts are only updated using atomic |
| 136 | // read-modify-write operations, this also extends to any earlier decrements. |
| 137 | // (See "release sequence" in 1.10.) |
| 138 | // |
| 139 | // Since all operations on an object happen before the corresponding reference |
| 140 | // count decrement, and all reference count decrements happen before the final |
| 141 | // one, we are guaranteed that all other object accesses happen before the |
| 142 | // object is destroyed. |
| 143 | |
| 144 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | #define INITIAL_STRONG_VALUE (1<<28) |
| 146 | |
Hans Boehm | 23c857e | 2016-08-02 18:39:30 -0700 | [diff] [blame] | 147 | #define MAX_COUNT 0xfffff |
| 148 | |
| 149 | // Test whether the argument is a clearly invalid strong reference count. |
| 150 | // Used only for error checking on the value before an atomic decrement. |
| 151 | // Intended to be very cheap. |
| 152 | // Note that we cannot just check for excess decrements by comparing to zero |
| 153 | // since the object would be deallocated before that. |
| 154 | #define BAD_STRONG(c) \ |
| 155 | ((c) == 0 || ((c) & (~(MAX_COUNT | INITIAL_STRONG_VALUE))) != 0) |
| 156 | |
| 157 | // Same for weak counts. |
| 158 | #define BAD_WEAK(c) ((c) == 0 || ((c) & (~MAX_COUNT)) != 0) |
| 159 | |
Steven Moreland | cd4ef87 | 2022-07-29 00:40:29 +0000 | [diff] [blame] | 160 | // name kept because prebuilts used to use it from inlining sp<> code |
| 161 | void sp_report_stack_pointer() { LOG_ALWAYS_FATAL("RefBase used with stack pointer argument"); } |
Steven Moreland | c340a08 | 2022-07-26 17:09:50 +0000 | [diff] [blame] | 162 | |
| 163 | // Check whether address is definitely on the calling stack. We actually check whether it is on |
| 164 | // the same 4K page as the frame pointer. |
| 165 | // |
| 166 | // Assumptions: |
| 167 | // - Pages are never smaller than 4K (MIN_PAGE_SIZE) |
| 168 | // - Malloced memory never shares a page with a stack. |
| 169 | // |
| 170 | // It does not appear safe to broaden this check to include adjacent pages; apparently this code |
| 171 | // is used in environments where there may not be a guard page below (at higher addresses than) |
| 172 | // the bottom of the stack. |
| 173 | static void check_not_on_stack(const void* ptr) { |
| 174 | static constexpr int MIN_PAGE_SIZE = 0x1000; // 4K. Safer than including sys/user.h. |
| 175 | static constexpr uintptr_t MIN_PAGE_MASK = ~static_cast<uintptr_t>(MIN_PAGE_SIZE - 1); |
| 176 | uintptr_t my_frame_address = |
| 177 | reinterpret_cast<uintptr_t>(__builtin_frame_address(0 /* this frame */)); |
| 178 | if (((reinterpret_cast<uintptr_t>(ptr) ^ my_frame_address) & MIN_PAGE_MASK) == 0) { |
| 179 | sp_report_stack_pointer(); |
| 180 | } |
| 181 | } |
| 182 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | // --------------------------------------------------------------------------- |
| 184 | |
| 185 | class RefBase::weakref_impl : public RefBase::weakref_type |
| 186 | { |
| 187 | public: |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 188 | std::atomic<int32_t> mStrong; |
| 189 | std::atomic<int32_t> mWeak; |
| 190 | RefBase* const mBase; |
| 191 | std::atomic<int32_t> mFlags; |
Mathias Agopian | 9c8fa9e | 2011-06-15 20:42:47 -0700 | [diff] [blame] | 192 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | #if !DEBUG_REFS |
| 194 | |
Chih-Hung Hsieh | 1c563d9 | 2016-04-29 15:44:04 -0700 | [diff] [blame] | 195 | explicit weakref_impl(RefBase* base) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | : mStrong(INITIAL_STRONG_VALUE) |
| 197 | , mWeak(0) |
| 198 | , mBase(base) |
é™ˆå† æœ‰ | 0cbef72 | 2021-08-22 16:36:28 +0000 | [diff] [blame] | 199 | , mFlags(OBJECT_LIFETIME_STRONG) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | { |
| 201 | } |
| 202 | |
| 203 | void addStrongRef(const void* /*id*/) { } |
| 204 | void removeStrongRef(const void* /*id*/) { } |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 205 | void renameStrongRefId(const void* /*old_id*/, const void* /*new_id*/) { } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | void addWeakRef(const void* /*id*/) { } |
| 207 | void removeWeakRef(const void* /*id*/) { } |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 208 | void renameWeakRefId(const void* /*old_id*/, const void* /*new_id*/) { } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | void printRefs() const { } |
| 210 | void trackMe(bool, bool) { } |
| 211 | |
| 212 | #else |
| 213 | |
| 214 | weakref_impl(RefBase* base) |
| 215 | : mStrong(INITIAL_STRONG_VALUE) |
| 216 | , mWeak(0) |
| 217 | , mBase(base) |
é™ˆå† æœ‰ | 0cbef72 | 2021-08-22 16:36:28 +0000 | [diff] [blame] | 218 | , mFlags(OBJECT_LIFETIME_STRONG) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | , mStrongRefs(NULL) |
| 220 | , mWeakRefs(NULL) |
| 221 | , mTrackEnabled(!!DEBUG_REFS_ENABLED_BY_DEFAULT) |
| 222 | , mRetain(false) |
| 223 | { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | } |
Christopher Ferris | 0e69160 | 2020-10-14 14:13:58 -0700 | [diff] [blame] | 225 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | ~weakref_impl() |
| 227 | { |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 228 | bool dumpStack = false; |
| 229 | if (!mRetain && mStrongRefs != NULL) { |
| 230 | dumpStack = true; |
Steve Block | 1b781ab | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 231 | ALOGE("Strong references remain:"); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 232 | ref_entry* refs = mStrongRefs; |
| 233 | while (refs) { |
| 234 | char inc = refs->ref >= 0 ? '+' : '-'; |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 235 | ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref); |
Christopher Ferris | 0e69160 | 2020-10-14 14:13:58 -0700 | [diff] [blame] | 236 | #if DEBUG_REFS_CALLSTACK_ENABLED && CALLSTACK_ENABLED |
Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 237 | CallStack::logStack(LOG_TAG, refs->stack.get()); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 238 | #endif |
| 239 | refs = refs->next; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | if (!mRetain && mWeakRefs != NULL) { |
| 244 | dumpStack = true; |
Steve Block | 1b781ab | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 245 | ALOGE("Weak references remain!"); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 246 | ref_entry* refs = mWeakRefs; |
| 247 | while (refs) { |
| 248 | char inc = refs->ref >= 0 ? '+' : '-'; |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 249 | ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref); |
Christopher Ferris | 0e69160 | 2020-10-14 14:13:58 -0700 | [diff] [blame] | 250 | #if DEBUG_REFS_CALLSTACK_ENABLED && CALLSTACK_ENABLED |
Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 251 | CallStack::logStack(LOG_TAG, refs->stack.get()); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 252 | #endif |
| 253 | refs = refs->next; |
| 254 | } |
| 255 | } |
| 256 | if (dumpStack) { |
Steve Block | 1b781ab | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 257 | ALOGE("above errors at:"); |
Christopher Ferris | 0e69160 | 2020-10-14 14:13:58 -0700 | [diff] [blame] | 258 | #if CALLSTACK_ENABLED |
Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 259 | CallStack::logStack(LOG_TAG); |
Christopher Ferris | 0e69160 | 2020-10-14 14:13:58 -0700 | [diff] [blame] | 260 | #endif |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 261 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 264 | void addStrongRef(const void* id) { |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 265 | //ALOGD_IF(mTrackEnabled, |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 266 | // "addStrongRef: RefBase=%p, id=%p", mBase, id); |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 267 | addRef(&mStrongRefs, id, mStrong.load(std::memory_order_relaxed)); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 270 | void removeStrongRef(const void* id) { |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 271 | //ALOGD_IF(mTrackEnabled, |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 272 | // "removeStrongRef: RefBase=%p, id=%p", mBase, id); |
| 273 | if (!mRetain) { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | removeRef(&mStrongRefs, id); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 275 | } else { |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 276 | addRef(&mStrongRefs, id, -mStrong.load(std::memory_order_relaxed)); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 277 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 280 | void renameStrongRefId(const void* old_id, const void* new_id) { |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 281 | //ALOGD_IF(mTrackEnabled, |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 282 | // "renameStrongRefId: RefBase=%p, oid=%p, nid=%p", |
| 283 | // mBase, old_id, new_id); |
| 284 | renameRefsId(mStrongRefs, old_id, new_id); |
| 285 | } |
| 286 | |
| 287 | void addWeakRef(const void* id) { |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 288 | addRef(&mWeakRefs, id, mWeak.load(std::memory_order_relaxed)); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | } |
| 290 | |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 291 | void removeWeakRef(const void* id) { |
| 292 | if (!mRetain) { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 293 | removeRef(&mWeakRefs, id); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 294 | } else { |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 295 | addRef(&mWeakRefs, id, -mWeak.load(std::memory_order_relaxed)); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| 299 | void renameWeakRefId(const void* old_id, const void* new_id) { |
| 300 | renameRefsId(mWeakRefs, old_id, new_id); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | } |
| 302 | |
Christopher Ferris | 0e69160 | 2020-10-14 14:13:58 -0700 | [diff] [blame] | 303 | void trackMe(bool track, bool retain) { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | mTrackEnabled = track; |
| 305 | mRetain = retain; |
| 306 | } |
| 307 | |
| 308 | void printRefs() const |
| 309 | { |
| 310 | String8 text; |
| 311 | |
| 312 | { |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 313 | Mutex::Autolock _l(mMutex); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | char buf[128]; |
George Burgess IV | e7aa2b2 | 2016-03-02 14:02:55 -0800 | [diff] [blame] | 315 | snprintf(buf, sizeof(buf), |
| 316 | "Strong references on RefBase %p (weakref_type %p):\n", |
| 317 | mBase, this); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | text.append(buf); |
| 319 | printRefsLocked(&text, mStrongRefs); |
George Burgess IV | e7aa2b2 | 2016-03-02 14:02:55 -0800 | [diff] [blame] | 320 | snprintf(buf, sizeof(buf), |
| 321 | "Weak references on RefBase %p (weakref_type %p):\n", |
| 322 | mBase, this); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | text.append(buf); |
| 324 | printRefsLocked(&text, mWeakRefs); |
| 325 | } |
| 326 | |
| 327 | { |
| 328 | char name[100]; |
George Burgess IV | e7aa2b2 | 2016-03-02 14:02:55 -0800 | [diff] [blame] | 329 | snprintf(name, sizeof(name), DEBUG_REFS_CALLSTACK_PATH "/%p.stack", |
| 330 | this); |
Steven Moreland | b7412c8 | 2022-10-08 05:07:52 +0000 | [diff] [blame] | 331 | int rc = open(name, O_RDWR | O_CREAT | O_APPEND, 0644); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | if (rc >= 0) { |
Tomasz Wasilczyk | 18b7461 | 2023-08-10 23:29:50 +0000 | [diff] [blame^] | 333 | (void)write(rc, text.c_str(), text.length()); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | close(rc); |
Steven Moreland | b7412c8 | 2022-10-08 05:07:52 +0000 | [diff] [blame] | 335 | ALOGI("STACK TRACE for %p saved in %s", this, name); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | } |
Steve Block | 1b781ab | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 337 | else ALOGE("FAILED TO PRINT STACK TRACE for %p in %s: %s", this, |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | name, strerror(errno)); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | private: |
| 343 | struct ref_entry |
| 344 | { |
| 345 | ref_entry* next; |
| 346 | const void* id; |
Christopher Ferris | 0e69160 | 2020-10-14 14:13:58 -0700 | [diff] [blame] | 347 | #if DEBUG_REFS_CALLSTACK_ENABLED && CALLSTACK_ENABLED |
Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 348 | CallStack::CallStackUPtr stack; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | #endif |
| 350 | int32_t ref; |
| 351 | }; |
| 352 | |
| 353 | void addRef(ref_entry** refs, const void* id, int32_t mRef) |
| 354 | { |
| 355 | if (mTrackEnabled) { |
| 356 | AutoMutex _l(mMutex); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 357 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | ref_entry* ref = new ref_entry; |
| 359 | // Reference count at the time of the snapshot, but before the |
| 360 | // update. Positive value means we increment, negative--we |
| 361 | // decrement the reference count. |
| 362 | ref->ref = mRef; |
| 363 | ref->id = id; |
Christopher Ferris | 0e69160 | 2020-10-14 14:13:58 -0700 | [diff] [blame] | 364 | #if DEBUG_REFS_CALLSTACK_ENABLED && CALLSTACK_ENABLED |
Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 365 | ref->stack = CallStack::getCurrent(2); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | #endif |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | ref->next = *refs; |
| 368 | *refs = ref; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | void removeRef(ref_entry** refs, const void* id) |
| 373 | { |
| 374 | if (mTrackEnabled) { |
| 375 | AutoMutex _l(mMutex); |
Christopher Ferris | 0e69160 | 2020-10-14 14:13:58 -0700 | [diff] [blame] | 376 | |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 377 | ref_entry* const head = *refs; |
| 378 | ref_entry* ref = head; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | while (ref != NULL) { |
| 380 | if (ref->id == id) { |
| 381 | *refs = ref->next; |
| 382 | delete ref; |
| 383 | return; |
| 384 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | refs = &ref->next; |
| 386 | ref = *refs; |
| 387 | } |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 388 | |
Steve Block | 1b781ab | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 389 | ALOGE("RefBase: removing id %p on RefBase %p" |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 390 | "(weakref_type %p) that doesn't exist!", |
| 391 | id, mBase, this); |
| 392 | |
| 393 | ref = head; |
| 394 | while (ref) { |
| 395 | char inc = ref->ref >= 0 ? '+' : '-'; |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 396 | ALOGD("\t%c ID %p (ref %d):", inc, ref->id, ref->ref); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 397 | ref = ref->next; |
| 398 | } |
| 399 | |
Christopher Ferris | 0e69160 | 2020-10-14 14:13:58 -0700 | [diff] [blame] | 400 | #if CALLSTACK_ENABLED |
Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 401 | CallStack::logStack(LOG_TAG); |
Christopher Ferris | 0e69160 | 2020-10-14 14:13:58 -0700 | [diff] [blame] | 402 | #endif |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
| 406 | void renameRefsId(ref_entry* r, const void* old_id, const void* new_id) |
| 407 | { |
| 408 | if (mTrackEnabled) { |
| 409 | AutoMutex _l(mMutex); |
| 410 | ref_entry* ref = r; |
| 411 | while (ref != NULL) { |
| 412 | if (ref->id == old_id) { |
| 413 | ref->id = new_id; |
| 414 | } |
| 415 | ref = ref->next; |
| 416 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
| 420 | void printRefsLocked(String8* out, const ref_entry* refs) const |
| 421 | { |
| 422 | char buf[128]; |
| 423 | while (refs) { |
| 424 | char inc = refs->ref >= 0 ? '+' : '-'; |
George Burgess IV | e7aa2b2 | 2016-03-02 14:02:55 -0800 | [diff] [blame] | 425 | snprintf(buf, sizeof(buf), "\t%c ID %p (ref %d):\n", |
| 426 | inc, refs->id, refs->ref); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | out->append(buf); |
Christopher Ferris | 0e69160 | 2020-10-14 14:13:58 -0700 | [diff] [blame] | 428 | #if DEBUG_REFS_CALLSTACK_ENABLED && CALLSTACK_ENABLED |
Hans Boehm | 2a019ec | 2018-08-07 23:45:25 +0000 | [diff] [blame] | 429 | out->append(CallStack::stackToString("\t\t", refs->stack.get())); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | #else |
| 431 | out->append("\t\t(call stacks disabled)"); |
| 432 | #endif |
| 433 | refs = refs->next; |
| 434 | } |
| 435 | } |
| 436 | |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 437 | mutable Mutex mMutex; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 438 | ref_entry* mStrongRefs; |
| 439 | ref_entry* mWeakRefs; |
| 440 | |
| 441 | bool mTrackEnabled; |
| 442 | // Collect stack traces on addref and removeref, instead of deleting the stack references |
| 443 | // on removeref that match the address ones. |
| 444 | bool mRetain; |
| 445 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 446 | #endif |
| 447 | }; |
| 448 | |
| 449 | // --------------------------------------------------------------------------- |
| 450 | |
| 451 | void RefBase::incStrong(const void* id) const |
| 452 | { |
| 453 | weakref_impl* const refs = mRefs; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | refs->incWeak(id); |
Christopher Ferris | 0e69160 | 2020-10-14 14:13:58 -0700 | [diff] [blame] | 455 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | refs->addStrongRef(id); |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 457 | const int32_t c = refs->mStrong.fetch_add(1, std::memory_order_relaxed); |
Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 458 | ALOG_ASSERT(c > 0, "incStrong() called on %p after last strong ref", refs); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 459 | #if PRINT_REFS |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 460 | ALOGD("incStrong of %p from %p: cnt=%d\n", this, id, c); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | #endif |
| 462 | if (c != INITIAL_STRONG_VALUE) { |
| 463 | return; |
| 464 | } |
| 465 | |
Steven Moreland | c340a08 | 2022-07-26 17:09:50 +0000 | [diff] [blame] | 466 | check_not_on_stack(this); |
| 467 | |
Chih-Hung Hsieh | 122352d | 2017-10-02 15:20:07 -0700 | [diff] [blame] | 468 | int32_t old __unused = refs->mStrong.fetch_sub(INITIAL_STRONG_VALUE, std::memory_order_relaxed); |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 469 | // A decStrong() must still happen after us. |
| 470 | ALOG_ASSERT(old > INITIAL_STRONG_VALUE, "0x%x too small", old); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 471 | refs->mBase->onFirstRef(); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | } |
| 473 | |
Steven Moreland | da75cef | 2021-03-31 16:05:04 +0000 | [diff] [blame] | 474 | void RefBase::incStrongRequireStrong(const void* id) const { |
| 475 | weakref_impl* const refs = mRefs; |
| 476 | refs->incWeak(id); |
| 477 | |
| 478 | refs->addStrongRef(id); |
| 479 | const int32_t c = refs->mStrong.fetch_add(1, std::memory_order_relaxed); |
| 480 | |
| 481 | LOG_ALWAYS_FATAL_IF(c <= 0 || c == INITIAL_STRONG_VALUE, |
| 482 | "incStrongRequireStrong() called on %p which isn't already owned", refs); |
| 483 | #if PRINT_REFS |
| 484 | ALOGD("incStrong (requiring strong) of %p from %p: cnt=%d\n", this, id, c); |
| 485 | #endif |
| 486 | } |
| 487 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | void RefBase::decStrong(const void* id) const |
| 489 | { |
| 490 | weakref_impl* const refs = mRefs; |
| 491 | refs->removeStrongRef(id); |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 492 | const int32_t c = refs->mStrong.fetch_sub(1, std::memory_order_release); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | #if PRINT_REFS |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 494 | ALOGD("decStrong of %p from %p: cnt=%d\n", this, id, c); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | #endif |
Hans Boehm | 23c857e | 2016-08-02 18:39:30 -0700 | [diff] [blame] | 496 | LOG_ALWAYS_FATAL_IF(BAD_STRONG(c), "decStrong() called on %p too many times", |
| 497 | refs); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | if (c == 1) { |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 499 | std::atomic_thread_fence(std::memory_order_acquire); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 500 | refs->mBase->onLastStrongRef(id); |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 501 | int32_t flags = refs->mFlags.load(std::memory_order_relaxed); |
| 502 | if ((flags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) { |
Mathias Agopian | 9c8fa9e | 2011-06-15 20:42:47 -0700 | [diff] [blame] | 503 | delete this; |
Hans Boehm | 23c857e | 2016-08-02 18:39:30 -0700 | [diff] [blame] | 504 | // The destructor does not delete refs in this case. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | } |
| 506 | } |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 507 | // Note that even with only strong reference operations, the thread |
| 508 | // deallocating this may not be the same as the thread deallocating refs. |
| 509 | // That's OK: all accesses to this happen before its deletion here, |
| 510 | // and all accesses to refs happen before its deletion in the final decWeak. |
| 511 | // The destructor can safely access mRefs because either it's deleting |
| 512 | // mRefs itself, or it's running entirely before the final mWeak decrement. |
George Burgess IV | 6753bc4 | 2017-10-01 12:38:44 -0700 | [diff] [blame] | 513 | // |
| 514 | // Since we're doing atomic loads of `flags`, the static analyzer assumes |
| 515 | // they can change between `delete this;` and `refs->decWeak(id);`. This is |
| 516 | // not the case. The analyzer may become more okay with this patten when |
| 517 | // https://bugs.llvm.org/show_bug.cgi?id=34365 gets resolved. NOLINTNEXTLINE |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 518 | refs->decWeak(id); |
| 519 | } |
| 520 | |
| 521 | void RefBase::forceIncStrong(const void* id) const |
| 522 | { |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 523 | // Allows initial mStrong of 0 in addition to INITIAL_STRONG_VALUE. |
| 524 | // TODO: Better document assumptions. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 525 | weakref_impl* const refs = mRefs; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 526 | refs->incWeak(id); |
Christopher Ferris | 0e69160 | 2020-10-14 14:13:58 -0700 | [diff] [blame] | 527 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 528 | refs->addStrongRef(id); |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 529 | const int32_t c = refs->mStrong.fetch_add(1, std::memory_order_relaxed); |
Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 530 | ALOG_ASSERT(c >= 0, "forceIncStrong called on %p after ref count underflow", |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 531 | refs); |
| 532 | #if PRINT_REFS |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 533 | ALOGD("forceIncStrong of %p from %p: cnt=%d\n", this, id, c); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 534 | #endif |
| 535 | |
| 536 | switch (c) { |
| 537 | case INITIAL_STRONG_VALUE: |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 538 | refs->mStrong.fetch_sub(INITIAL_STRONG_VALUE, |
| 539 | std::memory_order_relaxed); |
Chih-Hung Hsieh | 502f486 | 2018-09-13 11:08:41 -0700 | [diff] [blame] | 540 | FALLTHROUGH_INTENDED; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 541 | case 0: |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 542 | refs->mBase->onFirstRef(); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
| 546 | int32_t RefBase::getStrongCount() const |
| 547 | { |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 548 | // Debugging only; No memory ordering guarantees. |
| 549 | return mRefs->mStrong.load(std::memory_order_relaxed); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 550 | } |
| 551 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | RefBase* RefBase::weakref_type::refBase() const |
| 553 | { |
| 554 | return static_cast<const weakref_impl*>(this)->mBase; |
| 555 | } |
| 556 | |
| 557 | void RefBase::weakref_type::incWeak(const void* id) |
| 558 | { |
| 559 | weakref_impl* const impl = static_cast<weakref_impl*>(this); |
| 560 | impl->addWeakRef(id); |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 561 | const int32_t c __unused = impl->mWeak.fetch_add(1, |
| 562 | std::memory_order_relaxed); |
Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 563 | ALOG_ASSERT(c >= 0, "incWeak called on %p after last weak ref", this); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 564 | } |
| 565 | |
Steven Moreland | da75cef | 2021-03-31 16:05:04 +0000 | [diff] [blame] | 566 | void RefBase::weakref_type::incWeakRequireWeak(const void* id) |
| 567 | { |
| 568 | weakref_impl* const impl = static_cast<weakref_impl*>(this); |
| 569 | impl->addWeakRef(id); |
| 570 | const int32_t c __unused = impl->mWeak.fetch_add(1, |
| 571 | std::memory_order_relaxed); |
| 572 | LOG_ALWAYS_FATAL_IF(c <= 0, "incWeakRequireWeak called on %p which has no weak refs", this); |
| 573 | } |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 574 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | void RefBase::weakref_type::decWeak(const void* id) |
| 576 | { |
| 577 | weakref_impl* const impl = static_cast<weakref_impl*>(this); |
| 578 | impl->removeWeakRef(id); |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 579 | const int32_t c = impl->mWeak.fetch_sub(1, std::memory_order_release); |
Hans Boehm | 23c857e | 2016-08-02 18:39:30 -0700 | [diff] [blame] | 580 | LOG_ALWAYS_FATAL_IF(BAD_WEAK(c), "decWeak called on %p too many times", |
| 581 | this); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | if (c != 1) return; |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 583 | atomic_thread_fence(std::memory_order_acquire); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 584 | |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 585 | int32_t flags = impl->mFlags.load(std::memory_order_relaxed); |
| 586 | if ((flags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) { |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 587 | // This is the regular lifetime case. The object is destroyed |
| 588 | // when the last strong reference goes away. Since weakref_impl |
Hans Boehm | 23c857e | 2016-08-02 18:39:30 -0700 | [diff] [blame] | 589 | // outlives the object, it is not destroyed in the dtor, and |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 590 | // we'll have to do it here. |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 591 | if (impl->mStrong.load(std::memory_order_relaxed) |
| 592 | == INITIAL_STRONG_VALUE) { |
Hans Boehm | 23c857e | 2016-08-02 18:39:30 -0700 | [diff] [blame] | 593 | // Decrementing a weak count to zero when object never had a strong |
| 594 | // reference. We assume it acquired a weak reference early, e.g. |
| 595 | // in the constructor, and will eventually be properly destroyed, |
| 596 | // usually via incrementing and decrementing the strong count. |
| 597 | // Thus we no longer do anything here. We log this case, since it |
| 598 | // seems to be extremely rare, and should not normally occur. We |
| 599 | // used to deallocate mBase here, so this may now indicate a leak. |
| 600 | ALOGW("RefBase: Object at %p lost last weak reference " |
| 601 | "before it had a strong reference", impl->mBase); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 602 | } else { |
Steve Block | b37fbe9 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 603 | // ALOGV("Freeing refs %p of old RefBase %p\n", this, impl->mBase); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 604 | delete impl; |
| 605 | } |
| 606 | } else { |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 607 | // This is the OBJECT_LIFETIME_WEAK case. The last weak-reference |
| 608 | // is gone, we can destroy the object. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 609 | impl->mBase->onLastWeakRef(id); |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 610 | delete impl->mBase; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | |
| 614 | bool RefBase::weakref_type::attemptIncStrong(const void* id) |
| 615 | { |
| 616 | incWeak(id); |
Christopher Ferris | 0e69160 | 2020-10-14 14:13:58 -0700 | [diff] [blame] | 617 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 618 | weakref_impl* const impl = static_cast<weakref_impl*>(this); |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 619 | int32_t curCount = impl->mStrong.load(std::memory_order_relaxed); |
Dianne Hackborn | a729ab1 | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 620 | |
| 621 | ALOG_ASSERT(curCount >= 0, |
| 622 | "attemptIncStrong called on %p after underflow", this); |
| 623 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 624 | while (curCount > 0 && curCount != INITIAL_STRONG_VALUE) { |
Dianne Hackborn | a729ab1 | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 625 | // we're in the easy/common case of promoting a weak-reference |
| 626 | // from an existing strong reference. |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 627 | if (impl->mStrong.compare_exchange_weak(curCount, curCount+1, |
| 628 | std::memory_order_relaxed)) { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | break; |
| 630 | } |
Dianne Hackborn | a729ab1 | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 631 | // the strong count has changed on us, we need to re-assert our |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 632 | // situation. curCount was updated by compare_exchange_weak. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | } |
Christopher Ferris | 0e69160 | 2020-10-14 14:13:58 -0700 | [diff] [blame] | 634 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 635 | if (curCount <= 0 || curCount == INITIAL_STRONG_VALUE) { |
Dianne Hackborn | a729ab1 | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 636 | // we're now in the harder case of either: |
| 637 | // - there never was a strong reference on us |
| 638 | // - or, all strong references have been released |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 639 | int32_t flags = impl->mFlags.load(std::memory_order_relaxed); |
| 640 | if ((flags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) { |
Dianne Hackborn | a729ab1 | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 641 | // this object has a "normal" life-time, i.e.: it gets destroyed |
| 642 | // when the last strong reference goes away |
| 643 | if (curCount <= 0) { |
| 644 | // the last strong-reference got released, the object cannot |
| 645 | // be revived. |
| 646 | decWeak(id); |
| 647 | return false; |
| 648 | } |
| 649 | |
| 650 | // here, curCount == INITIAL_STRONG_VALUE, which means |
| 651 | // there never was a strong-reference, so we can try to |
| 652 | // promote this object; we need to do that atomically. |
| 653 | while (curCount > 0) { |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 654 | if (impl->mStrong.compare_exchange_weak(curCount, curCount+1, |
| 655 | std::memory_order_relaxed)) { |
Dianne Hackborn | a729ab1 | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 656 | break; |
| 657 | } |
| 658 | // the strong count has changed on us, we need to re-assert our |
| 659 | // situation (e.g.: another thread has inc/decStrong'ed us) |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 660 | // curCount has been updated. |
Dianne Hackborn | a729ab1 | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | if (curCount <= 0) { |
| 664 | // promote() failed, some other thread destroyed us in the |
| 665 | // meantime (i.e.: strong count reached zero). |
| 666 | decWeak(id); |
| 667 | return false; |
| 668 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 669 | } else { |
Dianne Hackborn | a729ab1 | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 670 | // this object has an "extended" life-time, i.e.: it can be |
| 671 | // revived from a weak-reference only. |
| 672 | // Ask the object's implementation if it agrees to be revived |
| 673 | if (!impl->mBase->onIncStrongAttempted(FIRST_INC_STRONG, id)) { |
| 674 | // it didn't so give-up. |
| 675 | decWeak(id); |
| 676 | return false; |
| 677 | } |
| 678 | // grab a strong-reference, which is always safe due to the |
| 679 | // extended life-time. |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 680 | curCount = impl->mStrong.fetch_add(1, std::memory_order_relaxed); |
Hans Boehm | 7f27cbc | 2016-07-29 14:39:10 -0700 | [diff] [blame] | 681 | // If the strong reference count has already been incremented by |
| 682 | // someone else, the implementor of onIncStrongAttempted() is holding |
| 683 | // an unneeded reference. So call onLastStrongRef() here to remove it. |
| 684 | // (No, this is not pretty.) Note that we MUST NOT do this if we |
| 685 | // are in fact acquiring the first reference. |
| 686 | if (curCount != 0 && curCount != INITIAL_STRONG_VALUE) { |
| 687 | impl->mBase->onLastStrongRef(id); |
| 688 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 689 | } |
| 690 | } |
Christopher Ferris | 0e69160 | 2020-10-14 14:13:58 -0700 | [diff] [blame] | 691 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | impl->addStrongRef(id); |
| 693 | |
| 694 | #if PRINT_REFS |
Steve Block | eb09533 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 695 | ALOGD("attemptIncStrong of %p from %p: cnt=%d\n", this, id, curCount); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 696 | #endif |
| 697 | |
Hans Boehm | 7f27cbc | 2016-07-29 14:39:10 -0700 | [diff] [blame] | 698 | // curCount is the value of mStrong before we incremented it. |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 699 | // Now we need to fix-up the count if it was INITIAL_STRONG_VALUE. |
| 700 | // This must be done safely, i.e.: handle the case where several threads |
Dianne Hackborn | a729ab1 | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 701 | // were here in attemptIncStrong(). |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 702 | // curCount > INITIAL_STRONG_VALUE is OK, and can happen if we're doing |
| 703 | // this in the middle of another incStrong. The subtraction is handled |
| 704 | // by the thread that started with INITIAL_STRONG_VALUE. |
| 705 | if (curCount == INITIAL_STRONG_VALUE) { |
| 706 | impl->mStrong.fetch_sub(INITIAL_STRONG_VALUE, |
| 707 | std::memory_order_relaxed); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 708 | } |
Dianne Hackborn | a729ab1 | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 709 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 710 | return true; |
| 711 | } |
| 712 | |
| 713 | bool RefBase::weakref_type::attemptIncWeak(const void* id) |
| 714 | { |
| 715 | weakref_impl* const impl = static_cast<weakref_impl*>(this); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 716 | |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 717 | int32_t curCount = impl->mWeak.load(std::memory_order_relaxed); |
Steve Block | ae07445 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 718 | ALOG_ASSERT(curCount >= 0, "attemptIncWeak called on %p after underflow", |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 719 | this); |
| 720 | while (curCount > 0) { |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 721 | if (impl->mWeak.compare_exchange_weak(curCount, curCount+1, |
| 722 | std::memory_order_relaxed)) { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 723 | break; |
| 724 | } |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 725 | // curCount has been updated. |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | if (curCount > 0) { |
| 729 | impl->addWeakRef(id); |
| 730 | } |
| 731 | |
| 732 | return curCount > 0; |
| 733 | } |
| 734 | |
| 735 | int32_t RefBase::weakref_type::getWeakCount() const |
| 736 | { |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 737 | // Debug only! |
| 738 | return static_cast<const weakref_impl*>(this)->mWeak |
| 739 | .load(std::memory_order_relaxed); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | void RefBase::weakref_type::printRefs() const |
| 743 | { |
| 744 | static_cast<const weakref_impl*>(this)->printRefs(); |
| 745 | } |
| 746 | |
| 747 | void RefBase::weakref_type::trackMe(bool enable, bool retain) |
| 748 | { |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 749 | static_cast<weakref_impl*>(this)->trackMe(enable, retain); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | RefBase::weakref_type* RefBase::createWeak(const void* id) const |
| 753 | { |
| 754 | mRefs->incWeak(id); |
| 755 | return mRefs; |
| 756 | } |
| 757 | |
| 758 | RefBase::weakref_type* RefBase::getWeakRefs() const |
| 759 | { |
| 760 | return mRefs; |
| 761 | } |
| 762 | |
| 763 | RefBase::RefBase() |
| 764 | : mRefs(new weakref_impl(this)) |
| 765 | { |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | RefBase::~RefBase() |
| 769 | { |
Hans Boehm | 23c857e | 2016-08-02 18:39:30 -0700 | [diff] [blame] | 770 | int32_t flags = mRefs->mFlags.load(std::memory_order_relaxed); |
| 771 | // Life-time of this object is extended to WEAK, in |
| 772 | // which case weakref_impl doesn't out-live the object and we |
| 773 | // can free it now. |
| 774 | if ((flags & OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_WEAK) { |
| 775 | // It's possible that the weak count is not 0 if the object |
| 776 | // re-acquired a weak reference in its destructor |
| 777 | if (mRefs->mWeak.load(std::memory_order_relaxed) == 0) { |
| 778 | delete mRefs; |
| 779 | } |
Steven Moreland | 483a2de | 2022-07-26 17:57:39 +0000 | [diff] [blame] | 780 | } else { |
| 781 | int32_t strongs = mRefs->mStrong.load(std::memory_order_relaxed); |
Steven Moreland | f164de8 | 2022-03-11 02:28:57 +0000 | [diff] [blame] | 782 | |
Steven Moreland | 483a2de | 2022-07-26 17:57:39 +0000 | [diff] [blame] | 783 | if (strongs == INITIAL_STRONG_VALUE) { |
| 784 | // We never acquired a strong reference on this object. |
| 785 | |
| 786 | // It would be nice to make this fatal, but many places use RefBase on the stack. |
| 787 | // However, this is dangerous because it's also common for code to use the |
| 788 | // sp<T>(T*) constructor, assuming that if the object is around, it is already |
| 789 | // owned by an sp<>. |
| 790 | ALOGW("RefBase: Explicit destruction, weak count = %d (in %p). Use sp<> to manage this " |
| 791 | "object.", |
| 792 | mRefs->mWeak.load(), this); |
Christopher Ferris | 0e69160 | 2020-10-14 14:13:58 -0700 | [diff] [blame] | 793 | |
| 794 | #if CALLSTACK_ENABLED |
Steven Moreland | 483a2de | 2022-07-26 17:57:39 +0000 | [diff] [blame] | 795 | CallStack::logStack(LOG_TAG); |
Christopher Ferris | 0e69160 | 2020-10-14 14:13:58 -0700 | [diff] [blame] | 796 | #endif |
Steven Moreland | 483a2de | 2022-07-26 17:57:39 +0000 | [diff] [blame] | 797 | } else if (strongs != 0) { |
| 798 | LOG_ALWAYS_FATAL("RefBase: object %p with strong count %d deleted. Double owned?", this, |
| 799 | strongs); |
| 800 | } |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 801 | } |
Hans Boehm | 23c857e | 2016-08-02 18:39:30 -0700 | [diff] [blame] | 802 | // For debugging purposes, clear mRefs. Ineffective against outstanding wp's. |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 803 | const_cast<weakref_impl*&>(mRefs) = nullptr; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | void RefBase::extendObjectLifetime(int32_t mode) |
| 807 | { |
Steven Moreland | d086fe5 | 2022-07-26 22:11:13 +0000 | [diff] [blame] | 808 | check_not_on_stack(this); |
| 809 | |
Hans Boehm | e263e6c | 2016-05-11 18:15:12 -0700 | [diff] [blame] | 810 | // Must be happens-before ordered with respect to construction or any |
| 811 | // operation that could destroy the object. |
| 812 | mRefs->mFlags.fetch_or(mode, std::memory_order_relaxed); |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | void RefBase::onFirstRef() |
| 816 | { |
| 817 | } |
| 818 | |
| 819 | void RefBase::onLastStrongRef(const void* /*id*/) |
| 820 | { |
| 821 | } |
| 822 | |
Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 823 | bool RefBase::onIncStrongAttempted(uint32_t flags, const void* /*id*/) |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 824 | { |
| 825 | return (flags&FIRST_INC_STRONG) ? true : false; |
| 826 | } |
| 827 | |
| 828 | void RefBase::onLastWeakRef(const void* /*id*/) |
| 829 | { |
| 830 | } |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 831 | |
| 832 | // --------------------------------------------------------------------------- |
| 833 | |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 834 | #if DEBUG_REFS |
Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 835 | void RefBase::renameRefs(size_t n, const ReferenceRenamer& renamer) { |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 836 | for (size_t i=0 ; i<n ; i++) { |
Mathias Agopian | 6cd548c | 2013-03-18 22:27:41 -0700 | [diff] [blame] | 837 | renamer(i); |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 838 | } |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 839 | } |
Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 840 | #else |
| 841 | void RefBase::renameRefs(size_t /*n*/, const ReferenceRenamer& /*renamer*/) { } |
| 842 | #endif |
Mathias Agopian | ad09965 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 843 | |
Mathias Agopian | 6cd548c | 2013-03-18 22:27:41 -0700 | [diff] [blame] | 844 | void RefBase::renameRefId(weakref_type* ref, |
| 845 | const void* old_id, const void* new_id) { |
| 846 | weakref_impl* const impl = static_cast<weakref_impl*>(ref); |
| 847 | impl->renameStrongRefId(old_id, new_id); |
| 848 | impl->renameWeakRefId(old_id, new_id); |
| 849 | } |
| 850 | |
| 851 | void RefBase::renameRefId(RefBase* ref, |
| 852 | const void* old_id, const void* new_id) { |
| 853 | ref->mRefs->renameStrongRefId(old_id, new_id); |
| 854 | ref->mRefs->renameWeakRefId(old_id, new_id); |
| 855 | } |
| 856 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 857 | }; // namespace android |