闻道丶iOS(大杂烩)

浅谈Block原理

2016-07-15  本文已影响483人  西门吹雪123

摘要
block是2010年WWDC苹果为Objective-C提供的一个新特性,它为我们开发提供了便利,比如GCD就大量使用了block,用来往执行队列中添加执行。那么block到底是什么东西呢。其实它就是一个闭包,一个引用自动变量的函数。很多语言也实现自己的闭包,比如C#的lamda表达式。这篇文章将从分析源码的角度来分析下block到底是什么鬼。
最简单的block,不持有变量
我们先新建一个源文件:block.c 代码如下

include <stdio.h>int main(){ void (^blk)(void) = ^(){printf("This is a block.");}; blk(); return 0;}

我们使用clang(LLVM编译器,和GCC类似),通过命令clang -rewrite-objc block.c
,解析block.c这样我们就会得到对应的cpp文件block.cpp。去除一些影响我们阅读的代码。如下:
struct __block_impl { void *isa; int Flags; int Reserved; void FuncPtr;};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("This is a block.");}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(){ void (blk)(void) = (void ()())&__main_block_impl_0((void )__main_block_func_0 ,&__main_block_desc_0_DATA); ((void ()(__block_impl *))((__block_impl *)blk)->FuncPtr)((__block_impl *)blk); return 0;}

下面我们来分析下源码,看看我们定义的block到底是个什么东西。先看下main()函数,我们定义了block
void (^blk)(void) = ^(){printf("This is a block.");};

被转化成了
void (blk)(void) = (void ()())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA);

去除影响阅读的强制转换代码后
void (*blk)(void) = &__main_block_impl_0(__main_block_func_0, &__main_block_desc_0_DATA);

这样就清晰了。我们写的block被转化成了指向__main_block_impl_0
结构体的指针。构造函数的参数我们先不管,慢慢一步步分析首先,我们来看下第一个struct
struct __block_impl { void *isa; int Flags; int Reserved; void *FuncPtr;};

isa指针,如果我们对runtime了解的话,就明白isa指向Class的指针。
Flags,当block被copy时,应该执行的操作
Reserved为保留字段
FuncPtr指针,指向block内的函数实现

__block_impl
保存block的类型isa(如&_NSConcreteStackBlock),标识(当block发生copy时,会用到),block的方法。方法实现如下
static void __main_block_func_0(struct __main_block_impl_0 *__cself) { printf("This is a block.");}

下面我们再看一个结构体
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)};

reserved为保留字段默认为0
Block_size为sizeof(struct __main_block_impl_0)
,用来表示block所占内存大小。因为没有持有变量,block大小为impl的大小加上Desc指针大小
__main_block_desc_0_DATA
为__main_block_desc_0的一个结构体实例这个结构体,用来描述block的大小等信息。如果持有可修改的捕获变量时(即加__block
),会增加两个函数(copy和dispose),我们后面会分析

再看最重要的一个结构体__main_block_impl_0

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; }};

__main_block_impl_0
里面有两个变量struct __block_impl impl
和struct __main_block_desc_0

__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;}

结构体构造函数用来初始化变量__main_block_impl_0
和__main_block_desc_0
注:clang转换的代码和真实运行时有区别。应该为impl.isa = &_NSConcreteGlobalBlock

我们再来看下最开始的
void (*blk)(void) = &__main_block_impl_0(__main_block_func_0, &__main_block_desc_0_DATA);

我们可以看到,block其实就是指向__main_block_impl_0
的结构体指针,这个结构体包含两个__block_impl
和__main_block_desc_0
两个结构体,和一个方法。通过上面的分析,是不是很已经清晰了最后,main函数里面的
((void (*)(__block_impl *))((__block_impl *)blk)->FuncPtr)((__block_impl *)blk);

同样,去除转化代码,上面的代码就可以转化为
blk->FuncPtr(blk);

执行block函数
这样我们就完成了,对简单block实现的分析。是不是很简单
持有变量的block
我们知道block可以持有变量,现在我们实现一个持有变量的block。修改下原来的block.c源文件

