lstate.c 8.4 KB

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