aboutsummaryrefslogtreecommitdiff
path: root/lstate.c
diff options
context:
space:
mode:
Diffstat (limited to 'lstate.c')
-rw-r--r--lstate.c111
1 files changed, 69 insertions, 42 deletions
diff --git a/lstate.c b/lstate.c
index 67613b3d..5783f639 100644
--- a/lstate.c
+++ b/lstate.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.c,v 1.50 2000/12/28 12:55:41 roberto Exp roberto $ 2** $Id: lstate.c,v 1.51 2001/01/19 13:20:30 roberto Exp roberto $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -38,52 +38,70 @@ static int errormessage (lua_State *L) {
38} 38}
39 39
40 40
41struct Sopen {
42 int stacksize;
43 lua_State *L;
44};
45
46
41/* 47/*
42** open parts that may cause memory-allocation errors 48** open parts that may cause memory-allocation errors
43*/ 49*/
44static void f_luaopen (lua_State *L, void *ud) { 50static void f_luaopen (lua_State *L, void *ud) {
45 int stacksize = *(int *)ud; 51 struct Sopen *so = (struct Sopen *)ud;
46 if (stacksize == 0) 52 if (so->stacksize == 0)
47 stacksize = DEFAULT_STACK_SIZE; 53 so->stacksize = DEFAULT_STACK_SIZE;
48 else 54 else
49 stacksize += LUA_MINSTACK; 55 so->stacksize += LUA_MINSTACK;
50 L->G = luaM_new(L, global_State); 56 if (so->L != NULL) { /* shared global state? */
51 G(L)->strt.size = G(L)->udt.size = 0; 57 L->G = G(so->L);
52 G(L)->strt.nuse = G(L)->udt.nuse = 0; 58 L->gt = so->L->gt; /* share table of globals */
53 G(L)->strt.hash = G(L)->udt.hash = NULL; 59 so->L->next->previous = L; /* insert L into linked list */
54 G(L)->Mbuffer = NULL; 60 L->next = so->L->next;
55 G(L)->Mbuffsize = 0; 61 so->L->next = L;
56 G(L)->rootproto = NULL; 62 L->previous = so->L;
57 G(L)->rootcl = NULL; 63 luaD_init(L, so->stacksize); /* init stack */
58 G(L)->roottable = NULL; 64 }
59 G(L)->TMtable = NULL; 65 else { /* create a new global state */
60 G(L)->sizeTM = 0; 66 L->G = luaM_new(L, global_State);
61 G(L)->ntag = 0; 67 G(L)->strt.size = G(L)->udt.size = 0;
62 G(L)->refArray = NULL; 68 G(L)->strt.nuse = G(L)->udt.nuse = 0;
63 G(L)->nref = 0; 69 G(L)->strt.hash = G(L)->udt.hash = NULL;
64 G(L)->sizeref = 0; 70 G(L)->Mbuffer = NULL;
65 G(L)->refFree = NONEXT; 71 G(L)->Mbuffsize = 0;
66 G(L)->nblocks = sizeof(lua_State) + sizeof(global_State); 72 G(L)->rootproto = NULL;
67 G(L)->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */ 73 G(L)->rootcl = NULL;
68 L->gt = luaH_new(L, 10); /* table of globals */ 74 G(L)->roottable = NULL;
69 luaD_init(L, stacksize); 75 G(L)->TMtable = NULL;
70 luaS_init(L); 76 G(L)->sizeTM = 0;
71 luaX_init(L); 77 G(L)->ntag = 0;
72 luaT_init(L); 78 G(L)->refArray = NULL;
73 lua_newtable(L); 79 G(L)->nref = 0;
74 lua_ref(L, 1); /* create registry */ 80 G(L)->sizeref = 0;
75 lua_register(L, LUA_ERRORMESSAGE, errormessage); 81 G(L)->refFree = NONEXT;
82 G(L)->nblocks = sizeof(lua_State) + sizeof(global_State);
83 G(L)->GCthreshold = MAX_INT; /* to avoid GC during pre-definitions */
84 luaD_init(L, so->stacksize); /* init stack */
85 L->gt = luaH_new(L, 10); /* table of globals */
86 luaS_init(L);
87 luaX_init(L);
88 luaT_init(L);
89 lua_newtable(L);
90 lua_ref(L, 1); /* create registry */
91 lua_register(L, LUA_ERRORMESSAGE, errormessage);
76#ifdef LUA_DEBUG 92#ifdef LUA_DEBUG
77 luaB_opentests(L); 93 luaB_opentests(L);
78 if (lua_state == NULL) lua_state = L; /* keep first state to be opened */ 94 if (lua_state == NULL) lua_state = L; /* keep first state to be opened */
79 lua_assert(lua_gettop(L) == 0); 95 lua_assert(lua_gettop(L) == 0);
80#endif 96#endif
97 G(L)->GCthreshold = 2*G(L)->nblocks;
98 }
81} 99}
82 100
83 101
84LUA_API lua_State *lua_open (int stacksize) { 102LUA_API lua_State *lua_open (lua_State *OL, int stacksize) {
85 lua_State *L; 103 struct Sopen so;
86 L = luaM_new(NULL, lua_State); 104 lua_State *L = luaM_new(OL, lua_State);
87 if (L == NULL) return NULL; /* memory allocation error */ 105 if (L == NULL) return NULL; /* memory allocation error */
88 L->G = NULL; 106 L->G = NULL;
89 L->stack = NULL; 107 L->stack = NULL;
@@ -92,19 +110,28 @@ LUA_API lua_State *lua_open (int stacksize) {
92 L->callhook = NULL; 110 L->callhook = NULL;
93 L->linehook = NULL; 111 L->linehook = NULL;
94 L->allowhooks = 1; 112 L->allowhooks = 1;
95 if (luaD_runprotected(L, f_luaopen, &stacksize) != 0) { 113 L->next = L->previous = L;
114 so.stacksize = stacksize;
115 so.L = OL;
116 if (luaD_runprotected(L, f_luaopen, &so) != 0) {
96 /* memory allocation error: free partial state */ 117 /* memory allocation error: free partial state */
97 lua_close(L); 118 lua_close(L);
98 return NULL; 119 return NULL;
99 } 120 }
100 G(L)->GCthreshold = 2*G(L)->nblocks;
101 return L; 121 return L;
102} 122}
103 123
104 124
105LUA_API void lua_close (lua_State *L) { 125LUA_API void lua_close (lua_State *L) {
126 lua_State *L1 = L->next; /* any surviving thread (if there is one) */
106 lua_assert(L != lua_state || lua_gettop(L) == 0); 127 lua_assert(L != lua_state || lua_gettop(L) == 0);
107 if (G(L)) { /* close global state */ 128 if (L1 == L) L1 = NULL; /* no surviving threads */
129 if (L1 != NULL) { /* are there other threads? */
130 lua_assert(L->previous != L);
131 L->previous->next = L->next;
132 L->next->previous = L->previous;
133 }
134 else if (G(L)) { /* last thread; close global state */
108 luaC_collect(L, 1); /* collect all elements */ 135 luaC_collect(L, 1); /* collect all elements */
109 lua_assert(G(L)->rootproto == NULL); 136 lua_assert(G(L)->rootproto == NULL);
110 lua_assert(G(L)->rootcl == NULL); 137 lua_assert(G(L)->rootcl == NULL);
@@ -115,8 +142,8 @@ LUA_API void lua_close (lua_State *L) {
115 luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, char); 142 luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, char);
116 luaM_freelem(NULL, L->G, global_State); 143 luaM_freelem(NULL, L->G, global_State);
117 } 144 }
118 luaM_freearray(NULL, L->stack, L->stacksize, TObject); 145 luaM_freearray(L1, L->stack, L->stacksize, TObject);
119 luaM_freelem(NULL, L, lua_State); 146 luaM_freelem(L1, L, lua_State);
120 lua_assert(L != lua_state || memdebug_numblocks == 0); 147 lua_assert(L != lua_state || memdebug_numblocks == 0);
121 lua_assert(L != lua_state || memdebug_total == 0); 148 lua_assert(L != lua_state || memdebug_total == 0);
122} 149}