Skip to content
全部文章
TechiOSLearn

iOS底层探索 - 实例对象的创建

前言 之前探索完对象的销毁,这次就来看看对象的创建。常见创建一个实例的方法就是调用[[XXX alloc] init]来实现,既然是两个方法调用,那我们就一个一个来看。

前言

之前探索完对象的销毁,这次就来看看对象的创建。常见创建一个实例的方法就是调用[[XXX alloc] init]来实现,既然是两个方法调用,那我们就一个一个来看。

落叶生根 - alloc

我们先来看下runtime的源码中alloc都做了什么,

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

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

我们可以看到alloc中只有一句调用_objc_rootAlloc,这个套路和dealloc是一样一样的。再看_objc_rootAlloc的实现,在当前版本中调用了callAlloc方法。继续跟踪,

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

这里就相对复杂一点了,首先进行判断cls->ISA()->hasCustomAWZ(),如果有自定义的alloc/allocWithZone实现则直接调用[cls allocWithZone:nil]实现。如果没有,则继续判断cls->canAllocFast()canAllocFast()FAST_ALLOC这个值有关,

#if !__LP64__

//some #define

#elif 1

//some #define

#else

//some #define

#define FAST_ALLOC              (1UL<<50)

//some #define

从源码中可以看到,由于#elif 1的原因,FAST_ALLOC的定义是没有走到的,也就是说这里FAST_ALLOC没有定义。

#if FAST_ALLOC

    //some code
    
#else
    //some code
    
    bool canAllocFast() {
        return false;
    }
#endif

因此canAllocFast()返回的是false,也就是说callAlloc的时候会走到else的逻辑,调用class_createInstance来创建对象。那就来看一下class_createInstance的实现:

class_createInstance(Class cls, size_t extraBytes)
{
    return _class_createInstanceFromZone(cls, extraBytes, nil);
}

static __attribute__((always_inline)) 
id
_class_createInstanceFromZone(Class cls, size_t extraBytes, void *zone, 
                              bool cxxConstruct = true, 
                              size_t *outAllocatedSize = nil)
{
    if (!cls) return nil;

    assert(cls->isRealized());

    // Read class's info bits all at once for performance
    bool hasCxxCtor = cls->hasCxxCtor();
    bool hasCxxDtor = cls->hasCxxDtor();
    bool fast = cls->canAllocNonpointer();

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

    id obj;
    if (!zone  &&  fast) {
        obj = (id)calloc(1, size);
        if (!obj) return nil;
        obj->initInstanceIsa(cls, hasCxxDtor);
    } 
    else {
        if (zone) {
            obj = (id)malloc_zone_calloc ((malloc_zone_t *)zone, 1, size);
        } else {
            obj = (id)calloc(1, size);
        }
        if (!obj) return nil;

        // Use raw pointer isa on the assumption that they might be 
        // doing something weird with the zone or RR.
        obj->initIsa(cls);
    }

    if (cxxConstruct && hasCxxCtor) {
        obj = _objc_constructOrFree(obj, cls);
    }

    return obj;
}

可以看到class_createInstance里面直接调用了_class_createInstanceFromZone,在_class_createInstanceFromZone里面首先做了一些保护性判断,然后做了cls->canAllocNonpointer()的判断。

canAllocNonpointer和之前就见过的nonpointer有关,也就是是否开启了指针优化功能,具体的可以参考这篇文章深入理解Tagged Pointer,目前一般都为truezone从之前的调用可以知道为nil,所以大部分情况下回走到if的逻辑,调用calloc分配内存,再调用initInstanceIsa创建isa。如果走到了else的逻辑,那么其实也是调用calloc分配内存,但不同的是调用了initIsa来创建isa

inline void  
objc_object::initInstanceIsa(Class cls, bool hasCxxDtor)
{
    assert(!cls->instancesRequireRawIsa());
    assert(hasCxxDtor == cls->hasCxxDtor());

    initIsa(cls, true, hasCxxDtor);
}

inline void 
objc_object::initIsa(Class cls)
{
    initIsa(cls, false, false);
}

但是看了下initInstanceIsa的实现,其实内部也是调用了initIsa,所以两个路径最后都调用到了同一个方法中,只是传入的参数不同而已。最后我们来看下initIsa

inline void 
objc_object::initIsa(Class cls, bool nonpointer, bool hasCxxDtor) 
{ 
    assert(!isTaggedPointer()); 
    
    if (!nonpointer) {
        isa.cls = cls;
    } else {
        assert(!DisableNonpointerIsa);
        assert(!cls->instancesRequireRawIsa());

        isa_t newisa(0);

#if SUPPORT_INDEXED_ISA
        assert(cls->classArrayIndex() > 0);
        newisa.bits = ISA_INDEX_MAGIC_VALUE;
        // isa.magic is part of ISA_MAGIC_VALUE
        // isa.nonpointer is part of ISA_MAGIC_VALUE
        newisa.has_cxx_dtor = hasCxxDtor;
        newisa.indexcls = (uintptr_t)cls->classArrayIndex();
#else
        newisa.bits = ISA_MAGIC_VALUE;
        // isa.magic is part of ISA_MAGIC_VALUE
        // isa.nonpointer is part of ISA_MAGIC_VALUE
        newisa.has_cxx_dtor = hasCxxDtor;
        newisa.shiftcls = (uintptr_t)cls >> 3;
#endif

        // This write must be performed in a single store in some cases
        // (for example when realizing a class because other threads
        // may simultaneously try to use the class).
        // fixme use atomics here to guarantee single-store and to
        // guarantee memory order w.r.t. the class index table
        // ...but not too atomic because we don't want to hurt instantiation
        isa = newisa;
    }
}

