diff options
Diffstat (limited to '')
-rw-r--r-- | lstate.c | 71 |
1 files changed, 17 insertions, 54 deletions
@@ -52,46 +52,15 @@ typedef struct LG { | |||
52 | 52 | ||
53 | 53 | ||
54 | /* | 54 | /* |
55 | ** A macro to create a "random" seed when a state is created; | 55 | ** set GCdebt to a new value keeping the value (totalobjs + GCdebt) |
56 | ** the seed is used to randomize string hashes. | 56 | ** invariant (and avoiding underflows in 'totalobjs') |
57 | */ | 57 | */ |
58 | #if !defined(luai_makeseed) | 58 | void luaE_setdebt (global_State *g, l_obj debt) { |
59 | 59 | l_obj tb = gettotalobjs(g); | |
60 | #include <time.h> | ||
61 | |||
62 | /* | ||
63 | ** Compute an initial seed with some level of randomness. | ||
64 | ** Rely on Address Space Layout Randomization (if present) and | ||
65 | ** current time. | ||
66 | */ | ||
67 | #define addbuff(b,p,e) \ | ||
68 | { size_t t = cast_sizet(e); \ | ||
69 | memcpy(b + p, &t, sizeof(t)); p += sizeof(t); } | ||
70 | |||
71 | static unsigned int luai_makeseed (lua_State *L) { | ||
72 | char buff[3 * sizeof(size_t)]; | ||
73 | unsigned int h = cast_uint(time(NULL)); | ||
74 | int p = 0; | ||
75 | addbuff(buff, p, L); /* heap variable */ | ||
76 | addbuff(buff, p, &h); /* local variable */ | ||
77 | addbuff(buff, p, &lua_newstate); /* public function */ | ||
78 | lua_assert(p == sizeof(buff)); | ||
79 | return luaS_hash(buff, p, h); | ||
80 | } | ||
81 | |||
82 | #endif | ||
83 | |||
84 | |||
85 | /* | ||
86 | ** set GCdebt to a new value keeping the value (totalbytes + GCdebt) | ||
87 | ** invariant (and avoiding underflows in 'totalbytes') | ||
88 | */ | ||
89 | void luaE_setdebt (global_State *g, l_mem debt) { | ||
90 | l_mem tb = gettotalbytes(g); | ||
91 | lua_assert(tb > 0); | 60 | lua_assert(tb > 0); |
92 | if (debt < tb - MAX_LMEM) | 61 | if (debt > MAX_LOBJ - tb) |
93 | debt = tb - MAX_LMEM; /* will make 'totalbytes == MAX_LMEM' */ | 62 | debt = MAX_LOBJ - tb; /* will make 'totalobjs == MAX_LMEM' */ |
94 | g->totalbytes = tb - debt; | 63 | g->totalobjs = tb + debt; |
95 | g->GCdebt = debt; | 64 | g->GCdebt = debt; |
96 | } | 65 | } |
97 | 66 | ||
@@ -281,7 +250,8 @@ static void close_state (lua_State *L) { | |||
281 | } | 250 | } |
282 | luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); | 251 | luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); |
283 | freestack(L); | 252 | freestack(L); |
284 | lua_assert(gettotalbytes(g) == sizeof(LG)); | 253 | lua_assert(g->totalbytes == sizeof(LG)); |
254 | lua_assert(gettotalobjs(g) == 1); | ||
285 | (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */ | 255 | (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */ |
286 | } | 256 | } |
287 | 257 | ||
@@ -352,15 +322,7 @@ LUA_API int lua_closethread (lua_State *L, lua_State *from) { | |||
352 | } | 322 | } |
353 | 323 | ||
354 | 324 | ||
355 | /* | 325 | LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned int seed) { |
356 | ** Deprecated! Use 'lua_closethread' instead. | ||
357 | */ | ||
358 | LUA_API int lua_resetthread (lua_State *L) { | ||
359 | return lua_closethread(L, NULL); | ||
360 | } | ||
361 | |||
362 | |||
363 | LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { | ||
364 | int i; | 326 | int i; |
365 | lua_State *L; | 327 | lua_State *L; |
366 | global_State *g; | 328 | global_State *g; |
@@ -380,7 +342,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { | |||
380 | g->warnf = NULL; | 342 | g->warnf = NULL; |
381 | g->ud_warn = NULL; | 343 | g->ud_warn = NULL; |
382 | g->mainthread = L; | 344 | g->mainthread = L; |
383 | g->seed = luai_makeseed(L); | 345 | g->seed = seed; |
384 | g->gcstp = GCSTPGC; /* no GC while building state */ | 346 | g->gcstp = GCSTPGC; /* no GC while building state */ |
385 | g->strt.size = g->strt.nuse = 0; | 347 | g->strt.size = g->strt.nuse = 0; |
386 | g->strt.hash = NULL; | 348 | g->strt.hash = NULL; |
@@ -398,14 +360,15 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { | |||
398 | g->weak = g->ephemeron = g->allweak = NULL; | 360 | g->weak = g->ephemeron = g->allweak = NULL; |
399 | g->twups = NULL; | 361 | g->twups = NULL; |
400 | g->totalbytes = sizeof(LG); | 362 | g->totalbytes = sizeof(LG); |
363 | g->totalobjs = 1; | ||
364 | g->marked = 0; | ||
401 | g->GCdebt = 0; | 365 | g->GCdebt = 0; |
402 | g->lastatomic = 0; | ||
403 | setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */ | 366 | setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */ |
404 | setgcparam(g->gcpause, LUAI_GCPAUSE); | 367 | setgcparam(g, gcpause, LUAI_GCPAUSE); |
405 | setgcparam(g->gcstepmul, LUAI_GCMUL); | 368 | setgcparam(g, gcstepmul, LUAI_GCMUL); |
406 | g->gcstepsize = LUAI_GCSTEPSIZE; | 369 | g->gcstepsize = LUAI_GCSTEPSIZE; |
407 | setgcparam(g->genmajormul, LUAI_GENMAJORMUL); | 370 | setgcparam(g, genmajormul, LUAI_GENMAJORMUL); |
408 | g->genminormul = LUAI_GENMINORMUL; | 371 | setgcparam(g, genminormul, LUAI_GENMINORMUL); |
409 | for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; | 372 | for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; |
410 | if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { | 373 | if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { |
411 | /* memory allocation error: free partial state */ | 374 | /* memory allocation error: free partial state */ |