溫馨提示×

溫馨提示×

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

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

Android系統中強指針是如何實現的

發布時間:2021-10-09 09:13:13 來源:億速云 閱讀:152 作者:iii 欄目:開發技術

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

強指針和弱指針基礎

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

強指針原理分析

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

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

            //獲取強指針計數的數量.
            int32_t         getStrongCount() const;
    //這個類主要實現計數器的
    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;
    //通過類對象來獲取計數器數據。
    class weakref_impl;

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

        weakref_impl* const mRefs;
};

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

到此,相信大家對“Android系統中強指針是如何實現的”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女