美文网首页
OC高级编程 ---Block实现原理

OC高级编程 ---Block实现原理

作者: 有一种再见叫青春 | 来源:发表于2016-11-16 09:10 被阅读41次

    Block的实质

    <pre>`
    int main(int argc, const char * argv[]) {

    void (^block)(void) = ^{
        (printf("Block\n"));
              };
        block();
    return 0;
    

    }`</pre>

    通过LLVM转化后的主要源码为

    <pre>`

    struct __main_block_impl_0 {
    struct __block_impl impl;
    struct __main_block_desc_0* Desc;
    __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int flags=0) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
    }
    };
    static void __main_block_func_0(struct __main_block_impl_0 *__cself) {

        (printf("Block\n"));
    
                             }
    

    static struct __main_block_desc_0 {
    size_t reserved;
    size_t Block_size;
    } __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0)};
    int main(int argc, const char * argv[]) {

    void (*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA));
        ((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);
    
    return 0;
    

    }

    `</pre>

    <code>static void __main_block_func_0(struct __main_block_impl_0 *__cself)</code>

    scself相当于C++实例方法中指向实例变量this,或者OC中指向自身变量的self,_cself为指向Block的变量.

    cself参数的声明

    __cself是struct __main_block_impl_0 结构体的指针,该结构体声明如下:

    <pre>`
    struct __main_block_impl_0 {

    struct __block_impl impl;
    struct __main_block_desc_0 *Desc;
    }
    `</pre>

    由于转换后的源代码写入了构造函数,所以看起来稍显复杂,如果去除构造函数 .第一个成员变量是impl 其结构体如下:

    <pre>struct __block_impl { void *isa; int Flags; int Reserved; void *FuncPtr; };</pre>
    标志,今后升级所需的区域 函数指针

    第二个成员变量是Desc指针

    <pre>static struct __main_block_desc_0 { size_t reserved; size_t Block_size;</pre>
    其构造为今后升级所需的区域Block的大小

    __mian_block_impl_0结构体的构造函数

    <pre>__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int flags=0) { impl.isa = &_NSConcreteStackBlock; impl.Flags = flags; impl.FuncPtr = fp; Desc = desc; }</pre>

    以上就是初始化__mian_block_impl_0结构体成员的源码._NSConcreteStackBlock用于初始化_block_impl_0结构体的isa成员

    <pre>void (*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA));</pre>

    因为转化较多,去掉转换部分,如下:
    <pre>struct __mian_block_impl_0 tmp = __mian_block_impl_)(__mian_block_func_0, &__mian_block_desc_o_DATA); struct __mian_block_impl_0 * blk = &tmp;</pre>
    该源函数将__mian_block_impl_0结构体类型的自动变量,即栈上生成的__mian_block_impl_0机构提实例的指针,赋值给__mian_block_impl_0结构体指针类型的变量blk.一下为这部分代码对应的最初源代码.
    <code>void (^blk)(void) = ^{printf("Block\n");};</code>

    将block语法生成的Block赋值给Block类型变量blk.它等同于将__mian_block_impl_0结构体实例的指针赋值给blk

    __mian_block_impl_0的构造参数为:
    <pre>__mian_block_impl_0(main_block_func_0, &__main_block_desc_0_DATA).</pre>

    第一个参数是Block语法转换的C语言函数指针.第二个参数是作为静态全局变量初始化的__mian_block_desc_0结构体指针.下面为__mian_block_desc_0初始化的代码:

    相关文章

      网友评论

          本文标题:OC高级编程 ---Block实现原理

          本文链接:https://www.haomeiwen.com/subject/cscxpttx.html