01、iOS底层探索-Alloc原理上

探索前的准备

1、了解使用

查看底层的方法

符号断点:Symbolic BreakPoint
汇编跟流程(开启Always Show Disassembly

2、源码分析

苹果开源源码资源

源码分析流程
+ (id)alloc {
    return _objc_rootAlloc(self);
}
_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;    //编译优化可根据设置将slowpath或fastpath的语句优化掉
    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));
}
_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;
    
    /*****现在苹果废弃了zone,不用过多考虑,早期的内存空间是通过zone申请的*****/
    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);
}
/*****instanceSize实现*****/
inline size_t instanceSize(size_t extraBytes) const {
        if (fastpath(cache.hasFastInstanceSize(extraBytes))) {
            return cache.fastInstanceSize(extraBytes);  //进入fastInstanceSize()查看
        }
        size_t size = alignedInstanceSize() + extraBytes;
        // CF requires all objects be at least 16 bytes.
        if (size < 16) size = 16;  
        return size;
    }
/*****hasFastInstanceSize实现*****/
bool hasFastInstanceSize(size_t extra) const
    {
        if (__builtin_constant_p(extra) && extra == 0) {
            return _flags & FAST_CACHE_ALLOC_MASK16;
        }
        return _flags & FAST_CACHE_ALLOC_MASK;
    }
    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;
            // remove the FAST_CACHE_ALLOC_DELTA16 that was added
            // by setFastInstanceSize
            return align16(size + extra - FAST_CACHE_ALLOC_DELTA16);    //16字节对齐
        }
    }
/*****16字节对齐*****/
static inline size_t align16(size_t x) {
    return (x + size_t(15)) & ~size_t(15);
    
    /*****
    wei
    x = 8
    (8 + 15) & ~15
    23 : 0000 0000 0001 0111   
    ~15: 1111 1111 1111 0000 非15先算15:0000 0000 0000 1111
    取与: 0000 0000 0001 0000 --> 16
    *****/
}
编译优化

Alloc核心方法
Alloc、init、new的区别
补充

总结