review
In the previous blog, through theblock
Trace the source,Assembly trace mode
, source code analysis, on the underlying structure andblock
Have a certain understanding of the attributes and methods of, so this blog will continue toblock
The bottom layer of.

Block of IOS bottom layer exploration (I) — getting to know block (how many blocks do you know?)
Block (II) – how to solve the problem of block circular reference?
IOS bottom layer exploration block (III) — the essence of block
IOS bottom layer exploration block (IV) — block exploration and source code analysis
1. block bottom layer exploration
block
The structure and signature of are analyzed, butblock
The most difficult point is how to capture variables.
CPP view underlying structure
Take another look at the underlying structure, as shown in the following code:
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSObject *objc = [NSObject alloc];
void (^jp_block)(void) = ^{
NSLog(@"zjpreno: %@ ",objc);
};
jp_block();
}
@end
adoptxcrun -sdk iphonesimulator clang -S -rewrite-objc -fobjc-arc -fobjc-runtime=ios-14.2 ViewController.m
Command generation.cpp
file

Generated by.cpp
File visible
-
__ViewController__viewDidLoad_block_copy_0
Is correspondingBlock_descriptor_2
Insidecopy
-
__ViewController__viewDidLoad_block_dispose_0
Is correspondingBlock_descriptor_2
Insidedispose
- stay
__ViewController__viewDidLoad_block_copy_0
There are so many methods_Block_object_assign
This thing,__ViewController__viewDidLoad_block_dispose_0
There are too many in it_Block_object_dispose
, which makes people puzzled. Don’t worry. Please continue to read.
Substructure analysis

thiscpp
The structure of the corresponding source codeBlock_descriptor
Information.
-
reserved
andsize
correspondingBlock_descriptor_1
Two properties of. -
void (*copy)
andvoid (*dispose)
correspondingBlock_descriptor_2
Two methods of. - stay
copy
Method, the_Block_object_assign
, which is the method of capturing external variables. - stay
dispose
Method, the_Block_object_dispose
, which is a release method.
2. block source code analysis
Search in source code_Block_object_assign
, find the following annotation information:

WhenBlock
When copied to the heap, aBlock
You can quote four different things that need help.
- base on
C++
Stack objects - right
Objective-C
References to objects - Other blocks
-
__block
variable
In these cases, auxiliary functions are synthesized by the compiler for
Block_copy
andBlock_release
, calledcopy
andDisposal auxiliary function
. Replication assistant is based onC++
Stack objects emit pairs ofC++ const
Copy calls to constructors and run-time support functions for the rest of the calls_Block_object_assign
。dispose helper
callC++
Destructors forCase 1
, call_Block_object_dispose
For the rest of the cases.
- Runtime support functions used by the compiler when generating the copy / dispose assistant
-
_Block_object_assign()
and_Block_object_dispose()
Value of parameter

_Block_object_assign
and_Block_object_dispose
offlags
Parameter settings are:
-
BLOCK_FIELD_IS_OBJECT (3)
, forObjective-C Object
Situation -
BLOCK_FIELD_IS_BLOCK (7)
, for anotherblock
Situation -
BLOCK_FIELD_IS_BYREF (8)
, for__block
Variable.
If__block
Variable is marked asweak
, the compiler is alsoBLOCK_FIELD_IS_WEAK (16)
in
therefore
Block copy
/dispose helper
Should only generate3
、7
、8
and24
Four flag values.
The above is the comments of the source code. Now let’s verify:

fromcpp
We can also see that forOC
Object, yesBLOCK_FIELD_IS_OBJECT (3)
Well, let’s add it now__block
, see if it will becomeBLOCK_FIELD_IS_BYREF (8)
And?

See? Yes__block
And then it becameBLOCK_FIELD_IS_BYREF (8)
Who else, looking up at the ceiling at 45 degrees, I have no place to put my charm!

_Block_object_assign
_Block_object_assign

- If the holding variable is
BLOCK_FIELD_IS_OBJECT
Type, i.e. no__block
Embellishment,dest
Pointer toobjec
, reference count plus1
, the original object address is givenblock
The target object inside, so that they all point to the same address space - If it is
BLOCK_FIELD_IS_BLOCK
Type, i.eblock
Is capturedblock
Then proceed_Block_copy
Operation, handleobjec
Copy the contents of to your own, that is, to the heap - If it is
BLOCK_FIELD_IS_BYREF
, i.e__block
Decorated, the_Block_byref_copy
_Block_byref_copy
- _Block_byref_copy

-
Encapsulate external objects into structures
Block_byref *src
-
If it is a normal object with reference count, the reference count is processed, and the
malloc
, generate aBlock_byref *copy
, if not, thememmove
-
set up
forwarding
, guaranteeblock
Both internal and external points to the same objectforwarding
Block_byref_2
- Block_byref_2
Block_byref_2
Here it is right
block
The captured variables are processed, and thebyref_keep
Save its lifecycle.Block_byref
Design idea andBlock_layout
indescriptor
Similar process, throughbyref->flag
The identification code determines the corresponding attribute to determineBlock_byref_2
Whether it exists, as shown in the following figure:Block_byrefIf our
block
Captured usage__block
Decorated external variables, incpp
In the document,Block_byref
Two methods will be generated in the structure by default, i.e. correspondingBlock_byref_2
ofkeep
Methods anddestory
Method, the verification is as follows:CPP verification block_ Byref_ two
stay
cpp
The implementation of these two functions in the file is as follows:

- _Block_object_assign
_Block_object_assign((char*)dst + 40, *(void * *) ((char*)src + 40), 131);
(char*)dst + 40)
amount toobjec
, pointer translation

How did the top 40 come from? As follows:

__Block_byref_id_object_copy_131
Method calls_Block_object_assign
Functions, rightBlock_byref
Objects in the structureBLOCK_FIELD_IS_OBJECT
Process processing.
_Block_object_dispose
about_Block_object_dispose
Method, that is, release process, is similar.

Yeahblock
Type judgment, and then call_Block_release(object)
Follow the release process.

- flags parameter
3. summary
block
Three tier copy of
- If it is
__Block
Decorated variables willblock
conductcopy
Operation, copy from stack area to heap area -
block
Capture variables, forBlock_byref
Copy of structure -
Block_byref
The incomingobjec
Copy, doneblock
Three tier copy of
More constantly updated
Just like it
If you feel you have something to gain, you can have a wave, collect + pay attention, comment + forward, so that you won’t find me next time
Welcome to exchange messages, criticize and correct, learn from each other and improve yourself