美文网首页
OC实例对象底层

OC实例对象底层

作者: king_jensen | 来源:发表于2019-02-21 22:05 被阅读1次

    前言

    当我们不知道一个方法的源码实现在哪个库的时,我们可以在这个方法调用处设置断点,在Xcode Debug->Always Show Disassembly直接查看汇编代码
    [图片上传中...(1.png-92c650-1550751638553-0)]
    由此找到alloc方法的实现在objc源码。
    打开apple open source, 下载objc源码最新版本 objc4-750.tar.gz,下载后编译会报错,需要源码配置,配置完成后,创建一个mac命令行,就可以通过LLDB源码调试。

    alloc&init源码探索

    一、alloc的源码查看

    1、alloc的执行流程

    LGPerson *objc = [LGPerson 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 (slowpath(checkNil && !cls)) return nil;
    
    #if __OBJC2__
        if (fastpath(!cls->ISA()->hasCustomAWZ())) {
            // No alloc/allocWithZone implementation. Go straight to the allocator.
            // fixme store hasCustomAWZ in the non-meta class and 
            // add it to canAllocFast's summary
            if (fastpath(cls->canAllocFast())) {
                // No ctors, raw isa, etc. Go straight to the metal.
                bool dtor = cls->hasCxxDtor();
                id obj = (id)calloc(1, cls->bits.fastInstanceSize());
                if (slowpath(!obj)) return callBadAllocHandler(cls);
                obj->initInstanceIsa(cls, dtor);
                return obj;
            }
            else {
                // Has ctor or raw isa or something. Use the slower path.
                id obj = class_createInstance(cls, 0);
                if (slowpath(!obj)) return callBadAllocHandler(cls);
                return obj;
            }
        }
    #endif
    
        // No shortcuts available.
        if (allocWithZone) return [cls allocWithZone:nil];
        return [cls alloc];
    }
    
    8F91D41F746B3CBBB491162A1869595E.jpg

    2.alloc核心代码分析
    要创建对象,那就必须要分配内存,分配内存就必须要知道内存大小

     size_t size = cls->instanceSize(extraBytes);
    

    跟进cls->instanceSize

     size_t instanceSize(size_t extraBytes) {
            size_t size = alignedInstanceSize() + extraBytes;
            // CF requires all objects be at least 16 bytes.
            if (size < 16) size = 16;
            return size;
        }
    

    对象大小分配,至少为16
    跟进 alignedInstanceSize()

     uint32_t alignedInstanceSize() {
            return word_align(unalignedInstanceSize());
        }
    

    define WORD_MASK 7UL

    static inline uint32_t word_align(uint32_t x) {
    return (x + WORD_MASK) & ~WORD_MASK;
    }
    对象内存8字节对齐

    调用calloc分配内存

     obj = (id)calloc(1, size);
    

    calloc方法的实现在libmalloc源码中,因此我们需要下载libmalloc,并配置。
    通过LLDB调试,我们发现calloc函数中会调用malloc_zone_calloc(default_zone, num_items, size);malloc_zone_calloc函数又调用ptr = zone->calloc(zone, num_items, size);指针函数。


    FE430E9D9D21B3869174FA1155BECBC4.png

    我们找到函数指针就是malloc中的default_zone_calloc函数.
    跟进default_zone_calloc
    static void *
    default_zone_calloc(malloc_zone_t *zone, size_t num_items, size_t size)
    {
    zone = runtime_default_zone();

    return zone->calloc(zone, num_items, size);
    

    }
    default_zone_calloc函数调用zone->calloc指针函数


    13A9733F86BAA04E907425F70D26779D.png

    通过LLDB调试,我们找到了函数指针是nano_calloc函数
    跟进nano_calloc函数,在nano_calloc继续跟进_nano_malloc_check_clear

    static void *
    _nano_malloc_check_clear(nanozone_t *nanozone, size_t size, boolean_t cleared_requested)
    {
        MALLOC_TRACE(TRACE_nano_malloc, (uintptr_t)nanozone, size, cleared_requested, 0);
    
        void *ptr;
        size_t slot_key;
        size_t slot_bytes = segregated_size_to_fit(nanozone, size, &slot_key); // Note slot_key is set here
        mag_index_t mag_index = nano_mag_index(nanozone);
    
        nano_meta_admin_t pMeta = &(nanozone->meta_data[mag_index][slot_key]);
    
        ptr = OSAtomicDequeue(&(pMeta->slot_LIFO), offsetof(struct chained_block_s, next));
        if (ptr) {
            unsigned debug_flags = nanozone->debug_flags;
    #if NANO_FREE_DEQUEUE_DILIGENCE
            size_t gotSize;
            nano_blk_addr_t p; // the compiler holds this in a register
    
            p.addr = (uint64_t)ptr; // Begin the dissection of ptr
            if (NANOZONE_SIGNATURE != p.fields.nano_signature) {
                malloc_zone_error(debug_flags, true,
                        "Invalid signature for pointer %p dequeued from free list\n",
                        ptr);
            }
    
            if (mag_index != p.fields.nano_mag_index) {
                malloc_zone_error(debug_flags, true,
                        "Mismatched magazine for pointer %p dequeued from free list\n",
                        ptr);
            }
    
            gotSize = _nano_vet_and_size_of_free(nanozone, ptr);
            if (0 == gotSize) {
                malloc_zone_error(debug_flags, true,
                        "Invalid pointer %p dequeued from free list\n", ptr);
            }
            if (gotSize != slot_bytes) {
                malloc_zone_error(debug_flags, true,
                        "Mismatched size for pointer %p dequeued from free list\n",
                        ptr);
            }
    
            if (!_nano_block_has_canary_value(nanozone, ptr)) {
                malloc_zone_error(debug_flags, true,
                        "Heap corruption detected, free list canary is damaged for %p\n"
                        "*** Incorrect guard value: %lu\n", ptr,
                        ((chained_block_t)ptr)->double_free_guard);
            }
    
    #if defined(DEBUG)
            void *next = (void *)(((chained_block_t)ptr)->next);
            if (next) {
                p.addr = (uint64_t)next; // Begin the dissection of next
                if (NANOZONE_SIGNATURE != p.fields.nano_signature) {
                    malloc_zone_error(debug_flags, true,
                            "Invalid next signature for pointer %p dequeued from free "
                            "list, next = %p\n", ptr, "next");
                }
    
                if (mag_index != p.fields.nano_mag_index) {
                    malloc_zone_error(debug_flags, true,
                            "Mismatched next magazine for pointer %p dequeued from "
                            "free list, next = %p\n", ptr, next);
                }
    
                gotSize = _nano_vet_and_size_of_free(nanozone, next);
                if (0 == gotSize) {
                    malloc_zone_error(debug_flags, true,
                            "Invalid next for pointer %p dequeued from free list, "
                            "next = %p\n", ptr, next);
                }
                if (gotSize != slot_bytes) {
                    malloc_zone_error(debug_flags, true,
                            "Mismatched next size for pointer %p dequeued from free "
                            "list, next = %p\n", ptr, next);
                }
            }
    #endif /* DEBUG */
    #endif /* NANO_FREE_DEQUEUE_DILIGENCE */
    
            ((chained_block_t)ptr)->double_free_guard = 0;
            ((chained_block_t)ptr)->next = NULL; // clear out next pointer to protect free list
        } else {
            ptr = segregated_next_block(nanozone, pMeta, slot_bytes, mag_index);
        }
    
        if (cleared_requested && ptr) {
            memset(ptr, 0, slot_bytes); // TODO: Needs a memory barrier after memset to ensure zeroes land first?
        }
        return ptr;
    }
    

    _nano_malloc_check_clear函数中,我们发现了

    size_t slot_bytes = segregated_size_to_fit(nanozone, size, &slot_key);
    

    跟进segregated_size_to_fit函数

    static MALLOC_INLINE size_t
    segregated_size_to_fit(nanozone_t *nanozone, size_t size, size_t *pKey)
    {
        size_t k, slot_bytes;
    
        if (0 == size) { // 16倍数对齐
            size = NANO_REGIME_QUANTA_SIZE; // Historical behavior
        }
        k = (size + NANO_REGIME_QUANTA_SIZE - 1) >> SHIFT_NANO_QUANTUM; // round up and shift for number of quanta
        slot_bytes = k << SHIFT_NANO_QUANTUM;                           // multiply by power of two quanta size
        *pKey = k - 1;                                                  // Zero-based!
    
        return slot_bytes;
    }
    
    

    我们发现了系统分配内存是16进制对齐的,算法是k = (size + NANO_REGIME_QUANTA_SIZE - 1) >> SHIFT_NANO_QUANTUM; //

    初始化isa

    obj->initInstanceIsa(cls, hasCxxDtor);
    

    总结:对象内存内配大小与isa、 属性、对象8字节对齐、系统分配内存16字节对齐有关。
    alloc的作用是通过类创建实例对象。

    二、init的源码查看
     -(id)init {
        return _objc_rootInit(self);
    }
    
    
    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;
    }
    

    通过init的源码查看,我们发现NSObject的init方法只返回了自己。使用了设计模式,把实现交给子类。

    相关文章

      网友评论

          本文标题:OC实例对象底层

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