万物皆对象,那对象是怎么创建出来的呢?带着这个问题,我们首先得了解一个类
[[LGPerson alloc] init]
在创建的过程中alloc
做了些什么?init
做了些什么?alloc
是怎样开辟内存的?
1.我们首先来看一个例子:
image.png
- 根据打印的信息,我们可以看出p1,p2,p3的指针地址是相同的,但是他们的内存地址却是不同的,为什么是这样呢?这就是接下来我们要讨论的
alloc 和 init
到底做了些什么?
准备工作:
- 下载 objc4-781 源码
- 编译源码,这期间会出现各类问题,解决的方法请参考这篇文章objc4_debug
image.png
通过配置好的源码,我们可以打断点一步一步的往下走:
- 在
[LGPerson alloc]
方法打上断点,会走到alloc
方法
- 在
+ (id)alloc {
return _objc_rootAlloc(self);
}
- 进入到
_objc_rootAlloc
方法
- 进入到
id
_objc_rootAlloc(Class cls)
{
return callAlloc(cls, false/*checkNil*/, true/*allocWithZone*/);
}
- 进入到
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));
}
- 进入到
_objc_rootAllocWithZone
方法
- 进入到
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
方法, 这里面主要实现了 3个方法-
cls->instanceSize
计算出需要开辟的内存空间大小 -
calloc
向系统申请开辟内存,返回地址指针 -
obj->initInstanceIsa
将 cls类 与 obj指针(即isa)进行关联
-
- 进入到
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);
}
- 我们可以通过断点调试进入计算内存大小的方法
cls->instanceSize
size_t instanceSize(size_t extraBytes) const {
if (fastpath(cache.hasFastInstanceSize(extraBytes))) {
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;
}
- 断点会进入
cache.fastInstanceSize
方法
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);
}
}
- 断点继续下一步,会走到
align16
方法, 这个方法的作用是 16字节对齐算法。以前的都是8字节对齐,由于内存的消耗越来越大了,8字节对齐已经不能满足用户需求了,所以现在都是16字节对齐。
static inline size_t align16(size_t x) {
return (x + size_t(15)) & ~size_t(15);
}
接下来我们分析init
方法做了什么
- 根据源码我们可以进入到
init
的类方法实现,这里是一个工厂方法,返回一个强转为id
类型的构造方法。
+ (id)init {
return (id)self;
}
- 通过 点击
init
方法,我们会计入到init
的实例方法, 会返回_objc_rootInit
方法,跳转进入到方法实现中,会返回obj
对象本身
- (id) init {
return _objc_rootInit(self);
}
id
_objc_rootInit(id obj)
{
return obj;
}
补充: 我们可以看一下 new
方法的源码是怎样实现的。
+ (id)new {
return [callAlloc(self, false/*checkNil*/) init];
}
- 通过源码我们可以看出,调用
new
方法会返回callAlloc
方法调用init
方法。实际上就是调用了[alloc init]
方法。