blob: ad0939e574b27dd00185114979d48d540d6f7ce5 [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"
18
19#include <utils/RefBase.h>
20
21#include <utils/Atomic.h>
22#include <utils/CallStack.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080023#include <utils/Log.h>
24#include <utils/threads.h>
Mathias Agopianad099652011-08-10 21:07:02 -070025#include <utils/TextOutput.h>
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080026
27#include <stdlib.h>
28#include <stdio.h>
29#include <typeinfo>
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <fcntl.h>
33#include <unistd.h>
34
35// compile with refcounting debugging enabled
36#define DEBUG_REFS 0
Mathias Agopianad099652011-08-10 21:07:02 -070037#define DEBUG_REFS_FATAL_SANITY_CHECKS 0
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080038#define DEBUG_REFS_ENABLED_BY_DEFAULT 1
39#define DEBUG_REFS_CALLSTACK_ENABLED 1
40
41// log all reference counting operations
42#define PRINT_REFS 0
43
44// ---------------------------------------------------------------------------
45
46namespace android {
47
48#define INITIAL_STRONG_VALUE (1<<28)
49
50// ---------------------------------------------------------------------------
51
52class RefBase::weakref_impl : public RefBase::weakref_type
53{
54public:
55 volatile int32_t mStrong;
56 volatile int32_t mWeak;
57 RefBase* const mBase;
58 volatile int32_t mFlags;
Mathias Agopian9c8fa9e2011-06-15 20:42:47 -070059
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080060#if !DEBUG_REFS
61
62 weakref_impl(RefBase* base)
63 : mStrong(INITIAL_STRONG_VALUE)
64 , mWeak(0)
65 , mBase(base)
66 , mFlags(0)
67 {
68 }
69
70 void addStrongRef(const void* /*id*/) { }
71 void removeStrongRef(const void* /*id*/) { }
Mathias Agopianad099652011-08-10 21:07:02 -070072 void renameStrongRefId(const void* /*old_id*/, const void* /*new_id*/) { }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080073 void addWeakRef(const void* /*id*/) { }
74 void removeWeakRef(const void* /*id*/) { }
Mathias Agopianad099652011-08-10 21:07:02 -070075 void renameWeakRefId(const void* /*old_id*/, const void* /*new_id*/) { }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080076 void printRefs() const { }
77 void trackMe(bool, bool) { }
78
79#else
80
81 weakref_impl(RefBase* base)
82 : mStrong(INITIAL_STRONG_VALUE)
83 , mWeak(0)
84 , mBase(base)
85 , mFlags(0)
86 , mStrongRefs(NULL)
87 , mWeakRefs(NULL)
88 , mTrackEnabled(!!DEBUG_REFS_ENABLED_BY_DEFAULT)
89 , mRetain(false)
90 {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080091 }
92
93 ~weakref_impl()
94 {
Mathias Agopianad099652011-08-10 21:07:02 -070095 bool dumpStack = false;
96 if (!mRetain && mStrongRefs != NULL) {
97 dumpStack = true;
98#if DEBUG_REFS_FATAL_SANITY_CHECKS
99 LOG_ALWAYS_FATAL("Strong references remain!");
100#else
Steve Block1b781ab2012-01-06 19:20:56 +0000101 ALOGE("Strong references remain:");
Mathias Agopianad099652011-08-10 21:07:02 -0700102#endif
103 ref_entry* refs = mStrongRefs;
104 while (refs) {
105 char inc = refs->ref >= 0 ? '+' : '-';
Steve Blockeb095332011-12-20 16:23:08 +0000106 ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref);
Mathias Agopianad099652011-08-10 21:07:02 -0700107#if DEBUG_REFS_CALLSTACK_ENABLED
108 refs->stack.dump();
109#endif
110 refs = refs->next;
111 }
112 }
113
114 if (!mRetain && mWeakRefs != NULL) {
115 dumpStack = true;
116#if DEBUG_REFS_FATAL_SANITY_CHECKS
117 LOG_ALWAYS_FATAL("Weak references remain:");
118#else
Steve Block1b781ab2012-01-06 19:20:56 +0000119 ALOGE("Weak references remain!");
Mathias Agopianad099652011-08-10 21:07:02 -0700120#endif
121 ref_entry* refs = mWeakRefs;
122 while (refs) {
123 char inc = refs->ref >= 0 ? '+' : '-';
Steve Blockeb095332011-12-20 16:23:08 +0000124 ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref);
Mathias Agopianad099652011-08-10 21:07:02 -0700125#if DEBUG_REFS_CALLSTACK_ENABLED
126 refs->stack.dump();
127#endif
128 refs = refs->next;
129 }
130 }
131 if (dumpStack) {
Steve Block1b781ab2012-01-06 19:20:56 +0000132 ALOGE("above errors at:");
Mathias Agopianad099652011-08-10 21:07:02 -0700133 CallStack stack;
134 stack.update();
135 stack.dump();
136 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800137 }
138
Mathias Agopianad099652011-08-10 21:07:02 -0700139 void addStrongRef(const void* id) {
Steve Blockeb095332011-12-20 16:23:08 +0000140 //ALOGD_IF(mTrackEnabled,
Mathias Agopianad099652011-08-10 21:07:02 -0700141 // "addStrongRef: RefBase=%p, id=%p", mBase, id);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800142 addRef(&mStrongRefs, id, mStrong);
143 }
144
Mathias Agopianad099652011-08-10 21:07:02 -0700145 void removeStrongRef(const void* id) {
Steve Blockeb095332011-12-20 16:23:08 +0000146 //ALOGD_IF(mTrackEnabled,
Mathias Agopianad099652011-08-10 21:07:02 -0700147 // "removeStrongRef: RefBase=%p, id=%p", mBase, id);
148 if (!mRetain) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800149 removeRef(&mStrongRefs, id);
Mathias Agopianad099652011-08-10 21:07:02 -0700150 } else {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800151 addRef(&mStrongRefs, id, -mStrong);
Mathias Agopianad099652011-08-10 21:07:02 -0700152 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800153 }
154
Mathias Agopianad099652011-08-10 21:07:02 -0700155 void renameStrongRefId(const void* old_id, const void* new_id) {
Steve Blockeb095332011-12-20 16:23:08 +0000156 //ALOGD_IF(mTrackEnabled,
Mathias Agopianad099652011-08-10 21:07:02 -0700157 // "renameStrongRefId: RefBase=%p, oid=%p, nid=%p",
158 // mBase, old_id, new_id);
159 renameRefsId(mStrongRefs, old_id, new_id);
160 }
161
162 void addWeakRef(const void* id) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800163 addRef(&mWeakRefs, id, mWeak);
164 }
165
Mathias Agopianad099652011-08-10 21:07:02 -0700166 void removeWeakRef(const void* id) {
167 if (!mRetain) {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800168 removeRef(&mWeakRefs, id);
Mathias Agopianad099652011-08-10 21:07:02 -0700169 } else {
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800170 addRef(&mWeakRefs, id, -mWeak);
Mathias Agopianad099652011-08-10 21:07:02 -0700171 }
172 }
173
174 void renameWeakRefId(const void* old_id, const void* new_id) {
175 renameRefsId(mWeakRefs, old_id, new_id);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800176 }
177
178 void trackMe(bool track, bool retain)
179 {
180 mTrackEnabled = track;
181 mRetain = retain;
182 }
183
184 void printRefs() const
185 {
186 String8 text;
187
188 {
Mathias Agopianad099652011-08-10 21:07:02 -0700189 Mutex::Autolock _l(mMutex);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800190 char buf[128];
191 sprintf(buf, "Strong references on RefBase %p (weakref_type %p):\n", mBase, this);
192 text.append(buf);
193 printRefsLocked(&text, mStrongRefs);
194 sprintf(buf, "Weak references on RefBase %p (weakref_type %p):\n", mBase, this);
195 text.append(buf);
196 printRefsLocked(&text, mWeakRefs);
197 }
198
199 {
200 char name[100];
201 snprintf(name, 100, "/data/%p.stack", this);
202 int rc = open(name, O_RDWR | O_CREAT | O_APPEND);
203 if (rc >= 0) {
204 write(rc, text.string(), text.length());
205 close(rc);
Steve Blockeb095332011-12-20 16:23:08 +0000206 ALOGD("STACK TRACE for %p saved in %s", this, name);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800207 }
Steve Block1b781ab2012-01-06 19:20:56 +0000208 else ALOGE("FAILED TO PRINT STACK TRACE for %p in %s: %s", this,
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800209 name, strerror(errno));
210 }
211 }
212
213private:
214 struct ref_entry
215 {
216 ref_entry* next;
217 const void* id;
218#if DEBUG_REFS_CALLSTACK_ENABLED
219 CallStack stack;
220#endif
221 int32_t ref;
222 };
223
224 void addRef(ref_entry** refs, const void* id, int32_t mRef)
225 {
226 if (mTrackEnabled) {
227 AutoMutex _l(mMutex);
Mathias Agopianad099652011-08-10 21:07:02 -0700228
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800229 ref_entry* ref = new ref_entry;
230 // Reference count at the time of the snapshot, but before the
231 // update. Positive value means we increment, negative--we
232 // decrement the reference count.
233 ref->ref = mRef;
234 ref->id = id;
235#if DEBUG_REFS_CALLSTACK_ENABLED
236 ref->stack.update(2);
237#endif
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800238 ref->next = *refs;
239 *refs = ref;
240 }
241 }
242
243 void removeRef(ref_entry** refs, const void* id)
244 {
245 if (mTrackEnabled) {
246 AutoMutex _l(mMutex);
247
Mathias Agopianad099652011-08-10 21:07:02 -0700248 ref_entry* const head = *refs;
249 ref_entry* ref = head;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800250 while (ref != NULL) {
251 if (ref->id == id) {
252 *refs = ref->next;
253 delete ref;
254 return;
255 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800256 refs = &ref->next;
257 ref = *refs;
258 }
Mathias Agopianad099652011-08-10 21:07:02 -0700259
260#if DEBUG_REFS_FATAL_SANITY_CHECKS
261 LOG_ALWAYS_FATAL("RefBase: removing id %p on RefBase %p"
262 "(weakref_type %p) that doesn't exist!",
263 id, mBase, this);
264#endif
265
Steve Block1b781ab2012-01-06 19:20:56 +0000266 ALOGE("RefBase: removing id %p on RefBase %p"
Mathias Agopianad099652011-08-10 21:07:02 -0700267 "(weakref_type %p) that doesn't exist!",
268 id, mBase, this);
269
270 ref = head;
271 while (ref) {
272 char inc = ref->ref >= 0 ? '+' : '-';
Steve Blockeb095332011-12-20 16:23:08 +0000273 ALOGD("\t%c ID %p (ref %d):", inc, ref->id, ref->ref);
Mathias Agopianad099652011-08-10 21:07:02 -0700274 ref = ref->next;
275 }
276
277 CallStack stack;
278 stack.update();
279 stack.dump();
280 }
281 }
282
283 void renameRefsId(ref_entry* r, const void* old_id, const void* new_id)
284 {
285 if (mTrackEnabled) {
286 AutoMutex _l(mMutex);
287 ref_entry* ref = r;
288 while (ref != NULL) {
289 if (ref->id == old_id) {
290 ref->id = new_id;
291 }
292 ref = ref->next;
293 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800294 }
295 }
296
297 void printRefsLocked(String8* out, const ref_entry* refs) const
298 {
299 char buf[128];
300 while (refs) {
301 char inc = refs->ref >= 0 ? '+' : '-';
302 sprintf(buf, "\t%c ID %p (ref %d):\n",
303 inc, refs->id, refs->ref);
304 out->append(buf);
305#if DEBUG_REFS_CALLSTACK_ENABLED
306 out->append(refs->stack.toString("\t\t"));
307#else
308 out->append("\t\t(call stacks disabled)");
309#endif
310 refs = refs->next;
311 }
312 }
313
Mathias Agopianad099652011-08-10 21:07:02 -0700314 mutable Mutex mMutex;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800315 ref_entry* mStrongRefs;
316 ref_entry* mWeakRefs;
317
318 bool mTrackEnabled;
319 // Collect stack traces on addref and removeref, instead of deleting the stack references
320 // on removeref that match the address ones.
321 bool mRetain;
322
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800323#endif
324};
325
326// ---------------------------------------------------------------------------
327
328void RefBase::incStrong(const void* id) const
329{
330 weakref_impl* const refs = mRefs;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800331 refs->incWeak(id);
332
333 refs->addStrongRef(id);
334 const int32_t c = android_atomic_inc(&refs->mStrong);
335 LOG_ASSERT(c > 0, "incStrong() called on %p after last strong ref", refs);
336#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000337 ALOGD("incStrong of %p from %p: cnt=%d\n", this, id, c);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800338#endif
339 if (c != INITIAL_STRONG_VALUE) {
340 return;
341 }
342
343 android_atomic_add(-INITIAL_STRONG_VALUE, &refs->mStrong);
Mathias Agopianad099652011-08-10 21:07:02 -0700344 refs->mBase->onFirstRef();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800345}
346
347void RefBase::decStrong(const void* id) const
348{
349 weakref_impl* const refs = mRefs;
350 refs->removeStrongRef(id);
351 const int32_t c = android_atomic_dec(&refs->mStrong);
352#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000353 ALOGD("decStrong of %p from %p: cnt=%d\n", this, id, c);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800354#endif
355 LOG_ASSERT(c >= 1, "decStrong() called on %p too many times", refs);
356 if (c == 1) {
Mathias Agopianad099652011-08-10 21:07:02 -0700357 refs->mBase->onLastStrongRef(id);
358 if ((refs->mFlags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) {
Mathias Agopian9c8fa9e2011-06-15 20:42:47 -0700359 delete this;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800360 }
361 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800362 refs->decWeak(id);
363}
364
365void RefBase::forceIncStrong(const void* id) const
366{
367 weakref_impl* const refs = mRefs;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800368 refs->incWeak(id);
369
370 refs->addStrongRef(id);
371 const int32_t c = android_atomic_inc(&refs->mStrong);
372 LOG_ASSERT(c >= 0, "forceIncStrong called on %p after ref count underflow",
373 refs);
374#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000375 ALOGD("forceIncStrong of %p from %p: cnt=%d\n", this, id, c);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800376#endif
377
378 switch (c) {
379 case INITIAL_STRONG_VALUE:
380 android_atomic_add(-INITIAL_STRONG_VALUE, &refs->mStrong);
381 // fall through...
382 case 0:
Mathias Agopianad099652011-08-10 21:07:02 -0700383 refs->mBase->onFirstRef();
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800384 }
385}
386
387int32_t RefBase::getStrongCount() const
388{
389 return mRefs->mStrong;
390}
391
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800392RefBase* RefBase::weakref_type::refBase() const
393{
394 return static_cast<const weakref_impl*>(this)->mBase;
395}
396
397void RefBase::weakref_type::incWeak(const void* id)
398{
399 weakref_impl* const impl = static_cast<weakref_impl*>(this);
400 impl->addWeakRef(id);
401 const int32_t c = android_atomic_inc(&impl->mWeak);
402 LOG_ASSERT(c >= 0, "incWeak called on %p after last weak ref", this);
403}
404
Mathias Agopianad099652011-08-10 21:07:02 -0700405
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800406void RefBase::weakref_type::decWeak(const void* id)
407{
408 weakref_impl* const impl = static_cast<weakref_impl*>(this);
409 impl->removeWeakRef(id);
410 const int32_t c = android_atomic_dec(&impl->mWeak);
411 LOG_ASSERT(c >= 1, "decWeak called on %p too many times", this);
412 if (c != 1) return;
Mathias Agopianad099652011-08-10 21:07:02 -0700413
414 if ((impl->mFlags&OBJECT_LIFETIME_WEAK) == OBJECT_LIFETIME_STRONG) {
415 // This is the regular lifetime case. The object is destroyed
416 // when the last strong reference goes away. Since weakref_impl
417 // outlive the object, it is not destroyed in the dtor, and
418 // we'll have to do it here.
419 if (impl->mStrong == INITIAL_STRONG_VALUE) {
420 // Special case: we never had a strong reference, so we need to
421 // destroy the object now.
Mathias Agopian9c8fa9e2011-06-15 20:42:47 -0700422 delete impl->mBase;
Mathias Agopianad099652011-08-10 21:07:02 -0700423 } else {
Steve Blockb37fbe92011-10-20 11:56:00 +0100424 // ALOGV("Freeing refs %p of old RefBase %p\n", this, impl->mBase);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800425 delete impl;
426 }
427 } else {
Mathias Agopianad099652011-08-10 21:07:02 -0700428 // less common case: lifetime is OBJECT_LIFETIME_{WEAK|FOREVER}
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800429 impl->mBase->onLastWeakRef(id);
Mathias Agopianad099652011-08-10 21:07:02 -0700430 if ((impl->mFlags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_WEAK) {
431 // this is the OBJECT_LIFETIME_WEAK case. The last weak-reference
432 // is gone, we can destroy the object.
Mathias Agopian9c8fa9e2011-06-15 20:42:47 -0700433 delete impl->mBase;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800434 }
435 }
436}
437
438bool RefBase::weakref_type::attemptIncStrong(const void* id)
439{
440 incWeak(id);
441
442 weakref_impl* const impl = static_cast<weakref_impl*>(this);
443
444 int32_t curCount = impl->mStrong;
445 LOG_ASSERT(curCount >= 0, "attemptIncStrong called on %p after underflow",
446 this);
447 while (curCount > 0 && curCount != INITIAL_STRONG_VALUE) {
448 if (android_atomic_cmpxchg(curCount, curCount+1, &impl->mStrong) == 0) {
449 break;
450 }
451 curCount = impl->mStrong;
452 }
453
454 if (curCount <= 0 || curCount == INITIAL_STRONG_VALUE) {
455 bool allow;
456 if (curCount == INITIAL_STRONG_VALUE) {
457 // Attempting to acquire first strong reference... this is allowed
458 // if the object does NOT have a longer lifetime (meaning the
459 // implementation doesn't need to see this), or if the implementation
460 // allows it to happen.
461 allow = (impl->mFlags&OBJECT_LIFETIME_WEAK) != OBJECT_LIFETIME_WEAK
462 || impl->mBase->onIncStrongAttempted(FIRST_INC_STRONG, id);
463 } else {
464 // Attempting to revive the object... this is allowed
465 // if the object DOES have a longer lifetime (so we can safely
466 // call the object with only a weak ref) and the implementation
467 // allows it to happen.
468 allow = (impl->mFlags&OBJECT_LIFETIME_WEAK) == OBJECT_LIFETIME_WEAK
469 && impl->mBase->onIncStrongAttempted(FIRST_INC_STRONG, id);
470 }
471 if (!allow) {
472 decWeak(id);
473 return false;
474 }
475 curCount = android_atomic_inc(&impl->mStrong);
476
477 // If the strong reference count has already been incremented by
478 // someone else, the implementor of onIncStrongAttempted() is holding
479 // an unneeded reference. So call onLastStrongRef() here to remove it.
480 // (No, this is not pretty.) Note that we MUST NOT do this if we
481 // are in fact acquiring the first reference.
482 if (curCount > 0 && curCount < INITIAL_STRONG_VALUE) {
483 impl->mBase->onLastStrongRef(id);
484 }
485 }
486
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800487 impl->addStrongRef(id);
488
489#if PRINT_REFS
Steve Blockeb095332011-12-20 16:23:08 +0000490 ALOGD("attemptIncStrong of %p from %p: cnt=%d\n", this, id, curCount);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800491#endif
492
493 if (curCount == INITIAL_STRONG_VALUE) {
494 android_atomic_add(-INITIAL_STRONG_VALUE, &impl->mStrong);
495 impl->mBase->onFirstRef();
496 }
497
498 return true;
499}
500
501bool RefBase::weakref_type::attemptIncWeak(const void* id)
502{
503 weakref_impl* const impl = static_cast<weakref_impl*>(this);
Mathias Agopianad099652011-08-10 21:07:02 -0700504
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800505 int32_t curCount = impl->mWeak;
506 LOG_ASSERT(curCount >= 0, "attemptIncWeak called on %p after underflow",
507 this);
508 while (curCount > 0) {
509 if (android_atomic_cmpxchg(curCount, curCount+1, &impl->mWeak) == 0) {
510 break;
511 }
512 curCount = impl->mWeak;
513 }
514
515 if (curCount > 0) {
516 impl->addWeakRef(id);
517 }
518
519 return curCount > 0;
520}
521
522int32_t RefBase::weakref_type::getWeakCount() const
523{
524 return static_cast<const weakref_impl*>(this)->mWeak;
525}
526
527void RefBase::weakref_type::printRefs() const
528{
529 static_cast<const weakref_impl*>(this)->printRefs();
530}
531
532void RefBase::weakref_type::trackMe(bool enable, bool retain)
533{
Mathias Agopianad099652011-08-10 21:07:02 -0700534 static_cast<weakref_impl*>(this)->trackMe(enable, retain);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800535}
536
537RefBase::weakref_type* RefBase::createWeak(const void* id) const
538{
539 mRefs->incWeak(id);
540 return mRefs;
541}
542
543RefBase::weakref_type* RefBase::getWeakRefs() const
544{
545 return mRefs;
546}
547
548RefBase::RefBase()
549 : mRefs(new weakref_impl(this))
550{
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800551}
552
553RefBase::~RefBase()
554{
Mathias Agopianad099652011-08-10 21:07:02 -0700555 if (mRefs->mStrong == INITIAL_STRONG_VALUE) {
556 // we never acquired a strong (and/or weak) reference on this object.
Mathias Agopian9c8fa9e2011-06-15 20:42:47 -0700557 delete mRefs;
Mathias Agopianad099652011-08-10 21:07:02 -0700558 } else {
559 // life-time of this object is extended to WEAK or FOREVER, in
560 // which case weakref_impl doesn't out-live the object and we
561 // can free it now.
562 if ((mRefs->mFlags & OBJECT_LIFETIME_MASK) != OBJECT_LIFETIME_STRONG) {
563 // It's possible that the weak count is not 0 if the object
564 // re-acquired a weak reference in its destructor
565 if (mRefs->mWeak == 0) {
566 delete mRefs;
567 }
568 }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800569 }
Mathias Agopianad099652011-08-10 21:07:02 -0700570 // for debugging purposes, clear this.
571 const_cast<weakref_impl*&>(mRefs) = NULL;
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800572}
573
574void RefBase::extendObjectLifetime(int32_t mode)
575{
576 android_atomic_or(mode, &mRefs->mFlags);
577}
578
579void RefBase::onFirstRef()
580{
581}
582
583void RefBase::onLastStrongRef(const void* /*id*/)
584{
585}
586
587bool RefBase::onIncStrongAttempted(uint32_t flags, const void* id)
588{
589 return (flags&FIRST_INC_STRONG) ? true : false;
590}
591
592void RefBase::onLastWeakRef(const void* /*id*/)
593{
594}
Mathias Agopianad099652011-08-10 21:07:02 -0700595
596// ---------------------------------------------------------------------------
597
598void RefBase::moveReferences(void* dst, void const* src, size_t n,
599 const ReferenceConverterBase& caster)
600{
601#if DEBUG_REFS
602 const size_t itemSize = caster.getReferenceTypeSize();
603 for (size_t i=0 ; i<n ; i++) {
604 void* d = reinterpret_cast<void *>(intptr_t(dst) + i*itemSize);
605 void const* s = reinterpret_cast<void const*>(intptr_t(src) + i*itemSize);
606 RefBase* ref(reinterpret_cast<RefBase*>(caster.getReferenceBase(d)));
607 ref->mRefs->renameStrongRefId(s, d);
608 ref->mRefs->renameWeakRefId(s, d);
609 }
610#endif
611}
612
613// ---------------------------------------------------------------------------
614
615TextOutput& printStrongPointer(TextOutput& to, const void* val)
616{
617 to << "sp<>(" << val << ")";
618 return to;
619}
620
621TextOutput& printWeakPointer(TextOutput& to, const void* val)
622{
623 to << "wp<>(" << val << ")";
624 return to;
625}
626
627
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800628}; // namespace android