iOS 开发好几年,
alloc
写了没一万次也得八九千。alloc
底层苹果是怎么实现的?在底层发挥了什么样的作用? 带着疑惑去试探试探实践中,看看能否找到答案,搞起~
创建个UI仔
为了试探,新的项目UI仔
运行一下,来一波日常打印,看看效果。
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(@"i'm coming");
UIPerson *nickname = [UIPerson alloc];
UIPerson *firstname = [nickname init];
UIPerson *lastname = [nickname init];
NSLog(@"nickname-%@-%p",nickname,nickname);
NSLog(@"firstname-%@-%p",firstname,firstname);
NSLog(@"lastname-%@-%p",lastname,lastname);
}
@end
@implementation UIPersion
@end
复制代码
结果:三个指针指向的
对象地址
齐刷刷的一样,init
看起来好像是什么都没做?确实不得不进行一番底层的试探了。
试探Alloc底层
试探思路:
- 思路1: 断点
Step Over instruction
进行‘断点符号’查找。- 思路2:
Debug
->always show disassembly
汇编跟踪代码流程。- 思路3: 直接对“
alloc
”进行符号断点。
思路1: Step-into-instruction
就像平时断点调试一样,将断点打到UI仔
创建的时候。当断点停住的时候,按住control将转变成 Step into instruction,进入
alloc
执行流程看看alloc
下一步会执行到什么方法。
昂~ objc_alloc
,继续看看 objc_alloc
下一步会执行什么.
结果什么都没了,看不到更多有用信息了。那到现在已经探到的信息就是objc_alloc
,那就用objc_alloc
加个符号断点看看有什么效果。
加好断点,Continue progress execution 走你~
最终得到了libobjc.A.dylib'objc_alloc
的信息。
思路2: Disassembly
依然断点到UI仔
,断点停住的时候,操作Debug
->Debug workfolw
->Always show Disassembly
,噢吼~运行流程就被清晰的跟踪到了。
虽然汇编麻麻赖赖的一大堆,但是不懂汇编,UIPerson
还不认识吗?symbol stub for: objc_alloc
也能看懂的。那这个断点标识又被拿到了,剩下的同理符号断点objc_alloc
,依旧能得到libobjc.A.dylib'objc_alloc
。
思路3:一刀切
假设其他方法都不会,明面上的 alloc
不就是最清晰的已知条件么,那么就干脆对allo
打一个符号断点试试,看看会发生什么?
噢吼~ 干脆直接的拿到 NSObject 的libobjc.A.dylib'objc_alloc
试探底层源码
通过上述三种思路对 alloc
分析,得出 [NSObject alloc]
的实现 libobjc
中,
那么
- 第一种方式,可以去苹果开源代码地址(下载objc4),只能根据已知符号去搜索方法名,生硬中冲满了苦涩。
- 第二种方式,去Cooci大神配置-可编译苹果官方源码objc!,可以直接将
objc4
库运行起来,即能command 方法名
跳转,还能运行起来跟断点,简直不要太舒服。
流程图
根据源码,对方法一路打好断点,跟着断点 alloc
底层的流程就出来了:
方法跟踪
首先:
+ (id)alloc {
return _objc_rootAlloc(self);
}
复制代码
在试探思路中多次见到的 _objc_rootAlloc
// Base class implementation of +alloc. cls is not nil.
// Calls [cls allocWithZone:nil].
id _objc_rootAlloc(Class cls)
{
return callAlloc(cls, false/*checkNil*/, true/*allocWithZone*/);
}
复制代码
根据层层递进,离核心也越来越近了。
// Call [cls alloc] or [cls allocWithZone:nil], with appropriate
// shortcutting optimizations.
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
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);
}
复制代码
至此,就进入了 alloc 底层实现的真正核心代码了。来看看这里都做了写什么操作吧。
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); //initIsa指针,将内存与类关联起来。
} 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
的核心作用就是开辟内存,初始化 Isa
指针与类绑定。
在最后的过程中还有一些可以继续展开的,如instanceSize
、calloc
、initInstanceIsa
的内部实现等,这个展开就等我另起一篇再更新。