]>
git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/src/third_party/lua-5.3.1/src/lstate.c
2 ** $Id: lstate.c,v 2.128 2015/03/04 13:31:21 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, time(NULL))
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(buff + 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)
98 void luaE_setdebt (global_State
*g
, l_mem debt
) {
99 g
->totalbytes
-= (debt
- g
->GCdebt
);
104 CallInfo
*luaE_extendCI (lua_State
*L
) {
105 CallInfo
*ci
= luaM_new(L
, CallInfo
);
106 lua_assert(L
->ci
->next
== NULL
);
108 ci
->previous
= L
->ci
;
115 ** free all CallInfo structures not in use by a thread
117 void luaE_freeCI (lua_State
*L
) {
118 CallInfo
*ci
= L
->ci
;
119 CallInfo
*next
= ci
->next
;
121 while ((ci
= next
) != NULL
) {
129 ** free half of the CallInfo structures not in use by a thread
131 void luaE_shrinkCI (lua_State
*L
) {
132 CallInfo
*ci
= L
->ci
;
133 while (ci
->next
!= NULL
) { /* while there is 'next' */
134 CallInfo
*next2
= ci
->next
->next
; /* next's next */
135 if (next2
== NULL
) break;
136 luaM_free(L
, ci
->next
); /* remove next */
137 ci
->next
= next2
; /* remove 'next' from the list */
138 next2
->previous
= ci
;
144 static void stack_init (lua_State
*L1
, lua_State
*L
) {
146 /* initialize stack array */
147 L1
->stack
= luaM_newvector(L
, BASIC_STACK_SIZE
, TValue
);
148 L1
->stacksize
= BASIC_STACK_SIZE
;
149 for (i
= 0; i
< BASIC_STACK_SIZE
; i
++)
150 setnilvalue(L1
->stack
+ i
); /* erase new stack */
152 L1
->stack_last
= L1
->stack
+ L1
->stacksize
- EXTRA_STACK
;
153 /* initialize first ci */
155 ci
->next
= ci
->previous
= NULL
;
158 setnilvalue(L1
->top
++); /* 'function' entry for this 'ci' */
159 ci
->top
= L1
->top
+ LUA_MINSTACK
;
164 static void freestack (lua_State
*L
) {
165 if (L
->stack
== NULL
)
166 return; /* stack not completely built yet */
167 L
->ci
= &L
->base_ci
; /* free the entire 'ci' list */
169 luaM_freearray(L
, L
->stack
, L
->stacksize
); /* free stack array */
174 ** Create registry table and its predefined values
176 static void init_registry (lua_State
*L
, global_State
*g
) {
178 /* create registry */
179 Table
*registry
= luaH_new(L
);
180 sethvalue(L
, &g
->l_registry
, registry
);
181 luaH_resize(L
, registry
, LUA_RIDX_LAST
, 0);
182 /* registry[LUA_RIDX_MAINTHREAD] = L */
183 setthvalue(L
, &temp
, L
); /* temp = L */
184 luaH_setint(L
, registry
, LUA_RIDX_MAINTHREAD
, &temp
);
185 /* registry[LUA_RIDX_GLOBALS] = table of globals */
186 sethvalue(L
, &temp
, luaH_new(L
)); /* temp = new table (global table) */
187 luaH_setint(L
, registry
, LUA_RIDX_GLOBALS
, &temp
);
192 ** open parts of the state that may cause memory-allocation errors.
193 ** ('g->version' != NULL flags that the state was completely build)
195 static void f_luaopen (lua_State
*L
, void *ud
) {
196 global_State
*g
= G(L
);
198 stack_init(L
, L
); /* init stack */
203 g
->gcrunning
= 1; /* allow gc */
204 g
->version
= lua_version(NULL
);
205 luai_userstateopen(L
);
210 ** preinitialize a thread with consistent values without allocating
211 ** any memory (to avoid errors)
213 static void preinit_thread (lua_State
*L
, global_State
*g
) {
218 L
->twups
= L
; /* thread has no upvalues */
223 L
->basehookcount
= 0;
233 static void close_state (lua_State
*L
) {
234 global_State
*g
= G(L
);
235 luaF_close(L
, L
->stack
); /* close all upvalues for this thread */
236 luaC_freeallobjects(L
); /* collect all objects */
237 if (g
->version
) /* closing a fully built state? */
238 luai_userstateclose(L
);
239 luaM_freearray(L
, G(L
)->strt
.hash
, G(L
)->strt
.size
);
240 luaZ_freebuffer(L
, &g
->buff
);
242 lua_assert(gettotalbytes(g
) == sizeof(LG
));
243 (*g
->frealloc
)(g
->ud
, fromstate(L
), sizeof(LG
), 0); /* free main block */
247 LUA_API lua_State
*lua_newthread (lua_State
*L
) {
248 global_State
*g
= G(L
);
252 /* create new thread */
253 L1
= &cast(LX
*, luaM_newobject(L
, LUA_TTHREAD
, sizeof(LX
)))->l
;
254 L1
->marked
= luaC_white(g
);
255 L1
->tt
= LUA_TTHREAD
;
256 /* link it on list 'allgc' */
258 g
->allgc
= obj2gco(L1
);
259 /* anchor it on L stack */
260 setthvalue(L
, L
->top
, L1
);
262 preinit_thread(L1
, g
);
263 L1
->hookmask
= L
->hookmask
;
264 L1
->basehookcount
= L
->basehookcount
;
267 /* initialize L1 extra space */
268 memcpy(lua_getextraspace(L1
), lua_getextraspace(g
->mainthread
),
270 luai_userstatethread(L
, L1
);
271 stack_init(L1
, L
); /* init stack */
277 void luaE_freethread (lua_State
*L
, lua_State
*L1
) {
278 LX
*l
= fromstate(L1
);
279 luaF_close(L1
, L1
->stack
); /* close all upvalues for this thread */
280 lua_assert(L1
->openupval
== NULL
);
281 luai_userstatefree(L
, L1
);
287 LUA_API lua_State
*lua_newstate (lua_Alloc f
, void *ud
) {
291 LG
*l
= cast(LG
*, (*f
)(ud
, NULL
, LUA_TTHREAD
, sizeof(LG
)));
292 if (l
== NULL
) return NULL
;
297 g
->currentwhite
= bitmask(WHITE0BIT
);
298 L
->marked
= luaC_white(g
);
299 preinit_thread(L
, g
);
303 g
->seed
= makeseed(L
);
304 g
->gcrunning
= 0; /* no GC while building state */
306 g
->strt
.size
= g
->strt
.nuse
= 0;
308 setnilvalue(&g
->l_registry
);
309 luaZ_initbuffer(L
, &g
->buff
);
312 g
->gcstate
= GCSpause
;
313 g
->gckind
= KGC_NORMAL
;
314 g
->allgc
= g
->finobj
= g
->tobefnz
= g
->fixedgc
= NULL
;
316 g
->gray
= g
->grayagain
= NULL
;
317 g
->weak
= g
->ephemeron
= g
->allweak
= NULL
;
319 g
->totalbytes
= sizeof(LG
);
322 g
->gcpause
= LUAI_GCPAUSE
;
323 g
->gcstepmul
= LUAI_GCMUL
;
324 for (i
=0; i
< LUA_NUMTAGS
; i
++) g
->mt
[i
] = NULL
;
325 if (luaD_rawrunprotected(L
, f_luaopen
, NULL
) != LUA_OK
) {
326 /* memory allocation error: free partial state */
334 LUA_API
void lua_close (lua_State
*L
) {
335 L
= G(L
)->mainthread
; /* only the main thread can be closed */