- 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); it.Reset(STAT_FAF_COPY);
while ((i = it.NextIndex()) >= 0) while ((i = it.NextIndex()) >= 0)
{ {
if (User[i]) FreeUser(i);
{
FreeMem(User[i]);
User[i] = NULL;
}
deletesprite(i); deletesprite(i);
} }
} }
@ -1512,7 +1507,7 @@ void PreDrawStackedWater(void)
if (New >= 0) if (New >= 0)
{ {
// spawn a user // spawn a user
User[New] = nu = (USERp)CallocMem(sizeof(USER), 1); User[New] = nu = NewUser();
ASSERT(nu != NULL); ASSERT(nu != NULL);
nu->xchange = -989898; nu->xchange = -989898;

View file

@ -491,12 +491,7 @@ void TerminateLevel(void)
// Free any track points // Free any track points
for (ndx = 0; ndx < MAX_TRACKS; ndx++) for (ndx = 0; ndx < MAX_TRACKS; ndx++)
{ {
if (Track[ndx].TrackPoint) Track[ndx].FreeTrackPoints();
{
FreeMem(Track[ndx].TrackPoint);
// !JIM! I added null assigner
Track[ndx].TrackPoint = NULL;
}
} }
// Clear the tracks // Clear the tracks

View file

@ -1441,6 +1441,26 @@ typedef struct
} RANGE,*RANGEp; } 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 // Sector Stuff - Sector Objects and Tracks
@ -1644,19 +1664,37 @@ extern ANIM Anim[MAXANIM];
extern short AnimCnt; extern short AnimCnt;
typedef struct typedef struct TRACK_POINT
{ {
int x,y,z; int x,y,z;
short ang, tag_low, tag_high, filler; short ang, tag_low, tag_high, filler;
} TRACK_POINT, *TRACK_POINTp; } *TRACK_POINTp;
typedef struct typedef struct TRACK
{ {
TRACK_POINTp TrackPoint; TRACK_POINTp TrackPoint;
int ttflags; int ttflags;
short flags; short flags;
short NumPoints; 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 // 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); start0 = SpawnSprite(MultiStatList[stat], ST1, NULL, pp->cursectnum, pp->posx, pp->posy, pp->posz, pp->angle.ang.asbuild(), 0);
ASSERT(start0 >= 0); ASSERT(start0 >= 0);
if (User[start0]) FreeUser(start0);
{
FreeMem(User[start0]);
User[start0] = NULL;
}
sprite[start0].picnum = ST1; sprite[start0].picnum = ST1;
} }

View file

