change: g->seed改成固定值,使得字符串的hash值固定
[LuatOS.git] / lua / src / lstate.c
blob315f2eec3011a96d6ecb0ed6812c5a1b56e4681a
1 /*
2 ** $Id: lstate.c,v 2.133.1.1 2017/04/19 17:39:34 roberto Exp $
3 ** Global State
4 ** See Copyright Notice in lua.h
5 */
7 #define lstate_c
8 #define LUA_CORE
10 #include "lprefix.h"
13 #include <stddef.h>
14 #include <string.h>
16 #include "lua.h"
18 #include "lapi.h"
19 #include "ldebug.h"
20 #include "ldo.h"
21 #include "lfunc.h"
22 #include "lgc.h"
23 #include "llex.h"
24 #include "lmem.h"
25 #include "lstate.h"
26 #include "lstring.h"
27 #include "ltable.h"
28 #include "ltm.h"
31 #if !defined(LUAI_GCPAUSE)
32 #define LUAI_GCPAUSE 200 /* 200% */
33 #endif
35 #if !defined(LUAI_GCMUL)
36 #define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */
37 #endif
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)
45 #include <time.h>
46 #define luai_makeseed() cast(unsigned int, ~0)
47 #endif
52 ** thread state + extra space
54 typedef struct LX {
55 lu_byte extra_[LUA_EXTRASPACE];
56 lua_State l;
57 } LX;
61 ** Main thread combines a thread state and the global state
63 typedef struct LG {
64 LX l;
65 global_State g;
66 } LG;
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();
84 int p = 0;
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);
100 lua_assert(tb > 0);
101 if (debt < tb - MAX_LMEM)
102 debt = tb - MAX_LMEM; /* will make 'totalbytes == MAX_LMEM' */
103 g->totalbytes = tb - debt;
104 g->GCdebt = debt;
108 CallInfo *luaE_extendCI (lua_State *L) {
109 CallInfo *ci = luaM_new(L, CallInfo);
110 lua_assert(L->ci->next == NULL);
111 L->ci->next = ci;
112 ci->previous = L->ci;
113 ci->next = NULL;
114 L->nci++;
115 return 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;
125 ci->next = NULL;
126 while ((ci = next) != NULL) {
127 next = ci->next;
128 luaM_free(L, ci);
129 L->nci--;
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 */
143 L->nci--;
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) {
152 int i; CallInfo *ci;
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 */
158 L1->top = L1->stack;
159 L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;
160 /* initialize first ci */
161 ci = &L1->base_ci;
162 ci->next = ci->previous = NULL;
163 ci->callstatus = 0;
164 ci->func = L1->top;
165 setnilvalue(L1->top++); /* 'function' entry for this 'ci' */
166 ci->top = L1->top + LUA_MINSTACK;
167 L1->ci = ci;
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 */
175 luaE_freeCI(L);
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) {
185 TValue temp;
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);
205 UNUSED(ud);
206 stack_init(L, L); /* init stack */
207 init_registry(L, g);
208 luaS_init(L);
209 luaT_init(L);
210 luaX_init(L);
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) {
222 G(L) = g;
223 L->stack = NULL;
224 L->ci = NULL;
225 L->nci = 0;
226 L->stacksize = 0;
227 L->twups = L; /* thread has no upvalues */
228 L->errorJmp = NULL;
229 L->nCcalls = 0;
230 L->hook = NULL;
231 L->hookmask = 0;
232 L->basehookcount = 0;
233 L->allowhook = 1;
234 resethookcount(L);
235 L->openupval = NULL;
236 L->nny = 1;
237 L->status = LUA_OK;
238 L->errfunc = 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);
249 freestack(L);
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);
257 lua_State *L1;
258 lua_lock(L);
259 luaC_checkGC(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' */
265 L1->next = g->allgc;
266 g->allgc = obj2gco(L1);
267 /* anchor it on L stack */
268 setthvalue(L, L->top, L1);
269 api_incr_top(L);
270 preinit_thread(L1, g);
271 L1->hookmask = L->hookmask;
272 L1->basehookcount = L->basehookcount;
273 L1->hook = L->hook;
274 resethookcount(L1);
275 /* initialize L1 extra space */
276 memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread),
277 LUA_EXTRASPACE);
278 luai_userstatethread(L, L1);
279 stack_init(L1, L); /* init stack */
280 lua_unlock(L);
281 return L1;
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);
290 freestack(L1);
291 luaM_free(L, l);
294 #ifdef FEATURE_STATIC_LUASTATE
295 #undef FEATURE_STATIC_LUASTATE
296 //static LG LUAT_LG;
297 #endif
300 LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
301 int i;
302 lua_State *L;
303 global_State *g;
304 #ifdef FEATURE_STATIC_LUASTATE
305 LG *l = &LUAT_LG;
306 #else
307 LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
308 if (l == NULL) return NULL;
309 #endif
310 L = &l->l.l;
311 g = &l->g;
312 L->next = NULL;
313 L->tt = LUA_TTHREAD;
314 g->currentwhite = bitmask(WHITE0BIT);
315 L->marked = luaC_white(g);
316 preinit_thread(L, g);
317 g->frealloc = f;
318 g->ud = ud;
319 g->mainthread = L;
320 g->seed = G_SEED_FIXED;//fixed seed. 2024.5.10 by wendal. makeseed(L);
321 g->gcrunning = 0; /* no GC while building state */
322 g->GCestimate = 0;
323 g->strt.size = g->strt.nuse = 0;
324 g->strt.hash = NULL;
325 setnilvalue(&g->l_registry);
326 g->panic = NULL;
327 g->version = NULL;
328 g->gcstate = GCSpause;
329 g->gckind = KGC_NORMAL;
330 g->allgc = g->finobj = g->tobefnz = g->fixedgc = NULL;
331 g->sweepgc = NULL;
332 g->gray = g->grayagain = NULL;
333 g->weak = g->ephemeron = g->allweak = NULL;
334 g->twups = NULL;
335 g->totalbytes = sizeof(LG);
336 g->GCdebt = 0;
337 g->gcfinnum = 0;
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 */
343 close_state(L);
344 L = NULL;
346 return L;
350 LUA_API void lua_close (lua_State *L) {
351 L = G(L)->mainthread; /* only the main thread can be closed */
352 lua_lock(L);
353 close_state(L);