From 038fdc2017dd2c953211a38ac5d44faf7a83db66 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 13 Apr 2020 11:20:43 +0200 Subject: [PATCH] LibC: Simplify ASSERT() to reduce code size Instead of pushing the message, file name, line# and function name separately, we now mash the message, file name and line# into a string constant and pass that. This means that the failure path only has to push a single address onto the stack, reducing the code size and causing the compiler to inline many more functions containing an assertions (e.g RefPtr::operator*()) Obviously if you wanted minimal size, you could turn assertions off entirely, but I really like running with assertions, so let's make a little effort to reduce their impact. :^) --- Libraries/LibC/assert.cpp | 6 +++--- Libraries/LibC/assert.h | 6 ++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Libraries/LibC/assert.cpp b/Libraries/LibC/assert.cpp index f5659b1a59..244074d5c3 100644 --- a/Libraries/LibC/assert.cpp +++ b/Libraries/LibC/assert.cpp @@ -32,10 +32,10 @@ extern "C" { #ifdef DEBUG -void __assertion_failed(const char* msg, const char* file, unsigned line, const char* func) +void __assertion_failed(const char* msg) { - dbgprintf("USERSPACE(%d) ASSERTION FAILED: %s\n%s:%u in %s\n", getpid(), msg, file, line, func); - fprintf(stderr, "ASSERTION FAILED: %s\n%s:%u in %s\n", msg, file, line, func); + dbgprintf("USERSPACE(%d) ASSERTION FAILED: %s\n", getpid(), msg); + fprintf(stderr, "ASSERTION FAILED: %s\n", msg); abort(); for (;;) ; diff --git a/Libraries/LibC/assert.h b/Libraries/LibC/assert.h index bc03750bb4..bc374e5e12 100644 --- a/Libraries/LibC/assert.h +++ b/Libraries/LibC/assert.h @@ -31,8 +31,10 @@ __BEGIN_DECLS #ifdef DEBUG -__attribute__((noreturn)) void __assertion_failed(const char* msg, const char* file, unsigned line, const char* func); -# define assert(expr) ((expr) ? (void)0 : __assertion_failed(# expr, __FILE__, __LINE__, __PRETTY_FUNCTION__)) +__attribute__((noreturn)) void __assertion_failed(const char* msg); +# define __stringify_helper(x) # x +# define __stringify(x) __stringify_helper(x) +# define assert(expr) ((expr) ? (void)0 : __assertion_failed(# expr "\n" __FILE__ ":" __stringify(__LINE__))); # define ASSERT_NOT_REACHED() assert(false) #else # define assert(expr)