美文网首页iOS Interview
Runtime-内存模型

Runtime-内存模型

作者: iOS小虫下米 | 来源:发表于2020-03-08 10:35 被阅读0次

    Runtime 是 iOS编程人员的核心基础知识,Objc Runtime使得C具有了面向对象能力,在程序运行时创建,检查,修改类、对象和它们的方法。
    runtime是开源的,GitHub 有可调式的源码 objc4_debug(待工程配置过程👍),也可以去官网找 objc4.
    学习参考 苹果官方的Runtime编程指南

    isa & id

    第一印象指针,在<objc.h>中可以看到:

    /// An opaque type that represents an Objective-C class.
    typedef struct objc_class *Class;
    
    /// Represents an instance of a class.
    struct objc_object {
        Class _Nonnull isa  OBJC_ISA_AVAILABILITY;
    };
    
    /// A pointer to an instance of a class.
    typedef struct objc_object *id;
    };
    
    1. isaobjc_object一个属性,类型是Class
    2. Class 是指向 objc_class的一个指针,存放着objc_class的地址
    3. id是一个objc_object结构类型的指针,这个类型的对象能够转换成任何一种对象

    Class

    objc_class 是什么呢?让我们看到<objc/runtime.h>

    struct objc_class {
        Class _Nonnull isa  OBJC_ISA_AVAILABILITY;
    
    #if !__OBJC2__
        Class _Nullable super_class                              OBJC2_UNAVAILABLE; // 父类 
        const char * _Nonnull name                               OBJC2_UNAVAILABLE; // 类名
        long version                                             OBJC2_UNAVAILABLE; //  类的版本信息,默认为0
        long info                                                OBJC2_UNAVAILABLE; // 类信息,供运行期间使用的一些位标识
        long instance_size                                       OBJC2_UNAVAILABLE; // 该类的实例变量大小
        struct objc_ivar_list * _Nullable ivars                  OBJC2_UNAVAILABLE; // 该类的成员变量链表
        struct objc_method_list * _Nullable * _Nullable methodLists                    OBJC2_UNAVAILABLE; // 方法定义的链表
        struct objc_cache * _Nonnull cache                       OBJC2_UNAVAILABLE; // 方法缓存
        struct objc_protocol_list * _Nullable protocols          OBJC2_UNAVAILABLE;
    #endif
    
    } OBJC2_UNAVAILABLE;
    /* Use `Class` instead of `struct objc_class *` */
    
    1. objc_class中也有一个isa指针,指向Meta Class。作用:因为 Objc 的类本身也是一个对象,为了处理这个关系,runtime就创造了Meta Class,当给类发送 [NSObject alloc] 的消息时,实际上是把这个消息发给 NSObject Class Object。
    2. cache 作用:对象接受到一个消息,首次会根据isa指针查找消息对象,然后在methodLists遍历,调用后加入cache,下次直接从缓存获取,提高调用效率

    objc_ivar_list

    objc_ivar_list 结构体存储objc_ivar(成员变量)数组列表

    struct objc_ivar {
        char * _Nullable ivar_name                               OBJC2_UNAVAILABLE; // 命名
        char * _Nullable ivar_type                               OBJC2_UNAVAILABLE; // 类型
        int ivar_offset                                          OBJC2_UNAVAILABLE;
    #ifdef __LP64__
        int space                                                OBJC2_UNAVAILABLE;
    #endif
    }    
    
    struct objc_ivar_list {
        int ivar_count                                           OBJC2_UNAVAILABLE;
    #ifdef __LP64__
        int space                                                OBJC2_UNAVAILABLE;
    #endif
        /* variable length structure */
        struct objc_ivar ivar_list[1]                            OBJC2_UNAVAILABLE;
    }                                                            OBJC2_UNAVAILABLE;
    
    

    objc_method_list

    objc_method_list结构体存储objc_method(方法)数组列表

    struct objc_method {
        SEL _Nonnull method_name                                 OBJC2_UNAVAILABLE;
        char * _Nullable method_types                            OBJC2_UNAVAILABLE;
        IMP _Nonnull method_imp                                  OBJC2_UNAVAILABLE;
    }                                                            OBJC2_UNAVAILABLE;
    
    struct objc_method_list {
        struct objc_method_list * _Nullable obsolete             OBJC2_UNAVAILABLE;
    
        int method_count                                         OBJC2_UNAVAILABLE;
    #ifdef __LP64__
        int space                                                OBJC2_UNAVAILABLE;
    #endif
        /* variable length structure */
        struct objc_method method_list[1]                        OBJC2_UNAVAILABLE;
    }  
    

    objc_cache

    struct objc_cache {
        unsigned int mask /* total = mask + 1 */                 OBJC2_UNAVAILABLE;
        unsigned int occupied                                    OBJC2_UNAVAILABLE;
        Method _Nullable buckets[1]                              OBJC2_UNAVAILABLE;
    };
    
    1. mask: 指定分配缓存bucket的总数。runtime 使用这个字段确定线性查找的索引位置
    2. occupied: 实际占用缓存 bucket的总数
    3. 指向Method数据结构指针的数组,总数不能超过mask+1;指针可能为空,这标识缓存bucket没有被占用,数组随着时间增长

    Meta Class & Super Class

    提供一个实例,来体感metaclasssuperclass

    #import <Foundation/Foundation.h>
    #import <objc/runtime.h>
    
    typedef struct mtm_objc_class *mClass;
    struct mtm_objc_class {
        mClass ISA;
        mClass superclass;
    };
    
    void testMetaClass(id self, SEL _cmd) {
        NSLog(@"This object is %p", self);
        NSLog(@"Class is %@, super class is %@", [self class], [self superclass]);
        
        Class currentClass = [self class];
        for (int i = 0; i < 4; i++) {
            NSLog(@"Following the [isa] pointer %d timer gives %p, class name is %@", i, currentClass, NSStringFromClass(currentClass));
            currentClass = object_getClass(currentClass);
        }
        
        mClass testClass = (__bridge mClass)[self superclass];
        for (int i = 0; i < 4; i++) {
            NSLog(@"Following the [super] pointer %d timer gives %p", i, testClass);
            testClass = testClass ? testClass->superclass : nil;
        }
        NSLog(@"NSObject's class is %p", [NSObject class]);
        Class meta_NSObject = object_getClass([NSObject class]);
        NSLog(@"NSobject's meta class is %p, meta super class is %p", meta_NSObject, ((__bridge mClass)meta_NSObject)->superclass);
    }
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            // insert code here...
            Class newClass = objc_allocateClassPair([NSURL class], "TestClass", 0);
            class_addMethod(newClass, @selector(testHandler), (IMP)testMetaClass, "v@:");
            objc_registerClassPair(newClass);
            id instance = [[newClass alloc] initWithString:@"http://www.baidu.com"];
            [instance performSelector:@selector(testHandler)];
        }
        return 0;
    }
    

    结果如下

    ObjcTest[74791:1048449] This object is 0x100784c00
    ObjcTest[74791:1048449] Class is TestClass, super class is NSURL
    ObjcTest[74791:1048449] Following the [isa] pointer 0 timer gives 0x100784420, class name is TestClass
    ObjcTest[74791:1048449] Following the [isa] pointer 1 timer gives 0x100784450, class name is TestClass
    ObjcTest[74791:1048449] Following the [isa] pointer 2 timer gives 0x1003310f0, class name is NSObject
    ObjcTest[74791:1048449] Following the [isa] pointer 3 timer gives 0x1003310f0, class name is NSObject
    ObjcTest[74791:1048449] Following the [super] pointer 0 timer gives 0x7fff859d7230
    ObjcTest[74791:1048449] Following the [super] pointer 1 timer gives 0x100331140
    ObjcTest[74791:1048449] Following the [super] pointer 2 timer gives 0x0
    ObjcTest[74791:1048449] Following the [super] pointer 3 timer gives 0x0
    ObjcTest[74791:1048449] NSObject's class is 0x100331140
    ObjcTest[74791:1048449] NSobject's meta class is 0x1003310f0, meta super class is 0x100331140
    

    把日志中的地址放到图里,应该就更好理解 isa 链表super 链表

    class_isa_super_class.png

    小测试

    来看看能答对几个?

    BOOL res1 = [(id)[NSObject class] isKindOfClass:[NSObject class]];  
    BOOL res2 = [(id)[NSObject class] isMemberOfClass:[NSObject class]];  
    BOOL res3 = [(id)[NSURL class] isKindOfClass:[NSURL class]];  
    BOOL res4 = [(id)[NSURL class] isMemberOfClass:[NSURL class]];
    

    解密开始

    1. 误区:isKindOfClass有类方法和对象方法,实现有区别
    2. res1:第一次,Class 和 MetaClass(Class->ISA)对比,失败;再跟NSObject's meta class 的 superclass 即 NSObject class,结果为true
    3. res2:Class->ISA == Class 为false,下面依此类推
    4. res3 = false
    5. res4 = false
    + (BOOL)isKindOfClass:(Class)cls {
        for (Class tcls = self->ISA(); tcls; tcls = tcls->superclass) {
            if (tcls == cls) return YES;
        }
        return NO;
    }
    
    - (BOOL)isKindOfClass:(Class)cls {
        for (Class tcls = [self class]; tcls; tcls = tcls->superclass) {
            if (tcls == cls) return YES;
        }
        return NO;
    }
    
    + (BOOL)isMemberOfClass:(Class)cls {
        return self->ISA() == cls;
    }
    
    - (BOOL)isMemberOfClass:(Class)cls {
        return [self class] == cls;
    }
    

    相关文章

      网友评论

        本文标题:Runtime-内存模型

        本文链接:https://www.haomeiwen.com/subject/vzitrhtx.html