引言
我们都知道,一个类有成员变量、对象方法、类方法,那么它们在底层是如何实现的呢?
前提
要想研究上面内容,首先要知道oc对象在c++
层面对应的内容,新创建一个工程,里面创建一个Dog类,编译main.m文件,就会发现与main.m
同级的目录下多了一个cpp
文件。
#import <Foundation/Foundation.h>
@interface Dog : NSObject
@property(nonatomic,copy)NSString*dogName;
-(void)eatFood;
-(void)playWithWhom:(NSString*)name;
@end
#import "Dog.h"
@implementation Dog
-(void)eatFood
{
NSLog(@"eatFood");
}
-(void)playWithWhom:(NSString*)name
{
NSLog(@"--%@",name);
}
@end
打开cpp
搜索关键字
dogName,可以看出Dog类是一个结构体Dog_IMPL
,我们继续看NSObject_IMPL
,发现也是一个结构体,里面只有isa
指针,并没有我们定义的成员变量和方法。
NSObject
在runtime
中对应的源码为
类对象
的源码为
接下来引出我们的重点
,我们可以看出objc_class
里最主要的三个成员变量。
Class superclass; //继承 objc_object 的ISA 8字节
cache_t cache; // formerly cache pointer and vtable //16字节,下面有详解
class_data_bits_t bits; // class_rw_t * plus custom rr/alloc flags
其余的全是方法
不用考虑,因为方法的存储是在别的地方。
首先,我们来研究class_data_bits_t bits
,通过首地址
+指针偏移量
来获取到bits
struct cache_t {
private:
explicit_atomic<uintptr_t> _bucketsAndMaybeMask; //8个字节
union {
struct {
explicit_atomic<mask_t> _maybeMask; //4字节
#if __LP64__
uint16_t _flags; //2字节
#endif
uint16_t _occupied; //2字节
};
explicit_atomic<preopt_cache_t *> _originalPreoptCache; //8个字节
};
...省略的是一些static变量(全局区)和方法,没有影响。
}
由于联合体union
共用内存,所以union
为8
个字节,所以cache_t
为 8 (uintptr_t)+ 8(union)=16字节。
结论:
想获取bits
的值,就是首地址
偏移 8+8+16 = 32
个字节。
创建一个类:WJPerson
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface WJPerson : NSObject{
int age;
}
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) float height;
- (void)playGuitar;
+ (void)eatFood;
// 1: 类的探究分析
// 2: 类方法 + ivar 在哪里
@end
NS_ASSUME_NONNULL_END
#import "WJPerson.h"
@implementation WJPerson
- (instancetype)init{
if (self = [super init]) {
self.name = @"wuji";
}
return self;
}
- (void)playGuitar{
}
+ (void)eatFood{
}
@end
那么,成员变量
和方法
到底存储在哪里呢?
经过源码分析,我们发现class_data_bits_t
里面有个data
方法,返回的class_rw_t
是结构体
类型。
class_rw_t* data() const {
return (class_rw_t *)(bits & FAST_DATA_MASK);
}
那么class_rw_t
里面有什么呢?
struct class_rw_t {
const method_array_t methods() const {
auto v = get_ro_or_rwe();
if (v.is<class_rw_ext_t *>()) {
return v.get<class_rw_ext_t *>(&ro_or_rw_ext)->methods;
} else {
return method_array_t{v.get<const class_ro_t *>(&ro_or_rw_ext)->baseMethods()};
}
}
const property_array_t properties() const {
auto v = get_ro_or_rwe();
if (v.is<class_rw_ext_t *>()) {
return v.get<class_rw_ext_t *>(&ro_or_rw_ext)->properties;
} else {
return property_array_t{v.get<const class_ro_t *>(&ro_or_rw_ext)->baseProperties};
}
}
const protocol_array_t protocols() const {
auto v = get_ro_or_rwe();
if (v.is<class_rw_ext_t *>()) {
return v.get<class_rw_ext_t *>(&ro_or_rw_ext)->protocols;
} else {
return protocol_array_t{v.get<const class_ro_t *>(&ro_or_rw_ext)->baseProtocols};
}
}
//存储成员变量,比如age
const class_ro_t *ro() const {
auto v = get_ro_or_rwe();
if (slowpath(v.is<class_rw_ext_t *>())) {
return v.get<class_rw_ext_t *>(&ro_or_rw_ext)->ro;
}
return v.get<const class_ro_t *>(&ro_or_rw_ext);
}
...//里面省略的包括一些copy之类的方法,这里不做研究
};
那么,method_array_t
、property_array_t
、protocol_array_t
里面会不会存储着我们定义的方法
和成员变量
呢?
-
property_array_t:
(lldb) x/4gx WJPerson.class 0x100004470: 0x0000000100004498 0x0000000100354140 0x100004480: 0x000000010034b360 0x0000802000000000 (lldb) p/x 0x100004470+0x20 //首地址(第一个地址)+ 偏移量32字节(16进制即0x20) (long) $5 = 0x0000000100004490 (lldb) p (class_data_bits_t*)0x0000000100004490 (class_data_bits_t *) $6 = 0x0000000100004490 (lldb) p $6->data() (class_rw_t *) $9 = 0x0000000100706010 //获取class_rw_t结构体 (lldb) p $9->properties() //获取properties() (const property_array_t) $11 = { list_array_tt<property_t, property_list_t, RawPtr> = { = { list = { ptr = 0x0000000100004348 } arrayAndFlag = 4294984520 } } } (lldb) p $11.list (const RawPtr<property_list_t>) $12 = { ptr = 0x0000000100004348 } (lldb) p $12.ptr (property_list_t *const) $13 = 0x0000000100004348 (lldb) p *$13 (property_list_t) $14 = { entsize_list_tt<property_t, property_list_t, 0, PointerModifierNop> = (entsizeAndFlags = 16, count = 2) } (lldb) p $14.get(0) (property_t) $15 = (name = "name", attributes = "T@\"NSString\",C,N,V_name") (lldb) p $14.get(1) (property_t) $16 = (name = "height", attributes = "Tf,N,V_height") (lldb) p $14.get(2) Assertion failed: (i < count), function get, file
至此,我们获取到了属性
name
和height
,但是我们并没有看到成员变量age
,为什么,age
到底在哪里,这里放到最后探究。 -
method_array_t:
(lldb) p $9->methods() (const method_array_t) $17 = { list_array_tt<method_t, method_list_t, method_list_t_authed_ptr> = { = { list = { ptr = 0x0000000100004248 } arrayAndFlag = 4294984264 } } } (lldb) p $17.list.ptr (method_list_t *const) $18 = 0x0000000100004248 (lldb) p *$18 (method_list_t) $19 = { entsize_list_tt<method_t, method_list_t, 4294901763, method_t::pointer_modifier> = (entsizeAndFlags = 27, count = 6) } (lldb) p $19.get(0) (method_t) $20 = {}//这个地方直接get()打印不出来我们想要的结果了,下面分析 (lldb) p $19.get(0).big() (method_t::big) $21 = { name = "playGuitar" types = 0x0000000100003f72 "v16@0:8" imp = 0x0000000100003d20 (KCObjcBuild`-[WJPerson playGuitar]) } (lldb) p $19.get(1).big() (method_t::big) $22 = { name = "init" types = 0x0000000100003f6a "@16@0:8" imp = 0x0000000100003cc0 (KCObjcBuild`-[WJPerson init]) } (lldb) p $19.get(2).big() (method_t::big) $23 = { name = "name" types = 0x0000000100003f6a "@16@0:8" imp = 0x0000000100003d30 (KCObjcBuild`-[WJPerson name]) } (lldb) p $19.get(3).big() (method_t::big) $24 = { name = "height" types = 0x0000000100003f89 "f16@0:8" imp = 0x0000000100003d90 (KCObjcBuild`-[WJPerson height]) } (lldb) p $19.get(4).big() (method_t::big) $25 = { name = "setName:" types = 0x0000000100003f7a "v24@0:8@16" imp = 0x0000000100003d60 (KCObjcBuild`-[WJPerson setName:]) } (lldb) p $19.get(5).big() (method_t::big) $26 = { name = "setHeight:" types = 0x0000000100003f91 "v20@0:8f16" imp = 0x0000000100003db0 (KCObjcBuild`-[WJPerson setHeight:]) } (lldb) p $19.get(6).big() Assertion failed: (i < count), function get, file
至此,我们获取到了- (void)playGuitar
,同时还有name
和height
的set、get
方法,但是我们并没有看到+ (void)eatFood
,为什么,+ (void)eatFood
到底在哪里,最后探究。
补充: method_t
和property_t
调用get()
分析
struct property_t {
const char *name;
const char *attributes; //这个里面有name和attributes可以输出相关的信息
};
struct method_t { //这个里面什么都没有,但是里面有一个big结构体,它的里面有信息。可以通过big()获取
struct big {
SEL name;
const char *types;
MethodListIMP imp;
};
big &big() const {
ASSERT(!isSmall());
return *(struct big *)this;
}
-vars(成员变量)存储
(lldb) p $9->ro()
(const class_ro_t *) $34 = 0x0000000100004200
(lldb) p *$34
(const class_ro_t) $35 = {
flags = 0
instanceStart = 8
instanceSize = 24
reserved = 0
= {
ivarLayout = 0x0000000000000000
nonMetaclass = nil
}
name = {
std::__1::atomic<const char *> = "WJPerson" {
Value = 0x0000000100003ee4 "WJPerson"
}
}
baseMethodList = 0x0000000100004248
baseProtocols = 0x0000000000000000
ivars = 0x00000001000042e0
weakIvarLayout = 0x0000000000000000
baseProperties = 0x0000000100004348
_swiftMetadataInitializer_NEVER_USE = {}
}
(lldb) p $35.ivars
(const ivar_list_t *const) $36 = 0x00000001000042e0
(lldb) p *$36
(const ivar_list_t) $37 = {
entsize_list_tt<ivar_t, ivar_list_t, 0, PointerModifierNop> = (entsizeAndFlags = 32, count = 3)
}
(lldb) p $37.get(0)
(ivar_t) $38 = {
offset = 0x0000000100004408
name = 0x0000000100003e95 "age" //这里输出了age成员变量
type = 0x0000000100003f85 "i"
alignment_raw = 2
size = 4
}
(lldb) p $37.get(1)
(ivar_t) $39 = {
offset = 0x0000000100004410
name = 0x0000000100003e99 "_height"
type = 0x0000000100003f87 "f"
alignment_raw = 2
size = 4
}
(lldb) p $37.get(2)
(ivar_t) $40 = {
offset = 0x0000000100004418
name = 0x0000000100003ea1 "_name"
type = 0x0000000100003f5e "@\"NSString\""
alignment_raw = 3
size = 8
}
(lldb) p $37.get(3)
Assertion failed: (i < count), function get, file
至此,我们从ivars获取到了age
,同时还有_name
和_height
。
由上面也可得出一个结论:property
=ivar + get + set