IOS bottom layer exploration: block (V) — block source code analysis (\u what does the bottom layer do?)

Time:2022-5-27

review

In the previous blog, through theblockTrace the source,Assembly trace mode, source code analysis, on the underlying structure andblockHave a certain understanding of the attributes and methods of, so this blog will continue toblockThe bottom layer of.

IOS bottom layer exploration: block (V) -- block source code analysis (\u what does the bottom layer do?)

Block exploration analysis

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

blockThe structure and signature of are analyzed, butblockThe 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.mCommand generation.cppfile

IOS bottom layer exploration: block (V) -- block source code analysis (\u what does the bottom layer do?)

ViewController.cpp

Generated by.cppFile visible

  • __ViewController__viewDidLoad_block_copy_0Is correspondingBlock_descriptor_2Insidecopy
  • __ViewController__viewDidLoad_block_dispose_0Is correspondingBlock_descriptor_2Insidedispose
  • stay__ViewController__viewDidLoad_block_copy_0There are so many methods_Block_object_assignThis thing,__ViewController__viewDidLoad_block_dispose_0There are too many in it_Block_object_dispose, which makes people puzzled. Don’t worry. Please continue to read.

Substructure analysis

IOS bottom layer exploration: block (V) -- block source code analysis (\u what does the bottom layer do?)

CPP corresponds to the block in the source code_ Descriptor information

thiscppThe structure of the corresponding source codeBlock_descriptorInformation.

  • reservedandsizecorrespondingBlock_descriptor_1Two properties of.
  • void (*copy)andvoid (*dispose)correspondingBlock_descriptor_2Two methods of.
  • staycopyMethod, the_Block_object_assign, which is the method of capturing external variables.
  • staydisposeMethod, 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:

IOS bottom layer exploration: block (V) -- block source code analysis (\u what does the bottom layer do?)

_The Block_ object_ Assign comment information

WhenBlockWhen copied to the heap, aBlockYou can quote four different things that need help.

  1. base onC++Stack objects
  2. rightObjective-CReferences to objects
  3. Other blocks
  4. __blockvariable

In these cases, auxiliary functions are synthesized by the compiler forBlock_copyandBlock_release, calledcopyandDisposal auxiliary function. Replication assistant is based onC++Stack objects emit pairs ofC++ constCopy calls to constructors and run-time support functions for the rest of the calls_Block_object_assigndispose helpercallC++Destructors forCase 1, call_Block_object_disposeFor 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
IOS bottom layer exploration: block (V) -- block source code analysis (\u what does the bottom layer do?)

parameters

_Block_object_assignand_Block_object_disposeofflagsParameter settings are:

  • BLOCK_FIELD_IS_OBJECT (3), forObjective-C ObjectSituation
  • BLOCK_FIELD_IS_BLOCK (7), for anotherblockSituation
  • BLOCK_FIELD_IS_BYREF (8), for__blockVariable.

If__blockVariable is marked asweak, the compiler is alsoBLOCK_FIELD_IS_WEAK (16)in

thereforeBlock copy/dispose helperShould only generate378and24Four flag values.

The above is the comments of the source code. Now let’s verify:

IOS bottom layer exploration: block (V) -- block source code analysis (\u what does the bottom layer do?)

validate

fromcppWe can also see that forOCObject, yesBLOCK_FIELD_IS_OBJECT (3)Well, let’s add it now__block, see if it will becomeBLOCK_FIELD_IS_BYREF (8)And?

IOS bottom layer exploration: block (V) -- block source code analysis (\u what does the bottom layer do?)

__The Block validation

See? Yes__blockAnd 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!

IOS bottom layer exploration: block (V) -- block source code analysis (\u what does the bottom layer do?)

666

_Block_object_assign

  • _Block_object_assign
IOS bottom layer exploration: block (V) -- block source code analysis (\u what does the bottom layer do?)

