From dafb757549b5238f67c49b2c8c0a1b44aedf0bd4 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 12 Dec 2025 09:27:10 +0100 Subject: chore: make Ref `operator bool` explicit, remove useless `MUST_PASS` in TLS.inl, use `CFRef` in LaunchHelpers. Signed-off-by: Amlal El Mahrouss --- src/kernel/HALKit/AMD64/HalKernelMain.cc | 1 - src/kernel/KernelKit/FileMgr.h | 1 + src/kernel/KernelKit/Semaphore.h | 40 ++++++++++++++--------------- src/kernel/KernelKit/ThreadLocalStorage.inl | 2 -- src/kernel/NeKit/ErrorOr.h | 2 +- src/kernel/NeKit/Ref.h | 2 +- src/kernel/NeKit/Utils.h | 12 ++++++++- src/kernel/NeKit/Vettable.h | 6 ++--- src/kernel/src/FS/Ext2+IFS.cc | 40 +++++++++++++++-------------- src/kernel/src/FS/OpenHeFS+FileMgr.cc | 18 +++++++++++++ src/kernel/src/Pmm.cc | 9 +------ 11 files changed, 77 insertions(+), 56 deletions(-) (limited to 'src/kernel') diff --git a/src/kernel/HALKit/AMD64/HalKernelMain.cc b/src/kernel/HALKit/AMD64/HalKernelMain.cc index 831c3636..5f1d2430 100644 --- a/src/kernel/HALKit/AMD64/HalKernelMain.cc +++ b/src/kernel/HALKit/AMD64/HalKernelMain.cc @@ -17,7 +17,6 @@ #include #include #include -#include "NeKit/InitializerList.h" #ifndef __NE_MODULAR_KERNEL_COMPONENTS__ EXTERN_C Kernel::VoidPtr kInterruptVectorTable[]; diff --git a/src/kernel/KernelKit/FileMgr.h b/src/kernel/KernelKit/FileMgr.h index 9aebe1e9..5d3ab581 100644 --- a/src/kernel/KernelKit/FileMgr.h +++ b/src/kernel/KernelKit/FileMgr.h @@ -265,6 +265,7 @@ class HeFileSystemMgr final : public IFilesystemMgr { private: HeFileSystemParser* mParser{nullptr}; + DriveTrait mDriveTrait; }; #endif // ifdef __FSKIT_INCLUDES_OPENHEFS__ diff --git a/src/kernel/KernelKit/Semaphore.h b/src/kernel/KernelKit/Semaphore.h index 1cbc08c0..20965d91 100644 --- a/src/kernel/KernelKit/Semaphore.h +++ b/src/kernel/KernelKit/Semaphore.h @@ -25,37 +25,37 @@ namespace Kernel { /// @brief Semaphore structure used for synchronization. -typedef UInt64 SemaphoreArr[kSemaphoreCount]; +using SemaphoreArr = UInt64[kSemaphoreCount]; /// @brief Checks if the semaphore is valid. -inline BOOL rtl_sem_is_valid(const SemaphoreArr& sem, UInt64 owner = 0) { +inline bool rtl_sem_is_valid(const SemaphoreArr& sem, UInt64 owner = 0) { return sem[kSemaphoreOwnerIndex] == owner && sem[kSemaphoreCountIndex] > 0; } /// @brief Releases the semaphore, resetting its owner and count. /// @param sem /// @return -inline BOOL rtl_sem_release(SemaphoreArr& sem) { +inline bool rtl_sem_release(SemaphoreArr& sem) { sem[kSemaphoreOwnerIndex] = 0; sem[kSemaphoreCountIndex] = 0; - return TRUE; + return true; } /// @brief Initializes the semaphore with an owner and a count of zero. /// @param sem the semaphore array to use. /// @param owner the owner to set, could be anything identifitable. /// @return -inline BOOL rtl_sem_acquire(SemaphoreArr& sem, UInt64 owner) { +inline bool rtl_sem_acquire(SemaphoreArr& sem, const UInt64 owner) { if (!owner) { err_global_get() = kErrorInvalidData; - return FALSE; // Invalid owner + return false; // Invalid owner } sem[kSemaphoreOwnerIndex] = owner; sem[kSemaphoreCountIndex] = 0; - return TRUE; + return true; } /// @brief Waits for the semaphore to be available, blocking until it is. @@ -63,50 +63,50 @@ inline BOOL rtl_sem_acquire(SemaphoreArr& sem, UInt64 owner) { /// @param timeout /// @param condition condition pointer. /// @return -inline BOOL rtl_sem_wait(SemaphoreArr& sem, UInt64 owner, UInt64 timeout, - BOOL* condition = nullptr) { +inline bool rtl_sem_wait(SemaphoreArr& sem, const UInt64 owner, const UInt64 timeout, + bool& condition) { if (!rtl_sem_is_valid(sem, owner)) { - return FALSE; + return false; } - if (timeout <= 0) { + if (timeout < 1) { err_global_get() = kErrorTimeout; - return FALSE; + return false; } - if (!condition || *condition) { + if (!condition) { if (sem[kSemaphoreCountIndex] == 0) { err_global_get() = kErrorUnavailable; - return FALSE; + return false; } err_global_get() = kErrorSuccess; sem[kSemaphoreCountIndex]--; - return TRUE; + return true; } HardwareTimer timer(timeout); - BOOL ret = timer.Wait(); + bool ret = timer.Wait(); if (ret) { - if (!condition || *condition) { + if (!condition) { if (sem[kSemaphoreCountIndex] == 0) { err_global_get() = kErrorUnavailable; - return FALSE; + return false; } err_global_get() = kErrorSuccess; sem[kSemaphoreCountIndex]--; - return TRUE; + return true; } } err_global_get() = kErrorTimeout; - return FALSE; // Failed to acquire semaphore + return false; // Failed to acquire semaphore } } // namespace Kernel diff --git a/src/kernel/KernelKit/ThreadLocalStorage.inl b/src/kernel/KernelKit/ThreadLocalStorage.inl index 8e33cff3..dff2de30 100644 --- a/src/kernel/KernelKit/ThreadLocalStorage.inl +++ b/src/kernel/KernelKit/ThreadLocalStorage.inl @@ -16,7 +16,6 @@ inline T* tls_new_ptr(void) { using namespace Kernel; auto ref_process = UserProcessScheduler::The().TheCurrentProcess(); - MUST_PASS(ref_process); auto pointer = ref_process.Leak().New(sizeof(T)); @@ -34,7 +33,6 @@ inline Kernel::Bool tls_delete_ptr(T* obj) { if (!obj) return No; auto ref_process = UserProcessScheduler::The().TheCurrentProcess(); - MUST_PASS(ref_process); ErrorOr obj_wrapped{obj}; diff --git a/src/kernel/NeKit/ErrorOr.h b/src/kernel/NeKit/ErrorOr.h index a8280406..4e43cb27 100644 --- a/src/kernel/NeKit/ErrorOr.h +++ b/src/kernel/NeKit/ErrorOr.h @@ -16,7 +16,7 @@ namespace Kernel { /// ================================================================================ /// @brief ErrorOr class for error handling. /// ================================================================================ -template +template class ErrorOr final { public: explicit ErrorOr() = default; diff --git a/src/kernel/NeKit/Ref.h b/src/kernel/NeKit/Ref.h index 960df31f..253ee379 100644 --- a/src/kernel/NeKit/Ref.h +++ b/src/kernel/NeKit/Ref.h @@ -46,7 +46,7 @@ class Ref final { Type operator*() { return fClass; } - operator bool() { return Vettable::kValue; } + explicit operator bool() { return Vettable::kValue; } bool operator!() { return !Vettable::kValue; } private: diff --git a/src/kernel/NeKit/Utils.h b/src/kernel/NeKit/Utils.h index 28a153af..71cae42b 100644 --- a/src/kernel/NeKit/Utils.h +++ b/src/kernel/NeKit/Utils.h @@ -51,12 +51,22 @@ Size urt_string_len(const Utf8Char* str); /// =========================================================== /// template -inline SizeT oe_string_len(const CharType* str) { +inline constexpr SizeT oe_string_len(const CharType* str) { if (!str) return 0; +#if __cplusplus == 202302L + if consteval { + return ARRAY_SIZE(str); + } else { + SizeT len{0}; + while (str[len] != 0) ++len; + return len; + } +#else SizeT len{0}; while (str[len] != 0) ++len; return len; +#endif } } // namespace Kernel diff --git a/src/kernel/NeKit/Vettable.h b/src/kernel/NeKit/Vettable.h index 52528847..6769507f 100644 --- a/src/kernel/NeKit/Vettable.h +++ b/src/kernel/NeKit/Vettable.h @@ -29,7 +29,7 @@ struct INotVettable { NE_COPY_DEFAULT(INotVettable) }; -template +template struct Vettable final { static constexpr bool kValue = false; }; @@ -50,9 +50,9 @@ concept IsVettable = requires(OnFallback fallback) { { Vettable::kValue ? true : fallback() }; }; -template +template concept IsNotVettable = requires(OnFallback fallback) { - { !Vettable::kValue ? true : fallback() }; + { !Vettable::kValue ? true : fallback() }; }; } // namespace Kernel diff --git a/src/kernel/src/FS/Ext2+IFS.cc b/src/kernel/src/FS/Ext2+IFS.cc index 106229f7..bd70f334 100644 --- a/src/kernel/src/FS/Ext2+IFS.cc +++ b/src/kernel/src/FS/Ext2+IFS.cc @@ -26,16 +26,18 @@ constexpr ATTRIBUTE(unused) static UInt32 EXT2_SUPERBLOCK_BLOCK = 1; constexpr static UInt32 EXT2_GROUP_DESC_BLOCK_SMALL = 2; constexpr static UInt32 EXT2_GROUP_DESC_BLOCK_LARGE = 1; +namespace Detail { static inline SizeT ext2_min(SizeT a, SizeT b) { return a < b ? a : b; } -struct Ext2GroupInfo { +struct Ext2GroupInfo final { EXT2_GROUP_DESCRIPTOR* groupDesc; UInt32 groupDescriptorBlock; UInt32 offsetInGroupDescBlock; UInt8* blockBuffer; }; +} // namespace Detail // Convert EXT2 block number -> LBA (sector index) for Drive I/O. static inline UInt32 ext2_block_to_lba(Ext2Context* ctx, UInt32 blockNumber) { @@ -169,7 +171,7 @@ static ErrorOr ext2_read_inode_data(Ext2Context* ctx, Ext2Node* node, S return ErrorOr(5); // block read failed } - SizeT chunk = ext2_min(remaining, blockSize - offsetInBlock); + SizeT chunk = ::Detail::ext2_min(remaining, blockSize - offsetInBlock); rt_copy_memory_safe(static_cast(static_cast(blockBuf) + offsetInBlock), static_cast(dest), chunk, chunk); @@ -185,9 +187,9 @@ static ErrorOr ext2_read_inode_data(Ext2Context* ctx, Ext2Node* node, S } // Get group descriptor information for a given block/inode number -static ErrorOr ext2_get_group_descriptor_info(Ext2Context* ctx, +static ErrorOr<::Detail::Ext2GroupInfo*> ext2_get_group_descriptor_info(Ext2Context* ctx, UInt32 targetBlockOrInode) { - if (!ctx || !ctx->superblock || !ctx->drive) return ErrorOr(kErrorInvalidData); + if (!ctx || !ctx->superblock || !ctx->drive) return ErrorOr<::Detail::Ext2GroupInfo*>(kErrorInvalidData); UInt32 blockSize = ctx->BlockSize(); UInt32 blocksPerGroup = ctx->superblock->fBlocksPerGroup; @@ -195,7 +197,7 @@ static ErrorOr ext2_get_group_descriptor_info(Ext2Context* ctx, UInt32 totalBlocks = ctx->superblock->fBlockCount; UInt32 totalInodes = ctx->superblock->fInodeCount; - if (blocksPerGroup == 0 || inodesPerGroup == 0) return ErrorOr(kErrorInvalidData); + if (blocksPerGroup == 0 || inodesPerGroup == 0) return ErrorOr<::Detail::Ext2GroupInfo*>(kErrorInvalidData); // block group index UInt32 groupIndex = 0; @@ -215,7 +217,7 @@ static ErrorOr ext2_get_group_descriptor_info(Ext2Context* ctx, // Calculate number of block groups UInt32 groupsCount = static_cast((totalBlocks + blocksPerGroup - 1) / blocksPerGroup); - if (groupIndex >= groupsCount) return ErrorOr(kErrorInvalidData); + if (groupIndex >= groupsCount) return ErrorOr<::Detail::Ext2GroupInfo*>(kErrorInvalidData); // Determine GDT start block UInt32 gdtStartBlock = @@ -232,18 +234,18 @@ static ErrorOr ext2_get_group_descriptor_info(Ext2Context* ctx, // Allocate buffer and read the block containing the descriptor auto blockBuffer = mm_alloc_ptr(blockSize, true, false); - if (!blockBuffer) return ErrorOr(kErrorHeapOutOfMemory); + if (!blockBuffer) return ErrorOr<::Detail::Ext2GroupInfo*>(kErrorHeapOutOfMemory); UInt32 groupDescriptorLba = ext2_block_to_lba(ctx, groupDescriptorBlock); if (!ext2_read_block(ctx->drive, groupDescriptorLba, blockBuffer, blockSize)) { mm_free_ptr(blockBuffer); - return ErrorOr(kErrorDisk); + return ErrorOr<::Detail::Ext2GroupInfo*>(kErrorDisk); } - auto groupInfo = (Ext2GroupInfo*) mm_alloc_ptr(sizeof(Ext2GroupInfo), true, false); + auto groupInfo = (::Detail::Ext2GroupInfo*) mm_alloc_ptr(sizeof(::Detail::Ext2GroupInfo), true, false); if (!groupInfo) { mm_free_ptr(blockBuffer); - return ErrorOr(kErrorHeapOutOfMemory); + return ErrorOr<::Detail::Ext2GroupInfo*>(kErrorHeapOutOfMemory); } groupInfo->groupDesc = reinterpret_cast( @@ -252,7 +254,7 @@ static ErrorOr ext2_get_group_descriptor_info(Ext2Context* ctx, groupInfo->offsetInGroupDescBlock = offsetInGroupDescBlock; groupInfo->blockBuffer = reinterpret_cast(blockBuffer); - return ErrorOr(groupInfo); + return ErrorOr<::Detail::Ext2GroupInfo*>(groupInfo); } // Allocate a new block @@ -324,7 +326,7 @@ static ErrorOr ext2_set_block_address(Ext2Context* ctx, Ext2Node* node, auto groupInfoRes = ext2_get_group_descriptor_info(ctx, node->inodeNumber); if (groupInfoRes.HasError()) return ErrorOr(groupInfoRes.Error()); - auto groupInfo = groupInfoRes.Leak().Leak(); // Ref + auto groupInfo = groupInfoRes.Leak().Leak(); // Ref<::Detail::Ext2GroupInfo*> auto newBlockRes = ext2_alloc_block(ctx, groupInfo->groupDesc); if (newBlockRes.HasError()) { mm_free_ptr(reinterpret_cast(groupInfo->blockBuffer)); @@ -332,7 +334,7 @@ static ErrorOr ext2_set_block_address(Ext2Context* ctx, Ext2Node* node, return ErrorOr(newBlockRes.Error()); } - node->inode.fBlock[EXT2_SINGLE_INDIRECT_INDEX] = newBlockRes.Leak(); + node->inode.fBlock[EXT2_SINGLE_INDIRECT_INDEX] = newBlockRes.Leak().Leak(); UInt32 gdtLba = ext2_block_to_lba(ctx, groupInfo->groupDescriptorBlock); if (!ext2_write_block(ctx->drive, gdtLba, groupInfo->blockBuffer, blockSize)) { @@ -391,7 +393,7 @@ static ErrorOr ext2_set_block_address(Ext2Context* ctx, Ext2Node* node, return ErrorOr(newBlockRes.Error()); } - node->inode.fBlock[EXT2_DOUBLE_INDIRECT_INDEX] = newBlockRes.Leak(); + node->inode.fBlock[EXT2_DOUBLE_INDIRECT_INDEX] = newBlockRes.Leak().Leak(); UInt32 gdtLba = ext2_block_to_lba(ctx, groupInfo->groupDescriptorBlock); if (!ext2_write_block(ctx->drive, gdtLba, groupInfo->blockBuffer, blockSize)) { @@ -445,7 +447,7 @@ static ErrorOr ext2_set_block_address(Ext2Context* ctx, Ext2Node* node, return ErrorOr(newBlockRes.Error()); } - singleIndirectBlock = newBlockRes.Leak(); + singleIndirectBlock = newBlockRes.Leak().Leak(); doublePtr[firstIdx] = singleIndirectBlock; // Write back GDT @@ -595,7 +597,7 @@ static ErrorOr ext2_add_dir_entry(Ext2Context* ctx, Ext2Node* parentDirNo return ErrorOr(groupInfoRes.Error()); } - auto groupInfo = *groupInfoRes.Leak(); // Dereference to get Ext2GroupInfo* + auto groupInfo = *groupInfoRes.Leak(); // Dereference to get ::Detail::Ext2GroupInfo* auto allocBlockRes = ext2_alloc_block(ctx, groupInfo->groupDesc); if (!allocBlockRes) { mm_free_ptr(reinterpret_cast(groupInfo->blockBuffer)); @@ -708,7 +710,7 @@ static ErrorOr ext2_add_dir_entry(Ext2Context* ctx, Ext2Node* parentDirNo return ErrorOr(groupInfoResult.Error()); } - auto groupInfo = *groupInfoResult.Leak(); // Dereference to get Ext2GroupInfo* + auto groupInfo = *groupInfoResult.Leak(); // Dereference to get ::Detail::Ext2GroupInfo* auto newBlockRes = ext2_alloc_block(ctx, groupInfo->groupDesc); if (!newBlockRes) { mm_free_ptr(reinterpret_cast(groupInfo->blockBuffer)); @@ -820,7 +822,7 @@ static ErrorOr ext2_write_inode(Ext2Context* ctx, Ext2Node* node) { auto groupInfoResult = ext2_get_group_descriptor_info(ctx, node->inodeNumber); if (!groupInfoResult) return ErrorOr(groupInfoResult.Error()); - auto groupInfo = *groupInfoResult.Leak(); // Dereference to get Ext2GroupInfo* + auto groupInfo = *groupInfoResult.Leak(); // Dereference to get ::Detail::Ext2GroupInfo* // Calculate inode table position UInt32 inodeTableBlock = groupInfo->groupDesc->fInodeTable; @@ -1131,7 +1133,7 @@ void Ext2FileSystemParser::Write(NodePtr node, void* data, Int32 flags, SizeT si } UInt32 bytesInCurrentBlock = - static_cast(ext2_min(size - bytesWritten, blockSize - offsetInBlock)); + static_cast(::Detail::ext2_min(size - bytesWritten, blockSize - offsetInBlock)); rt_copy_memory_safe(src, static_cast((UInt8*) blockBuf + offsetInBlock), bytesInCurrentBlock, blockSize - offsetInBlock); diff --git a/src/kernel/src/FS/OpenHeFS+FileMgr.cc b/src/kernel/src/FS/OpenHeFS+FileMgr.cc index 4c49ac11..10c6da45 100644 --- a/src/kernel/src/FS/OpenHeFS+FileMgr.cc +++ b/src/kernel/src/FS/OpenHeFS+FileMgr.cc @@ -50,6 +50,24 @@ NodePtr HeFileSystemMgr::Create(_Input const Char* path) { kout << "OpenHeFS: Create called with null or empty path\n"; return nullptr; } + + // AMLALE: TODO, its own helper! + SizeT len = oe_string_len(path); + +#if defined(__clang__) + Utf8Char out[len]; + rt_set_memory(out, 0, len); +#else + Utf8Char* out = static_cast(RTL_ALLOCA(sizeof(Utf8Char) * len)); +#endif + + for (SizeT indx = 0UL; indx < len; ++indx) { + out[indx] = path[indx]; + } + + if (mParser->CreateINode(&mDriveTrait, 0, nullptr, out, 0)) + return nullptr; // AMLALE TODO: FetchINode method! + return nullptr; } diff --git a/src/kernel/src/Pmm.cc b/src/kernel/src/Pmm.cc index 84086929..074ae54b 100644 --- a/src/kernel/src/Pmm.cc +++ b/src/kernel/src/Pmm.cc @@ -6,14 +6,7 @@ #include #include - -#if defined(__NE_ARM64__) -#include -#endif // defined(__NE_ARM64__) - -#if defined(__NE_AMD64__) -#include -#endif // defined(__NE_AMD64__) +#include namespace Kernel { /***********************************************************************************/ -- cgit v1.2.3