Block是什么?
首先引入闭包的概念,闭包是一个函数(或指向函数的指针),再加上该函数执行的外部的上下文变量(有时候也称作自由变量)。
Block就是OC中的闭包的实现。
Block的数据结构
对应的结构体:
struct Block_descriptor {
unsigned long int reserved;
unsigned long int size;
void (*copy)(void *dst, void *src);
void (*dispose)(void *);
};
struct Block_layout {
void *isa;
int flags;
int reserved;
void (*invoke)(void *, ...);
struct Block_descriptor *descriptor;
/* Imported variables. */
};
block的数据结构包括6部分:
- isa指针,由此可以看出block其实是一个对象
- flags,block的附加信息,block copy 的方法中会对使用该变量
- reserved,保留变量
- invoke,函数指针
- descriptor,包括block的大小,copy/dispose函数
- variables,存放截获的变量等。
block的类型
- NSConcreteStackBlock
- NSConcreteGloableBlock
- NSConcreteMallocBlock
未引用任何外部变量,则为NSConcreteGloableBlock类型;
ARC下,block会被自动复制到堆上,类型为NSConcreteMallocBlock。
block会自动copy到堆上的情况有:
- block作为函数返回值
- block赋值给__strong变量
- CocoaAPI 含有block方法参数
- GCD API方法参数
截获局部变量
int main() {
int a = 100;
void (^block2)(void) = ^{
printf("%d\n", a);
};
block2();
return 0;
}
MRC下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 a;
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int _a, int flags=0) : a(_a) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
int a = __cself->a; // bound by copy
printf("%d\n", a);
}
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 a = 100;
void (*block2)(void) = (void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, a);
((void (*)(__block_impl *))((__block_impl *)block2)->FuncPtr)((__block_impl *)block2);
return 0;
}
由以上源码分析:
- __block_impl,其中包括isa和FuncPtr函数指针,函数指针指向__main_block_func_0
- __main_block_desc_0,结构体包括size,如上截获了变量后,结构体的大小会变化
- 截获的局部变量会复制到block的结构体中,所以在block后 改变变量值也不会影响block内部
__block
如果想要在block中使用局部变量,需要将变量用__block说明符来修饰。
int main()
{
__block int i = 1024;
void (^block1)(void) = ^{
printf("%d\n", i);
i = 1023;
};
block1();
return 0;
}
MRC下clang后,源码如下:
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
printf("%d\n", (i->__forwarding->i));
(i->__forwarding->i) = 1023;
}
static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_Block_object_assign((void*)&dst->i, (void*)src->i, 8/*BLOCK_FIELD_IS_BYREF*/);}
static void __main_block_dispose_0(struct __main_block_impl_0*src) {_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), 1024};
void (*block1)(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 *)block1)->FuncPtr)((__block_impl *)block1);
return 0;
}
如源码所示:
- 被__block说明符修饰的变量转换成__Block_byref_i_0结构体,里面含有isa指针,所以它也是一个对象。
- 结构体中含有forwarding指向自身的指针,访问变量值是通过__forwarding 指针访问的(i->__forwarding->i )
- block需要对__Block_byref_i_0结构体进行内存管理(block中截获了对象时也需要对该对象进行内存管理),block会生成copy/dispose函数,在copy函数中调用_Block_object_assign方法把对象地址拷贝给block的成员变量。
__block修饰OC变量
__block NSArray *arr = @[@"1"];
void (^tmpBlock)(void) = ^{
arr = @[@"2"];
};
tmpBlock();
clang后,源码如下:
struct __Block_byref_arr_0 {
void *__isa;
__Block_byref_arr_0 *__forwarding;
int __flags;
int __size;
void (*__Block_byref_id_object_copy)(void*, void*);
void (*__Block_byref_id_object_dispose)(void*);
NSArray *arr;
};
由源码所示:
__Block_byref_arr_0结构体中增加了copy/dispose的方法。
__Block_byref_copy的方法中将原本栈上的forwarding指针指向了堆。
参考博客:
谈Objective-C block的实现
Block中copy/dispose的方法分析
iOS block浅析-关于ARC下block类型
深入理解iOS的block