2 ** $Id: lstate.c,v 2.133.1.1 2017/04/19 17:39:34 roberto Exp $
4 ** See Copyright Notice in lua.h
31 #if !defined(LUAI_GCPAUSE)
32 #define LUAI_GCPAUSE 200 /* 200% */
35 #if !defined(LUAI_GCMUL)
36 #define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */
41 ** a macro to help the creation of a unique random seed when a state is
42 ** created; the seed is used to randomize hashes.
44 #if !defined(luai_makeseed)
46 #define luai_makeseed() cast(unsigned int, ~0)
52 ** thread state + extra space
55 lu_byte extra_
[LUA_EXTRASPACE
];
61 ** Main thread combines a thread state and the global state
70 #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
74 ** Compute an initial seed as random as possible. Rely on Address Space
75 ** Layout Randomization (if present) to increase randomness..
77 #define addbuff(b,p,e) \
78 { size_t t = cast(size_t, e); \
79 memcpy(b + p, &t, sizeof(t)); p += sizeof(t); }
81 static unsigned int makeseed (lua_State
*L
) {
82 char buff
[4 * sizeof(size_t)];
83 unsigned int h
= luai_makeseed();
85 addbuff(buff
, p
, L
); /* heap variable */
86 addbuff(buff
, p
, &h
); /* local variable */
87 addbuff(buff
, p
, luaO_nilobject
); /* global variable */
88 addbuff(buff
, p
, &lua_newstate
); /* public function */
89 lua_assert(p
== sizeof(buff
));
90 return luaS_hash(buff
, p
, h
);
95 ** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
96 ** invariant (and avoiding underflows in 'totalbytes')
98 void luaE_setdebt (global_State
*g
, l_mem debt
) {
99 l_mem tb
= gettotalbytes(g
);
101 if (debt
< tb
- MAX_LMEM
)
102 debt
= tb
- MAX_LMEM
; /* will make 'totalbytes == MAX_LMEM' */
103 g
->totalbytes
= tb
- debt
;
108 CallInfo
*luaE_extendCI (lua_State
*L
) {
109 CallInfo
*ci
= luaM_new(L
, CallInfo
);
110 lua_assert(L
->ci
->next
== NULL
);
112 ci
->previous
= L
->ci
;
120 ** free all CallInfo structures not in use by a thread
122 void luaE_freeCI (lua_State
*L
) {
123 CallInfo
*ci
= L
->ci
;
124 CallInfo
*next
= ci
->next
;
126 while ((ci
= next
) != NULL
) {
135 ** free half of the CallInfo structures not in use by a thread
137 void luaE_shrinkCI (lua_State
*L
) {
138 CallInfo
*ci
= L
->ci
;
139 CallInfo
*next2
; /* next's next */
140 /* while there are two nexts */
141 while (ci
->next
!= NULL
&& (next2
= ci
->next
->next
) != NULL
) {
142 luaM_free(L
, ci
->next
); /* free next */
144 ci
->next
= next2
; /* remove 'next' from the list */
145 next2
->previous
= ci
;
146 ci
= next2
; /* keep next's next */
151 static void stack_init (lua_State
*L1
, lua_State
*L
) {
153 /* initialize stack array */
154 L1
->stack
= luaM_newvector(L
, BASIC_STACK_SIZE
, TValue
);
155 L1
->stacksize
= BASIC_STACK_SIZE
;
156 for (i
= 0; i
< BASIC_STACK_SIZE
; i
++)
157 setnilvalue(L1
->stack
+ i
); /* erase new stack */
159 L1
->stack_last
= L1
->stack
+ L1
->stacksize
- EXTRA_STACK
;
160 /* initialize first ci */
162 ci
->next
= ci
->previous
= NULL
;
165 setnilvalue(L1
->top
++); /* 'function' entry for this 'ci' */
166 ci
->top
= L1
->top
+ LUA_MINSTACK
;
171 static void freestack (lua_State
*L
) {
172 if (L
->stack
== NULL
)
173 return; /* stack not completely built yet */
174 L
->ci
= &L
->base_ci
; /* free the entire 'ci' list */
176 lua_assert(L
->nci
== 0);
177 luaM_freearray(L
, L
->stack
, L
->stacksize
); /* free stack array */
182 ** Create registry table and its predefined values
184 static void init_registry (lua_State
*L
, global_State
*g
) {
186 /* create registry */
187 Table
*registry
= luaH_new(L
);
188 sethvalue(L
, &g
->l_registry
, registry
);
189 luaH_resize(L
, registry
, LUA_RIDX_LAST
, 0);
190 /* registry[LUA_RIDX_MAINTHREAD] = L */
191 setthvalue(L
, &temp
, L
); /* temp = L */
192 luaH_setint(L
, registry
, LUA_RIDX_MAINTHREAD
, &temp
);
193 /* registry[LUA_RIDX_GLOBALS] = table of globals */
194 sethvalue(L
, &temp
, luaH_new(L
)); /* temp = new table (global table) */
195 luaH_setint(L
, registry
, LUA_RIDX_GLOBALS
, &temp
);
200 ** open parts of the state that may cause memory-allocation errors.
201 ** ('g->version' != NULL flags that the state was completely build)
203 static void f_luaopen (lua_State
*L
, void *ud
) {
204 global_State
*g
= G(L
);
206 stack_init(L
, L
); /* init stack */
211 g
->gcrunning
= 1; /* allow gc */
212 g
->version
= lua_version(NULL
);
213 luai_userstateopen(L
);
218 ** preinitialize a thread with consistent values without allocating
219 ** any memory (to avoid errors)
221 static void preinit_thread (lua_State
*L
, global_State
*g
) {
227 L
->twups
= L
; /* thread has no upvalues */
232 L
->basehookcount
= 0;
242 static void close_state (lua_State
*L
) {
243 global_State
*g
= G(L
);
244 luaF_close(L
, L
->stack
); /* close all upvalues for this thread */
245 luaC_freeallobjects(L
); /* collect all objects */
246 if (g
->version
) /* closing a fully built state? */
247 luai_userstateclose(L
);
248 luaM_freearray(L
, G(L
)->strt
.hash
, G(L
)->strt
.size
);
250 lua_assert(gettotalbytes(g
) == sizeof(LG
));
251 (*g
->frealloc
)(g
->ud
, fromstate(L
), sizeof(LG
), 0); /* free main block */
255 LUA_API lua_State
*lua_newthread (lua_State
*L
) {
256 global_State
*g
= G(L
);
260 /* create new thread */
261 L1
= &cast(LX
*, luaM_newobject(L
, LUA_TTHREAD
, sizeof(LX
)))->l
;
262 L1
->marked
= luaC_white(g
);
263 L1
->tt
= LUA_TTHREAD
;
264 /* link it on list 'allgc' */
266 g
->allgc
= obj2gco(L1
);
267 /* anchor it on L stack */
268 setthvalue(L
, L
->top
, L1
);
270 preinit_thread(L1
, g
);
271 L1
->hookmask
= L
->hookmask
;
272 L1
->basehookcount
= L
->basehookcount
;
275 /* initialize L1 extra space */
276 memcpy(lua_getextraspace(L1
), lua_getextraspace(g
->mainthread
),
278 luai_userstatethread(L
, L1
);
279 stack_init(L1
, L
); /* init stack */
285 void luaE_freethread (lua_State
*L
, lua_State
*L1
) {
286 LX
*l
= fromstate(L1
);
287 luaF_close(L1
, L1
->stack
); /* close all upvalues for this thread */
288 lua_assert(L1
->openupval
== NULL
);
289 luai_userstatefree(L
, L1
);
294 #ifdef FEATURE_STATIC_LUASTATE
295 #undef FEATURE_STATIC_LUASTATE
300 LUA_API lua_State
*lua_newstate (lua_Alloc f
, void *ud
) {
304 #ifdef FEATURE_STATIC_LUASTATE
307 LG
*l
= cast(LG
*, (*f
)(ud
, NULL
, LUA_TTHREAD
, sizeof(LG
)));
308 if (l
== NULL
) return NULL
;
314 g
->currentwhite
= bitmask(WHITE0BIT
);
315 L
->marked
= luaC_white(g
);
316 preinit_thread(L
, g
);
320 g
->seed
= G_SEED_FIXED
;//fixed seed. 2024.5.10 by wendal. makeseed(L);
321 g
->gcrunning
= 0; /* no GC while building state */
323 g
->strt
.size
= g
->strt
.nuse
= 0;
325 setnilvalue(&g
->l_registry
);
328 g
->gcstate
= GCSpause
;
329 g
->gckind
= KGC_NORMAL
;
330 g
->allgc
= g
->finobj
= g
->tobefnz
= g
->fixedgc
= NULL
;
332 g
->gray
= g
->grayagain
= NULL
;
333 g
->weak
= g
->ephemeron
= g
->allweak
= NULL
;
335 g
->totalbytes
= sizeof(LG
);
338 g
->gcpause
= LUAI_GCPAUSE
;
339 g
->gcstepmul
= LUAI_GCMUL
;
340 for (i
=0; i
< LUA_NUMTAGS
; i
++) g
->mt
[i
] = NULL
;
341 if (luaD_rawrunprotected(L
, f_luaopen
, NULL
) != LUA_OK
) {
342 /* memory allocation error: free partial state */
350 LUA_API
void lua_close (lua_State
*L
) {
351 L
= G(L
)->mainthread
; /* only the main thread can be closed */