Android 智能指针总结

来源:互联网 发布:产品运营数据分析 编辑:程序博客网 时间:2024/05/11 16:11

这3种指针,在这篇博客http://blog.csdn.net/luoshengyang/article/details/6786239中讲的比较详细。我们这边自己再重新梳理,总结下。

这3种指针都是基于计数器的原理完成的。


一、轻量级指针

我们先来看第一个,LightRefBase类的实现在system/core/include/utils/Refbase.h中实现的

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. template <class T>  
  2. class LightRefBase  
  3. {  
  4. public:  
  5.     inline LightRefBase() : mCount(0) { }  
  6.     inline void incStrong(__attribute__((unused)) const void* id) const {  
  7.         android_atomic_inc(&mCount);  
  8.     }  
  9.     inline void decStrong(__attribute__((unused)) const void* id) const {  
  10.         if (android_atomic_dec(&mCount) == 1) {  
  11.             delete static_cast<const T*>(this);  
  12.         }  
  13.     }  
  14.     //! DEBUGGING ONLY: Get current strong ref count.  
  15.     inline int32_t getStrongCount() const {  
  16.         return mCount;  
  17.     }  
  18.   
  19.     typedef LightRefBase<T> basetype;  
  20.   
  21. protected:  
  22.     inline ~LightRefBase() { }  
  23.   
  24. private:  
  25.     friend class ReferenceMover;  
  26.     inline static void renameRefs(size_t n, const ReferenceRenamer& renamer) { }  
  27.     inline static void renameRefId(T* ref,  
  28.             const void* old_id, const void* new_id) { }  
  29.   
  30. private:  
  31.     mutable volatile int32_t mCount;  
  32. };  

这个类的mCount变量就是一个计数的功能,incStrong和decStrong分别计数加1 减1.这里都是使用的原子操作。这两个函数都是给智能指针调用的,当调用decStrong函数时,当计数为1时,直接就delete这个对象了。这里delete this,因为对象使用的时候肯定要继承LightRefBase这个类,所有这里delete this就是delete最后的对象。

而上面说的智能指针,这里说的就是sp了,在system/core/include/utils/StrongPoint.h中

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. template<typename T>  
  2. class sp {  
  3. public:  
  4.     inline sp() : m_ptr(0) { }  
  5.   
  6.     sp(T* other);  
  7.     sp(const sp<T>& other);  
  8.     template<typename U> sp(U* other);  
  9.     template<typename U> sp(const sp<U>& other);  
  10.   
  11.     ~sp();  
  12.   
  13.     // Assignment  
  14.   
  15.     sp& operator = (T* other);  
  16.     sp& operator = (const sp<T>& other);  
  17.   
  18.     template<typename U> sp& operator = (const sp<U>& other);  
  19.     template<typename U> sp& operator = (U* other);  
  20.   
  21.     //! Special optimization for use by ProcessState (and nobody else).  
  22.     void force_set(T* other);  
  23.   
  24.     // Reset  
  25.   
  26.     void clear();  
  27.   
  28.     // Accessors  
  29.   
  30.     inline  T&      operator* () const  { return *m_ptr; }  
  31.     inline  T*      operator-> () const { return m_ptr;  }  
  32.     inline  T*      get() const         { return m_ptr; }  
  33.   
  34.     // Operators  
  35.   
  36.     COMPARE(==)  
  37.     COMPARE(!=)  
  38.     COMPARE(>)  
  39.     COMPARE(<)  
  40.     COMPARE(<=)  
  41.     COMPARE(>=)  
  42.   
  43. private:      
  44.     template<typename Y> friend class sp;  
  45.     template<typename Y> friend class wp;  
  46.     void set_pointer(T* ptr);  
  47.     T* m_ptr;  
  48. };  

先看看其两个构造函数,m_ptr就是指的实际的对象,构造函数中直接调用对象的incStrong函数,另一个构造函数是以sp对象为参数的。

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. template<typename T>  
  2. sp<T>::sp(T* other)  
  3.         : m_ptr(other) {  
  4.     if (other)  
  5.         other->incStrong(this);  
  6. }  
  7.   
  8. template<typename T>  
  9. sp<T>::sp(const sp<T>& other)  
  10.         : m_ptr(other.m_ptr) {  
  11.     if (m_ptr)  
  12.         m_ptr->incStrong(this);  
  13. }  

再来看析构函数,就是调用了实际对象的decStrong函数。

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. template<typename T>  
  2. sp<T>::~sp() {  
  3.     if (m_ptr)  
  4.         m_ptr->decStrong(this);  
  5. }  

轻量级指针原理很简单,使用智能指针sp,sp的构造函数中调用对象的incStrong函数,析构函数中调用对象的decStrong函数。

下面我们举个例子:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>    
  2. #include <utils/RefBase.h>    
  3.     
  4. using namespace android;    
  5.     
  6. class LightClass : public LightRefBase<LightClass>    
  7. {    
  8. public:    
  9.         LightClass()    
  10.         {    
  11.                 printf("Construct LightClass Object.");    
  12.         }    
  13.     
  14.         virtual ~LightClass()    
  15.         {    
  16.                 printf("Destory LightClass Object.");    
  17.         }    
  18. };    
  19.     
  20. int main(int argc, char** argv)    
  21. {    
  22.         LightClass* pLightClass = new LightClass();    
  23.         sp<LightClass> lpOut = pLightClass;    
  24.     
  25.         printf("Light Ref Count: %d.\n", pLightClass->getStrongCount());    
  26.     
  27.         {    
  28.                 sp<LightClass> lpInner = lpOut;    
  29.     
  30.                 printf("Light Ref Count: %d.\n", pLightClass->getStrongCount());    
  31.         }    
  32.     
  33.         printf("Light Ref Count: %d.\n", pLightClass->getStrongCount());    
  34.     
  35.         return 0;    
  36. }    

