fixing valgrind issues, injecting garbage collector inside vulkan's funtions

This commit is contained in:
2024-12-13 02:51:52 +01:00
parent 34ea0822a7
commit f78c3e9f0f
8 changed files with 165 additions and 160 deletions

View File

@@ -19,6 +19,14 @@ namespace mlx
return ptr;
}
void* MemManager::AlignedMalloc(std::size_t alignment, std::size_t size)
{
void* ptr = std::aligned_alloc(alignment, size);
if(ptr != nullptr)
s_blocks.push_back(ptr);
return ptr;
}
void* MemManager::Calloc(std::size_t n, std::size_t size)
{
void* ptr = std::calloc(n, size);

View File

@@ -1,3 +1,4 @@
#include "vulkan/vulkan_core.h"
#include <PreCompiled.h>
#define VMA_IMPLEMENTATION
#ifdef MLX_COMPILER_CLANG
@@ -22,7 +23,7 @@
namespace mlx
{
void GPUAllocator::Init() noexcept
void GPUAllocator::Init(const VkAllocationCallbacks* callbacks) noexcept
{
MLX_PROFILE_FUNCTION();
VmaVulkanFunctions vma_vulkan_func{};
@@ -49,6 +50,7 @@ namespace mlx
allocator_create_info.physicalDevice = RenderCore::Get().GetPhysicalDevice();
allocator_create_info.device = RenderCore::Get().GetDevice();
allocator_create_info.instance = RenderCore::Get().GetInstance();
allocator_create_info.pAllocationCallbacks = callbacks;
allocator_create_info.pVulkanFunctions = &vma_vulkan_func;
kvfCheckVk(vmaCreateAllocator(&allocator_create_info, &m_allocator));

View File

@@ -31,6 +31,7 @@
#include <Platform/Window.h>
#include <Maths/Mat4.h>
namespace mlx
{
static std::unique_ptr<VulkanLoader> loader;
@@ -53,6 +54,21 @@ namespace mlx
std::cout << std::endl;
}
void* VulkanAllocationFunction(void*, std::size_t size, std::size_t alignment, VkSystemAllocationScope)
{
return MemManager::AlignedMalloc(alignment, size);
}
void* VulkanReallocationFunction(void*, void* ptr, std::size_t size, std::size_t, VkSystemAllocationScope)
{
return MemManager::Realloc(ptr, size);
}
void VulkanFreeFunction(void*, void* ptr)
{
MemManager::Free(ptr);
}
RenderCore* RenderCore::s_instance = nullptr;
RenderCore::RenderCore()
@@ -102,7 +118,17 @@ namespace mlx
vkDestroySurfaceKHR(m_instance, surface, nullptr);
m_allocator.Init();
VkAllocationCallbacks callbacks;
callbacks.pUserData = nullptr;
callbacks.pfnAllocation = VulkanAllocationFunction;
callbacks.pfnReallocation = VulkanReallocationFunction;
callbacks.pfnFree = VulkanFreeFunction;
callbacks.pfnInternalAllocation = nullptr;
callbacks.pfnInternalFree = nullptr;
kvfSetAllocationCallbacks(m_device, &callbacks);
m_allocator.Init(&callbacks);
}
#undef MLX_LOAD_FUNCTION