dynamically allocated script buffers

this is still broken


git-svn-id: https://svn.eduke32.com/eduke32@567 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
terminx 2007-08-29 04:01:21 +00:00
parent 826f786dcc
commit eec7e1b2e0
6 changed files with 188 additions and 49 deletions

View file

@ -148,7 +148,7 @@ enum gamemodes {
#define MAXCYCLERS 1024
#define MAXSCRIPTSIZE 98304
#define MAXSCRIPTSIZE 131072
#define MAXANIMATES 256
@ -437,8 +437,7 @@ typedef struct {
typedef struct {
long ox,oy,oz;
short oa,os;
} player_orig;
} player_spawnpoint;
extern char numplayersprites;
@ -529,7 +528,8 @@ extern char sounds[NUM_SOUNDS][BMAX_PATH];
// JBF 20040531: adding 16 extra to the script so we have some leeway
// to (hopefully) safely abort when hitting the limit
extern long script[MAXSCRIPTSIZE+16],*scriptptr,*insptr,*labelcode,labelcnt,defaultlabelcnt,*labeltype;
extern long *script,*scriptptr,*insptr,*labelcode,labelcnt,defaultlabelcnt,*labeltype;
extern int g_ScriptSize;
extern char *label;
extern long *actorscrptr[MAXTILES],*parsing_actor;
extern long *actorLoadEventScrptr[MAXTILES];
@ -980,7 +980,7 @@ typedef struct {
} playerdata_t;
extern input inputfifo[MOVEFIFOSIZ][MAXPLAYERS];
extern player_orig g_PlayerSpawnPoints[MAXPLAYERS];
extern player_spawnpoint g_PlayerSpawnPoints[MAXPLAYERS];
extern playerdata_t g_player[MAXPLAYERS];
#include "funct.h"

View file

@ -9314,6 +9314,8 @@ static void freeconmem(void)
Bfree(label);
if (labelcode != NULL)
Bfree(labelcode);
if (script != NULL)
Bfree(script);
}
/*

View file

@ -1443,9 +1443,9 @@ static long CountCaseStatements()
{
long lCount;
char *temptextptr = textptr;
long *savescript = scriptptr;
long *savecase = casescriptptr;
int temp_line_number = line_number;
long tempscriptptr = (unsigned)(scriptptr-script);
long tempsavecase = (unsigned)(casescriptptr-script);
casecount=0;
casescriptptr=NULL;
@ -1461,28 +1461,129 @@ static long CountCaseStatements()
checking_switch++;
textptr=temptextptr;
scriptptr=savescript;
scriptptr = (long *)(script+tempscriptptr);
line_number = temp_line_number;
lCount=casecount;
casecount=0;
casescriptptr=savecase;
casescriptptr = (long *)(script+tempsavecase);
return lCount;
}
static int parsecommand(void)
{
long i, j=0, k=0, *tempscrptr, done, tw;
long i, j=0, k=0, done, tw;
char *temptextptr;
long *tempscrptr;
if (((unsigned)(scriptptr-script) > MAXSCRIPTSIZE) && error == 0)
if ((unsigned)(scriptptr-script) > (unsigned)(g_ScriptSize-128))
{
long oscriptptr = (unsigned)(scriptptr-script);
long ocasescriptptr = (unsigned)(casescriptptr-script);
long oparsing_event = (unsigned)(parsing_event-script);
long oparsing_actor = (unsigned)(parsing_actor-script);
long olabelcode[MAXSECTORS];
long *scriptptrs;
Bmemset(&olabelcode,0,sizeof(olabelcode));
for (j=0;j<labelcnt;j++)
{
if (labeltype[j] != LABEL_DEFINE) //== LABEL_STATE || labeltype[j] == LABEL_AI || labeltype[j] == LABEL_ACTION || labeltype[j] == LABEL_MOVE)
olabelcode[j] = (long)(labelcode[j]-(long)script);
}
scriptptrs = Bcalloc(1, (g_ScriptSize+16) * sizeof(long));
for (i=0;i<g_ScriptSize-1;i++)
{
if ((long)script[i] >= (long)(&script[0]) && (long)script[i] < (long)(&script[g_ScriptSize]))
{
scriptptrs[i] = 1;
j = (long)script[i] - (long)&script[0];
script[i] = j;
}
else scriptptrs[i] = 0;
}
for (i=0;i<MAXTILES;i++)
if (actorscrptr[i])
{
j = (long)actorscrptr[i]-(long)&script[0];
actorscrptr[i] = (long *)j;
}
for (i=0;i<MAXTILES;i++)
if (actorLoadEventScrptr[i])
{
j = (long)actorLoadEventScrptr[i]-(long)&script[0];
actorLoadEventScrptr[i] = (long *)j;
}
for (i=0;i<MAXGAMEEVENTS;i++)
if (apScriptGameEvent[i])
{
j = (long)apScriptGameEvent[i]-(long)&script[0];
apScriptGameEvent[i] = (long *)j;
}
//initprintf("offset: %ld\n",(unsigned)(scriptptr-script));
g_ScriptSize += 16384;
initprintf("Increasing script buffer size to %ld bytes...\n",g_ScriptSize);
script = (long *)realloc(script, (g_ScriptSize+16) * sizeof(long));
if (script == NULL)
{
/* Bsprintf(tempbuf,"fatal error: Size of compiled CON code exceeds maximum size! (%ud, %d)\n",(unsigned)(scriptptr-script),MAXSCRIPTSIZE); */
ReportError(-1);
initprintf("%s:%ld: internal compiler error: Aborted (%ud)\n",compilefile,line_number,(unsigned)(scriptptr-script));
initprintf("%s:%ld: out of memory: Aborted (%ud)\n",compilefile,line_number,(unsigned)(scriptptr-script));
initprintf(tempbuf);
error++;
return 1;
}
scriptptr = (long *)(script+oscriptptr);
//initprintf("offset: %ld\n",(unsigned)(scriptptr-script));
if (casescriptptr != NULL)
casescriptptr = (long *)(script+ocasescriptptr);
if (parsing_event != NULL)
parsing_event = (long *)(script+oparsing_event);
if (parsing_actor != NULL)
parsing_actor = (long *)(script+oparsing_actor);
for (j=0;j<labelcnt;j++)
{
if (labeltype[j] != LABEL_DEFINE)//== LABEL_STATE || labeltype[j] == LABEL_AI || labeltype[j] == LABEL_ACTION || labeltype[j] == LABEL_MOVE)
{
labelcode[j] = (long)(script+olabelcode[j]);
}
}
for (i=0;i<g_ScriptSize-16384;i++)
if (scriptptrs[i])
{
j = (long)script[i]+(long)&script[0];
script[i] = j;
}
for (i=0;i<MAXTILES;i++)
if (actorscrptr[i])
{
j = (long)actorscrptr[i]+(long)&script[0];
actorscrptr[i] = (long *)j;
}
for (i=0;i<MAXTILES;i++)
if (actorLoadEventScrptr[i])
{
j = (long)actorLoadEventScrptr[i]+(long)&script[0];
actorLoadEventScrptr[i] = (long *)j;
}
for (i=0;i<MAXGAMEEVENTS;i++)
if (apScriptGameEvent[i])
{
j = (long)apScriptGameEvent[i]+(long)&script[0];
apScriptGameEvent[i] = (long *)j;
}
Bfree(scriptptrs);
}
if ((error+warning) > 63 || (*textptr == '\0') || (*(textptr+1) == '\0')) return 1;
@ -2435,10 +2536,13 @@ static int parsecommand(void)
case CON_ELSE:
if (checking_ifelse)
{
long offset;
checking_ifelse--;
tempscrptr = scriptptr;
offset = (unsigned)(tempscrptr-script);
scriptptr++; //Leave a spot for the fail location
parsecommand();
tempscrptr = (long *)script+offset;
*tempscrptr = (long) scriptptr;
}
else
@ -3311,17 +3415,23 @@ static int parsecommand(void)
case CON_IFVARVARN:
case CON_IFVARVARAND:
case CON_WHILEVARVARN:
{
long offset;
transmultvars(2);
tempscrptr = scriptptr;
offset = (unsigned)(tempscrptr-script);
scriptptr++; // Leave a spot for the fail location
j = keyword();
parsecommand();
tempscrptr = (long *)script+offset;
*tempscrptr = (long) scriptptr;
if (tw != CON_WHILEVARVARN) checking_ifelse++;
return 0;
}
case CON_SPGETLOTAG:
case CON_SPGETHITAG:
@ -3343,22 +3453,26 @@ static int parsecommand(void)
case CON_IFVARN:
case CON_IFVARAND:
case CON_WHILEVARN:
{
long offset;
// get the ID of the DEF
transvar();
transnum(LABEL_DEFINE); // the number to check against...
tempscrptr = scriptptr;
offset = (unsigned)(tempscrptr-script);
scriptptr++; //Leave a spot for the fail location
j = keyword();
parsecommand();
tempscrptr = (long *)script+offset;
*tempscrptr = (long) scriptptr;
if (tw != CON_WHILEVARN) checking_ifelse++;
return 0;
}
case CON_ADDLOGVAR:
// syntax: addlogvar <var>
@ -3503,6 +3617,9 @@ static int parsecommand(void)
break;
case CON_SWITCH:
{
long tempoffset;
//AddLog("Got Switch statement");
if (checking_switch)
{
@ -3514,6 +3631,7 @@ static int parsecommand(void)
transvar();
tempscrptr= scriptptr;
tempoffset = (unsigned)(tempscrptr-script);
*scriptptr++=0; // leave spot for end location (for after processing)
*scriptptr++=0; // count of case statements
casescriptptr=scriptptr; // the first case's pointer.
@ -3540,6 +3658,7 @@ static int parsecommand(void)
}
if (tempscrptr)
{
tempscrptr = (long *)(script+tempoffset);
tempscrptr[1]=(long)j; // save count of cases
}
else
@ -3559,12 +3678,12 @@ static int parsecommand(void)
//AddLog(g_szBuf);
casecount=0;
while (parsecommand() == 0)
{
//Bsprintf(g_szBuf,"SWITCH2: '%.22s'",textptr);
//AddLog(g_szBuf);
}
//Bsprintf(g_szBuf,"SWITCHXX: '%.22s'",textptr);
//AddLog(g_szBuf);
// done processing switch. clean up.
@ -3576,7 +3695,7 @@ static int parsecommand(void)
casecount=0;
if (tempscrptr)
{
tempscrptr[0]= (long)scriptptr - (long)&script[0]; // save 'end' location
tempscrptr[0]= (unsigned)(scriptptr-script); // save 'end' location
}
else
{
@ -3593,6 +3712,7 @@ static int parsecommand(void)
//AddLog(g_szBuf);
}
//AddLog("End of Switch statement");
}
break;
case CON_CASE:
@ -3622,7 +3742,7 @@ repeatcase:
{
//AddLog("Adding value to script");
casescriptptr[casecount++]=j; // save value
casescriptptr[casecount]=(long)((long*)scriptptr-&script[0]); // save offset
casescriptptr[casecount]=(long)((long*)scriptptr-script); // save offset
}
// j = keyword();
//Bsprintf(g_szBuf,"case3: %.12s",textptr);
@ -3670,7 +3790,7 @@ repeatcase:
}
if (casescriptptr)
{
casescriptptr[0]=(long)(scriptptr-&script[0]); // save offset
casescriptptr[0]=(long)(scriptptr-script); // save offset
}
//Bsprintf(g_szBuf,"default: '%.22s'",textptr);
//AddLog(g_szBuf);
@ -3827,6 +3947,8 @@ repeatcase:
case CON_IFAWAYFROMWALL:
case CON_IFCANSEETARGET:
case CON_IFNOSOUNDS:
{
long offset;
if (tw == CON_IFP)
{
j = 0;
@ -3842,16 +3964,18 @@ repeatcase:
}
tempscrptr = scriptptr;
offset = (unsigned)(tempscrptr-script);
scriptptr++; //Leave a spot for the fail location
j = keyword();
parsecommand();
tempscrptr = (long *)script+offset;
*tempscrptr = (long) scriptptr;
checking_ifelse++;
return 0;
}
case CON_LEFTBRACE:
if (!(parsing_state || parsing_actor || parsing_event))
{
@ -4735,7 +4859,10 @@ void loadefs(const char *filenam)
clearbuf(actorscrptr,MAXTILES,0L); // JBF 20040531: MAXSPRITES? I think Todd meant MAXTILES...
clearbuf(actorLoadEventScrptr,MAXTILES,0L); // I think this should be here...
clearbufbyte(actortype,MAXTILES,0L);
clearbufbyte(script,sizeof(script),0l); // JBF 20040531: yes? no?
// clearbufbyte(script,sizeof(script),0l); // JBF 20040531: yes? no?
if (script != NULL)
Bfree(script);
script = Bcalloc(1,sizeof(long)*(g_ScriptSize+16));
labelcnt = defaultlabelcnt = 0;
scriptptr = script+1;
@ -4816,7 +4943,7 @@ void loadefs(const char *filenam)
k++;
}
initprintf("\nCompiled code size: %ld/%ld bytes\n",(unsigned)(scriptptr-script),MAXSCRIPTSIZE);
initprintf("\nCompiled code size: %ld/%ld bytes\n",(unsigned)(scriptptr-script),g_ScriptSize);
initprintf("%ld/%ld labels, %d/%d variables\n",labelcnt,min((sizeof(sector)/sizeof(long)),(sizeof(sprite)/(1<<6))),iGameVarCount,MAXGAMEVARS);
initprintf("%ld event definitions, %ld defined actors\n\n",j,k);

