iOS底层探索1-alloc

243 阅读3分钟

iOS底层探索-alloc

macOS Catalina 10.15.7 (19H2) objc源码

问题引入

alloc 指针地址打印

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

LRPerson *temP = [LRPerson alloc];
NSLog(@"%@-%p-%p",temP,temP,&temP);
<LRPerson: 0x6000037a84e0>-0x6000037a84e0-0x7ffee2de8088
<LRPerson: 0x6000037a84e0>-0x6000037a84e0-0x7ffee2de8080
<LRPerson: 0x6000037a84e0>-0x6000037a84e0-0x7ffee2de8078

<LRPerson: 0x6000037bc1c0>-0x6000037bc1c0-0x7ffee2de8070

p1 p2 p3 打印的对象内容、对象指针相同,但对象指针的地址不同 (但是是顺序开辟的 88-80-78在栈中从高地址依次向低地址开辟);

表明p1 p2 p3 指向同一块内存,由p2 p3 可知 init方法没有开辟新的内存 temP 对象内容、对象指针、对象指针的地址都不相同(因为都是在栈中(0x7开头) 地址也是顺序向低地址开辟88-80-78-70) 表明temP是重新开辟了一块新内存。

综上,可 暂时 推理出 alloc具备开辟内存的能力,init则不具备。 (涉及内存知识:一般情况 0x6堆区 0x7栈区)

分析探索的方法

1: 符号断点 libobjc.A.dylib`objc_alloc:

图片.png 设置断点,运行xcode 停在断点处时,按住 control 键会变成如图效果,再点击step into进入 图片.png

2: 汇编 跟流程 - 符号断点: objc_alloc

图片.png 设置汇编,定位到objc_alloc 方法 按住control 键 step into 进入 图片.png

3: 符号断点 确定未知 : libobjc.A.dylib`+[NSObject alloc]:

图片.png 图片.png

图片.png 通过设置符号断点方式结合objc源码 进行分析

源码分析alloc的流程

alloc流程图

图片.png

具体调用方法分析

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;

    size = cls->instanceSize(extraBytes);
    if (outAllocatedSize) *outAllocatedSize = size;

    id obj;
    if (zone) {
        obj = (id)malloc_zone_calloc((malloc_zone_t *)zone, 1, size);
    } else {
        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);
}

三个重要方法实现内存开辟过程

  • cls->instanceSize(extraBytes); 计算内存大小
  • (id)calloc(1, size); 开辟内存并返回地址
  • obj->initInstanceIsa(cls, hasCxxDtor);创建isa指针用于关联类

具体实现分析

  1. instanceSize
  2. calloc
  3. initInstanceIsa

时间换空间(后期补上)

内存对齐

举例

总结

objc_alloc->callAlloc,在callAlloc的执行过程有缓存无缓存两种情况, 有缓存时通过 _objc_rootAllocWithZone -> _class_createInstanceFromZone -> instanceSize -> calloc -> initInstanceIsa 无缓存时,callAlloc内部通过objc_msgSend 调用alloc方法, 在alloc方法内部执行 _objc_rootAlloc -> callAlloc