]>
git.zerfleddert.de Git - proxmark3-svn/blob - liblua/lstate.c
2 ** $Id: lstate.c,v 2.99 2012/10/02 17:40:53 roberto Exp $
4 ** See Copyright Notice in lua.h
29 #if !defined(LUAI_GCPAUSE)
30 #define LUAI_GCPAUSE 200 /* 200% */
33 #if !defined(LUAI_GCMAJOR)
34 #define LUAI_GCMAJOR 200 /* 200% */
37 #if !defined(LUAI_GCMUL)
38 #define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */
42 #define MEMERRMSG "not enough memory"
46 ** a macro to help the creation of a unique random seed when a state is
47 ** created; the seed is used to randomize hashes.
49 #if !defined(luai_makeseed)
51 #define luai_makeseed() cast(unsigned int, time(NULL))
57 ** thread state + extra space
60 #if defined(LUAI_EXTRASPACE)
61 char buff
[LUAI_EXTRASPACE
];
68 ** Main thread combines a thread state and the global state
77 #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
81 ** Compute an initial seed as random as possible. In ANSI, rely on
82 ** Address Space Layout Randomization (if present) to increase
85 #define addbuff(b,p,e) \
86 { size_t t = cast(size_t, e); \
87 memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); }
89 static unsigned int makeseed (lua_State
*L
) {
90 char buff
[4 * sizeof(size_t)];
91 unsigned int h
= luai_makeseed();
93 addbuff(buff
, p
, L
); /* heap variable */
94 addbuff(buff
, p
, &h
); /* local variable */
95 addbuff(buff
, p
, luaO_nilobject
); /* global variable */
96 addbuff(buff
, p
, &lua_newstate
); /* public function */
97 lua_assert(p
== sizeof(buff
));
98 return luaS_hash(buff
, p
, h
);
103 ** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
106 void luaE_setdebt (global_State
*g
, l_mem debt
) {
107 g
->totalbytes
-= (debt
- g
->GCdebt
);
112 CallInfo
*luaE_extendCI (lua_State
*L
) {
113 CallInfo
*ci
= luaM_new(L
, CallInfo
);
114 lua_assert(L
->ci
->next
== NULL
);
116 ci
->previous
= L
->ci
;
122 void luaE_freeCI (lua_State
*L
) {
123 CallInfo
*ci
= L
->ci
;
124 CallInfo
*next
= ci
->next
;
126 while ((ci
= next
) != NULL
) {
133 static void stack_init (lua_State
*L1
, lua_State
*L
) {
135 /* initialize stack array */
136 L1
->stack
= luaM_newvector(L
, BASIC_STACK_SIZE
, TValue
);
137 L1
->stacksize
= BASIC_STACK_SIZE
;
138 for (i
= 0; i
< BASIC_STACK_SIZE
; i
++)
139 setnilvalue(L1
->stack
+ i
); /* erase new stack */
141 L1
->stack_last
= L1
->stack
+ L1
->stacksize
- EXTRA_STACK
;
142 /* initialize first ci */
144 ci
->next
= ci
->previous
= NULL
;
147 setnilvalue(L1
->top
++); /* 'function' entry for this 'ci' */
148 ci
->top
= L1
->top
+ LUA_MINSTACK
;
153 static void freestack (lua_State
*L
) {
154 if (L
->stack
== NULL
)
155 return; /* stack not completely built yet */
156 L
->ci
= &L
->base_ci
; /* free the entire 'ci' list */
158 luaM_freearray(L
, L
->stack
, L
->stacksize
); /* free stack array */
163 ** Create registry table and its predefined values
165 static void init_registry (lua_State
*L
, global_State
*g
) {
167 /* create registry */
168 Table
*registry
= luaH_new(L
);
169 sethvalue(L
, &g
->l_registry
, registry
);
170 luaH_resize(L
, registry
, LUA_RIDX_LAST
, 0);
171 /* registry[LUA_RIDX_MAINTHREAD] = L */
172 setthvalue(L
, &mt
, L
);
173 luaH_setint(L
, registry
, LUA_RIDX_MAINTHREAD
, &mt
);
174 /* registry[LUA_RIDX_GLOBALS] = table of globals */
175 sethvalue(L
, &mt
, luaH_new(L
));
176 luaH_setint(L
, registry
, LUA_RIDX_GLOBALS
, &mt
);
181 ** open parts of the state that may cause memory-allocation errors
183 static void f_luaopen (lua_State
*L
, void *ud
) {
184 global_State
*g
= G(L
);
186 stack_init(L
, L
); /* init stack */
188 luaS_resize(L
, MINSTRTABSIZE
); /* initial size of string table */
191 /* pre-create memory-error message */
192 g
->memerrmsg
= luaS_newliteral(L
, MEMERRMSG
);
193 luaS_fix(g
->memerrmsg
); /* it should never be collected */
194 g
->gcrunning
= 1; /* allow gc */
199 ** preinitialize a state with consistent values without allocating
200 ** any memory (to avoid errors)
202 static void preinit_state (lua_State
*L
, global_State
*g
) {
211 L
->basehookcount
= 0;
221 static void close_state (lua_State
*L
) {
222 global_State
*g
= G(L
);
223 luaF_close(L
, L
->stack
); /* close all upvalues for this thread */
224 luaC_freeallobjects(L
); /* collect all objects */
225 luaM_freearray(L
, G(L
)->strt
.hash
, G(L
)->strt
.size
);
226 luaZ_freebuffer(L
, &g
->buff
);
228 lua_assert(gettotalbytes(g
) == sizeof(LG
));
229 (*g
->frealloc
)(g
->ud
, fromstate(L
), sizeof(LG
), 0); /* free main block */
233 LUA_API lua_State
*lua_newthread (lua_State
*L
) {
237 L1
= &luaC_newobj(L
, LUA_TTHREAD
, sizeof(LX
), NULL
, offsetof(LX
, l
))->th
;
238 setthvalue(L
, L
->top
, L1
);
240 preinit_state(L1
, G(L
));
241 L1
->hookmask
= L
->hookmask
;
242 L1
->basehookcount
= L
->basehookcount
;
245 luai_userstatethread(L
, L1
);
246 stack_init(L1
, L
); /* init stack */
252 void luaE_freethread (lua_State
*L
, lua_State
*L1
) {
253 LX
*l
= fromstate(L1
);
254 luaF_close(L1
, L1
->stack
); /* close all upvalues for this thread */
255 lua_assert(L1
->openupval
== NULL
);
256 luai_userstatefree(L
, L1
);
262 LUA_API lua_State
*lua_newstate (lua_Alloc f
, void *ud
) {
266 LG
*l
= cast(LG
*, (*f
)(ud
, NULL
, LUA_TTHREAD
, sizeof(LG
)));
267 if (l
== NULL
) return NULL
;
272 g
->currentwhite
= bit2mask(WHITE0BIT
, FIXEDBIT
);
273 L
->marked
= luaC_white(g
);
274 g
->gckind
= KGC_NORMAL
;
279 g
->seed
= makeseed(L
);
280 g
->uvhead
.u
.l
.prev
= &g
->uvhead
;
281 g
->uvhead
.u
.l
.next
= &g
->uvhead
;
282 g
->gcrunning
= 0; /* no GC while building state */
287 setnilvalue(&g
->l_registry
);
288 luaZ_initbuffer(L
, &g
->buff
);
290 g
->version
= lua_version(NULL
);
291 g
->gcstate
= GCSpause
;
295 g
->sweepgc
= g
->sweepfin
= NULL
;
296 g
->gray
= g
->grayagain
= NULL
;
297 g
->weak
= g
->ephemeron
= g
->allweak
= NULL
;
298 g
->totalbytes
= sizeof(LG
);
300 g
->gcpause
= LUAI_GCPAUSE
;
301 g
->gcmajorinc
= LUAI_GCMAJOR
;
302 g
->gcstepmul
= LUAI_GCMUL
;
303 for (i
=0; i
< LUA_NUMTAGS
; i
++) g
->mt
[i
] = NULL
;
304 if (luaD_rawrunprotected(L
, f_luaopen
, NULL
) != LUA_OK
) {
305 /* memory allocation error: free partial state */
310 luai_userstateopen(L
);
315 LUA_API
void lua_close (lua_State
*L
) {
316 L
= G(L
)->mainthread
; /* only the main thread can be closed */
318 luai_userstateclose(L
);