View file

@ -4506,15 +4506,15 @@ static int parse(void)
case CON_LDIST:
insptr++;
{
int distvar = *insptr++, xvar = *insptr++, yvar = *insptr++, distx=0;
int distvar = *insptr++, xvar = GetGameVarID(*insptr++, g_i, g_p), yvar = GetGameVarID(*insptr++, g_i, g_p), distx=0;
switch (tw)
{
case CON_DIST:
distx = dist(&sprite[GetGameVarID(xvar, g_i, g_p)],&sprite[GetGameVarID(yvar, g_i, g_p)]);
distx = dist(&sprite[xvar],&sprite[yvar]);
break;
case CON_LDIST:
distx = ldist(&sprite[GetGameVarID(xvar, g_i, g_p)],&sprite[GetGameVarID(yvar, g_i, g_p)]);
distx = ldist(&sprite[xvar],&sprite[yvar]);
break;
}
@ -4526,14 +4526,16 @@ static int parse(void)
case CON_GETANGLE:
insptr++;
{
int angvar = *insptr++, xvar = *insptr++, yvar = *insptr++;
int angvar = *insptr++;
int xvar = GetGameVarID(*insptr++, g_i, g_p);
int yvar = GetGameVarID(*insptr++, g_i, g_p);
if (tw==CON_GETANGLE)
{
SetGameVarID(angvar, getangle(GetGameVarID(xvar, g_i, g_p),GetGameVarID(yvar, g_i, g_p)), g_i, g_p);
SetGameVarID(angvar, getangle(xvar,yvar), g_i, g_p);
break;
}
SetGameVarID(angvar, getincangle(GetGameVarID(xvar, g_i, g_p),GetGameVarID(yvar, g_i, g_p)), g_i, g_p);
SetGameVarID(angvar, getincangle(xvar,yvar), g_i, g_p);
break;
}

View file

@ -88,7 +88,7 @@ char numplayersprites,loadfromgrouponly=0,earthquaketime;
long fricxv,fricyv;
playerdata_t g_player[MAXPLAYERS];
input inputfifo[MOVEFIFOSIZ][MAXPLAYERS];
player_orig g_PlayerSpawnPoints[MAXPLAYERS];
player_spawnpoint g_PlayerSpawnPoints[MAXPLAYERS];
user_defs ud;
char pus, pub;
@ -114,7 +114,9 @@ long *actorscrptr[MAXTILES],*parsing_actor;
char *label;
char *music_pointer;
char actortype[MAXTILES];
long script[MAXSCRIPTSIZE+16];
long *script = NULL;
int g_ScriptSize = 16384;
char display_mirror,typebuflen,typebuf[141];

View file

@ -123,7 +123,8 @@ int loadplayer(int spot)
int k;
char fn[13];
char mpfn[13];
char *fnptr, scriptptrs[MAXSCRIPTSIZE];
char *fnptr;
long *scriptptrs;
long fil, bv, i, j, x;
int32 nump;
@ -263,9 +264,11 @@ int loadplayer(int spot)
if (kdfread(&cloudx[0],sizeof(short)<<7,1,fil) != 1) goto corrupt;
if (kdfread(&cloudy[0],sizeof(short)<<7,1,fil) != 1) goto corrupt;
if (kdfread(&scriptptrs[0],1,MAXSCRIPTSIZE,fil) != MAXSCRIPTSIZE) goto corrupt;
if (kdfread(&script[0],4,MAXSCRIPTSIZE,fil) != MAXSCRIPTSIZE) goto corrupt;
for (i=0;i<MAXSCRIPTSIZE;i++)
if (kdfread(&g_ScriptSize,sizeof(g_ScriptSize),1,fil) != 1) goto corrupt;
scriptptrs = Bcalloc(1,g_ScriptSize * sizeof(g_ScriptSize));
if (kdfread(&scriptptrs[0],sizeof(scriptptrs),g_ScriptSize,fil) != g_ScriptSize) goto corrupt;
if (kdfread(&script[0],sizeof(script),g_ScriptSize,fil) != g_ScriptSize) goto corrupt;
for (i=0;i<g_ScriptSize;i++)
if (scriptptrs[i])
{
j = (long)script[i]+(long)&script[0];
@ -287,7 +290,7 @@ int loadplayer(int spot)
actorLoadEventScrptr[i] = (long *)j;
}
if (kdfread(&scriptptrs[0],1,MAXSPRITES,fil) != MAXSPRITES) goto corrupt;
if (kdfread(&scriptptrs[0],sizeof(scriptptrs),MAXSPRITES,fil) != MAXSPRITES) goto corrupt;
if (kdfread(&hittype[0],sizeof(weaponhit),MAXSPRITES,fil) != MAXSPRITES) goto corrupt;
for (i=0;i<MAXSPRITES;i++)
@ -515,7 +518,8 @@ int saveplayer(int spot)
long i, j;
char fn[13];
char mpfn[13];
char *fnptr,scriptptrs[MAXSCRIPTSIZE];
char *fnptr;
long *scriptptrs;
FILE *fil;
long bv = BYTEVERSION;
@ -611,9 +615,11 @@ int saveplayer(int spot)
dfwrite(&cloudx[0],sizeof(short)<<7,1,fil);
dfwrite(&cloudy[0],sizeof(short)<<7,1,fil);
for (i=0;i<MAXSCRIPTSIZE;i++)
dfwrite(&g_ScriptSize,sizeof(g_ScriptSize),1,fil);
scriptptrs = Bcalloc(1, g_ScriptSize * sizeof(scriptptrs));
for (i=0;i<g_ScriptSize;i++)
{
if ((long)script[i] >= (long)(&script[0]) && (long)script[i] < (long)(&script[MAXSCRIPTSIZE]))
if ((long)script[i] >= (long)(&script[0]) && (long)script[i] < (long)(&script[g_ScriptSize]))
{
scriptptrs[i] = 1;
j = (long)script[i] - (long)&script[0];
@ -622,10 +628,10 @@ int saveplayer(int spot)
else scriptptrs[i] = 0;
}
dfwrite(&scriptptrs[0],1,MAXSCRIPTSIZE,fil);
dfwrite(&script[0],4,MAXSCRIPTSIZE,fil);
dfwrite(&scriptptrs[0],sizeof(scriptptrs),g_ScriptSize,fil);
dfwrite(&script[0],sizeof(script),g_ScriptSize,fil);
for (i=0;i<MAXSCRIPTSIZE;i++)
for (i=0;i<g_ScriptSize;i++)
if (scriptptrs[i])
{
j = script[i]+(long)&script[0];
@ -669,24 +675,24 @@ int saveplayer(int spot)
j = (long)&script[0];
if (T2 >= j && T2 < (long)(&script[MAXSCRIPTSIZE]))
if (T2 >= j && T2 < (long)(&script[g_ScriptSize]))
{
scriptptrs[i] |= 1;
T2 -= j;
}
if (T5 >= j && T5 < (long)(&script[MAXSCRIPTSIZE]))
if (T5 >= j && T5 < (long)(&script[g_ScriptSize]))
{
scriptptrs[i] |= 2;
T5 -= j;
}
if (T6 >= j && T6 < (long)(&script[MAXSCRIPTSIZE]))
if (T6 >= j && T6 < (long)(&script[g_ScriptSize]))
{
scriptptrs[i] |= 4;
T6 -= j;
}
}
dfwrite(&scriptptrs[0],1,MAXSPRITES,fil);
dfwrite(&scriptptrs[0],sizeof(scriptptrs),MAXSPRITES,fil);
dfwrite(&hittype[0],sizeof(weaponhit),MAXSPRITES,fil);
for (i=0;i<MAXSPRITES;i++)