summaryrefslogtreecommitdiffhomepage
path: root/Private/Source/KernelHeap.cxx
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-01-29 18:17:47 +0100
committerAmlal El Mahrouss <amlal.elmahrouss@icloud.com>2024-01-29 18:18:59 +0100
commit65254486efff0fd1bb78a48ff90b7713a5ce539f (patch)
tree20ce02c12a74ba9e6cd382bf9c1f09a0c611cb4d /Private/Source/KernelHeap.cxx
parentf03986937db0b927da4b10554801e18e4dc7c43f (diff)
Kernel: Update TODO.
Src: Refactorings according to clang-format. Meta: Update specification. Public: Remove useless UIKit. Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'Private/Source/KernelHeap.cxx')
-rw-r--r--Private/Source/KernelHeap.cxx109
1 files changed, 109 insertions, 0 deletions
diff --git a/Private/Source/KernelHeap.cxx b/Private/Source/KernelHeap.cxx
new file mode 100644
index 00000000..02688a2f
--- /dev/null
+++ b/Private/Source/KernelHeap.cxx
@@ -0,0 +1,109 @@
+/*
+ * ========================================================
+ *
+ * hCore
+ * Copyright 2024 Mahrouss Logic, all rights reserved.
+ *
+ * ========================================================
+ */
+
+#include <NewKit/KernelHeap.hpp>
+
+//! @file KernelHeap.cpp
+//! @brief Kernel allocator.
+
+#define kMaxWrappers (4096 * 8)
+
+namespace hCore {
+static Ref<PTEWrapper *> kWrapperList[kMaxWrappers];
+static SizeT kWrapperCount = 0UL;
+static Ref<PTEWrapper *> kLastWrapper;
+static Pmm kPmm;
+
+namespace Detail {
+static voidPtr find_ptr(const SizeT &sz, const bool rw, const bool user) {
+ for (SizeT indexWrapper = 0; indexWrapper < kMaxWrappers; ++indexWrapper) {
+ if (!kWrapperList[indexWrapper]->Present()) {
+ kWrapperList[indexWrapper]
+ ->Reclaim(); /* very straight-forward as you can see. */
+ return reinterpret_cast<voidPtr>(
+ kWrapperList[indexWrapper]->VirtualAddress());
+ }
+ }
+
+ return nullptr;
+}
+} // namespace Detail
+
+/// @brief manual allocation
+/// @param sz size of pointer
+/// @param rw read write (true to enable it)
+/// @param user is it accesible by user processes?
+/// @return the pointer
+VoidPtr kernel_new_ptr(const SizeT &sz, const bool rw, const bool user) {
+ if (kWrapperCount < sz) return nullptr;
+
+ if (auto ptr = Detail::find_ptr(sz, rw, user); ptr) return ptr;
+
+ Ref<PTEWrapper *> wrapper = kPmm.RequestPage(user, rw);
+
+ if (wrapper) {
+ kLastWrapper = wrapper;
+
+ kWrapperList[kWrapperCount] = wrapper;
+ ++kWrapperCount;
+
+ return reinterpret_cast<voidPtr>(wrapper->VirtualAddress());
+ }
+
+ return nullptr;
+}
+
+/// @brief Declare pointer as free.
+/// @param ptr the pointer.
+/// @return
+Int32 kernel_delete_ptr(voidPtr ptr) {
+ if (ptr) {
+ const UIntPtr virtualAddress = reinterpret_cast<UIntPtr>(ptr);
+
+ if (kLastWrapper && virtualAddress == kLastWrapper->VirtualAddress()) {
+ return kPmm.FreePage(kLastWrapper);
+ }
+
+ Ref<PTEWrapper *> wrapper;
+
+ for (SizeT indexWrapper = 0; indexWrapper < kWrapperCount; ++indexWrapper) {
+ if (kWrapperList[indexWrapper]->VirtualAddress() == virtualAddress) {
+ wrapper = kWrapperList[indexWrapper];
+ return kPmm.FreePage(wrapper);
+ }
+ }
+ }
+
+ return -1;
+}
+
+/// @brief find pointer in kernel heap
+/// @param ptr the pointer
+/// @return if it exists.
+Boolean kernel_valid_ptr(voidPtr ptr) {
+ if (ptr) {
+ const UIntPtr virtualAddress = reinterpret_cast<UIntPtr>(ptr);
+
+ if (kLastWrapper && virtualAddress == kLastWrapper->VirtualAddress()) {
+ return true;
+ }
+
+ Ref<PTEWrapper *> wrapper;
+
+ for (SizeT indexWrapper = 0; indexWrapper < kWrapperCount; ++indexWrapper) {
+ if (kWrapperList[indexWrapper]->VirtualAddress() == virtualAddress) {
+ wrapper = kWrapperList[indexWrapper];
+ return true;
+ }
+ }
+ }
+
+ return false;
+}
+} // namespace hCore