上面几遍文章是block的常见用法,接下来,我们将讨论一下,block 的底层是怎么实现的,block 的类型有哪些,block 的本质,又是什么?
block 的类型
BlocksRuntime 官方文档
类型 | 特征 | 内存管理 |
---|---|---|
_NSConcreteStackBlock | 只用到外部局部变量、成员属性变量,且没有强指针引用的block都是StackBlock。 | 系统管理 |
_NSConcreteMallocBlock | 有强指针引用或copy修饰的成员属性引用的block会被复制一份到堆中成为MallocBlock,没有强指针引用即销毁 | 程序员管理 |
_NSConcreteGlobalBlock | 没有用到外界变量或只用到全局变量、静态变量的block为_NSConcreteGlobalBlock,生命周期从创建到应用程序结束。 | 系统管理 |
通过clang main.m -rewrite-objc -o dest.cpp ,查看cpp 的代码来看看 block 做来什么
- stack block type
// source code
int main(int argc, char * argv[]) {
void (^myHello)(void) = ^{
int testHello = 4;
int static sHello = 2;
};
myHello();
}
// block impl
struct __block_impl {
void *isa;
int Flags;
int Reserved;
void *FuncPtr; // FuncPtr = (void (*)(__block_impl *)
};
// block desc
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)};
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
int testHello = 4;
int static sHello = 2;
}
// 对应 myHello 的结构体声明
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;
}
};
int main(int argc, char * argv[]) {
// 初始化block myHello
void (*myHello)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA));
// 调用block myHello
((void (*)(__block_impl *))((__block_impl *)myHello)->FuncPtr)((__block_impl *)myHello);
}
总结:
myHello 是一个结构体列席,有2部分构成,实现 + 描述,有构造函数
- stack block type + local var
// source code
int main(int argc, char * argv[]) {
int a = 3;
void (^myHello)(void) = ^{
int testHello = 4;
int static sHello = 2;
int b = a;
};
myHello();
}
// block impl
struct __block_impl {
void *isa;
int Flags;
int Reserved;
void *FuncPtr;
};
// block desc
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)};
// FuncPrt 对应的函数
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
int a = __cself->a; // bound by copy
int testHello = 4;
int static sHello = 2;
int b = a;
}
// myHello block 的实现
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;
}
};
// init
int a = 3;
// myhello block 声明
void (*myHello)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, a));
// myhello block 调用FuncPtr
((void (*)(__block_impl *))((__block_impl *)myHello)->FuncPtr)
((__block_impl *)myHello);
总结:
可以myhello 主体依然是desc + impl 另外在 +
a
捕获的变量,构成结构体,构造函数会对捕获的值,进行初始化,是一个值copy,这也就是,维护 block 内部,外部,对 a 的修改,不互通,是因为2个不同值了。
- stack type block + block args + local var
// source code
int main(int argc, char * argv[]) {
int a = 3;
void (^myHello)(int) = ^(int x){
int testHello = 4;
int static sHello = 2;
int b = a;
int c = x;
};
myHello(7);
}
// block impl
struct __block_impl {
void *isa;
int Flags;
int Reserved;
void *FuncPtr; // (void (*)(__block_impl *, int) 也就是block 对应的函数类型
};
// block desc
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)};
// FuncPrt 对应的函数
static void __main_block_func_0(struct __main_block_impl_0 *__cself, int x) {
int a = __cself->a; // bound by copy
int testHello = 4;
int static sHello = 2;
int b = a;
int c = x;
}
// myHello block 的实现
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;
}
};
// init
int a = 3;
// myhello block 声明
void (*myHello)(int) = ((void (*)(int))&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, a));
// myhello block 调用FuncPtr
((void (*)(__block_impl *, int))((__block_impl *)myHello)->FuncPtr)((__block_impl *)myHello, 7);
总结
可以myhello 主体依然是desc + impl 另外在 + a 捕获的变量,构成结构体,构造函数会对捕获的值,进行初始化,是一个值copy,这也就是,维护 block 内部,外部,对 a 的修改,不互通,是因为2个不同值了。对应block 的传递参数,有独立的函数,进行参数的处理。
可以发现这里设置的很巧妙,对block 捕获到外部的值,则有myHello 对应的结构体,来管理,其值,对于传递的参数,有单独的函数处理
可以猜测,传递进来的参数,无论什么类型,都会有对应的函数,进行处理。
- 局部静态变量的,全局变量的处理
// source code
int y = 10;
int main(int argc, char * argv[]) {
int a = 3;
static int f = 6;
void (^myHello)(int) = ^(int x){
int testHello = 4;
int sHello = 2;
int z = y;
int c = x;
f = 8;
};
myHello(8);
}
// block impl 声明
struct __block_impl {
void *isa;
int Flags;
int Reserved;
void *FuncPtr;
};
//block desc
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)};
// myhello block 结构体的声明
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
int *f;
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int *_f, int flags=0) : f(_f) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
// 调用函数
static void __main_block_func_0(struct __main_block_impl_0 *__cself, int x) {
int *f = __cself->f; // bound by copy
int testHello = 4;
int sHello = 2;
int z = y;
int c = x;
(*f) = 8;
}
static int f = 6;// 静态局部
// 初始化变量
void (*myHello)(int) = ((void (*)(int))&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, &f));
// block 调用 FuncPtr
((void (*)(__block_impl *, int))((__block_impl *)myHello)->FuncPtr)((__block_impl *)myHello, 8);
总结:
对静态变量,block 结构体声明的时候,获取的是其 地址,也是指针。在修改时,其实是,操作了,指针来修改值,能保证,变量的更新。
而对于全局变量,和 全局静态变量是无差别的,跟普通的全局变量使用无区别。
- __block 修饰的局部变量
// source code
int y = 10;
int main(int argc, char * argv[]) {
__block int a = 3;
static int f = 6;
void (^myHello)(int) = ^(int x){
int testHello = 4;
int sHello = 2;
int z = y;
int c = x;
f = 8;
};
myHello(8);
}
// 下面列出变化的部分
struct __Block_byref_a_0 {
void *__isa;
__Block_byref_a_0 *__forwarding;
int __flags;
int __size;
int a;
};
// block desc
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};
// copy
static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_Block_object_assign((void*)&dst->a, (void*)src->a, 8/*BLOCK_FIELD_IS_BYREF*/);}
// dispose
static void __main_block_dispose_0(struct __main_block_impl_0*src) {_Block_object_dispose((void*)src->a, 8/*BLOCK_FIELD_IS_BYREF*/);}
// block 结构体的声明
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
int *f;
__Block_byref_a_0 *a; // by ref
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int *_f, __Block_byref_a_0 *_a, int flags=0) : f(_f), a(_a->__forwarding) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
// __block int a = 3; 转变如下
__attribute__((__blocks__(byref))) __Block_byref_a_0 a = {(void*)0,(__Block_byref_a_0 *)&a, 0, sizeof(__Block_byref_a_0), 3};
// 初始化变量 myHello
void (*myHello)(int) = ((void (*)(int))&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, &f, (__Block_byref_a_0 *)&a, 570425344));
总结:
__block 修饰的变量,会转换成
__Block_byref_a_0
它是一种结构体,内部,会持有,真正的变量 int a,传入block 是 它的地址,对指针的操作,所以,也可以在内部,修改 a 的值。
多出来了 __main_block_copy_0, __main_block_dispose_0