Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2116748
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: Android平台

2014-05-13 14:36:11

<深入理解android>卷1 邓凡平著的 第5章 深入理解常见类
  1. class A: public RefBase
  2. {
  3. }
  4. int main()
  5. {
  6.     A* pA = new A;       //1.
  7.     sp<A> spA(pA);       //2.
  8.     wp<A> wpA(spA);
  9. }
1. new 的动作
1.1 执行RefBase的构造函数
其中类A继承自RefBase, 所以new A时,实际上执行了如下代码.
/work/ct/android42/frameworks/native/libs/utils/RefBase.cpp
  1. RefBase::RefBase()
  2.     : mRefs(new weakref_impl(this))
  3. {
  4. }
在RefBase的构造函数中又调用了weakref_impl的构造函数

1.2 执行RefBase的构造函数

  1. weakref_impl(RefBase* base)
  2.         : mStrong(INITIAL_STRONG_VALUE)
  3.         , mWeak(0)
  4.         , mBase(base)
  5.         , mFlags(0)
  6.         , mStrongRefs(NULL)
  7.         , mWeakRefs(NULL)
  8.         , mTrackEnabled(!!DEBUG_REFS_ENABLED_BY_DEFAULT)
  9.         , mRetain(false)
  10.     {
  11.     }

1.3 关于wreakref_impl的说明
RefBase的内部类weakref_impl的定义如下:
在/work/ct/android42/frameworks/native/libs/utils/RefBase.cpp中
  1. class RefBase::weakref_impl : public RefBase::weakref_type
  2. {
  3.     ...//省略
  4. }
这是在定义了一个RefBase的一个内部类weakref_impl,
同时weakref_impl是继承自RefBase的另一个内部类weakref_type
这需要看一下class RefBase的定义
在/work/ct/android42/frameworks/native/include/utils/RefBase.h中
2. sp<A> spA(pA)分析
sp的类定义及构造函数都在/work/ct/android42/frameworks/native/include/utils/StrongPointer.h中
  1. template<typename T>
  2. sp<T>::sp(T* other)
  3. : m_ptr(other)
  4.   {
  5.     if (other) other->incStrong(this);
  6.   }
2.1 incStrong 将mStrong计数+1
  1. void RefBase::incStrong(const void* id) const
  2. {
  3.     weakref_impl* const refs = mRefs;    //weakref_impl* const mRefs;
  4.     refs->incWeak(id);//将mWeak计数+1
  5.     
  6.     refs->addStrongRef(id);   //
  7.     //如是是第一次执行,因为mStrong初始化为INITIAL_STRONG_VALUE=10000,
  8.     //需要把这个INITIAL_STRONG_VALUE减掉, 不是第一次执行就直接将mStrong计数+1
  9.     const int32_t c = android_atomic_inc(&refs->mStrong);
  10.     if(c != INITIAL_STRONG_VALUE)
  11.         return ;
  12.     android_atomic_add(-INITIAL_STRONG_VALUE, &refs->mStrong);
  13.     refs->mBase->onFirstRef();    //执行一些初始化操作,这里略过分析
  14. }
2.1.1 incWeak 将mWeak计数+1
  1. void RefBase::weakref_type::incWeak(const void* id)
  2. {
  3.     weakref_impl* const impl = static_cast<weakref_impl*>(this);
  4.     impl->addWeakRef(id);    //空
  5.     const int32_t c = android_atomic_inc(&impl->mWeak);  //将mWeak计数+1
  6. }
执行sp<A> spA(pA)后mStrong与mWeak都加了1

3.wp<A> wpA(spA)分析
在/work/ct/android42/frameworks/native/include/utils/RefBase.h中
  1. template<typename T>
  2. wp<T>::wp(const sp<T>& other)
  3.     : m_ptr(other.m_ptr)
  4. {
  5.     if (m_ptr) {
  6.         m_refs = m_ptr->createWeak(this); //将mWeak计数+1
  7.     }
  8. }

在/work/ct/android42/frameworks/native/libs/utils/RefBase.cpp中
  1. RefBase::weakref_type* RefBase::createWeak(const void* id) const
  2. {
  3.     mRefs->incWeak(id); //将mWeak计数+1
  4.     return mRefs;
  5. }
执行wp<A> wpA(spA)只有mWeak加了1







阅读(1658) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~