In the last chapter of Principle of Classes (PART 1), we explored the attributes of classes and the storage location of object methods, but we did not find the storage location of member variables and class methods in the exploration process, so in this article, we will explore the storage location of member variables and class methods.

Member variables

1. We failed to find the location of the member and class method after the previous analysis, so we continued to analyze the class_rw_t structure, and finally found the following function:

2, so we go to use the source code + LLDB to tune to see, this inside the store is what?

The ro() callback in the class_rw_t constructor returns a class_ro_t constructor. The ro() callback in the class_rw_t constructor returns a class_ro_t constructor. The ro() callback returns a class_ro_t constructor.

struct class_ro_t {
    uint32_t flags;
    uint32_t instanceStart;
    uint32_t instanceSize;
#ifdef __LP64__
    uint32_t reserved;
#endif

    union {
        const uint8_t * ivarLayout;
        Class nonMetaclass;
    };

    void *baseMethodList;
    protocol_list_t * baseProtocols;
    const ivar_list_t * ivars;

    const uint8_t * weakIvarLayout;
    property_list_t *baseProperties;
}
Copy the code

These are the basic members we need to get the list of variables, methods, protocols, properties, and so on we want.

4. Here we find the locations where members are stored, as well as other structures of some classes. And then we see a base list, by the way, okay? The LLDB output is as follows:

From the above, we can see that there are only object methods, there are no class methods, and then we continue to explore where class methods are stored.

Class method

1, at present, we have found the attributes of a class object, member variables, methods of storage location, in this section, we explore the storage location of class methods, we looked for it in the class all relationship with the method of function, did not find the class method storage location, so whether we have such a speculation, whether it is in the metaclass? Because so far, I haven’t thought of any use for metaclasses. So let’s take a curious look at bits in the metaclass.

What does methods() in bits contain? But how do we access metaclasses? We can use this mask to get the metaclass, and look at the following result:

3. From the figure above, we can see that class methods are indeed stored in metaclasses. Instead of taking the kgPerson. class address, we get the metaclass from isa and offset the address. Everything else is the same as reading the class.

4. After two articles’ exploration, we have roughly analyzed the attributes, member variables, object methods (instance methods) and storage locations of class methods.

conclusion

Through the analysis of two articles, we explored the storage locations of class attributes, member variables, class methods and object methods, and roughly understood the memory structure of some classes.