defer simplified
This commit is contained in:
@@ -17,18 +17,21 @@ void foo(){
|
||||
#if defined(__GNUC__) || defined(__TINYC__)
|
||||
|
||||
#define Deferral(MAX_DEFER_STATEMENTS) \
|
||||
unsigned char _num_deferrals = 0; \
|
||||
void *_defer_return_loc = 0, *_deferrals[MAX_DEFER_STATEMENTS] = {0};
|
||||
int _num_deferrals = 0; \
|
||||
ATTRIBUTE_UNUSED void *_defer_return_loc = 0;\
|
||||
ATTRIBUTE_UNUSED void *_deferrals[MAX_DEFER_STATEMENTS] = {0};
|
||||
|
||||
# define Defer(block) _Defer(block, __LINE__)
|
||||
# define Return _Return(__LINE__)
|
||||
|
||||
#define _defer_token_cat(a, b) a ## b
|
||||
#ifndef CAT2
|
||||
#define CAT2(a, b) a ## b
|
||||
#endif
|
||||
|
||||
#define _Defer(block, n) do { \
|
||||
_deferrals[_num_deferrals++] = && _defer_token_cat(_defer_ini, n); \
|
||||
_deferrals[_num_deferrals++] = && CAT2(_defer_ini, n); \
|
||||
if (0) { \
|
||||
_defer_token_cat(_defer_ini, n): \
|
||||
CAT2(_defer_ini, n): \
|
||||
block; \
|
||||
if (_num_deferrals) { \
|
||||
goto *_deferrals[--_num_deferrals]; \
|
||||
@@ -39,13 +42,11 @@ void foo(){
|
||||
} while (0)
|
||||
|
||||
#define _Return(n) \
|
||||
if (_num_deferrals \
|
||||
/* this nonsense disables warning Wunused-but-set-variable */ \
|
||||
&& _defer_return_loc == _defer_return_loc ) \
|
||||
if (_num_deferrals) \
|
||||
{ \
|
||||
_defer_return_loc = && _defer_token_cat(_defer_fini_, n); \
|
||||
_defer_return_loc = && CAT2(_defer_fini_, n); \
|
||||
goto *_deferrals[--_num_deferrals]; \
|
||||
} else _defer_token_cat(_defer_fini_, n): \
|
||||
} else CAT2(_defer_fini_, n): \
|
||||
return
|
||||
|
||||
#else /* !__GNUC__ && !__TINYCC__ */
|
||||
@@ -59,7 +60,7 @@ void foo(){
|
||||
#endif
|
||||
|
||||
#define Deferral(MAX_DEFER_STATEMENTS) \
|
||||
volatile unsigned char _num_deferrals = 0; \
|
||||
volatile int _num_deferrals = 0; \
|
||||
jmp_buf _defer_return_loc = {0}, _deferrals[MAX_DEFER_STATEMENTS] = {0};
|
||||
|
||||
#define Defer(block) do { \
|
||||
|
||||
@@ -59,6 +59,8 @@ typedef void (*Destructor_t)(void* self);
|
||||
#define dbg(N) printf("\e[95m%d\n",N)
|
||||
|
||||
#define nameof(V) #V
|
||||
#define CAT2(a, b) a ## b
|
||||
#define CAT3(a, b, c) a ## b ## c
|
||||
|
||||
#define ARRAY_LEN(A) (sizeof(A)/sizeof(A[0]))
|
||||
#define ALIGN_TO(_SIZE,_ALIGN) (((_SIZE) + ((_ALIGN) - 1)) & ~((_ALIGN) - 1))
|
||||
@@ -122,6 +124,24 @@ typedef void (*Destructor_t)(void* self);
|
||||
#define ATTRIBUTE_THREAD_LOCAL __thread
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define PRAGMA_WARNING_PUSH __pragma(warning( push ))
|
||||
#define PRAGMA_WARNING_POP __pragma(warning( pop ))
|
||||
#define PRAGMA_WARNING_DISABLE(wNumber) __pragma(warning( disable : wNumber ))
|
||||
#define W_RETURN_TYPE
|
||||
#else
|
||||
#define _PRAGMA(P) _Pragma(#P)
|
||||
#define PRAGMA_WARNING_PUSH _PRAGMA(GCC diagnostic push)
|
||||
#define PRAGMA_WARNING_POP _PRAGMA(GCC diagnostic pop)
|
||||
#define PRAGMA_WARNING_DISABLE(wName) _PRAGMA(GCC diagnostic ignored wName)
|
||||
#define W_RETURN_TYPE "-Wreturn-type"
|
||||
#endif
|
||||
#define WARNING_DISABLE(WARNING, CODE...) \
|
||||
PRAGMA_WARNING_PUSH \
|
||||
PRAGMA_WARNING_DISABLE(WARNING) \
|
||||
CODE; \
|
||||
PRAGMA_WARNING_POP
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user