comparative analysis
  • If the holding variable isBLOCK_FIELD_IS_OBJECTType, i.e. no__blockEmbellishment,destPointer toobjec, reference count plus1, the original object address is givenblockThe target object inside, so that they all point to the same address space
  • If it isBLOCK_FIELD_IS_BLOCKType, i.eblockIs capturedblockThen proceed_Block_copyOperation, handleobjecCopy the contents of to your own, that is, to the heap
  • If it isBLOCK_FIELD_IS_BYREF, i.e__blockDecorated, the_Block_byref_copy

_Block_byref_copy

  • _Block_byref_copy
IOS bottom layer exploration: block (V) -- block source code analysis (\u what does the bottom layer do?)

_Block_byref_copy
  • Encapsulate external objects into structuresBlock_byref *src

  • If it is a normal object with reference count, the reference count is processed, and themalloc, generate aBlock_byref *copy, if not, thememmove

  • set upforwarding, guaranteeblockBoth internal and external points to the same object

    IOS bottom layer exploration: block (V) -- block source code analysis (\u what does the bottom layer do?)

    forwarding

Block_byref_2

  • Block_byref_2
    IOS bottom layer exploration: block (V) -- block source code analysis (\u what does the bottom layer do?)

    Block_byref_2

    Here it is rightblockThe captured variables are processed, and thebyref_keepSave its lifecycle.Block_byrefDesign idea andBlock_layoutindescriptorSimilar process, throughbyref->flagThe identification code determines the corresponding attribute to determineBlock_byref_2Whether it exists, as shown in the following figure:

    IOS bottom layer exploration: block (V) -- block source code analysis (\u what does the bottom layer do?)

    Block_byref

    If ourblockCaptured usage__blockDecorated external variables, incppIn the document,Block_byrefTwo methods will be generated in the structure by default, i.e. correspondingBlock_byref_2ofkeepMethods anddestoryMethod, the verification is as follows:

    IOS bottom layer exploration: block (V) -- block source code analysis (\u what does the bottom layer do?)

    CPP verification block_ Byref_ two

staycppThe implementation of these two functions in the file is as follows:

IOS bottom layer exploration: block (V) -- block source code analysis (\u what does the bottom layer do?)

Keep method and destory implementation
  • _Block_object_assign
 _Block_object_assign((char*)dst + 40, *(void * *) ((char*)src + 40), 131);

(char*)dst + 40)amount toobjec, pointer translation

IOS bottom layer exploration: block (V) -- block source code analysis (\u what does the bottom layer do?)

contrast

How did the top 40 come from? As follows:

IOS bottom layer exploration: block (V) -- block source code analysis (\u what does the bottom layer do?)

Memory size

__Block_byref_id_object_copy_131Method calls_Block_object_assignFunctions, rightBlock_byrefObjects in the structureBLOCK_FIELD_IS_OBJECTProcess processing.

_Block_object_dispose

about_Block_object_disposeMethod, that is, release process, is similar.

IOS bottom layer exploration: block (V) -- block source code analysis (\u what does the bottom layer do?)

_Block_object_dispose

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

IOS bottom layer exploration: block (V) -- block source code analysis (\u what does the bottom layer do?)

_Block_release
  • flags parameter

flags parameter

3. summary

blockThree tier copy of

  • If it is__BlockDecorated variables willblockconductcopyOperation, copy from stack area to heap area
  • blockCapture variables, forBlock_byrefCopy of structure
  • Block_byrefThe incomingobjecCopy, doneblockThree 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

Recommended Today

A front-end developer's Vim is the same as an IDE

Here is my new configurationjaywcjlove/vim-webI've been grinding it, and it's basically ready to use. Take it out and cheat the star Install The latest version of Vim 7.4+ uses (brew install macvim) installation, vim version updatebrew install macvim –override-system-vim View configuration locations # Enter vim and enter the following characters :echo $MYVIMRC download vim-web Download […]