no more maximum stack size

This commit is contained in:
Roberto Ierusalimschy 2002-02-14 19:47:29 -02:00
parent ac178ee478
commit b0a5e156b8
6 changed files with 37 additions and 47 deletions

View File

@ -434,7 +434,7 @@ static int luaB_coroutine (lua_State *L) {
int n = lua_gettop(L); int n = lua_gettop(L);
luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1, luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
"Lua function expected"); "Lua function expected");
NL = lua_newthread(L, 0); NL = lua_newthread(L);
if (NL == NULL) lua_error(L, "unable to create new thread"); if (NL == NULL) lua_error(L, "unable to create new thread");
/* move function and arguments from L to NL */ /* move function and arguments from L to NL */
for (i=0; i<n; i++) { for (i=0; i<n; i++) {

26
ldo.c
View File

@ -71,26 +71,26 @@ void luaD_reallocstack (lua_State *L, int newsize) {
static void restore_stack_limit (lua_State *L) { static void restore_stack_limit (lua_State *L) {
if (L->stacksize > L->maxstacksize) { /* there was an overflow? */ if (L->stacksize > LUA_MAXSTACK) { /* there was an overflow? */
int inuse = (L->top - L->stack); int inuse = (L->top - L->stack);
if (inuse + MAXSTACK < L->maxstacksize) /* can `undo' overflow? */ if (inuse + MAXSTACK < LUA_MAXSTACK) /* can `undo' overflow? */
luaD_reallocstack(L, L->maxstacksize); luaD_reallocstack(L, LUA_MAXSTACK);
} }
} }
void luaD_growstack (lua_State *L, int n) { void luaD_growstack (lua_State *L, int n) {
if (L->stacksize > L->maxstacksize) { /* overflow while handling overflow? */ if (L->stacksize > LUA_MAXSTACK) { /* overflow while handling overflow? */
luaD_breakrun(L, LUA_ERRERR); /* break run without error message */ luaD_breakrun(L, LUA_ERRERR); /* break run without error message */
} }
else { else {
if (n <= L->stacksize && 2*L->stacksize < L->maxstacksize) /* can double? */ if (n <= L->stacksize && 2*L->stacksize < LUA_MAXSTACK) /* can double? */
luaD_reallocstack(L, 2*L->stacksize); luaD_reallocstack(L, 2*L->stacksize);
else if (L->stacksize+n <= L->maxstacksize) /* no overflow? */ else if (L->stacksize+n <= LUA_MAXSTACK) /* no overflow? */
luaD_reallocstack(L, L->maxstacksize); luaD_reallocstack(L, LUA_MAXSTACK);
else { else {
/* resize to maximum + some extra space to handle error */ /* resize to maximum + some extra space to handle error */
luaD_reallocstack(L, L->maxstacksize+4*LUA_MINSTACK); luaD_reallocstack(L, LUA_MAXSTACK+4*LUA_MINSTACK);
luaD_error(L, "stack overflow"); luaD_error(L, "stack overflow");
} }
} }
@ -428,9 +428,13 @@ LUA_API int lua_loadfile (lua_State *L, const char *filename) {
f = fopen(filename, "rb"); /* reopen in binary mode */ f = fopen(filename, "rb"); /* reopen in binary mode */
if (f == NULL) return LUA_ERRFILE; /* unable to reopen file */ if (f == NULL) return LUA_ERRFILE; /* unable to reopen file */
} }
lua_pushliteral(L, "@"); if (filename == NULL)
lua_pushstring(L, (filename == NULL) ? "=stdin" : filename); lua_pushstring(L, "=stdin");
lua_concat(L, 2); else {
lua_pushliteral(L, "@");
lua_pushstring(L, filename);
lua_concat(L, 2);
}
nlevel = lua_gettop(L); nlevel = lua_gettop(L);
filename = lua_tostring(L, -1); /* filename = `@'..filename */ filename = lua_tostring(L, -1); /* filename = `@'..filename */
luaZ_Fopen(&z, f, filename); luaZ_Fopen(&z, f, filename);

View File

@ -21,22 +21,12 @@
struct Sopen {
lua_State *L;
int stacksize;
};
static void close_state (lua_State *L); static void close_state (lua_State *L);
static void stack_init (lua_State *L, lua_State *OL, int maxstacksize) { static void stack_init (lua_State *L, lua_State *OL) {
if (maxstacksize == 0)
maxstacksize = DEFAULT_MAXSTACK;
else
maxstacksize += 2*LUA_MINSTACK;
L->stack = luaM_newvector(OL, BASIC_STACK_SIZE, TObject); L->stack = luaM_newvector(OL, BASIC_STACK_SIZE, TObject);
L->maxstacksize = maxstacksize;
L->stacksize = BASIC_STACK_SIZE; L->stacksize = BASIC_STACK_SIZE;
L->top = L->stack + RESERVED_STACK_PREFIX; L->top = L->stack + RESERVED_STACK_PREFIX;
L->stack_last = L->stack+(BASIC_STACK_SIZE-EXTRA_STACK)-1; L->stack_last = L->stack+(BASIC_STACK_SIZE-EXTRA_STACK)-1;
@ -53,7 +43,7 @@ static void stack_init (lua_State *L, lua_State *OL, int maxstacksize) {
** open parts that may cause memory-allocation errors ** open parts that may cause memory-allocation errors
*/ */
static void f_luaopen (lua_State *L, void *ud) { static void f_luaopen (lua_State *L, void *ud) {
struct Sopen *so = cast(struct Sopen *, ud); UNUSED(ud);
/* create a new global state */ /* create a new global state */
L->_G = luaM_new(L, global_State); L->_G = luaM_new(L, global_State);
G(L)->strt.size = 0; G(L)->strt.size = 0;
@ -68,7 +58,7 @@ static void f_luaopen (lua_State *L, void *ud) {
G(L)->rootudata = NULL; G(L)->rootudata = NULL;
G(L)->tmudata = NULL; G(L)->tmudata = NULL;
G(L)->nblocks = sizeof(lua_State) + sizeof(global_State); G(L)->nblocks = sizeof(lua_State) + sizeof(global_State);
stack_init(L, L, so->stacksize); /* init stack */ stack_init(L, L); /* init stack */
/* create default meta table with a dummy table, and then close the loop */ /* create default meta table with a dummy table, and then close the loop */
sethvalue(defaultmeta(L), NULL); sethvalue(defaultmeta(L), NULL);
sethvalue(defaultmeta(L), luaH_new(L, 0, 4)); sethvalue(defaultmeta(L), luaH_new(L, 0, 4));
@ -85,7 +75,6 @@ static void f_luaopen (lua_State *L, void *ud) {
static void preinit_state (lua_State *L) { static void preinit_state (lua_State *L) {
L->stack = NULL; L->stack = NULL;
L->stacksize = 0; L->stacksize = 0;
L->maxstacksize = 1;
L->errorJmp = NULL; L->errorJmp = NULL;
L->callhook = NULL; L->callhook = NULL;
L->linehook = NULL; L->linehook = NULL;
@ -96,7 +85,7 @@ static void preinit_state (lua_State *L) {
} }
LUA_API lua_State *lua_newthread (lua_State *OL, int stacksize) { LUA_API lua_State *lua_newthread (lua_State *OL) {
lua_State *L; lua_State *L;
lua_lock(OL); lua_lock(OL);
L = luaM_new(OL, lua_State); L = luaM_new(OL, lua_State);
@ -106,7 +95,7 @@ LUA_API lua_State *lua_newthread (lua_State *OL, int stacksize) {
L->next = OL->next; L->next = OL->next;
OL->next = L; OL->next = L;
L->previous = OL; L->previous = OL;
stack_init(L, OL, stacksize); /* init stack */ stack_init(L, OL); /* init stack */
setobj(defaultmeta(L), defaultmeta(OL)); /* share default meta table */ setobj(defaultmeta(L), defaultmeta(OL)); /* share default meta table */
setobj(gt(L), gt(OL)); /* share table of globals */ setobj(gt(L), gt(OL)); /* share table of globals */
setobj(registry(L), registry(OL)); /* share registry */ setobj(registry(L), registry(OL)); /* share registry */
@ -116,17 +105,14 @@ LUA_API lua_State *lua_newthread (lua_State *OL, int stacksize) {
} }
LUA_API lua_State *lua_open (int stacksize) { LUA_API lua_State *lua_open (void) {
struct Sopen so;
lua_State *L; lua_State *L;
L = luaM_new(NULL, lua_State); L = luaM_new(NULL, lua_State);
if (L) { /* allocation OK? */ if (L) { /* allocation OK? */
preinit_state(L); preinit_state(L);
L->_G = NULL; L->_G = NULL;
L->next = L->previous = L; L->next = L->previous = L;
so.stacksize = stacksize; if (luaD_runprotected(L, f_luaopen, NULL) != 0) {
so.L = NULL;
if (luaD_runprotected(L, f_luaopen, &so) != 0) {
/* memory allocation error: free partial state */ /* memory allocation error: free partial state */
close_state(L); close_state(L);
L = NULL; L = NULL;

View File

@ -73,8 +73,9 @@ struct lua_longjmp; /* defined in ldo.c */
#define BASIC_STACK_SIZE (2*LUA_MINSTACK) #define BASIC_STACK_SIZE (2*LUA_MINSTACK)
#define DEFAULT_MAXSTACK 12000 #ifndef LUA_MAXSTACK
#define LUA_MAXSTACK 14000
#endif
@ -141,7 +142,6 @@ struct lua_State {
lua_State *next; /* circular double linked list of states */ lua_State *next; /* circular double linked list of states */
lua_State *previous; lua_State *previous;
int stacksize; int stacksize;
int maxstacksize;
int size_ci; /* size of array `base_ci' */ int size_ci; /* size of array `base_ci' */
int allowhooks; int allowhooks;
lua_Hook callhook; lua_Hook callhook;

View File

@ -378,9 +378,9 @@ static int udataval (lua_State *L) {
static int doonnewstack (lua_State *L) { static int doonnewstack (lua_State *L) {
lua_State *L1 = lua_newthread(L, luaL_check_int(L, 1)); lua_State *L1 = lua_newthread(L);
lua_dostring(L1, luaL_check_string(L, 2)); int status = lua_dostring(L1, luaL_check_string(L, 1));
lua_pushnumber(L, 1); lua_pushnumber(L, status);
lua_closethread(L, L1); lua_closethread(L, L1);
return 1; return 1;
} }
@ -399,7 +399,7 @@ static int d2s (lua_State *L) {
static int newstate (lua_State *L) { static int newstate (lua_State *L) {
lua_State *L1 = lua_open(luaL_check_int(L, 1)); lua_State *L1 = lua_open();
if (L1) { if (L1) {
*cast(int **, L1) = &islocked; /* initialize the lock */ *cast(int **, L1) = &islocked; /* initialize the lock */
lua_pushnumber(L, (unsigned long)L1); lua_pushnumber(L, (unsigned long)L1);

14
lua.h
View File

@ -57,12 +57,12 @@ typedef int (*lua_CFunction) (lua_State *L);
*/ */
#define LUA_TNONE (-1) #define LUA_TNONE (-1)
#define LUA_TUSERDATA 0 #define LUA_TNIL 0
#define LUA_TNIL 1 #define LUA_TNUMBER 1
#define LUA_TNUMBER 2 #define LUA_TSTRING 2
#define LUA_TBOOLEAN 3 #define LUA_TBOOLEAN 3
#define LUA_TSTRING 4 #define LUA_TTABLE 4
#define LUA_TTABLE 5 #define LUA_TUSERDATA 5
#define LUA_TFUNCTION 6 #define LUA_TFUNCTION 6
@ -94,9 +94,9 @@ typedef LUA_NUMBER lua_Number;
/* /*
** state manipulation ** state manipulation
*/ */
LUA_API lua_State *lua_open (int stacksize); LUA_API lua_State *lua_open (void);
LUA_API void lua_close (lua_State *L); LUA_API void lua_close (lua_State *L);
LUA_API lua_State *lua_newthread (lua_State *L, int stacksize); LUA_API lua_State *lua_newthread (lua_State *L);
LUA_API void lua_closethread (lua_State *L, lua_State *thread); LUA_API void lua_closethread (lua_State *L, lua_State *thread);