From e635d8c15c61f19c053b48e93298df9d797dabdd Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sun, 29 Jun 2025 10:50:25 -0700 Subject: Add memory diagnostics --- src/scene/scene_memory.c | 50 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 11 deletions(-) (limited to 'src/scene/scene_memory.c') diff --git a/src/scene/scene_memory.c b/src/scene/scene_memory.c index 85c27e7..4d7020a 100644 --- a/src/scene/scene_memory.c +++ b/src/scene/scene_memory.c @@ -12,6 +12,7 @@ #include "object_impl.h" #include "scene_impl.h" +#include #include DEF_MEMPOOL(anima_pool, Anima, GFX_MAX_NUM_ANIMAS) @@ -90,13 +91,40 @@ void scene_mem_destroy() { // NOTE: the dummy objects are not constructed, so the destruction code below // always skips index 0. (I don't really like the conditional inside the loop, // but this gets the job done without having to specialize the loop macro.) -#define DESTROY(name) \ - mempool_foreach(&MEM_FIELD(name), obj, { \ +#define DESTROY(NAME) \ + mempool_foreach(&MEM_FIELD(NAME), obj, { \ if (i > 0) { \ - gfx_destroy_##name(&obj); \ + gfx_destroy_##NAME(&obj); \ } \ }) + // Print memory diagnostics. +#define PRINT_POOL(POOL_NAME, POOL) \ + { \ + const size_t capacity = mempool_capacity(POOL); \ + const size_t size = mempool_size(POOL); \ + const size_t block_size_bytes = mempool_block_size_bytes(POOL); \ + const size_t size_bytes = size * block_size_bytes; \ + const size_t capacity_bytes = capacity * block_size_bytes; \ + LOGI( \ + "%s pool: %lu/%lu (%lu/%lu bytes)", POOL_NAME, size, capacity, \ + size_bytes, capacity_bytes); \ + } + + LOGI("Pool diagnostics:"); + PRINT_POOL("Animas", &mem.animas); + PRINT_POOL("Animations", &mem.animations); + PRINT_POOL("Cameras", &mem.cameras); + PRINT_POOL("Lights", &mem.lights); + PRINT_POOL("Materials", &mem.materials); + PRINT_POOL("Meshes", &mem.meshs); + PRINT_POOL("Mesh links", &mem.mesh_links); + PRINT_POOL("Models", &mem.models); + PRINT_POOL("Nodes", &mem.nodes); + PRINT_POOL("Objects", &mem.objects); + PRINT_POOL("Scenes", &mem.scenes); + PRINT_POOL("Skeletons", &mem.skeletons); + // Models contain scene elements. Destruction is handled by the remainder of // scene destructionb elow. // @@ -119,20 +147,20 @@ void scene_mem_destroy() { // Skeletons are owned by animas and do not have a destructor. } -#define DEF_MEMORY(name, type) \ +#define DEF_MEMORY(NAME, TYPE) \ /* xyz* mem_alloc_xyz(); */ \ - type* mem_alloc_##name() { return mempool_alloc(&MEM_FIELD(name)); } \ + TYPE* mem_alloc_##NAME() { return mempool_alloc(&MEM_FIELD(NAME)); } \ /* void mem_free_xyz(xyz**); */ \ - void mem_free_##name(type** obj) { mempool_free(&MEM_FIELD(name), obj); } \ + void mem_free_##NAME(TYPE** obj) { mempool_free(&MEM_FIELD(NAME), obj); } \ /* xyz* mem_get_xyz(xyz_idx); */ \ - type* mem_get_##name(NAMED_INDEX(name) index) { \ + TYPE* mem_get_##NAME(NAMED_INDEX(NAME) index) { \ assert(index.val != 0); /* 0 is the dummy allocation. */ \ - return mempool_get_block(&MEM_FIELD(name), index.val); \ + return mempool_get_block(&MEM_FIELD(NAME), index.val); \ } \ /* xyz_idx mem_get_xyz_index(const xyz*); */ \ - NAMED_INDEX(name) mem_get_##name##_index(const type* obj) { \ - return (NAMED_INDEX(name)){ \ - .val = mempool_get_block_index(&MEM_FIELD(name), obj)}; \ + NAMED_INDEX(NAME) mem_get_##NAME##_index(const TYPE* obj) { \ + return (NAMED_INDEX(NAME)){ \ + .val = mempool_get_block_index(&MEM_FIELD(NAME), obj)}; \ } DEF_MEMORY(anima, Anima) -- cgit v1.2.3