美文网首页
iOS alloc & init 探索

iOS alloc & init 探索

作者: 猿人 | 来源:发表于2020-09-09 15:53 被阅读0次

初探

1、新建一个Person 类 先思考打印情况 再运行

 Person *p1 = [Person alloc];
 Person *p2 = [p1 init];
 Person *p3 = [p2 init];
 NSLog(@"%@ - %p - %p",p1,p1,&p1);
 NSLog(@"%@ - %p - %p",p2,p2,&p2);
 NSLog(@"%@ - %p - %p",p3,p3,&p3);

得到结果为

<Person: 0x100721af0> - 0x100721af0 - 0x7ffeefbff508
<Person: 0x100721af0> - 0x100721af0 - 0x7ffeefbff500
<Person: 0x100721af0> - 0x100721af0 - 0x7ffeefbff4f8

猜测:
1、init 没有对内存进行任何操作
2、前两个打印 输出的为打印的同一片内存地址 说明 p1 p2 p3 指向着同一片内存地址空间
3、&p1 &p2 &p3 不一样 打印的是每个指针自身所在的内存地址空间

断点跟踪 源码下载 分析

我们按住command + 鼠标点击 发现只能到到达 NSObject 的声明文件 再也进不去而且没有注释 这时候我们可以进行 下符号断点
复制alloc 添加符号断点 继续跟进


截屏2020-09-09下午2.23.33.png 首先将符号断点关掉 在 Person *p1 = [Person alloc]; 这一行进行断点 再打开 符号断点alloc 继续 85AB5774-8A02-47D9-A43F-C057CA2EA759.png

根据我们得到的信息继续跟踪 我们发现 跳到了 _objc_rootAllocWithZone


7DEC262C-E96F-4EAD-BB78-4BDD73677599.png

再次打符号断点我们发现 很难读懂 但是很好 -objc4 是开源的 我们可以去这里下载 进行跟进 https://opensource.apple.com/tarballs/
配置可编译源码 https://juejin.im/post/6844904082226806792

配置好可编译源码我们一步一步的看源码

+ (id)alloc

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

_objc_rootAlloc / 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));
}


// Base class implementation of +alloc. cls is not nil.
// Calls [cls allocWithZone:nil].
id
_objc_rootAlloc(Class cls)
{
    return callAlloc(cls, false/*checkNil*/, true/*allocWithZone*/);
}

_objc_rootAllocWithZone

_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;
   ///计算 需要多少内存空间
    size = cls->instanceSize(extraBytes);
    if (outAllocatedSize) *outAllocatedSize = size;

    id obj;
    if (zone) {
        obj = (id)malloc_zone_calloc((malloc_zone_t *)zone, 1, size);
    } else {
        // alloc 开辟内存的地方
        obj = (id)calloc(1, size);
    }
    if (slowpath(!obj)) {
        if (construct_flags & OBJECT_CONSTRUCT_CALL_BADALLOC) {
            return _objc_callBadAllocHandler(cls);
        }
        return nil;
    }

    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);
}
此时我们 通过断点及阅读源码总结 如下alloc流程图 ! A9822803-A872-4AA8-8F17-F871615C5704.png

init

// Replaced by CF (throws an NSException)
+ (id)init {
    return (id)self;
}

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



_objc_rootInit

_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;
}

new

+ (id)new {
    return [callAlloc(self, false/*checkNil*/) init];
}

相关文章

网友评论

      本文标题:iOS alloc & init 探索

      本文链接:https://www.haomeiwen.com/subject/qdpeektx.html