From ef9f323b9cbfd9e22e902be31bc22c0e47bb156e Mon Sep 17 00:00:00 2001 From: MysterD Date: Mon, 15 May 2023 12:03:57 -0700 Subject: [PATCH] Schedule level pool to be freed at a later time --- data/dynos.cpp.h | 1 + data/dynos_c.cpp | 1 + data/dynos_mgr_models.cpp | 27 +++++++++++++++++++++++++-- data/dynos_warps.cpp | 2 +- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/data/dynos.cpp.h b/data/dynos.cpp.h index 9c6d0ede..2d2c1726 100644 --- a/data/dynos.cpp.h +++ b/data/dynos.cpp.h @@ -966,6 +966,7 @@ void DynOS_MovtexQC_ModShutdown(); struct GraphNode* DynOS_Model_LoadGeo(enum ModelPool aModelPool, void* aAsset); struct GraphNode* DynOS_Model_LoadDl(enum ModelPool aModelPool, u8 aLayer, void* aAsset); void DynOS_Model_ClearPool(enum ModelPool aModelPool); +void DynOS_Model_Update(); // // Bin diff --git a/data/dynos_c.cpp b/data/dynos_c.cpp index 09f7f2bf..5aad0f45 100644 --- a/data/dynos_c.cpp +++ b/data/dynos_c.cpp @@ -14,6 +14,7 @@ void *dynos_update_cmd(void *cmd) { } void dynos_update_gfx() { + DynOS_Model_Update(); return DynOS_UpdateGfx(); } diff --git a/data/dynos_mgr_models.cpp b/data/dynos_mgr_models.cpp index 002a56c0..1a9db6ac 100644 --- a/data/dynos_mgr_models.cpp +++ b/data/dynos_mgr_models.cpp @@ -1,4 +1,5 @@ #include +#include #include "dynos.cpp.h" extern "C" { @@ -6,8 +7,14 @@ extern "C" { #include "engine/graph_node.h" } +struct ScheduledFreePool { + struct DynamicPool* pool; + u32 timeout; +}; + static struct DynamicPool* sModelPools[MODEL_POOL_MAX] = { 0 }; static std::map sModelMap[MODEL_POOL_MAX]; +static std::vector sPoolsToFree; struct GraphNode* DynOS_Model_LoadGeo(enum ModelPool aModelPool, void* aAsset) { // sanity check pool @@ -58,11 +65,27 @@ struct GraphNode* DynOS_Model_LoadDl(enum ModelPool aModelPool, u8 aLayer, void* void DynOS_Model_ClearPool(enum ModelPool aModelPool) { if (!sModelPools[aModelPool]) { return; } - // free and realloc pool - dynamic_pool_free_pool(sModelPools[aModelPool]); + // schedule pool to be freed + sPoolsToFree.push_back({ + .pool = sModelPools[aModelPool], + .timeout = 30 + }); + + // clear pointer sModelPools[aModelPool] = NULL; // clear map auto& map = sModelMap[aModelPool]; map.clear(); } + +void DynOS_Model_Update() { + for (auto it = sPoolsToFree.begin(); it != sPoolsToFree.end(); ) { + if (--it->timeout <= 0) { + dynamic_pool_free_pool(it->pool); + it = sPoolsToFree.erase(it); + } else { + it++; + } + } +} \ No newline at end of file diff --git a/data/dynos_warps.cpp b/data/dynos_warps.cpp index 5bde12f4..9a2a95c0 100644 --- a/data/dynos_warps.cpp +++ b/data/dynos_warps.cpp @@ -185,7 +185,7 @@ void DynOS_Warp_SetParam(s32 aLevel, s32 aIndex) { break; case LEVEL_WDW: - if (gEnvironmentRegions) { + if (gEnvironmentRegions && gEnvironmentRegionsLength > 6) { switch (aIndex) { case 1: gEnvironmentRegions[6] = *gEnvironmentLevels = 31; gWdwWaterLevelSet = 1; break; case 2: gEnvironmentRegions[6] = *gEnvironmentLevels = 1024; gWdwWaterLevelSet = 1; break;