Stylistic/formatting tweaks in cache1d code.

git-svn-id: https://svn.eduke32.com/eduke32@4255 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2014-01-12 14:54:33 +00:00
parent d452fd7891
commit 3b1515e51b
4 changed files with 75 additions and 28 deletions

View file

@ -84,21 +84,21 @@ static intptr_t kzipopen(const char *filnam)
// without first calling initcache. // without first calling initcache.
// Uncomment for easier allocache-allocated bound checking (e.g. with Valgrind) // Uncomment for easier allocache-allocated bound checking (e.g. with Valgrind)
// KEEPINSYNC game.c
//#define DEBUG_ALLOCACHE_AS_MALLOC //#define DEBUG_ALLOCACHE_AS_MALLOC
#define MAXCACHEOBJECTS 9216 #define MAXCACHEOBJECTS 9216
#ifndef DEBUG_ALLOCACHE_AS_MALLOC #if !defined DEBUG_ALLOCACHE_AS_MALLOC
static int32_t cachesize = 0; static int32_t cachesize = 0;
//static int32_t cachecount = 0;
static char zerochar = 0; static char zerochar = 0;
static intptr_t cachestart = 0; static intptr_t cachestart = 0;
static int32_t agecount = 0; static int32_t agecount = 0;
static int32_t lockrecip[200]; static int32_t lockrecip[200];
#endif
int32_t cacnum = 0; int32_t cacnum = 0;
cactype cac[MAXCACHEOBJECTS]; cactype cac[MAXCACHEOBJECTS];
#endif
static char toupperlookup[256] = static char toupperlookup[256] =
{ {
@ -128,7 +128,8 @@ void initcache(intptr_t dacachestart, int32_t dacachesize)
#ifndef DEBUG_ALLOCACHE_AS_MALLOC #ifndef DEBUG_ALLOCACHE_AS_MALLOC
int32_t i; int32_t i;
for (i=1; i<200; i++) lockrecip[i] = (1<<28)/(200-i); for (i=1; i<200; i++)
lockrecip[i] = (1<<28)/(200-i);
// The following code was relocated here from engine.c, since this // The following code was relocated here from engine.c, since this
// function is only ever called once (from there), and it seems to // function is only ever called once (from there), and it seems to
@ -165,9 +166,17 @@ void allocache(intptr_t *newhandle, int32_t newbytes, char *newlockptr)
reportandexit("OUT OF MEMORY in allocache as malloc wrapper!"); reportandexit("OUT OF MEMORY in allocache as malloc wrapper!");
} }
#else #else
static void inc_and_check_cacnum(void)
{
cacnum++;
if (cacnum > MAXCACHEOBJECTS)
reportandexit("Too many objects in cache! (cacnum > MAXCACHEOBJECTS)");
}
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, z, bestz=0, bestval, besto=0, o1, sucklen, suckz;
//printf(" ==> asking for %d bytes, ", newbytes); //printf(" ==> asking for %d bytes, ", newbytes);
// Make all requests a multiple of 16 bytes // Make all requests a multiple of 16 bytes
@ -190,17 +199,34 @@ void allocache(intptr_t *newhandle, int32_t newbytes, char *newlockptr)
bestval = 0x7fffffff; o1 = cachesize; bestval = 0x7fffffff; o1 = cachesize;
for (z=cacnum-1; z>=0; z--) for (z=cacnum-1; z>=0; z--)
{ {
int32_t zz, o2, daval;
o1 -= cac[z].leng; o1 -= cac[z].leng;
o2 = o1+newbytes; if (o2 > cachesize) continue; o2 = o1+newbytes;
if (o2 > cachesize)
continue;
daval = 0; daval = 0;
for (i=o1,zz=z; i<o2; i+=cac[zz++].leng) for (i=o1,zz=z; i<o2; i+=cac[zz++].leng)
{ {
if (*cac[zz].lock == 0) continue; if (*cac[zz].lock == 0)
if (*cac[zz].lock >= 200) { daval = 0x7fffffff; break; } continue;
daval += mulscale32(cac[zz].leng+65536,lockrecip[*cac[zz].lock]);
if (daval >= bestval) break; if (*cac[zz].lock >= 200)
{
daval = 0x7fffffff;
break;
}
// Potential for eviction increases with
// - smaller item size
// - smaller lock byte value (but in [1 .. 199])
daval += mulscale32(cac[zz].leng+65536, lockrecip[*cac[zz].lock]);
if (daval >= bestval)
break;
} }
if (daval < bestval) if (daval < bestval)
{ {
bestval = daval; besto = o1; bestz = z; bestval = daval; besto = o1; bestz = z;
@ -215,33 +241,43 @@ void allocache(intptr_t *newhandle, int32_t newbytes, char *newlockptr)
//Suck things out //Suck things out
for (sucklen=-newbytes,suckz=bestz; sucklen<0; sucklen+=cac[suckz++].leng) for (sucklen=-newbytes,suckz=bestz; sucklen<0; sucklen+=cac[suckz++].leng)
if (*cac[suckz].lock) *cac[suckz].hand = 0; if (*cac[suckz].lock)
*cac[suckz].hand = 0;
//Remove all blocks except 1 //Remove all blocks except 1
suckz -= (bestz+1); cacnum -= suckz; suckz -= bestz+1;
// copybufbyte(&cac[bestz+suckz],&cac[bestz],(cacnum-bestz)*sizeof(cactype)); cacnum -= suckz;
Bmemmove(&cac[bestz], &cac[bestz+suckz], (cacnum-bestz)*sizeof(cactype)); Bmemmove(&cac[bestz], &cac[bestz+suckz], (cacnum-bestz)*sizeof(cactype));
cac[bestz].hand = newhandle; *newhandle = cachestart+(intptr_t)besto; cac[bestz].hand = newhandle;
*newhandle = cachestart + besto;
cac[bestz].leng = newbytes; cac[bestz].leng = newbytes;
cac[bestz].lock = newlockptr; cac[bestz].lock = newlockptr;
// cachecount++;
//Add new empty block if necessary //Add new empty block if necessary
if (sucklen <= 0) return; if (sucklen <= 0)
return;
bestz++; bestz++;
if (bestz == cacnum) if (bestz == cacnum)
{ {
cacnum++; if (cacnum > MAXCACHEOBJECTS) reportandexit("Too many objects in cache! (cacnum > MAXCACHEOBJECTS)"); inc_and_check_cacnum();
cac[bestz].leng = sucklen; cac[bestz].leng = sucklen;
cac[bestz].lock = &zerochar; cac[bestz].lock = &zerochar;
return; return;
} }
if (*cac[bestz].lock == 0) { cac[bestz].leng += sucklen; return; } if (*cac[bestz].lock == 0)
{
cac[bestz].leng += sucklen;
return;
}
cacnum++; if (cacnum > MAXCACHEOBJECTS) reportandexit("Too many objects in cache! (cacnum > MAXCACHEOBJECTS)"); inc_and_check_cacnum();
for (z=cacnum-1; z>bestz; z--) cac[z] = cac[z-1];
for (z=cacnum-1; z>bestz; z--)
cac[z] = cac[z-1];
cac[bestz].leng = sucklen; cac[bestz].leng = sucklen;
cac[bestz].lock = &zerochar; cac[bestz].lock = &zerochar;
} }
@ -252,16 +288,21 @@ void agecache(void)
#ifndef DEBUG_ALLOCACHE_AS_MALLOC #ifndef DEBUG_ALLOCACHE_AS_MALLOC
int32_t cnt = (cacnum>>4); int32_t cnt = (cacnum>>4);
if (agecount >= cacnum) agecount = cacnum-1; if (agecount >= cacnum)
if (agecount < 0 || !cnt) return; agecount = cacnum-1;
if (agecount < 0 || !cnt)
return;
for (; cnt>=0; cnt--) for (; cnt>=0; cnt--)
{ {
// If we have pointer to lock char and it's in [2 .. 199], decrease.
if (cac[agecount].lock && (((*cac[agecount].lock)-2)&255) < 198) if (cac[agecount].lock && (((*cac[agecount].lock)-2)&255) < 198)
(*cac[agecount].lock)--; (*cac[agecount].lock)--;
agecount--; agecount--;
if (agecount < 0) agecount = cacnum-1; if (agecount < 0)
agecount = cacnum-1;
} }
#endif #endif
} }

