weak 的实现

2020-11-20  本文已影响0人  forping

代码如下

- (void)testWeak{
    __weak id weaSelf = self;
}

转成汇编

"-[A testWeak]":                        ; @"\01-[A testWeak]"
    sub sp, sp, #48             ; =48
    stp x29, x30, [sp, #32]     ; 16-byte Folded Spill
    add x29, sp, #32            ; =32
    stur    x0, [x29, #-8]
    str x1, [sp, #16]
    ldur    x1, [x29, #-8]
    add x8, sp, #8              ; =8
    mov x0, x8
    str x8, [sp]                ; 8-byte Folded Spill
    bl  _objc_initWeak
    ldr x8, [sp]                ; 8-byte Folded Reload
    mov x0, x8
    bl  _objc_destroyWeak
    ldp x29, x30, [sp, #32]     ; 16-byte Folded Reload
    add sp, sp, #48             ; =48
    ret

调用了 _objc_initWeak 和 _objc_destroyWeak 函数
去runtime里去找对应的实现

id
objc_initWeak(id *location, id newObj)
{
    if (!newObj) {
        *location = nil;
        return nil;
    }

    return storeWeak<DontHaveOld, DoHaveNew, DoCrashIfDeallocating>
        (location, (objc_object*)newObj);
}

void
objc_destroyWeak(id *location)
{
    (void)storeWeak<DoHaveOld, DontHaveNew, DontCrashIfDeallocating>
        (location, nil);
}

未完待续~

上一篇下一篇

猜你喜欢

热点阅读