Runtime

一、位运算
  1. &:按位与,可以用来取出特定位的值。需要取哪一位,就将哪一位置为0。掩码,一般是用来按位与运算。

         0011
        &0010
    ------
    0010
  2. |:按位或,可以用来设置某一位的值。需要设置哪一位,就将哪一位置为1。

         0001
        |0010
    ------
    0011
  3. ~:按位取反

  4. #define MJRichMask (1<<1),代表左移一位

  5. 使用位域

    struct {
        char tall : 1; // 只使用1位,默认开始是最右边一位
    char rich : 1;
    char handsome : 1;
    } tallRichHandsome;
二、isa
  1. 变化

    • 在arm64架构之前,isa就是一个普通的指针,存储着Class、Meta-Class对象的内存地址
    • 从arm64架构开始,对isa进行了优化,变成了一个共用体(union)结构,使用位域来存储更多的信息
        union isa_t {
        isa_t() { }
    isa_t(uintptr_t value) : bits(value) { }
    Class cls;
    uintptr_t bits;
    #if defined(ISA_BITFIELD)
    struct {
    ISA_BITFIELD; // defined in isa.h
    };
    #endif
    };
    # define ISA_BITFIELD \
    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
  2. isa位域详解

    • nonpointer

    0,代表普通指针,存储着Class、Meta-Class对象的内存地址
    1,代表优化过,使用位域存储更多的信息

    • has_assoc

    是否设置过关联对象,如果没有,释放时会更快

    • has_cxx_dtor

    是否有c++的析构函数(.cxx_destruct),如果没有,释放时会更快

    • shiftcls

    存储着Class、Meta-Class对象的内存地址信息

    • magic

    用于在调试时分辨对象是否未完成初始化

    • weakly_referenced

    是否有被弱引用指向过,如果没有,释放时会更快

    • deallocating

    对象是否正在释放

    • has_sidetable_rc

    引用计数器是否过大无法存储在isa中
    如果为1,那么引用计数会存储在一个叫SideTable的类的属性中

    • extra_rc

    里面存储的值是引用计数器减1

三、Class
  1. Class结构 image.png
  2. class_rw_t
    • class_rw_t里边的methods、properties、protocols都是二维数组,是可读可写的,包含了类的初始内容,分类的内容
      image.png
  3. class_ro_t
    • class_ro_t里面的baseMethodList、baseProtocols、ivars、baseProperties都是一维数组,是只读的,包含了类的初始内容
      image.png
  4. method_t

    • method_t是对方法/函数的封装
        struct method_t {
        SEL name; // 函数名
    const char *types; // 编码(返回值类型、参数类型)
    MethodListIMP imp; // 函数指针(函数地址)
    struct SortBySELAddress :
    public std::binary_function<const method_t&,
    const method_t&, bool>
    {
    bool operator() (const method_t& lhs,
    const method_t& rhs)
    { return lhs.name < rhs.name; }
    };
    };
    • IMP代表函数的具体实现
    typedef id _Nullable (*IMP)(id _Nonnull, SEL _Nonnull, ...); 
    
    • SEL代表方法名/函数名,一般叫做选择器,底层结构跟char *类似 * 可以通过@selector()sel_registerName()获得
      * 可以通过sel_getName()NSStringFromSelector()转成字符串
      * 不同类中相同名字的方法,所对应的方法选择器是相同的
    typedef struct objc_selector *SEL;
    
    • types包含了函数返回值、参数编码的字符串
      image.png
    • Type Encoding
      • iOS中提供了一个叫做@encode指令,可以将具体类型表示成字符串编码
        image.png
四、方法缓存
  • Class内部结构中有个方法缓存(cache_t),用散列表(哈希表)来缓存曾经调用过的方法,可以提高方法的查找速度(以空间换时间)

  • 缓存查找

五、objc_msgSend()
  1. 执行流程

    • OC中的方法,其实都是转换为objc_msgSend函数的调用
    • objc_msgSend的执行流程可以分为3大阶段
      • 消息发送
      • 动态方法解析
      • 消息转发
  2. objc_msgSend的源码跟读



  3. 消息发送流程

    • receiver通过isa指针找到receiverClass
    • receiverClass通过superclass指针找到superClass
    • 如果是从class_rw_t中查找方法
      • 已经排序的,二分查找
      • 没有排序的,遍历查找
  4. 动态方法解析流程

    • 可以实现以下方法,动态添加方法实现
      +resolveInstanceMethod
      +resolveClassMethod
    • 动态解析过后,会重新走“消息发送”流程
      • “从receiverClass的cache中查找方法”这一步开始执行
    • 动态方法解析例子
      • Method可以等价理解为struct method_t *
    • 补充:@dynamic告诉编译器不用自动生成gettersetter,不用自动生成成员变量,等到运行时再添加方法实现
  5. 消息转发流程

    • 开发者可以在forwardInvocation方法中自定义任何逻辑
    • 以上方法都有对象方法、类方法两种实现
    • NSMethodSignature的生成例子

