对于iOS开发者来说alloc
、init
、new
使用非常频繁,那么接下来说说本人的理解。
准备工作:
1、alloc
做了什么?
-
根据源码可得知流程,如下图
alloc源码流程图.png
其中instanceSize
、calloc
、initInstanceIsa
这三个函数最为重要,我们来分析一下。-
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
函数
static inline size_t align16(size_t x) {
return (x + size_t(15)) & ~size_t(15);
}
总结:instanceSize
函数分析需要开辟内存大小作用,其中align16
函数使用了16字节对齐算法。
-
calloc
函数,开辟根据instanceSize
函数分析出来的内存大小
obj = (id)calloc(1, size);
-
initInstanceIsa
函数,初始化一个isa指针,并将isa指针指向申请的内存地址,在将指针与类进行关联
inline void
objc_object::initInstanceIsa(Class cls, bool hasCxxDtor)
{
ASSERT(!cls->instancesRequireRawIsa());
ASSERT(hasCxxDtor == cls->hasCxxDtor());
initIsa(cls, true, hasCxxDtor);
}
inline void
objc_object::initIsa(Class cls, bool nonpointer, bool hasCxxDtor)
{
ASSERT(!isTaggedPointer());
if (!nonpointer) {
isa = isa_t((uintptr_t)cls);
} else {
ASSERT(!DisableNonpointerIsa);
ASSERT(!cls->instancesRequireRawIsa());
isa_t newisa(0);
#if SUPPORT_INDEXED_ISA
ASSERT(cls->classArrayIndex() > 0);
newisa.bits = ISA_INDEX_MAGIC_VALUE;
// isa.magic is part of ISA_MAGIC_VALUE
// isa.nonpointer is part of ISA_MAGIC_VALUE
newisa.has_cxx_dtor = hasCxxDtor;
newisa.indexcls = (uintptr_t)cls->classArrayIndex();
#else
newisa.bits = ISA_MAGIC_VALUE;
// isa.magic is part of ISA_MAGIC_VALUE
// isa.nonpointer is part of ISA_MAGIC_VALUE
newisa.has_cxx_dtor = hasCxxDtor;
newisa.shiftcls = (uintptr_t)cls >> 3;
#endif
// This write must be performed in a single store in some cases
// (for example when realizing a class because other threads
// may simultaneously try to use the class).
// fixme use atomics here to guarantee single-store and to
// guarantee memory order w.r.t. the class index table
// ...but not too atomic because we don't want to hurt instantiation
isa = newisa;
}
}
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;
}
源码看返回了self
本身,为啥还要写init
?init
是一个构造方法 ,是通过工厂设计(工厂方法模式),主要是用于给用户提供构造方法入口。
3、new
做了什么?
+ (id)new {
return [callAlloc(self, false/*checkNil*/) init];
}
源码看new
等价于alloc
+ init
4、补充16字节对齐算法
理解
16字节对齐的需要
- 通常内存是由一个个字节组成的,cpu在存取数据时,并不是以字节为单位存储,而是以块为单位存取,块的大小为内存存取力度。频繁存取字节未对齐的数据,会极大降低cpu的性能,所以可以通过减少存取次数来降低cpu的开销
- 16字节对齐,是由于在一个对象中,第一个属性isa占8字节,当然一个对象肯定还有其他属性,当无属性时,会预留8字节,即16字节对齐,如果不预留,相当于这个对象的isa和其他对象的isa紧挨着,容易造成访问混乱
16字节对齐后,可以加快CPU读取速度,同时使访问更安全,不会产生访问混乱的情况
例子align16(9)
static inline size_t align16(size_t x) {
return (x + size_t(15)) & ~size_t(15);
}
- 9 + 15 = 24 =
0000 0000 0001 1000
- 15 =
0000 0000 0000 1111
,~15
(取反15) =1111 1111 1111 0000
-
&
的算法规则:两个为1
才为1
,则为0
。所以24 & ~15
=0000 0000 0001 0000
= 16 -
align16(9)
= 16