前言:以下内容基于objc4-781的源码进行分析
在iOS中创建对象使用alloc/init 方式进行初始化,那么系统底层到底干了什么,接下来我们就一探究竟。 首先我们添加一个Person类,接下来我们看一个现象
Person *p1 = [Person alloc];
Person *p2 = [p1 init];
Person *p3 = [p1 init];
NSLog(@"%@ - %p - %p",p1,p1,&p1);
NSLog(@"%@ - %p - %p",p2,p2,&p2);
NSLog(@"%@ - %p - %p",p3,p3,&p3);
分别打印内容、内存地址和指针地址结果如下
<Person: 0x60000177cfa0> - 0x60000177cfa0 - 0x7ffeefbf9128
<Person: 0x60000177cfa0> - 0x60000177cfa0 - 0x7ffeefbf9120
<Person: 0x60000177cfa0> - 0x60000177cfa0 - 0x7ffeefbf9118
我们发现p1、p2、p3内容是相同的,指针指向的内存地址也是相同的,但指针地址是不用的,为什么会这样。我们接下来探究一下。
首先我们在main 函数中添加代码
Person *p1 = [Person alloc];
在NSObject.mm类alloc方法中添加断点,得到的堆栈如下
我们在看看具体的方法里都做了什么,首先我们看到调用的是objc_alloc
id objc_alloc(Class cls)
{
return callAlloc(cls, true/*checkNil*/, false/*allocWithZone*/);
}
objc_alloc中调用了callAlloc
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
// 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));
}
callAlloc中主要通过一系列判断之后通过objc_msgSend调用了alloc方法
+ (id)alloc {
return _objc_rootAlloc(self);
}
alloc 的类方法调用了_objc_rootAlloc
id _objc_rootAlloc(Class cls)
{
return callAlloc(cls, false/*checkNil*/, true/*allocWithZone*/);
}
_objc_rootAlloc有执行了callAlloc,这个时候走到了_objc_rootAllocWithZone
_objc_rootAllocWithZone中调用了_class_createInstanceFromZone
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);
}
_class_createInstanceFromZone代码如下:
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 {
// alloc 开辟内存的地方
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);
}
主要做了3件事:
- 计算需要开辟的空间:instanceSize
- 开辟内存:calloc
- 类地址与isa关联:initInstanceIsa
流程图如下:
到这里我们大概知道了alloc 方法底层走了哪些方法,做了哪些事情。现在有个问题:为什么一开始我们调用的alloc 的方法,底层却调用的是objc_alloc?
我们通过汇编在llvm编译器里里面找到相关代码
EmitObjCAlloc实现如下
/// Allocate the given objc object.
/// call i8* \@objc_alloc(i8* %value)
llvm::Value *CodeGenFunction::EmitObjCAlloc(llvm::Value *value,
llvm::Type *resultType) {
return emitObjCValueOperation(*this, value, resultType,
CGM.getObjCEntrypoints().objc_alloc,
"objc_alloc");
}
由此得出编译器阶段系统给我做了处理