我们创建一个自己的类LightClass,继承了LightRefBase模板类,这样类LightClass就具有引用计数的功能了。在main函数里面,我们首先new一个LightClass对象,然后把这个对象赋值给智能指针lpOut,这时候通过一个printf语句来将当前对象的引用计数值打印出来,从前面的分析可以看出,如果一切正常的话,这里打印出来的引用计数值为1。接着,我们又在两个大括号里面定义了另外一个智能指针lpInner,它通过lpOut间接地指向了前面我们所创建的对象,这时候再次将当前对象的引用计数值打印出来,从前面 的分析也可以看出,如果一切正常的话,这里打印出来的引用计数值应该为2。程序继承往下执行,当出了大括号的范围的时候,智能指针对象lpInner就被析构了,从前面的分析可以知道,智能指针在析构的时候,会减少当前对象的引用计数值,因此,最后一个printf语句打印出来的引用计数器值应该为1。当main函数执行完毕后,智能指针lpOut也会被析构,被析构时,它会再次减少当前对象的引用计数,这时候,对象的引用计数值就为0了,于是,它就会被delete了。

下面是Android.mk文件

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. LOCAL_PATH := $(call my-dir)    
  2. include $(CLEAR_VARS)    
  3. LOCAL_MODULE_TAGS := optional    
  4. LOCAL_MODULE := lightpointer    
  5. LOCAL_SRC_FILES := lightpointer.cpp    
  6. LOCAL_SHARED_LIBRARIES := \    
  7.         libcutils \    
  8.         libutils    
  9. include $(BUILD_EXECUTABLE)    

我们来看下结果:

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. Construct LightClass Object.    
  2. Light Ref Count: 1.    
  3. Light Ref Count: 2.    
  4. Light Ref Count: 1.    
  5. Destory LightClass Object.   


二、强指针

强指针所用的智能指针还是sp,但是计数器类是RefBase而不是LightRefBase,这个类就复杂多了。

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. class RefBase  
  2. {  
  3. public:  
  4.             void            incStrong(const void* id) const;  
  5.             void            decStrong(const void* id) const;  
  6.       
  7.             void            forceIncStrong(const void* id) const;  
  8.   
  9.             //! DEBUGGING ONLY: Get current strong ref count.  
  10.             int32_t         getStrongCount() const;  
  11.   
  12.     class weakref_type  
  13.     {  
  14.     public:  
  15.         RefBase*            refBase() const;  
  16.           
  17.         void                incWeak(const void* id);  
  18.         void                decWeak(const void* id);  
  19.           
  20.         // acquires a strong reference if there is already one.  
  21.         bool                attemptIncStrong(const void* id);  
  22.           
  23.         // acquires a weak reference if there is already one.  
  24.         // This is not always safe. see ProcessState.cpp and BpBinder.cpp  
  25.         // for proper use.  
  26.         bool                attemptIncWeak(const void* id);  
  27.   
  28.         //! DEBUGGING ONLY: Get current weak ref count.  
  29.         int32_t             getWeakCount() const;  
  30.   
  31.         //! DEBUGGING ONLY: Print references held on object.  
  32.         void                printRefs() const;  
  33.   
  34.         //! DEBUGGING ONLY: Enable tracking for this object.  
  35.         // enable -- enable/disable tracking  
  36.         // retain -- when tracking is enable, if true, then we save a stack trace  
  37.         //           for each reference and dereference; when retain == false, we  
  38.         //           match up references and dereferences and keep only the   
  39.         //           outstanding ones.  
  40.           
  41.         void                trackMe(bool enable, bool retain);  
  42.     };  
  43.       
  44.             weakref_type*   createWeak(const void* id) const;  
  45.               
  46.             weakref_type*   getWeakRefs() const;  
  47.   
  48.             //! DEBUGGING ONLY: Print references held on object.  
  49.     inline  void            printRefs() const { getWeakRefs()->printRefs(); }  
  50.   
  51.             //! DEBUGGING ONLY: Enable tracking of object.  
  52.     inline  void            trackMe(bool enable, bool retain)  
  53.     {   
  54.         getWeakRefs()->trackMe(enable, retain);   
  55.     }  
  56.   
  57.     typedef RefBase basetype;  
  58.   
  59. protected:  
  60.                             RefBase();  
  61.     virtual                 ~RefBase();  
  62.       
  63.     //! Flags for extendObjectLifetime()  
  64.     enum {  
  65.         OBJECT_LIFETIME_STRONG  = 0x0000,  
  66.         OBJECT_LIFETIME_WEAK    = 0x0001,  
  67.         OBJECT_LIFETIME_MASK    = 0x0001  
  68.     };  
  69.       
  70.             void            extendObjectLifetime(int32_t mode);  
  71.               
  72.     //! Flags for onIncStrongAttempted()  
  73.     enum {  
  74.         FIRST_INC_STRONG = 0x0001  
  75.     };  
  76.       
  77.     virtual void            onFirstRef();  
  78.     virtual void            onLastStrongRef(const void* id);  
  79.     virtual bool            onIncStrongAttempted(uint32_t flags, const void* id);  
  80.     virtual void            onLastWeakRef(const void* id);  
  81.   
  82. private:  
  83.     friend class weakref_type;  
  84.     class weakref_impl;  
  85.       
  86.                             RefBase(const RefBase& o);  
  87.             RefBase&        operator=(const RefBase& o);  
  88.   
  89. private:  
  90.     friend class ReferenceMover;  
  91.   
  92.     static void renameRefs(size_t n, const ReferenceRenamer& renamer);  
  93.   
  94.     static void renameRefId(weakref_type* ref,  
  95.             const void* old_id, const void* new_id);  
  96.   
  97.     static void renameRefId(RefBase* ref,  
  98.             const void* old_id, const void* new_id);  
  99.   
  100.         weakref_impl* const mRefs;  
  101. };  

       RefBase类和LightRefBase类一样,提供了incStrong和decStrong成员函数来操作它的引用计数器;而RefBase类与LightRefBase类最大的区别是,它不像LightRefBase类一样直接提供一个整型值(mutable volatile int32_t mCount)来维护对象的引用计数,前面我们说过,复杂的引用计数技术同时支持强引用计数和弱引用计数,在RefBase类中,这两种计数功能是通过其成员变量mRefs来提供的。


