alloc流程分析一

2021-06-07  本文已影响0人  一毛钱

1. 初始化创建三个变量,分别添加打印信息。代码如下:

NSObject* obj1 = [NSObject alloc];
NSObject* obj2 = [obj1 init];
NSObject* obj3 = [obj1 init];
NSLog(@"%@--%p---%p",obj1,obj1,&obj1);
NSLog(@"%@--%p---%p",obj2,obj2,&obj2);
NSLog(@"%@--%p---%p",obj3,obj3,&obj3);

2. 查找源码库方法:

libobjc.A.dylib`+[NSObject alloc]:
 ->  0x10a7e05ed <+0>: jmp    0x10a7e0611 ; _objc_rootAlloc

3. init

4. alloc底层代码流程:

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

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
    if (allocWithZone) {
        return ((id(*)(id, SEL, struct _NSZone *))objc_msgSend)(cls, @selector(allocWithZone:), nil);
    }
    return ((id(*)(id, SEL))objc_msgSend)(cls, @selector(alloc));
}

+ (id)allocWithZone:(struct _NSZone *)zone {
    return _objc_rootAllocWithZone(self, (malloc_zone_t *)zone);
}

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

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());
    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;

   //2 向系统申请开辟内存,返回地址指针。
    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;
    }

    //3. 关联到相应的类。
    if (!zone && fast) {
        obj->initInstanceIsa(cls, hasCxxDtor);
    } else {
        obj->initIsa(cls);
    }

    if (fastpath(!hasCxxCtor)) {
        return obj;
    }
    construct_flags |= OBJECT_CONSTRUCT_FREE_ONFAILURE;
    return object_cxxConstructFromClass(obj, cls, construct_flags);
}

5.instanceSize计算出变量所需内存大小具体流程如下:

size_t instanceSize(size_t extraBytes) const {
        if (fastpath(cache.hasFastInstanceSize(extraBytes))) {
            return cache.fastInstanceSize(extraBytes);
        }
        size_t size = alignedInstanceSize() + extraBytes;//extraBytes 默认传递0
        if (size < 16) size = 16;
        return size;
}
//流程一  
size_t fastInstanceSize(size_t extra) const {
        ASSERT(hasFastInstanceSize(extra));

        if (__builtin_constant_p(extra) && extra == 0) {
            return _flags & FAST_CACHE_ALLOC_MASK16;
        } else {
            size_t size = _flags & FAST_CACHE_ALLOC_MASK;
            return align16(size + extra - FAST_CACHE_ALLOC_DELTA16);
        }
}
    
//计算字节
static inline size_t align16(size_t x) {
    return (x + size_t(15)) & ~size_t(15);
}

//流程二
uint32_t alignedInstanceSize() const {
        return word_align(unalignedInstanceSize());
}

uint32_t unalignedInstanceSize() const {
        ASSERT(isRealized());
        return data()->ro()->instanceSize;//获取class_ro_t 结构体中instanceSize变量的值
}

#define WORD_MASK 7UL
static inline uint32_t word_align(uint32_t x) {
    return (x + WORD_MASK) & ~WORD_MASK;
}
上一篇 下一篇

猜你喜欢

热点阅读