savegame: fix g_tile[].proj memory leaks, halve number of projectile_t allocations.

git-svn-id: https://svn.eduke32.com/eduke32@5191 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2015-05-16 11:56:49 +00:00
parent 80b766f13a
commit 1fc7e10843
3 changed files with 22 additions and 8 deletions

View file

@ -2450,12 +2450,25 @@ int32_t g_numProjectiles = 0;
EDUKE32_STATIC_ASSERT(sizeof(projectile_t) == sizeof(DefaultProjectile));
void C_AllocProjectile(int32_t j)
{
g_tile[j].proj = (projectile_t *)Xmalloc(2*sizeof(projectile_t));
g_tile[j].defproj = g_tile[j].proj + 1;
}
void C_FreeProjectile(int32_t j)
{
Bfree(g_tile[j].proj);
g_tile[j].proj = g_tile[j].defproj = NULL;
}
LUNATIC_EXTERN void C_DefineProjectile(int32_t j, int32_t what, int32_t val)
{
if (g_tile[j].proj == NULL)
{
g_tile[j].proj = (projectile_t *) Xmalloc(sizeof(projectile_t));
*g_tile[j].proj = *(projectile_t *)&DefaultProjectile;
C_AllocProjectile(j);
*g_tile[j].proj = DefaultProjectile;
g_numProjectiles += 2;
}
@ -2526,9 +2539,6 @@ LUNATIC_EXTERN void C_DefineProjectile(int32_t j, int32_t what, int32_t val)
default: break;
}
if (g_tile[j].defproj == NULL)
g_tile[j].defproj = (projectile_t *)Xmalloc(sizeof(projectile_t));
*g_tile[j].defproj = *proj;
g_tile[j].flags |= SFLAG_PROJECTILE;

View file

@ -119,6 +119,8 @@ typedef projectile_t defaultprojectile_t;
extern defaultprojectile_t DefaultProjectile;
int32_t C_AllocQuote(int32_t qnum);
void C_AllocProjectile(int32_t j);
void C_FreeProjectile(int32_t j);
void C_InitQuotes(void);
void C_InitProjectiles(void);

View file

@ -1087,9 +1087,9 @@ static const dataspec_t svgm_script[] =
{ DS_SAVEFN|DS_NOCHK, (void *)&sv_prescriptsave_once, 0, 1 },
#endif
{ DS_NOCHK, &g_tile[0], sizeof(tiledata_t), MAXTILES },
{ DS_SAVEFN, (void *) &sv_preprojectilesave, 0, 1 },
{ DS_LOADFN, (void *) &sv_preprojectileload, 0, 1 },
{ DS_NOCHK, &g_tile[0], sizeof(tiledata_t), MAXTILES },
{ DS_DYNAMIC|DS_CNT(g_numProjectiles), &ProjectileData, sizeof(projectile_t), (intptr_t)&g_numProjectiles },
{ DS_SAVEFN, (void *) &sv_postprojectilesave, 0, 1 },
{ DS_LOADFN, (void *) &sv_postprojectileload, 0, 1 },
@ -1759,6 +1759,9 @@ static void sv_preprojectileload()
{
if (ProjectileData != NULL || g_numProjectiles > 0)
ProjectileData = (projectile_t *) Xrealloc(ProjectileData, sizeof(projectile_t) * g_numProjectiles);
for (int i=0; i<MAXTILES; i++)
C_FreeProjectile(i);
}
static void sv_postprojectileload()
@ -1772,8 +1775,7 @@ static void sv_postprojectileload()
{
if (g_tile[i].proj)
{
g_tile[i].proj = (projectile_t *) Xmalloc(sizeof(projectile_t));
g_tile[i].defproj = (projectile_t *) Xmalloc(sizeof(projectile_t));
C_AllocProjectile(i);
Bmemcpy(g_tile[i].proj, &ProjectileData[g_numProjectiles], sizeof(projectile_t));
Bmemcpy(g_tile[i].defproj, &ProjectileData[g_numProjectiles+1], sizeof(projectile_t));
g_numProjectiles += 2;