六、super
  1. [super message]的底层实现
    • 消息接受者仍然是子类对象
    • 从父类开始查找方法的实现
  2. 使用clang转化的c++代码,不一定是程序最终生成的代码。super的调用,底层其实是转换为objc_msgSendSuper2函数的调用,接受两个参数

    • struct objc_super2
    • SEL
    struct objc_super2 {
    id receiver; // 消息接受者
    Class current_class; // receiver的Class对象
    };
七、LLVM的中间代码(IR)
  • Objective-C在变为机器代码之前,会被LLVM编译器转换为中间代码(Intermediate Representation)
  • 可以使用以下命令行指令生成中间代码
    clang -emit-llvm -S main.m
  • 语法简介
  • 具体可以参考官方文档:https://llvm.org/docs/LangRef.html
八、Runtime API
    • 动态创建一个类(参数:父类,类名,额外的内存空间)
      Class _Nullable objc_allocateClassPair(Class _Nullable superclass, const char * _Nonnull name, size_t extraBytes)
    • 注册一个类(需要在类注册之前添加成员变量)
      void objc_registerClassPair(Class _Nonnull cls)
    • 销毁一个类
      void objc_disposeClassPair(Class _Nonnull cls)
    • 获取isa指向的Class
      Class object_getClass(id obj)
    • 设置isa指向的Class
      Class object_setClass(id obj, Class cls)
    • 判断一个OC对象是否为Class
      BOOL object_isClass(id obj)
    • 判断一个Class是否为元类
      BOOL class_isMetaClass(Class cls)
    • 获取父类
      Class class_getSuperclass(Class cls)
  1. 成员变量
    • 获取一个实例变量信息
      Ivar class_getInstanceVariable(Class cls, const char * name)
    • 拷贝示例变量列表(最后需要调用free释放)
      Ivar *class_copyIvarList(Class cls,unsigned int *outCount)
    • 设置和获取成员变量的值
      void object_setIvar(id obj, Ivar ivar, id value)
      id object_getIvar(id obj, Ivar ivar)
    • 动态添加成员变量(已经注册的类不能动态添加成员变量)
      BOOL class_addIvar(Class cls, const char * name, size_t size, uint8_t alignment, const char * types)
    • 获取成员变量的相关信息
      const char *ivar_getName(Ivar v)
      const char *ivar_getTypeEncoding(Ivar v)
  2. 属性
    • 获取一个属性
      objc_property_t _Nullable class_getProperty(Class _Nullable cls, const char * _Nonnull name)
    • 拷贝属性列表(需要调用free释放)
      objc_property_t _Nonnull * _Nullable class_copyPropertyList(Class _Nullable cls, unsigned int * _Nullable outCount)
    • 动态添加属性
      BOOL class_addProperty(Class _Nullable cls, const char * _Nonnull name,
      const objc_property_attribute_t * _Nullable attributes,
      unsigned int attributeCount)
    • 动态替换属性
      void class_replaceProperty(Class _Nullable cls, const char * _Nonnull name,
      const objc_property_attribute_t * _Nullable attributes,
      unsigned int attributeCount)
    • 获取属性的一些信息
      const char * _Nonnull property_getName(objc_property_t _Nonnull property)
      const char * _Nullable property_getAttributes(objc_property_t _Nonnull property)
  3. 方法
    • 获得一个实例方法、类方法
      Method _Nullable class_getInstanceMethod(Class _Nullable cls, SEL _Nonnull name)
      Method _Nullable class_getClassMethod(Class _Nullable cls, SEL _Nonnull name)
    • 方法实现相关操作
      IMP _Nullable class_getMethodImplementation(Class _Nullable cls, SEL _Nonnull name)
      IMP _Nonnull method_setImplementation(Method _Nonnull m, IMP _Nonnull imp)
      void method_exchangeImplementations(Method _Nonnull m1, Method _Nonnull m2)
    • 拷贝方法列表(需要调用free函数释放)
      Method _Nonnull * _Nullable class_copyMethodList(Class _Nullable cls, unsigned int * _Nullable outCount)
    • 动态添加方法
      BOOL class_addMethod(Class _Nullable cls, SEL _Nonnull name, IMP _Nonnull imp,
      const char * _Nullable types)
    • 动态替换方法
      IMP _Nullable class_replaceMethod(Class _Nullable cls, SEL _Nonnull name, IMP _Nonnull imp,
      const char * _Nullable types)
    • 获取方法的相关信息(带有copy的需要调用free释放)
      SEL _Nonnull method_getName(Method _Nonnull m)
      IMP _Nonnull method_getImplementation(Method _Nonnull m)
      const char * _Nullable method_getTypeEncoding(Method _Nonnull m)
      unsigned int method_getNumberOfArguments(Method _Nonnull m)
      char * _Nonnull method_copyReturnType(Method _Nonnull m)
      char * _Nullable method_copyArgumentType(Method _Nonnull m, unsigned int index)
    • 选择器相关
      const char * _Nonnull sel_getName(SEL _Nonnull sel)
      SEL _Nonnull sel_registerName(const char * _Nonnull str)
    • 用block作为方法实现
      IMP _Nonnull imp_implementationWithBlock(id _Nonnull block)
      id _Nullable imp_getBlock(IMP _Nonnull anImp)
      BOOL imp_removeBlock(IMP _Nonnull anImp)

--Edited from Rpc

转载于:https://my.oschina.net/u/4112912/blog/3042997

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值