2.1 weakref_impl类

        RefBase类的成员变量mRefs的类型为weakref_impl指针,它实现也在RefBase.cpp文件中。我们看weakref_impl这个类看着很复杂,其实我们注意有一个宏DEBUG_REFS,当是release版本的时候,这个类所有的函数都是空实现,而是debug版本的时候这些函数才会实现,所以我们就不关注这些函数了。

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. class RefBase::weakref_impl : public RefBase::weakref_type  
  2. {  
  3. public:  
  4.     volatile int32_t    mStrong;  
  5.     volatile int32_t    mWeak;  
  6.     RefBase* const      mBase;  
  7.     volatile int32_t    mFlags;  
  8.   
  9. #if !DEBUG_REFS  
  10.   
  11.     weakref_impl(RefBase* base)  
  12.         : mStrong(INITIAL_STRONG_VALUE)  
  13.         , mWeak(0)  
  14.         , mBase(base)  
  15.         , mFlags(0)  
  16.     {  
  17.     }  
  18.   
  19.     void addStrongRef(const void/*id*/) { }  
  20.     void removeStrongRef(const void/*id*/) { }  
  21.     void renameStrongRefId(const void/*old_id*/const void/*new_id*/) { }  
  22.     void addWeakRef(const void/*id*/) { }  
  23.     void removeWeakRef(const void/*id*/) { }  
  24.     void renameWeakRefId(const void/*old_id*/const void/*new_id*/) { }  
  25.     void printRefs() const { }  
  26.     void trackMe(boolbool) { }  
  27.   
  28. #else  
  29.   
  30.     weakref_impl(RefBase* base)  
  31.         : mStrong(INITIAL_STRONG_VALUE)  
  32.         , mWeak(0)  
  33.         , mBase(base)  
  34.         , mFlags(0)  
  35.         , mStrongRefs(NULL)  
  36.         , mWeakRefs(NULL)  
  37.         , mTrackEnabled(!!DEBUG_REFS_ENABLED_BY_DEFAULT)  
  38.         , mRetain(false)  
  39.     {  
  40.     }  
  41.     ......  
  42.   
  43.     void addStrongRef(const void* id) {  
  44.         //ALOGD_IF(mTrackEnabled,  
  45.         //        "addStrongRef: RefBase=%p, id=%p", mBase, id);  
  46.         addRef(&mStrongRefs, id, mStrong);  
  47.     }  
  48.   
  49.     void removeStrongRef(const void* id) {  
  50.         //ALOGD_IF(mTrackEnabled,  
  51.         //        "removeStrongRef: RefBase=%p, id=%p", mBase, id);  
  52.         if (!mRetain) {  
  53.             removeRef(&mStrongRefs, id);  
  54.         } else {  
  55.             addRef(&mStrongRefs, id, -mStrong);  
  56.         }  
  57.     }  
  58.   
  59.     void renameStrongRefId(const void* old_id, const void* new_id) {  
  60.         //ALOGD_IF(mTrackEnabled,  
  61.         //        "renameStrongRefId: RefBase=%p, oid=%p, nid=%p",  
  62.         //        mBase, old_id, new_id);  
  63.         renameRefsId(mStrongRefs, old_id, new_id);  
  64.     }  
  65.   
  66.     void addWeakRef(const void* id) {  
  67.         addRef(&mWeakRefs, id, mWeak);  
  68.     }  
  69.   
  70.     void removeWeakRef(const void* id) {  
  71.         if (!mRetain) {  
  72.             removeRef(&mWeakRefs, id);  
  73.         } else {  
  74.             addRef(&mWeakRefs, id, -mWeak);  
  75.         }  
  76.     }  
  77.   
  78.     void renameWeakRefId(const void* old_id, const void* new_id) {  
  79.         renameRefsId(mWeakRefs, old_id, new_id);  
  80.     }  
  81.     ......  
  82.   
  83.     void addRef(ref_entry** refs, const void* id, int32_t mRef)  
  84.     {  
  85.         if (mTrackEnabled) {  
  86.             AutoMutex _l(mMutex);  
  87.   
  88.             ref_entry* ref = new ref_entry;  
  89.             // Reference count at the time of the snapshot, but before the  
  90.             // update.  Positive value means we increment, negative--we  
  91.             // decrement the reference count.  
  92.             ref->ref = mRef;  
  93.             ref->id = id;  
  94. #if DEBUG_REFS_CALLSTACK_ENABLED  
  95.             ref->stack.update(2);  
  96. #endif  
  97.             ref->next = *refs;  
  98.             *refs = ref;  
  99.         }  
  100.     }  
  101.   
  102.     void removeRef(ref_entry** refs, const void* id)  
  103.     {  
  104.         if (mTrackEnabled) {  
  105.             AutoMutex _l(mMutex);  
  106.               
  107.             ref_entry* const head = *refs;  
  108.             ref_entry* ref = head;  
  109.             while (ref != NULL) {  
  110.                 if (ref->id == id) {  
  111.                     *refs = ref->next;  
  112.                     delete ref;  
  113.                     return;  
  114.                 }  
  115.                 refs = &ref->next;  
  116.                 ref = *refs;  
  117.             }  
  118.   
  119.             ALOGE("RefBase: removing id %p on RefBase %p"  
  120.                     "(weakref_type %p) that doesn't exist!",  
  121.                     id, mBase, this);  
  122.   
  123.             ref = head;  
  124.             while (ref) {  
  125.                 char inc = ref->ref >= 0 ? '+' : '-';  
  126.                 ALOGD("\t%c ID %p (ref %d):", inc, ref->id, ref->ref);  
  127.                 ref = ref->next;  
  128.             }  
  129.   
  130.             CallStack stack(LOG_TAG);  
  131.         }  
  132.     }  
  133.   
  134.     void renameRefsId(ref_entry* r, const void* old_id, const void* new_id)  
  135.     {  
  136.         if (mTrackEnabled) {  
  137.             AutoMutex _l(mMutex);  
  138.             ref_entry* ref = r;  
  139.             while (ref != NULL) {  
  140.                 if (ref->id == old_id) {  
  141.                     ref->id = new_id;  
  142.                 }  
  143.                 ref = ref->next;  
  144.             }  
  145.         }  
  146.     }  
  147.     ......  
  148.     mutable Mutex mMutex;  
  149.     ref_entry* mStrongRefs;  
  150.     ref_entry* mWeakRefs;  
  151.   
  152.     bool mTrackEnabled;  
  153.     // Collect stack traces on addref and removeref, instead of deleting the stack references  
  154.     // on removeref that match the address ones.  
  155.     bool mRetain;  
  156.   
  157. #endif  
  158. };  

