前言
在iOS开发过程中,和开发者打交道最多的就是对象,我们学习研究最多的也是对象。我们都知道通过alloc来创建实例对象并使用它,但是关于对象到底是怎么被创建出来的,创建流程又是怎么样的,却往往被我们忽略?现在,我们就从头开始了解一下对象的创建。
准备
在开始探究alloc之前,我们先来了解一下使用alloc和init方法,我们能够得到什么。我们先看一下下面这段代码。调用alloc和init方法,然后分别输出对象,对象的内存地址,以及指针的地址
FFAnimal *a1 = [FFAnimal alloc];
FFAnimal *a2 = [a1 init];
FFAnimal *a3 = [a1 init];
NSLog(@"%@-%p-%p",a1,a1,&a1);
NSLog(@"%@-%p-%p",a2,a2,&a2);
NSLog(@"%@-%p-%p",a3,a3,&a3);
FFAnimal *a4 = [FFAnimal alloc];
NSLog(@"%@-%p-%p",a4,a4,&a4);
执行结果如下:
<FFAnimal: 0x6000000ac0c0>-0x6000000ac0c0-0x7ffedfc74be0
<FFAnimal: 0x6000000ac0c0>-0x6000000ac0c0-0x7ffedfc74bd8
<FFAnimal: 0x6000000ac0c0>-0x6000000ac0c0-0x7ffedfc74bd0
<FFAnimal: 0x6000000a40a0>-0x6000000a40a0-0x7ffedfc74bc8
首先我们分析一下a1,a2和a3的结果。从打印结果可以很明显看出,通过alloc,我们开辟了一块内存来存储对象。但我们对这个对象init操作之后,我们得到了两个新的指针,指向了同一个内存地址。我们再看下a4,我们再次调用alloc方法之后,得到了一块新的内存。在内存中的表现,我们可以用下图表示:
总结一下
alloc具有开辟内存的能力,而init并不具备开辟内存的能力alloc申请创建的内存在堆区,而这些临时创建的指针则是存在栈区- 栈区是一片连续的内存区域,而堆区不是
- 栈区的写操作是从高地址到低地址
探索底层的方法
关于iOS底层原理,需要一些手段来辅助我们探索。这边介绍两种方法,来帮助我们玩的更轻松一些~
符号断点
我们首先在调用alloc方法的地方打上断点
执行程序,触发断点。按住
control,然后step into,这时我们可以看到调用了objc_alloc方法
接下来我们对
objc_alloc增加符号断点
继续执行,我们就能看到后面的执行流程啦
汇编(只要汇编,就会受欢迎~🤷♂️)
首先调整一下Xcode的debug配置。Debug->Debug Workflow->勾选Always Show Disassembly。
然后在调用
alloc方法的地方打上断点,执行程序。触发断点,然后会显示一大串汇编代码
其实从这里我们就能看到在
alloc和NSLog中间,还调用了objc_alloc方法。我们同样按住control+step into一步一步执行,最后可以看到的确进入了objc_alloc方法。
alloc流程源码探究
虽然说上面的方法可以让我们看到一些调用流程,但要通过这种方式来探究alloc的流程,实在是过于繁琐。对于我们来说,最简单的方式莫过于直接运行运行源码。从上面的我们已经知道,objc_alloc是libobjc库中的方法,那我们直接从苹果的开源代码网站上面下载objc的源码。
alloc代码调用流程
我们从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__ //判断当前是不是objc2.0的版本
//fastpath和slowpath是gcc引入的两个指令,作用是允许程序员将最有可能执行的分支告诉编译器,从而进行编译器优化,slowpath表示小概率情况,fastpath表示大概率情况。这两个指令并不会影响代码逻辑
if (slowpath(checkNil && !cls)) return nil;//是否需要判空&&cls为空,则返回nil
if (fastpath(!cls->ISA()->hasCustomAWZ())) {//cls有没有实现自定义allocWithZone方法,如果没有实现,调用_objc_rootAllocWithZone
return _objc_rootAllocWithZone(cls, nil);
}
#endif
// No shortcuts available.
if (allocWithZone) {//是否需要allocWithZone,是的话调用allocWithZone,否则调用alloc方法
return ((id(*)(id, SEL, struct _NSZone *))objc_msgSend)(cls, @selector(allocWithZone:), nil);
}
return ((id(*)(id, SEL))objc_msgSend)(cls, @selector(alloc));
}
NEVER_INLINE
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());
// 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);
}
所以我们就得出了下面的alloc调用流程图
当我们执行到_class_createInstanceFromZone这个方法的时候,我们终于进入到了alloc流程的核心代码。我们来分析一下这个方法到底做了什么,这里面有3个关键的方法调用
它们各自实现的功能是
- cls->instanceSize : 计算内存大小
- (id)calloc(1, size) : 开辟内存,返回地址指针
- obj->initInstanceIsa :初始化指针,和类关联起来
下面我们来重点分析一下这三个方法:
instanceSize
inline size_t instanceSize(size_t extraBytes) const {
if (fastpath(cache.hasFastInstanceSize(extraBytes))) {
//从cache中快速得到内存大小
return cache.fastInstanceSize(extraBytes);
}
size_t size = alignedInstanceSize() + extraBytes;
// CF requires all objects be at least 16 bytes.
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;
// remove the FAST_CACHE_ALLOC_DELTA16 that was added
// by setFastInstanceSize
return align16(size + extra - FAST_CACHE_ALLOC_DELTA16);
}
}
static inline size_t align16(size_t x) {
//内存16字节对齐
return (x + size_t(15)) & ~size_t(15);
}
关于内存对齐的算法,这边我们举个例子来说明一下,以align16(10)为例
- 10 + 15 = 25 0000 1010 + 0000 1111 = 0001 1001
- 15 0000 1111
- ~15 1111 0000
- 25 & ~15 0001 1001 & 1111 0000
- 结果:0001 0000 16
概括一下,算法的思想简单来说就是16字节向下取整。
calloc
在上面我们通过instanceSize得到对象内存大小之后,我们接下来就是通过calloc方法去开辟这么大的一个内存块。在执行calloc方法前后,我们打印一下obj,可以发现obj的内存地址发生了变化。这就说明,我们现在已经开辟成功了。
initInstanceIsa
现在我们已经开辟到了内存块,但是我们注意一下上面的打印内容,obj是一个id类型。我们继续执行,在执行完obj->initInstanceIsa之后,我们再打印一下obj,看看有什么变化
这个时候,obj已经是一个FFAnimal类型的对象了。我们来看一下initInstanceIsa方法的实现
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, bool nonpointer, UNUSED_WITHOUT_INDEXED_ISA_AND_DTOR_BIT bool hasCxxDtor)
{
ASSERT(!isTaggedPointer());
isa_t newisa(0);
if (!nonpointer) {
newisa.setClass(cls, this);
} else {
ASSERT(!DisableNonpointerIsa);
ASSERT(!cls->instancesRequireRawIsa());
#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
# if ISA_HAS_CXX_DTOR_BIT
newisa.has_cxx_dtor = hasCxxDtor;
# endif
newisa.setClass(cls, this);
#endif
newisa.extra_rc = 1;
}
// 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;
}
代码非常清晰,initIsa方法创建了一个isa,将cls设置到了新的isa中,然后将这个isa赋给了obj。看到这里,我们就知道initInstanceIsa方法的作用就是把这个对象和类关联起来。这就是我们通过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并没有对obj做什么修改,而是直接返回出去了。因此
init方法返回的是对象本身init的设计是工厂模式的一种体现,它给开发者提供了初始化对象的入口,方便做定制化
new探究
+ (id)new {
return [callAlloc(self, false/*checkNil*/) init];
}
源码中可以看到,new实际上调用了callAlloc进入alloc的流程,然后调用了init方法。因此,new可以看作是alloc+init。
总结
alloc的作用是开辟内存,并且创建isa来与类进行关联。init直接返回对象本身,它给了开发者更加灵活的定制初始化对象的入口。new实际上就是alloc+init,但它不支持自定义初始化。
参考资料
-
libmalloc源码:opensource.apple.com/tarballs/li…