From 5aaeff4c5d4ee86fc1ba8c211bcded7c732d6ffc Mon Sep 17 00:00:00 2001 From: Alug Date: Fri, 19 Jul 2024 22:44:12 +0200 Subject: [PATCH] improve virtual resource management --- src/p_setup.c | 26 ++++++++++++++++++-------- src/p_setup.h | 2 ++ src/p_tick.c | 2 +- src/w_wad.c | 12 ++++++++++++ 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/p_setup.c b/src/p_setup.c index c94a117f5..63a56999d 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -109,6 +109,8 @@ INT32 numstarposts; boolean levelloading; UINT8 levelfadecol; +virtres_t *curmapvirt; + // BLOCKMAP // Created from axis aligned bounding box // of the map, a rectangular array of @@ -2337,20 +2339,26 @@ static void P_MakeMapMD5(virtres_t *virt, void *dest) static void P_LoadMapFromFile(void) { - virtres_t *virt = vres_GetMap(lastloadedmaplumpnum); - - P_LoadMapData(virt); - P_LoadMapBSP(virt); - P_LoadMapLUT(virt); + P_LoadMapData(curmapvirt); + P_LoadMapBSP(curmapvirt); + P_LoadMapLUT(curmapvirt); P_LoadLineDefs2(); P_GroupLines(); - P_PrepareRawThings(vres_Find(virt, "THINGS")->data); + P_PrepareRawThings(vres_Find(curmapvirt, "THINGS")->data); - P_MakeMapMD5(virt, &mapmd5); + P_MakeMapMD5(curmapvirt, &mapmd5); - vres_Free(virt); + // We do the following silly + // construction because vres_Free + // no-sells deletions of pointers + // that are == curmapvirt. + { + virtres_t *temp = curmapvirt; + curmapvirt = NULL; + vres_Free(temp); + } } static void P_RunLevelScript(const char *scriptname) @@ -2803,6 +2811,8 @@ boolean P_SetupLevel(boolean skipprecip) if (lastloadedmaplumpnum == INT16_MAX) I_Error("Map %s not found.\n", maplumpname); + curmapvirt = vres_GetMap(lastloadedmaplumpnum); + R_ReInitColormaps(mapheaderinfo[gamemap-1]->palette, (encoremode ? W_CheckNumForName(va("%sE", maplumpname)) : LUMPERROR)); CON_SetupBackColormap(); diff --git a/src/p_setup.h b/src/p_setup.h index 9b96225ad..5df0f0b68 100644 --- a/src/p_setup.h +++ b/src/p_setup.h @@ -17,6 +17,7 @@ #include "doomdata.h" #include "doomstat.h" #include "r_defs.h" +#include "w_wad.h" extern SINT8 midgamejoin; @@ -32,6 +33,7 @@ extern boolean levelloading; extern UINT8 levelfadecol; extern lumpnum_t lastloadedmaplumpnum; // for comparative savegame +extern virtres_t *curmapvirt; // // MAP used flats lookup table // diff --git a/src/p_tick.c b/src/p_tick.c index 2044534f2..bbe6c1fb2 100644 --- a/src/p_tick.c +++ b/src/p_tick.c @@ -286,7 +286,7 @@ mobj_t *P_SetTarget(mobj_t **mop, mobj_t *targ) { if (*mop) // If there was a target already, decrease its refcount (*mop)->thinker.references--; -if ((*mop = targ) != NULL) // Set new target and if non-NULL, increase its counter + if ((*mop = targ) != NULL) // Set new target and if non-NULL, increase its counter targ->thinker.references++; return targ; } diff --git a/src/w_wad.c b/src/w_wad.c index 506a30022..f14eba245 100644 --- a/src/w_wad.c +++ b/src/w_wad.c @@ -2135,6 +2135,12 @@ virtres_t* vres_GetMap(lumpnum_t lumpnum) virtlump_t* vlumps = NULL; size_t numlumps = 0; + if (lastloadedmaplumpnum == lumpnum && curmapvirt != NULL) + { + // Avoid duplicating all our hard work. + return curmapvirt; + } + if (W_IsLumpWad(lumpnum)) { UINT32 realentry; @@ -2211,6 +2217,12 @@ virtres_t* vres_GetMap(lumpnum_t lumpnum) */ void vres_Free(virtres_t* vres) { + if (vres == curmapvirt) + { + // No-sell multiple references. + return; + } + while (vres->numlumps--) Z_Free(vres->vlumps[vres->numlumps].data); Z_Free(vres->vlumps);