而weakref_impl 是继承与weakref_type,这个只是接口与实现的问题。是让代码中接口与实现分离的一种编程模式。


2.2 RefBase的IncStrong函数

下面我们再次结合智能指针进一步分析

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. template<typename T>  
  2. sp<T>::sp(T* other)  
  3.         : m_ptr(other) {  
  4.     if (other)  
  5.         other->incStrong(this);  
  6. }  

这里的other就是实际的对象,这个对象的类必须继承RefBase这个类,我们来看看RefBase的incStrong函数:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. void RefBase::incStrong(const void* id) const  
  2. {  
  3.     weakref_impl* const refs = mRefs;  
  4.     refs->incWeak(id);  
  5.       
  6.     refs->addStrongRef(id);  
  7.     const int32_t c = android_atomic_inc(&refs->mStrong);  
  8.     ALOG_ASSERT(c > 0, "incStrong() called on %p after last strong ref", refs);  
  9. #if PRINT_REFS  
  10.     ALOGD("incStrong of %p from %p: cnt=%d\n"this, id, c);  
  11. #endif  
  12.     if (c != INITIAL_STRONG_VALUE)  {  
  13.         return;  
  14.     }  
  15.   
  16.     android_atomic_add(-INITIAL_STRONG_VALUE, &refs->mStrong);  
  17.     refs->mBase->onFirstRef();  
  18. }  
mRefs成员变量是在构造函数中创建的
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. RefBase::RefBase()  
  2.     : mRefs(new weakref_impl(this))  
  3. {  
  4. }  
在这个incStrong函数中,主要做了三件事情:

        一是增加弱引用计数:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. refs->addWeakRef(id);    
  2. refs->incWeak(id);   

        二是增加强引用计数:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. refs->addStrongRef(id);    
  2. const int32_t c = android_atomic_inc(&refs->mStrong);    

        三是如果发现是首次调用这个对象的incStrong函数,就会调用一个这个对象的onFirstRef函数,这个函数是个虚函数在RefBase中是空实现,一般在对象中实现,让对象有机会在对象被首次引用时做一些处理逻辑:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. if (c != INITIAL_STRONG_VALUE)  {    
  2.     return;    
  3. }    
  4.     
  5. android_atomic_add(-INITIAL_STRONG_VALUE, &refs->mStrong);    
  6. const_cast<RefBase*>(this)->onFirstRef();    

       这里的c返回的是refs->mStrong加1前的值,如果发现等于INITIAL_STRONG_VALUE,就说明这个对象的强引用计数是第一次被增加,因此,refs->mStrong就是初始化为INITIAL_STRONG_VALUE的,它的值为:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. #define INITIAL_STRONG_VALUE (1<<28)    
        这个值加1后等于1<<28 + 1,不等于1,因此,后面要再减去INITIAL_STRONG_VALUE,也就是加上负的INITIAL_STRONG_VALUE。这样refs->mStrong就等于1了,就表示当前对象的强引用计数值为1了,这与这个对象是第一次被增加强引用计数值的逻辑是一致的。

        回过头来看弱引用计数是如何增加的,首先是调用weakref_impl类的addWeakRef函数,我们知道,在Release版本中,这个函数也不做,而在Debug版本我们就不分析了。接着又调用了weakref_impl类的incWeak函数,真正增加弱引用计数值就是在这个函数实现的了,weakref_impl类的incWeak函数继承于其父类weakref_type的incWeak函数:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  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);    
  6.     LOG_ASSERT(c >= 0, "incWeak called on %p after last weak ref"this);    
  7. }    

       增加弱引用计数是下面语句执行的:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. const int32_t c = android_atomic_inc(&impl->mWeak);    

