根据类objc_class的结构,自定义lg_objc_class,来验证cache_t缓存,前提是了解原本cache_t的结构。
#import <Foundation/Foundation.h>
#import "LGPerson.h"
#import <objc/runtime.h>
typedef uint32_t mask_t;
typedef uintptr_t cache_key_t;
typedef unsigned long uintptr_t;
struct lg_bucket_t {
IMP _imp;
cache_key_t _key;
};
struct lg_cache_t {
struct lg_bucket_t *_buckets;
mask_t _mask;
mask_t _occupied;
};
struct lg_class_data_bits_t {
uintptr_t bits;
};
struct lg_objc_class {
Class ISA;
Class superclass;
struct lg_cache_t cache; // formerly cache pointer and vtable
struct lg_class_data_bits_t bits; // class_rw_t * plus custom rr/alloc flags
};
int main(int argc, const char * argv[]) {
@autoreleasepool {
LGPerson *person = [[LGPerson alloc] init];
Class pClass = [LGPerson class];
[person sayHello];
[person sayCode];
[person sayNB];
struct lg_objc_class *lg_pClass = (__bridge struct lg_objc_class *)(pClass);
for (mask_t i = 0; i<lg_pClass->cache._mask; i++) {
struct lg_bucket_t bucket = lg_pClass->cache._buckets[i];
NSLog(@"%lu - %p",bucket._key,bucket._imp);
}
NSLog(@"%@ - %p",person,pClass);
}
return 0;
}
使用此方法,可快速打印了解缓存的方法信息。