First of all, let me ask you a question. Do you know much about ISA?

I guess your answer is basically:

The ISA of an instance object points to a class object, and the ISA of a class object points to a metaclass. When the object method is called, the class is found through the instance isa, and finally the implementation of the object method is found to call. When a class method is called, find the meta-class through the class isa, and finally find the implementation of the class method to call. Basic estimate is these! So you’ll learn more about it with this blog introduction today.

Through this blog you will learn:

1. Gain a deeper understanding of ISA, such as why ISA uses a common union, which stores more things than addresses.

2. The last three digits of the ISA memory address are always 0.

3. You will learn about the function of MASK. Many layers have this thing, after understanding this will be a great help for us to read the source code

4. Run & and | how to calculate.

Basic Review:

Objective-c is a highly dynamic programming language, which is quite different from C, C++ and other languages

The dynamism of Objective-C is largely supported by the Runtime API

Runtime API provides the interface is basically C language, source code written by C++ assembly language

Starting with the ARM64 architecture, ISA has been optimized to become a common body (Union) structure, and bitfields are used to store more information

As always, from simple to complex, without more words,lets begin!

If not, take a closer look at the following code, which may be a bit abstract. It’s as easy as chopping vegetables

First let’s look at some code: I created a GDPerson, which inherits NSObject, as shown in the figure

Three BOOL variables

Take a look at the results:

Three attributes will definitely generate three member variables. Now let’s see how much memory this Person object uses. You can use class_getInstanceSize([GDPerson class])

The result is 16 bits (8+3=11 plus memory alignment is 16). We know that a BOOL takes up 1 bit of memory, and a byte has 8 bits, so we have a BOOL that only has 2 values, either 0 or 1, so for a BOOL we only need 1 binary bit to store it. Wow,3 BOO bits L property, which can theoretically be stored in a single byte. A byte is eight bits.

Ok, so let’s try to go in this direction: so instead of writing our own attributes, because writing our own attributes is bound to generate member variables, we implement our own set and get methods

The diagram below:

Does the method I drew above work in theory? Then we can see how to implement, we must first know the arithmetic operation & and | you know?

Ampersand: 1 in both cases, 0 in all other cases

| operation: as long as there is a is 1, the result is 1, the other is 0

Take the following calculation for an example:

Bit operation is the basic computer know point, here not to say! Let’s say I want to know what is the 4-digit number **? Do I just take 1111 & that’s the number,

So let’s say that this ** is 0001 so I’m going to say 0001&1111=0001, so I can pull this out

Ok, now we can write the value,tall is the first,rich is the second,handsome is the third

What does 0b0000 0011 mean tall:YES,rich:YES,handsome:NO

So these are masks, and you can see a lot of these, like the last time this guy got the ISA address it was & # define ISA_MASK 0x0000000ffffffff8ULL

This mask delves into where the object’s ISA pointer points, as described here, right

So how do we get the first digit out? Can we just put 0b 0000 0001 on it, and we can get the first digit out

So here’s how you write it

Looking at the output, it all depends on the initialized value of _tallRichHansome.

Exactly the same as above. There are some optimizations that you can make here, and I’m not going to write it down and just show you what it looks like, but you can write macros like 1<<0, moving digits

Now let’s look at the set method: did I just write it

Let’s take a look at the results:

You can look at other cases

If you have any questions, please point them out. With that said, here comes the main character

The key to the

Then we’re going to introduce something called union, which is the example I gave you above, which isa shared memory, like arm64, before the isa only stored 8 bytes, which is 64 bits, just stored the isa address, and if you think about all 64 bits storing isa, it must be a waste, some of them are useless, so arm64 bits Now, we’ve done a lot of optimizations, but in the 64 bits, not only do we store isa Pointers, but we also store a lot of other things, which we’ll talk about later

Take a look at the following code:

This is our own way of writing, but also imitation source code definition, let’s take a look at the source code:

Objc source

The source code is the same as we defined, are using the Union common body to achieve, so as long as we understand these, the source code definition ISA we can easily understand, more in-depth understanding of isa source code definition.

That’s it for today, too much writing, too much reading and impatient to see, so the next few focus on the next blog to answer. The above four questions will be solved by then (blog answers tomorrow)

I’m going to talk about Runtime in the next blog post, and I’m going to talk about Runtime in other ways

If you find my writing helpful, please follow me and I will continue to update 😄