2.3 RefBase的decStrong函数

下面再看看sp的析构函数:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. template<typename T>  
  2. sp<T>::~sp() {  
  3.     if (m_ptr)  
  4.         m_ptr->decStrong(this);  
  5. }  

同样的我们看下RefBase类的decStrong函数

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. void RefBase::decStrong(const void* id) const  
  2. {  
  3.     weakref_impl* const refs = mRefs;  
  4.     refs->removeStrongRef(id);//release版本空实现不分析了  
  5.     const int32_t c = android_atomic_dec(&refs->mStrong);//强引用次数减1  
  6. #if PRINT_REFS  
  7.     ALOGD("decStrong of %p from %p: cnt=%d\n"this, id, c);  
  8. #endif  
  9.     ALOG_ASSERT(c >= 1, "decStrong() called on %p too many times", refs);  
  10.     if (c == 1) {  
  11.         refs->mBase->onLastStrongRef(id);  
  12.         if ((refs->mFlags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_STRONG) {  
  13.             delete this;  
  14.         }  
  15.     }  
  16.     refs->decWeak(id);  
  17. }  

先对引用次数减1,然后在减1前发现如果是1,也就是减1后变0了,先调用RefBase的onLastStrongRef函数。这个函数是虚函数在RefBase中为空实现,一般在对象中继承实现。而mask为OBJECT_LIFETIME_STRONG说明这个对象的生命周期是受强指针控制的,当强指针的引用次数为1,就需要delete这个对象。

而对弱引用计数减1是在decWeak中执行的,这个函数是在weakref_type类中实现的

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. void RefBase::weakref_type::decWeak(const void* id)  
  2. {  
  3.     weakref_impl* const impl = static_cast<weakref_impl*>(this);  
  4.     impl->removeWeakRef(id);//release版本空实现  
  5.     const int32_t c = android_atomic_dec(&impl->mWeak);//弱引用次数减1  
  6.     ALOG_ASSERT(c >= 1, "decWeak called on %p too many times"this);  
  7.     if (c != 1) return;//减之前不为1,直接return。为1后面进行delete相关对象  
  8.   
  9.     if ((impl->mFlags&OBJECT_LIFETIME_WEAK) == OBJECT_LIFETIME_STRONG) {//对象生命周期受强引用控制  
  10.         // This is the regular lifetime case. The object is destroyed  
  11.         // when the last strong reference goes away. Since weakref_impl  
  12.         // outlive the object, it is not destroyed in the dtor, and  
  13.         // we'll have to do it here.  
  14.         if (impl->mStrong == INITIAL_STRONG_VALUE) {//这里代表强引用处于初始化状态,所有需要删除对象,删除对象的同时会删除impl的  
  15.             // Special case: we never had a strong reference, so we need to  
  16.             // destroy the object now.  
  17.             delete impl->mBase;  
  18.         } else {  
  19.             // ALOGV("Freeing refs %p of old RefBase %p\n", this, impl->mBase);  
  20.             delete impl;//删除impl  
  21.         }  
  22.     } else {  
  23.         // less common case: lifetime is OBJECT_LIFETIME_{WEAK|FOREVER}  
  24.         impl->mBase->onLastWeakRef(id);  
  25.         if ((impl->mFlags&OBJECT_LIFETIME_MASK) == OBJECT_LIFETIME_WEAK) {//对象生命周期受弱引用控制  
  26.             // this is the OBJECT_LIFETIME_WEAK case. The last weak-reference  
  27.             // is gone, we can destroy the object.  
  28.             delete impl->mBase;//删除对象  
  29.         }  
  30.     }  
  31. }  

这个函数,当弱引用次数不为1直接return,为1代表入弱引用次数减1后为0了,需要delete。

而为弱引用次数为1又分两种情况:

第一种对象生命周期受强引用控制,而当强引用计数为初始值,就要删除 impl->mBase就是删除实际对象。而RefBase被删除的时候调用析构函数,而在析构函数中决定是否delete mRefs。

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. RefBase::~RefBase()  
  2. {  
  3.     if (mRefs->mStrong == INITIAL_STRONG_VALUE) {//强引用初始值,直接删除mRefs  
  4.         // we never acquired a strong (and/or weak) reference on this object.  
  5.         delete mRefs;  
  6.     } else {  
  7.         // life-time of this object is extended to WEAK or FOREVER, in  
  8.         // which case weakref_impl doesn't out-live the object and we  
  9.         // can free it now.  
  10.         if ((mRefs->mFlags & OBJECT_LIFETIME_MASK) != OBJECT_LIFETIME_STRONG) {//对象生命周期不是受强引用控制  
  11.             // It's possible that the weak count is not 0 if the object  
  12.             // re-acquired a weak reference in its destructor  
  13.             if (mRefs->mWeak == 0) {//弱引用次数为0 删除mRefs  
  14.                 delete mRefs;  
  15.             }  
  16.         }  
  17.     }  
  18.     // for debugging purposes, clear this.  
  19.     const_cast<weakref_impl*&>(mRefs) = NULL;  
  20. }  

而当强引用次数不是初始值的时候,直接delete impl。

第二种情况,对象生命周期不是受强引用控制,先调用onLastWeakRef函数,然后如果对象的生命周期是由弱引用控制,就直接删除RefBase对象,当然在RefBase的析构函数中也会删除mRefs对象的。

三、弱指针

这里分析弱指针。

3.1 wp类

弱指针wp的代码在RefBase.cpp中的:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. template <typename T>  
  2. class wp  
  3. {  
  4. public:  
  5.     typedef typename RefBase::weakref_type weakref_type;  
  6.       
  7.     inline wp() : m_ptr(0) { }  
  8.   
  9.     wp(T* other);  
  10.     wp(const wp<T>& other);  
  11.     wp(const sp<T>& other);  
  12.     template<typename U> wp(U* other);  
  13.     template<typename U> wp(const sp<U>& other);  
  14.     template<typename U> wp(const wp<U>& other);  
  15.   
  16.     ~wp();  
  17.       
  18.     // Assignment  
  19.   
  20.     wp& operator = (T* other);  
  21.     wp& operator = (const wp<T>& other);  
  22.     wp& operator = (const sp<T>& other);  
  23.       
  24.     template<typename U> wp& operator = (U* other);  
  25.     template<typename U> wp& operator = (const wp<U>& other);  
  26.     template<typename U> wp& operator = (const sp<U>& other);  
  27.       
  28.     void set_object_and_refs(T* other, weakref_type* refs);  
  29.   
  30.     // promotion to sp  
  31.       
  32.     sp<T> promote() const;  
  33.   
  34.     // Reset  
  35.       
  36.     void clear();  
  37.   
  38.     // Accessors  
  39.       
  40.     inline  weakref_type* get_refs() const { return m_refs; }  
  41.       
  42.     inline  T* unsafe_get() const { return m_ptr; }  
  43.   
  44.     // Operators  
  45.   
  46.     COMPARE_WEAK(==)  
  47.     COMPARE_WEAK(!=)  
  48.     COMPARE_WEAK(>)  
  49.     COMPARE_WEAK(<)  
  50.     COMPARE_WEAK(<=)  
  51.     COMPARE_WEAK(>=)  
  52.   
  53.     inline bool operator == (const wp<T>& o) const {  
  54.         return (m_ptr == o.m_ptr) && (m_refs == o.m_refs);  
  55.     }  
  56.     template<typename U>  
  57.     inline bool operator == (const wp<U>& o) const {  
  58.         return m_ptr == o.m_ptr;  
  59.     }  
  60.   
  61.     inline bool operator > (const wp<T>& o) const {  
  62.         return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr);  
  63.     }  
  64.     template<typename U>  
  65.     inline bool operator > (const wp<U>& o) const {  
  66.         return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr);  
  67.     }  
  68.   
  69.     inline bool operator < (const wp<T>& o) const {  
  70.         return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);  
  71.     }  
  72.     template<typename U>  
  73.     inline bool operator < (const wp<U>& o) const {  
  74.         return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);  
  75.     }  
  76.                          inline bool operator != (const wp<T>& o) const { return m_refs != o.m_refs; }  
  77.     template<typename U> inline bool operator != (const wp<U>& o) const { return !operator == (o); }  
  78.                          inline bool operator <= (const wp<T>& o) const { return !operator > (o); }  
  79.     template<typename U> inline bool operator <= (const wp<U>& o) const { return !operator > (o); }  
  80.                          inline bool operator >= (const wp<T>& o) const { return !operator < (o); }  
  81.     template<typename U> inline bool operator >= (const wp<U>& o) const { return !operator < (o); }  
  82.   
  83. private:  
  84.     template<typename Y> friend class sp;  
  85.     template<typename Y> friend class wp;  
  86.   
  87.     T*              m_ptr;  
  88.     weakref_type*   m_refs;  
  89. };  

