aboutsummaryrefslogtreecommitdiff
path: root/lstate.c
diff options
context:
space:
mode:
Diffstat (limited to 'lstate.c')
-rw-r--r--lstate.c51
1 files changed, 22 insertions, 29 deletions
diff --git a/lstate.c b/lstate.c
index dd80a7e1..12acb3eb 100644
--- a/lstate.c
+++ b/lstate.c
@@ -1,11 +1,11 @@
1/* 1/*
2** $Id: lstate.c,v 1.125 2003/07/16 20:51:47 roberto Exp roberto $ 2** $Id: lstate.c,v 1.126 2003/09/04 20:19:07 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*/
6 6
7 7
8#include <stdlib.h> 8#include <stddef.h>
9 9
10#define lstate_c 10#define lstate_c
11 11
@@ -34,6 +34,11 @@ union UEXTRASPACE {L_Umaxalign a; LUA_USERSTATE b;};
34#endif 34#endif
35 35
36 36
37#define state_size(x) (sizeof(x) + EXTRASPACE)
38#define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + EXTRASPACE))
39#define fromstate(l) (cast(lu_byte *, (l)) - EXTRASPACE)
40
41
37/* 42/*
38** Main thread combines a thread state and the global state 43** Main thread combines a thread state and the global state
39*/ 44*/
@@ -44,23 +49,6 @@ typedef struct LG {
44 49
45 50
46 51
47
48static lua_State *mallocstate (lua_State *L, size_t size) {
49 lu_byte *block = (lu_byte *)luaM_malloc(L, size + EXTRASPACE);
50 if (block == NULL) return NULL;
51 else {
52 block += EXTRASPACE;
53 return cast(lua_State *, block);
54 }
55}
56
57
58static void freestate (lua_State *L, lua_State *L1, size_t size) {
59 luaM_free(L, cast(lu_byte *, L1) - EXTRASPACE,
60 size + EXTRASPACE);
61}
62
63
64static void stack_init (lua_State *L1, lua_State *L) { 52static void stack_init (lua_State *L1, lua_State *L) {
65 L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TObject); 53 L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TObject);
66 L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK; 54 L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK;
@@ -122,20 +110,21 @@ static void preinit_state (lua_State *L) {
122 110
123 111
124static void close_state (lua_State *L) { 112static void close_state (lua_State *L) {
113 global_State *g = G(L);
125 luaF_close(L, L->stack); /* close all upvalues for this thread */ 114 luaF_close(L, L->stack); /* close all upvalues for this thread */
126 luaC_sweep(L, 1); /* collect all elements */ 115 luaC_sweep(L, 1); /* collect all elements */
127 lua_assert(G(L)->rootgc == NULL); 116 lua_assert(g->rootgc == NULL);
128 lua_assert(G(L)->rootudata == NULL); 117 lua_assert(g->rootudata == NULL);
129 luaS_freeall(L); 118 luaS_freeall(L);
130 luaZ_freebuffer(L, &G(L)->buff); 119 luaZ_freebuffer(L, &g->buff);
131 freestack(L, L); 120 freestack(L, L);
132 lua_assert(G(L)->nblocks == sizeof(LG)); 121 lua_assert(g->nblocks == sizeof(LG));
133 freestate(NULL, L, sizeof(LG)); 122 (*g->realloc)(g->ud, fromstate(L), state_size(LG), 0);
134} 123}
135 124
136 125
137lua_State *luaE_newthread (lua_State *L) { 126lua_State *luaE_newthread (lua_State *L) {
138 lua_State *L1 = mallocstate(L, sizeof(lua_State)); 127 lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State)));
139 luaC_link(L, valtogco(L1), LUA_TTHREAD); 128 luaC_link(L, valtogco(L1), LUA_TTHREAD);
140 preinit_state(L1); 129 preinit_state(L1);
141 L1->l_G = L->l_G; 130 L1->l_G = L->l_G;
@@ -149,20 +138,24 @@ void luaE_freethread (lua_State *L, lua_State *L1) {
149 luaF_close(L1, L1->stack); /* close all upvalues for this thread */ 138 luaF_close(L1, L1->stack); /* close all upvalues for this thread */
150 lua_assert(L1->openupval == NULL); 139 lua_assert(L1->openupval == NULL);
151 freestack(L, L1); 140 freestack(L, L1);
152 freestate(L, L1, sizeof(lua_State)); 141 luaM_free(L, fromstate(L1), state_size(lua_State));
153} 142}
154 143
155 144
156LUA_API lua_State *lua_open (void) { 145LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
157 lua_State *L = mallocstate(NULL, sizeof(LG)); 146 lua_State *L;
158 global_State *g; 147 global_State *g;
159 if (L == NULL) return NULL; 148 void *l = (*f)(ud, NULL, 0, state_size(LG));
149 if (l == NULL) return NULL;
150 L = tostate(l);
160 g = &((LG *)L)->g; 151 g = &((LG *)L)->g;
161 L->tt = LUA_TTHREAD; 152 L->tt = LUA_TTHREAD;
162 L->marked = 0; 153 L->marked = 0;
163 L->next = L->gclist = NULL; 154 L->next = L->gclist = NULL;
164 preinit_state(L); 155 preinit_state(L);
165 L->l_G = g; 156 L->l_G = g;
157 g->realloc = f;
158 g->ud = ud;
166 g->mainthread = L; 159 g->mainthread = L;
167 g->GCthreshold = 0; /* mark it as unfinished state */ 160 g->GCthreshold = 0; /* mark it as unfinished state */
168 g->strt.size = 0; 161 g->strt.size = 0;