From b7f336383dc8ba58f720adb4c1d218348bf57e54 Mon Sep 17 00:00:00 2001 From: Michal Schmidt Date: Tue, 17 Jan 2012 12:05:33 +0100 Subject: [PATCH] log: make asserts cheaper On my x86_64 this shrinks the size of .text by 53 KB (7 %). --- src/log.c | 23 +++++++++++++---------- src/log.h | 7 ++----- src/macro.h | 8 ++------ 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/log.c b/src/log.c index 04e90eb20..f65e8d100 100644 --- a/src/log.c +++ b/src/log.c @@ -618,18 +618,13 @@ int log_meta( return r; } -void log_assert( - const char*file, - int line, - const char *func, - const char *format, ...) { - +_noreturn_ static void log_assert(const char *text, const char *file, int line, const char *func, const char *format) { static char buffer[LINE_MAX]; - va_list ap; - va_start(ap, format); - vsnprintf(buffer, sizeof(buffer), format, ap); - va_end(ap); +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wformat-nonliteral" + snprintf(buffer, sizeof(buffer), format, text, file, line, func); +#pragma GCC diagnostic pop char_array_0(buffer); log_abort_msg = buffer; @@ -638,6 +633,14 @@ void log_assert( abort(); } +void log_assert_failed(const char *text, const char *file, int line, const char *func) { + log_assert(text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Aborting."); +} + +void log_assert_failed_unreachable(const char *text, const char *file, int line, const char *func) { + log_assert(text, file, line, func, "Code should not be reached '%s' at %s:%u, function %s(). Aborting."); +} + int log_set_target_from_string(const char *e) { LogTarget t; diff --git a/src/log.h b/src/log.h index 6ab07a542..7028a1327 100644 --- a/src/log.h +++ b/src/log.h @@ -73,11 +73,8 @@ int log_meta( const char *func, const char *format, ...) _printf_attr_(5,6); -_noreturn_ void log_assert( - const char*file, - int line, - const char *func, - const char *format, ...) _printf_attr_(4,5); +_noreturn_ void log_assert_failed(const char *text, const char *file, int line, const char *func); +_noreturn_ void log_assert_failed_unreachable(const char *text, const char *file, int line, const char *func); /* This modifies the buffer passed! */ int log_dump_internal( diff --git a/src/macro.h b/src/macro.h index 3f30aa789..58de001f2 100644 --- a/src/macro.h +++ b/src/macro.h @@ -91,9 +91,7 @@ static inline size_t ALIGN_TO(size_t l, size_t ali) { #define assert_se(expr) \ do { \ if (_unlikely_(!(expr))) \ - log_assert(__FILE__, __LINE__, __PRETTY_FUNCTION__, \ - "Assertion '%s' failed at %s:%u, function %s(). Aborting.", \ - #expr , __FILE__, __LINE__, __PRETTY_FUNCTION__); \ + log_assert_failed(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ } while (false) \ /* We override the glibc assert() here. */ @@ -106,9 +104,7 @@ static inline size_t ALIGN_TO(size_t l, size_t ali) { #define assert_not_reached(t) \ do { \ - log_assert(__FILE__, __LINE__, __PRETTY_FUNCTION__, \ - "Code should not be reached '%s' at %s:%u, function %s(). Aborting.", \ - t, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ + log_assert_failed_unreachable(t, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ } while (false) #define assert_cc(expr) \ -- 2.30.2