ARC 中編譯生成的 C 函數(shù)分析

此文實(shí)際成于 2015/07/29

objc_retain

__attribute__((aligned(16)))
id 
objc_retain(id obj)
{
    if (!obj) return obj;
    if (obj->isTaggedPointer()) return obj;
    return obj->retain();
}

主要調(diào)用以下其類方法:

// Equivalent to calling [this retain], with shortcuts if there is no override
inline id 
objc_object::retain()
{
    // UseGC is allowed here, but requires hasCustomRR.
    assert(!UseGC  ||  ISA()->hasCustomRR());
    assert(!isTaggedPointer());

    if (! ISA()->hasCustomRR()) {
        return sidetable_retain();
    }

    return ((id(*)(objc_object *, SEL))objc_msgSend)(this, SEL_retain);
}

因此主要就是這一句代碼在起 retian的作用:

return ((id(*)(objc_object *, SEL))objc_msgSend)(this, SEL_retain);
它向?qū)ο蟀l(fā)送retain的消息买喧。

即調(diào)用:

// Replaced by ObjectAlloc
- (id)retain {
    return ((id)self)->rootRetain();
}

調(diào)用:

// Base retain implementation, ignoring overrides.
// This does not check isa.fast_rr; if there is an RR override then 
// it was already called and it chose to call [super retain].
inline id 
objc_object::rootRetain()
{
    assert(!UseGC);

    if (isTaggedPointer()) return (id)this;
    return sidetable_retain();
}

然后到:

id
objc_object::sidetable_retain()
{
#if SUPPORT_NONPOINTER_ISA
    assert(!isa.indexed);
#endif
    SideTable *table = SideTable::tableForPointer(this);

    if (spinlock_trylock(&table->slock)) {
        size_t& refcntStorage = table->refcnts[this];
        if (! (refcntStorage & SIDE_TABLE_RC_PINNED)) {
            refcntStorage += SIDE_TABLE_RC_ONE;
        }
        spinlock_unlock(&table->slock);
        return (id)this;
    }
    return sidetable_retain_slow(table);
}

主要就是引用計(jì)數(shù)加一: refcntStorage += SIDE_TABLE_RC_ONE;
不過有點(diǎn)費(fèi)解的是挟秤,SIDE_TABLE_RC_ONE的宏定義如下:
#define SIDE_TABLE_RC_ONE (1UL<<2) // MSB-ward of deallocating bit

這樣每次加的值就是 1 << 2 = 4了量九。

因?yàn)槲倚枰宄鋜etainCount 的對(duì)應(yīng)方法日川。所以:

objc_object::rootRetainCount()

- (NSUInteger)retainCount {
    return ((id)self)->rootRetainCount();
}

接著:

inline uintptr_t 
objc_object::rootRetainCount()
{
    assert(!UseGC);

    if (isTaggedPointer()) return (uintptr_t)this;
    return sidetable_retainCount();
}

然后:

uintptr_t
objc_object::sidetable_retainCount()
{
    SideTable *table = SideTable::tableForPointer(this);

    size_t refcnt_result = 1;
    
    spinlock_lock(&table->slock);
    RefcountMap::iterator it = table->refcnts.find(this);
    if (it != table->refcnts.end()) {
        // this is valid for SIDE_TABLE_RC_PINNED too
        refcnt_result += it->second >> SIDE_TABLE_RC_SHIFT;
    }
    spinlock_unlock(&table->slock);
    return refcnt_result;
}

先看下這個(gè)SHIFT
#define SIDE_TABLE_RC_SHIFT 2

int >> 2 相當(dāng)于是除4的效果渐扮。之前一是一次加4稻爬,現(xiàn)在的引用計(jì)數(shù)的結(jié)果是以除4的結(jié)果計(jì)算剃毒。
所以也還是相對(duì)應(yīng)的樊拓。
因?yàn)榧俪湟糜?jì)數(shù)為 2 4 + 4 = 8 然后 8 >> 2 = 2

objc_release

retain它后面調(diào)用如下:

// Base release implementation, ignoring overrides.
// Does not call -dealloc.
// Returns true if the object should now be deallocated.
// This does not check isa.fast_rr; if there is an RR override then 
// it was already called and it chose to call [super release].
inline bool 
objc_object::rootRelease()
{
    assert(!UseGC);

    if (isTaggedPointer()) return false;
    return sidetable_release(true);
}

最后都轉(zhuǎn)變成操作 SideTable 的相關(guān)操作:

