在android中使用了sp(strong pointer) 和 wp(weak pointer)实现类似于java内存回收机制。

实现代码:

frameworks/native/libs/utils/RefBase.cpp

frameworks/native/include/utils/RefBase.h

frameworks/native/include/utils/StrongPointer.h

强引用为0时,实际的对象会被删除

弱引用为0时,weakref_impl会被删除


强引用

sp<T>::sp(T* other)
: m_ptr(other)
{
if (other) other->incStrong(this);
}

sp<T>::~sp()
{
if (m_ptr) m_ptr->decStrong(this);
}


void RefBase::incStrong(const void* id) const
{
weakref_impl* const refs = mRefs;

//弱引用计数器增1
refs->incWeak(id);

//这个只在DEBUG版本追踪时有用,现在可以不用看
refs->addStrongRef(id);

//强引用计数器增1

const int32_t c = android_atomic_inc(&refs->mStrong);
ALOG_ASSERT(c > 0, "incStrong() called on %p after last strong ref", refs);

//INITIAL_STRONG_VALUE的值(1<<28) ,当refs->mStrong第一次创建完后,以后就直接return了

if (c != INITIAL_STRONG_VALUE) {
return;
}

//第一次构造时,会最终使refs->mStrong == 1
android_atomic_add(-INITIAL_STRONG_VALUE, &refs->mStrong);
refs->mBase->onFirstRef();
}


void RefBase::decStrong(const void* id) const
{
weakref_impl* const refs = mRefs;

//和addStrongRef相对应
refs->removeStrongRef(id);

//强引用计数器减1

const int32_t c = android_atomic_dec(&refs->mStrong);

ALOG_ASSERT(c >= 1, "decStrong() called on %p too many times", refs);
if (c == 1) {
refs->mBase->onLastStrongRef(id);
if ((refs->mFlags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) {
// 当refs->mStrong == 0时,自杀删除对象

delete this;
}
}
//弱引用计数器减1

refs->decWeak(id);
}


RefBase::~RefBase()
{
if (mRefs->mStrong == INITIAL_STRONG_VALUE) {
// we never acquired a strong (and/or weak) reference on this object.
//没有使用强引用计数,直接删除weakref_impl

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;
}
}
}
// for debugging purposes, clear this.
const_cast<weakref_impl*&>(mRefs) = NULL;
}


更多相关文章

  1. Android接收RabbitMQ消息。
  2. 把这份关于Android(安卓)Binder原理一系列笔记研究完,进大厂是个
  3. android nfc中Ndef格式的读写
  4. Android消息传递机制Handler完全解析之4内存泄漏等问题
  5. 详解Android(安卓)消息处理机制
  6. Android消息机制底层原理
  7. Android(安卓)Handler的使用(二)
  8. android中wifi使用方法介绍
  9. Android中Timer类和TimerTask粗解

随机推荐

  1. 4.27 定界符与运算流程控制
  2. CSS盒模型/Box-szing功能/相对定位、绝对
  3. a.img.table标签用法
  4. php之动态分页
  5. 【自撸框架】PHP实战利用composer自撸一
  6. 变量与常量学习入门
  7. PHP:文件上传上传限制,文件大小不超过5M,
  8. 8.)PHPWeb开发框架~Apache配置虚拟主机
  9. 7.)PHPWeb开发框架~5 个使用 Composer 小
  10. win7盘符误删的资料怎样恢复