2
0

lstate.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. ** $Id: lstate.c,v 2.124 2014/07/24 14:00:16 roberto Exp roberto $
  3. ** Global State
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stddef.h>
  7. #include <string.h>
  8. #define lstate_c
  9. #define LUA_CORE
  10. #include "lua.h"
  11. #include "lapi.h"
  12. #include "ldebug.h"
  13. #include "ldo.h"
  14. #include "lfunc.h"
  15. #include "lgc.h"
  16. #include "llex.h"
  17. #include "lmem.h"
  18. #include "lstate.h"
  19. #include "lstring.h"
  20. #include "ltable.h"
  21. #include "ltm.h"
  22. #if !defined(LUAI_GCPAUSE)
  23. #define LUAI_GCPAUSE 200 /* 200% */
  24. #endif
  25. #if !defined(LUAI_GCMUL)
  26. #define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */
  27. #endif
  28. #define MEMERRMSG "not enough memory"
  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. In ANSI, rely on
  54. ** Address Space Layout Randomization (if present) to increase
  55. ** randomness..
  56. */
  57. #define addbuff(b,p,e) \
  58. { size_t t = cast(size_t, e); \
  59. memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); }
  60. static unsigned int makeseed (lua_State *L) {
  61. char buff[4 * sizeof(size_t)];
  62. unsigned int h = luai_makeseed();
  63. int p = 0;
  64. addbuff(buff, p, L); /* heap variable */
  65. addbuff(buff, p, &h); /* local variable */
  66. addbuff(buff, p, luaO_nilobject); /* global variable */
  67. addbuff(buff, p, &lua_newstate); /* public function */
  68. lua_assert(p == sizeof(buff));
  69. return luaS_hash(buff, p, h);
  70. }
  71. /*
  72. ** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
  73. ** invariant
  74. */
  75. void luaE_setdebt (global_State *g, l_mem debt) {
  76. g->totalbytes -= (debt - g->GCdebt);
  77. g->GCdebt = debt;
  78. }
  79. CallInfo *luaE_extendCI (lua_State *L) {
  80. CallInfo *ci = luaM_new(L, CallInfo);
  81. lua_assert(L->ci->next == NULL);
  82. L->ci->next = ci;
  83. ci->previous = L->ci;
  84. ci->next = NULL;
  85. return ci;
  86. }
  87. /*
  88. ** free all CallInfo structures not in use by a thread
  89. */
  90. void luaE_freeCI (lua_State *L) {
  91. CallInfo *ci = L->ci;
  92. CallInfo *next = ci->next;
  93. ci->next = NULL;
  94. while ((ci = next) != NULL) {
  95. next = ci->next;
  96. luaM_free(L, ci);
  97. }
  98. }
  99. /*
  100. ** free half of the CallInfo structures not in use by a thread
  101. */
  102. void luaE_shrinkCI (lua_State *L) {
  103. CallInfo *ci = L->ci;
  104. while (ci->next != NULL) { /* while there is 'next' */
  105. CallInfo *next2 = ci->next->next; /* next's next */
  106. if (next2 == NULL) break;
  107. luaM_free(L, ci->next); /* remove next */
  108. ci->next = next2; /* remove 'next' from the list */
  109. next2->previous = ci;
  110. ci = next2;
  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, TValue);
  117. L1->stacksize = BASIC_STACK_SIZE;
  118. for (i = 0; i < BASIC_STACK_SIZE; i++)
  119. setnilvalue(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(L1->top++); /* 'function' entry for this 'ci' */
  128. ci->top = L1->top + LUA_MINSTACK;
  129. L1->ci = ci;
  130. }
  131. static void freestack (lua_State *L) {
  132. if (L->stack == NULL)
  133. return; /* stack not completely built yet */
  134. L->ci = &L->base_ci; /* free the entire 'ci' list */
  135. luaE_freeCI(L);
  136. luaM_freearray(L, L->stack, L->stacksize); /* free stack array */
  137. }
  138. /*
  139. ** Create registry table and its predefined values
  140. */
  141. static void init_registry (lua_State *L, global_State *g) {
  142. TValue temp;
  143. /* create registry */
  144. Table *registry = luaH_new(L);
  145. sethvalue(L, &g->l_registry, registry);
  146. luaH_resize(L, registry, LUA_RIDX_LAST, 0);
  147. /* registry[LUA_RIDX_MAINTHREAD] = L */
  148. setthvalue(L, &temp, L); /* temp = L */
  149. luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &temp);
  150. /* registry[LUA_RIDX_GLOBALS] = table of globals */
  151. sethvalue(L, &temp, luaH_new(L)); /* temp = new table (global table) */
  152. luaH_setint(L, registry, LUA_RIDX_GLOBALS, &temp);
  153. }
  154. /*
  155. ** open parts of the state that may cause memory-allocation errors.
  156. ** ('g->version' != NULL flags that the state was completely build)
  157. */
  158. static void f_luaopen (lua_State *L, void *ud) {
  159. global_State *g = G(L);
  160. UNUSED(ud);
  161. stack_init(L, L); /* init stack */
  162. init_registry(L, g);
  163. luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
  164. luaT_init(L);
  165. luaX_init(L);
  166. /* pre-create memory-error message */
  167. g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
  168. luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
  169. g->gcrunning = 1; /* allow gc */
  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->stacksize = 0;
  182. L->twups = L; /* thread has no upvalues */
  183. L->errorJmp = NULL;
  184. L->nCcalls = 0;
  185. L->hook = NULL;
  186. L->hookmask = 0;
  187. L->basehookcount = 0;
  188. L->allowhook = 1;
  189. resethookcount(L);
  190. L->openupval = NULL;
  191. L->nny = 1;
  192. L->status = LUA_OK;
  193. L->errfunc = 0;
  194. }
  195. static void close_state (lua_State *L) {
  196. global_State *g = G(L);
  197. luaF_close(L, L->stack); /* close all upvalues for this thread */
  198. luaC_freeallobjects(L); /* collect all objects */
  199. if (g->version) /* closing a fully built state? */
  200. luai_userstateclose(L);
  201. luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
  202. luaZ_freebuffer(L, &g->buff);
  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. setthvalue(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->next = NULL;
  252. L->tt = LUA_TTHREAD;
  253. g->currentwhite = bitmask(WHITE0BIT);
  254. L->marked = luaC_white(g);
  255. preinit_thread(L, g);
  256. g->frealloc = f;
  257. g->ud = ud;
  258. g->mainthread = L;
  259. g->seed = makeseed(L);
  260. g->gcrunning = 0; /* no GC while building state */
  261. g->GCestimate = 0;
  262. g->strt.size = g->strt.nuse = 0;
  263. g->strt.hash = NULL;
  264. setnilvalue(&g->l_registry);
  265. luaZ_initbuffer(L, &g->buff);
  266. g->panic = NULL;
  267. g->version = NULL;
  268. g->gcstate = GCSpause;
  269. g->gckind = KGC_NORMAL;
  270. g->allgc = g->finobj = g->tobefnz = g->fixedgc = NULL;
  271. g->sweepgc = NULL;
  272. g->gray = g->grayagain = NULL;
  273. g->weak = g->ephemeron = g->allweak = NULL;
  274. g->twups = NULL;
  275. g->totalbytes = sizeof(LG);
  276. g->GCdebt = 0;
  277. g->gcfinnum = 0;
  278. g->gcpause = LUAI_GCPAUSE;
  279. g->gcstepmul = LUAI_GCMUL;
  280. for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
  281. if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
  282. /* memory allocation error: free partial state */
  283. close_state(L);
  284. L = NULL;
  285. }
  286. return L;
  287. }
  288. LUA_API void lua_close (lua_State *L) {
  289. L = G(L)->mainthread; /* only the main thread can be closed */
  290. lua_lock(L);
  291. close_state(L);
  292. }