blob: abaf3c0ca65647246973082bfd5531527a524372 [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
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 Agopian8a4cdbc2013-03-11 21:27:16 -070018#define LOG_NDEBUG 0
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080019
20#include <utils/RefBase.h>
21
22#include <utils/Atomic.h>
23#include <utils/CallStack.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080024#include <utils/Log.h>
25#include <utils/threads.h>
Mathias Agopianad099652011-08-10 21:07:02 -070026#include <utils/TextOutput.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080027
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 Agopian6d4419d2013-03-18 20:31:18 -070038
39// whether ref-tracking is enabled by default, if not, trackMe(true, false)
40// needs to be called explicitly
41#define DEBUG_REFS_ENABLED_BY_DEFAULT 0
42
43// whether callstack are collected (significantly slows things down)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080044#define DEBUG_REFS_CALLSTACK_ENABLED 1
45
Mathias Agopian6d4419d2013-03-18 20:31:18 -070046// folder where stack traces are saved when DEBUG_REFS is enabled
47// this folder needs to exist and be writable
48#define DEBUG_REFS_CALLSTACK_PATH "/data/debug"
49
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080050// log all reference counting operations
51#define PRINT_REFS 0
52
53// ---------------------------------------------------------------------------
54
55namespace android {
56
57#define INITIAL_STRONG_VALUE (1<<28)
58
59// ---------------------------------------------------------------------------
60
61class RefBase::weakref_impl : public RefBase::weakref_type
62{
63public:
64 volatile int32_t mStrong;
65 volatile int32_t mWeak;
66 RefBase* const mBase;
67 volatile int32_t mFlags;
Mathias Agopian9c8fa9e2011-06-15 20:42:47 -070068
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080069#if !DEBUG_REFS
70
71 weakref_impl(RefBase* base)
72 : mStrong(INITIAL_STRONG_VALUE)
73 , mWeak(0)
74 , mBase(base)
75 , mFlags(0)
76 {
77 }
78
79 void addStrongRef(const void* /*id*/) { }
80 void removeStrongRef(const void* /*id*/) { }
Mathias Agopianad099652011-08-10 21:07:02 -070081 void renameStrongRefId(const void* /*old_id*/, const void* /*new_id*/) { }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080082 void addWeakRef(const void* /*id*/) { }
83 void removeWeakRef(const void* /*id*/) { }
Mathias Agopianad099652011-08-10 21:07:02 -070084 void renameWeakRefId(const void* /*old_id*/, const void* /*new_id*/) { }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080085 void printRefs() const { }
86 void trackMe(bool, bool) { }
87
88#else
89
90 weakref_impl(RefBase* base)
91 : mStrong(INITIAL_STRONG_VALUE)
92 , mWeak(0)
93 , mBase(base)
94 , mFlags(0)
95 , mStrongRefs(NULL)
96 , mWeakRefs(NULL)
97 , mTrackEnabled(!!DEBUG_REFS_ENABLED_BY_DEFAULT)
98 , mRetain(false)
99 {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800100 }
101
102 ~weakref_impl()
103 {
Mathias Agopianad099652011-08-10 21:07:02 -0700104 bool dumpStack = false;
105 if (!mRetain && mStrongRefs != NULL) {
106 dumpStack = true;
Steve Block1b781ab2012-01-06 19:20:56 +0000107 ALOGE("Strong references remain:");
Mathias Agopianad099652011-08-10 21:07:02 -0700108 ref_entry* refs = mStrongRefs;
109 while (refs) {
110 char inc = refs->ref >= 0 ? '+' : '-';
Steve Blockeb095332011-12-20 16:23:08 +0000111 ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref);
Mathias Agopianad099652011-08-10 21:07:02 -0700112#if DEBUG_REFS_CALLSTACK_ENABLED
113 refs->stack.dump();
114#endif
115 refs = refs->next;
116 }
117 }
118
119 if (!mRetain && mWeakRefs != NULL) {
120 dumpStack = true;
Steve Block1b781ab2012-01-06 19:20:56 +0000121 ALOGE("Weak references remain!");
Mathias Agopianad099652011-08-10 21:07:02 -0700122 ref_entry* refs = mWeakRefs;
123 while (refs) {
124 char inc = refs->ref >= 0 ? '+' : '-';
Steve Blockeb095332011-12-20 16:23:08 +0000125 ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref);
Mathias Agopianad099652011-08-10 21:07:02 -0700126#if DEBUG_REFS_CALLSTACK_ENABLED
127 refs->stack.dump();
128#endif
129 refs = refs->next;
130 }
131 }
132 if (dumpStack) {
Steve Block1b781ab2012-01-06 19:20:56 +0000133 ALOGE("above errors at:");
Mathias Agopianad099652011-08-10 21:07:02 -0700134 CallStack stack;
135 stack.update();
136 stack.dump();
137 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800138 }
139
Mathias Agopianad099652011-08-10 21:07:02 -0700140 void addStrongRef(const void* id) {
Steve Blockeb095332011-12-20 16:23:08 +0000141 //ALOGD_IF(mTrackEnabled,
Mathias Agopianad099652011-08-10 21:07:02 -0700142 // "addStrongRef: RefBase=%p, id=%p", mBase, id);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800143 addRef(&mStrongRefs, id, mStrong);
144 }
145
Mathias Agopianad099652011-08-10 21:07:02 -0700146 void removeStrongRef(const void* id) {
Steve Blockeb095332011-12-20 16:23:08 +0000147 //ALOGD_IF(mTrackEnabled,
Mathias Agopianad099652011-08-10 21:07:02 -0700148 // "removeStrongRef: RefBase=%p, id=%p", mBase, id);
149 if (!mRetain) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800150 removeRef(&mStrongRefs, id);
Mathias Agopianad099652011-08-10 21:07:02 -0700151 } else {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800152 addRef(&mStrongRefs, id, -mStrong);
Mathias Agopianad099652011-08-10 21:07:02 -0700153 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800154 }
155
Mathias Agopianad099652011-08-10 21:07:02 -0700156 void renameStrongRefId(const void* old_id, const void* new_id) {
Steve Blockeb095332011-12-20 16:23:08 +0000157 //ALOGD_IF(mTrackEnabled,
Mathias Agopianad099652011-08-10 21:07:02 -0700158 // "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 Projectcbb10112009-03-03 19:31:44 -0800164 addRef(&mWeakRefs, id, mWeak);
165 }
166
Mathias Agopianad099652011-08-10 21:07:02 -0700167 void removeWeakRef(const void* id) {
168 if (!mRetain) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800169 removeRef(&mWeakRefs, id);
Mathias Agopianad099652011-08-10 21:07:02 -0700170 } else {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800171 addRef(&mWeakRefs, id, -mWeak);
Mathias Agopianad099652011-08-10 21:07:02 -0700172 }
173 }
174
175 void renameWeakRefId(const void* old_id, const void* new_id) {
176 renameRefsId(mWeakRefs, old_id, new_id);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800177 }
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 Agopianad099652011-08-10 21:07:02 -0700190 Mutex::Autolock _l(mMutex);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800191 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];
Mathias Agopian6d4419d2013-03-18 20:31:18 -0700202 snprintf(name, 100, DEBUG_REFS_CALLSTACK_PATH "/%p.stack", this);
Mathias Agopian769828d2013-03-06 17:51:15 -0800203 int rc = open(name, O_RDWR | O_CREAT | O_APPEND, 644);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800204 if (rc >= 0) {
205 write(rc, text.string(), text.length());
206 close(rc);
Steve Blockeb095332011-12-20 16:23:08 +0000207 ALOGD("STACK TRACE for %p saved in %s", this, name);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800208 }
Steve Block1b781ab2012-01-06 19:20:56 +0000209 else ALOGE("FAILED TO PRINT STACK TRACE for %p in %s: %s", this,
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800210 name, strerror(errno));
211 }
212 }
213
214private:
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 Agopianad099652011-08-10 21:07:02 -0700229
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800230 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 Projectcbb10112009-03-03 19:31:44 -0800239 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 Agopianad099652011-08-10 21:07:02 -0700249 ref_entry* const head = *refs;
250 ref_entry* ref = head;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800251 while (ref != NULL) {
252 if (ref->id == id) {
253 *refs = ref->next;
254 delete ref;
255 return;
256 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800257 refs = &ref->next;
258 ref = *refs;
259 }
Mathias Agopianad099652011-08-10 21:07:02 -0700260
Steve Block1b781ab2012-01-06 19:20:56 +0000261 ALOGE("RefBase: removing id %p on RefBase %p"
Mathias Agopianad099652011-08-10 21:07:02 -0700262 "(weakref_type %p) that doesn't exist!",
263 id, mBase, this);
264
265 ref = head;
266 while (ref) {
267 char inc = ref->ref >= 0 ? '+' : '-';
Steve Blockeb095332011-12-20 16:23:08 +0000268 ALOGD("\t%c ID %p (ref %d):", inc, ref->id, ref->ref);
Mathias Agopianad099652011-08-10 21:07:02 -0700269 ref = ref->next;
270 }
271
272 CallStack stack;
273 stack.update();
274 stack.dump();
275 }
276 }
277
278 void renameRefsId(ref_entry* r, const void* old_id, const void* new_id)
279 {
280 if (mTrackEnabled) {
281 AutoMutex _l(mMutex);
282 ref_entry* ref = r;
283 while (ref != NULL) {
284 if (ref->id == old_id) {
285 ref->id = new_id;
286 }
287 ref = ref->next;
288 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800289 }
290 }
291
292 void printRefsLocked(String8* out, const ref_entry* refs) const
293 {
294 char buf[128];
295 while (refs) {
296 char inc = refs->ref >= 0 ? '+' : '-';
297 sprintf(buf, "\t%c ID %p (ref %d):\n",
298 inc, refs->id, refs->ref);
299 out->append(buf);
300#if DEBUG_REFS_CALLSTACK_ENABLED
301 out->append(refs->stack.toString("\t\t"));
302#else
303 out->append("\t\t(call stacks disabled)");
304#endif
305 refs = refs->next;
306 }
307 }
308
Mathias Agopianad099652011-08-10 21:07:02 -0700309 mutable Mutex mMutex;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800310 ref_entry* mStrongRefs;
311 ref_entry* mWeakRefs;
312
313 bool mTrackEnabled;
314 // Collect stack traces on addref and removeref, instead of deleting the stack references
315 // on removeref that match the address ones.
316 bool mRetain;
317
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800318#endif
319};
320
321// ---------------------------------------------------------------------------
322
323void RefBase::incStrong(const void* id) const
324{
325 weakref_impl* const refs = mRefs;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800326 refs->incWeak(id);
327
328 refs->addStrongRef(id);
329 const int32_t c = android_atomic_inc(&refs->mStrong);
Steve Blockae074452012-01-09 18:35:44 +0000330 ALOG_ASSERT(c > 0, "incStrong() called on %p after last strong ref", refs);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800331#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000332 ALOGD("incStrong of %p from %p: cnt=%d\n", this, id, c);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800333#endif
334 if (c != INITIAL_STRONG_VALUE) {
335 return;
336 }
337
338 android_atomic_add(-INITIAL_STRONG_VALUE, &refs->mStrong);
Mathias Agopianad099652011-08-10 21:07:02 -0700339 refs->mBase->onFirstRef();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800340}
341
342void RefBase::decStrong(const void* id) const
343{
344 weakref_impl* const refs = mRefs;
345 refs->removeStrongRef(id);
346 const int32_t c = android_atomic_dec(&refs->mStrong);
347#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000348 ALOGD("decStrong of %p from %p: cnt=%d\n", this, id, c);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800349#endif
Steve Blockae074452012-01-09 18:35:44 +0000350 ALOG_ASSERT(c >= 1, "decStrong() called on %p too many times", refs);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800351 if (c == 1) {
Mathias Agopianad099652011-08-10 21:07:02 -0700352 refs->mBase->onLastStrongRef(id);
353 if ((refs->mFlags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) {
Mathias Agopian9c8fa9e2011-06-15 20:42:47 -0700354 delete this;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800355 }
356 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800357 refs->decWeak(id);
358}
359
360void RefBase::forceIncStrong(const void* id) const
361{
362 weakref_impl* const refs = mRefs;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800363 refs->incWeak(id);
364
365 refs->addStrongRef(id);
366 const int32_t c = android_atomic_inc(&refs->mStrong);
Steve Blockae074452012-01-09 18:35:44 +0000367 ALOG_ASSERT(c >= 0, "forceIncStrong called on %p after ref count underflow",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800368 refs);
369#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000370 ALOGD("forceIncStrong of %p from %p: cnt=%d\n", this, id, c);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800371#endif
372
373 switch (c) {
374 case INITIAL_STRONG_VALUE:
375 android_atomic_add(-INITIAL_STRONG_VALUE, &refs->mStrong);
376 // fall through...
377 case 0:
Mathias Agopianad099652011-08-10 21:07:02 -0700378 refs->mBase->onFirstRef();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800379 }
380}
381
382int32_t RefBase::getStrongCount() const
383{
384 return mRefs->mStrong;
385}
386
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800387RefBase* RefBase::weakref_type::refBase() const
388{
389 return static_cast<const weakref_impl*>(this)->mBase;
390}
391
392void RefBase::weakref_type::incWeak(const void* id)
393{
394 weakref_impl* const impl = static_cast<weakref_impl*>(this);
395 impl->addWeakRef(id);
396 const int32_t c = android_atomic_inc(&impl->mWeak);
Steve Blockae074452012-01-09 18:35:44 +0000397 ALOG_ASSERT(c >= 0, "incWeak called on %p after last weak ref", this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800398}
399
Mathias Agopianad099652011-08-10 21:07:02 -0700400
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800401void RefBase::weakref_type::decWeak(const void* id)
402{
403 weakref_impl* const impl = static_cast<weakref_impl*>(this);
404 impl->removeWeakRef(id);
405 const int32_t c = android_atomic_dec(&impl->mWeak);
Steve Blockae074452012-01-09 18:35:44 +0000406 ALOG_ASSERT(c >= 1, "decWeak called on %p too many times", this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800407 if (c != 1) return;
Mathias Agopianad099652011-08-10 21:07:02 -0700408
409 if ((impl->mFlags&OBJECT_LIFETIME_WEAK) == OBJECT_LIFETIME_STRONG) {
410 // This is the regular lifetime case. The object is destroyed
411 // when the last strong reference goes away. Since weakref_impl
412 // outlive the object, it is not destroyed in the dtor, and
413 // we'll have to do it here.
414 if (impl->mStrong == INITIAL_STRONG_VALUE) {
415 // Special case: we never had a strong reference, so we need to
416 // destroy the object now.
Mathias Agopian9c8fa9e2011-06-15 20:42:47 -0700417 delete impl->mBase;
Mathias Agopianad099652011-08-10 21:07:02 -0700418 } else {
Steve Blockb37fbe92011-10-20 11:56:00 +0100419 // ALOGV("Freeing refs %p of old RefBase %p\n", this, impl->mBase);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800420 delete impl;
421 }
422 } else {
Mathias Agopianad099652011-08-10 21:07:02 -0700423 // less common case: lifetime is OBJECT_LIFETIME_{WEAK|FOREVER}
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800424 impl->mBase->onLastWeakRef(id);
Mathias Agopianad099652011-08-10 21:07:02 -0700425 if ((impl->mFlags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_WEAK) {
426 // this is the OBJECT_LIFETIME_WEAK case. The last weak-reference
427 // is gone, we can destroy the object.
Mathias Agopian9c8fa9e2011-06-15 20:42:47 -0700428 delete impl->mBase;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800429 }
430 }
431}
432
433bool RefBase::weakref_type::attemptIncStrong(const void* id)
434{
435 incWeak(id);
436
437 weakref_impl* const impl = static_cast<weakref_impl*>(this);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800438 int32_t curCount = impl->mStrong;
Dianne Hackborna729ab12013-03-14 15:26:30 -0700439
440 ALOG_ASSERT(curCount >= 0,
441 "attemptIncStrong called on %p after underflow", this);
442
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800443 while (curCount > 0 && curCount != INITIAL_STRONG_VALUE) {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700444 // we're in the easy/common case of promoting a weak-reference
445 // from an existing strong reference.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800446 if (android_atomic_cmpxchg(curCount, curCount+1, &impl->mStrong) == 0) {
447 break;
448 }
Dianne Hackborna729ab12013-03-14 15:26:30 -0700449 // the strong count has changed on us, we need to re-assert our
450 // situation.
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800451 curCount = impl->mStrong;
452 }
453
454 if (curCount <= 0 || curCount == INITIAL_STRONG_VALUE) {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700455 // we're now in the harder case of either:
456 // - there never was a strong reference on us
457 // - or, all strong references have been released
458 if ((impl->mFlags&OBJECT_LIFETIME_WEAK) == OBJECT_LIFETIME_STRONG) {
459 // this object has a "normal" life-time, i.e.: it gets destroyed
460 // when the last strong reference goes away
461 if (curCount <= 0) {
462 // the last strong-reference got released, the object cannot
463 // be revived.
464 decWeak(id);
465 return false;
466 }
467
468 // here, curCount == INITIAL_STRONG_VALUE, which means
469 // there never was a strong-reference, so we can try to
470 // promote this object; we need to do that atomically.
471 while (curCount > 0) {
472 if (android_atomic_cmpxchg(curCount, curCount + 1,
473 &impl->mStrong) == 0) {
474 break;
475 }
476 // the strong count has changed on us, we need to re-assert our
477 // situation (e.g.: another thread has inc/decStrong'ed us)
478 curCount = impl->mStrong;
479 }
480
481 if (curCount <= 0) {
482 // promote() failed, some other thread destroyed us in the
483 // meantime (i.e.: strong count reached zero).
484 decWeak(id);
485 return false;
486 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800487 } else {
Dianne Hackborna729ab12013-03-14 15:26:30 -0700488 // this object has an "extended" life-time, i.e.: it can be
489 // revived from a weak-reference only.
490 // Ask the object's implementation if it agrees to be revived
491 if (!impl->mBase->onIncStrongAttempted(FIRST_INC_STRONG, id)) {
492 // it didn't so give-up.
493 decWeak(id);
494 return false;
495 }
496 // grab a strong-reference, which is always safe due to the
497 // extended life-time.
498 curCount = android_atomic_inc(&impl->mStrong);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800499 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800500
501 // If the strong reference count has already been incremented by
502 // someone else, the implementor of onIncStrongAttempted() is holding
503 // an unneeded reference. So call onLastStrongRef() here to remove it.
504 // (No, this is not pretty.) Note that we MUST NOT do this if we
505 // are in fact acquiring the first reference.
506 if (curCount > 0 && curCount < INITIAL_STRONG_VALUE) {
507 impl->mBase->onLastStrongRef(id);
508 }
509 }
510
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800511 impl->addStrongRef(id);
512
513#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000514 ALOGD("attemptIncStrong of %p from %p: cnt=%d\n", this, id, curCount);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800515#endif
516
Dianne Hackborna729ab12013-03-14 15:26:30 -0700517 // now we need to fix-up the count if it was INITIAL_STRONG_VALUE
518 // this must be done safely, i.e.: handle the case where several threads
519 // were here in attemptIncStrong().
520 curCount = impl->mStrong;
521 while (curCount >= INITIAL_STRONG_VALUE) {
522 ALOG_ASSERT(curCount > INITIAL_STRONG_VALUE,
523 "attemptIncStrong in %p underflowed to INITIAL_STRONG_VALUE",
524 this);
525 if (android_atomic_cmpxchg(curCount, curCount-INITIAL_STRONG_VALUE,
526 &impl->mStrong) == 0) {
527 break;
528 }
529 // the strong-count changed on us, we need to re-assert the situation,
530 // for e.g.: it's possible the fix-up happened in another thread.
531 curCount = impl->mStrong;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800532 }
Dianne Hackborna729ab12013-03-14 15:26:30 -0700533
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800534 return true;
535}
536
537bool RefBase::weakref_type::attemptIncWeak(const void* id)
538{
539 weakref_impl* const impl = static_cast<weakref_impl*>(this);
Mathias Agopianad099652011-08-10 21:07:02 -0700540
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800541 int32_t curCount = impl->mWeak;
Steve Blockae074452012-01-09 18:35:44 +0000542 ALOG_ASSERT(curCount >= 0, "attemptIncWeak called on %p after underflow",
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800543 this);
544 while (curCount > 0) {
545 if (android_atomic_cmpxchg(curCount, curCount+1, &impl->mWeak) == 0) {
546 break;
547 }
548 curCount = impl->mWeak;
549 }
550
551 if (curCount > 0) {
552 impl->addWeakRef(id);
553 }
554
555 return curCount > 0;
556}
557
558int32_t RefBase::weakref_type::getWeakCount() const
559{
560 return static_cast<const weakref_impl*>(this)->mWeak;
561}
562
563void RefBase::weakref_type::printRefs() const
564{
565 static_cast<const weakref_impl*>(this)->printRefs();
566}
567
568void RefBase::weakref_type::trackMe(bool enable, bool retain)
569{
Mathias Agopianad099652011-08-10 21:07:02 -0700570 static_cast<weakref_impl*>(this)->trackMe(enable, retain);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800571}
572
573RefBase::weakref_type* RefBase::createWeak(const void* id) const
574{
575 mRefs->incWeak(id);
576 return mRefs;
577}
578
579RefBase::weakref_type* RefBase::getWeakRefs() const
580{
581 return mRefs;
582}
583
584RefBase::RefBase()
585 : mRefs(new weakref_impl(this))
586{
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800587}
588
589RefBase::~RefBase()
590{
Mathias Agopianad099652011-08-10 21:07:02 -0700591 if (mRefs->mStrong == INITIAL_STRONG_VALUE) {
592 // we never acquired a strong (and/or weak) reference on this object.
Mathias Agopian9c8fa9e2011-06-15 20:42:47 -0700593 delete mRefs;
Mathias Agopianad099652011-08-10 21:07:02 -0700594 } else {
595 // life-time of this object is extended to WEAK or FOREVER, in
596 // which case weakref_impl doesn't out-live the object and we
597 // can free it now.
598 if ((mRefs->mFlags & OBJECT_LIFETIME_MASK) != OBJECT_LIFETIME_STRONG) {
599 // It's possible that the weak count is not 0 if the object
600 // re-acquired a weak reference in its destructor
601 if (mRefs->mWeak == 0) {
602 delete mRefs;
603 }
604 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800605 }
Mathias Agopianad099652011-08-10 21:07:02 -0700606 // for debugging purposes, clear this.
607 const_cast<weakref_impl*&>(mRefs) = NULL;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800608}
609
610void RefBase::extendObjectLifetime(int32_t mode)
611{
612 android_atomic_or(mode, &mRefs->mFlags);
613}
614
615void RefBase::onFirstRef()
616{
617}
618
619void RefBase::onLastStrongRef(const void* /*id*/)
620{
621}
622
623bool RefBase::onIncStrongAttempted(uint32_t flags, const void* id)
624{
625 return (flags&FIRST_INC_STRONG) ? true : false;
626}
627
628void RefBase::onLastWeakRef(const void* /*id*/)
629{
630}
Mathias Agopianad099652011-08-10 21:07:02 -0700631
632// ---------------------------------------------------------------------------
633
Mathias Agopian6cd548c2013-03-18 22:27:41 -0700634void RefBase::renameRefs(size_t n, const ReferenceRenamer& renamer) {
Mathias Agopianad099652011-08-10 21:07:02 -0700635#if DEBUG_REFS
Mathias Agopianad099652011-08-10 21:07:02 -0700636 for (size_t i=0 ; i<n ; i++) {
Mathias Agopian6cd548c2013-03-18 22:27:41 -0700637 renamer(i);
Mathias Agopianad099652011-08-10 21:07:02 -0700638 }
639#endif
640}
641
Mathias Agopian6cd548c2013-03-18 22:27:41 -0700642void RefBase::renameRefId(weakref_type* ref,
643 const void* old_id, const void* new_id) {
644 weakref_impl* const impl = static_cast<weakref_impl*>(ref);
645 impl->renameStrongRefId(old_id, new_id);
646 impl->renameWeakRefId(old_id, new_id);
647}
648
649void RefBase::renameRefId(RefBase* ref,
650 const void* old_id, const void* new_id) {
651 ref->mRefs->renameStrongRefId(old_id, new_id);
652 ref->mRefs->renameWeakRefId(old_id, new_id);
653}
654
Mathias Agopianad099652011-08-10 21:07:02 -0700655// ---------------------------------------------------------------------------
656
657TextOutput& printStrongPointer(TextOutput& to, const void* val)
658{
659 to << "sp<>(" << val << ")";
660 return to;
661}
662
663TextOutput& printWeakPointer(TextOutput& to, const void* val)
664{
665 to << "wp<>(" << val << ")";
666 return to;
667}
668
669
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800670}; // namespace android