include <stdio.h>int main(){ int i = 4; void (^blk)(void) = ^(){printf("i = %d", i);}; i++; blk(); return 0;}

同样的,使用clang命令转化下上述代码
struct __block_impl { void *isa; int Flags; int Reserved; void FuncPtr;};struct __main_block_impl_0 { struct __block_impl impl; struct __main_block_desc_0 Desc; int i; __main_block_impl_0(void *fp, struct __main_block_desc_0 desc, int _i, int flags=0) : i(_i) { impl.isa = &_NSConcreteStackBlock; impl.Flags = flags; impl.FuncPtr = fp; Desc = desc; }};static void __main_block_func_0(struct __main_block_impl_0 __cself) { int i = __cself->i; // bound by copy printf("i=%d", i);}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 i = 4; void (blk)(void) = (void ()())&__main_block_impl_0((void )__main_block_func_0, &__main_block_desc_0_DATA, i); ((void ()(__block_impl *))((__block_impl *)blk)->FuncPtr)((__block_impl *)blk); i++; return 0;}

我们只看下在持有变量时,block转化,有哪些不同
struct __main_block_impl_0 { struct __block_impl impl; struct __main_block_desc_0* Desc; int i; /看这里~看这里~/ __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int _i, int flags=0) : i(_i) { impl.isa = &_NSConcreteStackBlock; impl.Flags = flags; impl.FuncPtr = fp; Desc = desc; }};