@ -747,7 +747,7 @@ bool GameInterface::LoadGame()
MREAD(&SpriteNum, sizeof(SpriteNum),1,fil); MREAD(&SpriteNum, sizeof(SpriteNum),1,fil);
while (SpriteNum != -1) while (SpriteNum != -1)
{ {
User[SpriteNum] = u = (USERp)CallocMem(sizeof(USER), 1); User[SpriteNum] = u = NewUser();
MREAD(u,sizeof(USER),1,fil); MREAD(u,sizeof(USER),1,fil);
if (u->WallShade) if (u->WallShade)

View file

@ -793,8 +793,7 @@ KillSprite(int16_t SpriteNum)
FreeMem(u->rotator); FreeMem(u->rotator);
} }
FreeMem(User[SpriteNum]); FreeUser(SpriteNum);
User[SpriteNum] = 0;
} }
FVector3 pos = GetSoundPos(&sprite[SpriteNum].pos); FVector3 pos = GetSoundPos(&sprite[SpriteNum].pos);
@ -887,7 +886,7 @@ SpawnUser(short SpriteNum, short id, STATEp state)
ASSERT(!Prediction); ASSERT(!Prediction);
User[SpriteNum] = u = (USERp) CallocMem(sizeof(USER), 1); User[SpriteNum] = u = NewUser();
PRODUCTION_ASSERT(u != NULL); PRODUCTION_ASSERT(u != NULL);
@ -5175,8 +5174,7 @@ DoGrating(short SpriteNum)
change_sprite_stat(SpriteNum, STAT_DEFAULT); change_sprite_stat(SpriteNum, STAT_DEFAULT);
if (User[SpriteNum]) if (User[SpriteNum])
{ {
FreeMem(User[SpriteNum]); FreeUser(SpriteNum);
User[SpriteNum] = 0;
} }
} }

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); ////DSPRINTF(ds,"1 ndx = %d, numpoints = %d\n", ndx, Track[ndx].NumPoints);
//MONO_PRINT(ds); //MONO_PRINT(ds);
FreeMem(Track[ndx].TrackPoint); Track[ndx].SetTrackSize(4);
Track[ndx].TrackPoint = (TRACK_POINTp)CallocMem((4 * sizeof(TRACK_POINT)), 1);
tp = Track[ndx].TrackPoint; tp = Track[ndx].TrackPoint;
t = &Track[ndx]; t = &Track[ndx];
@ -438,8 +437,7 @@ void QuickScanSetup(short stat, short lotag, short type)
ASSERT(ndx < MAX_TRACKS); ASSERT(ndx < MAX_TRACKS);
// save space for 3 points // save space for 3 points
FreeMem(Track[ndx].TrackPoint); Track[ndx].SetTrackSize(4);
Track[ndx].TrackPoint = (TRACK_POINTp)CallocMem((4 * sizeof(TRACK_POINT)), 1);
ASSERT(Track[ndx].TrackPoint != NULL); ASSERT(Track[ndx].TrackPoint != NULL);
@ -499,8 +497,7 @@ void QuickExitSetup(short stat, short type)
ASSERT(ndx < MAX_TRACKS); ASSERT(ndx < MAX_TRACKS);
// save space for 3 points // save space for 3 points
FreeMem(Track[ndx].TrackPoint); Track[ndx].SetTrackSize(4);
Track[ndx].TrackPoint = (TRACK_POINTp)CallocMem((4 * sizeof(TRACK_POINT)), 1);
ASSERT(Track[ndx].TrackPoint != NULL); ASSERT(Track[ndx].TrackPoint != NULL);
@ -555,8 +552,7 @@ void QuickLadderSetup(short stat, short lotag, short type)
ASSERT(ndx < MAX_TRACKS); ASSERT(ndx < MAX_TRACKS);
// save space for 3 points // save space for 3 points
FreeMem(Track[ndx].TrackPoint); Track[ndx].SetTrackSize(4);
Track[ndx].TrackPoint = (TRACK_POINTp)CallocMem((4 * sizeof(TRACK_POINT)), 1);
ASSERT(Track[ndx].TrackPoint != NULL); ASSERT(Track[ndx].TrackPoint != NULL);

View file

@ -20565,8 +20565,7 @@ int QueueStar(short SpriteNum)
// new star // new star
if (User[SpriteNum]) if (User[SpriteNum])
{ {
FreeMem(User[SpriteNum]); FreeUser(SpriteNum);
User[SpriteNum] = NULL;
} }
change_sprite_stat(SpriteNum, STAT_STAR_QUEUE); change_sprite_stat(SpriteNum, STAT_STAR_QUEUE);
StarQueue[StarQueueHead] = SpriteNum; StarQueue[StarQueueHead] = SpriteNum;
@ -21084,8 +21083,7 @@ int QueueGeneric(short SpriteNum, short pic)
{ {
if (User[SpriteNum]) if (User[SpriteNum])
{ {
FreeMem(User[SpriteNum]); FreeUser(SpriteNum);
User[SpriteNum] = NULL;
} }
change_sprite_stat(SpriteNum, STAT_GENERIC_QUEUE); change_sprite_stat(SpriteNum, STAT_GENERIC_QUEUE);
GenericQueue[GenericQueueHead] = SpriteNum; GenericQueue[GenericQueueHead] = SpriteNum;