Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define __STRMERGE(a, b) a##b
- #define _STRMERGE(a, b) __STRMERGE(a, b)
- #ifdef __clang__
- static void __attribute__ ((unused)) __clang_cleanup_func(void (^*dfunc) (void))
- {
- (*dfunc) ();
- }
- #define defer void (^_STRMERGE(__defer_f_, __COUNTER__))(void) __attribute__((cleanup(__clang_cleanup_func))) __attribute__((unused)) = ^
- #else
- #define __block
- #define _DEFER(a, count) \
- auto void _STRMERGE(__defer_f_, count)(void *_defer_arg __attribute__((unused))); \
- int _STRMERGE(__defer_var_, count) __attribute__((cleanup(_STRMERGE(__defer_f_, count)))) __attribute__((unused)); \
- void _STRMERGE(__defer_f_, count)(void *_defer_arg __attribute__((unused)))
- #define defer _DEFER(a, __COUNTER__)
- #endif
- ===
- Clang compilation:
- clang -fblocks -lBlocksRuntime
- GCC compilation:
- gcc -std=c11
- ===
- Example:
- void func(void) {
- void *a = malloc(10);
- defer { free(a); }
- do..sth..
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement