From 7b16a971891e12550569aa67d4185c2e51c3b8a9 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 22 Mar 2024 00:57:17 +0100 Subject: unstable: /Public/: Rename Kits/ to SDK/ Signed-off-by: Amlal El Mahrouss --- Public/SDK/System.Core/Sources/DllMain.cxx | 14 +++++ Public/SDK/System.Core/Sources/Heap.cxx | 83 +++++++++++++++++++++++++++ Public/SDK/System.Core/Sources/New+Delete.cxx | 65 +++++++++++++++++++++ 3 files changed, 162 insertions(+) create mode 100644 Public/SDK/System.Core/Sources/DllMain.cxx create mode 100644 Public/SDK/System.Core/Sources/Heap.cxx create mode 100644 Public/SDK/System.Core/Sources/New+Delete.cxx (limited to 'Public/SDK/System.Core/Sources') diff --git a/Public/SDK/System.Core/Sources/DllMain.cxx b/Public/SDK/System.Core/Sources/DllMain.cxx new file mode 100644 index 00000000..9412cbbb --- /dev/null +++ b/Public/SDK/System.Core/Sources/DllMain.cxx @@ -0,0 +1,14 @@ +/** =========================================== + (C) Mahrouss Logic + ===========================================*/ + +#include + +/// @brief Inits the DLL. +/// @return if it was succesful or not. +DWordType __DllMain(VoidType) { + kInstanceObject = HcGetInstanceObject(); + CA_MUST_PASS(kInstanceObject); + + return 0; +} \ No newline at end of file diff --git a/Public/SDK/System.Core/Sources/Heap.cxx b/Public/SDK/System.Core/Sources/Heap.cxx new file mode 100644 index 00000000..b3f43ad3 --- /dev/null +++ b/Public/SDK/System.Core/Sources/Heap.cxx @@ -0,0 +1,83 @@ +/* ------------------------------------------- + + Copyright Mahrouss Logic + +------------------------------------------- */ + +#include + +/// @brief Allocate from the user's heap. +/// @param refObj Process object. +/// @param sz size of object. +/// @param flags flags. +/// @return +CA_EXTERN_C PtrVoidType HcAllocateProcessHeap(ObjectPtr refObj, QWordType sz, + DWordType flags) { + CA_MUST_PASS(sz); + CA_MUST_PASS(flags); + + return (PtrVoidType)refObj->Invoke(refObj, kProcessCallAllocPtr, sz, flags); +} + +/// @brief Free pointer from the user's heap. +/// @param refObj Process object. +/// @param ptr the pointer to free. +CA_EXTERN_C VoidType HcFreeProcessHeap(ObjectPtr refObj, PtrVoidType ptr) { + CA_MUST_PASS(ptr); + CA_UNREFERENCED_PARAMETER(refObj->Invoke(refObj, kProcessCallFreePtr, ptr)); +} + +/// @brief Get pointer size. +/// @param refObj Process object. +/// @param ptr the pointer to find. +/// @return the size. +CA_EXTERN_C QWordType HcProcessHeapSize(ObjectPtr refObj, PtrVoidType ptr) { + CA_MUST_PASS(ptr); + return refObj->Invoke(refObj, kProcessCallSizePtr, ptr); +} + +/// @brief Check if the pointer exists. +/// @param refObj Process object. +/// @param ptr the pointer to check. +/// @return if it exists +CA_EXTERN_C BooleanType HcProcessHeapExists(ObjectPtr refObj, PtrVoidType ptr) { + CA_MUST_PASS(ptr); + return refObj->Invoke(refObj, kProcessCallCheckPtr, ptr); +} + +using namespace System; + +/// @brief Shared instance of the heap. +/// @return +HeapInterface* HeapInterface::Shared() noexcept { + static HeapInterface* heap = nullptr; + + if (!heap) { + heap = new HeapInterface(); + } + + return heap; +} + +HeapInterface::HeapInterface() { + CA_MUST_PASS(HcProcessHeapExists(kInstanceObject, (PtrVoidType)this)); +} + +HeapInterface::~HeapInterface() { delete this; } + +void HeapInterface::Delete(HeapPtr me) noexcept { + CA_MUST_PASS(me); + HcFreeProcessHeap(kInstanceObject, me); +} + +SizeType HeapInterface::Size(HeapPtr me) noexcept { + CA_MUST_PASS(me); + return HcProcessHeapSize(kInstanceObject, me); +} + +HeapPtr HeapInterface::New(const SizeType& sz, const DWordType flags) { + SizeType _sz = sz; + if (!_sz) ++_sz; + + return HcAllocateProcessHeap(kInstanceObject, _sz, flags); +} diff --git a/Public/SDK/System.Core/Sources/New+Delete.cxx b/Public/SDK/System.Core/Sources/New+Delete.cxx new file mode 100644 index 00000000..019db66c --- /dev/null +++ b/Public/SDK/System.Core/Sources/New+Delete.cxx @@ -0,0 +1,65 @@ +/* ------------------------------------------- + + Copyright Mahrouss Logic + +------------------------------------------- */ + +#include + +#define kAllocationTypes 2 + +enum HcAllocationKind { + kStandardAllocation = 0xC, + kArrayAllocation = 0xD, +}; + +CA_EXTERN_C PtrVoidType HcAllocateProcessHeap(ObjectPtr refObj, QWordType sz, + DWordType flags); +CA_EXTERN_C BooleanType HcProcessHeapExists(ObjectPtr refObj, PtrVoidType ptr); +CA_EXTERN_C QWordType HcProcessHeapSize(ObjectPtr refObj, PtrVoidType ptr); +CA_EXTERN_C VoidType HcFreeProcessHeap(ObjectPtr refObj, PtrVoidType ptr); + +typedef SizeType size_t; + +void* operator new[](size_t sz) +{ + if (sz == 0) + ++sz; + + return HcAllocateProcessHeap(kInstanceObject, sz, kStandardAllocation); +} + +void* operator new(size_t sz) +{ + if (sz == 0) + ++sz; + + return HcAllocateProcessHeap(kInstanceObject, sz, kArrayAllocation); +} + +void operator delete[](void* ptr) +{ + if (ptr == nullptr) + return; + + HcFreeProcessHeap(kInstanceObject, ptr); +} + +void operator delete(void* ptr) +{ + if (ptr == nullptr) + return; + + HcFreeProcessHeap(kInstanceObject, ptr); +} + +void operator delete(void* ptr, size_t sz) +{ + if (ptr == nullptr) + return; + + (void)sz; + + HcFreeProcessHeap(kInstanceObject, ptr); +} + -- cgit v1.2.3