From 8a63b04ff3e8b46bb3380b9769ad074eed3db50f Mon Sep 17 00:00:00 2001 From: OldSerpskiStalker Date: Mon, 2 Sep 2024 20:02:29 +0500 Subject: [PATCH] Added custom inline allocator for Render. Part 1 --- src/RenderLibrary/FabricCustomWindow.cpp | 23 +++++ src/RenderLibrary/FabricCustomWindow.hpp | 13 +++ src/RenderLibrary/FabricNativeWindow.hpp | 2 +- src/RenderLibrary/Memory.cpp | 90 +++++++++++++++++++ src/RenderLibrary/Memory.hpp | 33 +++++++ src/RenderLibrary/RenderLibrary.vcxproj | 4 + .../RenderLibrary.vcxproj.filters | 4 + src/RenderTestApp/StartUp.cpp | 2 +- 8 files changed, 169 insertions(+), 2 deletions(-) create mode 100644 src/RenderLibrary/FabricCustomWindow.cpp create mode 100644 src/RenderLibrary/FabricCustomWindow.hpp create mode 100644 src/RenderLibrary/Memory.cpp create mode 100644 src/RenderLibrary/Memory.hpp diff --git a/src/RenderLibrary/FabricCustomWindow.cpp b/src/RenderLibrary/FabricCustomWindow.cpp new file mode 100644 index 0000000..02061cf --- /dev/null +++ b/src/RenderLibrary/FabricCustomWindow.cpp @@ -0,0 +1,23 @@ +#include "StdAfx.h" + +/* Disable rarely used windows stuff */ +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif // !WIN32_LEAN_AND_MEAN + +#include + +#include +#include +#include +#include +#include +#include +#include + +using namespace DirectX; + +#include "Exports.hpp" +#include "FabricCustomWindow.hpp" + +using namespace RenderLibary; diff --git a/src/RenderLibrary/FabricCustomWindow.hpp b/src/RenderLibrary/FabricCustomWindow.hpp new file mode 100644 index 0000000..1f41fed --- /dev/null +++ b/src/RenderLibrary/FabricCustomWindow.hpp @@ -0,0 +1,13 @@ +#pragma once + +namespace RenderLibary +{ + struct RenderCustomObject + { + MSG _msg = {}; + HWND _window = {}; + WNDCLASSEXW _wndClassExW = {}; + const wchar_t* _wndClassName = L"ShaderPlaygroundRender"; + + } _renderCustomObject; +} diff --git a/src/RenderLibrary/FabricNativeWindow.hpp b/src/RenderLibrary/FabricNativeWindow.hpp index 509a408..0a2a5a8 100644 --- a/src/RenderLibrary/FabricNativeWindow.hpp +++ b/src/RenderLibrary/FabricNativeWindow.hpp @@ -7,7 +7,7 @@ namespace RenderLibary MSG _msg = {}; HWND _window = {}; WNDCLASSEXW _wndClassExW = {}; - const wchar_t* _wndClassName = L"ShaderPlayground.Render"; + const wchar_t* _wndClassName = L"ShaderPlaygroundRender"; } _renderNativeObject; } diff --git a/src/RenderLibrary/Memory.cpp b/src/RenderLibrary/Memory.cpp new file mode 100644 index 0000000..cf4c51d --- /dev/null +++ b/src/RenderLibrary/Memory.cpp @@ -0,0 +1,90 @@ +#include "StdAfx.h" + +#include +#include +#include +#include + +#include "Memory.hpp" + +#pragma section(".Hook", read) + +using namespace RenderLibary; + +Allocator* Allocator::instance = nullptr; + +std::unordered_set Allocator::objectCollection; +std::atomic Allocator::uniqueIDCounter{ 0 }; + +void* Allocator::operator new(std::size_t size) +{ + void* ptr = std::malloc(size); + + if (ptr) + { + AddToCollection(ptr); + std::size_t id = GenerateUniqueID(); + std::cout << "Allocated object with ID: " << id << " at address: " << ptr << std::endl; + } + + return ptr; +} + +void Allocator::operator delete(void* ptr) noexcept +{ + RemoveFromCollection(ptr); + std::free(ptr); +} + +void* Allocator::operator new[](std::size_t size) +{ + void* ptr = std::malloc(size); + return ptr; +} + +void Allocator::operator delete[](void* ptr) noexcept +{ + std::free(ptr); +} + +void Allocator::AddToCollection(void* ptr) +{ + objectCollection.insert(ptr); +} + +void Allocator::RemoveFromCollection(void* ptr) +{ + objectCollection.erase(ptr); +} + +void Allocator::PrintCollection() +{ + for (const auto& obj : objectCollection) + { + //Log::Get()->Debug("~ Object at address: %p", obj); + } +} + +std::size_t Allocator::GenerateUniqueID() +{ + return ++uniqueIDCounter; // Увеличиваем и возвращаем уникальный идентификатор +} + +Allocator& Allocator::GetInstance() +{ + if (!instance) + { + instance = new Allocator(); + } + + return *instance; +} + +Allocator::Allocator() +{ + std::cout << "Allocator created" << std::endl; +} + +#pragma init_seg(lib) +__declspec(allocate(".Hook")) +Allocator& Memory = Allocator::GetInstance(); diff --git a/src/RenderLibrary/Memory.hpp b/src/RenderLibrary/Memory.hpp new file mode 100644 index 0000000..a8c3fb7 --- /dev/null +++ b/src/RenderLibrary/Memory.hpp @@ -0,0 +1,33 @@ +#pragma once + +#pragma once + +namespace RenderLibary +{ + class Allocator + { + public: + static Allocator& GetInstance(); + + void* operator new(std::size_t size); + void operator delete(void* ptr) noexcept; + + void* operator new[](std::size_t size); + void operator delete[](void* ptr) noexcept; + + static void AddToCollection(void* ptr); + static void RemoveFromCollection(void* ptr); + static void PrintCollection(); + + static std::size_t GenerateUniqueID(); + + private: + Allocator(); + ~Allocator() = default; + + static Allocator* instance; + + static std::atomic uniqueIDCounter; + static std::unordered_set objectCollection; + }; +} diff --git a/src/RenderLibrary/RenderLibrary.vcxproj b/src/RenderLibrary/RenderLibrary.vcxproj index 0fdc1ac..b14c6e4 100644 --- a/src/RenderLibrary/RenderLibrary.vcxproj +++ b/src/RenderLibrary/RenderLibrary.vcxproj @@ -167,7 +167,9 @@ + + Create Create @@ -177,7 +179,9 @@ + + diff --git a/src/RenderLibrary/RenderLibrary.vcxproj.filters b/src/RenderLibrary/RenderLibrary.vcxproj.filters index 122131b..d0a6f3e 100644 --- a/src/RenderLibrary/RenderLibrary.vcxproj.filters +++ b/src/RenderLibrary/RenderLibrary.vcxproj.filters @@ -12,10 +12,14 @@ + + + + \ No newline at end of file diff --git a/src/RenderTestApp/StartUp.cpp b/src/RenderTestApp/StartUp.cpp index d24d2ad..e440916 100644 --- a/src/RenderTestApp/StartUp.cpp +++ b/src/RenderTestApp/StartUp.cpp @@ -11,7 +11,7 @@ using namespace DirectX; #include "Framework.h" #include "StartUp.hpp" -#include "FabricWindow.hpp" +#include "FabricCustomWindow.hpp" #include "Exports.hpp" void Start::Launch()