如果不是nonpointer那就比较简单,直接赋值一下isa.cls就可以了。如果是nonpointer那就要新建一个isa指针,然后根据SUPPORT_INDEXED_ISA来进行不同的初始化过程。下面看下SUPPORT_INDEXED_ISA的定义:

// Define SUPPORT_INDEXED_ISA=1 on platforms that store the class in the isa 
// field as an index into a class table.
// Note, keep this in sync with any .s files which also define it.
// Be sure to edit objc-abi.h as well.
#if __ARM_ARCH_7K__ >= 2
#   define SUPPORT_INDEXED_ISA 1
#else
#   define SUPPORT_INDEXED_ISA 0
#endif

__ARM_ARCH_7K__这个又是何方神圣呢,在runtime的源码中没有找到定义,还好我们还有clang,在clang的源码中可以看到:

// Unfortunately, __ARM_ARCH_7K__ is now more of an ABI descriptor. The CPU
// happens to be Cortex-A7 though, so it should still get __ARM_ARCH_7A__.
if (getTriple().isWatchABI())
    Builder.defineMacro("__ARM_ARCH_7K__", "2");

看来__ARM_ARCH_7K__ >= 2对应的应该就iWatch了,那么是iWatch的时候SUPPORT_INDEXED_ISA值为1,之后就是isa的赋值操作,这里就不展开讲了,感兴趣的可以看下这篇文章从 NSObject 的初始化了解 isa

最后我们可以用一张图总结一下alloc的流程:

原文图片托管服务已失效,图片暂未迁移。

开枝散叶 - init

接下来看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的实现非常简单,只是把obj原封不动的返回了回去。这是因为我们自己实现的类一般都会有自定义的init实现,所以NSObjectinit就是一个空壳,里面的内容需要用户自己去实现。

似曾相识 - new

在很多代码中会看到对象的创建不是使用[[XXX alloc] init]的方法,而是使用[XXX new]来返回一个新的对象。那么newalloc、init又有什么区别呢?我们这就来看一下:

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

我们可以看到new也是调用了callAlloc创建对象,然后又调用了init返回对象。那么它和[[XXX alloc] init]唯一的区别就是参数allocWithZone使用的是默认false,而alloctrue。也就是说在没有自定义alloc/allocWithZone的情况下,[[XXX alloc] init][XXX new]是等价的。

那么在有自定义alloc/allocWithZone的情况下,区别就在于[cls allocWithZone:nil][cls alloc]的实现,alloc我们之前已经分析过了,所以这里看一下allocWithZone的实现,

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

id
_objc_rootAllocWithZone(Class cls, malloc_zone_t *zone)
{
    id obj;

#if __OBJC2__
    // allocWithZone under __OBJC2__ ignores the zone parameter
    (void)zone;
    obj = class_createInstance(cls, 0);
#else
    if (!zone) {
        obj = class_createInstance(cls, 0);
    }
    else {
        obj = class_createInstanceFromZone(cls, 0, zone);
    }
#endif

    if (slowpath(!obj)) obj = callBadAllocHandler(cls);
    return obj;
}

我们可以看到allocWithZone调用了_objc_rootAllocWithZone_objc_rootAllocWithZone里面又调用了class_createInstance(cls, 0),而从上面的分析可以知道alloc最终也是调用到了class_createInstance(cls, 0),也就是说[cls allocWithZone:nil][cls alloc]最终的调用是一样的。allocWithZone的方法其实已经被废弃了,保存实现也是由于历史原因,详见官方文档

综上所述,[[XXX alloc] init][XXX new]的调用是等价的,不过之所以实际中使用new比较少的原因是:

  1. 一般对象的生成都不是使用默认的init方法来生成的
  2. 后期扩展性,如果后期需要改写成不用默认init的方法,修改起来比较麻烦
  3. 万一苹果爸爸哪天改了new的实现逻辑那就尴尬了

所以,还是用[[XXX alloc] init]比较稳妥一些。

总结

最后总结一下本次探索的要点:

  • alloc的实质是通过calloc分配了一块内存并初始化对象的isa指针
  • NSObjectinit方法就是一个空壳,没有任何实现
  • [[XXX alloc] init][XXX new]的调用是等价的
  • isa的实现演变可以深入研究一下

参考