From a39a53cf167337edf8dc7be13925adb77cb6e7ad Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Wed, 21 Nov 2012 22:38:31 +0100 Subject: digital/ucoolib: add assert function No assert message, no file, no line number, no fat. --- digital/ucoolib/ucoolib/arch/arch.host.cc | 17 +++++++++++++++ digital/ucoolib/ucoolib/arch/arch.stm32.cc | 14 ++++++++++++ digital/ucoolib/ucoolib/common.hh | 35 ++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) (limited to 'digital') diff --git a/digital/ucoolib/ucoolib/arch/arch.host.cc b/digital/ucoolib/ucoolib/arch/arch.host.cc index 17013f2f..a3c08419 100644 --- a/digital/ucoolib/ucoolib/arch/arch.host.cc +++ b/digital/ucoolib/ucoolib/arch/arch.host.cc @@ -22,6 +22,10 @@ // // }}} #include "ucoolib/arch/arch.hh" +#include "ucoolib/common.hh" + +#include +#include namespace ucoo { @@ -30,4 +34,17 @@ arch_init (int argc, const char **argv) { } +void +halt () +{ + abort (); +} + +void +halt_perror () +{ + perror ("halt"); + halt (); +} + } // namespace ucoo diff --git a/digital/ucoolib/ucoolib/arch/arch.stm32.cc b/digital/ucoolib/ucoolib/arch/arch.stm32.cc index 35004a72..23c772ab 100644 --- a/digital/ucoolib/ucoolib/arch/arch.stm32.cc +++ b/digital/ucoolib/ucoolib/arch/arch.stm32.cc @@ -22,6 +22,7 @@ // // }}} #include "ucoolib/arch/arch.hh" +#include "ucoolib/common.hh" #include @@ -33,4 +34,17 @@ arch_init (int argc, const char **argv) rcc_clock_setup_hse_3v3 (&hse_8mhz_3v3[CLOCK_3V3_120MHZ]); } +void +halt () +{ + while (1) + ; +} + +void +halt_perror () +{ + halt (); +} + } // namespace ucoo diff --git a/digital/ucoolib/ucoolib/common.hh b/digital/ucoolib/ucoolib/common.hh index a27dfe3b..5be6227f 100644 --- a/digital/ucoolib/ucoolib/common.hh +++ b/digital/ucoolib/ucoolib/common.hh @@ -35,6 +35,41 @@ barrier () __asm__ __volatile__("": : : "memory"); } +/// Stop, abruptly. +void +halt () __attribute__ ((noreturn)); + +/// Stop, try to output error description corresponding to errno. +void +halt_perror () __attribute__ ((noreturn)); + +/// To be used to swear that the given condition is true. If this is not the +/// case... well... you swore! +extern inline void +assert (bool condition) +{ + if (!condition) + halt (); +} + +/// To be used to swear that this code can never be reached. +void +assert_unreachable () __attribute__ ((noreturn)); +extern inline void +assert_unreachable () +{ + halt (); +} + +/// To be used to swear that the given condition is true, but print errno +/// description anyway in case you lied. +extern inline void +assert_perror (bool condition) +{ + if (!condition) + halt_perror (); +} + } // namespace ucoo #endif // ucoolib_common_h -- cgit v1.2.3