Added support via a macro (BASALT_ENABLE_ALLOC_RECORDS 1) to enable tracking all allocations pointer value and their size. Naturally this is slow and should be disabled for any non-debug build.
This commit is contained in:
@@ -7,11 +7,15 @@
|
||||
#include "core/basalt_logger.h"
|
||||
#include "basalt_defines.h"
|
||||
|
||||
#include <map>
|
||||
|
||||
static struct {
|
||||
i64 alloc_total;
|
||||
i64 class_alloc[MEMORY_TAG_CLASS_MAX+1];
|
||||
i64 zone_alloc[MEMORY_TAG_ZONE_MAX + 1];
|
||||
#if BASALT_ENABLE_ALLOC_RECORDS
|
||||
std::map<void*, u64> records;
|
||||
#endif
|
||||
} basalt_memory_state;
|
||||
|
||||
static const char* basalt_memory_class_names[MEMORY_TAG_CLASS_MAX_BUILTIN] = {
|
||||
@@ -49,9 +53,24 @@ void basalt::mem::initialize_memory(void)
|
||||
basalt_memory_class_name_lengths[i] = strnlen(basalt_memory_class_names[i], 64);
|
||||
for (size_t i = 0; i < MEMORY_TAG_ZONE_MAX_BUILTIN; ++i)
|
||||
basalt_memory_zone_name_lengths[i] = strnlen(basalt_memory_zone_names[i], 64);
|
||||
#if BASALT_ENABLE_ALLOC_RECORDS
|
||||
new (&basalt_memory_state.records) std::map<void*, u64>();
|
||||
#endif
|
||||
}
|
||||
|
||||
void basalt::mem::terminate_memory(void) {}
|
||||
void basalt::mem::terminate_memory(void)
|
||||
{
|
||||
#if BASALT_ENABLE_ALLOC_RECORDS
|
||||
if (basalt_memory_state.records.size() > 0)
|
||||
{
|
||||
BERROR("Memory leak detected!\n");
|
||||
for (auto const& [k, v] : basalt_memory_state.records)
|
||||
{
|
||||
BERROR("\tAllocation at %p leaked %llu bytes\n", k, v);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void* basalt::mem::alloc(u64 num_bytes, MEMORY_TAG tag)
|
||||
{
|
||||
@@ -71,6 +90,9 @@ void* basalt::mem::alloc(u64 num_bytes, MEMORY_TAG tag)
|
||||
basalt_memory_state.alloc_total += num_bytes;
|
||||
basalt_memory_state.class_alloc[(MEMORY_TAG_MASK_CLASS & tag) >> MEMORY_TAG_SHIFT_CLASS] += num_bytes;
|
||||
basalt_memory_state.zone_alloc[(MEMORY_TAG_MASK_ZONE & tag) >> MEMORY_TAG_SHIFT_ZONE] += num_bytes;
|
||||
#if BASALT_ENABLE_ALLOC_RECORDS
|
||||
basalt_memory_state.records.emplace(ptr, num_bytes);
|
||||
#endif
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
@@ -83,6 +105,9 @@ void basalt::mem::dealloc(void* ptr, u64 num_bytes, MEMORY_TAG tag)
|
||||
basalt_memory_state.alloc_total -= num_bytes;
|
||||
basalt_memory_state.class_alloc[(MEMORY_TAG_MASK_CLASS & tag) >> MEMORY_TAG_SHIFT_CLASS] -= num_bytes;
|
||||
basalt_memory_state.zone_alloc[(MEMORY_TAG_MASK_ZONE & tag) >> MEMORY_TAG_SHIFT_ZONE] -= num_bytes;
|
||||
#if BASALT_ENABLE_ALLOC_RECORDS
|
||||
basalt_memory_state.records.erase(ptr);
|
||||
#endif
|
||||
}
|
||||
|
||||
void* basalt::mem::setzero(void* dst, u64 num_bytes)
|
||||
|
||||
Reference in New Issue
Block a user