lstate.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. ** $Id: lstate.c,v 2.133 2015/11/13 12:16:51 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. #if !defined(LUAI_GCPAUSE)
  24. #define LUAI_GCPAUSE 200 /* 200% */
  25. #endif
  26. #if !defined(LUAI_GCMUL)
  27. #define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */
  28. #endif
  29. /*
  30. ** a macro to help the creation of a unique random seed when a state is
  31. ** created; the seed is used to randomize hashes.
  32. */
  33. #if !defined(luai_makeseed)
  34. #include <time.h>
  35. #define luai_makeseed() cast(unsigned int, time(NULL))
  36. #endif
  37. /*
  38. ** thread state + extra space
  39. */
  40. typedef struct LX
  41. {
  42. lu_byte extra_[LUA_EXTRASPACE];
  43. lua_State l;
  44. } LX;
  45. /*
  46. ** Main thread combines a thread state and the global state
  47. */
  48. typedef struct LG
  49. {
  50. LX l;
  51. global_State g;
  52. } LG;
  53. #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
  54. /*
  55. ** Compute an initial seed as random as possible. Rely on Address Space
  56. ** Layout Randomization (if present) to increase randomness..
  57. */
  58. #define addbuff(b,p,e) \
  59. { size_t t = cast(size_t, e); \
  60. memcpy(b + p, &t, sizeof(t)); p += sizeof(t); }
  61. static unsigned int makeseed(lua_State *L)
  62. {
  63. char buff[4 * sizeof(size_t)];
  64. unsigned int h = luai_makeseed();
  65. int p = 0;
  66. addbuff(buff, p, L); /* heap variable */
  67. addbuff(buff, p, &h); /* local variable */
  68. addbuff(buff, p, luaO_nilobject); /* global variable */
  69. addbuff(buff, p, &lua_newstate); /* public function */
  70. lua_assert(p == sizeof(buff));
  71. return luaS_hash(buff, p, h);
  72. }
  73. /*
  74. ** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
  75. ** invariant (and avoiding underflows in 'totalbytes')
  76. */
  77. void luaE_setdebt(global_State *g, l_mem debt)
  78. {
  79. l_mem tb = gettotalbytes(g);
  80. lua_assert(tb > 0);
  81. if (debt < tb - MAX_LMEM)
  82. debt = tb - MAX_LMEM; /* will make 'totalbytes == MAX_LMEM' */
  83. g->totalbytes = tb - debt;
  84. g->GCdebt = debt;
  85. }
  86. CallInfo *luaE_extendCI(lua_State *L)
  87. {
  88. CallInfo *ci = luaM_new(L, CallInfo);
  89. lua_assert(L->ci->next == NULL);
  90. L->ci->next = ci;
  91. ci->previous = L->ci;
  92. ci->next = NULL;
  93. L->nci++;
  94. return ci;
  95. }
  96. /*
  97. ** free all CallInfo structures not in use by a thread
  98. */
  99. void luaE_freeCI(lua_State *L)
  100. {
  101. CallInfo *ci = L->ci;
  102. CallInfo *next = ci->next;
  103. ci->next = NULL;
  104. while ((ci = next) != NULL)
  105. {
  106. next = ci->next;
  107. luaM_free(L, ci);
  108. L->nci--;
  109. }
  110. }
  111. /*
  112. ** free half of the CallInfo structures not in use by a thread
  113. */
  114. void luaE_shrinkCI(lua_State *L)
  115. {
  116. CallInfo *ci = L->ci;
  117. CallInfo *next2; /* next's next */
  118. /* while there are two nexts */
  119. while (ci->next != NULL && (next2 = ci->next->next) != NULL)
  120. {
  121. luaM_free(L, ci->next); /* free next */
  122. L->nci--;
  123. ci->next = next2; /* remove 'next' from the list */
  124. next2->previous = ci;
  125. ci = next2; /* keep next's next */
  126. }
  127. }
  128. static void stack_init(lua_State *L1, lua_State *L)
  129. {
  130. int i;
  131. CallInfo *ci;
  132. /* initialize stack array */
  133. L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue);
  134. L1->stacksize = BASIC_STACK_SIZE;
  135. for (i = 0; i < BASIC_STACK_SIZE; i++)
  136. setnilvalue(L1->stack + i); /* erase new stack */
  137. L1->top = L1->stack;
  138. L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;
  139. /* initialize first ci */
  140. ci = &L1->base_ci;
  141. ci->next = ci->previous = NULL;
  142. ci->callstatus = 0;
  143. ci->func = L1->top;
  144. setnilvalue(L1->top++); /* 'function' entry for this 'ci' */
  145. ci->top = L1->top + LUA_MINSTACK;
  146. L1->ci = ci;
  147. }
  148. static void freestack(lua_State *L)
  149. {
  150. if (L->stack == NULL)
  151. return; /* stack not completely built yet */
  152. L->ci = &L->base_ci; /* free the entire 'ci' list */
  153. luaE_freeCI(L);
  154. lua_assert(L->nci == 0);
  155. luaM_freearray(L, L->stack, L->stacksize); /* free stack array */
  156. }
  157. /*
  158. ** Create registry table and its predefined values
  159. */
  160. static void init_registry(lua_State *L, global_State *g)
  161. {
  162. TValue temp;
  163. /* create registry */
  164. Table *registry = luaH_new(L);
  165. sethvalue(L, &g->l_registry, registry);
  166. luaH_resize(L, registry, LUA_RIDX_LAST, 0);
  167. /* registry[LUA_RIDX_MAINTHREAD] = L */
  168. setthvalue(L, &temp, L); /* temp = L */
  169. luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &temp);
  170. /* registry[LUA_RIDX_GLOBALS] = table of globals */
  171. sethvalue(L, &temp, luaH_new(L)); /* temp = new table (global table) */
  172. luaH_setint(L, registry, LUA_RIDX_GLOBALS, &temp);
  173. }
  174. /*
  175. ** open parts of the state that may cause memory-allocation errors.
  176. ** ('g->version' != NULL flags that the state was completely build)
  177. */
  178. static void f_luaopen(lua_State *L, void *ud)
  179. {
  180. global_State *g = G(L);
  181. UNUSED(ud);
  182. stack_init(L, L); /* init stack */
  183. init_registry(L, g);
  184. luaS_init(L);
  185. luaT_init(L);
  186. luaX_init(L);
  187. g->gcrunning = 1; /* allow gc */
  188. g->version = lua_version(NULL);
  189. luai_userstateopen(L);
  190. }
  191. /*
  192. ** preinitialize a thread with consistent values without allocating
  193. ** any memory (to avoid errors)
  194. */
  195. static void preinit_thread(lua_State *L, global_State *g)
  196. {
  197. G(L) = g;
  198. L->stack = NULL;
  199. L->ci = NULL;
  200. L->nci = 0;
  201. L->stacksize = 0;
  202. L->twups = L; /* thread has no upvalues */
  203. L->errorJmp = NULL;
  204. L->nCcalls = 0;
  205. L->hook = NULL;
  206. L->hookmask = 0;
  207. L->basehookcount = 0;
  208. L->allowhook = 1;
  209. resethookcount(L);
  210. L->openupval = NULL;
  211. L->nny = 1;
  212. L->status = LUA_OK;
  213. L->errfunc = 0;
  214. }
  215. static void close_state(lua_State *L)
  216. {
  217. global_State *g = G(L);
  218. luaF_close(L, L->stack); /* close all upvalues for this thread */
  219. luaC_freeallobjects(L); /* collect all objects */
  220. if (g->version) /* closing a fully built state? */
  221. luai_userstateclose(L);
  222. luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
  223. freestack(L);
  224. lua_assert(gettotalbytes(g) == sizeof(LG));
  225. (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
  226. }
  227. LUA_API lua_State *lua_newthread(lua_State *L)
  228. {
  229. global_State *g = G(L);
  230. lua_State *L1;
  231. lua_lock(L);
  232. luaC_checkGC(L);
  233. /* create new thread */
  234. L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l;
  235. L1->marked = luaC_white(g);
  236. L1->tt = LUA_TTHREAD;
  237. /* link it on list 'allgc' */
  238. L1->next = g->allgc;
  239. g->allgc = obj2gco(L1);
  240. /* anchor it on L stack */
  241. setthvalue(L, L->top, L1);
  242. api_incr_top(L);
  243. preinit_thread(L1, g);
  244. L1->hookmask = L->hookmask;
  245. L1->basehookcount = L->basehookcount;
  246. L1->hook = L->hook;
  247. resethookcount(L1);
  248. /* initialize L1 extra space */
  249. memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread),
  250. LUA_EXTRASPACE);
  251. luai_userstatethread(L, L1);
  252. stack_init(L1, L); /* init stack */
  253. lua_unlock(L);
  254. return L1;
  255. }
  256. void luaE_freethread(lua_State *L, lua_State *L1)
  257. {
  258. LX *l = fromstate(L1);
  259. luaF_close(L1, L1->stack); /* close all upvalues for this thread */
  260. lua_assert(L1->openupval == NULL);
  261. luai_userstatefree(L, L1);
  262. freestack(L1);
  263. luaM_free(L, l);
  264. }
  265. LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud)
  266. {
  267. int i;
  268. lua_State *L;
  269. global_State *g;
  270. LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
  271. if (l == NULL) return NULL;
  272. L = &l->l.l;
  273. g = &l->g;
  274. L->next = NULL;
  275. L->tt = LUA_TTHREAD;
  276. g->currentwhite = bitmask(WHITE0BIT);
  277. L->marked = luaC_white(g);
  278. preinit_thread(L, g);
  279. g->frealloc = f;
  280. g->ud = ud;
  281. g->mainthread = L;
  282. g->seed = makeseed(L);
  283. g->gcrunning = 0; /* no GC while building state */
  284. g->GCestimate = 0;
  285. g->strt.size = g->strt.nuse = 0;
  286. g->strt.hash = NULL;
  287. setnilvalue(&g->l_registry);
  288. g->panic = NULL;
  289. g->version = NULL;
  290. g->gcstate = GCSpause;
  291. g->gckind = KGC_NORMAL;
  292. g->allgc = g->finobj = g->tobefnz = g->fixedgc = NULL;
  293. g->sweepgc = NULL;
  294. g->gray = g->grayagain = NULL;
  295. g->weak = g->ephemeron = g->allweak = NULL;
  296. g->twups = NULL;
  297. g->totalbytes = sizeof(LG);
  298. g->GCdebt = 0;
  299. g->gcfinnum = 0;
  300. g->gcpause = LUAI_GCPAUSE;
  301. g->gcstepmul = LUAI_GCMUL;
  302. for (i = 0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
  303. if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK)
  304. {
  305. /* memory allocation error: free partial state */
  306. close_state(L);
  307. L = NULL;
  308. }
  309. return L;
  310. }
  311. LUA_API void lua_close(lua_State *L)
  312. {
  313. L = G(L)->mainthread; /* only the main thread can be closed */
  314. lua_lock(L);
  315. close_state(L);
  316. }