block本质和原理,网上资料早已被写的烂大街了,但是仅仅阅读网上博客,对于原理的理解是不够的,绝知此事要躬行.
先写一个最简单的block
我这里新建的的是iOS项目
int main(int argc, char * argv[]) {
@autoreleasepool {
NSLog(@"======1=======");
void(^myBlock)(void) = ^(){
NSLog(@"testBlock");
};
myBlock();
NSLog(@"======2=======");
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
当然新建一个Mac console项目也行
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSLog(@"======1=======");
void(^myBlock)(void) = ^(){
NSLog(@"testBlock");
};
myBlock();
NSLog(@"======2=======");
}
return 0;
}
cd 命令切换到main.m目录,执行下面命令,把OC代码编译成C++代码,得到mian.cpp文件
xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m
上面代码编译成的C++代码如下:
int main(int argc, const char * argv[]) {
/* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool;
NSLog((NSString *)&__NSConstantStringImpl__var_folders_vh_q79lg2jd2zz2by83j2rfsn_r0000gp_T_main_6cfc2b_mi_0);
void(*myBlock)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA));
((void (*)(__block_impl *))((__block_impl *)myBlock)->FuncPtr)((__block_impl *)myBlock);
NSLog((NSString *)&__NSConstantStringImpl__var_folders_vh_q79lg2jd2zz2by83j2rfsn_r0000gp_T_main_6cfc2b_mi_2);
}
return 0;
}
所以block定义和调用,分别变成如下
void(*myBlock)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA));
((void (*)(__block_impl *))((__block_impl *)myBlock)->FuncPtr)((__block_impl *)myBlock);
接着进一步简化代码为
void(*myBlock)(void) = &__main_block_impl_0(__main_block_func_0, &__main_block_desc_0_DATA);
(__block_impl *) myBlock->FuncPtr(myBlock);
所以mian函数最终可以简化为:
int main(int argc, const char * argv[]) {
void(*myBlock)(void) = &__main_block_impl_0(__main_block_func_0, &__main_block_desc_0_DATA);
(__block_impl *) myBlock->FuncPtr(myBlock);
return 0;
}
完整代码为:
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
NSLog(@"testBlock");
}
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)};
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;
}
};
int main(int argc, const char * argv[]) {
void(*myBlock)(void) = &__main_block_impl_0(__main_block_func_0, &__main_block_desc_0_DATA);
(__block_impl *) myBlock->FuncPtr(myBlock);
return 0;
}
下面开始分析代码:
由上面代码可以看出,block最终被编译成了一个结构体,这个结构体内部又有两个结构体成员impl和desc;其中impl结构体内部又包含isa指针和FuncPtr函数指针;
由于由于impl和block结构体是同一个内存地址,所以可以直接使用block的结构体指针myBlock访问函数指针方式调用函数,即myBlock->FuncPtr()
所以blcok本质可以简写如下:
static struct __block_desc_0 {
size_t reserved;
size_t Block_size;
} __block_desc_0_DATA = { 0, sizeof(struct __block_impl_0)};
struct __block_impl {
void *isa;
int Flags;
int Reserved;
void *FuncPtr;
};
struct __block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
__block_impl_0(void *fp, struct __block_desc_0 *desc, int flags=0) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
网友评论