美文网首页面试
iOS 源码分析 Class 本质,objc_class,cla

iOS 源码分析 Class 本质,objc_class,cla

作者: 孙掌门 | 来源:发表于2020-04-04 13:55 被阅读0次

    iOS 源码分析 Class 本质,objc_class,class_rw_t,class_ro_t 分析

    我们先来看下源码内部对clas的定义

    typedef struct objc_class *Class;
    
    

    可以看出来,他就是一个 objc_class 指针

    
    struct objc_class : objc_object {
        // Class ISA;
        Class superclass;
        cache_t cache;             // formerly cache pointer and vtable
        class_data_bits_t bits;    // class_rw_t * plus custom rr/alloc flags
    
        class_rw_t *data() { 
            return bits.data();
        }
    
    
    

    我们摘出来重要的部分,可以看出来,我们的 objc_class 是继承 objc_object 结构体

    
    struct objc_object {
    private:
        isa_t isa;
    
    

    我们的 objc_object 有 isa 指针,所以我们的 class 也有 isa 指针,这就是为什么 我们可以通过我们的类对象找到我们的元类的原因,因为我们的额class 有 isa 指针,如果没有isa指针是不行的,然后还有个 superclass class 指针,只用来指向当前类的父类的,苹果有一张图,画了父类和元类的关系,指向,class 的这个结构体,完全可以诠释那张图,cache_t 我之前的文章也讲过,是加快查找效率的,可以看看我那篇文章,然后,下一个很重要的东西就是 bits

    struct class_data_bits_t {
    
        // Values are the FAST_ flags above.
        uintptr_t bits;
    
    

    bits 可以理解为一个指针,里面存放着 class_rw_t 和 class_ro_t 的地址,之前文章也有说过,苹果为了节省空间,让一个指针里面保存更多的信息,到时候通过按位与运算,取出不同的值就行。

    比如当我们取出来我们class的data的时候,实际上就是取出来这个类的一些信息

     class_rw_t *data() { 
            return bits.data();
        }
    
    

    可以看到,返回的是一个 class_rw_t 结构体,然后调用 bits.data();,我们看下实现

    class_rw_t* data() {
            return (class_rw_t *)(bits & FAST_DATA_MASK);
        }
    
    

    这段代码什么意思呢?就是就是找到 bits 这个指针中 class_rw_t 这个结构体对应的那个data的值,通过按位与运算,#define FAST_DATA_MASK 0x00007ffffffffff8UL 其实就是取出来,bits中第3-64位,然后就拿到了我们想要的值,这个设计是不是很牛,然后我们看下 class_rw_t 这个结构体

    
    struct class_rw_t {
        // Be warned that Symbolication knows the layout of this structure.
        uint32_t flags;
        uint32_t version;
    
        const class_ro_t *ro;
    
        method_array_t methods;
        property_array_t properties;
        protocol_array_t protocols;
    
        Class firstSubclass;
        Class nextSiblingClass;
    
    
    

    从结构体重可以看到,累的 方法,属性,协议,都保存在这里,然后还有个 const class_ro_t *ro; 这个是什么呢?

    
    
    struct class_ro_t {
        uint32_t flags;
        uint32_t instanceStart;
        uint32_t instanceSize;
    #ifdef __LP64__
        uint32_t reserved;
    #endif
    
        const uint8_t * ivarLayout;
        
        const char * name;
        method_list_t * baseMethodList;
        protocol_list_t * baseProtocols;
        const ivar_list_t * ivars;
    
        const uint8_t * weakIvarLayout;
        property_list_t *baseProperties;
    
    
    
    1. baseMethodList 方法列表
    2. baseProtocols 协议列表
    3. ivars 成员变量列表
    4. baseProperties 属性列表
    5. weakIvarLayout weak 成员变量内存布局
    6. ivarLayout 成员变量 ivar 内存布局,是放在我们的 io 里面的,并且是 const 不允许修改的,也就是说明,我们的 成员变量布局,在编译阶段就确定了,内存布局已经确定了,在运行时是不可以修改了,这就说明了,为什么运行时不能往类中动态添加成员变量。
    
    

    class_ro_t 的意思是 readonly 的,在编译阶段就已经确定了,不可以修改。

    class_ro_t 是只读的,是再编译的时候,将累的属性,方法,协议和成员变量,添加到我们的 class_ro_t 中,然后运行的时候,会动态的创建 class_rw_t 然后将 class_ro_t 和分类中的属性,协议方法存储到 class_rw_t 中,并进行排序,分类中的存储在数组的前部,原始类信息,存储在数组的后面,class_ro_t 是只能的,在运行时是不可以添加进去的

    class_rw_t 是运行时可以添加的,比如分类中的方法会在运行时,添加到 class_rw_t 的 method_array_t methods; 中去,可以看到,我们的 class_rw_t 中没有成员变量的信息,成员变量的信息是以编译就确定添加到 class_ro_t 中去,并且只读

    接着跟着源码分析下

    
    static Class realizeClassWithoutSwift(Class cls)
    {
        runtimeLock.assertLocked();
    
        const class_ro_t *ro;
        class_rw_t *rw;
        Class supercls;
        Class metacls;
        bool isMeta;
    
        if (!cls) return nil;
        if (cls->isRealized()) return cls;
        assert(cls == remapClass(cls));
    
        // fixme verify class is not in an un-dlopened part of the shared cache?
    
        ro = (const class_ro_t *)cls->data();
        if (ro->flags & RO_FUTURE) {
            // This was a future class. rw data is already allocated.
            rw = cls->data();
            ro = cls->data()->ro;
            cls->changeInfo(RW_REALIZED|RW_REALIZING, RW_FUTURE);
        } else {
            // Normal class. Allocate writeable class data.
            rw = (class_rw_t *)calloc(sizeof(class_rw_t), 1);
            rw->ro = ro;
            rw->flags = RW_REALIZED|RW_REALIZING;
            cls->setData(rw);
        }
    
        isMeta = ro->flags & RO_META;
    
        rw->version = isMeta ? 7 : 0;  // old runtime went up to 6
    
    
        // Choose an index for this class.
        // Sets cls->instancesRequireRawIsa if indexes no more indexes are available
        cls->chooseClassArrayIndex();
    
        if (PrintConnecting) {
            _objc_inform("CLASS: realizing class '%s'%s %p %p #%u %s%s",
                         cls->nameForLogging(), isMeta ? " (meta)" : "", 
                         (void*)cls, ro, cls->classArrayIndex(),
                         cls->isSwiftStable() ? "(swift)" : "",
                         cls->isSwiftLegacy() ? "(pre-stable swift)" : "");
        }
    
        // Realize superclass and metaclass, if they aren't already.
        // This needs to be done after RW_REALIZED is set above, for root classes.
        // This needs to be done after class index is chosen, for root metaclasses.
        // This assumes that none of those classes have Swift contents,
        //   or that Swift's initializers have already been called.
        //   fixme that assumption will be wrong if we add support
        //   for ObjC subclasses of Swift classes.
        supercls = realizeClassWithoutSwift(remapClass(cls->superclass));
        metacls = realizeClassWithoutSwift(remapClass(cls->ISA()));
    
    #if SUPPORT_NONPOINTER_ISA
        // Disable non-pointer isa for some classes and/or platforms.
        // Set instancesRequireRawIsa.
        bool instancesRequireRawIsa = cls->instancesRequireRawIsa();
        bool rawIsaIsInherited = false;
        static bool hackedDispatch = false;
    
        if (DisableNonpointerIsa) {
            // Non-pointer isa disabled by environment or app SDK version
            instancesRequireRawIsa = true;
        }
        else if (!hackedDispatch  &&  !(ro->flags & RO_META)  &&  
                 0 == strcmp(ro->name, "OS_object")) 
        {
            // hack for libdispatch et al - isa also acts as vtable pointer
            hackedDispatch = true;
            instancesRequireRawIsa = true;
        }
        else if (supercls  &&  supercls->superclass  &&  
                 supercls->instancesRequireRawIsa()) 
        {
            // This is also propagated by addSubclass() 
            // but nonpointer isa setup needs it earlier.
            // Special case: instancesRequireRawIsa does not propagate 
            // from root class to root metaclass
            instancesRequireRawIsa = true;
            rawIsaIsInherited = true;
        }
        
        if (instancesRequireRawIsa) {
            cls->setInstancesRequireRawIsa(rawIsaIsInherited);
        }
    // SUPPORT_NONPOINTER_ISA
    #endif
    
        // Update superclass and metaclass in case of remapping
        cls->superclass = supercls;
        cls->initClassIsa(metacls);
    
        // Reconcile instance variable offsets / layout.
        // This may reallocate class_ro_t, updating our ro variable.
        if (supercls  &&  !isMeta) reconcileInstanceVariables(cls, supercls, ro);
    
        // Set fastInstanceSize if it wasn't set already.
        cls->setInstanceSize(ro->instanceSize);
    
        // Copy some flags from ro to rw
        if (ro->flags & RO_HAS_CXX_STRUCTORS) {
            cls->setHasCxxDtor();
            if (! (ro->flags & RO_HAS_CXX_DTOR_ONLY)) {
                cls->setHasCxxCtor();
            }
        }
        
        // Propagate the associated objects forbidden flag from ro or from
        // the superclass.
        if ((ro->flags & RO_FORBIDS_ASSOCIATED_OBJECTS) ||
            (supercls && supercls->forbidsAssociatedObjects()))
        {
            rw->flags |= RW_FORBIDS_ASSOCIATED_OBJECTS;
        }
    
        // Connect this class to its superclass's subclass lists
        if (supercls) {
            addSubclass(supercls, cls);
        } else {
            addRootClass(cls);
        }
    
        // Attach categories
        methodizeClass(cls);
    
        return cls;
    }
    
    

    我们在 alloc 的时候,会调用上面的方法,然后紧接着有个判断

        // This was a future class. rw data is already allocated.
            rw = cls->data();
            ro = cls->data()->ro;
            cls->changeInfo(RW_REALIZED|RW_REALIZING, RW_FUTURE);
        } else {
            // Normal class. Allocate writeable class data.
            rw = (class_rw_t *)calloc(sizeof(class_rw_t), 1);
            rw->ro = ro;
            rw->flags = RW_REALIZED|RW_REALIZING;
            cls->setData(rw);
        }
    
    

    判断我们的rw是否创建过了,如果没有,我们会创建一个rw的结构体,然后将ro赋值给rw中的ro,然后将rw赋值给class,

    
     supercls = realizeClassWithoutSwift(remapClass(cls->superclass));
        metacls = realizeClassWithoutSwift(remapClass(cls->ISA()));
    
    

    然后递归遍历他的父类和元类,同样的方式分配。最后

    
     // Attach categories
        methodizeClass(cls);
    
    
    
    static void methodizeClass(Class cls)
    {
        runtimeLock.assertLocked();
    
        bool isMeta = cls->isMetaClass();
        auto rw = cls->data();
        auto ro = rw->ro;
    
        // Methodizing for the first time
        if (PrintConnecting) {
            _objc_inform("CLASS: methodizing class '%s' %s", 
                         cls->nameForLogging(), isMeta ? "(meta)" : "");
        }
    
        // Install methods and properties that the class implements itself.
        method_list_t *list = ro->baseMethods();
        if (list) {
            prepareMethodLists(cls, &list, 1, YES, isBundleClass(cls));
            rw->methods.attachLists(&list, 1);
        }
    
        property_list_t *proplist = ro->baseProperties;
        if (proplist) {
            rw->properties.attachLists(&proplist, 1);
        }
    
        protocol_list_t *protolist = ro->baseProtocols;
        if (protolist) {
            rw->protocols.attachLists(&protolist, 1);
        }
    
        // Root classes get bonus method implementations if they don't have 
        // them already. These apply before category replacements.
        if (cls->isRootMetaclass()) {
            // root metaclass
            addMethod(cls, SEL_initialize, (IMP)&objc_noop_imp, "", NO);
        }
    
        // Attach categories.
        category_list *cats = unattachedCategoriesForClass(cls, true /*realizing*/);
        attachCategories(cls, cats, false /*don't flush caches*/);
    
        if (PrintConnecting) {
            if (cats) {
                for (uint32_t i = 0; i < cats->count; i++) {
                    _objc_inform("CLASS: attached category %c%s(%s)", 
                                 isMeta ? '+' : '-', 
                                 cls->nameForLogging(), cats->list[i].cat->name);
                }
            }
        }
        
        if (cats) free(cats);
    
    #if DEBUG
        // Debug: sanity-check all SELs; log method list contents
        for (const auto& meth : rw->methods) {
            if (PrintConnecting) {
                _objc_inform("METHOD %c[%s %s]", isMeta ? '+' : '-', 
                             cls->nameForLogging(), sel_getName(meth.name));
            }
            assert(sel_registerName(sel_getName(meth.name)) == meth.name); 
        }
    #endif
    }
    
    

    可以看到

    
     method_list_t *list = ro->baseMethods();
        if (list) {
            prepareMethodLists(cls, &list, 1, YES, isBundleClass(cls));
            rw->methods.attachLists(&list, 1);
        }
    
        property_list_t *proplist = ro->baseProperties;
        if (proplist) {
            rw->properties.attachLists(&proplist, 1);
        }
    
        protocol_list_t *protolist = ro->baseProtocols;
        if (protolist) {
            rw->protocols.attachLists(&protolist, 1);
        }
    
    

    是将ro里面改的方法列表啊,属性列表还有协议列表,添加到rw里面,然后紧接着

    
     // Attach categories.
        category_list *cats = unattachedCategoriesForClass(cls, true /*realizing*/);
        attachCategories(cls, cats, false /*don't flush caches*/);
    
    
    
    
    static void 
    attachCategories(Class cls, category_list *cats, bool flush_caches)
    {
        if (!cats) return;
        if (PrintReplacedMethods) printReplacements(cls, cats);
    
        bool isMeta = cls->isMetaClass();
    
        // fixme rearrange to remove these intermediate allocations
        method_list_t **mlists = (method_list_t **)
            malloc(cats->count * sizeof(*mlists));
        property_list_t **proplists = (property_list_t **)
            malloc(cats->count * sizeof(*proplists));
        protocol_list_t **protolists = (protocol_list_t **)
            malloc(cats->count * sizeof(*protolists));
    
        // Count backwards through cats to get newest categories first
        int mcount = 0;
        int propcount = 0;
        int protocount = 0;
        int i = cats->count;
        bool fromBundle = NO;
        while (i--) {
            auto& entry = cats->list[i];
    
            method_list_t *mlist = entry.cat->methodsForMeta(isMeta);
            if (mlist) {
                mlists[mcount++] = mlist;
                fromBundle |= entry.hi->isBundle();
            }
    
            property_list_t *proplist = 
                entry.cat->propertiesForMeta(isMeta, entry.hi);
            if (proplist) {
                proplists[propcount++] = proplist;
            }
    
            protocol_list_t *protolist = entry.cat->protocols;
            if (protolist) {
                protolists[protocount++] = protolist;
            }
        }
    
        auto rw = cls->data();
    
        prepareMethodLists(cls, mlists, mcount, NO, fromBundle);
        rw->methods.attachLists(mlists, mcount);
        free(mlists);
        if (flush_caches  &&  mcount > 0) flushCaches(cls);
    
        rw->properties.attachLists(proplists, propcount);
        free(proplists);
    
        rw->protocols.attachLists(protolists, protocount);
        free(protolists);
    }
    
    
    
    prepareMethodLists(cls, mlists, mcount, NO, fromBundle);
        rw->methods.attachLists(mlists, mcount);
        free(mlists);
        if (flush_caches  &&  mcount > 0) flushCaches(cls);
    
        rw->properties.attachLists(proplists, propcount);
        free(proplists);
    
        rw->protocols.attachLists(protolists, protocount);
        free(protolists);
    
    

    将分类中的发昂发,属性,协议,添加到rw中

    总结

    
    struct class_ro_t {
        uint32_t flags;
        uint32_t instanceStart;
        uint32_t instanceSize;
    #ifdef __LP64__
        uint32_t reserved;
    #endif
    
        const uint8_t * ivarLayout;
        
        const char * name;
        method_list_t * baseMethodList;
        protocol_list_t * baseProtocols;
        const ivar_list_t * ivars;
    
        const uint8_t * weakIvarLayout;
        property_list_t *baseProperties;
    
    

    最后看一眼我们的 class_ro_t ,有三个属性是不允许我们修改的

    const uint8_t * ivarLayout;
        
        const char * name;
        
            const ivar_list_t * ivars;
    
        
    

    当我们初始化一个类的时候

    1. 在编译的时候已经确定我们的类的原始信息,并将它存储在 class_ro_t 结构体中,并且运行时不能改变
    2. 递归初始化他的父类和元类
    3. 将ro中的方法协议属性等,添加到rw对应的数组中
    4. 将分类中的属性方法协议添加到rw中
    5. 在运行时,不能动态的想类中添加成员变量,还有弱引用成员变量,和修改类名
    6. 因为运行时,我们的rw对ro进行了引用,ro的方法列表协议列表添加到了我们的rw对用的数组中,所以就给我们在运行时对方法等做动态修改提供了可能。

    很多人可能会有疑问,runtime 不是提供了 动态添加成员变量的方法 class_addIvar() ,但是苹果的官方文档已经有明确的说明

    This function may only be called after objc_allocateClassPair and before objc_registerClassPair. Adding an instance variable to an existing class is not supported.
    
    

    必须在alloc 和 register 之间调用,之前说过,程序编译的时候就生成了成员变量布局,程序启动后就没有机会再添加成员变量了,

    因为我们的类实例是需要一块内存空间的,他有isa指针指向,如果我们在运行时允许动态修改成员变量的布局,那么创建出来的类实例就属于无效的了,能够被所以修改,但是属性和方法是我们 objc_class 可以管理的,增删改都不影响我们实例内存布局。

    相关文章

      网友评论

        本文标题:iOS 源码分析 Class 本质,objc_class,cla

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