lstate.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. ** $Id: lstate.c,v 2.144 2017/11/03 12:12:30 roberto Exp roberto $
  3. ** Global State
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lstate_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <stddef.h>
  10. #include <string.h>
  11. #include "lua.h"
  12. #include "lapi.h"
  13. #include "ldebug.h"
  14. #include "ldo.h"
  15. #include "lfunc.h"
  16. #include "lgc.h"
  17. #include "llex.h"
  18. #include "lmem.h"
  19. #include "lstate.h"
  20. #include "lstring.h"
  21. #include "ltable.h"
  22. #include "ltm.h"
  23. /*
  24. ** a macro to help the creation of a unique random seed when a state is
  25. ** created; the seed is used to randomize hashes.
  26. */
  27. #if !defined(luai_makeseed)
  28. #include <time.h>
  29. #define luai_makeseed() cast(unsigned int, time(NULL))
  30. #endif
  31. /*
  32. ** thread state + extra space
  33. */
  34. typedef struct LX {
  35. lu_byte extra_[LUA_EXTRASPACE];
  36. lua_State l;
  37. } LX;
  38. /*
  39. ** Main thread combines a thread state and the global state
  40. */
  41. typedef struct LG {
  42. LX l;
  43. global_State g;
  44. } LG;
  45. #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
  46. /*
  47. ** Compute an initial seed as random as possible. Rely on Address Space
  48. ** Layout Randomization (if present) to increase randomness..
  49. */
  50. #define addbuff(b,p,e) \
  51. { size_t t = cast(size_t, e); \
  52. memcpy(b + p, &t, sizeof(t)); p += sizeof(t); }
  53. static unsigned int makeseed (lua_State *L) {
  54. char buff[4 * sizeof(size_t)];
  55. unsigned int h = luai_makeseed();
  56. int p = 0;
  57. addbuff(buff, p, L); /* heap variable */
  58. addbuff(buff, p, &h); /* local variable */
  59. addbuff(buff, p, luaO_nilobject); /* global variable */
  60. addbuff(buff, p, &lua_newstate); /* public function */
  61. lua_assert(p == sizeof(buff));
  62. return luaS_hash(buff, p, h);
  63. }
  64. /*
  65. ** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
  66. ** invariant (and avoiding underflows in 'totalbytes')
  67. */
  68. void luaE_setdebt (global_State *g, l_mem debt) {
  69. l_mem tb = gettotalbytes(g);
  70. lua_assert(tb > 0);
  71. if (debt < tb - MAX_LMEM)
  72. debt = tb - MAX_LMEM; /* will make 'totalbytes == MAX_LMEM' */
  73. g->totalbytes = tb - debt;
  74. g->GCdebt = debt;
  75. }
  76. static void stack_init (lua_State *L1, lua_State *L) {
  77. int i;
  78. /* initialize stack array */
  79. L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, StackValue);
  80. L1->stacksize = BASIC_STACK_SIZE;
  81. for (i = 0; i < BASIC_STACK_SIZE; i++)
  82. setnilvalue(s2v(L1->stack + i)); /* erase new stack */
  83. L1->top = L1->stack;
  84. L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;
  85. /* initialize first 'function' */
  86. L1->func = L1->stack;
  87. L1->func->stkci.previous = 0; /* end of linked list */
  88. L1->func->stkci.framesize = LUA_MINSTACK + 1;
  89. callstatus(L1->func) = 0;
  90. setnilvalue(s2v(L1->top)); /* 'function' entry for this 'ci' */
  91. L1->top++;
  92. }
  93. static void freestack (lua_State *L) {
  94. if (L->stack == NULL)
  95. return; /* stack not completely built yet */
  96. lua_assert(L->nci == 0);
  97. luaM_freearray(L, L->stack, L->stacksize); /* free stack array */
  98. }
  99. /*
  100. ** Create registry table and its predefined values
  101. */
  102. static void init_registry (lua_State *L, global_State *g) {
  103. TValue temp;
  104. /* create registry */
  105. Table *registry = luaH_new(L);
  106. sethvalue(L, &g->l_registry, registry);
  107. luaH_resize(L, registry, LUA_RIDX_LAST, 0);
  108. /* registry[LUA_RIDX_MAINTHREAD] = L */
  109. setthvalue(L, &temp, L); /* temp = L */
  110. luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &temp);
  111. /* registry[LUA_RIDX_GLOBALS] = table of globals */
  112. sethvalue(L, &temp, luaH_new(L)); /* temp = new table (global table) */
  113. luaH_setint(L, registry, LUA_RIDX_GLOBALS, &temp);
  114. }
  115. /*
  116. ** open parts of the state that may cause memory-allocation errors.
  117. ** ('g->version' != NULL flags that the state was completely build)
  118. */
  119. static void f_luaopen (lua_State *L, void *ud) {
  120. global_State *g = G(L);
  121. UNUSED(ud);
  122. stack_init(L, L); /* init stack */
  123. init_registry(L, g);
  124. luaS_init(L);
  125. luaT_init(L);
  126. luaX_init(L);
  127. g->gcrunning = 1; /* allow gc */
  128. g->gcemergency = 0;
  129. g->version = lua_version(NULL);
  130. luai_userstateopen(L);
  131. }
  132. /*
  133. ** preinitialize a thread with consistent values without allocating
  134. ** any memory (to avoid errors)
  135. */
  136. static void preinit_thread (lua_State *L, global_State *g) {
  137. G(L) = g;
  138. L->stack = NULL;
  139. L->func = NULL;
  140. L->nci = 0;
  141. L->stacksize = 0;
  142. L->twups = L; /* thread has no upvalues */
  143. L->errorJmp = NULL;
  144. L->nCcalls = 0;
  145. L->hook = NULL;
  146. L->hookmask = 0;
  147. L->basehookcount = 0;
  148. L->allowhook = 1;
  149. resethookcount(L);
  150. L->openupval = NULL;
  151. L->nny = 1;
  152. L->status = LUA_OK;
  153. L->errfunc = 0;
  154. }
  155. static void close_state (lua_State *L) {
  156. global_State *g = G(L);
  157. luaF_close(L, L->stack); /* close all upvalues for this thread */
  158. luaC_freeallobjects(L); /* collect all objects */
  159. if (g->version) /* closing a fully built state? */
  160. luai_userstateclose(L);
  161. luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
  162. freestack(L);
  163. lua_assert(gettotalbytes(g) == sizeof(LG));
  164. (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
  165. }
  166. LUA_API lua_State *lua_newthread (lua_State *L) {
  167. global_State *g = G(L);
  168. lua_State *L1;
  169. lua_lock(L);
  170. luaC_checkGC(L);
  171. /* create new thread */
  172. L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l;
  173. L1->marked = luaC_white(g);
  174. L1->tt = LUA_TTHREAD;
  175. /* link it on list 'allgc' */
  176. L1->next = g->allgc;
  177. g->allgc = obj2gco(L1);
  178. /* anchor it on L stack */
  179. setthvalue2s(L, L->top, L1);
  180. api_incr_top(L);
  181. preinit_thread(L1, g);
  182. L1->hookmask = L->hookmask;
  183. L1->basehookcount = L->basehookcount;
  184. L1->hook = L->hook;
  185. resethookcount(L1);
  186. /* initialize L1 extra space */
  187. memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread),
  188. LUA_EXTRASPACE);
  189. luai_userstatethread(L, L1);
  190. stack_init(L1, L); /* init stack */
  191. lua_unlock(L);
  192. return L1;
  193. }
  194. void luaE_freethread (lua_State *L, lua_State *L1) {
  195. LX *l = fromstate(L1);
  196. luaF_close(L1, L1->stack); /* close all upvalues for this thread */
  197. lua_assert(L1->openupval == NULL);
  198. luai_userstatefree(L, L1);
  199. freestack(L1);
  200. luaM_free(L, l);
  201. }
  202. LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
  203. int i;
  204. lua_State *L;
  205. global_State *g;
  206. LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
  207. if (l == NULL) return NULL;
  208. L = &l->l.l;
  209. g = &l->g;
  210. L->tt = LUA_TTHREAD;
  211. g->currentwhite = bitmask(WHITE0BIT);
  212. L->marked = luaC_white(g);
  213. preinit_thread(L, g);
  214. g->allgc = obj2gco(L); /* by now, only object is the main thread */
  215. L->next = NULL;
  216. g->frealloc = f;
  217. g->ud = ud;
  218. g->mainthread = L;
  219. g->seed = makeseed(L);
  220. g->gcrunning = 0; /* no GC while building state */
  221. g->strt.size = g->strt.nuse = 0;
  222. g->strt.hash = NULL;
  223. setnilvalue(&g->l_registry);
  224. g->panic = NULL;
  225. g->version = NULL;
  226. g->gcstate = GCSpause;
  227. g->gckind = KGC_INC;
  228. g->finobj = g->tobefnz = g->fixedgc = NULL;
  229. g->survival = g->old = g->reallyold = NULL;
  230. g->finobjsur = g->finobjold = g->finobjrold = NULL;
  231. g->sweepgc = NULL;
  232. g->gray = g->grayagain = NULL;
  233. g->weak = g->ephemeron = g->allweak = g->protogray = NULL;
  234. g->twups = NULL;
  235. g->totalbytes = sizeof(LG);
  236. g->GCdebt = 0;
  237. setgcparam(g->gcpause, LUAI_GCPAUSE);
  238. setgcparam(g->gcstepmul, LUAI_GCMUL);
  239. g->gcstepsize = LUAI_GCSTEPSIZE;
  240. setgcparam(g->genmajormul, LUAI_GENMAJORMUL);
  241. g->genminormul = LUAI_GENMINORMUL;
  242. for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
  243. if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
  244. /* memory allocation error: free partial state */
  245. close_state(L);
  246. L = NULL;
  247. }
  248. return L;
  249. }
  250. LUA_API void lua_close (lua_State *L) {
  251. L = G(L)->mainthread; /* only the main thread can be closed */
  252. lua_lock(L);
  253. close_state(L);
  254. }