aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-03-21 13:33:47 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-03-21 13:33:47 -0300
commit9284742a11b92dfe4ef011b963240cfa588515cd (patch)
tree96cc498fcc5ec27546fc0738998319c829df55d0
parent9704ff4cb14f34077062447d15196d32ace23e95 (diff)
downloadlua-9284742a11b92dfe4ef011b963240cfa588515cd.tar.gz
lua-9284742a11b92dfe4ef011b963240cfa588515cd.tar.bz2
lua-9284742a11b92dfe4ef011b963240cfa588515cd.zip
better control when growing arrays.
-rw-r--r--fallback.c5
-rw-r--r--func.c7
-rw-r--r--lex.c5
-rw-r--r--lua.stx13
-rw-r--r--luamem.c26
-rw-r--r--luamem.h16
-rw-r--r--opcode.c17
-rw-r--r--table.c29
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 @@
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4*/ 4*/
5 5
6char *rcs_fallback="$Id: fallback.c,v 1.21 1996/03/04 13:29:10 roberto Exp roberto $"; 6char *rcs_fallback="$Id: fallback.c,v 1.22 1996/03/19 22:28:37 roberto Exp roberto $";
7 7
8#include <stdio.h> 8#include <stdio.h>
9#include <string.h> 9#include <string.h>
@@ -132,8 +132,7 @@ int luaI_lock (Object *object)
132 } 132 }
133 /* no more empty spaces */ 133 /* no more empty spaces */
134 oldSize = lockSize; 134 oldSize = lockSize;
135 lockSize = (lockSize == 0) ? 10 : 3*lockSize/2 + 5; 135 lockSize = growvector(&lockArray, lockSize, Object, lockEM, MAX_WORD);
136 lockArray = growvector(lockArray, lockSize, Object);
137 for (i=oldSize; i<lockSize; i++) 136 for (i=oldSize; i<lockSize; i++)
138 tag(&lockArray[i]) = LUA_T_NIL; 137 tag(&lockArray[i]) = LUA_T_NIL;
139 lockArray[oldSize] = *object; 138 lockArray[oldSize] = *object;
diff --git a/func.c b/func.c
index e4fc9f53..d9ed9831 100644
--- a/func.c
+++ b/func.c
@@ -6,7 +6,6 @@
6#include "func.h" 6#include "func.h"
7#include "opcode.h" 7#include "opcode.h"
8 8
9#define LOCALVARINITSIZE 10
10 9
11static TFunc *function_root = NULL; 10static TFunc *function_root = NULL;
12static LocVar *currvars = NULL; 11static LocVar *currvars = NULL;
@@ -103,10 +102,8 @@ void lua_funcinfo (lua_Object func, char **filename, int *linedefined)
103void luaI_registerlocalvar (TaggedString *varname, int line) 102void luaI_registerlocalvar (TaggedString *varname, int line)
104{ 103{
105 if (numcurrvars >= maxcurrvars) 104 if (numcurrvars >= maxcurrvars)
106 { 105 maxcurrvars = growvector(&currvars, maxcurrvars, LocVar,
107 maxcurrvars = (maxcurrvars == 0) ? LOCALVARINITSIZE : maxcurrvars*2; 106 lockEM, MAX_WORD);
108 currvars = growvector(currvars, maxcurrvars, LocVar);
109 }
110 currvars[numcurrvars].varname = varname; 107 currvars[numcurrvars].varname = varname;
111 currvars[numcurrvars].line = line; 108 currvars[numcurrvars].line = line;
112 numcurrvars++; 109 numcurrvars++;
diff --git a/lex.c b/lex.c
index 8e744e2c..27049be0 100644
--- a/lex.c
+++ b/lex.c
@@ -1,4 +1,4 @@
1char *rcs_lex = "$Id: lex.c,v 2.30 1996/03/14 15:17:28 roberto Exp roberto $"; 1char *rcs_lex = "$Id: lex.c,v 2.31 1996/03/19 16:50:24 roberto Exp roberto $";
2 2
3 3
4#include <ctype.h> 4#include <ctype.h>
@@ -83,8 +83,7 @@ void luaI_addReserved (void)
83static void growtext (void) 83static void growtext (void)
84{ 84{
85 int size = yytextLast - yytext; 85 int size = yytextLast - yytext;
86 textsize *= 2; 86 textsize = growvector(&yytext, textsize, char, lexEM, MAX_WORD);
87 yytext = growvector(yytext, textsize, char);
88 yytextLast = yytext + size; 87 yytextLast = yytext + size;
89} 88}
90 89
diff --git a/lua.stx b/lua.stx
index 47925c33..5df83550 100644
--- a/lua.stx
+++ b/lua.stx
@@ -1,6 +1,6 @@
1%{ 1%{
2 2
3char *rcs_luastx = "$Id: lua.stx,v 3.34 1996/02/26 21:00:27 roberto Exp roberto $"; 3char *rcs_luastx = "$Id: lua.stx,v 3.35 1996/03/08 12:02:37 roberto Exp roberto $";
4 4
5#include <stdio.h> 5#include <stdio.h>
6#include <stdlib.h> 6#include <stdlib.h>
@@ -31,7 +31,7 @@ int yyparse (void);
31#endif 31#endif
32static int maxcode; 32static int maxcode;
33static int maxmain; 33static int maxmain;
34static Long maxcurr; /* to allow maxcurr *= 2 without overflow */ 34static int maxcurr;
35static Byte *funcCode = NULL; 35static Byte *funcCode = NULL;
36static Byte **initcode; 36static Byte **initcode;
37static Byte *basepc; 37static Byte *basepc;
@@ -70,14 +70,7 @@ static void yyerror (char *s)
70static void code_byte (Byte c) 70static void code_byte (Byte c)
71{ 71{
72 if (pc>maxcurr-2) /* 1 byte free to code HALT of main code */ 72 if (pc>maxcurr-2) /* 1 byte free to code HALT of main code */
73 { 73 maxcurr = growvector(&basepc, maxcurr, Byte, codeEM, MAX_INT);
74 if (maxcurr >= MAX_INT)
75 yyerror("code size overflow");
76 maxcurr *= 2;
77 if (maxcurr >= MAX_INT)
78 maxcurr = MAX_INT;
79 basepc = growvector(basepc, maxcurr, Byte);
80 }
81 basepc[pc++] = c; 74 basepc[pc++] = c;
82} 75}
83 76
diff --git a/luamem.c b/luamem.c
index eeceaea2..164cbca2 100644
--- a/luamem.c
+++ b/luamem.c
@@ -3,7 +3,7 @@
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4*/ 4*/
5 5
6char *rcs_mem = "$Id: mem.c,v 1.8 1996/02/22 20:34:33 roberto Exp roberto $"; 6char *rcs_mem = "$Id: mem.c,v 1.9 1996/03/14 15:55:49 roberto Exp roberto $";
7 7
8#include <stdlib.h> 8#include <stdlib.h>
9#include <string.h> 9#include <string.h>
@@ -13,6 +13,17 @@ char *rcs_mem = "$Id: mem.c,v 1.8 1996/02/22 20:34:33 roberto Exp roberto $";
13#include "lua.h" 13#include "lua.h"
14#include "table.h" 14#include "table.h"
15 15
16
17char *luaI_memerrormsg[NUMERRMSG] = {
18 "code size overflow",
19 "symbol table overflow",
20 "constant table overflow",
21 "stack size overflow",
22 "lex buffer overflow",
23 "lock table overflow"
24};
25
26
16static void mem_error (void) 27static void mem_error (void)
17{ 28{
18 Long recovered = luaI_collectgarbage(); /* try to collect garbage */ 29 Long recovered = luaI_collectgarbage(); /* try to collect garbage */
@@ -54,6 +65,19 @@ void *luaI_realloc (void *oldblock, unsigned long size)
54} 65}
55 66
56 67
68int luaI_growvector (void **block, unsigned long nelems, int size,
69 enum memerrormsg errormsg, unsigned long limit)
70{
71 if (nelems >= limit)
72 lua_error(luaI_memerrormsg[errormsg]);
73 nelems = (nelems == 0) ? 20 : nelems*2;
74 if (nelems > limit)
75 nelems = limit;
76 *block = luaI_realloc(*block, nelems*size);
77 return (int) nelems;
78}
79
80
57void* luaI_buffer (unsigned long size) 81void* luaI_buffer (unsigned long size)
58{ 82{
59 static unsigned long buffsize = 0; 83 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 @@
1/* 1/*
2** mem.c 2** mem.c
3** memory manager for lua 3** memory manager for lua
4** $Id: mem.h,v 1.3 1996/02/22 20:34:33 roberto Exp roberto $ 4** $Id: mem.h,v 1.4 1996/03/14 15:55:49 roberto Exp roberto $
5*/ 5*/
6 6
7#ifndef mem_h 7#ifndef mem_h
@@ -11,14 +11,24 @@
11#define NULL 0 11#define NULL 0
12#endif 12#endif
13 13
14
15/* memory error messages */
16#define NUMERRMSG 6
17enum memerrormsg {codeEM, symbolEM, constantEM, stackEM, lexEM, lockEM};
18extern char *luaI_memerrormsg[];
19
20
14void luaI_free (void *block); 21void luaI_free (void *block);
15void *luaI_malloc (unsigned long size); 22void *luaI_malloc (unsigned long size);
16void *luaI_realloc (void *oldblock, unsigned long size); 23void *luaI_realloc (void *oldblock, unsigned long size);
17void* luaI_buffer (unsigned long size); 24void *luaI_buffer (unsigned long size);
25int luaI_growvector (void **block, unsigned long nelems, int size,
26 enum memerrormsg errormsg, unsigned long limit);
18 27
19#define new(s) ((s *)luaI_malloc(sizeof(s))) 28#define new(s) ((s *)luaI_malloc(sizeof(s)))
20#define newvector(n,s) ((s *)luaI_malloc((n)*sizeof(s))) 29#define newvector(n,s) ((s *)luaI_malloc((n)*sizeof(s)))
21#define growvector(old,n,s) ((s *)luaI_realloc(old,(n)*sizeof(s))) 30#define growvector(old,n,s,e,l) \
31 (luaI_growvector((void**)old,n,sizeof(s),e,l))
22 32
23#endif 33#endif
24 34
diff --git a/opcode.c b/opcode.c
index 3b4ce09c..c7db7824 100644
--- a/opcode.c
+++ b/opcode.c
@@ -3,7 +3,7 @@
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4*/ 4*/
5 5
6char *rcs_opcode="$Id: opcode.c,v 3.62 1996/03/19 22:28:37 roberto Exp roberto $"; 6char *rcs_opcode="$Id: opcode.c,v 3.63 1996/03/20 17:05:44 roberto Exp roberto $";
7 7
8#include <setjmp.h> 8#include <setjmp.h>
9#include <stdio.h> 9#include <stdio.h>
@@ -93,14 +93,17 @@ static void growstack (void)
93 lua_initstack(); 93 lua_initstack();
94 else 94 else
95 { 95 {
96 static int limit = 10000;
96 StkId t = top-stack; 97 StkId t = top-stack;
97 Long maxstack = stackLimit - stack; 98 Long stacksize = stackLimit - stack;
98 maxstack *= 2; 99 stacksize = growvector(&stack, stacksize, Object, stackEM, limit+100);
99 stack = growvector(stack, maxstack, Object); 100 stackLimit = stack+stacksize;
100 stackLimit = stack+maxstack;
101 top = stack + t; 101 top = stack + t;
102 if (maxstack >= MAX_WORD/2) 102 if (stacksize >= limit)
103 lua_error("stack size overflow"); 103 {
104 limit = stacksize;
105 lua_error(luaI_memerrormsg[stackEM]);
106 }
104 } 107 }
105} 108}
106 109
diff --git a/table.c b/table.c
index 4206f6cd..3920ed19 100644
--- a/table.c
+++ b/table.c
@@ -3,7 +3,7 @@
3** Module to control static tables 3** Module to control static tables
4*/ 4*/
5 5
6char *rcs_table="$Id: table.c,v 2.48 1996/02/26 21:00:27 roberto Exp roberto $"; 6char *rcs_table="$Id: table.c,v 2.49 1996/03/14 15:57:19 roberto Exp roberto $";
7 7
8#include "mem.h" 8#include "mem.h"
9#include "opcode.h" 9#include "opcode.h"
@@ -33,7 +33,7 @@ static Long lua_maxconstant = 0;
33static void lua_nextvar (void); 33static void lua_nextvar (void);
34 34
35/* 35/*
36** Initialise symbol table with internal functions 36** Internal functions
37*/ 37*/
38static struct { 38static struct {
39 char *name; 39 char *name;
@@ -56,6 +56,7 @@ static struct {
56 56
57#define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0])) 57#define INTFUNCSIZE (sizeof(int_funcs)/sizeof(int_funcs[0]))
58 58
59
59void luaI_initsymbol (void) 60void luaI_initsymbol (void)
60{ 61{
61 int i; 62 int i;
@@ -74,8 +75,12 @@ void luaI_initsymbol (void)
74*/ 75*/
75void luaI_initconstant (void) 76void luaI_initconstant (void)
76{ 77{
78 int i;
77 lua_maxconstant = BUFFER_BLOCK; 79 lua_maxconstant = BUFFER_BLOCK;
78 lua_constant = newvector(lua_maxconstant, TaggedString *); 80 lua_constant = newvector(lua_maxconstant, TaggedString *);
81 /* pre-register mem error messages, to avoid loop when error arises */
82 for (i=0; i<NUMERRMSG; i++)
83 luaI_findconstantbyname(luaI_memerrormsg[i]);
79} 84}
80 85
81 86
@@ -88,14 +93,8 @@ Word luaI_findsymbol (TaggedString *t)
88 if (t->varindex == NOT_USED) 93 if (t->varindex == NOT_USED)
89 { 94 {
90 if (lua_ntable == lua_maxsymbol) 95 if (lua_ntable == lua_maxsymbol)
91 { 96 lua_maxsymbol = growvector(&lua_table, lua_maxsymbol, Symbol,
92 if (lua_maxsymbol >= MAX_WORD) 97 symbolEM, MAX_WORD);
93 lua_error("symbol table overflow");
94 lua_maxsymbol *= 2;
95 if (lua_maxsymbol >= MAX_WORD)
96 lua_maxsymbol = MAX_WORD;
97 lua_table = growvector(lua_table, lua_maxsymbol, Symbol);
98 }
99 t->varindex = lua_ntable; 98 t->varindex = lua_ntable;
100 lua_table[lua_ntable].varname = t; 99 lua_table[lua_ntable].varname = t;
101 s_tag(lua_ntable) = LUA_T_NIL; 100 s_tag(lua_ntable) = LUA_T_NIL;
@@ -120,14 +119,8 @@ Word luaI_findconstant (TaggedString *t)
120 if (t->constindex == NOT_USED) 119 if (t->constindex == NOT_USED)
121 { 120 {
122 if (lua_nconstant == lua_maxconstant) 121 if (lua_nconstant == lua_maxconstant)
123 { 122 lua_maxconstant = growvector(&lua_constant, lua_maxconstant, TaggedString *,
124 if (lua_maxconstant >= MAX_WORD) 123 constantEM, MAX_WORD);
125 lua_error("constant table overflow");
126 lua_maxconstant *= 2;
127 if (lua_maxconstant >= MAX_WORD)
128 lua_maxconstant = MAX_WORD;
129 lua_constant = growvector(lua_constant, lua_maxconstant, TaggedString *);
130 }
131 t->constindex = lua_nconstant; 124 t->constindex = lua_nconstant;
132 lua_constant[lua_nconstant] = t; 125 lua_constant[lua_nconstant] = t;
133 lua_nconstant++; 126 lua_nconstant++;