2009-03-04 03:31:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2005 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LOG_TAG "RefBase"
|
2013-03-20 00:36:57 +00:00
|
|
|
// #define LOG_NDEBUG 0
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
#include <utils/RefBase.h>
|
|
|
|
|
|
|
|
#include <utils/Atomic.h>
|
|
|
|
#include <utils/CallStack.h>
|
|
|
|
#include <utils/Log.h>
|
|
|
|
#include <utils/threads.h>
|
2011-02-16 23:23:08 +00:00
|
|
|
#include <utils/TextOutput.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <typeinfo>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
// compile with refcounting debugging enabled
|
|
|
|
#define DEBUG_REFS 0
|
2013-03-19 03:31:18 +00:00
|
|
|
|
|
|
|
// whether ref-tracking is enabled by default, if not, trackMe(true, false)
|
|
|
|
// needs to be called explicitly
|
|
|
|
#define DEBUG_REFS_ENABLED_BY_DEFAULT 0
|
|
|
|
|
|
|
|
// whether callstack are collected (significantly slows things down)
|
2009-03-04 03:31:44 +00:00
|
|
|
#define DEBUG_REFS_CALLSTACK_ENABLED 1
|
|
|
|
|
2013-03-19 03:31:18 +00:00
|
|
|
// folder where stack traces are saved when DEBUG_REFS is enabled
|
|
|
|
// this folder needs to exist and be writable
|
|
|
|
#define DEBUG_REFS_CALLSTACK_PATH "/data/debug"
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
// log all reference counting operations
|
|
|
|
#define PRINT_REFS 0
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
|
|
|
#define INITIAL_STRONG_VALUE (1<<28)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class RefBase::weakref_impl : public RefBase::weakref_type
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
volatile int32_t mStrong;
|
|
|
|
volatile int32_t mWeak;
|
|
|
|
RefBase* const mBase;
|
|
|
|
volatile int32_t mFlags;
|
|
|
|
|
|
|
|
#if !DEBUG_REFS
|
|
|
|
|
|
|
|
weakref_impl(RefBase* base)
|
|
|
|
: mStrong(INITIAL_STRONG_VALUE)
|
|
|
|
, mWeak(0)
|
|
|
|
, mBase(base)
|
|
|
|
, mFlags(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void addStrongRef(const void* /*id*/) { }
|
|
|
|
void removeStrongRef(const void* /*id*/) { }
|
2011-02-17 04:23:43 +00:00
|
|
|
void renameStrongRefId(const void* /*old_id*/, const void* /*new_id*/) { }
|
2009-03-04 03:31:44 +00:00
|
|
|
void addWeakRef(const void* /*id*/) { }
|
|
|
|
void removeWeakRef(const void* /*id*/) { }
|
2011-02-17 04:23:43 +00:00
|
|
|
void renameWeakRefId(const void* /*old_id*/, const void* /*new_id*/) { }
|
2009-03-04 03:31:44 +00:00
|
|
|
void printRefs() const { }
|
|
|
|
void trackMe(bool, bool) { }
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
weakref_impl(RefBase* base)
|
|
|
|
: mStrong(INITIAL_STRONG_VALUE)
|
|
|
|
, mWeak(0)
|
|
|
|
, mBase(base)
|
|
|
|
, mFlags(0)
|
|
|
|
, mStrongRefs(NULL)
|
|
|
|
, mWeakRefs(NULL)
|
|
|
|
, mTrackEnabled(!!DEBUG_REFS_ENABLED_BY_DEFAULT)
|
|
|
|
, mRetain(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~weakref_impl()
|
|
|
|
{
|
2011-02-17 04:23:43 +00:00
|
|
|
bool dumpStack = false;
|
|
|
|
if (!mRetain && mStrongRefs != NULL) {
|
|
|
|
dumpStack = true;
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("Strong references remain:");
|
2011-02-26 00:11:44 +00:00
|
|
|
ref_entry* refs = mStrongRefs;
|
|
|
|
while (refs) {
|
|
|
|
char inc = refs->ref >= 0 ? '+' : '-';
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref);
|
2011-02-26 00:11:44 +00:00
|
|
|
#if DEBUG_REFS_CALLSTACK_ENABLED
|
2013-03-22 00:12:40 +00:00
|
|
|
refs->stack.dump(LOG_TAG);
|
2011-08-11 04:07:02 +00:00
|
|
|
#endif
|
2011-02-26 00:11:44 +00:00
|
|
|
refs = refs->next;
|
|
|
|
}
|
2011-02-17 04:23:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mRetain && mWeakRefs != NULL) {
|
|
|
|
dumpStack = true;
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("Weak references remain!");
|
2011-02-26 00:11:44 +00:00
|
|
|
ref_entry* refs = mWeakRefs;
|
|
|
|
while (refs) {
|
|
|
|
char inc = refs->ref >= 0 ? '+' : '-';
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("\t%c ID %p (ref %d):", inc, refs->id, refs->ref);
|
2011-02-26 00:11:44 +00:00
|
|
|
#if DEBUG_REFS_CALLSTACK_ENABLED
|
2013-03-22 00:12:40 +00:00
|
|
|
refs->stack.dump(LOG_TAG);
|
2011-08-11 04:07:02 +00:00
|
|
|
#endif
|
2011-02-26 00:11:44 +00:00
|
|
|
refs = refs->next;
|
|
|
|
}
|
2011-02-17 04:23:43 +00:00
|
|
|
}
|
|
|
|
if (dumpStack) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("above errors at:");
|
2013-03-22 00:12:40 +00:00
|
|
|
CallStack stack(LOG_TAG);
|
2011-02-17 04:23:43 +00:00
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2011-02-17 04:23:43 +00:00
|
|
|
void addStrongRef(const void* id) {
|
2011-12-20 16:23:08 +00:00
|
|
|
//ALOGD_IF(mTrackEnabled,
|
2011-02-17 04:23:43 +00:00
|
|
|
// "addStrongRef: RefBase=%p, id=%p", mBase, id);
|
2009-03-04 03:31:44 +00:00
|
|
|
addRef(&mStrongRefs, id, mStrong);
|
|
|
|
}
|
|
|
|
|
2011-02-17 04:23:43 +00:00
|
|
|
void removeStrongRef(const void* id) {
|
2011-12-20 16:23:08 +00:00
|
|
|
//ALOGD_IF(mTrackEnabled,
|
2011-02-17 04:23:43 +00:00
|
|
|
// "removeStrongRef: RefBase=%p, id=%p", mBase, id);
|
|
|
|
if (!mRetain) {
|
2009-03-04 03:31:44 +00:00
|
|
|
removeRef(&mStrongRefs, id);
|
2011-02-17 04:23:43 +00:00
|
|
|
} else {
|
2009-03-04 03:31:44 +00:00
|
|
|
addRef(&mStrongRefs, id, -mStrong);
|
2011-02-17 04:23:43 +00:00
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2011-02-17 04:23:43 +00:00
|
|
|
void renameStrongRefId(const void* old_id, const void* new_id) {
|
2011-12-20 16:23:08 +00:00
|
|
|
//ALOGD_IF(mTrackEnabled,
|
2011-02-17 04:23:43 +00:00
|
|
|
// "renameStrongRefId: RefBase=%p, oid=%p, nid=%p",
|
|
|
|
// mBase, old_id, new_id);
|
|
|
|
renameRefsId(mStrongRefs, old_id, new_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void addWeakRef(const void* id) {
|
2009-03-04 03:31:44 +00:00
|
|
|
addRef(&mWeakRefs, id, mWeak);
|
|
|
|
}
|
|
|
|
|
2011-02-17 04:23:43 +00:00
|
|
|
void removeWeakRef(const void* id) {
|
|
|
|
if (!mRetain) {
|
2009-03-04 03:31:44 +00:00
|
|
|
removeRef(&mWeakRefs, id);
|
2011-02-17 04:23:43 +00:00
|
|
|
} else {
|
2009-03-04 03:31:44 +00:00
|
|
|
addRef(&mWeakRefs, id, -mWeak);
|
2011-02-17 04:23:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void renameWeakRefId(const void* old_id, const void* new_id) {
|
|
|
|
renameRefsId(mWeakRefs, old_id, new_id);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void trackMe(bool track, bool retain)
|
|
|
|
{
|
|
|
|
mTrackEnabled = track;
|
|
|
|
mRetain = retain;
|
|
|
|
}
|
|
|
|
|
|
|
|
void printRefs() const
|
|
|
|
{
|
|
|
|
String8 text;
|
|
|
|
|
|
|
|
{
|
2011-08-11 04:07:02 +00:00
|
|
|
Mutex::Autolock _l(mMutex);
|
2009-03-04 03:31:44 +00:00
|
|
|
char buf[128];
|
|
|
|
sprintf(buf, "Strong references on RefBase %p (weakref_type %p):\n", mBase, this);
|
|
|
|
text.append(buf);
|
|
|
|
printRefsLocked(&text, mStrongRefs);
|
|
|
|
sprintf(buf, "Weak references on RefBase %p (weakref_type %p):\n", mBase, this);
|
|
|
|
text.append(buf);
|
|
|
|
printRefsLocked(&text, mWeakRefs);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
char name[100];
|
2013-03-19 03:31:18 +00:00
|
|
|
snprintf(name, 100, DEBUG_REFS_CALLSTACK_PATH "/%p.stack", this);
|
2013-03-07 01:51:15 +00:00
|
|
|
int rc = open(name, O_RDWR | O_CREAT | O_APPEND, 644);
|
2009-03-04 03:31:44 +00:00
|
|
|
if (rc >= 0) {
|
|
|
|
write(rc, text.string(), text.length());
|
|
|
|
close(rc);
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("STACK TRACE for %p saved in %s", this, name);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2012-01-06 19:20:56 +00:00
|
|
|
else ALOGE("FAILED TO PRINT STACK TRACE for %p in %s: %s", this,
|
2009-03-04 03:31:44 +00:00
|
|
|
name, strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct ref_entry
|
|
|
|
{
|
|
|
|
ref_entry* next;
|
|
|
|
const void* id;
|
|
|
|
#if DEBUG_REFS_CALLSTACK_ENABLED
|
|
|
|
CallStack stack;
|
|
|
|
#endif
|
|
|
|
int32_t ref;
|
|
|
|
};
|
|
|
|
|
|
|
|
void addRef(ref_entry** refs, const void* id, int32_t mRef)
|
|
|
|
{
|
|
|
|
if (mTrackEnabled) {
|
|
|
|
AutoMutex _l(mMutex);
|
2011-02-17 04:23:43 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
ref_entry* ref = new ref_entry;
|
|
|
|
// Reference count at the time of the snapshot, but before the
|
|
|
|
// update. Positive value means we increment, negative--we
|
|
|
|
// decrement the reference count.
|
|
|
|
ref->ref = mRef;
|
|
|
|
ref->id = id;
|
|
|
|
#if DEBUG_REFS_CALLSTACK_ENABLED
|
|
|
|
ref->stack.update(2);
|
|
|
|
#endif
|
|
|
|
ref->next = *refs;
|
|
|
|
*refs = ref;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeRef(ref_entry** refs, const void* id)
|
|
|
|
{
|
|
|
|
if (mTrackEnabled) {
|
|
|
|
AutoMutex _l(mMutex);
|
|
|
|
|
2011-02-26 00:11:44 +00:00
|
|
|
ref_entry* const head = *refs;
|
|
|
|
ref_entry* ref = head;
|
2009-03-04 03:31:44 +00:00
|
|
|
while (ref != NULL) {
|
|
|
|
if (ref->id == id) {
|
|
|
|
*refs = ref->next;
|
|
|
|
delete ref;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
refs = &ref->next;
|
|
|
|
ref = *refs;
|
|
|
|
}
|
2011-02-17 04:23:43 +00:00
|
|
|
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("RefBase: removing id %p on RefBase %p"
|
2011-02-17 04:23:43 +00:00
|
|
|
"(weakref_type %p) that doesn't exist!",
|
|
|
|
id, mBase, this);
|
|
|
|
|
2011-02-26 00:11:44 +00:00
|
|
|
ref = head;
|
|
|
|
while (ref) {
|
|
|
|
char inc = ref->ref >= 0 ? '+' : '-';
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("\t%c ID %p (ref %d):", inc, ref->id, ref->ref);
|
2011-02-26 00:11:44 +00:00
|
|
|
ref = ref->next;
|
|
|
|
}
|
|
|
|
|
2013-03-22 00:12:40 +00:00
|
|
|
CallStack stack(LOG_TAG);
|
2011-02-17 04:23:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void renameRefsId(ref_entry* r, const void* old_id, const void* new_id)
|
|
|
|
{
|
|
|
|
if (mTrackEnabled) {
|
|
|
|
AutoMutex _l(mMutex);
|
|
|
|
ref_entry* ref = r;
|
|
|
|
while (ref != NULL) {
|
|
|
|
if (ref->id == old_id) {
|
|
|
|
ref->id = new_id;
|
|
|
|
}
|
|
|
|
ref = ref->next;
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void printRefsLocked(String8* out, const ref_entry* refs) const
|
|
|
|
{
|
|
|
|
char buf[128];
|
|
|
|
while (refs) {
|
|
|
|
char inc = refs->ref >= 0 ? '+' : '-';
|
|
|
|
sprintf(buf, "\t%c ID %p (ref %d):\n",
|
|
|
|
inc, refs->id, refs->ref);
|
|
|
|
out->append(buf);
|
|
|
|
#if DEBUG_REFS_CALLSTACK_ENABLED
|
|
|
|
out->append(refs->stack.toString("\t\t"));
|
|
|
|
#else
|
|
|
|
out->append("\t\t(call stacks disabled)");
|
|
|
|
#endif
|
|
|
|
refs = refs->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-11 04:07:02 +00:00
|
|
|
mutable Mutex mMutex;
|
2009-03-04 03:31:44 +00:00
|
|
|
ref_entry* mStrongRefs;
|
|
|
|
ref_entry* mWeakRefs;
|
|
|
|
|
|
|
|
bool mTrackEnabled;
|
|
|
|
// Collect stack traces on addref and removeref, instead of deleting the stack references
|
|
|
|
// on removeref that match the address ones.
|
|
|
|
bool mRetain;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void RefBase::incStrong(const void* id) const
|
|
|
|
{
|
|
|
|
weakref_impl* const refs = mRefs;
|
|
|
|
refs->incWeak(id);
|
|
|
|
|
|
|
|
refs->addStrongRef(id);
|
|
|
|
const int32_t c = android_atomic_inc(&refs->mStrong);
|
2012-01-09 18:35:44 +00:00
|
|
|
ALOG_ASSERT(c > 0, "incStrong() called on %p after last strong ref", refs);
|
2009-03-04 03:31:44 +00:00
|
|
|
#if PRINT_REFS
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("incStrong of %p from %p: cnt=%d\n", this, id, c);
|
2009-03-04 03:31:44 +00:00
|
|
|
#endif
|
|
|
|
if (c != INITIAL_STRONG_VALUE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
android_atomic_add(-INITIAL_STRONG_VALUE, &refs->mStrong);
|
2011-08-11 04:07:02 +00:00
|
|
|
refs->mBase->onFirstRef();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RefBase::decStrong(const void* id) const
|
|
|
|
{
|
|
|
|
weakref_impl* const refs = mRefs;
|
|
|
|
refs->removeStrongRef(id);
|
|
|
|
const int32_t c = android_atomic_dec(&refs->mStrong);
|
|
|
|
#if PRINT_REFS
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("decStrong of %p from %p: cnt=%d\n", this, id, c);
|
2009-03-04 03:31:44 +00:00
|
|
|
#endif
|
2012-01-09 18:35:44 +00:00
|
|
|
ALOG_ASSERT(c >= 1, "decStrong() called on %p too many times", refs);
|
2009-03-04 03:31:44 +00:00
|
|
|
if (c == 1) {
|
2011-08-11 04:07:02 +00:00
|
|
|
refs->mBase->onLastStrongRef(id);
|
|
|
|
if ((refs->mFlags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) {
|
|
|
|
delete this;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
refs->decWeak(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RefBase::forceIncStrong(const void* id) const
|
|
|
|
{
|
|
|
|
weakref_impl* const refs = mRefs;
|
|
|
|
refs->incWeak(id);
|
|
|
|
|
|
|
|
refs->addStrongRef(id);
|
|
|
|
const int32_t c = android_atomic_inc(&refs->mStrong);
|
2012-01-09 18:35:44 +00:00
|
|
|
ALOG_ASSERT(c >= 0, "forceIncStrong called on %p after ref count underflow",
|
2009-03-04 03:31:44 +00:00
|
|
|
refs);
|
|
|
|
#if PRINT_REFS
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("forceIncStrong of %p from %p: cnt=%d\n", this, id, c);
|
2009-03-04 03:31:44 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case INITIAL_STRONG_VALUE:
|
|
|
|
android_atomic_add(-INITIAL_STRONG_VALUE, &refs->mStrong);
|
|
|
|
// fall through...
|
|
|
|
case 0:
|
2011-08-11 04:07:02 +00:00
|
|
|
refs->mBase->onFirstRef();
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t RefBase::getStrongCount() const
|
|
|
|
{
|
|
|
|
return mRefs->mStrong;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefBase* RefBase::weakref_type::refBase() const
|
|
|
|
{
|
|
|
|
return static_cast<const weakref_impl*>(this)->mBase;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RefBase::weakref_type::incWeak(const void* id)
|
|
|
|
{
|
|
|
|
weakref_impl* const impl = static_cast<weakref_impl*>(this);
|
|
|
|
impl->addWeakRef(id);
|
|
|
|
const int32_t c = android_atomic_inc(&impl->mWeak);
|
2012-01-09 18:35:44 +00:00
|
|
|
ALOG_ASSERT(c >= 0, "incWeak called on %p after last weak ref", this);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2011-08-11 04:07:02 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
void RefBase::weakref_type::decWeak(const void* id)
|
|
|
|
{
|
|
|
|
weakref_impl* const impl = static_cast<weakref_impl*>(this);
|
|
|
|
impl->removeWeakRef(id);
|
|
|
|
const int32_t c = android_atomic_dec(&impl->mWeak);
|
2012-01-09 18:35:44 +00:00
|
|
|
ALOG_ASSERT(c >= 1, "decWeak called on %p too many times", this);
|
2009-03-04 03:31:44 +00:00
|
|
|
if (c != 1) return;
|
2011-08-11 04:07:02 +00:00
|
|
|
|
|
|
|
if ((impl->mFlags&OBJECT_LIFETIME_WEAK) == OBJECT_LIFETIME_STRONG) {
|
|
|
|
// This is the regular lifetime case. The object is destroyed
|
|
|
|
// when the last strong reference goes away. Since weakref_impl
|
|
|
|
// outlive the object, it is not destroyed in the dtor, and
|
|
|
|
// we'll have to do it here.
|
2011-06-08 23:05:30 +00:00
|
|
|
if (impl->mStrong == INITIAL_STRONG_VALUE) {
|
2011-08-11 04:07:02 +00:00
|
|
|
// Special case: we never had a strong reference, so we need to
|
|
|
|
// destroy the object now.
|
|
|
|
delete impl->mBase;
|
2011-06-08 23:05:30 +00:00
|
|
|
} else {
|
2011-10-20 10:56:00 +00:00
|
|
|
// ALOGV("Freeing refs %p of old RefBase %p\n", this, impl->mBase);
|
2009-03-04 03:31:44 +00:00
|
|
|
delete impl;
|
|
|
|
}
|
|
|
|
} else {
|
2011-08-11 04:07:02 +00:00
|
|
|
// less common case: lifetime is OBJECT_LIFETIME_{WEAK|FOREVER}
|
2009-03-04 03:31:44 +00:00
|
|
|
impl->mBase->onLastWeakRef(id);
|
2011-08-11 04:07:02 +00:00
|
|
|
if ((impl->mFlags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_WEAK) {
|
|
|
|
// this is the OBJECT_LIFETIME_WEAK case. The last weak-reference
|
|
|
|
// is gone, we can destroy the object.
|
|
|
|
delete impl->mBase;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RefBase::weakref_type::attemptIncStrong(const void* id)
|
|
|
|
{
|
|
|
|
incWeak(id);
|
|
|
|
|
|
|
|
weakref_impl* const impl = static_cast<weakref_impl*>(this);
|
|
|
|
int32_t curCount = impl->mStrong;
|
2013-03-14 22:26:30 +00:00
|
|
|
|
|
|
|
ALOG_ASSERT(curCount >= 0,
|
|
|
|
"attemptIncStrong called on %p after underflow", this);
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
while (curCount > 0 && curCount != INITIAL_STRONG_VALUE) {
|
2013-03-14 22:26:30 +00:00
|
|
|
// we're in the easy/common case of promoting a weak-reference
|
|
|
|
// from an existing strong reference.
|
2009-03-04 03:31:44 +00:00
|
|
|
if (android_atomic_cmpxchg(curCount, curCount+1, &impl->mStrong) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2013-03-14 22:26:30 +00:00
|
|
|
// the strong count has changed on us, we need to re-assert our
|
|
|
|
// situation.
|
2009-03-04 03:31:44 +00:00
|
|
|
curCount = impl->mStrong;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (curCount <= 0 || curCount == INITIAL_STRONG_VALUE) {
|
2013-03-14 22:26:30 +00:00
|
|
|
// we're now in the harder case of either:
|
|
|
|
// - there never was a strong reference on us
|
|
|
|
// - or, all strong references have been released
|
|
|
|
if ((impl->mFlags&OBJECT_LIFETIME_WEAK) == OBJECT_LIFETIME_STRONG) {
|
|
|
|
// this object has a "normal" life-time, i.e.: it gets destroyed
|
|
|
|
// when the last strong reference goes away
|
|
|
|
if (curCount <= 0) {
|
|
|
|
// the last strong-reference got released, the object cannot
|
|
|
|
// be revived.
|
|
|
|
decWeak(id);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// here, curCount == INITIAL_STRONG_VALUE, which means
|
|
|
|
// there never was a strong-reference, so we can try to
|
|
|
|
// promote this object; we need to do that atomically.
|
|
|
|
while (curCount > 0) {
|
|
|
|
if (android_atomic_cmpxchg(curCount, curCount + 1,
|
|
|
|
&impl->mStrong) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// the strong count has changed on us, we need to re-assert our
|
|
|
|
// situation (e.g.: another thread has inc/decStrong'ed us)
|
|
|
|
curCount = impl->mStrong;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (curCount <= 0) {
|
|
|
|
// promote() failed, some other thread destroyed us in the
|
|
|
|
// meantime (i.e.: strong count reached zero).
|
|
|
|
decWeak(id);
|
|
|
|
return false;
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
} else {
|
2013-03-14 22:26:30 +00:00
|
|
|
// this object has an "extended" life-time, i.e.: it can be
|
|
|
|
// revived from a weak-reference only.
|
|
|
|
// Ask the object's implementation if it agrees to be revived
|
|
|
|
if (!impl->mBase->onIncStrongAttempted(FIRST_INC_STRONG, id)) {
|
|
|
|
// it didn't so give-up.
|
|
|
|
decWeak(id);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// grab a strong-reference, which is always safe due to the
|
|
|
|
// extended life-time.
|
|
|
|
curCount = android_atomic_inc(&impl->mStrong);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the strong reference count has already been incremented by
|
|
|
|
// someone else, the implementor of onIncStrongAttempted() is holding
|
|
|
|
// an unneeded reference. So call onLastStrongRef() here to remove it.
|
|
|
|
// (No, this is not pretty.) Note that we MUST NOT do this if we
|
|
|
|
// are in fact acquiring the first reference.
|
|
|
|
if (curCount > 0 && curCount < INITIAL_STRONG_VALUE) {
|
|
|
|
impl->mBase->onLastStrongRef(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl->addStrongRef(id);
|
|
|
|
|
|
|
|
#if PRINT_REFS
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("attemptIncStrong of %p from %p: cnt=%d\n", this, id, curCount);
|
2009-03-04 03:31:44 +00:00
|
|
|
#endif
|
|
|
|
|
2013-03-14 22:26:30 +00:00
|
|
|
// now we need to fix-up the count if it was INITIAL_STRONG_VALUE
|
|
|
|
// this must be done safely, i.e.: handle the case where several threads
|
|
|
|
// were here in attemptIncStrong().
|
|
|
|
curCount = impl->mStrong;
|
|
|
|
while (curCount >= INITIAL_STRONG_VALUE) {
|
|
|
|
ALOG_ASSERT(curCount > INITIAL_STRONG_VALUE,
|
|
|
|
"attemptIncStrong in %p underflowed to INITIAL_STRONG_VALUE",
|
|
|
|
this);
|
|
|
|
if (android_atomic_cmpxchg(curCount, curCount-INITIAL_STRONG_VALUE,
|
|
|
|
&impl->mStrong) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// the strong-count changed on us, we need to re-assert the situation,
|
|
|
|
// for e.g.: it's possible the fix-up happened in another thread.
|
|
|
|
curCount = impl->mStrong;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2013-03-14 22:26:30 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RefBase::weakref_type::attemptIncWeak(const void* id)
|
|
|
|
{
|
|
|
|
weakref_impl* const impl = static_cast<weakref_impl*>(this);
|
2011-02-17 04:23:43 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
int32_t curCount = impl->mWeak;
|
2012-01-09 18:35:44 +00:00
|
|
|
ALOG_ASSERT(curCount >= 0, "attemptIncWeak called on %p after underflow",
|
2009-03-04 03:31:44 +00:00
|
|
|
this);
|
|
|
|
while (curCount > 0) {
|
|
|
|
if (android_atomic_cmpxchg(curCount, curCount+1, &impl->mWeak) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
curCount = impl->mWeak;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (curCount > 0) {
|
|
|
|
impl->addWeakRef(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return curCount > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t RefBase::weakref_type::getWeakCount() const
|
|
|
|
{
|
|
|
|
return static_cast<const weakref_impl*>(this)->mWeak;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RefBase::weakref_type::printRefs() const
|
|
|
|
{
|
|
|
|
static_cast<const weakref_impl*>(this)->printRefs();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RefBase::weakref_type::trackMe(bool enable, bool retain)
|
|
|
|
{
|
2011-04-22 18:13:35 +00:00
|
|
|
static_cast<weakref_impl*>(this)->trackMe(enable, retain);
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RefBase::weakref_type* RefBase::createWeak(const void* id) const
|
|
|
|
{
|
|
|
|
mRefs->incWeak(id);
|
|
|
|
return mRefs;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefBase::weakref_type* RefBase::getWeakRefs() const
|
|
|
|
{
|
|
|
|
return mRefs;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefBase::RefBase()
|
|
|
|
: mRefs(new weakref_impl(this))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
RefBase::~RefBase()
|
|
|
|
{
|
2011-08-11 04:07:02 +00:00
|
|
|
if (mRefs->mStrong == INITIAL_STRONG_VALUE) {
|
|
|
|
// we never acquired a strong (and/or weak) reference on this object.
|
|
|
|
delete mRefs;
|
|
|
|
} else {
|
|
|
|
// life-time of this object is extended to WEAK or FOREVER, in
|
|
|
|
// which case weakref_impl doesn't out-live the object and we
|
|
|
|
// can free it now.
|
|
|
|
if ((mRefs->mFlags & OBJECT_LIFETIME_MASK) != OBJECT_LIFETIME_STRONG) {
|
|
|
|
// It's possible that the weak count is not 0 if the object
|
|
|
|
// re-acquired a weak reference in its destructor
|
|
|
|
if (mRefs->mWeak == 0) {
|
|
|
|
delete mRefs;
|
|
|
|
}
|
2011-06-08 23:05:30 +00:00
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
2011-08-11 04:07:02 +00:00
|
|
|
// for debugging purposes, clear this.
|
|
|
|
const_cast<weakref_impl*&>(mRefs) = NULL;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RefBase::extendObjectLifetime(int32_t mode)
|
|
|
|
{
|
|
|
|
android_atomic_or(mode, &mRefs->mFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RefBase::onFirstRef()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void RefBase::onLastStrongRef(const void* /*id*/)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RefBase::onIncStrongAttempted(uint32_t flags, const void* id)
|
|
|
|
{
|
|
|
|
return (flags&FIRST_INC_STRONG) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RefBase::onLastWeakRef(const void* /*id*/)
|
|
|
|
{
|
|
|
|
}
|
2011-02-16 23:23:08 +00:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2013-03-19 05:27:41 +00:00
|
|
|
void RefBase::renameRefs(size_t n, const ReferenceRenamer& renamer) {
|
2011-02-17 04:23:43 +00:00
|
|
|
#if DEBUG_REFS
|
|
|
|
for (size_t i=0 ; i<n ; i++) {
|
2013-03-19 05:27:41 +00:00
|
|
|
renamer(i);
|
2011-02-17 04:23:43 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-03-19 05:27:41 +00:00
|
|
|
void RefBase::renameRefId(weakref_type* ref,
|
|
|
|
const void* old_id, const void* new_id) {
|
|
|
|
weakref_impl* const impl = static_cast<weakref_impl*>(ref);
|
|
|
|
impl->renameStrongRefId(old_id, new_id);
|
|
|
|
impl->renameWeakRefId(old_id, new_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RefBase::renameRefId(RefBase* ref,
|
|
|
|
const void* old_id, const void* new_id) {
|
|
|
|
ref->mRefs->renameStrongRefId(old_id, new_id);
|
|
|
|
ref->mRefs->renameWeakRefId(old_id, new_id);
|
|
|
|
}
|
|
|
|
|
2011-02-17 04:23:43 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2011-02-16 23:23:08 +00:00
|
|
|
TextOutput& printStrongPointer(TextOutput& to, const void* val)
|
|
|
|
{
|
|
|
|
to << "sp<>(" << val << ")";
|
|
|
|
return to;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextOutput& printWeakPointer(TextOutput& to, const void* val)
|
|
|
|
{
|
|
|
|
to << "wp<>(" << val << ")";
|
|
|
|
return to;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
}; // namespace android
|