bool 
objc_object::sidetable_release(bool performDealloc)
{
#if SUPPORT_NONPOINTER_ISA
    assert(!isa.indexed);
#endif
    SideTable *table = SideTable::tableForPointer(this);

    bool do_dealloc = false;

    if (spinlock_trylock(&table->slock)) {
        RefcountMap::iterator it = table->refcnts.find(this);
        if (it == table->refcnts.end()) {
            do_dealloc = true;
            table->refcnts[this] = SIDE_TABLE_DEALLOCATING;
        } else if (it->second < SIDE_TABLE_DEALLOCATING) {
            // SIDE_TABLE_WEAKLY_REFERENCED may be set. Don't change it.
            do_dealloc = true;
            it->second |= SIDE_TABLE_DEALLOCATING;
        } else if (! (it->second & SIDE_TABLE_RC_PINNED)) {
            it->second -= SIDE_TABLE_RC_ONE;
        }
        spinlock_unlock(&table->slock);
        if (do_dealloc  &&  performDealloc) {
            ((void(*)(objc_object *, SEL))objc_msgSend)(this, SEL_dealloc);
        }
        return do_dealloc;
    }

    return sidetable_release_slow(table, performDealloc);
}

看到上面判斷引用計(jì)數(shù)是否為 0餐塘,總算是明白了為什么引用計(jì)數(shù)加1要加4了妥衣。
當(dāng)值為 1,和 2 是用作特殊的標(biāo)志的戒傻。

#define SIDE_TABLE_WEAKLY_REFERENCED (1UL<<0)
#define SIDE_TABLE_DEALLOCATING      (1UL<<1)  // MSB-ward of weak bit

附錄

SUPPORT_NONPOINTER_ISA

上面的宏判斷
對(duì)于 基于 arm64的 iOS 上 Objective-C 的對(duì)象的isa字段不再是一個(gè)指針税手。
對(duì)于此轉(zhuǎn)變的解釋見: http://www.sealiesoftware.com/blog/archive/2013/09/24/objc_explain_Non-pointer_isa.html
宏聲明如下:

// Define SUPPORT_NONPOINTER_ISA=1 to enable extra data in the isa field.
#if !__LP64__  ||  TARGET_OS_WIN32  ||  TARGET_IPHONE_SIMULATOR  ||  __x86_64__
#   define SUPPORT_NONPOINTER_ISA 0
#else
#   define SUPPORT_NONPOINTER_ISA 1
#endif
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市需纳,隨后出現(xiàn)的幾起案子芦倒,更是在濱河造成了極大的恐慌,老刑警劉巖不翩,帶你破解...
    沈念sama閱讀 217,277評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件兵扬,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡口蝠,警方通過查閱死者的電腦和手機(jī)器钟,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來妙蔗,“玉大人傲霸,你說我怎么就攤上這事。” “怎么了昙啄?”我有些...
    開封第一講書人閱讀 163,624評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵穆役,是天一觀的道長。 經(jīng)常有香客問我跟衅,道長孵睬,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,356評(píng)論 1 293
  • 正文 為了忘掉前任伶跷,我火速辦了婚禮掰读,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘叭莫。我一直安慰自己蹈集,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評(píng)論 6 392
  • 文/花漫 我一把揭開白布雇初。 她就那樣靜靜地躺著拢肆,像睡著了一般。 火紅的嫁衣襯著肌膚如雪靖诗。 梳的紋絲不亂的頭發(fā)上郭怪,一...
    開封第一講書人閱讀 51,292評(píng)論 1 301
  • 那天,我揣著相機(jī)與錄音刊橘,去河邊找鬼鄙才。 笑死,一個(gè)胖子當(dāng)著我的面吹牛促绵,可吹牛的內(nèi)容都是我干的攒庵。 我是一名探鬼主播,決...
    沈念sama閱讀 40,135評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼败晴,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼浓冒!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起尖坤,我...
    開封第一講書人閱讀 38,992評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤稳懒,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后糖驴,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體僚祷,經(jīng)...
    沈念sama閱讀 45,429評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評(píng)論 3 334
  • 正文 我和宋清朗相戀三年贮缕,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片俺榆。...
    茶點(diǎn)故事閱讀 39,785評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡感昼,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出罐脊,到底是詐尸還是另有隱情定嗓,我是刑警寧澤蜕琴,帶...
    沈念sama閱讀 35,492評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站宵溅,受9級(jí)特大地震影響凌简,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜恃逻,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評(píng)論 3 328
  • 文/蒙蒙 一雏搂、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧寇损,春花似錦凸郑、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至浊吏,卻和暖如春而昨,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背找田。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評(píng)論 1 269
  • 我被黑心中介騙來泰國打工歌憨, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人午阵。 一個(gè)月前我還...
    沈念sama閱讀 47,891評(píng)論 2 370
  • 正文 我出身青樓躺孝,卻偏偏與公主長得像,于是被迫代替她去往敵國和親底桂。 傳聞我的和親對(duì)象是個(gè)殘疾皇子植袍,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容