From 5e585fa5cdcb9648cf84a650b409e7589de1cecf Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 3 Apr 2024 16:07:51 +0200 Subject: Kernel: Rename HeapManager to UserHeapManager, document FileManager' FilesystemManagerInterface thoroughly. --- Private/Source/FileManager.cxx | 4 +- Private/Source/UserHeap.cxx | 87 ++++++++++++++++++++++-------------------- 2 files changed, 47 insertions(+), 44 deletions(-) diff --git a/Private/Source/FileManager.cxx b/Private/Source/FileManager.cxx index 4d3c7d32..4742769c 100644 --- a/Private/Source/FileManager.cxx +++ b/Private/Source/FileManager.cxx @@ -21,7 +21,7 @@ FilesystemManagerInterface* FilesystemManagerInterface::GetMounted() { } /// @brief Unmount filesystem. -/// @return the unmounted filesystem. +/// @return The unmounted filesystem. FilesystemManagerInterface* FilesystemManagerInterface::Unmount() { if (kMounted) { auto mount = kMounted; @@ -34,7 +34,7 @@ FilesystemManagerInterface* FilesystemManagerInterface::Unmount() { } /// @brief Mount filesystem. -/// @param mountPtr the filesystem to mount. +/// @param mountPtr The filesystem to mount. /// @return if it succeeded true, otherwise false. bool FilesystemManagerInterface::Mount(FilesystemManagerInterface* mountPtr) { if (kMounted == nullptr) { diff --git a/Private/Source/UserHeap.cxx b/Private/Source/UserHeap.cxx index 1747b3fa..5f092102 100644 --- a/Private/Source/UserHeap.cxx +++ b/Private/Source/UserHeap.cxx @@ -4,9 +4,9 @@ ------------------------------------------- */ -#include -#include #include +#include +#include #define kHeapHeaderPaddingSz (16U) @@ -20,7 +20,7 @@ namespace NewOS { /** * @brief Process Heap Header * @note Allocated per process, it denotes the user's heap. -*/ + */ struct UserHeapHeader final { UInt32 fMagic; Int32 fFlags; @@ -33,7 +33,11 @@ struct UserHeapHeader final { * @note This rely on Virtual Memory! Consider adding good vmem support when * @note porting to a new arch. */ -class HeapManager final { +class UserHeapManager final { + public: + UserHeapManager() = delete; + ~UserHeapManager() = default; + public: STATIC SizeT& Count() { return s_NumPools; } STATIC Ref& Leak() { return s_Pmm; } @@ -51,32 +55,30 @@ class HeapManager final { //! declare fields -SizeT HeapManager::s_NumPools = 0UL; -Ref HeapManager::s_Pmm; -Boolean HeapManager::s_PoolsAreEnabled = true; -MutableArray> HeapManager::s_Pool; +SizeT UserHeapManager::s_NumPools = 0UL; +Ref UserHeapManager::s_Pmm; +Boolean UserHeapManager::s_PoolsAreEnabled = true; +MutableArray> UserHeapManager::s_Pool; -STATIC VoidPtr ke_find_unused_heap(Int flags); +STATIC VoidPtr ke_find_unused_heap(Int32 flags); STATIC Void ke_free_heap_internal(VoidPtr vaddr); -STATIC VoidPtr ke_make_heap(VoidPtr vaddr, Int flags); +STATIC VoidPtr ke_make_heap_internal(VoidPtr vaddr, Int32 flags); STATIC Boolean ke_check_and_free_heap(const SizeT& index, VoidPtr ptr); /// @brief Find an unused heap header to allocate on. /// @param flags the flags to use. -/// @return -STATIC VoidPtr ke_find_unused_heap(Int flags) { +/// @return VoidPtr the heap pointer. +STATIC VoidPtr ke_find_unused_heap(Int32 flags) { for (SizeT index = 0; index < kUserHeapMaxSz; ++index) { - if (HeapManager::The()[index] && - !HeapManager::The()[index].Leak().Leak().Present()) { - HeapManager::Leak().Leak().TogglePresent( - HeapManager::The()[index].Leak().Leak(), true); + if (UserHeapManager::The()[index] && + !UserHeapManager::The()[index].Leak().Leak().Present()) { + UserHeapManager::Leak().Leak().TogglePresent( + UserHeapManager::The()[index].Leak().Leak(), true); kcout << "[ke_find_unused_heap] Done, trying to make a pool now...\r\n"; - return ke_make_heap((VoidPtr)HeapManager::The()[index] - .Leak() - .Leak() - .VirtualAddress(), - flags); + return ke_make_heap_internal( + (VoidPtr)UserHeapManager::The()[index].Leak().Leak().VirtualAddress(), + flags); } } @@ -86,13 +88,14 @@ STATIC VoidPtr ke_find_unused_heap(Int flags) { /// @brief Makes a new heap for the process to use. /// @param virtualAddress the virtual address of the process. /// @param flags the flags. -/// @return -STATIC VoidPtr ke_make_heap(VoidPtr virtualAddress, Int flags) { +/// @return +STATIC VoidPtr ke_make_heap_internal(VoidPtr virtualAddress, Int32 flags) { if (virtualAddress) { UserHeapHeader* poolHdr = reinterpret_cast(virtualAddress); if (!poolHdr->fFree) { - kcout << "[ke_make_heap] poolHdr->fFree, HeapPtr already exists\n"; + kcout + << "[ke_make_heap_internal] poolHdr->fFree, HeapPtr already exists\n"; return nullptr; } @@ -100,19 +103,19 @@ STATIC VoidPtr ke_make_heap(VoidPtr virtualAddress, Int flags) { poolHdr->fMagic = kUserHeapMag; poolHdr->fFree = false; - kcout << "[ke_make_heap] New allocation has been done.\n"; + kcout << "[ke_make_heap_internal] New allocation has been done.\n"; return reinterpret_cast( (reinterpret_cast(virtualAddress) + sizeof(UserHeapHeader))); } - kcout << "[ke_make_heap] Address is invalid"; + kcout << "[ke_make_heap_internal] Address is invalid"; return nullptr; } /// @brief Internally makrs the heap as free. /// This is done by setting the fFree bit to true -/// @param virtualAddress -/// @return +/// @param virtualAddress +/// @return STATIC Void ke_free_heap_internal(VoidPtr virtualAddress) { UserHeapHeader* poolHdr = reinterpret_cast( reinterpret_cast(virtualAddress) - sizeof(UserHeapHeader)); @@ -138,16 +141,16 @@ STATIC Void ke_free_heap_internal(VoidPtr virtualAddress) { * @return Boolean true if successful. */ STATIC Boolean ke_check_and_free_heap(const SizeT& index, VoidPtr ptr) { - if (HeapManager::The()[index]) { + if (UserHeapManager::The()[index]) { // ErrorOr<>::operator Boolean /// if (address matches) /// -> Free heap. - if (HeapManager::The()[index].Leak().Leak().VirtualAddress() == + if (UserHeapManager::The()[index].Leak().Leak().VirtualAddress() == (UIntPtr)ptr) { - HeapManager::Leak().Leak().FreePage( - HeapManager::The()[index].Leak().Leak()); + UserHeapManager::Leak().Leak().FreePage( + UserHeapManager::The()[index].Leak().Leak()); - --HeapManager::Count(); + --UserHeapManager::Count(); ke_free_heap_internal(ptr); ptr = nullptr; @@ -163,25 +166,25 @@ STATIC Boolean ke_check_and_free_heap(const SizeT& index, VoidPtr ptr) { /// @param flags the flags attached to it. /// @return a pool pointer with selected permissions. VoidPtr rt_new_heap(Int32 flags) { - if (!HeapManager::IsEnabled()) return nullptr; + if (!UserHeapManager::IsEnabled()) return nullptr; - if (HeapManager::Count() > kUserHeapMaxSz) return nullptr; + if (UserHeapManager::Count() > kUserHeapMaxSz) return nullptr; if (VoidPtr ret = ke_find_unused_heap(flags)) return ret; // this wasn't set to true - auto ref_page = HeapManager::Leak().Leak().RequestPage(((flags & kUserHeapUser)), - (flags & kUserHeapRw)); + auto ref_page = UserHeapManager::Leak().Leak().RequestPage( + ((flags & kUserHeapUser)), (flags & kUserHeapRw)); if (ref_page) { ///! reserve page. - HeapManager::The()[HeapManager::Count()].Leak() = ref_page; - auto& ref = HeapManager::Count(); + UserHeapManager::The()[UserHeapManager::Count()].Leak() = ref_page; + auto& ref = UserHeapManager::Count(); ++ref; // increment the number of addresses we have now. // finally make the pool address. - return ke_make_heap( + return ke_make_heap_internal( reinterpret_cast(ref_page.Leak().VirtualAddress()), flags); } @@ -192,10 +195,10 @@ VoidPtr rt_new_heap(Int32 flags) { /// @param ptr The pool pointer to free. /// @return status code Int32 rt_free_heap(VoidPtr ptr) { - if (!HeapManager::IsEnabled()) return -1; + if (!UserHeapManager::IsEnabled()) return -1; if (ptr) { - SizeT base = HeapManager::Count(); + SizeT base = UserHeapManager::Count(); if (ke_check_and_free_heap(base, ptr)) return 0; -- cgit v1.2.3