The Android Open Source Project | edbf3b6 | 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 | f795852 | 2013-03-11 21:27:16 -0700 | [diff] [blame] | 18 | #define LOG_NDEBUG 0 |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | |
| 20 | #include <utils/RefBase.h> |
| 21 | |
| 22 | #include <utils/Atomic.h> |
| 23 | #include <utils/CallStack.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | #include <utils/Log.h> |
| 25 | #include <utils/threads.h> |
Mathias Agopian | a08ef49 | 2011-02-16 15:23:08 -0800 | [diff] [blame] | 26 | #include <utils/TextOutput.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | |
| 28 | #include <stdlib.h> |
| 29 | #include <stdio.h> |
| 30 | #include <typeinfo> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/stat.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <unistd.h> |
| 35 | |
| 36 | // compile with refcounting debugging enabled |
| 37 | #define DEBUG_REFS 0 |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 38 | #define DEBUG_REFS_FATAL_SANITY_CHECKS 0 |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | #define DEBUG_REFS_ENABLED_BY_DEFAULT 1 |
| 40 | #define DEBUG_REFS_CALLSTACK_ENABLED 1 |
| 41 | |
| 42 | // log all reference counting operations |
| 43 | #define PRINT_REFS 0 |
| 44 | |
| 45 | // --------------------------------------------------------------------------- |
| 46 | |
| 47 | namespace android { |
| 48 | |
| 49 | #define INITIAL_STRONG_VALUE (1<<28) |
| 50 | |
| 51 | // --------------------------------------------------------------------------- |
| 52 | |
| 53 | class RefBase::weakref_impl : public RefBase::weakref_type |
| 54 | { |
| 55 | public: |
| 56 | volatile int32_t mStrong; |
| 57 | volatile int32_t mWeak; |
| 58 | RefBase* const mBase; |
| 59 | volatile int32_t mFlags; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | |
| 61 | #if !DEBUG_REFS |
| 62 | |
| 63 | weakref_impl(RefBase* base) |
| 64 | : mStrong(INITIAL_STRONG_VALUE) |
| 65 | , mWeak(0) |
| 66 | , mBase(base) |
| 67 | , mFlags(0) |
| 68 | { |
| 69 | } |
| 70 | |
| 71 | void addStrongRef(const void* /*id*/) { } |
| 72 | void removeStrongRef(const void* /*id*/) { } |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 73 | void renameStrongRefId(const void* /*old_id*/, const void* /*new_id*/) { } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | void addWeakRef(const void* /*id*/) { } |
| 75 | void removeWeakRef(const void* /*id*/) { } |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 76 | void renameWeakRefId(const void* /*old_id*/, const void* /*new_id*/) { } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | void printRefs() const { } |
| 78 | void trackMe(bool, bool) { } |
| 79 | |
| 80 | #else |
| 81 | |
| 82 | weakref_impl(RefBase* base) |
| 83 | : mStrong(INITIAL_STRONG_VALUE) |
| 84 | , mWeak(0) |
| 85 | , mBase(base) |
| 86 | , mFlags(0) |
| 87 | , mStrongRefs(NULL) |
| 88 | , mWeakRefs(NULL) |
| 89 | , mTrackEnabled(!!DEBUG_REFS_ENABLED_BY_DEFAULT) |
| 90 | , mRetain(false) |
| 91 | { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | ~weakref_impl() |
| 95 | { |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 96 | bool dumpStack = false; |
| 97 | if (!mRetain && mStrongRefs != NULL) { |
| 98 | dumpStack = true; |
| 99 | #if DEBUG_REFS_FATAL_SANITY_CHECKS |
| 100 | LOG_ALWAYS_FATAL("Strong references remain!"); |
| 101 | #else |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 102 | ALOGE("Strong references remain:"); |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 103 | #endif |
Mathias Agopian | 32bebb0 | 2011-02-25 16:11:44 -0800 | [diff] [blame] | 104 | ref_entry* refs = mStrongRefs; |
| 105 | while (refs) { |
| 106 | char inc = refs->ref >= 0 ? '+' : '-'; |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 107 | ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref); |
Mathias Agopian | 32bebb0 | 2011-02-25 16:11:44 -0800 | [diff] [blame] | 108 | #if DEBUG_REFS_CALLSTACK_ENABLED |
| 109 | refs->stack.dump(); |
Mathias Agopian | cbe5278 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 110 | #endif |
Mathias Agopian | 32bebb0 | 2011-02-25 16:11:44 -0800 | [diff] [blame] | 111 | refs = refs->next; |
| 112 | } |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | if (!mRetain && mWeakRefs != NULL) { |
| 116 | dumpStack = true; |
| 117 | #if DEBUG_REFS_FATAL_SANITY_CHECKS |
Mathias Agopian | 32bebb0 | 2011-02-25 16:11:44 -0800 | [diff] [blame] | 118 | LOG_ALWAYS_FATAL("Weak references remain:"); |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 119 | #else |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 120 | ALOGE("Weak references remain!"); |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 121 | #endif |
Mathias Agopian | 32bebb0 | 2011-02-25 16:11:44 -0800 | [diff] [blame] | 122 | ref_entry* refs = mWeakRefs; |
| 123 | while (refs) { |
| 124 | char inc = refs->ref >= 0 ? '+' : '-'; |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 125 | ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref); |
Mathias Agopian | 32bebb0 | 2011-02-25 16:11:44 -0800 | [diff] [blame] | 126 | #if DEBUG_REFS_CALLSTACK_ENABLED |
| 127 | refs->stack.dump(); |
Mathias Agopian | cbe5278 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 128 | #endif |
Mathias Agopian | 32bebb0 | 2011-02-25 16:11:44 -0800 | [diff] [blame] | 129 | refs = refs->next; |
| 130 | } |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 131 | } |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 132 | if (dumpStack) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 133 | ALOGE("above errors at:"); |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 134 | CallStack stack; |
| 135 | stack.update(); |
| 136 | stack.dump(); |
| 137 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 140 | void addStrongRef(const void* id) { |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 141 | //ALOGD_IF(mTrackEnabled, |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 142 | // "addStrongRef: RefBase=%p, id=%p", mBase, id); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | addRef(&mStrongRefs, id, mStrong); |
| 144 | } |
| 145 | |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 146 | void removeStrongRef(const void* id) { |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 147 | //ALOGD_IF(mTrackEnabled, |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 148 | // "removeStrongRef: RefBase=%p, id=%p", mBase, id); |
| 149 | if (!mRetain) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | removeRef(&mStrongRefs, id); |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 151 | } else { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | addRef(&mStrongRefs, id, -mStrong); |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 153 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 156 | void renameStrongRefId(const void* old_id, const void* new_id) { |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 157 | //ALOGD_IF(mTrackEnabled, |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 158 | // "renameStrongRefId: RefBase=%p, oid=%p, nid=%p", |
| 159 | // mBase, old_id, new_id); |
| 160 | renameRefsId(mStrongRefs, old_id, new_id); |
| 161 | } |
| 162 | |
| 163 | void addWeakRef(const void* id) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | addRef(&mWeakRefs, id, mWeak); |
| 165 | } |
| 166 | |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 167 | void removeWeakRef(const void* id) { |
| 168 | if (!mRetain) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | removeRef(&mWeakRefs, id); |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 170 | } else { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | addRef(&mWeakRefs, id, -mWeak); |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | void renameWeakRefId(const void* old_id, const void* new_id) { |
| 176 | renameRefsId(mWeakRefs, old_id, new_id); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void trackMe(bool track, bool retain) |
| 180 | { |
| 181 | mTrackEnabled = track; |
| 182 | mRetain = retain; |
| 183 | } |
| 184 | |
| 185 | void printRefs() const |
| 186 | { |
| 187 | String8 text; |
| 188 | |
| 189 | { |
Mathias Agopian | cbe5278 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 190 | Mutex::Autolock _l(mMutex); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | char buf[128]; |
| 192 | sprintf(buf, "Strong references on RefBase %p (weakref_type %p):\n", mBase, this); |
| 193 | text.append(buf); |
| 194 | printRefsLocked(&text, mStrongRefs); |
| 195 | sprintf(buf, "Weak references on RefBase %p (weakref_type %p):\n", mBase, this); |
| 196 | text.append(buf); |
| 197 | printRefsLocked(&text, mWeakRefs); |
| 198 | } |
| 199 | |
| 200 | { |
| 201 | char name[100]; |
| 202 | snprintf(name, 100, "/data/%p.stack", this); |
Mathias Agopian | dbf146f | 2013-03-06 17:51:15 -0800 | [diff] [blame] | 203 | int rc = open(name, O_RDWR | O_CREAT | O_APPEND, 644); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | if (rc >= 0) { |
| 205 | write(rc, text.string(), text.length()); |
| 206 | close(rc); |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 207 | ALOGD("STACK TRACE for %p saved in %s", this, name); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | } |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 209 | else ALOGE("FAILED TO PRINT STACK TRACE for %p in %s: %s", this, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | name, strerror(errno)); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | private: |
| 215 | struct ref_entry |
| 216 | { |
| 217 | ref_entry* next; |
| 218 | const void* id; |
| 219 | #if DEBUG_REFS_CALLSTACK_ENABLED |
| 220 | CallStack stack; |
| 221 | #endif |
| 222 | int32_t ref; |
| 223 | }; |
| 224 | |
| 225 | void addRef(ref_entry** refs, const void* id, int32_t mRef) |
| 226 | { |
| 227 | if (mTrackEnabled) { |
| 228 | AutoMutex _l(mMutex); |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 229 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | ref_entry* ref = new ref_entry; |
| 231 | // Reference count at the time of the snapshot, but before the |
| 232 | // update. Positive value means we increment, negative--we |
| 233 | // decrement the reference count. |
| 234 | ref->ref = mRef; |
| 235 | ref->id = id; |
| 236 | #if DEBUG_REFS_CALLSTACK_ENABLED |
| 237 | ref->stack.update(2); |
| 238 | #endif |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | ref->next = *refs; |
| 240 | *refs = ref; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | void removeRef(ref_entry** refs, const void* id) |
| 245 | { |
| 246 | if (mTrackEnabled) { |
| 247 | AutoMutex _l(mMutex); |
| 248 | |
Mathias Agopian | 32bebb0 | 2011-02-25 16:11:44 -0800 | [diff] [blame] | 249 | ref_entry* const head = *refs; |
| 250 | ref_entry* ref = head; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | while (ref != NULL) { |
| 252 | if (ref->id == id) { |
| 253 | *refs = ref->next; |
| 254 | delete ref; |
| 255 | return; |
| 256 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | refs = &ref->next; |
| 258 | ref = *refs; |
| 259 | } |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 260 | |
| 261 | #if DEBUG_REFS_FATAL_SANITY_CHECKS |
| 262 | LOG_ALWAYS_FATAL("RefBase: removing id %p on RefBase %p" |
| 263 | "(weakref_type %p) that doesn't exist!", |
| 264 | id, mBase, this); |
| 265 | #endif |
| 266 | |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 267 | ALOGE("RefBase: removing id %p on RefBase %p" |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 268 | "(weakref_type %p) that doesn't exist!", |
| 269 | id, mBase, this); |
| 270 | |
Mathias Agopian | 32bebb0 | 2011-02-25 16:11:44 -0800 | [diff] [blame] | 271 | ref = head; |
| 272 | while (ref) { |
| 273 | char inc = ref->ref >= 0 ? '+' : '-'; |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 274 | ALOGD("\t%c ID %p (ref %d):", inc, ref->id, ref->ref); |
Mathias Agopian | 32bebb0 | 2011-02-25 16:11:44 -0800 | [diff] [blame] | 275 | ref = ref->next; |
| 276 | } |
| 277 | |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 278 | CallStack stack; |
| 279 | stack.update(); |
| 280 | stack.dump(); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | void renameRefsId(ref_entry* r, const void* old_id, const void* new_id) |
| 285 | { |
| 286 | if (mTrackEnabled) { |
| 287 | AutoMutex _l(mMutex); |
| 288 | ref_entry* ref = r; |
| 289 | while (ref != NULL) { |
| 290 | if (ref->id == old_id) { |
| 291 | ref->id = new_id; |
| 292 | } |
| 293 | ref = ref->next; |
| 294 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
| 298 | void printRefsLocked(String8* out, const ref_entry* refs) const |
| 299 | { |
| 300 | char buf[128]; |
| 301 | while (refs) { |
| 302 | char inc = refs->ref >= 0 ? '+' : '-'; |
| 303 | sprintf(buf, "\t%c ID %p (ref %d):\n", |
| 304 | inc, refs->id, refs->ref); |
| 305 | out->append(buf); |
| 306 | #if DEBUG_REFS_CALLSTACK_ENABLED |
| 307 | out->append(refs->stack.toString("\t\t")); |
| 308 | #else |
| 309 | out->append("\t\t(call stacks disabled)"); |
| 310 | #endif |
| 311 | refs = refs->next; |
| 312 | } |
| 313 | } |
| 314 | |
Mathias Agopian | cbe5278 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 315 | mutable Mutex mMutex; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 316 | ref_entry* mStrongRefs; |
| 317 | ref_entry* mWeakRefs; |
| 318 | |
| 319 | bool mTrackEnabled; |
| 320 | // Collect stack traces on addref and removeref, instead of deleting the stack references |
| 321 | // on removeref that match the address ones. |
| 322 | bool mRetain; |
| 323 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | #endif |
| 325 | }; |
| 326 | |
| 327 | // --------------------------------------------------------------------------- |
| 328 | |
| 329 | void RefBase::incStrong(const void* id) const |
| 330 | { |
| 331 | weakref_impl* const refs = mRefs; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | refs->incWeak(id); |
| 333 | |
| 334 | refs->addStrongRef(id); |
| 335 | const int32_t c = android_atomic_inc(&refs->mStrong); |
Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 336 | ALOG_ASSERT(c > 0, "incStrong() called on %p after last strong ref", refs); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | #if PRINT_REFS |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 338 | ALOGD("incStrong of %p from %p: cnt=%d\n", this, id, c); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | #endif |
| 340 | if (c != INITIAL_STRONG_VALUE) { |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | android_atomic_add(-INITIAL_STRONG_VALUE, &refs->mStrong); |
Mathias Agopian | cbe5278 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 345 | refs->mBase->onFirstRef(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | void RefBase::decStrong(const void* id) const |
| 349 | { |
| 350 | weakref_impl* const refs = mRefs; |
| 351 | refs->removeStrongRef(id); |
| 352 | const int32_t c = android_atomic_dec(&refs->mStrong); |
| 353 | #if PRINT_REFS |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 354 | ALOGD("decStrong of %p from %p: cnt=%d\n", this, id, c); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | #endif |
Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 356 | ALOG_ASSERT(c >= 1, "decStrong() called on %p too many times", refs); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | if (c == 1) { |
Mathias Agopian | cbe5278 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 358 | refs->mBase->onLastStrongRef(id); |
| 359 | if ((refs->mFlags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) { |
| 360 | delete this; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | } |
| 362 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | refs->decWeak(id); |
| 364 | } |
| 365 | |
| 366 | void RefBase::forceIncStrong(const void* id) const |
| 367 | { |
| 368 | weakref_impl* const refs = mRefs; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | refs->incWeak(id); |
| 370 | |
| 371 | refs->addStrongRef(id); |
| 372 | const int32_t c = android_atomic_inc(&refs->mStrong); |
Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 373 | ALOG_ASSERT(c >= 0, "forceIncStrong called on %p after ref count underflow", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | refs); |
| 375 | #if PRINT_REFS |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 376 | ALOGD("forceIncStrong of %p from %p: cnt=%d\n", this, id, c); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | #endif |
| 378 | |
| 379 | switch (c) { |
| 380 | case INITIAL_STRONG_VALUE: |
| 381 | android_atomic_add(-INITIAL_STRONG_VALUE, &refs->mStrong); |
| 382 | // fall through... |
| 383 | case 0: |
Mathias Agopian | cbe5278 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 384 | refs->mBase->onFirstRef(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
| 388 | int32_t RefBase::getStrongCount() const |
| 389 | { |
| 390 | return mRefs->mStrong; |
| 391 | } |
| 392 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | RefBase* RefBase::weakref_type::refBase() const |
| 394 | { |
| 395 | return static_cast<const weakref_impl*>(this)->mBase; |
| 396 | } |
| 397 | |
| 398 | void RefBase::weakref_type::incWeak(const void* id) |
| 399 | { |
| 400 | weakref_impl* const impl = static_cast<weakref_impl*>(this); |
| 401 | impl->addWeakRef(id); |
| 402 | const int32_t c = android_atomic_inc(&impl->mWeak); |
Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 403 | ALOG_ASSERT(c >= 0, "incWeak called on %p after last weak ref", this); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Mathias Agopian | cbe5278 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 406 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | void RefBase::weakref_type::decWeak(const void* id) |
| 408 | { |
| 409 | weakref_impl* const impl = static_cast<weakref_impl*>(this); |
| 410 | impl->removeWeakRef(id); |
| 411 | const int32_t c = android_atomic_dec(&impl->mWeak); |
Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 412 | ALOG_ASSERT(c >= 1, "decWeak called on %p too many times", this); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 413 | if (c != 1) return; |
Mathias Agopian | cbe5278 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 414 | |
| 415 | if ((impl->mFlags&OBJECT_LIFETIME_WEAK) == OBJECT_LIFETIME_STRONG) { |
| 416 | // This is the regular lifetime case. The object is destroyed |
| 417 | // when the last strong reference goes away. Since weakref_impl |
| 418 | // outlive the object, it is not destroyed in the dtor, and |
| 419 | // we'll have to do it here. |
Mathias Agopian | 3b0a7a7 | 2011-06-08 16:05:30 -0700 | [diff] [blame] | 420 | if (impl->mStrong == INITIAL_STRONG_VALUE) { |
Mathias Agopian | cbe5278 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 421 | // Special case: we never had a strong reference, so we need to |
| 422 | // destroy the object now. |
| 423 | delete impl->mBase; |
Mathias Agopian | 3b0a7a7 | 2011-06-08 16:05:30 -0700 | [diff] [blame] | 424 | } else { |
Steve Block | 6807e59 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 425 | // ALOGV("Freeing refs %p of old RefBase %p\n", this, impl->mBase); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | delete impl; |
| 427 | } |
| 428 | } else { |
Mathias Agopian | cbe5278 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 429 | // less common case: lifetime is OBJECT_LIFETIME_{WEAK|FOREVER} |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | impl->mBase->onLastWeakRef(id); |
Mathias Agopian | cbe5278 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 431 | if ((impl->mFlags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_WEAK) { |
| 432 | // this is the OBJECT_LIFETIME_WEAK case. The last weak-reference |
| 433 | // is gone, we can destroy the object. |
| 434 | delete impl->mBase; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | bool RefBase::weakref_type::attemptIncStrong(const void* id) |
| 440 | { |
| 441 | incWeak(id); |
| 442 | |
| 443 | weakref_impl* const impl = static_cast<weakref_impl*>(this); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | int32_t curCount = impl->mStrong; |
Dianne Hackborn | 1791eef | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 445 | |
| 446 | ALOG_ASSERT(curCount >= 0, |
| 447 | "attemptIncStrong called on %p after underflow", this); |
| 448 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 449 | while (curCount > 0 && curCount != INITIAL_STRONG_VALUE) { |
Dianne Hackborn | 1791eef | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 450 | // we're in the easy/common case of promoting a weak-reference |
| 451 | // from an existing strong reference. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 | if (android_atomic_cmpxchg(curCount, curCount+1, &impl->mStrong) == 0) { |
| 453 | break; |
| 454 | } |
Dianne Hackborn | 1791eef | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 455 | // the strong count has changed on us, we need to re-assert our |
| 456 | // situation. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 457 | curCount = impl->mStrong; |
| 458 | } |
| 459 | |
| 460 | if (curCount <= 0 || curCount == INITIAL_STRONG_VALUE) { |
Dianne Hackborn | 1791eef | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 461 | // we're now in the harder case of either: |
| 462 | // - there never was a strong reference on us |
| 463 | // - or, all strong references have been released |
| 464 | if ((impl->mFlags&OBJECT_LIFETIME_WEAK) == OBJECT_LIFETIME_STRONG) { |
| 465 | // this object has a "normal" life-time, i.e.: it gets destroyed |
| 466 | // when the last strong reference goes away |
| 467 | if (curCount <= 0) { |
| 468 | // the last strong-reference got released, the object cannot |
| 469 | // be revived. |
| 470 | decWeak(id); |
| 471 | return false; |
| 472 | } |
| 473 | |
| 474 | // here, curCount == INITIAL_STRONG_VALUE, which means |
| 475 | // there never was a strong-reference, so we can try to |
| 476 | // promote this object; we need to do that atomically. |
| 477 | while (curCount > 0) { |
| 478 | if (android_atomic_cmpxchg(curCount, curCount + 1, |
| 479 | &impl->mStrong) == 0) { |
| 480 | break; |
| 481 | } |
| 482 | // the strong count has changed on us, we need to re-assert our |
| 483 | // situation (e.g.: another thread has inc/decStrong'ed us) |
| 484 | curCount = impl->mStrong; |
| 485 | } |
| 486 | |
| 487 | if (curCount <= 0) { |
| 488 | // promote() failed, some other thread destroyed us in the |
| 489 | // meantime (i.e.: strong count reached zero). |
| 490 | decWeak(id); |
| 491 | return false; |
| 492 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | } else { |
Dianne Hackborn | 1791eef | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 494 | // this object has an "extended" life-time, i.e.: it can be |
| 495 | // revived from a weak-reference only. |
| 496 | // Ask the object's implementation if it agrees to be revived |
| 497 | if (!impl->mBase->onIncStrongAttempted(FIRST_INC_STRONG, id)) { |
| 498 | // it didn't so give-up. |
| 499 | decWeak(id); |
| 500 | return false; |
| 501 | } |
| 502 | // grab a strong-reference, which is always safe due to the |
| 503 | // extended life-time. |
| 504 | curCount = android_atomic_inc(&impl->mStrong); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | |
| 507 | // If the strong reference count has already been incremented by |
| 508 | // someone else, the implementor of onIncStrongAttempted() is holding |
| 509 | // an unneeded reference. So call onLastStrongRef() here to remove it. |
| 510 | // (No, this is not pretty.) Note that we MUST NOT do this if we |
| 511 | // are in fact acquiring the first reference. |
| 512 | if (curCount > 0 && curCount < INITIAL_STRONG_VALUE) { |
| 513 | impl->mBase->onLastStrongRef(id); |
| 514 | } |
| 515 | } |
| 516 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | impl->addStrongRef(id); |
| 518 | |
| 519 | #if PRINT_REFS |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 520 | ALOGD("attemptIncStrong of %p from %p: cnt=%d\n", this, id, curCount); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 521 | #endif |
| 522 | |
Dianne Hackborn | 1791eef | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 523 | // now we need to fix-up the count if it was INITIAL_STRONG_VALUE |
| 524 | // this must be done safely, i.e.: handle the case where several threads |
| 525 | // were here in attemptIncStrong(). |
| 526 | curCount = impl->mStrong; |
| 527 | while (curCount >= INITIAL_STRONG_VALUE) { |
| 528 | ALOG_ASSERT(curCount > INITIAL_STRONG_VALUE, |
| 529 | "attemptIncStrong in %p underflowed to INITIAL_STRONG_VALUE", |
| 530 | this); |
| 531 | if (android_atomic_cmpxchg(curCount, curCount-INITIAL_STRONG_VALUE, |
| 532 | &impl->mStrong) == 0) { |
| 533 | break; |
| 534 | } |
| 535 | // the strong-count changed on us, we need to re-assert the situation, |
| 536 | // for e.g.: it's possible the fix-up happened in another thread. |
| 537 | curCount = impl->mStrong; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 538 | } |
Dianne Hackborn | 1791eef | 2013-03-14 15:26:30 -0700 | [diff] [blame] | 539 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | return true; |
| 541 | } |
| 542 | |
| 543 | bool RefBase::weakref_type::attemptIncWeak(const void* id) |
| 544 | { |
| 545 | weakref_impl* const impl = static_cast<weakref_impl*>(this); |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 546 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 547 | int32_t curCount = impl->mWeak; |
Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 548 | ALOG_ASSERT(curCount >= 0, "attemptIncWeak called on %p after underflow", |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | this); |
| 550 | while (curCount > 0) { |
| 551 | if (android_atomic_cmpxchg(curCount, curCount+1, &impl->mWeak) == 0) { |
| 552 | break; |
| 553 | } |
| 554 | curCount = impl->mWeak; |
| 555 | } |
| 556 | |
| 557 | if (curCount > 0) { |
| 558 | impl->addWeakRef(id); |
| 559 | } |
| 560 | |
| 561 | return curCount > 0; |
| 562 | } |
| 563 | |
| 564 | int32_t RefBase::weakref_type::getWeakCount() const |
| 565 | { |
| 566 | return static_cast<const weakref_impl*>(this)->mWeak; |
| 567 | } |
| 568 | |
| 569 | void RefBase::weakref_type::printRefs() const |
| 570 | { |
| 571 | static_cast<const weakref_impl*>(this)->printRefs(); |
| 572 | } |
| 573 | |
| 574 | void RefBase::weakref_type::trackMe(bool enable, bool retain) |
| 575 | { |
Josh Stone | e6b2162 | 2011-04-22 11:13:35 -0700 | [diff] [blame] | 576 | static_cast<weakref_impl*>(this)->trackMe(enable, retain); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | RefBase::weakref_type* RefBase::createWeak(const void* id) const |
| 580 | { |
| 581 | mRefs->incWeak(id); |
| 582 | return mRefs; |
| 583 | } |
| 584 | |
| 585 | RefBase::weakref_type* RefBase::getWeakRefs() const |
| 586 | { |
| 587 | return mRefs; |
| 588 | } |
| 589 | |
| 590 | RefBase::RefBase() |
| 591 | : mRefs(new weakref_impl(this)) |
| 592 | { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | RefBase::~RefBase() |
| 596 | { |
Mathias Agopian | cbe5278 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 597 | if (mRefs->mStrong == INITIAL_STRONG_VALUE) { |
| 598 | // we never acquired a strong (and/or weak) reference on this object. |
| 599 | delete mRefs; |
| 600 | } else { |
| 601 | // life-time of this object is extended to WEAK or FOREVER, in |
| 602 | // which case weakref_impl doesn't out-live the object and we |
| 603 | // can free it now. |
| 604 | if ((mRefs->mFlags & OBJECT_LIFETIME_MASK) != OBJECT_LIFETIME_STRONG) { |
| 605 | // It's possible that the weak count is not 0 if the object |
| 606 | // re-acquired a weak reference in its destructor |
| 607 | if (mRefs->mWeak == 0) { |
| 608 | delete mRefs; |
| 609 | } |
Mathias Agopian | 3b0a7a7 | 2011-06-08 16:05:30 -0700 | [diff] [blame] | 610 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 611 | } |
Mathias Agopian | cbe5278 | 2011-08-10 21:07:02 -0700 | [diff] [blame] | 612 | // for debugging purposes, clear this. |
| 613 | const_cast<weakref_impl*&>(mRefs) = NULL; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | void RefBase::extendObjectLifetime(int32_t mode) |
| 617 | { |
| 618 | android_atomic_or(mode, &mRefs->mFlags); |
| 619 | } |
| 620 | |
| 621 | void RefBase::onFirstRef() |
| 622 | { |
| 623 | } |
| 624 | |
| 625 | void RefBase::onLastStrongRef(const void* /*id*/) |
| 626 | { |
| 627 | } |
| 628 | |
| 629 | bool RefBase::onIncStrongAttempted(uint32_t flags, const void* id) |
| 630 | { |
| 631 | return (flags&FIRST_INC_STRONG) ? true : false; |
| 632 | } |
| 633 | |
| 634 | void RefBase::onLastWeakRef(const void* /*id*/) |
| 635 | { |
| 636 | } |
Mathias Agopian | a08ef49 | 2011-02-16 15:23:08 -0800 | [diff] [blame] | 637 | |
| 638 | // --------------------------------------------------------------------------- |
| 639 | |
Mathias Agopian | f14a104 | 2011-02-16 20:23:43 -0800 | [diff] [blame] | 640 | void RefBase::moveReferences(void* dst, void const* src, size_t n, |
| 641 | const ReferenceConverterBase& caster) |
| 642 | { |
| 643 | #if DEBUG_REFS |
| 644 | const size_t itemSize = caster.getReferenceTypeSize(); |
| 645 | for (size_t i=0 ; i<n ; i++) { |
| 646 | void* d = reinterpret_cast<void *>(intptr_t(dst) + i*itemSize); |
| 647 | void const* s = reinterpret_cast<void const*>(intptr_t(src) + i*itemSize); |
| 648 | RefBase* ref(reinterpret_cast<RefBase*>(caster.getReferenceBase(d))); |
| 649 | ref->mRefs->renameStrongRefId(s, d); |
| 650 | ref->mRefs->renameWeakRefId(s, d); |
| 651 | } |
| 652 | #endif |
| 653 | } |
| 654 | |
| 655 | // --------------------------------------------------------------------------- |
| 656 | |
Mathias Agopian | a08ef49 | 2011-02-16 15:23:08 -0800 | [diff] [blame] | 657 | TextOutput& printStrongPointer(TextOutput& to, const void* val) |
| 658 | { |
| 659 | to << "sp<>(" << val << ")"; |
| 660 | return to; |
| 661 | } |
| 662 | |
| 663 | TextOutput& printWeakPointer(TextOutput& to, const void* val) |
| 664 | { |
| 665 | to << "wp<>(" << val << ")"; |
| 666 | return to; |
| 667 | } |
| 668 | |
| 669 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 670 | }; // namespace android |