cache1d.c: debugging feature routing all allocache requests to malloc.

This is useful to debug out-of-bound violations for memory allocated
with that function.

git-svn-id: https://svn.eduke32.com/eduke32@2360 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2012-02-18 17:57:07 +00:00
parent 1ad110ff6a
commit b51a3d91a3
1 changed files with 19 additions and 1 deletions

View File

@ -57,6 +57,9 @@ static intptr_t kzipopen(const char *filnam)
// After calling uninitcache, it is still ok to call allocache // After calling uninitcache, it is still ok to call allocache
// without first calling initcache. // without first calling initcache.
// Uncomment for easier allocache-allocated bound checking (e.g. with Valgrind)
//#define DEBUG_ALLOCACHE_AS_MALLOC
#define MAXCACHEOBJECTS 9216 #define MAXCACHEOBJECTS 9216
static int32_t cachesize = 0; static int32_t cachesize = 0;
@ -101,6 +104,16 @@ void initcache(intptr_t dacachestart, int32_t dacachesize)
initprintf("Initialized %.1fM cache\n", (float)(dacachesize/1024.f/1024.f)); initprintf("Initialized %.1fM cache\n", (float)(dacachesize/1024.f/1024.f));
} }
#ifdef DEBUG_ALLOCACHE_AS_MALLOC
void allocache(intptr_t *newhandle, int32_t newbytes, char *newlockptr)
{
UNREFERENCED_PARAMETER(newlockptr);
*newhandle = (intptr_t)Bmalloc(newbytes);
if (!*newhandle)
reportandexit("OUT OF MEMORY in allocache as malloc wrapper!");
}
#else
void allocache(intptr_t *newhandle, int32_t newbytes, char *newlockptr) void allocache(intptr_t *newhandle, int32_t newbytes, char *newlockptr)
{ {
int32_t i, /*j,*/ z, zz, bestz=0, daval, bestval, besto=0, o1, o2, sucklen, suckz; int32_t i, /*j,*/ z, zz, bestz=0, daval, bestval, besto=0, o1, o2, sucklen, suckz;
@ -180,6 +193,7 @@ void allocache(intptr_t *newhandle, int32_t newbytes, char *newlockptr)
cac[bestz].leng = sucklen; cac[bestz].leng = sucklen;
cac[bestz].lock = &zerochar; cac[bestz].lock = &zerochar;
} }
#endif
#if 0 #if 0
void suckcache(intptr_t *suckptr) void suckcache(intptr_t *suckptr)
@ -213,6 +227,7 @@ void suckcache(intptr_t *suckptr)
void agecache(void) void agecache(void)
{ {
#ifndef DEBUG_ALLOCACHE_AS_MALLOC
int32_t cnt = (cacnum>>4); int32_t cnt = (cacnum>>4);
if (agecount >= cacnum) agecount = cacnum-1; if (agecount >= cacnum) agecount = cacnum-1;
@ -226,10 +241,12 @@ void agecache(void)
agecount--; agecount--;
if (agecount < 0) agecount = cacnum-1; if (agecount < 0) agecount = cacnum-1;
} }
#endif
} }
static void reportandexit(const char *errormessage) static void reportandexit(const char *errormessage)
{ {
#ifndef DEBUG_ALLOCACHE_AS_MALLOC
int32_t i, j; int32_t i, j;
//setvmode(0x3); //setvmode(0x3);
@ -247,8 +264,9 @@ static void reportandexit(const char *errormessage)
Bprintf("Cachesize = %d\n",cachesize); Bprintf("Cachesize = %d\n",cachesize);
Bprintf("Cacnum = %d\n",cacnum); Bprintf("Cacnum = %d\n",cacnum);
Bprintf("Cache length sum = %d\n",j); Bprintf("Cache length sum = %d\n",j);
#endif
initprintf("ERROR: %s\n",errormessage); initprintf("ERROR: %s\n",errormessage);
exit(0); exit(1);
} }
#include <errno.h> #include <errno.h>