A bit of cleanup with memory issues.

First, it is now possible to disable the memcache (which is there to cache the texcache) by setting the new cvar 'r_memcache' to 0. Do this if you're constrained on memory or getting crashes when e.g. doing vidrestart often. Also, the memcache will disable itself (and free its storage, if it's there) the first time it fails to allocate.

Fix a strcpy with identical arguments in game.c and a couple of uninitialized mem accesses related to Polymer lights.


git-svn-id: https://svn.eduke32.com/eduke32@1941 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2011-07-22 13:32:01 +00:00
parent 0e727f01d3
commit d4f234b09f
5 changed files with 53 additions and 21 deletions

View file

@ -569,6 +569,7 @@ int32_t app_main(int32_t argc, const char **argv)
for (i=0; i < g_defModulesNum; ++i)
Bfree (g_defModules[i]);
Bfree (g_defModules);
g_defModules = NULL; // be defensive...
k = 0;
for (i=0; i<256; i++)

View file

@ -1296,7 +1296,7 @@ void polymer_updatesprite(int32_t snum)
return;
}
prsprites[tspr->owner]->plane.buffer = (GLfloat *) Bmalloc(4 * sizeof(GLfloat) * 5);
prsprites[tspr->owner]->plane.buffer = (GLfloat *) Bcalloc(4, sizeof(GLfloat) * 5); // XXX
prsprites[tspr->owner]->plane.vertcount = 4;
}
@ -2799,7 +2799,7 @@ static void polymer_updatewall(int16_t wallnum)
}
if (w->wall.buffer == NULL) {
w->wall.buffer = Bmalloc(4 * sizeof(GLfloat) * 5);
w->wall.buffer = Bcalloc(4, sizeof(GLfloat) * 5); // XXX
w->wall.vertcount = 4;
}

View file

@ -147,6 +147,7 @@ int32_t glanisotropy = 1; // 0 = maximum supported by card
int32_t glusetexcompr = 1;
int32_t gltexfiltermode = 2; // GL_NEAREST_MIPMAP_NEAREST
int32_t glusetexcache = 2;
static int32_t glusememcache = 1;
int32_t glmultisample = 0, glnvmultisamplehint = 0;
int32_t gltexmaxsize = 0; // 0 means autodetection on first run
int32_t gltexmiplevel = 0; // discards this many mipmap levels
@ -285,6 +286,8 @@ FILE *cacheindexptr = NULL;
uint8_t *memcachedata = NULL;
int32_t memcachesize = -1;
int32_t cachepos = 0;
// Set to 1 when we failed (re)allocating space for the memcache:
static int32_t dont_alloc_memcache = 0;
hashtable_t h_texcache = { 1024, NULL };
@ -564,11 +567,24 @@ void polymost_cachesync(void)
if (memcachedata && cachefilehandle != -1 && filelength(cachefilehandle) > memcachesize)
{
size_t len = filelength(cachefilehandle);
initprintf("Syncing memcache to texcache\n");
memcachedata = (uint8_t *)Brealloc(memcachedata, len);
Blseek(cachefilehandle, memcachesize, BSEEK_SET);
Bread(cachefilehandle, memcachedata + memcachesize, len - memcachesize);
memcachesize = len;
uint8_t *tmpptr = (uint8_t *)Brealloc(memcachedata, len);
if (!tmpptr)
{
Bfree(memcachedata);
memcachedata = NULL;
memcachesize = -1;
initprintf("Failed syncing memcache to texcache, disabling memcache.\n");
dont_alloc_memcache = 1;
}
else
{
initprintf("Syncing memcache to texcache\n");
memcachedata = tmpptr;
Blseek(cachefilehandle, memcachesize, BSEEK_SET);
Bread(cachefilehandle, memcachedata + memcachesize, len - memcachesize);
memcachesize = len;
}
}
}
@ -781,23 +797,35 @@ void polymost_glinit()
return;
}
memcachesize = filelength(cachefilehandle);
if (memcachesize > 0)
if (glusememcache && !dont_alloc_memcache)
{
memcachedata = (uint8_t *)Brealloc(memcachedata, memcachesize);
memcachesize = filelength(cachefilehandle);
if (!memcachedata)
if (memcachesize > 0)
{
initprintf("Failed allocating %d bytes for memcache\n", memcachesize);
memcachesize = -1;
}
uint8_t *tmpptr = (uint8_t *)Brealloc(memcachedata, memcachesize);
if (Bread(cachefilehandle, memcachedata, memcachesize) != memcachesize)
{
initprintf("Failed reading texcache into memcache!\n");
Bfree(memcachedata);
memcachesize = -1;
if (!tmpptr)
{
initprintf("Failed allocating %d bytes for memcache, disabling memcache.\n", memcachesize);
if (memcachedata)
Bfree(memcachedata);
memcachedata = NULL;
memcachesize = -1;
dont_alloc_memcache = 1;
}
else
{
memcachedata = tmpptr;
if (Bread(cachefilehandle, memcachedata, memcachesize) != memcachesize)
{
initprintf("Failed reading texcache into memcache!\n");
Bfree(memcachedata);
memcachedata = NULL;
memcachesize = -1;
}
}
}
}
@ -6269,6 +6297,7 @@ void polymost_initosdfuncs(void)
{ "r_shadescale_unbounded","r_shadescale_unbounded: enable/disable allowance of complete blackness",(void *) &shadescale_unbounded, CVAR_BOOL, 0, 1 },
{ "r_swapinterval","r_swapinterval: sets the GL swap interval (VSync)",(void *) &vsync, CVAR_BOOL|CVAR_FUNCPTR, 0, 1 },
{ "r_texcache","r_texcache: enable/disable OpenGL compressed texture cache",(void *) &glusetexcache, CVAR_INT, 0, 2 },
{ "r_memcache","r_memcache: enable/disable texture cache memory cache",(void *) &glusememcache, CVAR_BOOL, 0, 1 },
{ "r_texcompr","r_texcompr: enable/disable OpenGL texture compression",(void *) &glusetexcompr, CVAR_BOOL, 0, 1 },
{ "r_textureanisotropy", "r_textureanisotropy: changes the OpenGL texture anisotropy setting", (void *) &glanisotropy, CVAR_INT|CVAR_FUNCPTR, 0, 16 },
{ "r_texturemaxsize","r_texturemaxsize: changes the maximum OpenGL texture size limit",(void *) &gltexmaxsize, CVAR_INT | CVAR_NOSAVE, 0, 4096 },

View file

@ -705,6 +705,7 @@ void G_AddGameLight(int32_t radius, int32_t srcsprite, int32_t zoffset, int32_t
#pragma pack(push,1)
_prlight mylight;
#pragma pack(pop)
Bmemset(&mylight, 0, sizeof(mylight));
mylight.sector = s->sectnum;
mylight.x = s->x;

View file

@ -10021,7 +10021,8 @@ CLEAN_DIRECTORY:
initprintf("Using '%s' as definitions file\n", g_defNamePtr);
}
if (g_skipDefaultDefs == 0)
Bstrcpy(g_defNamePtr, defsfilename); // it MAY have changed, with NAM/WWII GI
if (g_defNamePtr != defsfilename)
Bstrcpy(g_defNamePtr, defsfilename); // it MAY have changed, with NAM/WWII GI
flushlogwindow = 0;
loaddefinitions_game(g_defNamePtr, TRUE);