struct objc_class的结构 类对象和元类内存结构问题

861 阅读2分钟

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。


/// An opaque type that represents an Objective-C class.

typedef struct objc_class *Class;
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;

    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 *` */

我们发现OC2.0 此种结构已经过时了。 c++的结构体和类基本没什么区别

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();

    }

    bool isMetaClass() {

        assert(this);

        assert(isRealized());

        return data()->ro->flags & RO_META;

    }


    // NOT identical to this->ISA when this is a metaclass

    Class getMeta() {

        if (isMetaClass()) return (Class)this;

        else return this->ISA();

    }


    bool isRootClass() {

        return superclass == nil;

    }

    bool isRootMetaclass() {

        return ISA() == (Class)this;

    }

    size_t instanceSize(size_t extraBytes) {
        size_t size = alignedInstanceSize() + extraBytes;
        // CF requires all objects be at least 16 bytes.
        if (size < 16) size = 16;
        return size;
    }
};

struct objc_object {

private:

    isa_t isa;

public:

    // ISA() assumes this is NOT a tagged pointer object

    Class ISA();

    // getIsa() allows this to be a tagged pointer object

    Class getIsa();

    bool isClass();


    // object may have associated objects?

    id retain();

    void release();

    id autorelease();

    // Implementations of retain/release methods

    id rootRetain();

    bool rootRelease();

    id rootAutorelease();

    bool rootTryRetain();

    bool rootReleaseShouldDealloc();

    uintptr_t rootRetainCount();

    void rootDealloc();

};

综合两者:

struct objc_class {

    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();
    }
};
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;

    char *demangledName;

#if SUPPORT_INDEXED_ISA

    uint32_t index;

#endif
};
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;

    method_list_t *baseMethods() const {
        return baseMethodList;
    }
};

简化:struct objc_class的结构

struct objc_class{

    Class isa;\


    Class superclass;\


    cache_t cache; // 方法缓存

    class_data_bits_t bits;// 用于获取具体的类信息

};

bits & FAST_DATA_MASK

获取具体的类信息。


struct class_rw_t{

        uint32_t flags;\


        uint32_t version;\


        const class_ro_t *ro; // 方法列表\


        property_list_t *properties; //属性列表

        const protocol_list_t *protocols; //协议列表

        Class firstSubclass;

        Class nextSiblingClass;

        char *demangleName;\


};

struct class_ro_t {

    uint32_t flags;

    uint32_t instanceStart;

    uint32_t instanceSize; //instance对象占用的内存控件 

#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;

};

image.png

对象的isa指针指向哪里?

instance对象的isa指向class对象

class对象的isa指向meta-class对象

meta-class对象的isa指针指向基类的meta-class对象。

OC的类信息存放在那里?

对象方法,属性,成员变量,协议信息,存放在class

对象中

类方法,存放在meta-class对象中