一文理解Android系統(tǒng)中強(qiáng)指針的實現(xiàn)
強(qiáng)指針和弱指針基礎(chǔ)
android中的智能指針包括:輕量級指針、強(qiáng)指針、弱指針。
強(qiáng)指針:它主要是通過強(qiáng)引用計數(shù)來進(jìn)行維護(hù)對象的生命周期。
弱指針:它主要是通過弱引用計數(shù)來進(jìn)行維護(hù)所指向?qū)ο蟮纳芷凇?br />
如果在一個類中使用了強(qiáng)指針或者弱指針的技術(shù),那么這個類就必須從RefBase這個類進(jìn)行做繼承,因為強(qiáng)指針和弱指針是通過RefBase這個類來提供實現(xiàn)的引用計數(shù)器。
強(qiáng)指針和弱指針關(guān)系相對于輕量級指針來說更加親密,因此他們一般是相互配合使用的。
強(qiáng)指針原理分析
以下針對源碼的分析都是來源于android5.0系統(tǒng)源碼
強(qiáng)指針的定義實現(xiàn)主要在\frameworks\rs\cpp\util\RefBase.h文件中
class RefBase
{
public:
//定義了成員變量用于維護(hù)強(qiáng)引用對象的引用計數(shù)
void incStrong(const void* id) const;
//定義了成員變量用于維護(hù)強(qiáng)引用對象的引用計數(shù)
void decStrong(const void* id) const;
void forceIncStrong(const void* id) const;
//獲取強(qiáng)指針計數(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ù),
總結(jié)
如果一個對象的生命周期控制標(biāo)志值被設(shè)置為0的情況下,只要它的強(qiáng)引用計數(shù)值也為0,那么系統(tǒng)就會自動釋放這個對象。
到此這篇關(guān)于一文理解Android系統(tǒng)中強(qiáng)指針的實現(xiàn)的文章就介紹到這了,更多相關(guān)Android 強(qiáng)指針內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android Material Design 陰影實現(xiàn)示例
這篇文章主要介紹了Android Material Design 陰影實現(xiàn)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
android使用viewpager計算偏移量實現(xiàn)選項卡功能
這篇文章主要為大家詳細(xì)介紹了android使用viewpager計算偏移量實現(xiàn)選項卡功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12
使用RecylerView完成拖動排序高仿qq側(cè)滑刪除功能
最近在做一個android項目,使用到Recylerview完成拖動排序,側(cè)滑刪除功能,今天小編把思路分享到腳本之家平臺,供大家學(xué)習(xí)2016-10-10
android使用PullToRefresh框架實現(xiàn)ListView下拉刷新上拉加載更多
這篇文章主要介紹了android使用PullToRefresh框架實現(xiàn)ListView下拉刷新上拉加載更多,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
Android使用相機(jī)實現(xiàn)拍照存儲及展示功能詳解
這篇文章主要介紹了Android使用相機(jī)實現(xiàn)拍照存儲及展示功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01
Android 實現(xiàn)銀聯(lián)刷卡機(jī)消費后手動簽名的功能(示例代碼)
在一些商場購物時,不需要用筆在銀聯(lián)機(jī)上簽名了,直接用手指觸摸實現(xiàn)消費簽名,非常方便,下面小編給大家分享Android 實現(xiàn)銀聯(lián)刷卡機(jī)消費后手動簽名的功能,需要的朋友參考下吧2017-12-12
Android中WebView加載網(wǎng)頁設(shè)置進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android中WebView加載網(wǎng)頁設(shè)置進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05
Android實例代碼理解設(shè)計模式SOLID六大原則
程序設(shè)計領(lǐng)域, SOLID (單一功能、開閉原則、里氏替換、接口隔離以及依賴反轉(zhuǎn))是由羅伯特·C·馬丁在21世紀(jì)早期 引入的記憶術(shù)首字母縮略字,指代了面向?qū)ο缶幊毯兔嫦驅(qū)ο笤O(shè)計的基本原則2021-10-10

