From 1220cf6e7af3b99390d636803f1462f8df8baa16 Mon Sep 17 00:00:00 2001 From: Sven Balzer <4653051+Kyuusokuna@users.noreply.github.com> Date: Sun, 16 Mar 2025 12:10:00 +0100 Subject: [PATCH] add tracy memory tracking to SDL --- src/main.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index b7deac5..df7e43e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -626,7 +626,44 @@ V2 get_floor_intersection_of_mouse(V2 mouse_pos) { return floor_intersection.xy; } +#ifdef TRACY_ENABLE +SDL_malloc_func sdl_malloc = NULL; +SDL_calloc_func sdl_calloc = NULL; +SDL_realloc_func sdl_realloc = NULL; +SDL_free_func sdl_free = NULL; + +void setup_memory_functions() { + SDL_GetMemoryFunctions(&sdl_malloc, &sdl_calloc, &sdl_realloc, &sdl_free); + SDL_SetMemoryFunctions( + [](size_t size) -> void * { + void *result = sdl_malloc(size); + TracyAllocN(result, size, "SDL"); + return result; + }, + [](size_t nmemb, size_t size) -> void * { + void *result = sdl_calloc(nmemb, size); + TracyAllocN(result, nmemb * size, "SDL"); + return result; + }, + [](void *mem, size_t size) -> void * { + void *result = sdl_realloc(mem, size); + TracyFreeN(mem, "SDL"); + TracyAllocN(result, size, "SDL"); + return result; + }, + [](void *mem) { + TracyFreeN(mem, "SDL"); + sdl_free(mem); + } + ); +} +#else +void setup_memory_functions() {} +#endif + int main(int argc, char **argv) { + setup_memory_functions(); + load_map(); #ifdef SDL_PLATFORM_LINUX