iOS-OC对象原理_alloc&init

alloc&init探索
- (void)viewDidLoad {
    [super viewDidLoad];
    // 熟悉的入手 - 对象
    // alloc 做了什么
    // init 做了什么
    ZZPerson *p1 = [ZZPerson alloc];
    ZZPerson *p2 = [p1 init];
    ZZPerson *p3 = [p1 init];
    ZZNSLog(@"%@ - %p - %p",p1,p1,&p1);
    ZZNSLog(@"%@ - %p - %p",p2,p2,&p2);
    ZZNSLog(@"%@ - %p - %p",p3,p3,&p3);
}

NSLog结果:

 - 0x600003c9cc30 - 0x7ffee24cc0f8
 - 0x600003c9cc30 - 0x7ffee24cc0f0
 - 0x600003c9cc30 - 0x7ffee24cc0e8

用图表示大概是这个样子:

指针表示.png

&p1表示我是谁(我在栈上的位置),p1表示我指向的位置(指向堆的值的地址)。从打印的信息可以看出,alloc开辟了空间,而init只是持有了这个空间。

通过objc4-781探索alloc&init的底层

关于objc4-781参考链接:Source Browser
首先进入alloc方法,我们会看到:

+ (id)alloc {
    return _objc_rootAlloc(self);
}

内部调用了_objc_rootAlloc()方法

id _objc_rootAlloc(Class cls)
{
    return callAlloc(cls, false/*checkNil*/, true/*allocWithZone*/);
}

接下来是callAlloc()

static ALWAYS_INLINE id
callAlloc(Class cls, bool checkNil, bool allocWithZone=false)
{
#if __OBJC2__
    if (slowpath(checkNil && !cls)) return nil;
    if (fastpath(!cls->ISA()->hasCustomAWZ())) {
        return _objc_rootAllocWithZone(cls, nil);
    }
#endif

    // No shortcuts available.
    if (allocWithZone) {
        return ((id(*)(id, SEL, struct _NSZone *))objc_msgSend)(cls, @selector(allocWithZone:), nil);
    }
    return ((id(*)(id, SEL))objc_msgSend)(cls, @selector(alloc));
}

这里通过断点发现调用了_objc_rootAllocWithZone()

id _objc_rootAllocWithZone(Class cls, malloc_zone_t *zone __unused)
{
    // allocWithZone under __OBJC2__ ignores the zone parameter
    return _class_createInstanceFromZone(cls, 0, nil,
                                         OBJECT_CONSTRUCT_CALL_BADALLOC);
}

_class_createInstanceFromZone ()

static ALWAYS_INLINE id
_class_createInstanceFromZone(Class cls, size_t extraBytes, void *zone,
                              int construct_flags = OBJECT_CONSTRUCT_NONE,
                              bool cxxConstruct = true,
                              size_t *outAllocatedSize = nil)
{
    ASSERT(cls->isRealized());

    // Read class's info bits all at once for performance
    bool hasCxxCtor = cxxConstruct && cls->hasCxxCtor();
    bool hasCxxDtor = cls->hasCxxDtor();
    bool fast = cls->canAllocNonpointer();
    size_t size;
    // 1:要开辟多少内存
    size = cls->instanceSize(extraBytes);
    if (outAllocatedSize) *outAllocatedSize = size;

    id obj;
    if (zone) {
        obj = (id)malloc_zone_calloc((malloc_zone_t *)zone, 1, size);
    } else {
        // 2;怎么去申请内存
        obj = (id)calloc(1, size);
    }
    if (slowpath(!obj)) {
        if (construct_flags & OBJECT_CONSTRUCT_CALL_BADALLOC) {
            return _objc_callBadAllocHandler(cls);
        }
        return nil;
    }

    // 3: ?
    if (!zone && fast) {
        obj->initInstanceIsa(cls, hasCxxDtor);
    } else {
        // Use raw pointer isa on the assumption that they might be
        // doing something weird with the zone or RR.
        obj->initIsa(cls);
    }

    if (fastpath(!hasCxxCtor)) {
        return obj;
    }

    construct_flags |= OBJECT_CONSTRUCT_FREE_ONFAILURE;
    return object_cxxConstructFromClass(obj, cls, construct_flags);
}

_class_createInstanceFromZone ()中有个核心的三部曲:

  • size = cls->instanceSize(extraBytes):申请要开辟的内存大小
  • obj = (id)calloc(1, size);:根据大小开辟内存空间
  • obj->initInstanceIsa(cls, hasCxxDtor);将class与开辟的空间建立绑定
alloc执行流程图

再看init方法

- (id)init {
    return _objc_rootInit(self);
}

_objc_rootInit

id
_objc_rootInit(id obj)
{
    // In practice, it will be hard to rely on this function.
    // Many classes do not properly chain -init calls.
    return obj;
}

直接返回了当前实例对象。

你可能感兴趣的:(iOS-OC对象原理_alloc&init)