From e7eedd234954509f4f5ec52b2d62cbc4a1723936 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 14 Jul 2025 15:39:09 +0000 Subject: libs: begin extraction of kernel std --- libs/kstd/src/mutex.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 libs/kstd/src/mutex.cpp (limited to 'libs/kstd/src') diff --git a/libs/kstd/src/mutex.cpp b/libs/kstd/src/mutex.cpp new file mode 100644 index 0000000..cfb1c84 --- /dev/null +++ b/libs/kstd/src/mutex.cpp @@ -0,0 +1,16 @@ +#include "kstd/mutex.hpp" + +namespace kstd +{ + auto mutex::lock() -> void + { + while (!try_lock()) + { + asm volatile("nop"); + } + } + + auto mutex::try_lock() -> bool { return !locked.exchange(true, std::memory_order_acquire); } + + auto mutex::unlock() -> void { locked.store(false, std::memory_order_release); } +} // namespace kstd -- cgit v1.2.3 From 833738f7023324172dbb0922fe1be9ad9cc88330 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 14 Jul 2025 20:35:38 +0000 Subject: libs: rename kstd headers to be more STL like --- libs/kstd/src/mutex.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libs/kstd/src') diff --git a/libs/kstd/src/mutex.cpp b/libs/kstd/src/mutex.cpp index cfb1c84..137ebc0 100644 --- a/libs/kstd/src/mutex.cpp +++ b/libs/kstd/src/mutex.cpp @@ -1,4 +1,4 @@ -#include "kstd/mutex.hpp" +#include "kstd/mutex" namespace kstd { -- cgit v1.2.3 From 12dedc7e2e51390fdf2caec3700e75db19be1cd4 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Tue, 28 Oct 2025 18:31:39 +0100 Subject: kstd: don't rely on newlib --- libs/kstd/src/bits/os.cpp | 10 ++++++++++ libs/kstd/src/cstdlib.cpp | 18 ++++++++++++++++++ libs/kstd/src/cstring.cpp | 10 ++++++++++ 3 files changed, 38 insertions(+) create mode 100644 libs/kstd/src/bits/os.cpp create mode 100644 libs/kstd/src/cstdlib.cpp create mode 100644 libs/kstd/src/cstring.cpp (limited to 'libs/kstd/src') diff --git a/libs/kstd/src/bits/os.cpp b/libs/kstd/src/bits/os.cpp new file mode 100644 index 0000000..fb64202 --- /dev/null +++ b/libs/kstd/src/bits/os.cpp @@ -0,0 +1,10 @@ +#include "kstd/bits/os.hpp" + +namespace kstd::os +{ + [[gnu::weak]] + auto abort() -> bool + { + return false; + } +} // namespace kstd::os \ No newline at end of file diff --git a/libs/kstd/src/cstdlib.cpp b/libs/kstd/src/cstdlib.cpp new file mode 100644 index 0000000..8fb5f63 --- /dev/null +++ b/libs/kstd/src/cstdlib.cpp @@ -0,0 +1,18 @@ +#include "kstd/bits/os.hpp" + +namespace kstd +{ + + extern "C" + { + [[noreturn]] auto abort() -> void + { + if (!kstd::os::abort()) + { + os::panic("System abort handler returned."); + } + __builtin_trap(); + } + } + +} // namespace kstd \ No newline at end of file diff --git a/libs/kstd/src/cstring.cpp b/libs/kstd/src/cstring.cpp new file mode 100644 index 0000000..5419b7d --- /dev/null +++ b/libs/kstd/src/cstring.cpp @@ -0,0 +1,10 @@ +#include +#include + +extern "C" +{ + auto strlen(const char * string) -> std::size_t + { + return std::distance(string, std::ranges::find(string, nullptr, '\0')); + } +} -- cgit v1.2.3 From acabbacdee68ad80e829bda56ae5363d04646d2d Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 29 Oct 2025 10:39:30 +0100 Subject: kstd: clean up libc implementation --- libs/kstd/src/bits/os.cpp | 6 +++--- libs/kstd/src/cstdlib.cpp | 18 ------------------ libs/kstd/src/cstring.cpp | 10 ---------- libs/kstd/src/libc/stdlib.cpp | 11 +++++++++++ libs/kstd/src/libc/string.cpp | 15 +++++++++++++++ 5 files changed, 29 insertions(+), 31 deletions(-) delete mode 100644 libs/kstd/src/cstdlib.cpp delete mode 100644 libs/kstd/src/cstring.cpp create mode 100644 libs/kstd/src/libc/stdlib.cpp create mode 100644 libs/kstd/src/libc/string.cpp (limited to 'libs/kstd/src') diff --git a/libs/kstd/src/bits/os.cpp b/libs/kstd/src/bits/os.cpp index fb64202..e6edbfd 100644 --- a/libs/kstd/src/bits/os.cpp +++ b/libs/kstd/src/bits/os.cpp @@ -2,9 +2,9 @@ namespace kstd::os { - [[gnu::weak]] - auto abort() -> bool + [[gnu::weak, noreturn]] + auto abort() -> void { - return false; + os::panic("Abort called."); } } // namespace kstd::os \ No newline at end of file diff --git a/libs/kstd/src/cstdlib.cpp b/libs/kstd/src/cstdlib.cpp deleted file mode 100644 index 8fb5f63..0000000 --- a/libs/kstd/src/cstdlib.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "kstd/bits/os.hpp" - -namespace kstd -{ - - extern "C" - { - [[noreturn]] auto abort() -> void - { - if (!kstd::os::abort()) - { - os::panic("System abort handler returned."); - } - __builtin_trap(); - } - } - -} // namespace kstd \ No newline at end of file diff --git a/libs/kstd/src/cstring.cpp b/libs/kstd/src/cstring.cpp deleted file mode 100644 index 5419b7d..0000000 --- a/libs/kstd/src/cstring.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include - -extern "C" -{ - auto strlen(const char * string) -> std::size_t - { - return std::distance(string, std::ranges::find(string, nullptr, '\0')); - } -} diff --git a/libs/kstd/src/libc/stdlib.cpp b/libs/kstd/src/libc/stdlib.cpp new file mode 100644 index 0000000..fe1bc95 --- /dev/null +++ b/libs/kstd/src/libc/stdlib.cpp @@ -0,0 +1,11 @@ +#include "kstd/bits/os.hpp" + +namespace kstd::libc +{ + + extern "C" + { + [[noreturn]] auto abort() -> void { kstd::os::abort(); } + } + +} // namespace kstd::libc \ No newline at end of file diff --git a/libs/kstd/src/libc/string.cpp b/libs/kstd/src/libc/string.cpp new file mode 100644 index 0000000..c6b3847 --- /dev/null +++ b/libs/kstd/src/libc/string.cpp @@ -0,0 +1,15 @@ +#include +#include + +namespace kstd::libc +{ + + extern "C" + { + auto strlen(const char * string) -> std::size_t + { + return std::distance(string, std::ranges::find(string, nullptr, '\0')); + } + } + +} // namespace kstd::libc \ No newline at end of file -- cgit v1.2.3 From b157e2c472d8bd67ac1656404a6a6ee821260f4b Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 29 Oct 2025 15:01:43 +0100 Subject: chore: reformat source code --- libs/kstd/src/libc/stdlib.cpp | 5 ++++- libs/kstd/src/libc/string.cpp | 2 +- libs/kstd/src/mutex.cpp | 10 ++++++++-- 3 files changed, 13 insertions(+), 4 deletions(-) (limited to 'libs/kstd/src') diff --git a/libs/kstd/src/libc/stdlib.cpp b/libs/kstd/src/libc/stdlib.cpp index fe1bc95..752e616 100644 --- a/libs/kstd/src/libc/stdlib.cpp +++ b/libs/kstd/src/libc/stdlib.cpp @@ -5,7 +5,10 @@ namespace kstd::libc extern "C" { - [[noreturn]] auto abort() -> void { kstd::os::abort(); } + [[noreturn]] auto abort() -> void + { + kstd::os::abort(); + } } } // namespace kstd::libc \ No newline at end of file diff --git a/libs/kstd/src/libc/string.cpp b/libs/kstd/src/libc/string.cpp index c6b3847..a42aedc 100644 --- a/libs/kstd/src/libc/string.cpp +++ b/libs/kstd/src/libc/string.cpp @@ -6,7 +6,7 @@ namespace kstd::libc extern "C" { - auto strlen(const char * string) -> std::size_t + auto strlen(char const * string) -> std::size_t { return std::distance(string, std::ranges::find(string, nullptr, '\0')); } diff --git a/libs/kstd/src/mutex.cpp b/libs/kstd/src/mutex.cpp index 137ebc0..da1357f 100644 --- a/libs/kstd/src/mutex.cpp +++ b/libs/kstd/src/mutex.cpp @@ -10,7 +10,13 @@ namespace kstd } } - auto mutex::try_lock() -> bool { return !locked.exchange(true, std::memory_order_acquire); } + auto mutex::try_lock() -> bool + { + return !locked.exchange(true, std::memory_order_acquire); + } - auto mutex::unlock() -> void { locked.store(false, std::memory_order_release); } + auto mutex::unlock() -> void + { + locked.store(false, std::memory_order_release); + } } // namespace kstd -- cgit v1.2.3 From 7b9df8bec5038e0316540d2397df632fb14c9169 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 29 Oct 2025 17:01:22 +0100 Subject: chore: configure clang-tidy --- libs/kstd/src/libc/stdlib.cpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'libs/kstd/src') diff --git a/libs/kstd/src/libc/stdlib.cpp b/libs/kstd/src/libc/stdlib.cpp index 752e616..a0f062a 100644 --- a/libs/kstd/src/libc/stdlib.cpp +++ b/libs/kstd/src/libc/stdlib.cpp @@ -9,6 +9,11 @@ namespace kstd::libc { kstd::os::abort(); } + + [[noreturn, gnu::weak]] auto free(void *) -> void + { + kstd::os::panic("Tried to call free."); + } } } // namespace kstd::libc \ No newline at end of file -- cgit v1.2.3 From fc6d44be383213b7609b5e3c4778e235fb6b00c3 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 5 Dec 2025 18:48:44 +0100 Subject: kstd: implement memcmp --- libs/kstd/src/libc/string.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'libs/kstd/src') diff --git a/libs/kstd/src/libc/string.cpp b/libs/kstd/src/libc/string.cpp index a42aedc..a9b85fc 100644 --- a/libs/kstd/src/libc/string.cpp +++ b/libs/kstd/src/libc/string.cpp @@ -1,5 +1,6 @@ #include #include +#include namespace kstd::libc { @@ -10,6 +11,20 @@ namespace kstd::libc { return std::distance(string, std::ranges::find(string, nullptr, '\0')); } + + auto memcmp(void const * lhs, void const * rhs, std::size_t size) -> std::size_t + { + auto left_span = std::span{static_cast(lhs), size}; + auto right_span = std::span{static_cast(rhs), size}; + auto mismatched = std::ranges::mismatch(left_span, right_span); + + if (mismatched.in1 == left_span.end()) + { + return 0; + } + + return std::bit_cast(*mismatched.in1) - std::bit_cast(*mismatched.in2); + } } } // namespace kstd::libc \ No newline at end of file -- cgit v1.2.3 From 1945dd16716392e70e74efacd19e779f121bd1da Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Mon, 15 Dec 2025 16:46:51 +0100 Subject: chore: fix missing includes --- libs/kstd/src/libc/string.cpp | 2 ++ libs/kstd/src/mutex.cpp | 2 ++ 2 files changed, 4 insertions(+) (limited to 'libs/kstd/src') diff --git a/libs/kstd/src/libc/string.cpp b/libs/kstd/src/libc/string.cpp index a9b85fc..319f6fd 100644 --- a/libs/kstd/src/libc/string.cpp +++ b/libs/kstd/src/libc/string.cpp @@ -1,5 +1,7 @@ #include +#include #include +#include #include namespace kstd::libc diff --git a/libs/kstd/src/mutex.cpp b/libs/kstd/src/mutex.cpp index da1357f..5a26154 100644 --- a/libs/kstd/src/mutex.cpp +++ b/libs/kstd/src/mutex.cpp @@ -1,5 +1,7 @@ #include "kstd/mutex" +#include + namespace kstd { auto mutex::lock() -> void -- cgit v1.2.3