与强指针类相比,它们都有一个成员变量m_ptr指向目标对象,但是弱指针还有一个额外的成员变量m_refs,它的类型是weakref_type指针,下面我们分析弱指针的构造函数时再看看它是如果初始化的。这里我们需要关注的仍然是弱指针的构造函数和析构函数。

先看wp的构造函数:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. template<typename T>  
  2. wp<T>::wp(T* other)  
  3.     : m_ptr(other)  
  4. {  
  5.     if (other) m_refs = other->createWeak(this);  
  6. }  

这里调用了RefBase的createWeak函数,返回值为m_refs。

在RefBase的createWeak函数中,直接调用了weakref_impl的incWeak函数,这个函数之前分析过了就是增加弱引用次数,然后返回mRefs.

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. RefBase::weakref_type* RefBase::createWeak(const void* id) const  
  2. {  
  3.     mRefs->incWeak(id);  
  4.     return mRefs;  
  5. }  

再来看析构函数,这里直接调用了weakref_impl的decWeak函数,前面分析过这个函数了。在这个函数中弱引用次数减1,然后决定是否需要delete impl等。

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. template<typename T>  
  2. wp<T>::~wp()  
  3. {  
  4.     if (m_ptr) m_refs->decWeak(this);  
  5. }  


3.2 升级强指针

