<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                如果一個類的對象支持使用弱指針,那么這個類就必須要從RefBase類繼承下來,因為RefBase類提供了弱引用計數器。在前面的3.2.1小節中,我們已經分析過RefBase類的實現了,因此,在本節中,我們只分析弱指針類wp的實現。 wp類的定義如下所示。 **frameworks/base/include/utils/RefBase.h** ~~~ template <typename T> class wp { public: typedef typename RefBase::weakref_type weakref_type; inline wp() : m_ptr(0) { } wp(T* other); wp(const wp<T>& other); wp(const sp<T>& other); template<typename U> wp(U* other); template<typename U> wp(const sp<U>& other); template<typename U> wp(const wp<U>& other); ~wp(); // Assignment wp& operator = (T* other); wp& operator = (const wp<T>& other); wp& operator = (const sp<T>& other); template<typename U> wp& operator = (U* other); template<typename U> wp& operator = (const wp<U>& other); template<typename U> wp& operator = (const sp<U>& other); void set_object_and_refs(T* other, weakref_type* refs); // promotion to sp sp<T> promote() const; // Reset void clear(); // Accessors inline weakref_type* get_refs() const { return m_refs; } inline T* unsafe_get() const { return m_ptr; } // Operators COMPARE_WEAK(==) COMPARE_WEAK(!=) COMPARE_WEAK(>) COMPARE_WEAK(<) COMPARE_WEAK(<=) COMPARE_WEAK(>=) inline bool operator == (const wp<T>& o) const { return (m_ptr == o.m_ptr) && (m_refs == o.m_refs); } template<typename U> inline bool operator == (const wp<U>& o) const { return m_ptr == o.m_ptr; } inline bool operator > (const wp<T>& o) const { return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr); } template<typename U> inline bool operator > (const wp<U>& o) const { return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr); } inline bool operator < (const wp<T>& o) const { return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr); } template<typename U> inline bool operator < (const wp<U>& o) const { return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr); } inline bool operator != (const wp<T>& o) const { return m_refs != o.m_refs; } template<typename U> inline bool operator != (const wp<U>& o) const { return !operator == (o); } inline bool operator <= (const wp<T>& o) const { return !operator > (o); } template<typename U> inline bool operator <= (const wp<U>& o) const { return !operator > (o); } inline bool operator >= (const wp<T>& o) const { return !operator < (o); } template<typename U> inline bool operator >= (const wp<U>& o) const { return !operator < (o); } private: template<typename Y> friend class sp; template<typename Y> friend class wp; T* m_ptr; weakref_type* m_refs; }; ~~~ wp類是一個模板類,其中,模板參數T表示對象的實際類型,它必須是從RefBase類繼承下來的。與強指針類sp類似,弱指針類wp也有一個成員變量m_ptr,用來指向它所引用的對象,但是弱指針類wp還使用另外一個類型為weakref_type*的成員變量m_refs,用來維護對象的弱引用計數。 弱指針與強指針有一個很大的區別,就是弱指針不可以直接操作它所引用的對象,因為它所引用的對象可能是不受弱引用計數控制的,即它所引用的對象可能是一個無效的對象。因此,如果需要操作一個弱指針所引用的對象,那么就需要將這個弱指針升級為強指針,這是通過調用它的成員函數promote來實現的。如果升級成功,就說明該弱指針所引用的對象還沒有被銷毀,可以正常使用。 wp類的實現比較復雜,但是只要掌握了它的構造函數和析構函數的實現,以及它是如何將一個弱指針升級為強指針的,就可以理解它的實現原理了。因此,在接下來的內容中,我們首先分析wp類的構造函數和析構函數的實現,然后再分析它的成員函數promote的實現。 wp類的構造函數的實現如下所示。 **frameworks/base/include/utils/RefBase.h** ~~~ template<typename T> wp<T>::wp(T* other) : m_ptr(other) { if (other) m_refs = other->createWeak(this); } ~~~ > 注意:模板參數T是一個繼承了RefBase類的子類,因此,第5行實際上是調用了RefBase類的成員函數createWeak來增加對象的弱引用計數,如下所示。 **frameworks/base/libs/utils/RefBase.cpp** ~~~ RefBase::weakref_type* RefBase::createWeak(const void* id) const { mRefs->incWeak(id); return mRefs; } ~~~ RefBase類的成員變量mRefs指向的是一個weakref_impl對象。在前面的3.2.1小節中,我們已經介紹過它的成員函數incWeak的實現,它主要就是增加實際引用對象的弱引用計數。RefBase類的成員函數createWeak最后將它的成員變量mRefs所指向的一個weakref_impl對象返回給調用者。 wp類的析構函數的實現如下所示。 **frameworks/base/include/utils/RefBase.h** ~~~ template<typename T> wp<T>::~wp() { if (m_ptr) m_refs->decWeak(this); } ~~~ 弱指針在析構時,會調用它的成員變量m_refs的成員函數decWeak來減少對象的弱引用計數。從wp類的構造函數的實現可以知道,wp類的成員變量m_refs指向的是一個weakref_impl對象,因此,第4行實際上是調用了weakref_impl類的成員函數decWeak來減少對象的弱引用計數。在前面的3.2.1小節中,我們已經分析過weakref_impl類的成員函數decWeak的實現,它主要就是將對象的弱引用計數值減少1。 介紹完wp類的構造函數和析構函數的實現之后,我們就來重點分析wp類的成員函數promote的實現。因為所有的弱指針都要通過調用這個成員函數來成功升級為強指針之后,才能操作它所引用的對象。wp類是如何使得一個弱指針不能直接操作它所引用的對象的呢?秘密就在于wp類沒有重載*和->操作符號,因此,我們就不能直接操作它所引用的對象。回到正題,wp類的成員函數promote的實現如下所示。 **frameworks/base/include/utils/RefBase.h** ~~~ template<typename T> sp<T> wp<T>::promote() const { return sp<T>(m_ptr, m_refs); } ~~~ 弱指針升級為強指針的方式是通過其內部的成員變量m_ptr和m_refs來創建一個強指針。 **frameworks/base/include/utils/RefBase.h** ~~~ template<typename T> sp<T>::sp(T* p, weakref_type* refs) : m_ptr((p && refs->attemptIncStrong(this)) ? p : 0) { } ~~~ 參數p指向對象的地址,而參數refs指向該對象內部的一個弱引用計數器對象。只有在對象地址不為NULL的情況下,才會調用它內部的弱引用計數器對象的成員函數attemptIncStrong來試圖增加該對象的強引用計數。如果能夠成功增加對象的強引用計數,那么就可以成功地把一個弱指針升級為一個強指針了。參數refs是一個類型為weakref_type的指針,因此,接下來就會調用weakref_type類的成員函數attemptIncStrong,它的實現如下所示。 **frameworks/base/libs/utils/RefBase.cpp** ~~~ bool RefBase::weakref_type::attemptIncStrong(const void* id) { incWeak(id); weakref_impl* const impl = static_cast<weakref_impl*>(this); int32_t curCount = impl->mStrong; LOG_ASSERT(curCount >= 0, "attemptIncStrong called on %p after underflow", this); while (curCount > 0 && curCount != INITIAL_STRONG_VALUE) { if (android_atomic_cmpxchg(curCount, curCount+1, &impl->mStrong) == 0) { break; } curCount = impl->mStrong; } if (curCount <= 0 || curCount == INITIAL_STRONG_VALUE) { bool allow; if (curCount == INITIAL_STRONG_VALUE) { // Attempting to acquire first strong reference... this is allowed // if the object does NOT have a longer lifetime (meaning the // implementation doesn’t need to see this), or if the implementation // allows it to happen. allow = (impl->mFlags&OBJECT_LIFETIME_WEAK) != OBJECT_LIFETIME_WEAK || impl->mBase->onIncStrongAttempted(FIRST_INC_STRONG, id); } else { // Attempting to revive the object... this is allowed // if the object DOES have a longer lifetime (so we can safely // call the object with only a weak ref) and the implementation // allows it to happen. allow = (impl->mFlags&OBJECT_LIFETIME_WEAK) == OBJECT_LIFETIME_WEAK && impl->mBase->onIncStrongAttempted(FIRST_INC_STRONG, id); } if (!allow) { decWeak(id); return false; } curCount = android_atomic_inc(&impl->mStrong); // 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->addWeakRef(id); impl->addStrongRef(id); #if PRINT_REFS LOGD("attemptIncStrong of %p from %p: cnt=%d\n", this, id, curCount); #endif if (curCount == INITIAL_STRONG_VALUE) { android_atomic_add(-INITIAL_STRONG_VALUE, &impl->mStrong); impl->mBase->onFirstRef(); } return true; } ~~~ weakref_type類的成員函數attemptIncStrong試圖增加目標對象的強引用計數,但是有可能會增加失敗,因為目標對象可能已經被釋放了,或者該目標對象不允許使用強指針引用它。 在前面的3.2.1小節中提到,在增加對象的強引用計數時,同時也會增加該對象的弱引用計數,因此,函數第3行首先調用成員函數incWeak來增加對象的弱引用計數。如果后面增加對象的強引用計數失敗,則會調用成員函數decWeak來減少對象的弱引用計數。前面提到,wp類的成員變量m_refs實際上指向的是一個weakref_impl對象,因此,第5行可以安全地將this指針轉換為一個weakref_impl指針,保存在變量impl中。 一個弱指針所引用的對象可能處于兩種狀態。第一種狀態是該對象同時也正在被其他強指針所引用,即它的強引用計數值大于0,并且不等于INITIAL_STRONG_VALUE;第二種狀態是該對象沒有被任何強指針引用,即它的強引用計數值小于等于0,或者等于INITIAL_STRONG_VALUE。接下來,我們就根據這兩種情況來分析一個弱指針是如何升級為一個強指針的。 第一種情況比較簡單,因為這時候對象是一定存在的,因此,我們就可以安全地將這個弱指針升級為強指針。函數第10行到第15行代碼處理的正是第一種情況,它將對象的強引用計數值增加1。在增加對象的強引用計數時,要保證原子性,因為其他地方也有可能正在對該對象的強引用計數進行操作。前面我們一般都是直接調用android_atomic_inc函數來保證加1操作的原子性的,但是第11行調用了函數android_atomic_cmpxchg來實現。函數android_atomic_cmpxchg是一個與CPU體系結構相關的函數,在某些提供了特殊指令的體系結構中,函數android_atomic_cmpxchg執行原子性的加1操作效率會比函數android_atomic_inc高一些。 函數android_atomic_cmpxchg的原型如下所示。 **system/core/include/cutils/atomic.h** ~~~ int android_atomic_release_cas(int32_t oldvalue, int32_t newvalue, volatile int32_t* addr); #define android_atomic_cmpxchg android_atomic_release_cas ~~~ 它只是一個宏定義,實際上指向的是函數android_atomic_release_cas。函數android_atomic_release_cas的工作原理是這樣的:如果它發現地址addr的內容等于參數oldvalue的值,那么它就會將地址addr的內容修改為newvalue,然后給調用者返回0,表示修改地址addr的內容成功;否則,就什么也不做, 然后給調用者返回1。 回到weakref_type類的成員函數attemptIncStrong中,第11行在調用android_atomic_cmpxchg函數時,將參數oldvalue的值設置為curCount,而將參數newvalue的值設置為curCount + 1。變量curCount保存的是對象的強引用計數,因此,第11行調用android_atomic_cmpxchg函數的目的是增加對象的強引用計數。在什么情況下,函數android_atomic_release_cas會不能成功地增加對象的強引用計數呢?在調用函數android_atomic_cmpxchg之前,對象的強引用計數保存在變量curCount中,在這種情況下,在調用函數android_atomic_cmpxchg時,有可能其他線程已經修改了對象的強引用計數。因此,就會導致函數android_atomic_cmpxchg不能成功地增加對象的強引用計數。如果出現這種情況,第10行到第15行的while循環就必須重新執行增加對象的強引用計數的操作。 在第二種情況下,第17行的if語句會為true,這時候情況就比較復雜了,因為此時對象可能存在,也可能不存在,要進一步進行判斷。 如果對象的強引用計數值等于INITIAL_STRONG_VALUE,即第19行的if語句為true,那么就說明這個對象從來沒有被強指針引用過。因此,第24行和第25行就根據對象的生命周期控制方式或者對象的實現來判斷是否允許將一個引用了它的弱指針升級為強指針。如果對象的生命周期只受強引用計數影響,那么就可以成功地將該弱指針升級為強指針。這一點比較容易理解,因為如果對象的生命周期只受強引用計數影響,而此時該對象又沒有被強指針引用過,那么它就必然不會被釋放;如果對象的生命周期受弱引用計數影響,即第24行的表達式(impl->mFlags&OBJECT_LIFETIME_WEAK) != OBJECT_LIFETIME_WEAK為false,那么就說明對象現在肯定是存在的,因為現在正有一個弱指針在引用它。但是,這種情況需要進一步調用對象的成員函數onIncStrongAttempted來確認對象是否允許強指針引用它。如果對象的成員函數onIncStrongAttempted的返回值為true,就說明允許使用強指針來引用它,因此,這種情況也可以成功地將該弱指針升級為強指針。RefBase類的成員函數在參數flags為FIRST_INC_STRONG的情況下,是允許將一個指向只受弱引用計數影響生命周期的對象的弱指針升級為一個強指針的,它的實現如下所示。 **frameworks/base/libs/utils/RefBase.cpp** ~~~ bool RefBase::onIncStrongAttempted(uint32_t flags, const void* id) { return (flags&FIRST_INC_STRONG) ? true : false; } ~~~ 如果對象的強引用計數值小于等于0,那么就會執行第31行和第32行代碼。這時候說明對象之前被強指針引用過,因此,就必須要保證對象的生命周期受到弱引用計數的影響;否則,對象就已經被釋放了。當對象的生命周期控制標志值的OBJECT_LIFETIME_WEAK位不等于0時,就說明對象的生命周期受到弱引用計數的影響,但是這時候還不能直接將該弱指針升級為強指針,因為對象可能不希望它被強指針引用。因此,就需要調用對象的成員函數onIncStrongAttempted來進一步確認。如果對象的成員函數onIncStrongAttempted的返回值為true,就說明允許將該弱指針升級為強指針。 在第二種情況下,如果最終得到的變量allow的值為false,那么就說明弱指針升級為強指針失敗,于是函數第36行就直接返回false給調用者了。在返回之前,第35行會調用成員函數decWeak來減少對象的弱引用計數,因為函數在開始的地方調用了成員函數incWeak來增加對象的弱引用計數。另外,如果最終得到的變量allow的值為true,那么就說明弱指針可以升級為強指針,因此,第38行就會增加對象的強引用計數。 至此,弱指針升級為強指針的過程就介紹完了,它是弱指針實現原理中最具有挑戰性的一個環節。 Android系統提供的強指針和弱指針的實現比較復雜,接下來,我們再通過一個實例來說明它們的使用方法,以便加深對它們的理解。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看