9
3

lstate.c 8.3 KB

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