From b456c928fecf78157270b052e441a427f06afdda Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sat, 29 Nov 2025 21:38:33 -0500 Subject: feat: kernel: Introduce Vet.h, better Ref.h, and more improved containers for better kernel code. Signed-off-by: Amlal El Mahrouss --- src/kernel/NeKit/Array.h | 5 +- src/kernel/NeKit/ArrayList.h | 2 +- src/kernel/NeKit/Atom.h | 2 +- src/kernel/NeKit/Config.h | 179 ++++++++++++++++++++++++++++++++++++++++ src/kernel/NeKit/Crc32.h | 2 +- src/kernel/NeKit/CxxAbi.h | 2 +- src/kernel/NeKit/Defines.h | 179 ---------------------------------------- src/kernel/NeKit/ErrorOr.h | 2 +- src/kernel/NeKit/Function.h | 2 +- src/kernel/NeKit/Json.h | 2 +- src/kernel/NeKit/KString.h | 2 +- src/kernel/NeKit/KernelPanic.h | 2 +- src/kernel/NeKit/MutableArray.h | 2 +- src/kernel/NeKit/OwnPtr.h | 15 ++-- src/kernel/NeKit/PageMgr.h | 2 +- src/kernel/NeKit/Pair.h | 2 +- src/kernel/NeKit/Ref.h | 5 +- src/kernel/NeKit/Stream.h | 2 +- src/kernel/NeKit/Utils.h | 2 +- src/kernel/NeKit/Variant.h | 2 +- src/kernel/NeKit/Vet.h | 34 ++++++++ 21 files changed, 240 insertions(+), 207 deletions(-) create mode 100644 src/kernel/NeKit/Config.h delete mode 100644 src/kernel/NeKit/Defines.h create mode 100644 src/kernel/NeKit/Vet.h (limited to 'src/kernel/NeKit') diff --git a/src/kernel/NeKit/Array.h b/src/kernel/NeKit/Array.h index 5b8371db..f4673b68 100644 --- a/src/kernel/NeKit/Array.h +++ b/src/kernel/NeKit/Array.h @@ -7,7 +7,7 @@ #pragma once #include -#include +#include #include namespace Kernel { @@ -27,8 +27,7 @@ class Array final { SizeT Capacity() { return N; } SizeT Count() { - const static SizeT kArrCnt = N; - return kArrCnt; // avoid constexpr error. + return N; // avoid constexpr error. } const T* CData() { return fArray; } diff --git a/src/kernel/NeKit/ArrayList.h b/src/kernel/NeKit/ArrayList.h index 54613b67..d3225a50 100644 --- a/src/kernel/NeKit/ArrayList.h +++ b/src/kernel/NeKit/ArrayList.h @@ -6,7 +6,7 @@ #pragma once -#include +#include namespace Kernel { template diff --git a/src/kernel/NeKit/Atom.h b/src/kernel/NeKit/Atom.h index 0f8eefbc..51808a0a 100644 --- a/src/kernel/NeKit/Atom.h +++ b/src/kernel/NeKit/Atom.h @@ -5,7 +5,7 @@ ======================================== */ #pragma once -#include +#include namespace Kernel { template diff --git a/src/kernel/NeKit/Config.h b/src/kernel/NeKit/Config.h new file mode 100644 index 00000000..e53616a9 --- /dev/null +++ b/src/kernel/NeKit/Config.h @@ -0,0 +1,179 @@ +/* ======================================== + + Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. + +======================================== */ + +#pragma once + +#include + +#define NEKIT_VERSION_STR "0.0.1" +#define NEKIT_VERSION_BCD 0x0001 + +#ifndef __cplusplus +#error !!! Kernel compiles only with a C++ compiler. !!! +#endif + +#if __cplusplus <= 201703L +#define char8_t char +#endif + +#ifdef __has_feature +#if !__has_feature(cxx_nullptr) +#if !__has_nullptr +#error !!! You must at least have nullptr featured on your C++ compiler. !!! +#endif +#endif +#endif + +/// @brief The **Kernel** namespace. +namespace Kernel { +using voidPtr = void*; +using VoidPtr = void*; +using nullPtr = decltype(nullptr); +using NullPtr = decltype(nullptr); + +using Int = int; +using Int32 = __INT32_TYPE__; +using UShort = __UINT16_TYPE__; +using UInt16 = __UINT16_TYPE__; +using Short = short; +using Int16 = __INT16_TYPE__; +using UInt = __UINT32_TYPE__; +using UInt32 = __UINT32_TYPE__; +using Long = __INT64_TYPE__; +using Int64 = __INT64_TYPE__; +using ULong = __UINT64_TYPE__; +using UInt64 = __UINT64_TYPE__; +using Boolean = bool; +using Bool = bool; +using Char = char; +using Int8 = __INT8_TYPE__; +using Char8 = char8_t; +using UChar = __UINT8_TYPE__; +using UInt8 = __UINT8_TYPE__; + +using SSize = long; +using SSizeT = long; +using Size = __SIZE_TYPE__; +using SizeT = __SIZE_TYPE__; +using IntPtr = __INTPTR_TYPE__; +using UIntPtr = __UINTPTR_TYPE__; +using IntFast = __INT_FAST32_TYPE__; +using IntFast64 = __INT_FAST64_TYPE__; +using PtrDiff = __PTRDIFF_TYPE__; + +using SInt16 = Int16; +using SInt32 = Int32; +using SInt64 = Int64; + +typedef UIntPtr* Ptr64; +typedef UInt32* Ptr32; +typedef UInt8* Ptr8; + +using Utf8Char = char8_t; +using Utf16Char = char16_t; +using WideChar = wchar_t; +using Utf32Char = char32_t; + +using LongDouble = long double; +using Double = double; +using Float = float; + +typedef UInt32 PhysicalAddressKind; +typedef UIntPtr VirtualAddressKind; + +using Void = void; +using Any = void*; + +using Lba = UInt64; + +using Char16 = char16_t; + +enum class Endian : UInt8 { kEndianInvalid, kEndianBig, kEndianLittle, kEndianMixed, kEndianCount }; + +/// @brief Forward object. +/// @tparam Args the object type. +/// @param arg the object. +/// @return object's rvalue +template +inline Args&& forward(Args& arg) { + return static_cast(arg); +} + +/// @brief Move object. +/// @tparam Args the object type. +/// @param arg the object. +/// @return object's rvalue +template +inline Args&& move(Args&& arg) { + return static_cast(arg); +} + +/// @brief Encoding interface, used as a proxy to convert T to Char* +/// Used to cast A to B or B to A. +class ICodec { + public: + explicit ICodec() = default; + virtual ~ICodec() = default; + + ICodec& operator=(const ICodec&) = default; + ICodec(const ICodec&) = default; + + public: + /// @brief Convert type to bytes. + /// @tparam T the type. + /// @param type (a1) the data. + /// @return a1 as Char* + template + const Char* AsBytes(T type) { + NE_UNUSED(type); + return nullptr; + } + + /// @brief Construct from type to class. + /// @tparam T the type to convert. + /// @param type (a1) the data. + /// @return a1 as Char* + template + OutputClass* Construct(Char* type) { + FactoryClass class_fac; + return class_fac.template From(type); + } + + /// @brief Convert T class to Y class. + /// @tparam T the class type of type. + /// @tparam Y the result class. + /// @param type the class to cast. + /// @return the class as Y. + template + Y As(T type) { + if (type.template IsSerializable()) { + return reinterpret_cast(type); + } + + return type.template As(); + } +}; + +/// \brief Scheduler interface, represents a scheduler object. +/// @note This is used to schedule tasks, such as threads, drivers, user threads, etc. +class ISchedulable { + public: + explicit ISchedulable() = default; + virtual ~ISchedulable() = default; + + ISchedulable& operator=(const ISchedulable&) = default; + ISchedulable(const ISchedulable&) = default; + + /// @brief Is this object only accepting user tasks? + virtual Bool IsUser() { return NO; } + + /// @brief Is this object only accepting kernel tasks? + virtual Bool IsKernel() { return NO; } + + /// @brief Is this object offloading to another CPU? + virtual Bool HasMP() { return NO; } +}; +} // namespace Kernel diff --git a/src/kernel/NeKit/Crc32.h b/src/kernel/NeKit/Crc32.h index 125ee5b7..ecec9519 100644 --- a/src/kernel/NeKit/Crc32.h +++ b/src/kernel/NeKit/Crc32.h @@ -11,7 +11,7 @@ #ifndef NEKIT_CRC32_H #define NEKIT_CRC32_H -#include +#include namespace Kernel { UInt32 ke_calculate_crc32(const VoidPtr crc, Int32 len); diff --git a/src/kernel/NeKit/CxxAbi.h b/src/kernel/NeKit/CxxAbi.h index 7b13d6b3..41956d3f 100644 --- a/src/kernel/NeKit/CxxAbi.h +++ b/src/kernel/NeKit/CxxAbi.h @@ -5,7 +5,7 @@ ======================================== */ #pragma once -#include +#include #ifndef __NECTI__ diff --git a/src/kernel/NeKit/Defines.h b/src/kernel/NeKit/Defines.h deleted file mode 100644 index e53616a9..00000000 --- a/src/kernel/NeKit/Defines.h +++ /dev/null @@ -1,179 +0,0 @@ -/* ======================================== - - Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. - -======================================== */ - -#pragma once - -#include - -#define NEKIT_VERSION_STR "0.0.1" -#define NEKIT_VERSION_BCD 0x0001 - -#ifndef __cplusplus -#error !!! Kernel compiles only with a C++ compiler. !!! -#endif - -#if __cplusplus <= 201703L -#define char8_t char -#endif - -#ifdef __has_feature -#if !__has_feature(cxx_nullptr) -#if !__has_nullptr -#error !!! You must at least have nullptr featured on your C++ compiler. !!! -#endif -#endif -#endif - -/// @brief The **Kernel** namespace. -namespace Kernel { -using voidPtr = void*; -using VoidPtr = void*; -using nullPtr = decltype(nullptr); -using NullPtr = decltype(nullptr); - -using Int = int; -using Int32 = __INT32_TYPE__; -using UShort = __UINT16_TYPE__; -using UInt16 = __UINT16_TYPE__; -using Short = short; -using Int16 = __INT16_TYPE__; -using UInt = __UINT32_TYPE__; -using UInt32 = __UINT32_TYPE__; -using Long = __INT64_TYPE__; -using Int64 = __INT64_TYPE__; -using ULong = __UINT64_TYPE__; -using UInt64 = __UINT64_TYPE__; -using Boolean = bool; -using Bool = bool; -using Char = char; -using Int8 = __INT8_TYPE__; -using Char8 = char8_t; -using UChar = __UINT8_TYPE__; -using UInt8 = __UINT8_TYPE__; - -using SSize = long; -using SSizeT = long; -using Size = __SIZE_TYPE__; -using SizeT = __SIZE_TYPE__; -using IntPtr = __INTPTR_TYPE__; -using UIntPtr = __UINTPTR_TYPE__; -using IntFast = __INT_FAST32_TYPE__; -using IntFast64 = __INT_FAST64_TYPE__; -using PtrDiff = __PTRDIFF_TYPE__; - -using SInt16 = Int16; -using SInt32 = Int32; -using SInt64 = Int64; - -typedef UIntPtr* Ptr64; -typedef UInt32* Ptr32; -typedef UInt8* Ptr8; - -using Utf8Char = char8_t; -using Utf16Char = char16_t; -using WideChar = wchar_t; -using Utf32Char = char32_t; - -using LongDouble = long double; -using Double = double; -using Float = float; - -typedef UInt32 PhysicalAddressKind; -typedef UIntPtr VirtualAddressKind; - -using Void = void; -using Any = void*; - -using Lba = UInt64; - -using Char16 = char16_t; - -enum class Endian : UInt8 { kEndianInvalid, kEndianBig, kEndianLittle, kEndianMixed, kEndianCount }; - -/// @brief Forward object. -/// @tparam Args the object type. -/// @param arg the object. -/// @return object's rvalue -template -inline Args&& forward(Args& arg) { - return static_cast(arg); -} - -/// @brief Move object. -/// @tparam Args the object type. -/// @param arg the object. -/// @return object's rvalue -template -inline Args&& move(Args&& arg) { - return static_cast(arg); -} - -/// @brief Encoding interface, used as a proxy to convert T to Char* -/// Used to cast A to B or B to A. -class ICodec { - public: - explicit ICodec() = default; - virtual ~ICodec() = default; - - ICodec& operator=(const ICodec&) = default; - ICodec(const ICodec&) = default; - - public: - /// @brief Convert type to bytes. - /// @tparam T the type. - /// @param type (a1) the data. - /// @return a1 as Char* - template - const Char* AsBytes(T type) { - NE_UNUSED(type); - return nullptr; - } - - /// @brief Construct from type to class. - /// @tparam T the type to convert. - /// @param type (a1) the data. - /// @return a1 as Char* - template - OutputClass* Construct(Char* type) { - FactoryClass class_fac; - return class_fac.template From(type); - } - - /// @brief Convert T class to Y class. - /// @tparam T the class type of type. - /// @tparam Y the result class. - /// @param type the class to cast. - /// @return the class as Y. - template - Y As(T type) { - if (type.template IsSerializable()) { - return reinterpret_cast(type); - } - - return type.template As(); - } -}; - -/// \brief Scheduler interface, represents a scheduler object. -/// @note This is used to schedule tasks, such as threads, drivers, user threads, etc. -class ISchedulable { - public: - explicit ISchedulable() = default; - virtual ~ISchedulable() = default; - - ISchedulable& operator=(const ISchedulable&) = default; - ISchedulable(const ISchedulable&) = default; - - /// @brief Is this object only accepting user tasks? - virtual Bool IsUser() { return NO; } - - /// @brief Is this object only accepting kernel tasks? - virtual Bool IsKernel() { return NO; } - - /// @brief Is this object offloading to another CPU? - virtual Bool HasMP() { return NO; } -}; -} // namespace Kernel diff --git a/src/kernel/NeKit/ErrorOr.h b/src/kernel/NeKit/ErrorOr.h index d930fe17..f0740488 100644 --- a/src/kernel/NeKit/ErrorOr.h +++ b/src/kernel/NeKit/ErrorOr.h @@ -9,7 +9,7 @@ #pragma once -#include +#include #include namespace Kernel { diff --git a/src/kernel/NeKit/Function.h b/src/kernel/NeKit/Function.h index 5dea8f85..1ceda345 100644 --- a/src/kernel/NeKit/Function.h +++ b/src/kernel/NeKit/Function.h @@ -6,7 +6,7 @@ #pragma once -#include +#include #include namespace Kernel { diff --git a/src/kernel/NeKit/Json.h b/src/kernel/NeKit/Json.h index 82ce3b99..e6106a2b 100644 --- a/src/kernel/NeKit/Json.h +++ b/src/kernel/NeKit/Json.h @@ -10,7 +10,7 @@ /// @brief Kernel JSON API. #include -#include +#include #include #include #include diff --git a/src/kernel/NeKit/KString.h b/src/kernel/NeKit/KString.h index fa83fed4..fa0ebcbf 100644 --- a/src/kernel/NeKit/KString.h +++ b/src/kernel/NeKit/KString.h @@ -7,7 +7,7 @@ #pragma once #include -#include +#include #include #include #include diff --git a/src/kernel/NeKit/KernelPanic.h b/src/kernel/NeKit/KernelPanic.h index f716e6de..cba8897c 100644 --- a/src/kernel/NeKit/KernelPanic.h +++ b/src/kernel/NeKit/KernelPanic.h @@ -7,7 +7,7 @@ #pragma once -#include +#include namespace Kernel { void ke_runtime_check(bool expr, const Char* file, const Char* line); diff --git a/src/kernel/NeKit/MutableArray.h b/src/kernel/NeKit/MutableArray.h index e14afbfe..6dafa5b9 100644 --- a/src/kernel/NeKit/MutableArray.h +++ b/src/kernel/NeKit/MutableArray.h @@ -7,7 +7,7 @@ #include #include -#include +#include #define RTL_TRY_FIND_NODE(NAME, NODE) \ auto* NAME = NODE; \ diff --git a/src/kernel/NeKit/OwnPtr.h b/src/kernel/NeKit/OwnPtr.h index bec2813b..30c8f9eb 100644 --- a/src/kernel/NeKit/OwnPtr.h +++ b/src/kernel/NeKit/OwnPtr.h @@ -7,7 +7,7 @@ #pragma once -#include +#include #include #include @@ -21,8 +21,8 @@ class NonNullRefPtr; template class OwnPtr final { public: - OwnPtr() {} - ~OwnPtr() { this->Delete(); } + OwnPtr() = default; + ~OwnPtr() { this->Reset(); } OwnPtr& operator=(const OwnPtr&) = default; OwnPtr(const OwnPtr&) = default; @@ -38,9 +38,8 @@ class OwnPtr final { return fCls; } - void Delete() { + void Reset() { if (fCls) delete fCls; - fCls = nullptr; } @@ -50,15 +49,15 @@ class OwnPtr final { Ref AsRef() { return Ref(fCls); } - operator bool() { return fCls; } + explicit operator bool() { return fCls; } bool operator!() { return !fCls; } private: - T* fCls; + T* fCls{nullptr}; }; template -inline OwnPtr mm_make_own_ptr(Args... args) { +inline OwnPtr make_ptr(Args... args) { OwnPtr ret; ret.template New(forward(args)...); diff --git a/src/kernel/NeKit/PageMgr.h b/src/kernel/NeKit/PageMgr.h index 6cdd5a5c..1bd489af 100644 --- a/src/kernel/NeKit/PageMgr.h +++ b/src/kernel/NeKit/PageMgr.h @@ -9,7 +9,7 @@ #pragma once -#include +#include #include namespace Kernel { diff --git a/src/kernel/NeKit/Pair.h b/src/kernel/NeKit/Pair.h index c8914ec6..6c67d508 100644 --- a/src/kernel/NeKit/Pair.h +++ b/src/kernel/NeKit/Pair.h @@ -7,7 +7,7 @@ #pragma once #include -#include +#include #include namespace Kernel { diff --git a/src/kernel/NeKit/Ref.h b/src/kernel/NeKit/Ref.h index 59991442..ff8731e3 100644 --- a/src/kernel/NeKit/Ref.h +++ b/src/kernel/NeKit/Ref.h @@ -10,8 +10,9 @@ #include #include -#include +#include #include +#include namespace Kernel { /// =========================================================== /// @@ -43,7 +44,7 @@ class Ref final { T operator*() { return fClass; } - operator bool() { return true; } + operator bool() { return Vettable::kValue; } private: T fClass; diff --git a/src/kernel/NeKit/Stream.h b/src/kernel/NeKit/Stream.h index 1a53e7f0..44b98e35 100644 --- a/src/kernel/NeKit/Stream.h +++ b/src/kernel/NeKit/Stream.h @@ -7,7 +7,7 @@ #pragma once -#include +#include #include namespace Kernel { diff --git a/src/kernel/NeKit/Utils.h b/src/kernel/NeKit/Utils.h index caabd2af..28a153af 100644 --- a/src/kernel/NeKit/Utils.h +++ b/src/kernel/NeKit/Utils.h @@ -6,7 +6,7 @@ #pragma once -#include +#include namespace Kernel { /// =========================================================== /// diff --git a/src/kernel/NeKit/Variant.h b/src/kernel/NeKit/Variant.h index 1b0fbe5d..a7257805 100644 --- a/src/kernel/NeKit/Variant.h +++ b/src/kernel/NeKit/Variant.h @@ -6,7 +6,7 @@ #pragma once -#include +#include #include #include #include diff --git a/src/kernel/NeKit/Vet.h b/src/kernel/NeKit/Vet.h new file mode 100644 index 00000000..d4c1060f --- /dev/null +++ b/src/kernel/NeKit/Vet.h @@ -0,0 +1,34 @@ + +/* ======================================== + + Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. + +======================================== */ + +#pragma once + +#include +#include + +#define NE_VETTABLE : public IVet + +namespace Kernel { +struct IVet { + IVet() = default; + virtual ~IVet() = default; + + NE_COPY_DEFAULT(IVet) + + operator bool() = delete; +}; + +template +struct Vettable final { + static constexpr bool kValue = false; +}; + +template <> +struct Vettable final { + static constexpr bool kValue = true; +}; +} // namespace Kernel \ No newline at end of file -- cgit v1.2.3