溫馨提示×

溫馨提示×

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

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

Android系統(tǒng)中輕量級指針是如何實(shí)現(xiàn)的

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

這篇文章主要介紹“Android系統(tǒng)中輕量級指針是如何實(shí)現(xiàn)的”,在日常操作中,相信很多人在Android系統(tǒng)中輕量級指針是如何實(shí)現(xiàn)的問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Android系統(tǒng)中輕量級指針是如何實(shí)現(xiàn)的”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

智能指針來源

引發(fā)指針錯誤情況表現(xiàn)常常有如下幾個表現(xiàn)情況:
1.申請了內(nèi)存空間,但是忘記釋放指針?biāo)赶虻膶ο笳加玫膬?nèi)存空間。
2.使用了無效的指針。
因此在android的C++代碼部分采用了智能指針的技術(shù)。智能指針通過一種能夠自動危害對象引用計(jì)數(shù)的技術(shù)。來解決C++中指針存在的缺陷問題。
在android系統(tǒng)中提供了三種類型的C++智能指針,分別為:輕量級智能指針、強(qiáng)指針、弱指針。
下面主要通過進(jìn)行分析輕量級指針的實(shí)現(xiàn)原理。

輕量級指針

輕量級指針就是通過利用簡單的引用計(jì)數(shù)計(jì)數(shù)類維護(hù)對象的生命周期,如果一個類的對象支持使用輕量級指針,那么它就必須要從LightRefBase類進(jìn)行繼承,因?yàn)檫@個LightRefBase類提供了一個簡單的引用計(jì)數(shù)器。

LightRefBase類定義

下面的源碼主要依據(jù)android5.0的源碼進(jìn)行分析的。LightRefBase類的定義在android系統(tǒng)中的\frameworks\rs\cpp\util\RefBase.h 這個文件中。
LightRefBase類也是一個模板類。模板參數(shù)T表示對象的實(shí)際類型,它必須進(jìn)行對LightRefBase這個類繼承。

//模板類
template <class T>
class LightRefBase
{
public:
    //公共的內(nèi)聯(lián)函數(shù)
    inline LightRefBase() : mCount(0) { }
    inline void incStrong(__attribute__((unused)) const void* id) const {
        __sync_fetch_and_add(&mCount, 1);
    }
    inline void decStrong(__attribute__((unused)) const void* id) const {
        if (__sync_fetch_and_sub(&mCount, 1) == 1) {
            delete static_cast<const T*>(this);
        }
    }
    //! DEBUGGING ONLY: Get current strong ref count.
    inline int32_t getStrongCount() const {
        return mCount;
    }

    typedef LightRefBase<T> basetype;

protected:
    //內(nèi)聯(lián)的虛構(gòu)函數(shù)
    inline ~LightRefBase() { }

private:
    friend class ReferenceMover;
    inline static void moveReferences(void*, void const*, size_t,
            const ReferenceConverterBase&) { }

private:
    mutable volatile int32_t mCount;
};

上面LightRefBase類定義涉及到幾個知識點(diǎn):

  • 1、C++的三個訪問權(quán)限類型:public、protected、private。

public:可以被任意實(shí)體訪問。
protected:只允許子類及本類的成員函數(shù)訪問。
private:只允許本類的成員函數(shù)訪問。

  • 2、C++中的inline內(nèi)聯(lián)函數(shù)

在函數(shù)第一部分如果包含有inline關(guān)鍵字的函數(shù),那么這個函數(shù)就表示為內(nèi)聯(lián)函數(shù)。內(nèi)聯(lián)函數(shù)主要為了解決一些頻繁調(diào)用的小函數(shù)大量消耗棧空間(棧內(nèi)存)的問題。
inline的使用是有所限制的,inline只適合涵數(shù)體內(nèi)代碼簡單的涵數(shù)使用,不能包含復(fù)雜的結(jié)構(gòu)控制語句例如while、switch,并且不能內(nèi)聯(lián)函數(shù)本身不能是直接遞歸函數(shù)(遞歸函數(shù):自己內(nèi)部還調(diào)用自己的函數(shù))。

  • 3、C++中的friend友元函數(shù)

C++中的友元機(jī)制允許類的非公有成員被一個類或者函數(shù)訪問,友元按類型分為三種:普通非類成員函數(shù)作為友元,類的成員函數(shù)作為友元,類作為友元。
友元函數(shù)是可以直接訪問類的私有成員的非成員函數(shù)。它是定義在類外的普通函數(shù),它不屬于任何類,但需要在類的定義中加以聲明,聲明時只需在友元的名稱前加上關(guān)鍵字friend。

  • 4、C++中的mutable關(guān)鍵字

mutable是為了突破const的限制而設(shè)置的。被mutable修飾的變量,將永遠(yuǎn)處于可變的狀態(tài),即使在一個const函數(shù)中。

  • 5、C++中的template類模板

一個類模板(也稱為類屬類或類生成類)同意用戶為類定義一種模式。使得類中的某些數(shù)據(jù)成員、默寫成員函數(shù)的參數(shù)、某些成員函數(shù)的返回值,能夠取隨意類型(包含系統(tǒng)提前定義的和用戶自己定義的)。
類模板的應(yīng)用場景:多個類有著共同操作,但是數(shù)據(jù)類型不同。

LightRefBase的實(shí)現(xiàn)類

輕量級LightRefBase類的實(shí)現(xiàn)類為wp類,它也是在\frameworks\rs\cpp\util\RefBase.h 這個文件中。wp也是一個模板類,模板參數(shù)T表示的是對象的實(shí)際類型,它必須繼承LightRefBase類。由于wp類也是強(qiáng)指針的實(shí)現(xiàn)類,因此我們只需要分析下該wp類中關(guān)于輕量級指針類的實(shí)現(xiàn)部分就可以了。

//模板類
template <typename T>
class wp
{
public:
    typedef typename RefBase::weakref_type weakref_type;
    //輕量級指針需要用到的構(gòu)造函數(shù)
    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);
    //輕量級指針需要用到的虛構(gòu)函數(shù)
    ~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;
    //輕量級指針會用到的變量m_ptr
    T*              m_ptr;
    weakref_type*   m_refs;
};

到此,關(guān)于“Android系統(tǒng)中輕量級指針是如何實(shí)現(xiàn)的”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

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

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

AI