From b52668ecdf73b74aa1e6f48dfd8784d7e93c32c5 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Tue, 1 Dec 2020 16:53:29 +0100 Subject: [PATCH] - SW: added some simple allocation wrappers around common code sequences. --- source/sw/src/draw.cpp | 9 ++------ source/sw/src/game.cpp | 7 +----- source/sw/src/game.h | 46 ++++++++++++++++++++++++++++++++++++---- source/sw/src/player.cpp | 6 +----- source/sw/src/save.cpp | 2 +- source/sw/src/sprite.cpp | 8 +++---- source/sw/src/track.cpp | 12 ++++------- source/sw/src/weapon.cpp | 6 ++---- 8 files changed, 56 insertions(+), 40 deletions(-) diff --git a/source/sw/src/draw.cpp b/source/sw/src/draw.cpp index 8aef77b39..ed7e3ebce 100644 --- a/source/sw/src/draw.cpp +++ b/source/sw/src/draw.cpp @@ -1404,12 +1404,7 @@ PostDraw(void) it.Reset(STAT_FAF_COPY); while ((i = it.NextIndex()) >= 0) { - if (User[i]) - { - FreeMem(User[i]); - User[i] = NULL; - } - + FreeUser(i); deletesprite(i); } } @@ -1512,7 +1507,7 @@ void PreDrawStackedWater(void) if (New >= 0) { // spawn a user - User[New] = nu = (USERp)CallocMem(sizeof(USER), 1); + User[New] = nu = NewUser(); ASSERT(nu != NULL); nu->xchange = -989898; diff --git a/source/sw/src/game.cpp b/source/sw/src/game.cpp index e6e04c058..16eae2bb5 100644 --- a/source/sw/src/game.cpp +++ b/source/sw/src/game.cpp @@ -491,12 +491,7 @@ void TerminateLevel(void) // Free any track points for (ndx = 0; ndx < MAX_TRACKS; ndx++) { - if (Track[ndx].TrackPoint) - { - FreeMem(Track[ndx].TrackPoint); - // !JIM! I added null assigner - Track[ndx].TrackPoint = NULL; - } + Track[ndx].FreeTrackPoints(); } // Clear the tracks diff --git a/source/sw/src/game.h b/source/sw/src/game.h index 850532875..0843f5098 100644 --- a/source/sw/src/game.h +++ b/source/sw/src/game.h @@ -1441,6 +1441,26 @@ typedef struct } RANGE,*RANGEp; +inline void ClearUser(USER* user) +{ + *user = {}; +} + +inline USER* NewUser() +{ + auto u = (USER*)M_Calloc(sizeof(USER), 1);// new USER; + ClearUser(u); + return u; +} + +inline void FreeUser(int num) +{ + if (User[num]) M_Free(User[num]);// delete User[num]; + User[num] = nullptr; +} + + + /////////////////////////////////////////////////////////////////////////////////////////// // // Sector Stuff - Sector Objects and Tracks @@ -1644,19 +1664,37 @@ extern ANIM Anim[MAXANIM]; extern short AnimCnt; -typedef struct +typedef struct TRACK_POINT { int x,y,z; short ang, tag_low, tag_high, filler; -} TRACK_POINT, *TRACK_POINTp; +} *TRACK_POINTp; -typedef struct +typedef struct TRACK { TRACK_POINTp TrackPoint; int ttflags; short flags; short NumPoints; -} TRACK, *TRACKp; + + void FreeTrackPoints() + { + if (TrackPoint) + { + M_Free(TrackPoint); + // !JIM! I added null assigner + TrackPoint = nullptr; + } + } + + TRACK_POINTp SetTrackSize(unsigned newsize) + { + FreeTrackPoints(); + TrackPoint = (TRACK_POINTp)M_Calloc((newsize * sizeof(TRACK_POINT)), 1); + return TrackPoint; + } + +}*TRACKp; // Most track type flags are in tags.h diff --git a/source/sw/src/player.cpp b/source/sw/src/player.cpp index 9b8df669d..d7497b8ec 100644 --- a/source/sw/src/player.cpp +++ b/source/sw/src/player.cpp @@ -7495,11 +7495,7 @@ InitMultiPlayerInfo(void) start0 = SpawnSprite(MultiStatList[stat], ST1, NULL, pp->cursectnum, pp->posx, pp->posy, pp->posz, pp->angle.ang.asbuild(), 0); ASSERT(start0 >= 0); - if (User[start0]) - { - FreeMem(User[start0]); - User[start0] = NULL; - } + FreeUser(start0); sprite[start0].picnum = ST1; } diff --git a/source/sw/src/save.cpp b/source/sw/src/save.cpp index 038679833..247040c4b 100644 --- a/source/sw/src/save.cpp +++ b/source/sw/src/save.cpp @@ -747,7 +747,7 @@ bool GameInterface::LoadGame() MREAD(&SpriteNum, sizeof(SpriteNum),1,fil); while (SpriteNum != -1) { - User[SpriteNum] = u = (USERp)CallocMem(sizeof(USER), 1); + User[SpriteNum] = u = NewUser(); MREAD(u,sizeof(USER),1,fil); if (u->WallShade) diff --git a/source/sw/src/sprite.cpp b/source/sw/src/sprite.cpp index 42b540387..6a431210d 100644 --- a/source/sw/src/sprite.cpp +++ b/source/sw/src/sprite.cpp @@ -793,8 +793,7 @@ KillSprite(int16_t SpriteNum) FreeMem(u->rotator); } - FreeMem(User[SpriteNum]); - User[SpriteNum] = 0; + FreeUser(SpriteNum); } FVector3 pos = GetSoundPos(&sprite[SpriteNum].pos); @@ -887,7 +886,7 @@ SpawnUser(short SpriteNum, short id, STATEp state) ASSERT(!Prediction); - User[SpriteNum] = u = (USERp) CallocMem(sizeof(USER), 1); + User[SpriteNum] = u = NewUser(); PRODUCTION_ASSERT(u != NULL); @@ -5175,8 +5174,7 @@ DoGrating(short SpriteNum) change_sprite_stat(SpriteNum, STAT_DEFAULT); if (User[SpriteNum]) { - FreeMem(User[SpriteNum]); - User[SpriteNum] = 0; + FreeUser(SpriteNum); } } diff --git a/source/sw/src/track.cpp b/source/sw/src/track.cpp index fece6bcbc..f26056362 100644 --- a/source/sw/src/track.cpp +++ b/source/sw/src/track.cpp @@ -374,8 +374,7 @@ void QuickJumpSetup(short stat, short lotag, short type) ////DSPRINTF(ds,"1 ndx = %d, numpoints = %d\n", ndx, Track[ndx].NumPoints); //MONO_PRINT(ds); - FreeMem(Track[ndx].TrackPoint); - Track[ndx].TrackPoint = (TRACK_POINTp)CallocMem((4 * sizeof(TRACK_POINT)), 1); + Track[ndx].SetTrackSize(4); tp = Track[ndx].TrackPoint; t = &Track[ndx]; @@ -438,8 +437,7 @@ void QuickScanSetup(short stat, short lotag, short type) ASSERT(ndx < MAX_TRACKS); // save space for 3 points - FreeMem(Track[ndx].TrackPoint); - Track[ndx].TrackPoint = (TRACK_POINTp)CallocMem((4 * sizeof(TRACK_POINT)), 1); + Track[ndx].SetTrackSize(4); ASSERT(Track[ndx].TrackPoint != NULL); @@ -499,8 +497,7 @@ void QuickExitSetup(short stat, short type) ASSERT(ndx < MAX_TRACKS); // save space for 3 points - FreeMem(Track[ndx].TrackPoint); - Track[ndx].TrackPoint = (TRACK_POINTp)CallocMem((4 * sizeof(TRACK_POINT)), 1); + Track[ndx].SetTrackSize(4); ASSERT(Track[ndx].TrackPoint != NULL); @@ -555,8 +552,7 @@ void QuickLadderSetup(short stat, short lotag, short type) ASSERT(ndx < MAX_TRACKS); // save space for 3 points - FreeMem(Track[ndx].TrackPoint); - Track[ndx].TrackPoint = (TRACK_POINTp)CallocMem((4 * sizeof(TRACK_POINT)), 1); + Track[ndx].SetTrackSize(4); ASSERT(Track[ndx].TrackPoint != NULL); diff --git a/source/sw/src/weapon.cpp b/source/sw/src/weapon.cpp index 1b084cf0d..8dfb1c0c0 100644 --- a/source/sw/src/weapon.cpp +++ b/source/sw/src/weapon.cpp @@ -20565,8 +20565,7 @@ int QueueStar(short SpriteNum) // new star if (User[SpriteNum]) { - FreeMem(User[SpriteNum]); - User[SpriteNum] = NULL; + FreeUser(SpriteNum); } change_sprite_stat(SpriteNum, STAT_STAR_QUEUE); StarQueue[StarQueueHead] = SpriteNum; @@ -21084,8 +21083,7 @@ int QueueGeneric(short SpriteNum, short pic) { if (User[SpriteNum]) { - FreeMem(User[SpriteNum]); - User[SpriteNum] = NULL; + FreeUser(SpriteNum); } change_sprite_stat(SpriteNum, STAT_GENERIC_QUEUE); GenericQueue[GenericQueueHead] = SpriteNum;