Annotate Memory Allocations.

This commit is contained in:
Anish Bhobe 2025-02-02 17:51:59 +01:00
parent 91010a448e
commit 466e4a4093
1 changed files with 66 additions and 2 deletions

View File

@ -14,16 +14,80 @@
// NOTE: Vulkan Dispatch Loader Storage - Should only appear once. // NOTE: Vulkan Dispatch Loader Storage - Should only appear once.
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE
struct MemorySize
{
u16 m_Gigabytes;
u16 m_Megabytes;
u16 m_Kilobytes;
u16 m_Bytes;
MemorySize &
operator+=(usize bytes)
{
usize totalBytes = bytes + m_Bytes;
m_Bytes = totalBytes % 1024;
const usize totalKb = m_Kilobytes + totalBytes / 1024;
m_Kilobytes = totalKb % 1024;
const usize totalMb = m_Megabytes + totalKb / 1024;
m_Megabytes = totalMb % 1024;
m_Gigabytes += totalMb / 1024;
return *this;
}
};
MemorySize g_TotalAlloc = {};
template <>
struct fmt::formatter<MemorySize>
{
// ReSharper disable once CppInconsistentNaming
constexpr auto
parse(format_parse_context &ctx)
{
return ctx.begin();
}
template <typename Context>
// ReSharper disable once CppInconsistentNaming
constexpr auto
format(MemorySize const &mem, Context &ctx) const
{
// return format_to(ctx.out(), "({}, {})", foo.a, foo.b); // --== KEY LINE ==--
if (mem.m_Gigabytes > 0)
{
return v10::format_to(ctx.out(), "{}.{} GB", mem.m_Gigabytes,
Cast<u16>(mem.m_Megabytes / 1024.0));
}
if (mem.m_Megabytes > 0)
{
return v10::format_to(ctx.out(), "{}.{} MB", mem.m_Megabytes, Cast<u16>(mem.m_Kilobytes / 1024.0));
}
if (mem.m_Kilobytes > 0)
{
return v10::format_to(ctx.out(), "{}.{} KB", mem.m_Kilobytes, Cast<u16>(mem.m_Bytes / 1024.0));
}
return v10::format_to(ctx.out(), "{} Bytes", mem.m_Bytes);
}
};
void * void *
operator new[](size_t size, const char * /*pName*/, int /*flags*/, unsigned /*debugFlags*/, const char * /*file*/, operator new[](size_t size, const char * /*pName*/, int flags, unsigned /*debugFlags*/, const char * /*file*/,
int /*line*/) int /*line*/)
{ {
g_TotalAlloc += size;
VERBOSE("Total: {} - Allocated {} bytes. ({})", g_TotalAlloc, size, (flags & eastl::MEM_TEMP) ? "temp" : "perm");
return new u8[size]; return new u8[size];
} }
void * void *
operator new[](size_t size, size_t /*alignment*/, size_t /*alignmentOffset*/, const char * /*pName*/, int /*flags*/, operator new[](size_t size, size_t /*alignment*/, size_t /*alignmentOffset*/, const char * /*pName*/, int flags,
unsigned /*debugFlags*/, const char * /*file*/, int /*line*/) unsigned /*debugFlags*/, const char * /*file*/, int /*line*/)
{ {
g_TotalAlloc += size;
VERBOSE("Total: {} - Allocated {} bytes. ({})", g_TotalAlloc, size, (flags & eastl::MEM_TEMP) ? "temp" : "perm");
return new u8[size]; return new u8[size];
} }