lstate.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. ** $Id: lstate.c,v 2.146 2017/11/07 13:25:26 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. CallInfo *luaE_extendCI (lua_State *L) {
  77. CallInfo *ci = luaM_new(L, CallInfo);
  78. lua_assert(L->ci->next == NULL);
  79. L->ci->next = ci;
  80. ci->previous = L->ci;
  81. ci->next = NULL;
  82. ci->u.l.trap = 0;
  83. L->nci++;
  84. return ci;
  85. }
  86. /*
  87. ** free all CallInfo structures not in use by a thread
  88. */
  89. void luaE_freeCI (lua_State *L) {
  90. CallInfo *ci = L->ci;
  91. CallInfo *next = ci->next;
  92. ci->next = NULL;
  93. while ((ci = next) != NULL) {
  94. next = ci->next;
  95. luaM_free(L, ci);
  96. L->nci--;
  97. }
  98. }
  99. /*
  100. ** free half of the CallInfo structures not in use by a thread
  101. */
  102. void luaE_shrinkCI (lua_State *L) {
  103. CallInfo *ci = L->ci;
  104. CallInfo *next2; /* next's next */
  105. /* while there are two nexts */
  106. while (ci->next != NULL && (next2 = ci->next->next) != NULL) {
  107. luaM_free(L, ci->next); /* free next */
  108. L->nci--;
  109. ci->next = next2; /* remove 'next' from the list */
  110. next2->previous = ci;
  111. ci = next2; /* keep next's next */
  112. }
  113. }
  114. static void stack_init (lua_State *L1, lua_State *L) {
  115. int i; CallInfo *ci;
  116. /* initialize stack array */
  117. L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, StackValue);
  118. L1->stacksize = BASIC_STACK_SIZE;
  119. for (i = 0; i < BASIC_STACK_SIZE; i++)
  120. setnilvalue(s2v(L1->stack + i)); /* erase new stack */
  121. L1->top = L1->stack;
  122. L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;
  123. /* initialize first ci */
  124. ci = &L1->base_ci;
  125. ci->next = ci->previous = NULL;
  126. ci->callstatus = 0;
  127. ci->func = L1->top;
  128. setnilvalue(s2v(L1->top)); /* 'function' entry for this 'ci' */
  129. L1->top++;
  130. ci->top = L1->top + LUA_MINSTACK;
  131. L1->ci = ci;
  132. }
  133. static void freestack (lua_State *L) {
  134. if (L->stack == NULL)
  135. return; /* stack not completely built yet */
  136. L->ci = &L->base_ci; /* free the entire 'ci' list */
  137. luaE_freeCI(L);
  138. lua_assert(L->nci == 0);
  139. luaM_freearray(L, L->stack, L->stacksize); /* free stack array */
  140. }
  141. /*
  142. ** Create registry table and its predefined values
  143. */
  144. static void init_registry (lua_State *L, global_State *g) {
  145. TValue temp;
  146. /* create registry */
  147. Table *registry = luaH_new(L);
  148. sethvalue(L, &g->l_registry, registry);
  149. luaH_resize(L, registry, LUA_RIDX_LAST, 0);
  150. /* registry[LUA_RIDX_MAINTHREAD] = L */
  151. setthvalue(L, &temp, L); /* temp = L */
  152. luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &temp);
  153. /* registry[LUA_RIDX_GLOBALS] = table of globals */
  154. sethvalue(L, &temp, luaH_new(L)); /* temp = new table (global table) */
  155. luaH_setint(L, registry, LUA_RIDX_GLOBALS, &temp);
  156. }
  157. /*
  158. ** open parts of the state that may cause memory-allocation errors.
  159. ** ('g->version' != NULL flags that the state was completely build)
  160. */
  161. static void f_luaopen (lua_State *L, void *ud) {
  162. global_State *g = G(L);
  163. UNUSED(ud);
  164. stack_init(L, L); /* init stack */
  165. init_registry(L, g);
  166. luaS_init(L);
  167. luaT_init(L);
  168. luaX_init(L);
  169. g->gcrunning = 1; /* allow gc */
  170. g->gcemergency = 0;
  171. g->version = lua_version(NULL);
  172. luai_userstateopen(L);
  173. }
  174. /*
  175. ** preinitialize a thread with consistent values without allocating
  176. ** any memory (to avoid errors)
  177. */
  178. static void preinit_thread (lua_State *L, global_State *g) {
  179. G(L) = g;
  180. L->stack = NULL;
  181. L->ci = NULL;
  182. L->nci = 0;
  183. L->stacksize = 0;
  184. L->twups = L; /* thread has no upvalues */
  185. L->errorJmp = NULL;
  186. L->nCcalls = 0;
  187. L->hook = NULL;
  188. L->hookmask = 0;
  189. L->basehookcount = 0;
  190. L->allowhook = 1;
  191. resethookcount(L);
  192. L->openupval = NULL;
  193. L->nny = 1;
  194. L->status = LUA_OK;
  195. L->errfunc = 0;
  196. }
  197. static void close_state (lua_State *L) {
  198. global_State *g = G(L);
  199. luaF_close(L, L->stack); /* close all upvalues for this thread */
  200. luaC_freeallobjects(L); /* collect all objects */
  201. if (g->version) /* closing a fully built state? */
  202. luai_userstateclose(L);
  203. luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
  204. freestack(L);
  205. lua_assert(gettotalbytes(g) == sizeof(LG));
  206. (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
  207. }
  208. LUA_API lua_State *lua_newthread (lua_State *L) {
  209. global_State *g = G(L);
  210. lua_State *L1;
  211. lua_lock(L);
  212. luaC_checkGC(L);
  213. /* create new thread */
  214. L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l;
  215. L1->marked = luaC_white(g);
  216. L1->tt = LUA_TTHREAD;
  217. /* link it on list 'allgc' */
  218. L1->next = g->allgc;
  219. g->allgc = obj2gco(L1);
  220. /* anchor it on L stack */
  221. setthvalue2s(L, L->top, L1);
  222. api_incr_top(L);
  223. preinit_thread(L1, g);
  224. L1->hookmask = L->hookmask;
  225. L1->basehookcount = L->basehookcount;
  226. L1->hook = L->hook;
  227. resethookcount(L1);
  228. /* initialize L1 extra space */
  229. memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread),
  230. LUA_EXTRASPACE);
  231. luai_userstatethread(L, L1);
  232. stack_init(L1, L); /* init stack */
  233. lua_unlock(L);
  234. return L1;
  235. }
  236. void luaE_freethread (lua_State *L, lua_State *L1) {
  237. LX *l = fromstate(L1);
  238. luaF_close(L1, L1->stack); /* close all upvalues for this thread */
  239. lua_assert(L1->openupval == NULL);
  240. luai_userstatefree(L, L1);
  241. freestack(L1);
  242. luaM_free(L, l);
  243. }
  244. LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
  245. int i;
  246. lua_State *L;
  247. global_State *g;
  248. LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
  249. if (l == NULL) return NULL;
  250. L = &l->l.l;
  251. g = &l->g;
  252. L->tt = LUA_TTHREAD;
  253. g->currentwhite = bitmask(WHITE0BIT);
  254. L->marked = luaC_white(g);
  255. preinit_thread(L, g);
  256. g->allgc = obj2gco(L); /* by now, only object is the main thread */
  257. L->next = NULL;
  258. g->frealloc = f;
  259. g->ud = ud;
  260. g->mainthread = L;
  261. g->seed = makeseed(L);
  262. g->gcrunning = 0; /* no GC while building state */
  263. g->strt.size = g->strt.nuse = 0;
  264. g->strt.hash = NULL;
  265. setnilvalue(&g->l_registry);
  266. g->panic = NULL;
  267. g->version = NULL;
  268. g->gcstate = GCSpause;
  269. g->gckind = KGC_INC;
  270. g->finobj = g->tobefnz = g->fixedgc = NULL;
  271. g->survival = g->old = g->reallyold = NULL;
  272. g->finobjsur = g->finobjold = g->finobjrold = NULL;
  273. g->sweepgc = NULL;
  274. g->gray = g->grayagain = NULL;
  275. g->weak = g->ephemeron = g->allweak = g->protogray = NULL;
  276. g->twups = NULL;
  277. g->totalbytes = sizeof(LG);
  278. g->GCdebt = 0;
  279. setgcparam(g->gcpause, LUAI_GCPAUSE);
  280. setgcparam(g->gcstepmul, LUAI_GCMUL);
  281. g->gcstepsize = LUAI_GCSTEPSIZE;
  282. setgcparam(g->genmajormul, LUAI_GENMAJORMUL);
  283. g->genminormul = LUAI_GENMINORMUL;
  284. for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
  285. if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
  286. /* memory allocation error: free partial state */
  287. close_state(L);
  288. L = NULL;
  289. }
  290. return L;
  291. }
  292. LUA_API void lua_close (lua_State *L) {
  293. L = G(L)->mainthread; /* only the main thread can be closed */
  294. lua_lock(L);
  295. close_state(L);
  296. }