From 9284742a11b92dfe4ef011b963240cfa588515cd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 21 Mar 1996 13:33:47 -0300 Subject: better control when growing arrays. --- fallback.c | 5 ++--- func.c | 7 ++----- lex.c | 5 ++--- lua.stx | 13 +++---------- luamem.c | 26 +++++++++++++++++++++++++- luamem.h | 16 +++++++++++++--- opcode.c | 17 ++++++++++------- table.c | 29 +++++++++++------------------ 8 files changed, 68 insertions(+), 50 deletions(-) diff --git a/fallback.c b/fallback.c index 34d0b99c..8913797f 100644 --- a/fallback.c +++ b/fallback.c @@ -3,7 +3,7 @@ ** TecCGraf - PUC-Rio */ -char *rcs_fallback="$Id: fallback.c,v 1.21 1996/03/04 13:29:10 roberto Exp roberto $"; +char *rcs_fallback="$Id: fallback.c,v 1.22 1996/03/19 22:28:37 roberto Exp roberto $"; #include #include @@ -132,8 +132,7 @@ int luaI_lock (Object *object) } /* no more empty spaces */ oldSize = lockSize; - lockSize = (lockSize == 0) ? 10 : 3*lockSize/2 + 5; - lockArray = growvector(lockArray, lockSize, Object); + lockSize = growvector(&lockArray, lockSize, Object, lockEM, MAX_WORD); for (i=oldSize; i= maxcurrvars) - { - maxcurrvars = (maxcurrvars == 0) ? LOCALVARINITSIZE : maxcurrvars*2; - currvars = growvector(currvars, maxcurrvars, LocVar); - } + maxcurrvars = growvector(&currvars, maxcurrvars, LocVar, + lockEM, MAX_WORD); currvars[numcurrvars].varname = varname; currvars[numcurrvars].line = line; numcurrvars++; diff --git a/lex.c b/lex.c index 8e744e2c..27049be0 100644 --- a/lex.c +++ b/lex.c @@ -1,4 +1,4 @@ -char *rcs_lex = "$Id: lex.c,v 2.30 1996/03/14 15:17:28 roberto Exp roberto $"; +char *rcs_lex = "$Id: lex.c,v 2.31 1996/03/19 16:50:24 roberto Exp roberto $"; #include @@ -83,8 +83,7 @@ void luaI_addReserved (void) static void growtext (void) { int size = yytextLast - yytext; - textsize *= 2; - yytext = growvector(yytext, textsize, char); + textsize = growvector(&yytext, textsize, char, lexEM, MAX_WORD); yytextLast = yytext + size; } diff --git a/lua.stx b/lua.stx index 47925c33..5df83550 100644 --- a/lua.stx +++ b/lua.stx @@ -1,6 +1,6 @@ %{ -char *rcs_luastx = "$Id: lua.stx,v 3.34 1996/02/26 21:00:27 roberto Exp roberto $"; +char *rcs_luastx = "$Id: lua.stx,v 3.35 1996/03/08 12:02:37 roberto Exp roberto $"; #include #include @@ -31,7 +31,7 @@ int yyparse (void); #endif static int maxcode; static int maxmain; -static Long maxcurr; /* to allow maxcurr *= 2 without overflow */ +static int maxcurr; static Byte *funcCode = NULL; static Byte **initcode; static Byte *basepc; @@ -70,14 +70,7 @@ static void yyerror (char *s) static void code_byte (Byte c) { if (pc>maxcurr-2) /* 1 byte free to code HALT of main code */ - { - if (maxcurr >= MAX_INT) - yyerror("code size overflow"); - maxcurr *= 2; - if (maxcurr >= MAX_INT) - maxcurr = MAX_INT; - basepc = growvector(basepc, maxcurr, Byte); - } + maxcurr = growvector(&basepc, maxcurr, Byte, codeEM, MAX_INT); basepc[pc++] = c; } diff --git a/luamem.c b/luamem.c index eeceaea2..164cbca2 100644 --- a/luamem.c +++ b/luamem.c @@ -3,7 +3,7 @@ ** TecCGraf - PUC-Rio */ -char *rcs_mem = "$Id: mem.c,v 1.8 1996/02/22 20:34:33 roberto Exp roberto $"; +char *rcs_mem = "$Id: mem.c,v 1.9 1996/03/14 15:55:49 roberto Exp roberto $"; #include #include @@ -13,6 +13,17 @@ char *rcs_mem = "$Id: mem.c,v 1.8 1996/02/22 20:34:33 roberto Exp roberto $"; #include "lua.h" #include "table.h" + +char *luaI_memerrormsg[NUMERRMSG] = { + "code size overflow", + "symbol table overflow", + "constant table overflow", + "stack size overflow", + "lex buffer overflow", + "lock table overflow" +}; + + static void mem_error (void) { Long recovered = luaI_collectgarbage(); /* try to collect garbage */ @@ -54,6 +65,19 @@ void *luaI_realloc (void *oldblock, unsigned long size) } +int luaI_growvector (void **block, unsigned long nelems, int size, + enum memerrormsg errormsg, unsigned long limit) +{ + if (nelems >= limit) + lua_error(luaI_memerrormsg[errormsg]); + nelems = (nelems == 0) ? 20 : nelems*2; + if (nelems > limit) + nelems = limit; + *block = luaI_realloc(*block, nelems*size); + return (int) nelems; +} + + void* luaI_buffer (unsigned long size) { static unsigned long buffsize = 0; diff --git a/luamem.h b/luamem.h index 57e8f2d9..dc0e925b 100644 --- a/luamem.h +++ b/luamem.h @@ -1,7 +1,7 @@ /* ** mem.c ** memory manager for lua -** $Id: mem.h,v 1.3 1996/02/22 20:34:33 roberto Exp roberto $ +** $Id: mem.h,v 1.4 1996/03/14 15:55:49 roberto Exp roberto $ */ #ifndef mem_h @@ -11,14 +11,24 @@ #define NULL 0 #endif + +/* memory error messages */ +#define NUMERRMSG 6 +enum memerrormsg {codeEM, symbolEM, constantEM, stackEM, lexEM, lockEM}; +extern char *luaI_memerrormsg[]; + + void luaI_free (void *block); void *luaI_malloc (unsigned long size); void *luaI_realloc (void *oldblock, unsigned long size); -void* luaI_buffer (unsigned long size); +void *luaI_buffer (unsigned long size); +int luaI_growvector (void **block, unsigned long nelems, int size, + enum memerrormsg errormsg, unsigned long limit); #define new(s) ((s *)luaI_malloc(sizeof(s))) #define newvector(n,s) ((s *)luaI_malloc((n)*sizeof(s))) -#define growvector(old,n,s) ((s *)luaI_realloc(old,(n)*sizeof(s))) +#define growvector(old,n,s,e,l) \ + (luaI_growvector((void**)old,n,sizeof(s),e,l)) #endif diff --git a/opcode.c b/opcode.c index 3b4ce09c..c7db7824 100644 --- a/opcode.c +++ b/opcode.c @@ -3,7 +3,7 @@ ** TecCGraf - PUC-Rio */ -char *rcs_opcode="$Id: opcode.c,v 3.62 1996/03/19 22:28:37 roberto Exp roberto $"; +char *rcs_opcode="$Id: opcode.c,v 3.63 1996/03/20 17:05:44 roberto Exp roberto $"; #include #include @@ -93,14 +93,17 @@ static void growstack (void) lua_initstack(); else { + static int limit = 10000; StkId t = top-stack; - Long maxstack = stackLimit - stack; - maxstack *= 2; - stack = growvector(stack, maxstack, Object); - stackLimit = stack+maxstack; + Long stacksize = stackLimit - stack; + stacksize = growvector(&stack, stacksize, Object, stackEM, limit+100); + stackLimit = stack+stacksize; top = stack + t; - if (maxstack >= MAX_WORD/2) - lua_error("stack size overflow"); + if (stacksize >= limit) + { + limit = stacksize; + lua_error(luaI_memerrormsg[stackEM]); + } } } diff --git a/table.c b/table.c index 4206f6cd..3920ed19 100644 --- a/table.c +++ b/table.c @@ -3,7 +3,7 @@ ** Module to control static tables */ -char *rcs_table="$Id: table.c,v 2.48 1996/02/26 21:00:27 roberto Exp roberto $"; +char *rcs_table="$Id: table.c,v 2.49 1996/03/14 15:57:19 roberto Exp roberto $"; #include "mem.h" #include "opcode.h" @@ -33,7 +33,7 @@ static Long lua_maxconstant = 0; static void lua_nextvar (void); /* -** Initialise symbol table with internal functions +** Internal functions */ static struct { char *name; @@ -56,6 +56,7 @@ static struct { #define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0])) + void luaI_initsymbol (void) { int i; @@ -74,8 +75,12 @@ void luaI_initsymbol (void) */ void luaI_initconstant (void) { + int i; lua_maxconstant = BUFFER_BLOCK; lua_constant = newvector(lua_maxconstant, TaggedString *); + /* pre-register mem error messages, to avoid loop when error arises */ + for (i=0; ivarindex == NOT_USED) { if (lua_ntable == lua_maxsymbol) - { - if (lua_maxsymbol >= MAX_WORD) - lua_error("symbol table overflow"); - lua_maxsymbol *= 2; - if (lua_maxsymbol >= MAX_WORD) - lua_maxsymbol = MAX_WORD; - lua_table = growvector(lua_table, lua_maxsymbol, Symbol); - } + lua_maxsymbol = growvector(&lua_table, lua_maxsymbol, Symbol, + symbolEM, MAX_WORD); t->varindex = lua_ntable; lua_table[lua_ntable].varname = t; s_tag(lua_ntable) = LUA_T_NIL; @@ -120,14 +119,8 @@ Word luaI_findconstant (TaggedString *t) if (t->constindex == NOT_USED) { if (lua_nconstant == lua_maxconstant) - { - if (lua_maxconstant >= MAX_WORD) - lua_error("constant table overflow"); - lua_maxconstant *= 2; - if (lua_maxconstant >= MAX_WORD) - lua_maxconstant = MAX_WORD; - lua_constant = growvector(lua_constant, lua_maxconstant, TaggedString *); - } + lua_maxconstant = growvector(&lua_constant, lua_maxconstant, TaggedString *, + constantEM, MAX_WORD); t->constindex = lua_nconstant; lua_constant[lua_nconstant] = t; lua_nconstant++; -- cgit v1.2.3-55-g6feb