-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added custom inline allocator for Render. Part 1
- Loading branch information
1 parent
cd6041a
commit 8a63b04
Showing
8 changed files
with
169 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters