Small valley bottom exploration collection

  • Let’s explore a wave todayclass, this thing everyone is familiar with, but today we go inside to see what his structure is like!
  • We will passIsa towards,Class inheritanceandMemory migrationThree aspects of in-depth explorationClass structure
  • We are working with the current source codeobjc4-781

1. The trend of ISA

1.1. Proof of the existence of ISA

    1. forisaI’m sure you’re familiar with this, but we’re going to prove today,isaThe existence of!
    1. Let’s define a
@interface XGClass : NSObject
@end
Copy the code
    1. When we compile the file into C++ using clang, we see something like this:
#ifndef _REWRITER_typedef_XGClass
#define _REWRITER_typedef_XGClass
typedef struct objc_object XGClass;// Brothers!! So in low-level C++, class objects are created for templates through the objc_object structure!!
typedef struct {} _objc_exc_XGClass;
#endif


struct XGClass_IMPL {
     struct NSObject_IMPL NSObject_IVARS;
};
Copy the code
    1. We can check the source code:
typedef struct objc_class *Class;/ / class information

struct objc_object {
    Class _Nonnull isa  OBJC_ISA_AVAILABILITY;
};
Copy the code
    1. As we can see, all class objects have itisa

1.2. Analyze the direction of ISA

  • The exploration may involve the contents of a metaclass, which is automatically created by the compiler and belongs to a class! , don’t panic. We explore it step by step.

  • 1. Everyone is familiar with ISA. We can get this kind of information through ISA

  • 2. Let’s look at the memory distribution of the class

  • 3. We found that the class does store something. The first address is stored orisaAnd then checkisaOf what

  • Discovery is also to saveXGClassHere it isThe metaclass!!!!!
  • We continue to explore and studyisaDoes he finally point toThe metaclass? Or is he not finished?

  • 6, we find pointingThe metaclassAfter that,isaThe class information is the same!! So we draw a conclusion graph! As follows:

2. Class inheritance

  • I believe you are no stranger to inheritance! Inheritance is simply (who is whose son? Who is whose father 😆)

  • I won’t say more about this! (After all, I think my blogging buddies are smart in addition to being handsome.)

  • I stole a wave chart (on the surface: steal chart makes people happy!! Inner thought: Why can’t anyone draw better than me?)

  • throughIsa pointing diagramandInheritance relationshipsFigure, we can synthesize a fried chicken classic 🐍 skin go bitmap (big name:The flow chart of the isa)

  • Ok, the inheritance relationship should be known, so let’s move on!!

3. Memory offset

I didn’t want to write about memory offset in my blog, but it feels like everyone is so smart, what are they using me to prove? Ah, but I was born ignorant, when to explain a wave of their own

  • Let’s start with examples, and then we’ll see through the examplesMemory migration

3.1. Ordinary Pointers

int a = 10;
int b = 10;
NSLog(@"a's address is: %p A's value is: %d",&a,a);
NSLog(@" the address of b is: %p the value of b is: %d",&b,b);
Copy the code

Print result:

  • The a and B addresses differ by 4 bytes. This depends on the type (int) of a and B, and if a and B have the same address, they both refer to the constant 10.

  • This is not stolen!! 😆) :

3.2. Object Pointers

  • This has always been used (though probably as a rule, 😆)
XGStudent *student1 = [XGStudent alloc];
XGStudent *student2 = [XGStudent alloc];
    
NSLog(@"%@ --- %p",student1,&student1);
NSLog(@"%@ --- %p",student2,&student2);
Copy the code
  • Print result:

  • Student1 and student2 point to the memory address of [XGStudent alloc]!

  • Student1 = student1; student1 = student1; student1 = student1

  • To figure (to use my talent as a painter again) :

3.3. Array Pointers

  • This is a little bit easier to understand, after allThe C languageIs defined in the
    // Declare an array
    int a[5] = {0.1.2.3.4};
    // A pointer to an integer
    int *p;
    // point to the first address of array A
    p = a;
    
    NSLog(@"p pointer to element: %d, first element of array A: %d",*p,a[0]);
    NSLog(@"p pointer offset by one bit: %d, second element of array A: %d",*(p+1),a[1]);
Copy the code
  • Output result:

  • Let me draw a picture to make it a little bit clearer

This is the legendary memory offset

4. Explore the class

  • OK, I’m doneIsa pointing,Class inheritance relationshipsandMemory migrationFinally, we can get down to business!

4.1. Analyze the class structure

  • According to the above we can find the structure of this class, I directly paste out here!
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(a) const {
        return bits.data();
    }
    void setData(class_rw_t *newData) {
        bits.setData(newData);
    }

// The following code is too long, irrelevant, omit a wave.
}
Copy the code

In fact, when you first see such a large string of code, it’s confusing. Very afflictive, the most horrible is what annotation have no! My English is not good either

  • At first glance, we don’t know what this is, and then we simply understand!

  • The first one is ISA.

  • The second one is Class

  • Third cache_t, don’t know what this is (it doesn’t matter, exploration is a cognitive process, no one knows at first)

  • The fourth one class_data_bits_t, I don’t know. But t the following set, get method how to operate him. Ah, this time, I feel, there is something inside!! (Let’s look at him.)

We can use memory offset. Find this thing!

4.4.1. Explore cache_t

  • OK, let’s look for: bits

  • Isa = 8 bytes, class = 8 bytes, cache = 8 bytes

  • Should becacheIs a structure pointer not a structure pointer. If you don’t understand, check out my other blog post:Memory alignment

4.2. To obtain the data

  • Now that we know how it offsets, we can lock the bits by offsetting 32 bytes and get it!!

  • Brothers watch me operate!!

  • Some fraternities look confused (what’s going on?). , I am very considerate to draw a flow chart 😆

  • You can go throughdataYou get a lot of information, you can click inclass_rw_tTake a look. Of course, there are a lot of things you can’t understand. That’s okay. Let’s just pick something we can understand:

You can explore this wave if you’re interested

OK! Conclusion! After all, this blog is a little long, write too many words, I am afraid of everyone fidgety, I hope to help you!! (See I have bitter and tired of the case, conditional can point a praise, ha ha 😆)