一、isa指針結構 分析: 1.我們知道,實例對象的isa指針指向該對象所屬類的類對象;類對象的isa指向其元類對象; 2.真機為arm64架構,模擬器和mac電腦為x86架架構,以下以arm64為例講解; 3.在64位系統下,指針所占位元組為8個即64位; 4.在arm64之前,isa就是一個普通的 ...
一、isa指針結構
union isa_t { isa_t() { } isa_t(uintptr_t value) : bits(value) { } Class cls; uintptr_t bits; #if SUPPORT_PACKED_ISA // extra_rc must be the MSB-most field (so it matches carry/overflow flags) // nonpointer must be the LSB (fixme or get rid of it) // shiftcls must occupy the same bits that a real class pointer would // bits + RC_ONE is equivalent to extra_rc + 1 // RC_HALF is the high bit of extra_rc (i.e. half of its range) // future expansion: // uintptr_t fast_rr : 1; // no r/r overrides // uintptr_t lock : 2; // lock for atomic property, @synch // uintptr_t extraBytes : 1; // allocated with extra bytes # if __arm64__ # define ISA_MASK 0x0000000ffffffff8ULL # define ISA_MAGIC_MASK 0x000003f000000001ULL # define ISA_MAGIC_VALUE 0x000001a000000001ULL struct { uintptr_t nonpointer : 1; uintptr_t has_assoc : 1; uintptr_t has_cxx_dtor : 1; uintptr_t shiftcls : 33; // MACH_VM_MAX_ADDRESS 0x1000000000 uintptr_t magic : 6; uintptr_t weakly_referenced : 1; uintptr_t deallocating : 1; uintptr_t has_sidetable_rc : 1; uintptr_t extra_rc : 19; # define RC_ONE (1ULL<<45) # define RC_HALF (1ULL<<18) }; # elif __x86_64__ # define ISA_MASK 0x00007ffffffffff8ULL # define ISA_MAGIC_MASK 0x001f800000000001ULL # define ISA_MAGIC_VALUE 0x001d800000000001ULL struct { 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 deallocating : 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 // SUPPORT_PACKED_ISA #endif #if SUPPORT_INDEXED_ISA # if __ARM_ARCH_7K__ >= 2 # define ISA_INDEX_IS_NPI 1 # define ISA_INDEX_MASK 0x0001FFFC # define ISA_INDEX_SHIFT 2 # define ISA_INDEX_BITS 15 # define ISA_INDEX_COUNT (1 << ISA_INDEX_BITS) # define ISA_INDEX_MAGIC_MASK 0x001E0001 # define ISA_INDEX_MAGIC_VALUE 0x001C0001 struct { uintptr_t nonpointer : 1; uintptr_t has_assoc : 1; uintptr_t indexcls : 15; uintptr_t magic : 4; uintptr_t has_cxx_dtor : 1; uintptr_t weakly_referenced : 1; uintptr_t deallocating : 1; uintptr_t has_sidetable_rc : 1; uintptr_t extra_rc : 7; # define RC_ONE (1ULL<<25) # define RC_HALF (1ULL<<6) }; # else # error unknown architecture for indexed isa # endif // SUPPORT_INDEXED_ISA #endif };
分析:
1.我們知道,實例對象的isa指針指向該對象所屬類的類對象;類對象的isa指向其元類對象;
2.真機為arm64架構,模擬器和mac電腦為x86架架構,以下以arm64為例講解;
3.在64位系統下,指針所占位元組為8個即64位;
4.在arm64之前,isa就是一個普通的指針,存放著類(元類)對象的地址;之後,則需要&
ISA_MASK掩碼,才能獲取到類(元類)對象的地址,此時isa指針為一個共用體,存儲的信息不局限於類(元類)對象的地址;
5.存儲信息介紹:
其中,shiftcls結構體成員變數(33位)用來存儲類(元類)對象的地址;
二、類(元類)對象的地址取值原理——位域
1.結構體支持位域運算
//代碼
struct bs { unsigned a : 9;//如果超過位域範圍(511),則只取範圍內的值,其他位(高位)丟棄 unsigned b : 4; unsigned c : 3; }bit, *pbit; void test1() { bit.a = 512;//超過位域範圍報警告 bit.b = 10; bit.c = 7; NSLog(@"%d,%d,%d\n", bit.a, bit.b, bit.c); pbit=&bit; pbit-> a=0; pbit-> b&=3; pbit-> c|=1; printf("%d,%d,%d\n ",pbit-> a,pbit-> b,pbit-> c); }
//輸出
2019-10-08 18:22:37.051464+0800 SetAndGetsForMask[1966:248996] 0,10,7 0,2,7 Program ended with exit code: 0
//分析
1)unsigned即無符號整型,占4個位元組;結構體中成員變數所占記憶體相互獨立且連續;
2)以a為例,所占位數為9位即0b111111111(十進位511),所以a的取值範圍0~511,如果是512(二進位0b1000000000),由於只取低9位(000000000),所以取出值為0;
3)按位與&:兩個都為1運算結果為1,否則為0;按位或|:兩個都為0運算結果為0,否則為1;
2.參照isa,共用體套用結構體,一個char字元(一個位元組)存儲多個BOOL值並制定存儲位置
2.設置類屬性BOOL值(setter and getter)
//Person
#import "Person.h" //mask即掩碼,表示二進位數(0b開頭) #define TallMask (1<<0) //表示1左移0位:0b 0000 0001 #define RichMask (1<<1) //表示1左移1位:0b 0000 0010 #define HandsomeMask (1<<2) //表示1左移2位:0b 0000 0100 //拓展:10<<3即在10對應的二進位數後添加3個0 @interface Person() { char _saveBox; } @end @implementation Person - (instancetype)init { if (self = [super init]) { //用一個位元組來存儲三個變數:從最右往左依次為Tall、Rich、Handsome _saveBox = 0b00000101; } return self; } /*思路 0000 0101(_saveBox) |0000 0001(掩碼) --------- 0000 0001(賦值tall為1) 0000 0101 &1111 1110(掩碼取反) --------- 0000 0100(賦值tall為0) 1.如果賦的值為1,則按位或; 2.如果賦的值為0,則掩碼先取反,後按位與; */ - (void)setTall:(BOOL)tall { if (tall) { _saveBox |= TallMask; } else { _saveBox &= ~TallMask; } } - (void)setRich:(BOOL)rich { if (rich) { _saveBox |= RichMask; } else { _saveBox &= ~RichMask; } } - (void)setHandsome:(BOOL)handsome { if (handsome) { _saveBox |= HandsomeMask; } else { _saveBox &= ~HandsomeMask; } } /*思路 0000 0101 &0000 0001 --------- 0000 0001(取出tall值) 1.按位與,用掩碼取出_saveBox中特定位; 2.結果>=1,取反為0,再取反為1;同理,為0則雙取反後為0; */ - (BOOL)isTall { return !!(_saveBox & TallMask); } - (BOOL)isRich { return !!(_saveBox & RichMask); } - (BOOL)isHandsome { return !!(_saveBox & HandsomeMask); } @end
//Student
#import "Student.h" @interface Student() { /*思路 1.用一個結構體來存放變數; 2.結構體支持位域:按先後順序,一個char字元一個位元組(0b0000 0000),從最右至左依次為tall、rich、handsome; */ struct { char tall : 1;//用一位來存儲 char rich : 1; char handsome : 1; }_tallRichHandsome; } @end @implementation Student - (void)setTall:(BOOL)tall { _tallRichHandsome.tall = tall; } - (void)setRich:(BOOL)rich { _tallRichHandsome.rich = rich; } - (void)setHandsome:(BOOL)handsome { _tallRichHandsome.handsome = handsome; } - (BOOL)isTall { return !!_tallRichHandsome.tall;//非0(包括負數)取反為0 } - (BOOL)isRich { return !!_tallRichHandsome.rich; } - (BOOL)isHandsome { return !!_tallRichHandsome.handsome; } @end
//Worker
#import "Worker.h" #define TallMask (1<<0)//也可以左移6位,剩餘位沒用到 #define RichMask (1<<1) #define HandsomeMask (1<<2) #define ThinMask (1<<3) @interface Worker() { //蘋果系統設計思路 union { char bits;//一個位元組存儲結構體中的所有成員變數 struct {//擺設用:位域,增加可讀性 char tall : 1;//占一位 char rich : 1; char handsome : 1; char thin : 1; }; }_tallRichHandsome; } @end @implementation Worker - (void)setTall:(BOOL)tall { if (tall) { NSLog(@"----%c", _tallRichHandsome.bits); _tallRichHandsome.bits |= TallMask; } else { _tallRichHandsome.bits &= ~TallMask; } } - (void)setRich:(BOOL)rich { if (rich) { _tallRichHandsome.bits |= RichMask; } else { _tallRichHandsome.bits &= ~RichMask; } } - (void)setHandsome:(BOOL)handsome { if (handsome) { _tallRichHandsome.bits |= HandsomeMask; } else { _tallRichHandsome.bits &= ~HandsomeMask; } } - (void)setThin:(BOOL)thin { if (thin) { _tallRichHandsome.bits |= ThinMask; } else { _tallRichHandsome.bits &= ~ThinMask; } } - (BOOL)isTall { return !!(_tallRichHandsome.bits & TallMask); } - (BOOL)isRich { return !!(_tallRichHandsome.bits & RichMask); } - (BOOL)isHandsome { return !!(_tallRichHandsome.bits & HandsomeMask); } - (BOOL)isThin { return !!(_tallRichHandsome.bits & ThinMask); } @end
//main
#import <Foundation/Foundation.h> #import "Person.h" #import "Student.h" #import "Worker.h" #import "Engineer.h" struct bs { unsigned a : 9;//如果超過位域範圍(511),則只取範圍內的值,其他位(高位)丟棄 unsigned b : 4; unsigned c : 3; }bit, *pbit; void test1() { bit.a = 512;//超過位域範圍報警告 bit.b = 10; bit.c = 7; NSLog(@"%d,%d,%d\n", bit.a, bit.b, bit.c); pbit=&bit; pbit-> a=0; pbit-> b&=3; pbit-> c|=1; printf("%d,%d,%d\n ",pbit-> a,pbit-> b,pbit-> c); } void test2() { Person *per = [[Person alloc] init]; per.tall = NO; per.rich = NO; per.handsome = YES; NSLog(@"%d %d %d", per.isTall, per.isRich, per.isHandsome); } void test3() { Student *stu = [[Student alloc] init]; stu.tall = YES; stu.rich = NO; stu.handsome = YES; NSLog(@"%d %d %d", stu.isTall, stu.isRich, stu.isHandsome); } void test4() { Worker *worker = [[Worker alloc] init]; // worker.tall = YES; worker.rich = NO; worker.handsome = NO; worker.thin = YES; NSLog(@"%d %d %d", worker.isThin, worker.isRich, worker.isHandsome); } void test5() { Engineer *engineer = [[Engineer alloc] init]; // engineer.age = 12; // engineer.level = 6; // engineer.workers = 5; //0b 1111 1111 1111 1111(十進位:65535) //0b 0010 1100 1110 1101(十進位:11501) engineer->_personalInfo.bits =11501; NSLog(@"%d %d %d", engineer.getAge, engineer.getLevel, engineer.getWorkers); //2019-10-08 16:42:09.612140+0800 SetAndGetsForMask[1488:127227] 7 16 8160 // } int main(int argc, const char * argv[]) { @autoreleasepool { test1(); // test2(); // test3(); // test4(); // test5(); } return 0; }
//列印
2019-10-09 10:42:04.998750+0800 SetAndGetsForMask[2513:316066] 0 0 1 2019-10-09 10:42:04.999093+0800 SetAndGetsForMask[2513:316066] 1 0 1 2019-10-09 10:42:04.999122+0800 SetAndGetsForMask[2513:316066] 1 0 0 Program ended with exit code: 0
//分析(以Worker為例)
1)共用體中所有成員共同占用一塊記憶體區,其大小等於最大那個成員所占位元組數;
2)Worker中的結構體併為定義變數,編譯器不會計算其記憶體,僅是增加可讀性;
3)Worker中只有一個char型變數bits(占一個位元組),故該共用體變數_tallRichHandsome也占一個位元組;
4)結構體的位域限制變數的取值範圍(一位:即0或1),mask掩碼規定該變數存儲的位置(在哪一位上);
3.設置類屬性非BOOL類型(setter and getter)——限定變數值範圍且指定存儲位置
//Engineer
#import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN //位域位置(變數值存儲位置) #define AgeMask 0b00000111//最低三位存儲 #define LevelMask (1<<4)//低位往高位數,第5位存儲 #define WorkersMask 0b0001111111100000 @interface Engineer : NSObject { @public union { int bits; struct {//位域範圍(變數值範圍) int age : 3; int level : 1; int workers : 8; }; }_personalInfo; } //- (void)setAge:(int)age; //- (void)setLevel:(int)level; //- (void)setWorkers:(int)workers; - (int)getAge; - (int)getLevel; - (int)getWorkers; @end NS_ASSUME_NONNULL_END #import "Engineer.h" @implementation Engineer //- (void)setAge:(int)age //{ // self->_personalInfo.bits |= AgeMask; //} // //- (void)setLevel:(int)level //{ // self->_personalInfo.bits |= LevelMask; //} // //- (void)setWorkers:(int)workers //{ // self->_personalInfo.bits |= WorkersMask; //} - (int)getAge { return self->_personalInfo.bits & AgeMask; } - (int)getLevel { return self->_personalInfo.bits & LevelMask; } - (int)getWorkers { return self->_personalInfo.bits & WorkersMask; } @end
//列印
2019-10-09 11:08:14.617655+0800 SetAndGetsForMask[2630:349068] 5 0 3296 Program ended with exit code: 0
//說明
1)掩碼mask既可以直接用二進位(0b開頭)或十六進位(0x開頭)表示,也可以左移符號<<表示(一般用於位域為1的情況);
2)掩碼表示所占位數:1表示占住該位,0未占;並且所占位數應當是連續的,不存在兩側為1,中間為0的情況;
三、結論
1.arm64之後,isa是一個共用體類型的指針,存儲內部套用的結構體中的所有成員變數;
2.根據結構體的位域來限製成員變數的值範圍,用掩碼來規定成員變數存儲的位置,對掩碼按位與運算取出特定位置的成員變數的值;
如:用bits對ISA_MASK按位與運算後,得到的是類(元類)對象的地址;
可以看到shiftcls成員變數位域為33位,所占bits變數的存儲位置為:地位到高位第四位起,最低三位是空出來的
————因此,在arm64架構中,所有的類和元類對象地址二進位表示時最低三位都為0,十六進位表示時最低一位為0或8(這個用class和object_getClass去列印地址,此處不再展示了)!