diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-12-28 10:55:41 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-12-28 10:55:41 -0200 |
commit | 0183b8030c80f57b87874ff7867ccdb172d9d3dc (patch) | |
tree | 1033b5a84489a2f1f1bd210b1b120155cd7aeed7 /ldo.c | |
parent | 8c49e198654567f770a7d5081b886a7c35201d81 (diff) | |
download | lua-0183b8030c80f57b87874ff7867ccdb172d9d3dc.tar.gz lua-0183b8030c80f57b87874ff7867ccdb172d9d3dc.tar.bz2 lua-0183b8030c80f57b87874ff7867ccdb172d9d3dc.zip |
`free' gets size of the block: complete control over memory use
Diffstat (limited to 'ldo.c')
-rw-r--r-- | ldo.c | 26 |
1 files changed, 14 insertions, 12 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldo.c,v 1.109 2000/10/30 12:38:50 roberto Exp roberto $ | 2 | ** $Id: ldo.c,v 1.110 2000/11/24 17:39:56 roberto Exp roberto $ |
3 | ** Stack and Call structure of Lua | 3 | ** Stack and Call structure of Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -28,38 +28,40 @@ | |||
28 | 28 | ||
29 | 29 | ||
30 | /* space to handle stack overflow errors */ | 30 | /* space to handle stack overflow errors */ |
31 | #define EXTRA_STACK (2*LUA_MINSTACK) | 31 | #define EXTRA_STACK (2*LUA_MINSTACK) |
32 | |||
33 | |||
34 | static void restore_stack_limit (lua_State *L) { | ||
35 | StkId limit = L->stack+(L->stacksize-EXTRA_STACK)-1; | ||
36 | if (L->top < limit) | ||
37 | L->stack_last = limit; | ||
38 | } | ||
32 | 39 | ||
33 | 40 | ||
34 | void luaD_init (lua_State *L, int stacksize) { | 41 | void luaD_init (lua_State *L, int stacksize) { |
35 | L->stack = luaM_newvector(L, stacksize+EXTRA_STACK, TObject); | 42 | stacksize += EXTRA_STACK; |
36 | L->nblocks += stacksize*sizeof(TObject); | 43 | L->stack = luaM_newvector(L, stacksize, TObject); |
37 | L->stack_last = L->stack+(stacksize-1); | ||
38 | L->stacksize = stacksize; | 44 | L->stacksize = stacksize; |
39 | L->Cbase = L->top = L->stack; | 45 | L->Cbase = L->top = L->stack; |
46 | restore_stack_limit(L); | ||
40 | } | 47 | } |
41 | 48 | ||
42 | 49 | ||
43 | void luaD_checkstack (lua_State *L, int n) { | 50 | void luaD_checkstack (lua_State *L, int n) { |
44 | if (L->stack_last - L->top <= n) { /* stack overflow? */ | 51 | if (L->stack_last - L->top <= n) { /* stack overflow? */ |
45 | if (L->stack_last-L->stack > (L->stacksize-1)) { | 52 | if (L->stack_last == L->stack+L->stacksize-1) { |
46 | /* overflow while handling overflow */ | 53 | /* overflow while handling overflow */ |
47 | luaD_breakrun(L, LUA_ERRERR); /* break run without error message */ | 54 | luaD_breakrun(L, LUA_ERRERR); /* break run without error message */ |
48 | } | 55 | } |
49 | else { | 56 | else { |
50 | L->stack_last += EXTRA_STACK; /* to be used by error message */ | 57 | L->stack_last += EXTRA_STACK; /* to be used by error message */ |
58 | LUA_ASSERT(L->stack_last == L->stack+L->stacksize-1, "wrong stack limit"); | ||
51 | lua_error(L, "stack overflow"); | 59 | lua_error(L, "stack overflow"); |
52 | } | 60 | } |
53 | } | 61 | } |
54 | } | 62 | } |
55 | 63 | ||
56 | 64 | ||
57 | static void restore_stack_limit (lua_State *L) { | ||
58 | if (L->top - L->stack < L->stacksize - 1) | ||
59 | L->stack_last = L->stack + (L->stacksize-1); | ||
60 | } | ||
61 | |||
62 | |||
63 | /* | 65 | /* |
64 | ** Adjust stack. Set top to base+extra, pushing NILs if needed. | 66 | ** Adjust stack. Set top to base+extra, pushing NILs if needed. |
65 | ** (we cannot add base+extra unless we are sure it fits in the stack; | 67 | ** (we cannot add base+extra unless we are sure it fits in the stack; |