一. 先看一段代码:
YJPerson *p1 = [YJPerson alloc];
YJPerson *p2 = [p1 init];
YJPerson *p3 = [p1 init];
NSLog(@"%@ - %p - %p", p1, p1, &p1);
NSLog(@"%@ - %p - %p", p2, p2, &p2);
NSLog(@"%@ - %p - %p", p3, p3, &p3);
输出结果:
<YJPerson: 0x600001d28140> - 0x600001d28140 - 0x30df21ec8
<YJPerson: 0x600001d28140> - 0x600001d28140 - 0x30df21ec0
<YJPerson: 0x600001d28140> - 0x600001d28140 - 0x30df21eb8
由输出结果可以看出 p1、p2、p3 由不同的指针指向同一块内存地址
那么 alloc init 到底都做了些什么呢?我们通过查看源码来探索。。。
二. 源码探索:
2.1.0 - 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__
if (slowpath(checkNil && !cls)) return nil;
// 没有实现 allocWithZone 时,执行
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));
}
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 的源码执行过程: alloc -> _objc_rootAlloc -> callAlloc -> hasCustomAWZ()没有实现+allocWithZone -> _objc_rootAllocWithZone -> _class_createInstanceFromZone 里面 申请内存calloc 将 isa 与类关联 initInstanceIsa 返回 objc
2.1.1 - YJPerson 类中实现 +allocWithZone
#import "YJPerson.h"
@implementation YJPerson
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
static id instance = nil;
@synchronized (self) {
if (instance == nil) {
instance = [super allocWithZone:zone];
}
}
return instance;
}
@end
前面的执行流程是一样的,hasCustomAWZ()会跳过去
static ALWAYS_INLINE id
callAlloc(Class cls, bool checkNil, bool allocWithZone=false) {
#if __OBJC2__
if (slowpath(checkNil && !cls)) return nil;
// 没有实现 allocWithZone 时,执行
if (fastpath(!cls->ISA()->hasCustomAWZ())) {
return _objc_rootAllocWithZone(cls, nil);
}
#endif
// No shortcuts available. 没有使用快速创造方式
// 实现了 allocWithZone,会走这里
if (allocWithZone) {
return ((id(*)(id, SEL, struct _NSZone *))objc_msgSend)(cls, @selector(allocWithZone:), nil);
}
return ((id(*)(id, SEL))objc_msgSend)(cls, @selector(alloc));
}
流程图:
2.2 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,那个这个有什么用呢,这是一种工厂设计模式,可以方便我们在初始化的时候可以做一些其他的操作。
3.2 new 源码实现
+ (id)new {
return [callAlloc(self, false/*checkNil*/) init];
}
这句代码可以看见new调用了callAlloc参数默认false``allocWithZone=false,并且默认调用了init的方法。
总结
alloc开辟了内存空间,并且将类关联了isa指针
init什么都没有做,返回了原理的类
new相当于固化了实例化方式调用了init, [Class new]等价于[[Class alloc]init]