From 35d889c3444f3d196e6e0415313282db4d381f02 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Mon, 7 Sep 2015 14:51:59 +0200 Subject: ucoo: add assert message --- ucoo/arch/arch.host.cc | 7 +++++++ ucoo/arch/arch.stm32.cc | 10 ++++++++++ ucoo/common.hh | 15 +++++++++++++++ 3 files changed, 32 insertions(+) (limited to 'ucoo') diff --git a/ucoo/arch/arch.host.cc b/ucoo/arch/arch.host.cc index 1ddd6a9..431b776 100644 --- a/ucoo/arch/arch.host.cc +++ b/ucoo/arch/arch.host.cc @@ -62,6 +62,13 @@ halt () abort (); } +void +halt (const char *msg) +{ + fprintf (stderr, "halt: %s\n", msg); + abort (); +} + void halt_perror () { diff --git a/ucoo/arch/arch.stm32.cc b/ucoo/arch/arch.stm32.cc index 27cd721..aa07575 100644 --- a/ucoo/arch/arch.stm32.cc +++ b/ucoo/arch/arch.stm32.cc @@ -42,6 +42,16 @@ halt () ; } +void +halt (const char *msg) +{ + volatile const char *halt_message = msg; + (void) halt_message; + asm volatile ("bkpt"); + while (1) + ; +} + void halt_perror () { diff --git a/ucoo/common.hh b/ucoo/common.hh index 24ead61..708b9a1 100644 --- a/ucoo/common.hh +++ b/ucoo/common.hh @@ -52,6 +52,10 @@ access_once (T &x) void halt () __attribute__ ((noreturn)); +/// Stop, try to output error message. +void +halt (const char *msg) __attribute__ ((noreturn)); + /// Stop, try to output error description corresponding to errno. void halt_perror () __attribute__ ((noreturn)); @@ -67,6 +71,17 @@ assert (bool condition) halt (); } +void +assert (bool condition, const char *msg) __attribute__ ((always_inline)); +extern inline void +assert (bool condition, const char *msg) +{ + if (!__builtin_expect (condition, 1)) + halt (msg); +} + +#define assert(c) assert (c, #c) + /// To be used to swear that this code can never be reached. void assert_unreachable () __attribute__ ((noreturn)); -- cgit v1.2.3