summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--digital/ucoolib/ucoolib/arch/arch.host.cc17
-rw-r--r--digital/ucoolib/ucoolib/arch/arch.stm32.cc14
-rw-r--r--digital/ucoolib/ucoolib/common.hh35
3 files changed, 66 insertions, 0 deletions
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 <cstdlib>
+#include <cstdio>
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 <libopencm3/stm32/f4/rcc.h>
@@ -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