__main_block_impl_0
结构体多了一个变量i。这个变量用来保存main函数的变量i。
static void __main_block_func_0(struct __main_block_impl_0 *__cself) { int i = __cself->i; // bound by copy printf("i=%d", i);}

在执行block时,取出的i为__main_block_impl_0保存的值,这两个变量不是同一个。这就是为什么我们执行了i++操作,再执行block,i的值仍然不变的原因
可修改持有变量的block
为了修改持有变量,我们在变量前面加上__block
,修改后的block.c如下

include <stdio.h>int main(){ __block int i = 4; void (^blk)(void) = ^(){printf("i = %d", i);}; i++; blk(); return 0;}

转化后的代码如下
struct __block_impl { void isa; int Flags; int Reserved; void FuncPtr;};struct __Block_byref_i_0 { void __isa; __Block_byref_i_0 __forwarding; int __flags; int __size; int i;};struct __main_block_impl_0 { struct __block_impl impl; struct __main_block_desc_0 Desc; __Block_byref_i_0 i; // by ref __main_block_impl_0(void fp, struct __main_block_desc_0 desc, __Block_byref_i_0 _i, int flags=0) : i(_i->__forwarding) { impl.isa = &_NSConcreteStackBlock; impl.Flags = flags; impl.FuncPtr = fp; Desc = desc; }};static void __main_block_func_0(struct __main_block_impl_0 __cself) { __Block_byref_i_0 i = __cself->i; // bound by ref (i->__forwarding->i)++; printf("i=%d", (i->__forwarding->i));}static void __main_block_copy_0(struct __main_block_impl_0dst, struct __main_block_impl_0src) { _Block_object_assign((void)&dst->i, (void)src->i, 8/BLOCK_FIELD_IS_BYREF/);}static void __main_block_dispose_0(struct __main_block_impl_0src) { _Block_object_dispose((void)src->i, 8/BLOCK_FIELD_IS_BYREF/);}static struct __main_block_desc_0 { size_t reserved; size_t Block_size; void (copy)(struct __main_block_impl_0, struct __main_block_impl_0); void (dispose)(struct __main_block_impl_0);} __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0), __main_block_copy_0, __main_block_dispose_0};int main(){ attribute((blocks(byref))) __Block_byref_i_0 i = {(void)0,(__Block_byref_i_0 )&i, 0, sizeof(__Block_byref_i_0), 4}; void (blk)(void) = (void ()())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, (__Block_byref_i_0 )&i, 570425344); ((void ()(__block_impl *))((__block_impl *)blk)->FuncPtr)((__block_impl *)blk); return 0;}

我们发现当我们想要修改持有变量时,转化后的代码有所增加。当我们在变量前面加上__block
时,就会生成一个结构体,来保存变量值。新增了结构体__Block_byref_i_0
,实现如下
struct __Block_byref_i_0 { void *__isa; __Block_byref_i_0 *__forwarding; int __flags; int __size; int i;};

__isa指向变量Class
____forwarding,指向自己的指针,当从栈copy到堆时,指向堆上的block
__flags,当block被copy时,标识被捕获的对象,该执行的操作
__size,结构体大小
i,持有的变量

看下转换后的main函数
attribute((blocks(byref))) __Block_byref_i_0 i = {(void*)0,(__Block_byref_i_0 *)&i, 0, sizeof(__Block_byref_i_0), 4};

Block_byref_i_0 i = {(void)0,&i, 0, sizeof(*Block_byref_i_0), 4};int i = 4
被转化成上述代码。它被转化成结构体__Block_byref_i_0
。__Block_byref_i_0
持有变量i。
i++;blk();

也转化成对__Block_byref_i_0
中的变量i进行++运算
static void __main_block_func_0(struct __main_block_impl_0 *__cself) { __Block_byref_i_0 *i = __cself->i; // bound by ref (i->__forwarding->i)++; printf("i=%d", (i->__forwarding->i));}

这样便达到对i值的修改
Block_copy(...)的实现
根据Block.h上显示,Block_copy(...)被定义如下:

define Block_copy(...) ((__typeof(VA_ARGS))_Block_copy((const void *)(VA_ARGS)))

_Block_copy
被声明在runtime.c中,对应实现:
void *_Block_copy(const void *arg) { return _Block_copy_internal(arg, WANTS_ONE);}

该方法调用了
/* Copy, or bump refcount, of a block. If really copying, call the copy helper if present. */static void *_Block_copy_internal(const void *arg, const int flags) { struct Block_layout *aBlock; ... if (aBlock->flags & BLOCK_NEEDS_FREE) { // latches on high latching_incr_int(&aBlock->flags); return aBlock; } else if (aBlock->flags & BLOCK_IS_GLOBAL) { return aBlock; } // Its a stack block. Make a copy. struct Block_layout *result = malloc(aBlock->descriptor->size); if (!result) return (void )0; memmove(result, aBlock, aBlock->descriptor->size); // bitcopy first // reset refcount result->flags &= ~(BLOCK_REFCOUNT_MASK); // XXX not needed result->flags |= BLOCK_NEEDS_FREE | 1; result->isa = _NSConcreteMallocBlock; if (result->flags & BLOCK_HAS_COPY_DISPOSE) { //printf("calling block copy helper %p(%p, %p)...\n", aBlock->descriptor->copy, result, aBlock); (aBlock->descriptor->copy)(result, aBlock); // do fixup return result; }}

当原始block在堆上时,引用计数+1。当为全局block时,copy不做任何操作
// Its a stack block. Make a copy.struct Block_layout *result = malloc(aBlock->descriptor->size);if (!result) return (void )0;memmove(result, aBlock, aBlock->descriptor->size); // bitcopy first// reset refcountresult->flags &= ~(BLOCK_REFCOUNT_MASK); // XXX not neededresult->flags |= BLOCK_NEEDS_FREE | 1;result->isa = _NSConcreteMallocBlock;if (result->flags & BLOCK_HAS_COPY_DISPOSE) { //printf("calling block copy helper %p(%p, %p)...\n", aBlock->descriptor->copy, result, aBlock); (aBlock->descriptor->copy)(result, aBlock); // do fixup}

当block在栈上时,调用Block_copy,block将被copy到堆上。如果block实现了copy和dispose方法,则调用对应的方法,来处理捕获的变量。
小节
通过上面的分析,相信大家对block有了更加清晰的理解。😊如果大家有兴趣,可以看下block在runtime的源码,结合我们上面转换的c++代码,可以看到更完整实现细节。下节,我将从使用block所引发的retain cycle问题,来分析runtime的源码

上一篇下一篇

猜你喜欢

热点阅读