iOS 黑魔法 __attribute__((cleanup))

SD onExit 宏分析

在读 SDWebimage 源码的时候,发现有这么一个骚操作

 @onExit {
        if (input_buffer.data) free(input_buffer.data);
        if (output_buffer.data) free(output_buffer.data);
    };

然后就来看看 OnExit 的宏是啥样的

#ifndef onExit
#define onExit \
sd_keywordify \
__strong sd_cleanupBlock_t metamacro_concat(sd_exitBlock_, __LINE__) __attribute__((cleanup(sd_executeCleanupBlock), unused)) = ^
#endif

typedef void (^sd_cleanupBlock_t)(void);

#if defined(__cplusplus)
extern "C" {
#endif
    void sd_executeCleanupBlock (__strong sd_cleanupBlock_t *block);
#if defined(__cplusplus)
}
#endif

这一看,我蒙了,这是什么骚操作,之前看过有 @weakify 这种宏定义,其实就是弱引用,前面加个 @ 可能都是来源于RAC的灵感吧,这里这个宏的定义比较复杂,我们一点一点来解释
,首先

__strong sd_cleanupBlock_t

typedef void (^sd_cleanupBlock_t)(void);

这个都能看懂, sd_cleanupBlock_t 是下面定义的block,然后

metamacro_concat(sd_exitBlock_, __LINE__)

这个又是设么意思呢,其实也是 SD 定义的一个宏

#define metamacro_concat(A, B) \
        metamacro_concat_(A, B)

#define metamacro_concat_(A, B) A ## B

看到这里是不是就明白了?就是连接两个字符串,所以

__strong sd_cleanupBlock_t metamacro_concat(sd_exitBlock_, __LINE__) 这一行就等于

__strong sd_cleanupBlock_t sd_exitBlock_ 10 ,其实就是根据当前行数,定义了一个 block 的名字,接下来重点来了

__attribute__((cleanup(sd_executeCleanupBlock), unused)) = ^

attribute((cleanup)) 黑魔法

关于 cleanup 的黑魔法,可以参考这篇文章
,和这篇文章我就直接将 sunnyxx 大神的文章搬过来了,做个记录

attribute((cleanup(...))),用于修饰一个变量,在它的作用域结束时可以自动执行一个指定的方法,如:

// 指定一个cleanup方法,注意入参是所修饰变量的地址,类型要一样
// 对于指向objc对象的指针(id *),如果不强制声明__strong默认是__autoreleasing,造成类型不匹配
static void stringCleanUp(__strong NSString **string) {
    NSLog(@"%@", *string);
}
// 在某个方法中:
{
    __strong NSString *string __attribute__((cleanup(stringCleanUp))) = @"sunnyxx";
} // 当运行到这个作用域结束时,自动调用stringCleanUp

所谓作用域结束,包括大括号结束、return、goto、break、exception等各种情况。
当然,可以修饰的变量不止NSString,自定义Class或基本类型都是可以的:

// 自定义的Class
static void sarkCleanUp(__strong Sark **sark) {
    NSLog(@"%@", *sark);
}
__strong Sark *sark __attribute__((cleanup(sarkCleanUp))) = [Sark new];
// 基本类型
static void intCleanUp(NSInteger *integer) {
    NSLog(@"%d", *integer);
}
NSInteger integer __attribute__((cleanup(intCleanUp))) = 1;

假如一个作用域内有若干个cleanup的变量,他们的调用顺序是先入后出的栈式顺序;
而且,cleanup是先于这个对象的dealloc调用的。

既然attribute((cleanup(...)))可以用来修饰变量,block当然也是其中之一,写一个block的cleanup函数非常有趣:

// void(^block)(void)的指针是void(^*block)(void)
static void blockCleanUp(__strong void(^*block)(void)) {
    (*block)();
}

于是在一个作用域里声明一个block:

{
   // 加了个`unused`的attribute用来消除`unused variable`的warning
    __strong void(^block)(void) __attribute__((cleanup(blockCleanUp), unused)) = ^{
        NSLog(@"I'm dying...");
    };
} // 这里输出"I'm dying..."
#define onExit\
    __strong void(^block)(void) __attribute__((cleanup(blockCleanUp), unused)) = ^

用这个宏就能将一段写在前面的代码最后执行:

{
    onExit {
        NSLog(@"yo");
    };
} // Log

这样的写法可以将成对出现的代码写在一起,比如说一个lock:

NSRecursiveLock *aLock = [[NSRecursiveLock alloc] init];
[aLock lock];
// 这里
//     有
//        100多万行
[aLock unlock]; // 看到这儿的时候早忘了和哪个lock对应着了

用了onExit之后,代码更集中了:

NSRecursiveLock *aLock = [[NSRecursiveLock alloc] init];
[aLock lock];
onExit {
    [aLock unlock]; // 妈妈再也不用担心我忘写后半段了
};
// 这里
//    爱多少行
//           就多少行

妈耶,看到这里,是不是已经豁然开朗了。

你可能感兴趣的:(iOS 黑魔法 __attribute__((cleanup)))