Added COLD_EXPR() and HOT_EXPR() macros that (ab)use label attrs. Not sure of a better way to mark blocks/expressions as `cold` :/ I dunno if `if(UNLIKELY(1))` would work.

Fortune for naka's current commit: Great blessing − 大吉
master
Avril 3 years ago
parent 7fd570ff6f
commit 145781a293
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -16,6 +16,9 @@
#define _pure __attribute__((const)) #define _pure __attribute__((const))
#define _readonly __attribute__((pure)) #define _readonly __attribute__((pure))
#define _cold __attribute__((cold))
#define _hot __attribute__((hot))
#ifndef __cplusplus #ifndef __cplusplus
#define noreturn __attribute__((noreturn)) #define noreturn __attribute__((noreturn))
#define deprecated __attribute__((deprecated)) #define deprecated __attribute__((deprecated))
@ -71,6 +74,21 @@
#define ifL(x) if(LIKELY((x))) #define ifL(x) if(LIKELY((x)))
#define ifU(x) if(UNLIKELY((x))) #define ifU(x) if(UNLIKELY((x)))
#define COLD_EXPR(expr) ({ \
goto _cold_expr; \
UNREACHABLE_UNSAFE(); \
_cold_expr: \
__attribute__((cold)); \
expr; \
})
#define HOT_EXPR(expr) ({ \
goto _hot_expr; \
UNREACHABLE_UNSAFE(); \
_hot_expr: \
__attribute__((hot)); \
expr; \
})
/// Equivalent to GNU C `x ?: y` operator. /// Equivalent to GNU C `x ?: y` operator.
#define TERN_OR(x, y) ({ let _x = (x); _x ? _x : (y); }) #define TERN_OR(x, y) ({ let _x = (x); _x ? _x : (y); })
@ -100,6 +118,14 @@ _mixin void _drain_val(void* x, ...) { IGNORE(x); } // This compiles to no-op on
#define assert_msg(expr, ...) ( (expr) ? 1 : FATAL(__VA_ARGS__)) #define assert_msg(expr, ...) ( (expr) ? 1 : FATAL(__VA_ARGS__))
#define UNREACHABLE_UNSAFE() __builtin_unreachable()
#ifdef DEBUG
#define UNREACHABLE() do { COLD_EXPR(FATAL("unreachable code entered")); } while(0)
#else
#define UNREACHABLE() UNREACHABLE_UNSAFE()
#endif
#ifdef DEBUG #ifdef DEBUG
#define debug_assert(x) assert((x)) #define debug_assert(x) assert((x))
#elif defined(_EVAL_DEBUG_ONLY_STMTS) #elif defined(_EVAL_DEBUG_ONLY_STMTS)

@ -68,6 +68,7 @@ _mixin int _map_handle_err(const char* stmt, const char* file, int line, const c
return 0; return 0;
} }
} }
UNREACHABLE();
} }
#define map_handle_err(stmt) _map_handle_err(#stmt, __FILE__, __LINE__, __func__, stmt) #define map_handle_err(stmt) _map_handle_err(#stmt, __FILE__, __LINE__, __func__, stmt)

Loading…
Cancel
Save