溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android系統(tǒng)中強指針是如何實現(xiàn)的

發(fā)布時間:2021-10-09 09:13:13 來源:億速云 閱讀:134 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“Android系統(tǒng)中強指針是如何實現(xiàn)的”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“Android系統(tǒng)中強指針是如何實現(xiàn)的”吧!

強指針和弱指針基礎(chǔ)

android中的智能指針包括:輕量級指針、強指針、弱指針。
強指針:它主要是通過強引用計數(shù)來進行維護對象的生命周期。
弱指針:它主要是通過弱引用計數(shù)來進行維護所指向?qū)ο蟮纳芷凇?br/>如果在一個類中使用了強指針或者弱指針的技術(shù),那么這個類就必須從RefBase這個類進行做繼承,因為強指針和弱指針是通過RefBase這個類來提供實現(xiàn)的引用計數(shù)器。
強指針和弱指針關(guān)系相對于輕量級指針來說更加親密,因此他們一般是相互配合使用的。

強指針原理分析

以下針對源碼的分析都是來源于android5.0系統(tǒng)源碼
強指針的定義實現(xiàn)主要在\frameworks\rs\cpp\util\RefBase.h文件中

class RefBase
{
public:
            //定義了成員變量用于維護強引用對象的引用計數(shù)
            void            incStrong(const void* id) const;
            //定義了成員變量用于維護強引用對象的引用計數(shù)
            void            decStrong(const void* id) const;
          
            void            forceIncStrong(const void* id) const;

            //獲取強指針計數(shù)的數(shù)量.
            int32_t         getStrongCount() const;
    //這個類主要實現(xiàn)計數(shù)器的
    class weakref_type
    {
    public:
        RefBase*            refBase() const;

        void                incWeak(const void* id);
        void                decWeak(const void* id);

        // acquires a strong reference if there is already one.
        bool                attemptIncStrong(const void* id);

        // acquires a weak reference if there is already one.
        // This is not always safe. see ProcessState.cpp and BpBinder.cpp
        // for proper use.
        bool                attemptIncWeak(const void* id);

        //! DEBUGGING ONLY: Get current weak ref count.
        int32_t             getWeakCount() const;

        //! DEBUGGING ONLY: Print references held on object.
        void                printRefs() const;

        //! DEBUGGING ONLY: Enable tracking for this object.
        // enable -- enable/disable tracking
        // retain -- when tracking is enable, if true, then we save a stack trace
        //           for each reference and dereference; when retain == false, we
        //           match up references and dereferences and keep only the
        //           outstanding ones.

        void                trackMe(bool enable, bool retain);
    };

            weakref_type*   createWeak(const void* id) const;

            weakref_type*   getWeakRefs() const;

            //! DEBUGGING ONLY: Print references held on object.
    inline  void            printRefs() const { getWeakRefs()->printRefs(); }

            //! DEBUGGING ONLY: Enable tracking of object.
    inline  void            trackMe(bool enable, bool retain)
    {
        getWeakRefs()->trackMe(enable, retain);
    }

    typedef RefBase basetype;

protected:
                            RefBase();
    virtual                 ~RefBase();

    //! Flags for extendObjectLifetime()
    enum {
        OBJECT_LIFETIME_STRONG  = 0x0000,
        OBJECT_LIFETIME_WEAK    = 0x0001,
        OBJECT_LIFETIME_MASK    = 0x0001
    };

            void            extendObjectLifetime(int32_t mode);

    //! Flags for onIncStrongAttempted()
    enum {
        FIRST_INC_STRONG = 0x0001
    };

    virtual void            onFirstRef();
    virtual void            onLastStrongRef(const void* id);
    virtual bool            onIncStrongAttempted(uint32_t flags, const void* id);
    virtual void            onLastWeakRef(const void* id);

private:
    friend class ReferenceMover;
    static void moveReferences(void* d, void const* s, size_t n,
            const ReferenceConverterBase& caster);

private:
    friend class weakref_type;
    //通過類對象來獲取計數(shù)器數(shù)據(jù)。
    class weakref_impl;

                            RefBase(const RefBase& o);
            RefBase&        operator=(const RefBase& o);

        weakref_impl* const mRefs;
};

通過以上類定義可以看到 RefBase類里面嵌套著weakref_type類,這個weakref_type類也的對象mRefs來描述對象的引用計數(shù)。也就是說每一個RefBase對象都包含一個weakref_type對象。
virtual表示的是虛函數(shù),friend表示友元函數(shù),

到此,相信大家對“Android系統(tǒng)中強指針是如何實現(xiàn)的”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI