Skip to content

Instantly share code, notes, and snippets.

View sinecode's full-sized avatar

Emilio Cecchini sinecode

  • Extendi
  • Florence, Italy
View GitHub Profile
@baruch
baruch / defer.h
Created May 27, 2018 19:09
Defer cleanup for C (gcc and llvm)
#define DEFER_MERGE(a,b) a##b
#define DEFER_VARNAME(a) DEFER_MERGE(defer_scopevar_, a)
#define DEFER_FUNCNAME(a) DEFER_MERGE(defer_scopefunc_, a)
#define DEFER(BLOCK) void DEFER_FUNCNAME(__LINE__)(int *a) BLOCK; __attribute__((cleanup(DEFER_FUNCNAME(__LINE__)))) int DEFER_VARNAME(__LINE__)
// Usage:
/*
void dosomething()
{
char* data = malloc(100);