Objective-C Block

对于闭包的定义以及其他定义就不多说了。
主要说一下:1、Block内部实现
2、Block种类
3、__block变量以及循环引用

Block实现

Block内存结构

Objective-C Block_第1张图片
image.png

对应结构体

struct __block_impl {
void *isa;         ///< 对象指针
int Flags;         ///< block附加信息。(bit表示)
int Reserved;      ///< 保留
void *FuncPtr;     ///< 函数指针
};

static struct __testBlockMethod_block_desc_0 {
size_t reserved;           ///< 保留
size_t Block_size;         ///< block大小
///< copy和dispose只有 使用了__block 变量才会产生  对截获对象的持有or释放
///< copy:栈上的block复制到堆上 时调用
///< dispose:堆上的block被废弃 时调用
void (*copy)(struct __testBlockMethod_block_impl_0*, struct __testBlockMethod_block_impl_0*);
void (*dispose)(struct __testBlockMethod_block_impl_0*);
} __testBlockMethod_block_desc_0_DATA = { 0, sizeof(struct __testBlockMethod_block_impl_0), __testBlockMethod_block_copy_0, __testBlockMethod_block_dispose_0};

struct __Block_byref_testValue1_0 {
void *__isa;
__Block_byref_testValue1_0 *__forwarding;    ///< 一个指向自身的指针。(当__block变量复制到堆上后,__forwarding指向复制到堆上的__block变量的结构体的指针。所以:不管__block变量配置在栈上还是堆上,都能够正确访问变量。)
int __flags;
int __size;
int testValue1;
};

struct __testBlockMethod_block_impl_0 {
struct __block_impl impl;                             ///< 函数指针,上图的invoke
struct __testBlockMethod_block_desc_0* Desc;          ///< block的附加信息
__Block_byref_testValue1_0 *testValue1; // by ref
__Block_byref_testValue2_1 *testValue2; // by ref
__testBlockMethod_block_impl_0(void *fp, struct __testBlockMethod_block_desc_0 *desc, __Block_byref_testValue1_0 *_testValue1, __Block_byref_testValue2_1 *_testValue2, int flags=0) : testValue1(_testValue1->__forwarding), testValue2(_testValue2->__forwarding) {
  impl.isa = &_NSConcreteStackBlock;
  impl.Flags = flags;
  impl.FuncPtr = fp;
  Desc = desc;
}
};

上图表示的是block的内存结构,其中最重要的是invoke。这个变量是一个函数指针,可以看到他的第一个参数是:void * (这个参数代表block)。why?*** 因为在执行block的时候,需要从内存中把block所捕获的变量读出来。***
问题来了,block捕获的变量在内存里? 是的,block会把所有捕获的变量copy一份,放在上图中descriptor的后面。捕获了多少变量,就要占据多少内存。(这里copy的并不是对象本身,而是指向这些对象的指针)

eg:现在来使用clang -rewrite-objc -filename 来看一下block

#include 

void testBlockMethod() {
  int testValue1 = 10;
  void (^testBlock)(void) = ^ {
      printf("%d\n", testValue1);
  };
  testBlock();
}

///< 产生的源代码

struct __block_impl {
void *isa;
int Flags;
int Reserved;
void *FuncPtr;
};
struct __testBlockMethod_block_impl_0 {
struct __block_impl impl;
struct __testBlockMethod_block_desc_0* Desc;
int testValue1;
__testBlockMethod_block_impl_0(void *fp, struct __testBlockMethod_block_desc_0 *desc, int _testValue1, int flags=0) : testValue1(_testValue1) {
  impl.isa = &_NSConcreteStackBlock;    ///< 是的,Block是个OC对象,(想一下OC对象结构)。 _NSConcreteStackBlock对应_NSConcreteGlobalBlock和_NSConcreteMallocBlock。是的,Block分为栈Block、全局Block和堆Block。如果你的Block捕获周围变量(testValue1),那么就会在栈上。如果对栈Block执行copy,那么就会去堆上。
  impl.Flags = flags;
  impl.FuncPtr = fp;
  Desc = desc;
}
};
static void __testBlockMethod_block_func_0(struct __testBlockMethod_block_impl_0 *__cself) {
int testValue1 = __cself->testValue1; // bound by copy

      printf("%d\n", testValue1);
  }

