2
0

lstate.c 8.1 KB

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