1 /* $NetBSD: lstate.c,v 1.1.1.2 2012/03/15 00:08:14 alnsn Exp $ */
4 ** $Id: lstate.c,v 1.1.1.2 2012/03/15 00:08:14 alnsn Exp $
6 ** See Copyright Notice in lua.h
29 #define state_size(x) (sizeof(x) + LUAI_EXTRASPACE)
30 #define fromstate(l) (cast(lu_byte *, (l)) - LUAI_EXTRASPACE)
31 #define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE))
35 ** Main thread combines a thread state and the global state
44 static void stack_init (lua_State
*L1
, lua_State
*L
) {
45 /* initialize CallInfo array */
46 L1
->base_ci
= luaM_newvector(L
, BASIC_CI_SIZE
, CallInfo
);
48 L1
->size_ci
= BASIC_CI_SIZE
;
49 L1
->end_ci
= L1
->base_ci
+ L1
->size_ci
- 1;
50 /* initialize stack array */
51 L1
->stack
= luaM_newvector(L
, BASIC_STACK_SIZE
+ EXTRA_STACK
, TValue
);
52 L1
->stacksize
= BASIC_STACK_SIZE
+ EXTRA_STACK
;
54 L1
->stack_last
= L1
->stack
+(L1
->stacksize
- EXTRA_STACK
)-1;
55 /* initialize first ci */
56 L1
->ci
->func
= L1
->top
;
57 setnilvalue(L1
->top
++); /* `function' entry for this `ci' */
58 L1
->base
= L1
->ci
->base
= L1
->top
;
59 L1
->ci
->top
= L1
->top
+ LUA_MINSTACK
;
63 static void freestack (lua_State
*L
, lua_State
*L1
) {
64 luaM_freearray(L
, L1
->base_ci
, L1
->size_ci
, CallInfo
);
65 luaM_freearray(L
, L1
->stack
, L1
->stacksize
, TValue
);
70 ** open parts that may cause memory-allocation errors
72 static void f_luaopen (lua_State
*L
, void *ud
) {
73 global_State
*g
= G(L
);
75 stack_init(L
, L
); /* init stack */
76 sethvalue(L
, gt(L
), luaH_new(L
, 0, 2)); /* table of globals */
77 sethvalue(L
, registry(L
), luaH_new(L
, 0, 2)); /* registry */
78 luaS_resize(L
, MINSTRTABSIZE
); /* initial size of string table */
81 luaS_fix(luaS_newliteral(L
, MEMERRMSG
));
82 g
->GCthreshold
= 4*g
->totalbytes
;
86 static void preinit_state (lua_State
*L
, global_State
*g
) {
98 L
->nCcalls
= L
->baseCcalls
= 0;
100 L
->base_ci
= L
->ci
= NULL
;
107 static void close_state (lua_State
*L
) {
108 global_State
*g
= G(L
);
109 luaF_close(L
, L
->stack
); /* close all upvalues for this thread */
110 luaC_freeall(L
); /* collect all objects */
111 lua_assert(g
->rootgc
== obj2gco(L
));
112 lua_assert(g
->strt
.nuse
== 0);
113 luaM_freearray(L
, G(L
)->strt
.hash
, G(L
)->strt
.size
, TString
*);
114 luaZ_freebuffer(L
, &g
->buff
);
116 lua_assert(g
->totalbytes
== sizeof(LG
));
117 (*g
->frealloc
)(g
->ud
, fromstate(L
), state_size(LG
), 0);
121 lua_State
*luaE_newthread (lua_State
*L
) {
122 lua_State
*L1
= tostate(luaM_malloc(L
, state_size(lua_State
)));
123 luaC_link(L
, obj2gco(L1
), LUA_TTHREAD
);
124 preinit_state(L1
, G(L
));
125 stack_init(L1
, L
); /* init stack */
126 setobj2n(L
, gt(L1
), gt(L
)); /* share table of globals */
127 L1
->hookmask
= L
->hookmask
;
128 L1
->basehookcount
= L
->basehookcount
;
131 lua_assert(iswhite(obj2gco(L1
)));
136 void luaE_freethread (lua_State
*L
, lua_State
*L1
) {
137 luaF_close(L1
, L1
->stack
); /* close all upvalues for this thread */
138 lua_assert(L1
->openupval
== NULL
);
139 luai_userstatefree(L1
);
141 luaM_freemem(L
, fromstate(L1
), state_size(lua_State
));
145 LUA_API lua_State
*lua_newstate (lua_Alloc f
, void *ud
) {
149 void *l
= (*f
)(ud
, NULL
, 0, state_size(LG
));
150 if (l
== NULL
) return NULL
;
155 g
->currentwhite
= bit2mask(WHITE0BIT
, FIXEDBIT
);
156 L
->marked
= luaC_white(g
);
157 set2bits(L
->marked
, FIXEDBIT
, SFIXEDBIT
);
162 g
->uvhead
.u
.l
.prev
= &g
->uvhead
;
163 g
->uvhead
.u
.l
.next
= &g
->uvhead
;
164 g
->GCthreshold
= 0; /* mark it as unfinished state */
168 setnilvalue(registry(L
));
169 luaZ_initbuffer(L
, &g
->buff
);
171 g
->gcstate
= GCSpause
;
172 g
->rootgc
= obj2gco(L
);
174 g
->sweepgc
= &g
->rootgc
;
179 g
->totalbytes
= sizeof(LG
);
180 g
->gcpause
= LUAI_GCPAUSE
;
181 g
->gcstepmul
= LUAI_GCMUL
;
183 for (i
=0; i
<NUM_TAGS
; i
++) g
->mt
[i
] = NULL
;
184 if (luaD_rawrunprotected(L
, f_luaopen
, NULL
) != 0) {
185 /* memory allocation error: free partial state */
190 luai_userstateopen(L
);
195 static void callallgcTM (lua_State
*L
, void *ud
) {
197 luaC_callGCTM(L
); /* call GC metamethods for all udata */
201 LUA_API
void lua_close (lua_State
*L
) {
202 L
= G(L
)->mainthread
; /* only the main thread can be closed */
204 luaF_close(L
, L
->stack
); /* close all upvalues for this thread */
205 luaC_separateudata(L
, 1); /* separate udata that have GC metamethods */
206 L
->errfunc
= 0; /* no error function during GC metamethods */
207 do { /* repeat until no more errors */
209 L
->base
= L
->top
= L
->ci
->base
;
210 L
->nCcalls
= L
->baseCcalls
= 0;
211 } while (luaD_rawrunprotected(L
, callallgcTM
, NULL
) != 0);
212 lua_assert(G(L
)->tmudata
== NULL
);
213 luai_userstateclose(L
);