static struct __testBlockMethod_block_desc_0 {
size_t reserved;
size_t Block_size;
} __testBlockMethod_block_desc_0_DATA = { 0, sizeof(struct __testBlockMethod_block_impl_0)};
void testBlockMethod() {
  int testValue1 = 10;
  void (*testBlock)(void) = ((void (*)())&__testBlockMethod_block_impl_0((void *)__testBlockMethod_block_func_0, &__testBlockMethod_block_desc_0_DATA, testValue1));
//    struct __testBlockMethod_block_impl_0 tempTestBlock = __testBlockMethod_block_impl_0((void *)__testBlockMethod_block_func_0, &__testBlockMethod_block_desc_0_DATA, testValue1);
//    struct __testBlockMethod_block_impl_0 *testBlock = &tempTestBlock;
//    ///< __testBlockMethod_block_impl_0 构造函数
//    __testBlockMethod_block_impl_0(__testBlockMethod_block_func_0, __testBlockMethod_block_desc_0_DATA, testValue1, 0) {
//        testValue1      = _testValue1;
//        
//        impl.isa        = &_NSConcreteStackBlock;
//        impl.Flags      = 0;
//        impl.FuncPtr    = __testBlockMethod_block_func_0;
//        impl.Desc       = &__testBlockMethod_block_desc_0_DATA;
//    }
  ((void (*)(__block_impl *))((__block_impl *)testBlock)->FuncPtr)((__block_impl *)testBlock);
//    (testBlock->FuncPtr)(testBlock);    ///< 明显的函数指针调用
}
static struct IMAGE_INFO { unsigned version; unsigned flag; } _OBJC_IMAGE_INFO = { 0, 2 };

通过上述实例,可以看到如果将C语言数组截获的话,那就会产生诸如:

int testValueA[10] = { 1 };
int testValueB[10] = testVauleA;

此类的代码了,C语言规范不允许此类赋值。

Block种类

全局Block _NSConcreteGlobalBlock (程序的数据区域.data)(不会访问外部变量)

Block内部没有捕获任何外部变量。
全局Block不会捕捉任何状态,他的内存区域在编译期已经完全确定,他声明在全局内存里,不需要每次用到的时候再在栈上创建。全局Block相当于单例,所以不会被系统回收。

栈Block _NSConcreteStackBlock (栈)(返回时销毁)

在定义Block的时候,他的内存区域是分配在栈上的。也就是说,Block只能在定义他的范围内有效。出了此范围就失效了。but,如果在此时,覆盖了此内存块就会出问题了。
*** 可以使用copy操作,将栈block 拷贝到堆上。***
对于栈Block的回收,无需担心,系统会自动回收。(如果栈Block被回收了,此时使用栈Block就会出问题)。
1)、mrc
当函数退出的时候,Block会被释放,再次调用会产生crash。
2)、arc
在arc下,生成的Block也是栈Block,但是再将Block赋值给strong类型的变量的时候,会自动执行一次copy。

堆Block _NSConcreteMallocBlock (堆)(引用计数为0时销毁)

跟OC对象一样,拥有引用计数了。(对于堆Block的拷贝操作只是对引用计数的操作。)在arc环境下,堆Block和普通OC对象一样,可以交给系统处理内存。
此时~循环引用就出现了。
1)、mrc
需要手动将栈Block拷贝到 堆上面。
2)、arc
自动执行copy操作。

__block 变量

__block变量的转换代码在上文中都有看到。 __block变量会跟随Block内存结构的变化。
当Block从栈复制到堆上的时候,1:如果__block变量在栈上,那么__block变量将从栈复制到堆。2:如果__block变量在堆上,那么他将被Block持有。
在arc和mrc下的区别:
1、arc:
1)、__block修饰会引起循环引用
2)、__block修饰的变量在block代码中会被retain
2、mrc:
1)、__block修饰不会引起循环引用
2)、__block修饰的变量在block代码中不会被retain

Block循环引用

这里就不说产生循环引用的原因或者是解决方法了,网上一搜一大把。

总结

Block提供了与C函数相同的功能。但是他使用起来更直观。而且,Block可以捕获周围变量的值。
还要注意的是:
1、Block对于外部变量的引用是将变量复制到Block数据结构中实现的。
2、Block对于__block修饰的外部变量的引用,是通过复制变量的地址来实现的。

参考1:tutorials blocks
参考2:【Objective-C 高级编程】

你可能感兴趣的:(Objective-C Block)