From 974d5eae785025facb2c0037c4ec3aaca1da7b20 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 19 Oct 2025 20:06:33 +0200 Subject: feat: kernel: disk_swap and network improvements. Signed-off-by: Amlal El Mahrouss --- dev/kernel/src/Network/NetworkDevice.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'dev/kernel/src/Network') diff --git a/dev/kernel/src/Network/NetworkDevice.cc b/dev/kernel/src/Network/NetworkDevice.cc index 7f93fa1b..ffdfa53b 100644 --- a/dev/kernel/src/Network/NetworkDevice.cc +++ b/dev/kernel/src/Network/NetworkDevice.cc @@ -11,7 +11,7 @@ namespace Kernel { /// \brief Getter for fNetworkName. /// \return Network device name. const Char* NetworkDevice::Name() const { - return "/devices/net{}"; + return "/devices/net/net{}"; } /// \brief Setter for fNetworkName. -- cgit v1.2.3 From 9c4d2506ccb897045d9fb0c1082ed69ac24ce251 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 22 Oct 2025 11:52:04 +0200 Subject: feat: kernel! new KString API. And Network stack fixes. Signed-off-by: Amlal El Mahrouss --- dev/kernel/NeKit/KString.h | 45 +++++++------- dev/kernel/NeKit/KString.inl | 116 ++++++++++++++++-------------------- dev/kernel/NeKit/Utils.h | 12 ++++ dev/kernel/src/Network/IPAddress.cc | 4 +- 4 files changed, 91 insertions(+), 86 deletions(-) (limited to 'dev/kernel/src/Network') diff --git a/dev/kernel/NeKit/KString.h b/dev/kernel/NeKit/KString.h index bbe49f8e..ec883a2f 100644 --- a/dev/kernel/NeKit/KString.h +++ b/dev/kernel/NeKit/KString.h @@ -16,13 +16,13 @@ namespace Kernel { /// @brief Kernel string class, not dynamic. -template +template class BasicKString final { public: explicit BasicKString() { - fDataSz = MinSz; + fDataSz = kMinimumStringSize; - fData = new Char[fDataSz]; + fData = new CharKind[fDataSz]; MUST_PASS(fData); rt_set_memory(fData, 0, fDataSz); @@ -31,7 +31,7 @@ class BasicKString final { explicit BasicKString(SizeT Sz) : fDataSz(Sz) { MUST_PASS(Sz > 1); - fData = new Char[Sz]; + fData = new CharKind[Sz]; MUST_PASS(fData); rt_set_memory(fData, 0, Sz); @@ -46,18 +46,18 @@ class BasicKString final { NE_COPY_DEFAULT(BasicKString) - Char* Data(); - const Char* CData() const; - Size Length() const; + CharKind* Data(); + const CharKind* CData() const; + Size Length() const; - bool operator==(const Char* rhs) const; - bool operator!=(const Char* rhs) const; + bool operator==(const CharKind* rhs) const; + bool operator!=(const CharKind* rhs) const; - bool operator==(const BasicKString<>& rhs) const; - bool operator!=(const BasicKString<>& rhs) const; + bool operator==(const BasicKString& rhs) const; + bool operator!=(const BasicKString& rhs) const; - BasicKString<>& operator+=(const Char* rhs); - BasicKString<>& operator+=(const BasicKString<>& rhs); + BasicKString& operator+=(const CharKind* rhs); + BasicKString& operator+=(const BasicKString& rhs); operator const char*() { return fData; } @@ -66,9 +66,9 @@ class BasicKString final { bool operator!() { return fData; } private: - Char* fData{nullptr}; - Size fDataSz{0}; - Size fCur{0}; + CharKind* fData{nullptr}; + Size fDataSz{0}; + Size fCur{0}; friend class KStringBuilder; }; @@ -78,11 +78,14 @@ using KStringOr = ErrorOr; class KStringBuilder final { public: - static ErrorOr Construct(const Char* data); - static const Char* FromBool(const Char* fmt, bool n); - static const Char* Format(const Char* fmt, const Char* from); - static bool Equals(const Char* lhs, const Char* rhs); - static bool Equals(const Utf8Char* lhs, const Utf8Char* rhs); + template + static ErrorOr> Construct(const CharKind* data); + template + static const CharKind* FromBool(const CharKind* fmt, bool n); + template + static const CharKind* Format(const CharKind* fmt, const CharKind* from); + template + static bool Equals(const CharKind* lhs, const CharKind* rhs); }; } // namespace Kernel diff --git a/dev/kernel/NeKit/KString.inl b/dev/kernel/NeKit/KString.inl index 1faefb08..4bb48415 100644 --- a/dev/kernel/NeKit/KString.inl +++ b/dev/kernel/NeKit/KString.inl @@ -4,14 +4,13 @@ ------------------------------------------- */ -#include - -/// @file BasicKString<>.cc +/// @file BasicKString.inl /// @brief Kernel String manipulation file. namespace Kernel { -inline void rt_string_append(Char* lhs, const Char* rhs, Int32 cur) { - SizeT sz_rhs = rt_string_len(rhs); +template +inline void ort_string_append(CharKind* lhs, const CharKind* rhs, Int32 cur) { + SizeT sz_rhs = ort_string_len(rhs); SizeT rhs_i = 0; for (; rhs_i < sz_rhs; ++rhs_i) { @@ -19,23 +18,23 @@ inline void rt_string_append(Char* lhs, const Char* rhs, Int32 cur) { } } -template <> -inline Char* BasicKString<>::Data() { +template +inline CharKind* BasicKString::Data() { return this->fData; } -template <> -inline const Char* BasicKString<>::CData() const { - return const_cast(this->fData); +template +inline const CharKind* BasicKString::CData() const { + return const_cast(this->fData); } -template <> -inline SizeT BasicKString<>::Length() const { +template +inline SizeT BasicKString::Length() const { return this->fDataSz; } -template <> -inline bool BasicKString<>::operator==(const BasicKString<>& rhs) const { +template +inline bool BasicKString::operator==(const BasicKString& rhs) const { if (rhs.Length() != this->Length()) return false; for (Size index = 0; index < this->Length(); ++index) { @@ -45,19 +44,19 @@ inline bool BasicKString<>::operator==(const BasicKString<>& rhs) const { return true; } -template <> -inline bool BasicKString<>::operator==(const Char* rhs) const { - if (rt_string_len(rhs) != this->Length()) return false; +template +inline bool BasicKString::operator==(const CharKind* rhs) const { + if (ort_string_len(rhs) != this->Length()) return false; - for (Size index = 0; index < rt_string_len(rhs); ++index) { + for (Size index = 0; index < ort_string_len(rhs); ++index) { if (rhs[index] != this->fData[index]) return false; } return true; } -template <> -inline bool BasicKString<>::operator!=(const BasicKString<>& rhs) const { +template +inline bool BasicKString::operator!=(const BasicKString& rhs) const { if (rhs.Length() != this->Length()) return false; for (Size index = 0; index < rhs.Length(); ++index) { @@ -67,54 +66,55 @@ inline bool BasicKString<>::operator!=(const BasicKString<>& rhs) const { return true; } -template <> -inline bool BasicKString<>::operator!=(const Char* rhs) const { - if (rt_string_len(rhs) != this->Length()) return false; +template +inline bool BasicKString::operator!=(const CharKind* rhs) const { + if (ort_string_len(rhs) != this->Length()) return false; - for (Size index = 0; index < rt_string_len(rhs); ++index) { + for (Size index = 0; index < ort_string_len(rhs); ++index) { if (rhs[index] == this->fData[index]) return false; } return true; } -template <> -inline BasicKString<>& BasicKString<>::operator+=(const BasicKString<>& rhs) { - if (rt_string_len(rhs.fData) > this->Length()) return *this; +template +inline BasicKString& BasicKString::operator+=(const BasicKString& rhs) { + if (ort_string_len(rhs.fData) > this->Length()) return *this; - rt_string_append(this->fData, const_cast(rhs.fData), this->fCur); - this->fCur += rt_string_len(const_cast(rhs.fData)); + ort_string_append(this->fData, const_cast(rhs.fData), this->fCur); + this->fCur += ort_string_len(const_cast(rhs.fData)); return *this; } -template <> -inline BasicKString<>& BasicKString<>::operator+=(const Char* rhs) { - rt_string_append(this->fData, const_cast(rhs), this->fCur); - this->fCur += rt_string_len(const_cast(rhs)); +template +inline BasicKString& BasicKString::operator+=(const CharKind* rhs) { + ort_string_append(this->fData, const_cast(rhs), this->fCur); + this->fCur += ort_string_len(const_cast(rhs)); return *this; } -inline ErrorOr> KStringBuilder::Construct(const Char* data) { - if (!data || *data == 0) return ErrorOr>(new BasicKString<>(0)); +template +inline ErrorOr> KStringBuilder::Construct(const CharKind* data) { + if (!data || *data == 0) return ErrorOr>(nullptr); - BasicKString<>* view = new BasicKString<>(rt_string_len(data)); + BasicKString* view = new BasicKString(ort_string_len(data)); (*view) += data; - return ErrorOr>(*view); + return ErrorOr>(*view); } - -inline const Char* KStringBuilder::FromBool(const Char* fmt, bool i) { +template +inline const CharKind* KStringBuilder::FromBool(const CharKind* fmt, bool i) { if (!fmt) return ("?"); - const Char* boolean_expr = i ? "YES" : "NO"; - Char* ret = (Char*) RTL_ALLOCA(rt_string_len(boolean_expr) + rt_string_len(fmt)); + const CharKind* boolean_expr = i ? "YES" : "NO"; + CharKind* ret = (CharKind*) RTL_ALLOCA(ort_string_len(boolean_expr) + ort_string_len(fmt)); if (!ret) return ("?"); - const auto fmt_len = rt_string_len(fmt); - const auto res_len = rt_string_len(boolean_expr); + const auto fmt_len = ort_string_len(fmt); + const auto res_len = ort_string_len(boolean_expr); for (Size idx = 0; idx < fmt_len; ++idx) { if (fmt[idx] == '%') { @@ -133,41 +133,31 @@ inline const Char* KStringBuilder::FromBool(const Char* fmt, bool i) { return ret; } +template +inline bool KStringBuilder::Equals(const CharKind* lhs, const CharKind* rhs) { + if (ort_string_len(rhs) != ort_string_len(lhs)) return false; -inline bool KStringBuilder::Equals(const Char* lhs, const Char* rhs) { - if (rt_string_len(rhs) != rt_string_len(lhs)) return false; - - for (Size index = 0; index < rt_string_len(rhs); ++index) { + for (Size index = 0; index < ort_string_len(rhs); ++index) { if (rhs[index] != lhs[index]) return false; } return true; } - -inline bool KStringBuilder::Equals(const Utf8Char* lhs, const Utf8Char* rhs) { - if (urt_string_len(rhs) != urt_string_len(lhs)) return false; - - for (Size index = 0; rhs[index] != 0; ++index) { - if (rhs[index] != lhs[index]) return false; - } - - return true; -} - -inline const Char* KStringBuilder::Format(const Char* fmt, const Char* fmt2) { +template +inline const CharKind* KStringBuilder::Format(const CharKind* fmt, const CharKind* fmt2) { if (!fmt || !fmt2) return ("?"); - Char* ret = (Char*) RTL_ALLOCA(sizeof(char) * (rt_string_len(fmt2) + rt_string_len(fmt))); + CharKind* ret = (CharKind*) RTL_ALLOCA(sizeof(char) * (ort_string_len(fmt2) + ort_string_len(fmt))); if (!ret) return ("?"); - const auto len = rt_string_len(fmt); + const auto len = ort_string_len(fmt); for (Size idx = 0; idx < len; ++idx) { - if (fmt[idx] == '%' && idx < rt_string_len(fmt) && fmt[idx] == 's') { + if (fmt[idx] == '%' && idx < ort_string_len(fmt) && fmt[idx] == 's') { Size result_cnt = idx; - for (Size y_idx = 0; y_idx < rt_string_len(fmt2); ++y_idx) { + for (Size y_idx = 0; y_idx < ort_string_len(fmt2); ++y_idx) { ret[result_cnt] = fmt2[y_idx]; ++result_cnt; } diff --git a/dev/kernel/NeKit/Utils.h b/dev/kernel/NeKit/Utils.h index a7576e77..ee18d04c 100644 --- a/dev/kernel/NeKit/Utils.h +++ b/dev/kernel/NeKit/Utils.h @@ -38,4 +38,16 @@ Int urt_string_cmp(const Utf8Char* src, const Utf8Char* cmp, Size len); Void urt_set_memory(const voidPtr src, UInt32 dst, Size len); Int urt_copy_memory(const voidPtr src, voidPtr dst, Size len); Size urt_string_len(const Utf8Char* str); + +/// OpenTemplate UTILS API + +template +inline SizeT ort_string_len(const CharType* str) { + if (!str) return 0; + + SizeT len{0}; + + while (str[len] != 0) ++len; + return len; +} } // namespace Kernel diff --git a/dev/kernel/src/Network/IPAddress.cc b/dev/kernel/src/Network/IPAddress.cc index b02eae08..bc46292b 100644 --- a/dev/kernel/src/Network/IPAddress.cc +++ b/dev/kernel/src/Network/IPAddress.cc @@ -73,14 +73,14 @@ bool RawIPAddress6::operator!=(const RawIPAddress6& ipv6) { /// @todo ErrorOr IPFactory::ToKString(Ref& ipv6) { NE_UNUSED(ipv6); - auto str = KStringBuilder::Construct(0); + auto str = KStringBuilder::Construct(""); return str; } /// @todo ErrorOr IPFactory::ToKString(Ref& ipv4) { NE_UNUSED(ipv4); - auto str = KStringBuilder::Construct(0); + auto str = KStringBuilder::Construct(""); return str; } -- cgit v1.2.3