- SW: added some simple allocation wrappers around common code sequences.

This commit is contained in:
Christoph Oelckers 2020-12-01 16:53:29 +01:00
parent 6dfe869614
commit b52668ecdf
8 changed files with 56 additions and 40 deletions

View file

@ -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;

View file

@ -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

View file

@ -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

View file

@ -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;
}

View file

@ -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)

View file

@ -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);
}
}

View file

@ -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);

View file

@ -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;