Skip to content

Commit

Permalink
Added custom inline allocator for Render. Part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
OldSerpskiStalker committed Sep 2, 2024
1 parent cd6041a commit 8a63b04
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 2 deletions.
23 changes: 23 additions & 0 deletions src/RenderLibrary/FabricCustomWindow.cpp
Original file line number Diff line number Diff line change
@@ -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 <Windows.h>

#include <list>
#include <stddef.h>
#include <string>
#include <unordered_set>
#include <vector>
#include <d3d11.h>
#include <DirectXMath.h>

using namespace DirectX;

#include "Exports.hpp"
#include "FabricCustomWindow.hpp"

using namespace RenderLibary;
13 changes: 13 additions & 0 deletions src/RenderLibrary/FabricCustomWindow.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

namespace RenderLibary
{
struct RenderCustomObject
{
MSG _msg = {};
HWND _window = {};
WNDCLASSEXW _wndClassExW = {};
const wchar_t* _wndClassName = L"ShaderPlaygroundRender";

} _renderCustomObject;
}
2 changes: 1 addition & 1 deletion src/RenderLibrary/FabricNativeWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
90 changes: 90 additions & 0 deletions src/RenderLibrary/Memory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "StdAfx.h"

#include <iostream>
#include <new>
#include <unordered_set>
#include <atomic>

#include "Memory.hpp"

#pragma section(".Hook", read)

using namespace RenderLibary;

Allocator* Allocator::instance = nullptr;

std::unordered_set<void*> Allocator::objectCollection;
std::atomic<std::size_t> 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();
33 changes: 33 additions & 0 deletions src/RenderLibrary/Memory.hpp
Original file line number Diff line number Diff line change
@@ -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<std::size_t> uniqueIDCounter;
static std::unordered_set<void*> objectCollection;
};
}
4 changes: 4 additions & 0 deletions src/RenderLibrary/RenderLibrary.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="Exports.cpp" />
<ClCompile Include="FabricCustomWindow.cpp" />
<ClCompile Include="FabricNativeWindow.cpp" />
<ClCompile Include="Memory.cpp" />
<ClCompile Include="StdAfx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
Expand All @@ -177,7 +179,9 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="Exports.hpp" />
<ClInclude Include="FabricCustomWindow.hpp" />
<ClInclude Include="FabricNativeWindow.hpp" />
<ClInclude Include="Memory.hpp" />
<ClInclude Include="StdAfx.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
Expand Down
4 changes: 4 additions & 0 deletions src/RenderLibrary/RenderLibrary.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
<ClCompile Include="FabricNativeWindow.cpp" />
<ClCompile Include="StdAfx.cpp" />
<ClCompile Include="Exports.cpp" />
<ClCompile Include="FabricCustomWindow.cpp" />
<ClCompile Include="Memory.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="FabricNativeWindow.hpp" />
<ClInclude Include="StdAfx.h" />
<ClInclude Include="Exports.hpp" />
<ClInclude Include="FabricCustomWindow.hpp" />
<ClInclude Include="Memory.hpp" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/RenderTestApp/StartUp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 8a63b04

Please sign in to comment.