本文主要探究以下两个问题:
- 对象的本质
- isa
对象的本质
有如下.m文件
// 对象在底层的本质就是结构体
@interface Person : NSObject
@property (nonatomic, strong) NSString *Name;
@end
@implementation Person
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
}
return 0;
}
复制代码
使用clang命令 clang -rewrite-objc main.m -o main.cpp 把目标文件编译成c++文件。查看main.cpp文件
可以看出对象的本质是结构体,我们来看一下NSObject_IMPL
里面有一个Class类型的isa
我们看到有如下两个方法:
由于前面声明了Name的属性,所以这两个分别是Name的getter方法和setter方法
(*(NSString **)((char *)self + OBJC_IVAR_$_Person$_Name)); //由于对象在堆区开辟内存,要读取到一个变量的,首先要找到这个对象的首地址,然后对象的首地址+偏移量既可以读取变量
复制代码
里面还有一些protocal、method…在以后的介绍中在陆续展开分析。
isa
- isa的数据结构
在alloc文章中讲到,分配内存之后,会进行一个isa指针的绑定
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);
}
复制代码
objc_object::initIsa(Class cls, bool nonpointer, UNUSED_WITHOUT_INDEXED_ISA_AND_DTOR_BIT bool hasCxxDtor)
{
ASSERT(!isTaggedPointer());
isa_t newisa(0);
if (!nonpointer) {
newisa.setClass(cls, this);
} else {
ASSERT(!DisableNonpointerIsa);
ASSERT(!cls->instancesRequireRawIsa());
#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
# if ISA_HAS_CXX_DTOR_BIT
newisa.has_cxx_dtor = hasCxxDtor;
# endif
newisa.setClass(cls, this);
#endif
newisa.extra_rc = 1;
}
// 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;
}
复制代码
我们来看一下isa_t的结构如下,可以看出isa_t是一个共用体类型(关于共用体可以看补充部分)
__x86_64__下的isa
在isa_t内部存在Class、以及bits值,并且bits与cls是互斥关系
我们看下__x86_64__下的ISA_BITFIELD
# elif __x86_64__
# define ISA_MASK 0x00007ffffffffff8ULL
# define ISA_MAGIC_MASK 0x001f800000000001ULL
# define ISA_MAGIC_VALUE 0x001d800000000001ULL
# define ISA_HAS_CXX_DTOR_BIT 1
# define ISA_BITFIELD \
uintptr_t nonpointer : 1; \
uintptr_t has_assoc : 1; \
uintptr_t has_cxx_dtor : 1; \
uintptr_t shiftcls : 44; /*MACH_VM_MAX_ADDRESS 0x7fffffe00000*/ \
uintptr_t magic : 6; \
uintptr_t weakly_referenced : 1; \
uintptr_t unused : 1; \
uintptr_t has_sidetable_rc : 1; \
uintptr_t extra_rc : 8
# define RC_ONE (1ULL<<56)
# define RC_HALF (1ULL<<7)
# else
# error unknown architecture for packed isa
# endif
复制代码
- nonpointer:表示是否对 isa 指针开启指针优化 0:纯isa指针,1:不止是类对象地址,isa 中包含了类信息、对象的引用计数等。
- has_assoc:关联对象标志位,0没有,1存在。
- has_cxx_dtor:该对象是否有 C++ 或者 Objc 的析构器,如果有析构函数,则需要做析构逻辑, 如果没有,则可以更快的释放对象。
- shiftcls: 存储类指针的值。开启指针优化的情况下,在 arm64 架构中有 33 位用来存储类指针。
- magic:用于调试器判断当前对象是真的对象还是没有初始化的空间。
- weakly_referenced:志对象是否被指向或者曾经指向一个 ARC 的弱变量,没有弱引用的对象可以更快释放。
- deallocating:标志对象是否正在释放内存。
- has_sidetable_rc:当对象引用技术大于 10 时,则需要借用该变量存储进位。
- extra_rc:当表示该对象的引用计数值,实际上是引用计数值减 1, 例如,如果对象的引用计数为 10,那么 extra_rc 为 9。如果引用计数大于 10, 则需要使用到下面的 has_sidetable_rc。
怎么通过isa找到class
既然shiftcls中存在的是类的指针,那么我们来验证一下。
- 通过掩码
define ISA_MASK 0x00007ffffffffff8ULL
通过isa指针&掩码,掩码就像一个一个面具,漏出想要的部分
&掩码
下面我们来打印一下LGPerson.class的地址
2. 从x86_63位架构下的isa可以看到,shiftcls中可以读取class,我们可以先拿到isa,然后向右平移3位,向左平移20位,然后再向右平移17位取到class的地址(原因看补充)。
补充
- 1.联合体位域
struct LGCar1 {
BOOL front; // 0 1
BOOL back;
BOOL left;
BOOL right;
};
//从结构体的对齐原则中我们可以知道,LGCar1的实例占4个字节。
//如果用位域表示LGCar2只占4位(半个字节),当然没有半个字节,所以是一个字节
struct LGCar2 {
BOOL front: 1;
BOOL back : 1;
BOOL left : 1;
BOOL right: 1;
};
复制代码
struct LGCar1 car1;
struct LGCar2 car2;
NSLog(@"%ld-%ld",sizeof(car1),sizeof(car2));//4-1
复制代码
// 共存
struct LGTeacher1 {
char *name;
int age;
double height ;
};
// 联合体 : 互斥
union LGTeacher2 {
char *name;
int age;
double height ;
};
复制代码
struct LGTeacher1 teacher1;
teacher1.name = "Cooco";
teacher1.age = 18;
union LGTeacher2 teacher2;
teacher2.name = "Cooco";
teacher2.age = 18;
NSLog(@"%ld-%ld",sizeof(teacher1),sizeof(teacher2)); 24-8
复制代码
得知,结构体的内存是共存的,但是共用体是互斥的。
isa_t是共用体结构这样内存使用更为精细灵活。也节省了内存空间。
- 2.shiftcls查找原理
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END