summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Schodet2015-09-07 14:51:59 +0200
committerNicolas Schodet2019-10-07 00:44:50 +0200
commit35d889c3444f3d196e6e0415313282db4d381f02 (patch)
tree191fccc90350aa94dc7eec72b75bdfc1f3b4c400
parentde2813fdf0f80c278261cf6a996077cd380ee25a (diff)
ucoo: add assert message
-rw-r--r--ucoo/arch/arch.host.cc7
-rw-r--r--ucoo/arch/arch.stm32.cc10
-rw-r--r--ucoo/common.hh15
3 files changed, 32 insertions, 0 deletions
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
@@ -63,6 +63,13 @@ halt ()
}
void
+halt (const char *msg)
+{
+ fprintf (stderr, "halt: %s\n", msg);
+ abort ();
+}
+
+void
halt_perror ()
{
perror ("halt");
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
@@ -43,6 +43,16 @@ halt ()
}
void
+halt (const char *msg)
+{
+ volatile const char *halt_message = msg;
+ (void) halt_message;
+ asm volatile ("bkpt");
+ while (1)
+ ;
+}
+
+void
halt_perror ()
{
halt ();
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));