View file

@ -11054,11 +11054,12 @@ int32_t loadpics(const char *filename, int32_t askedsize)
for (i=localtilestart; i<=localtileend; i++) for (i=localtilestart; i<=localtileend; i++)
{ {
EDUKE32_STATIC_ASSERT(sizeof(picanm_t) == 4);
EDUKE32_STATIC_ASSERT(PICANM_ANIMTYPE_MASK == 192);
tilesizx[i] = B_LITTLE16(tilesizx[i]); tilesizx[i] = B_LITTLE16(tilesizx[i]);
tilesizy[i] = B_LITTLE16(tilesizy[i]); tilesizy[i] = B_LITTLE16(tilesizy[i]);
Bassert(sizeof(picanm_t)==4);
Bassert(PICANM_ANIMTYPE_MASK == 192);
// Old on-disk format: anim type is in the 2 highest bits of the lowest byte. // Old on-disk format: anim type is in the 2 highest bits of the lowest byte.
picanm[i].sf &= ~192; picanm[i].sf &= ~192;
picanm[i].sf |= picanm[i].num&192; picanm[i].sf |= picanm[i].num&192;

View file

@ -1371,8 +1371,14 @@ void G_GameQuit(void)
G_GameExit("Timed out."); G_GameExit("Timed out.");
} }
// Uncomment for easier allocache-allocated bound checking (e.g. with Valgrind)
// KEEPINSYNC cache1d.c
//#define DEBUG_ALLOCACHE_AS_MALLOC
#if !defined DEBUG_ALLOCACHE_AS_MALLOC
extern int32_t cacnum; extern int32_t cacnum;
extern cactype cac[]; extern cactype cac[];
#endif
static void G_ShowCacheLocks(void) static void G_ShowCacheLocks(void)
{ {
@ -1382,6 +1388,7 @@ static void G_ShowCacheLocks(void)
return; return;
k = 0; k = 0;
#if !defined DEBUG_ALLOCACHE_AS_MALLOC
for (i=cacnum-1; i>=0; i--) for (i=cacnum-1; i>=0; i--)
if ((*cac[i].lock) >= 200) if ((*cac[i].lock) >= 200)
{ {
@ -1392,7 +1399,7 @@ static void G_ShowCacheLocks(void)
printext256(0L,k,31,-1,tempbuf,1); printext256(0L,k,31,-1,tempbuf,1);
k += 6; k += 6;
} }
#endif
if (k < ydim-12) if (k < ydim-12)
k += 6; k += 6;

View file

@ -241,8 +241,6 @@ typedef struct {
} user_defs; } user_defs;
extern cactype cac[];
// this is checked against http://eduke32.com/VERSION // this is checked against http://eduke32.com/VERSION
extern const char *s_buildDate; extern const char *s_buildDate;
extern const char *g_gameNamePtr; extern const char *g_gameNamePtr;