From 1225085ee11caacf3db365a440800bcc48cece01 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Tue, 29 Nov 2022 17:19:03 +0400 Subject: [PATCH] Move panic/assert to separate module --- src/Makefile | 2 +- src/main.c | 20 +++----------------- src/panic.c | 16 ++++++++++++++++ src/panic.h | 11 +++++++++++ 4 files changed, 31 insertions(+), 18 deletions(-) create mode 100644 src/panic.c create mode 100644 src/panic.h diff --git a/src/Makefile b/src/Makefile index d9abf7d..31c0dea 100644 --- a/src/Makefile +++ b/src/Makefile @@ -20,7 +20,7 @@ CPPFLAGS = \ $(MRUBY_FLAGS) \ -DKERNAUX_DEBUG \ -OBJS = start.S.o main.c.o libc.c.o logger.c.o +OBJS = start.S.o main.c.o libc.c.o logger.c.o panic.c.o all: $(MRUBYVISOR) diff --git a/src/main.c b/src/main.c index 3c475af..18221b3 100644 --- a/src/main.c +++ b/src/main.c @@ -1,13 +1,12 @@ #include "libc.h" #include "logger.h" +#include "panic.h" #include #include #include -#include #include -#include #include #include @@ -15,14 +14,9 @@ #include #include -#define PANIC(msg) (assert(__FILE__, __LINE__, msg)) -#define ASSERT(cond) ((cond) ? (void)0 : PANIC(#cond)) - static mrb_state *mrb = NULL; static mrbc_context *context = NULL; -static void assert(const char *file, int line, const char *str); - static bool load_module(const char *source, size_t size, const char *cmdline); static mrb_value ruby_console_puts(mrb_state *mrb, mrb_value self); @@ -31,12 +25,10 @@ void main( const uint32_t multiboot2_info_magic, const struct KernAux_Multiboot2_Info *const multiboot2_info ) { - kernaux_assert_cb = assert; - - ASSERT(multiboot2_info_magic == KERNAUX_MULTIBOOT2_INFO_MAGIC); - + panic_init(); libc_init(); + ASSERT(multiboot2_info_magic == KERNAUX_MULTIBOOT2_INFO_MAGIC); ASSERT(mrb = mrb_open()); ASSERT(context = mrbc_context_new(mrb)); @@ -74,12 +66,6 @@ void main( } } -void assert(const char *const file, const int line, const char *const str) -{ - logger_assert(file, line, str); - kernaux_drivers_shutdown_poweroff(); -} - bool load_module( const char *const source, const size_t size, diff --git a/src/panic.c b/src/panic.c new file mode 100644 index 0000000..b5c64cb --- /dev/null +++ b/src/panic.c @@ -0,0 +1,16 @@ +#include "logger.h" +#include "panic.h" + +#include +#include + +void panic_init() +{ + kernaux_assert_cb = assert; +} + +void assert(const char *const file, const int line, const char *const str) +{ + logger_assert(file, line, str); + kernaux_drivers_shutdown_poweroff(); +} diff --git a/src/panic.h b/src/panic.h new file mode 100644 index 0000000..1254bfb --- /dev/null +++ b/src/panic.h @@ -0,0 +1,11 @@ +#ifndef INCLUDED_PANIC +#define INCLUDED_LOGGERPANIC + +#define PANIC(msg) (assert(__FILE__, __LINE__, msg)) +#define ASSERT(cond) ((cond) ? (void)0 : PANIC(#cond)) + +void panic_init(); + +void assert(const char *file, int line, const char *str); + +#endif