一文理解Android系統(tǒng)中強(qiáng)指針的實(shí)現(xiàn)
強(qiáng)指針和弱指針基礎(chǔ)
android中的智能指針包括:輕量級(jí)指針、強(qiáng)指針、弱指針。
強(qiáng)指針:它主要是通過(guò)強(qiáng)引用計(jì)數(shù)來(lái)進(jìn)行維護(hù)對(duì)象的生命周期。
弱指針:它主要是通過(guò)弱引用計(jì)數(shù)來(lái)進(jìn)行維護(hù)所指向?qū)ο蟮纳芷凇?br />
如果在一個(gè)類中使用了強(qiáng)指針或者弱指針的技術(shù),那么這個(gè)類就必須從RefBase這個(gè)類進(jìn)行做繼承,因?yàn)閺?qiáng)指針和弱指針是通過(guò)RefBase這個(gè)類來(lái)提供實(shí)現(xiàn)的引用計(jì)數(shù)器。
強(qiáng)指針和弱指針關(guān)系相對(duì)于輕量級(jí)指針來(lái)說(shuō)更加親密,因此他們一般是相互配合使用的。
強(qiáng)指針原理分析
以下針對(duì)源碼的分析都是來(lái)源于android5.0系統(tǒng)源碼
強(qiáng)指針的定義實(shí)現(xiàn)主要在\frameworks\rs\cpp\util\RefBase.h文件中
class RefBase
{
public:
//定義了成員變量用于維護(hù)強(qiáng)引用對(duì)象的引用計(jì)數(shù)
void incStrong(const void* id) const;
//定義了成員變量用于維護(hù)強(qiáng)引用對(duì)象的引用計(jì)數(shù)
void decStrong(const void* id) const;
void forceIncStrong(const void* id) const;
//獲取強(qiáng)指針計(jì)數(shù)的數(shù)量.
int32_t getStrongCount() const;
//這個(gè)類主要實(shí)現(xiàn)計(jì)數(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;
//通過(guò)類對(duì)象來(lái)獲取計(jì)數(shù)器數(shù)據(jù)。
class weakref_impl;
RefBase(const RefBase& o);
RefBase& operator=(const RefBase& o);
weakref_impl* const mRefs;
};
通過(guò)以上類定義可以看到 RefBase類里面嵌套著weakref_type類,這個(gè)weakref_type類也的對(duì)象mRefs來(lái)描述對(duì)象的引用計(jì)數(shù)。也就是說(shuō)每一個(gè)RefBase對(duì)象都包含一個(gè)weakref_type對(duì)象。
virtual表示的是虛函數(shù),friend表示友元函數(shù),
總結(jié)
如果一個(gè)對(duì)象的生命周期控制標(biāo)志值被設(shè)置為0的情況下,只要它的強(qiáng)引用計(jì)數(shù)值也為0,那么系統(tǒng)就會(huì)自動(dòng)釋放這個(gè)對(duì)象。
到此這篇關(guān)于一文理解Android系統(tǒng)中強(qiáng)指針的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Android 強(qiáng)指針內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android-自定義控件之ListView下拉刷新的實(shí)現(xiàn)
本篇文章主要介紹了Android-自定義控件之ListView下拉刷新示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
手機(jī)/移動(dòng)前端開(kāi)發(fā)需要注意的20個(gè)要點(diǎn)
本文主要介紹了手機(jī)/移動(dòng)前端開(kāi)發(fā)需要注意的20個(gè)要點(diǎn),具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
FloatingActionButton增強(qiáng)版一個(gè)按鈕跳出多個(gè)按鈕第三方開(kāi)源之FloatingActionButton
這篇文章主要介紹了FloatingActionButton增強(qiáng)版一個(gè)按鈕跳出多個(gè)按鈕第三方開(kāi)源之FloatingActionButton 的相關(guān)資料,需要的朋友可以參考下2015-12-12
Android冷啟動(dòng)實(shí)現(xiàn)app秒開(kāi)的實(shí)現(xiàn)代碼
本篇文章主要介紹了Android冷啟動(dòng)實(shí)現(xiàn)app秒開(kāi)的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
Flutter實(shí)現(xiàn)密碼強(qiáng)度校驗(yàn)結(jié)果的示例詳解
我們經(jīng)常在一些網(wǎng)站上看到這樣的密碼強(qiáng)度指示,使用三段線,分別用不同的顏色來(lái)表示弱密碼、中等強(qiáng)度密碼和強(qiáng)密碼,本篇我們就用?Flutter?來(lái)實(shí)現(xiàn)這樣一個(gè)密碼強(qiáng)度校驗(yàn)示例,希望對(duì)大家有所幫助2023-08-08
Jetpack Compose圖片組件使用實(shí)例詳細(xì)講解
在Compose中,圖片組件主要有兩種,分別是顯示圖標(biāo)的Icon組件和顯示圖片的Image組件,當(dāng)我們顯示一系列的小圖標(biāo)的時(shí)候,我們可以使用Icon組件,當(dāng)顯示圖片時(shí),我們就用專用的Image組件2023-04-04
Android獲取手機(jī)配置信息具體實(shí)現(xiàn)代碼
下面為大家介紹下使用android獲取手機(jī)配置信息的具體過(guò)程,感興趣的朋友可以參考下哈,希望對(duì)你有所幫助2013-06-06
Android 利用 APT 技術(shù)在編譯期生成代碼
本文主要講解Android 利用 APT 技術(shù)在編譯期生成代碼,這里提供詳細(xì)的資料,并講解如何實(shí)現(xiàn),有興趣的小伙伴可以參考下2016-08-08
Flutter實(shí)現(xiàn)編寫(xiě)富文本Text的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何通過(guò)Flutter實(shí)現(xiàn)編寫(xiě)富文本Text,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2022-11-11

