diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-03-21 13:33:47 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-03-21 13:33:47 -0300 |
commit | 9284742a11b92dfe4ef011b963240cfa588515cd (patch) | |
tree | 96cc498fcc5ec27546fc0738998319c829df55d0 | |
parent | 9704ff4cb14f34077062447d15196d32ace23e95 (diff) | |
download | lua-9284742a11b92dfe4ef011b963240cfa588515cd.tar.gz lua-9284742a11b92dfe4ef011b963240cfa588515cd.tar.bz2 lua-9284742a11b92dfe4ef011b963240cfa588515cd.zip |
better control when growing arrays.
-rw-r--r-- | fallback.c | 5 | ||||
-rw-r--r-- | func.c | 7 | ||||
-rw-r--r-- | lex.c | 5 | ||||
-rw-r--r-- | lua.stx | 13 | ||||
-rw-r--r-- | luamem.c | 26 | ||||
-rw-r--r-- | luamem.h | 16 | ||||
-rw-r--r-- | opcode.c | 17 | ||||
-rw-r--r-- | table.c | 29 |
8 files changed, 68 insertions, 50 deletions
@@ -3,7 +3,7 @@ | |||
3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_fallback="$Id: fallback.c,v 1.21 1996/03/04 13:29:10 roberto Exp roberto $"; | 6 | char *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; |
@@ -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 | ||
11 | static TFunc *function_root = NULL; | 10 | static TFunc *function_root = NULL; |
12 | static LocVar *currvars = NULL; | 11 | static LocVar *currvars = NULL; |
@@ -103,10 +102,8 @@ void lua_funcinfo (lua_Object func, char **filename, int *linedefined) | |||
103 | void luaI_registerlocalvar (TaggedString *varname, int line) | 102 | void 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++; |
@@ -1,4 +1,4 @@ | |||
1 | char *rcs_lex = "$Id: lex.c,v 2.30 1996/03/14 15:17:28 roberto Exp roberto $"; | 1 | char *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) | |||
83 | static void growtext (void) | 83 | static 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 | ||
@@ -1,6 +1,6 @@ | |||
1 | %{ | 1 | %{ |
2 | 2 | ||
3 | char *rcs_luastx = "$Id: lua.stx,v 3.34 1996/02/26 21:00:27 roberto Exp roberto $"; | 3 | char *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 |
32 | static int maxcode; | 32 | static int maxcode; |
33 | static int maxmain; | 33 | static int maxmain; |
34 | static Long maxcurr; /* to allow maxcurr *= 2 without overflow */ | 34 | static int maxcurr; |
35 | static Byte *funcCode = NULL; | 35 | static Byte *funcCode = NULL; |
36 | static Byte **initcode; | 36 | static Byte **initcode; |
37 | static Byte *basepc; | 37 | static Byte *basepc; |
@@ -70,14 +70,7 @@ static void yyerror (char *s) | |||
70 | static void code_byte (Byte c) | 70 | static 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 | ||
@@ -3,7 +3,7 @@ | |||
3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_mem = "$Id: mem.c,v 1.8 1996/02/22 20:34:33 roberto Exp roberto $"; | 6 | char *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 | |||
17 | char *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 | |||
16 | static void mem_error (void) | 27 | static 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 | ||
68 | int 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 | |||
57 | void* luaI_buffer (unsigned long size) | 81 | void* luaI_buffer (unsigned long size) |
58 | { | 82 | { |
59 | static unsigned long buffsize = 0; | 83 | static unsigned long buffsize = 0; |
@@ -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 | ||
17 | enum memerrormsg {codeEM, symbolEM, constantEM, stackEM, lexEM, lockEM}; | ||
18 | extern char *luaI_memerrormsg[]; | ||
19 | |||
20 | |||
14 | void luaI_free (void *block); | 21 | void luaI_free (void *block); |
15 | void *luaI_malloc (unsigned long size); | 22 | void *luaI_malloc (unsigned long size); |
16 | void *luaI_realloc (void *oldblock, unsigned long size); | 23 | void *luaI_realloc (void *oldblock, unsigned long size); |
17 | void* luaI_buffer (unsigned long size); | 24 | void *luaI_buffer (unsigned long size); |
25 | int 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 | ||
@@ -3,7 +3,7 @@ | |||
3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_opcode="$Id: opcode.c,v 3.62 1996/03/19 22:28:37 roberto Exp roberto $"; | 6 | char *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 | ||
@@ -3,7 +3,7 @@ | |||
3 | ** Module to control static tables | 3 | ** Module to control static tables |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_table="$Id: table.c,v 2.48 1996/02/26 21:00:27 roberto Exp roberto $"; | 6 | char *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; | |||
33 | static void lua_nextvar (void); | 33 | static void lua_nextvar (void); |
34 | 34 | ||
35 | /* | 35 | /* |
36 | ** Initialise symbol table with internal functions | 36 | ** Internal functions |
37 | */ | 37 | */ |
38 | static struct { | 38 | static 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 | |||
59 | void luaI_initsymbol (void) | 60 | void luaI_initsymbol (void) |
60 | { | 61 | { |
61 | int i; | 62 | int i; |
@@ -74,8 +75,12 @@ void luaI_initsymbol (void) | |||
74 | */ | 75 | */ |
75 | void luaI_initconstant (void) | 76 | void 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++; |