iOS oc对象与alloc
oc中创建一个对象
Person *p = [[Person alloc] init];
那么alloc做了什么?init又做了什么?
可以通过查看苹果官方开源的objc相关的源码
objc的源码地址
源码是基于objc4-781,781的源码与之前的源码有很大的不同,781做了fastpath和slowpath的区别,781版本代码大概率会走fastpath流程。且与zone相关的api在iOS8之后被废弃了。
alloc
查看源码前看一段代码
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: 0x6000032e8430> - 0x6000032e8430 - 0x7ffee29471a8
<Person: 0x6000032e8430> - 0x6000032e8430 - 0x7ffee29471a0
<Person: 0x6000032e8430> - 0x6000032e8430 - 0x7ffee2947198
p1、p2和p3所指向的是同一个对象,指针p1、p2、p3的地址是不一样的
alloc的流程图
截屏2020-09-05 22.47.52.png
_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;
// 1:要开辟多少内存
size = cls->instanceSize(extraBytes);
if (outAllocatedSize) *outAllocatedSize = size;
id obj;
if (zone) {
obj = (id)malloc_zone_calloc((malloc_zone_t *)zone, 1, size);
} else {
// 2;怎么去申请内存
obj = (id)calloc(1, size);
}
if (slowpath(!obj)) {
if (construct_flags & OBJECT_CONSTRUCT_CALL_BADALLOC) {
return _objc_callBadAllocHandler(cls);
}
return nil;
}
// 3: obj绑定类型
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);
}
截屏2020-09-05 22.47.59.png
size = cls->instanceSize(extraBytes);开辟对象的内存空间大小
instanceSize的源码在Class中,
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(extraBytes);分配了内存大小,fastInstanceSize中有内存对齐的算法,oc对象的字节对齐是16,需要注意的是苹果对源码进行了更新, size_t size = alignedInstanceSize() + extraBytes; 以前是通过该代码进行分配,但是有了fastPath后,代码大概率会走fastPath。
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) {
return (x + size_t(15)) & ~size_t(15);
}
假设x为8,则8 + 15 的16进制为
0000 0000 0001 0111
15取反的16进制
1111 1111 1111 0000
两个数进行位与
0000 0000 0001 0000
calloc 开辟内存区域
此时calloc为obj开辟了内存空间,但是并没有与类进行关联,
打印的至少内存地址0x6000032e8430
obj->initInstanceIsa
内存空间与类型进行绑定,打印的是
<Person: 0x6000032e8430>
init
构造方法/工厂设计,提供给开发人员的初始化方法
+ (id)init {
return (id)self;
}
- (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;
}
new
new 也可以初始化对象
+ (id)new {
return [callAlloc(self, false/*checkNil*/) init];
}
new 等同于[alloc init]
对象的内存
@interface Person : NSObject
// 影响的因素的 对象: 属性 : 8 + 8 + 8 = 24
// 内存的布局 属性
// isa
@property (nonatomic,strong) NSString *name;
@property (nonatomic) int hobby;
-(void)getUserName;
@end
oc对象的字节对齐是16,其中8个字节是isa指针,
影响oc对象内存大小与属性有关
Person *objc1 = [[Person alloc] init];
objc1.name = @"哈哈哈";
objc1.hobby= 180;
[objc1 getUserName];
NSLog(@"objc对象实际需要的内存大小: %zd", class_getInstanceSize([objc1 class]));
NSLog(@"objc对象实际分配的内存大小: %zd", malloc_size((__bridge const void *)(objc1)));
打印信息
objc对象实际需要的内存大小: 24
objc对象实际分配的内存大小: 32