lstate.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. ** $Id: lstate.c,v 2.125 2014/07/24 16:17:56 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. #define MEMERRMSG "not enough memory"
  30. /*
  31. ** a macro to help the creation of a unique random seed when a state is
  32. ** created; the seed is used to randomize hashes.
  33. */
  34. #if !defined(luai_makeseed)
  35. #include <time.h>
  36. #define luai_makeseed() cast(unsigned int, time(NULL))
  37. #endif
  38. /*
  39. ** thread state + extra space
  40. */
  41. typedef struct LX {
  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. LX l;
  50. global_State g;
  51. } LG;
  52. #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
  53. /*
  54. ** Compute an initial seed as random as possible. In ANSI, rely on
  55. ** Address Space Layout Randomization (if present) to increase
  56. ** randomness..
  57. */
  58. #define addbuff(b,p,e) \
  59. { size_t t = cast(size_t, e); \
  60. memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); }
  61. static unsigned int makeseed (lua_State *L) {
  62. char buff[4 * sizeof(size_t)];
  63. unsigned int h = luai_makeseed();
  64. int p = 0;
  65. addbuff(buff, p, L); /* heap variable */
  66. addbuff(buff, p, &h); /* local variable */
  67. addbuff(buff, p, luaO_nilobject); /* global variable */
  68. addbuff(buff, p, &lua_newstate); /* public function */
  69. lua_assert(p == sizeof(buff));
  70. return luaS_hash(buff, p, h);
  71. }
  72. /*
  73. ** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
  74. ** invariant
  75. */
  76. void luaE_setdebt (global_State *g, l_mem debt) {
  77. g->totalbytes -= (debt - g->GCdebt);
  78. g->GCdebt = debt;
  79. }
  80. CallInfo *luaE_extendCI (lua_State *L) {
  81. CallInfo *ci = luaM_new(L, CallInfo);
  82. lua_assert(L->ci->next == NULL);
  83. L->ci->next = ci;
  84. ci->previous = L->ci;
  85. ci->next = NULL;
  86. return ci;
  87. }
  88. /*
  89. ** free all CallInfo structures not in use by a thread
  90. */
  91. void luaE_freeCI (lua_State *L) {
  92. CallInfo *ci = L->ci;
  93. CallInfo *next = ci->next;
  94. ci->next = NULL;
  95. while ((ci = next) != NULL) {
  96. next = ci->next;
  97. luaM_free(L, ci);
  98. }
  99. }
  100. /*
  101. ** free half of the CallInfo structures not in use by a thread
  102. */
  103. void luaE_shrinkCI (lua_State *L) {
  104. CallInfo *ci = L->ci;
  105. while (ci->next != NULL) { /* while there is 'next' */
  106. CallInfo *next2 = ci->next->next; /* next's next */
  107. if (next2 == NULL) break;
  108. luaM_free(L, ci->next); /* remove next */
  109. ci->next = next2; /* remove 'next' from the list */
  110. next2->previous = ci;
  111. ci = next2;
  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, TValue);
  118. L1->stacksize = BASIC_STACK_SIZE;
  119. for (i = 0; i < BASIC_STACK_SIZE; i++)
  120. setnilvalue(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(L1->top++); /* 'function' entry for this 'ci' */
  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. luaM_freearray(L, L->stack, L->stacksize); /* free stack array */
  138. }
  139. /*
  140. ** Create registry table and its predefined values
  141. */
  142. static void init_registry (lua_State *L, global_State *g) {
  143. TValue temp;
  144. /* create registry */
  145. Table *registry = luaH_new(L);
  146. sethvalue(L, &g->l_registry, registry);
  147. luaH_resize(L, registry, LUA_RIDX_LAST, 0);
  148. /* registry[LUA_RIDX_MAINTHREAD] = L */
  149. setthvalue(L, &temp, L); /* temp = L */
  150. luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &temp);
  151. /* registry[LUA_RIDX_GLOBALS] = table of globals */
  152. sethvalue(L, &temp, luaH_new(L)); /* temp = new table (global table) */
  153. luaH_setint(L, registry, LUA_RIDX_GLOBALS, &temp);
  154. }
  155. /*
  156. ** open parts of the state that may cause memory-allocation errors.
  157. ** ('g->version' != NULL flags that the state was completely build)
  158. */
  159. static void f_luaopen (lua_State *L, void *ud) {
  160. global_State *g = G(L);
  161. UNUSED(ud);
  162. stack_init(L, L); /* init stack */
  163. init_registry(L, g);
  164. luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
  165. luaT_init(L);
  166. luaX_init(L);
  167. /* pre-create memory-error message */
  168. g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
  169. luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
  170. g->gcrunning = 1; /* allow gc */
  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->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. luaZ_freebuffer(L, &g->buff);
  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. setthvalue(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->next = NULL;
  253. L->tt = LUA_TTHREAD;
  254. g->currentwhite = bitmask(WHITE0BIT);
  255. L->marked = luaC_white(g);
  256. preinit_thread(L, g);
  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->GCestimate = 0;
  263. g->strt.size = g->strt.nuse = 0;
  264. g->strt.hash = NULL;
  265. setnilvalue(&g->l_registry);
  266. luaZ_initbuffer(L, &g->buff);
  267. g->panic = NULL;
  268. g->version = NULL;
  269. g->gcstate = GCSpause;
  270. g->gckind = KGC_NORMAL;
  271. g->allgc = g->finobj = g->tobefnz = g->fixedgc = NULL;
  272. g->sweepgc = NULL;
  273. g->gray = g->grayagain = NULL;
  274. g->weak = g->ephemeron = g->allweak = NULL;
  275. g->twups = NULL;
  276. g->totalbytes = sizeof(LG);
  277. g->GCdebt = 0;
  278. g->gcfinnum = 0;
  279. g->gcpause = LUAI_GCPAUSE;
  280. g->gcstepmul = LUAI_GCMUL;
  281. for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
  282. if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
  283. /* memory allocation error: free partial state */
  284. close_state(L);
  285. L = NULL;
  286. }
  287. return L;
  288. }
  289. LUA_API void lua_close (lua_State *L) {
  290. L = G(L)->mainthread; /* only the main thread can be closed */
  291. lua_lock(L);
  292. close_state(L);
  293. }