分析到这里,弱指针还没介绍完,它最重要的特性我们还没有分析到。前面我们说过,弱指针的最大特点是它不能直接操作目标对象,这是怎么样做到的呢?秘密就在于弱指针类没有重载*和->操作符号,而强指针重载了这两个操作符号。但是,如果我们要操作目标对象,应该怎么办呢,这就要把弱指针升级为强指针了:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. template<typename T>  
  2. sp<T> wp<T>::promote() const  
  3. {  
  4.     sp<T> result;  
  5.     if (m_ptr && m_refs->attemptIncStrong(&result)) {  
  6.         result.set_pointer(m_ptr);  
  7.     }  
  8.     return result;  
  9. }  

在上面这个函数中就是将wp升级为sp。


四、强指针弱指针用法

源文件weightpointer.cpp的内容如下:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>    
  2. #include <utils/RefBase.h>    
  3.     
  4. #define INITIAL_STRONG_VALUE (1<<28)    
  5.     
  6. using namespace android;    
  7.     
  8. class WeightClass : public RefBase    
  9. {    
  10. public:    
  11.         void printRefCount()    
  12.         {    
  13.                 int32_t strong = getStrongCount();    
  14.                 weakref_type* ref = getWeakRefs();    
  15.     
  16.                 printf("-----------------------\n");    
  17.                 printf("Strong Ref Count: %d.\n", (strong  == INITIAL_STRONG_VALUE ? 0 : strong));    
  18.                 printf("Weak Ref Count: %d.\n", ref->getWeakCount());    
  19.                 printf("-----------------------\n");    
  20.         }    
  21. };    
  22.     
  23. class StrongClass : public WeightClass    
  24. {    
  25. public:    
  26.         StrongClass()    
  27.         {    
  28.                 printf("Construct StrongClass Object.\n");    
  29.         }    
  30.     
  31.         virtual ~StrongClass()    
  32.         {    
  33.                 printf("Destory StrongClass Object.\n");    
  34.         }    
  35. };    
  36.     
  37.     
  38. class WeakClass : public WeightClass    
  39. {    
  40. public:    
  41.         WeakClass()    
  42.         {    
  43.                 extendObjectLifetime(OBJECT_LIFETIME_WEAK);// 设置对象生命周期受弱引用控制  
  44.     
  45.                 printf("Construct WeakClass Object.\n");    
  46.         }    
  47.     
  48.         virtual ~WeakClass()    
  49.         {    
  50.                 printf("Destory WeakClass Object.\n");    
  51.         }    
  52. };    
  53.     
  54. class ForeverClass : public WeightClass    
  55. {    
  56. public:    
  57.         ForeverClass()    
  58.         {    
  59.                 extendObjectLifetime(OBJECT_LIFETIME_FOREVER); //设置对象生命周期一直存在,就是普通指针。需要手动delete  
  60.     
  61.                 printf("Construct ForeverClass Object.\n");    
  62.         }    
  63.     
  64.         virtual ~ForeverClass()    
  65.         {    
  66.                 printf("Destory ForeverClass Object.\n");    
  67.         }    
  68. };    
  69.     
  70.     
  71. void TestStrongClass(StrongClass* pStrongClass)    
  72. {    
  73.         wp<StrongClass> wpOut = pStrongClass;    
  74.         pStrongClass->printRefCount();    
  75.     
  76.         {    
  77.                 sp<StrongClass> spInner = pStrongClass;    
  78.                 pStrongClass->printRefCount();    
  79.         }    
  80.     
  81.         sp<StrongClass> spOut = wpOut.promote();    
  82.         printf("spOut: %p.\n", spOut.get());    
  83. }    
  84.     
  85. void TestWeakClass(WeakClass* pWeakClass)    
  86. {    
  87.         wp<WeakClass> wpOut = pWeakClass;    
  88.         pWeakClass->printRefCount();    
  89.     
  90.         {    
  91.                 sp<WeakClass> spInner = pWeakClass;    
  92.                 pWeakClass->printRefCount();    
  93.         }    
  94.     
  95.         pWeakClass->printRefCount();    
  96.         sp<WeakClass> spOut = wpOut.promote();    
  97.         printf("spOut: %p.\n", spOut.get());    
  98. }    
  99.     
  100.     
  101. void TestForeverClass(ForeverClass* pForeverClass)    
  102. {    
  103.         wp<ForeverClass> wpOut = pForeverClass;    
  104.         pForeverClass->printRefCount();    
  105.     
  106.         {    
  107.                 sp<ForeverClass> spInner = pForeverClass;    
  108.                 pForeverClass->printRefCount();    
  109.         }    
  110. }    
  111.     
  112. int main(int argc, char** argv)    
  113. {    
  114.         printf("Test Strong Class: \n");    
  115.         StrongClass* pStrongClass = new StrongClass();    
  116.         TestStrongClass(pStrongClass);    
  117.     
  118.         printf("\nTest Weak Class: \n");    
  119.         WeakClass* pWeakClass = new WeakClass();    
  120.         TestWeakClass(pWeakClass);    
  121.     
  122.         printf("\nTest Froever Class: \n");    
  123.         ForeverClass* pForeverClass = new ForeverClass();    
  124.         TestForeverClass(pForeverClass);    
  125.         pForeverClass->printRefCount();    
  126.         delete pForeverClass;    
  127.     
  128.         return 0;    
  129. }    

        首先定义了一个基类WeightClass,继承于RefBase类,它只有一个成员函数printRefCount,作用是用来输出引用计数。接着分别定义了三个类StrongClass、WeakClass和ForeverClass,其中实例化StrongClass类的得到的对象的标志位为默认值0,实例化WeakClass类的得到的对象的标志位为OBJECT_LIFETIME_WEAK,实例化ForeverClass类的得到的对象的标志位为OBJECT_LIFETIME_FOREVER,后两者都是通过调用RefBase类的extendObjectLifetime成员函数来设置的。

        在main函数里面,分别实例化了这三个类的对象出来,然后分别传给TestStrongClass函数、TestWeakClass函数和TestForeverClass函数来说明智能指针的用法,我们主要是通过考察它们的强引用计数和弱引用计数来验证智能指针的实现原理。

        编译脚本文件Android.mk的内容如下:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. LOCAL_PATH := $(call my-dir)    
  2. include $(CLEAR_VARS)    
  3. LOCAL_MODULE_TAGS := optional    
  4. LOCAL_MODULE := weightpointer    
  5. LOCAL_SRC_FILES := weightpointer.cpp    
  6. LOCAL_SHARED_LIBRARIES := \    
  7.         libcutils \    
  8.         libutils    
  9. include $(BUILD_EXECUTABLE)    

        最后,我们参照如何单独编译Android源代码中的模块一文,使用mmm命令对工程进行编译:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. USER-NAME@MACHINE-NAME:~/Android$ mmm ./external/weightpointer   
        编译之后,就可以打包了:
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. USER-NAME@MACHINE-NAME:~/Android$ make snod    

        最后得到可执行程序weightpointer就位于设备上的/system/bin/目录下。启动模拟器,通过adb shell命令进入到模拟器终端,进入到/system/bin/目录,执行weightpointer可执行程序,验证程序是否按照我们设计的逻辑运行:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. USER-NAME@MACHINE-NAME:~/Android$ adb shell    
  2. root@android:/ # cd system/bin/            
  3. root@android:/system/bin # ./weightpointer      

        执行TestStrongClass函数的输出为:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. Test Strong Class:     
  2. Construct StrongClass Object.    
  3. -----------------------    
  4. Strong Ref Count: 0.    
  5. Weak Ref Count: 1.    
  6. -----------------------    
  7. -----------------------    
  8. Strong Ref Count: 1.    
  9. Weak Ref Count: 2.    
  10. -----------------------    
  11. Destory StrongClass Object.    
  12. spOut: 0x0.    

        在TestStrongClass函数里面,首先定义一个弱批针wpOut指向从main函数传进来的StrongClass对象,这时候我们可以看到StrongClass对象的强引用计数和弱引用计数值分别为0和1;接着在一个大括号里面定义一个强指针spInner指向这个StrongClass对象,这时候我们可以看到StrongClass对象的强引用计数和弱引用计数值分别为1和2;当程序跳出了大括号之后,强指针spInner就被析构了,从上面的分析我们知道,强指针spInner析构时,会减少目标对象的强引用计数值,因为前面得到的强引用计数值为1,这里减1后,就变为0了,又由于这个StrongClass对象的生命周期只受强引用计数控制,因此,这个StrongClass对象就被delete了,这一点可以从后面的输出(“Destory StrongClass Object.”)以及试图把弱指针wpOut提升为强指针时得到的对象指针为0x0得到验证。

        执行TestWeakClass函数的输出为:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. Test Weak Class:     
  2. Construct WeakClass Object.    
  3. -----------------------    
  4. Strong Ref Count: 0.    
  5. Weak Ref Count: 1.    
  6. -----------------------    
  7. -----------------------    
  8. Strong Ref Count: 1.    
  9. Weak Ref Count: 2.    
  10. -----------------------    
  11. -----------------------    
  12. Strong Ref Count: 0.    
  13. Weak Ref Count: 1.    
  14. -----------------------    
  15. spOut: 0xa528.    
  16. Destory WeakClass Object.    

        TestWeakClass函数和TestStrongClass函数的执行过程基本一样,所不同的是当程序跳出大括号之后,虽然这个WeakClass对象的强引用计数值已经为0,但是由于它的生命周期同时受强引用计数和弱引用计数控制,而这时它的弱引用计数值大于0,因此,这个WeakClass对象不会被delete掉,这一点可以从后面试图把弱批针wpOut提升为强指针时得到的对象指针不为0得到验证。

        执行TestForeverClass函数的输出来:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. Test Froever Class:     
  2. Construct ForeverClass Object.    
  3. -----------------------    
  4. Strong Ref Count: 0.    
  5. Weak Ref Count: 1.    
  6. -----------------------    
  7. -----------------------    
  8. Strong Ref Count: 1.    
  9. Weak Ref Count: 2.    
  10. -----------------------    

       当执行完TestForeverClass函数返回到main函数的输出来:

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
  1. -----------------------    
  2. Strong Ref Count: 0.    
  3. Weak Ref Count: 0.    
  4. -----------------------    
  5. Destory ForeverClass Object.    
        这里我们可以看出,虽然这个ForeverClass对象的强引用计数和弱引用计数值均为0了,但是它不自动被delete掉,虽然由我们手动地delete这个对象,它才会被析构,这是因为这个ForeverClass对象的生命周期是既不受强引用计数值控制,也不会弱引用计数值控制。

        这样,从TestStrongClass、TestWeakClass和TestForeverClass这三个函数的输出就可以验证了我们上面对Android系统的强指针和弱指针的实现原理的分析。

        这样Android系统的智能指针(轻量级指针、强指针和弱指针)的实现原理就分析完成了。


0 0
原创粉丝点击