Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2016
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. #define __STRMERGE(a, b) a##b
  2. #define _STRMERGE(a, b) __STRMERGE(a, b)
  3.  
  4. #ifdef __clang__
  5. static void __attribute__ ((unused)) __clang_cleanup_func(void (^*dfunc) (void))
  6. {
  7. (*dfunc) ();
  8. }
  9.  
  10. #define defer void (^_STRMERGE(__defer_f_, __COUNTER__))(void) __attribute__((cleanup(__clang_cleanup_func))) __attribute__((unused)) = ^
  11. #else
  12. #define __block
  13. #define _DEFER(a, count) \
  14. auto void _STRMERGE(__defer_f_, count)(void *_defer_arg __attribute__((unused))); \
  15. int _STRMERGE(__defer_var_, count) __attribute__((cleanup(_STRMERGE(__defer_f_, count)))) __attribute__((unused)); \
  16. void _STRMERGE(__defer_f_, count)(void *_defer_arg __attribute__((unused)))
  17. #define defer _DEFER(a, __COUNTER__)
  18. #endif
  19.  
  20.  
  21. ===
  22. Clang compilation:
  23. clang -fblocks -lBlocksRuntime
  24.  
  25. GCC compilation:
  26. gcc -std=c11
  27. ===
  28. Example:
  29.  
  30. void func(void) {
  31. void *a = malloc(10);
  32. defer { free(a); }
  33. do..sth..
  34. return;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement