From 1fc7e108439cd6e3252116d41ee0659c9b53b9fa Mon Sep 17 00:00:00 2001 From: helixhorned Date: Sat, 16 May 2015 11:56:49 +0000 Subject: [PATCH] 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 --- polymer/eduke32/source/gamedef.c | 20 +++++++++++++++----- polymer/eduke32/source/gamedef.h | 2 ++ polymer/eduke32/source/savegame.c | 8 +++++--- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/polymer/eduke32/source/gamedef.c b/polymer/eduke32/source/gamedef.c index cd5f33f55..bbf476f22 100644 --- a/polymer/eduke32/source/gamedef.c +++ b/polymer/eduke32/source/gamedef.c @@ -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; diff --git a/polymer/eduke32/source/gamedef.h b/polymer/eduke32/source/gamedef.h index 413316b46..3f084c219 100644 --- a/polymer/eduke32/source/gamedef.h +++ b/polymer/eduke32/source/gamedef.h @@ -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); diff --git a/polymer/eduke32/source/savegame.c b/polymer/eduke32/source/savegame.c index 89156bf96..9ed1a8676 100644 --- a/polymer/eduke32/source/savegame.c +++ b/polymer/eduke32/source/savegame.c @@ -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