From c63a0f8e7ed7a2d88cb24a46a6fd8e409f35756b Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 10 Sep 2026 16:38:41 +0200 Subject: kapi/system: reduce panic buffer size Previously, we allocated a 512-byte buffer for every call of the formatted panic function. This was heavily increasing the stack size for a number of functions. We now don't print to a statically allocated buffer, but rather use the iterator-based formatting facility and a proxy iterator with a small, self-flushing buffer. This reduces that static overhead of the formatted panic function to by almost 500 bytes, while increasing possible message fidelity, since we are not longer arbitrarily limited by the buffer. --- kernel/kapi/system.cpp | 90 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 85 insertions(+), 5 deletions(-) (limited to 'kernel') diff --git a/kernel/kapi/system.cpp b/kernel/kapi/system.cpp index 629e5990..34601b9d 100644 --- a/kernel/kapi/system.cpp +++ b/kernel/kapi/system.cpp @@ -11,19 +11,99 @@ namespace kapi::system { + namespace + { + + auto panic_write(kstd::error_code error) -> void + { + kstd::print(kstd::print_sink::stderr, " ({}) ", error); + } + + } // namespace + + namespace detail + { + + panic_buffer::~panic_buffer() + { + flush(); + } + + void panic_buffer::append(char character) + { + m_buffer[m_written++] = character; + if (m_written == m_buffer.size()) + { + flush(); + } + } + + void panic_buffer::flush() + { + if (m_written > 0) + { + panic_write(std::string_view{m_buffer.data(), m_written}); + m_written = 0; + } + } + + panic_iterator::panic_iterator(panic_buffer & buffer) + : m_buffer(&buffer) + {} + + auto panic_iterator::operator*() noexcept -> panic_iterator & + { + return *this; + } + + auto panic_iterator::operator=(char character) -> panic_iterator & + { + m_buffer->append(character); + return *this; + } + + auto panic_iterator::operator++() -> panic_iterator & + { + return *this; + } + + auto panic_iterator::operator++(int) -> panic_iterator + { + return *this; + } + + } // namespace detail + [[gnu::weak]] auto panic(std::string_view message, std::source_location location) -> void { - kstd::println(kstd::print_sink::stderr, "[PANIC] in {} : {} @ {}:{}", location.function_name(), message, - location.file_name(), location.line()); - cpu::halt(); + panic_start(location); + panic_write(message); + panic_finish(location); } [[gnu::weak]] auto panic(std::string_view message, kstd::error_code error, std::source_location location) -> void { - kstd::println(kstd::print_sink::stderr, "[PANIC] in {} : {} ({}) @ {}:{}", location.function_name(), message, error, - location.file_name(), location.line()); + panic_start(location); + panic_write(message); + panic_write(error); + panic_finish(location); + } + + auto panic_start(std::source_location location) -> void + { + kstd::print(kstd::print_sink::stderr, "[PANIC] in '{}': ", location.function_name()); + } + + auto panic_write(std::string_view message) -> void + { + kstd::print(kstd::print_sink::stderr, "{}", message); + } + + auto panic_finish(std::source_location location) -> void + { + kstd::println(kstd::print_sink::stderr, " @ {}:{}", location.file_name(), location.line()); cpu::halt(); } -- cgit v1.2.3