lstate.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. ** $Id: lstate.c,v 2.100 2013/08/05 16:58:28 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. #if defined(LUAI_EXTRASPACE)
  42. char buff[LUAI_EXTRASPACE];
  43. #endif
  44. lua_State l;
  45. } LX;
  46. /*
  47. ** Main thread combines a thread state and the global state
  48. */
  49. typedef struct LG {
  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. In ANSI, rely on
  56. ** Address Space Layout Randomization (if present) to increase
  57. ** randomness..
  58. */
  59. #define addbuff(b,p,e) \
  60. { size_t t = cast(size_t, e); \
  61. memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); }
  62. static unsigned int makeseed (lua_State *L) {
  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
  76. */
  77. void luaE_setdebt (global_State *g, l_mem debt) {
  78. g->totalbytes -= (debt - g->GCdebt);
  79. g->GCdebt = debt;
  80. }
  81. CallInfo *luaE_extendCI (lua_State *L) {
  82. CallInfo *ci = luaM_new(L, CallInfo);
  83. lua_assert(L->ci->next == NULL);
  84. L->ci->next = ci;
  85. ci->previous = L->ci;
  86. ci->next = NULL;
  87. return ci;
  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. static void stack_init (lua_State *L1, lua_State *L) {
  99. int i; CallInfo *ci;
  100. /* initialize stack array */
  101. L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue);
  102. L1->stacksize = BASIC_STACK_SIZE;
  103. for (i = 0; i < BASIC_STACK_SIZE; i++)
  104. setnilvalue(L1->stack + i); /* erase new stack */
  105. L1->top = L1->stack;
  106. L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;
  107. /* initialize first ci */
  108. ci = &L1->base_ci;
  109. ci->next = ci->previous = NULL;
  110. ci->callstatus = 0;
  111. ci->func = L1->top;
  112. setnilvalue(L1->top++); /* 'function' entry for this 'ci' */
  113. ci->top = L1->top + LUA_MINSTACK;
  114. L1->ci = ci;
  115. }
  116. static void freestack (lua_State *L) {
  117. if (L->stack == NULL)
  118. return; /* stack not completely built yet */
  119. L->ci = &L->base_ci; /* free the entire 'ci' list */
  120. luaE_freeCI(L);
  121. luaM_freearray(L, L->stack, L->stacksize); /* free stack array */
  122. }
  123. /*
  124. ** Create registry table and its predefined values
  125. */
  126. static void init_registry (lua_State *L, global_State *g) {
  127. TValue mt;
  128. /* create registry */
  129. Table *registry = luaH_new(L);
  130. sethvalue(L, &g->l_registry, registry);
  131. luaH_resize(L, registry, LUA_RIDX_LAST, 0);
  132. /* registry[LUA_RIDX_MAINTHREAD] = L */
  133. setthvalue(L, &mt, L);
  134. luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &mt);
  135. /* registry[LUA_RIDX_GLOBALS] = table of globals */
  136. sethvalue(L, &mt, luaH_new(L));
  137. luaH_setint(L, registry, LUA_RIDX_GLOBALS, &mt);
  138. }
  139. /*
  140. ** open parts of the state that may cause memory-allocation errors
  141. */
  142. static void f_luaopen (lua_State *L, void *ud) {
  143. global_State *g = G(L);
  144. UNUSED(ud);
  145. stack_init(L, L); /* init stack */
  146. init_registry(L, g);
  147. luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
  148. luaT_init(L);
  149. luaX_init(L);
  150. /* pre-create memory-error message */
  151. g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
  152. luaS_fix(g->memerrmsg); /* it should never be collected */
  153. g->gcrunning = 1; /* allow gc */
  154. }
  155. /*
  156. ** preinitialize a state with consistent values without allocating
  157. ** any memory (to avoid errors)
  158. */
  159. static void preinit_state (lua_State *L, global_State *g) {
  160. G(L) = g;
  161. L->stack = NULL;
  162. L->ci = NULL;
  163. L->stacksize = 0;
  164. L->errorJmp = NULL;
  165. L->nCcalls = 0;
  166. L->hook = NULL;
  167. L->hookmask = 0;
  168. L->basehookcount = 0;
  169. L->allowhook = 1;
  170. resethookcount(L);
  171. L->openupval = NULL;
  172. L->nny = 1;
  173. L->status = LUA_OK;
  174. L->errfunc = 0;
  175. }
  176. static void close_state (lua_State *L) {
  177. global_State *g = G(L);
  178. luaF_close(L, L->stack); /* close all upvalues for this thread */
  179. luaC_freeallobjects(L); /* collect all objects */
  180. luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
  181. luaZ_freebuffer(L, &g->buff);
  182. freestack(L);
  183. lua_assert(gettotalbytes(g) == sizeof(LG));
  184. (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
  185. }
  186. LUA_API lua_State *lua_newthread (lua_State *L) {
  187. lua_State *L1;
  188. lua_lock(L);
  189. luaC_checkGC(L);
  190. /* create new thread, linked after 'l_registry' */
  191. L1 = &luaC_newobj(L, LUA_TTHREAD, sizeof(LX),
  192. &hvalue(&G(L)->l_registry)->next, offsetof(LX, l))->th;
  193. setthvalue(L, L->top, L1);
  194. api_incr_top(L);
  195. preinit_state(L1, G(L));
  196. L1->hookmask = L->hookmask;
  197. L1->basehookcount = L->basehookcount;
  198. L1->hook = L->hook;
  199. resethookcount(L1);
  200. luai_userstatethread(L, L1);
  201. stack_init(L1, L); /* init stack */
  202. lua_unlock(L);
  203. return L1;
  204. }
  205. void luaE_freethread (lua_State *L, lua_State *L1) {
  206. LX *l = fromstate(L1);
  207. luaF_close(L1, L1->stack); /* close all upvalues for this thread */
  208. lua_assert(L1->openupval == NULL);
  209. luai_userstatefree(L, L1);
  210. freestack(L1);
  211. luaM_free(L, l);
  212. }
  213. LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
  214. int i;
  215. lua_State *L;
  216. global_State *g;
  217. LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
  218. if (l == NULL) return NULL;
  219. L = &l->l.l;
  220. g = &l->g;
  221. L->next = NULL;
  222. L->tt = LUA_TTHREAD;
  223. g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);
  224. L->marked = luaC_white(g);
  225. g->gckind = KGC_NORMAL;
  226. preinit_state(L, g);
  227. g->frealloc = f;
  228. g->ud = ud;
  229. g->mainthread = L;
  230. g->seed = makeseed(L);
  231. g->gcrunning = 0; /* no GC while building state */
  232. g->GCestimate = 0;
  233. g->strt.size = 0;
  234. g->strt.nuse = 0;
  235. g->strt.hash = NULL;
  236. setnilvalue(&g->l_registry);
  237. luaZ_initbuffer(L, &g->buff);
  238. g->panic = NULL;
  239. g->version = lua_version(NULL);
  240. g->gcstate = GCSpause;
  241. g->allgc = NULL;
  242. g->finobj = NULL;
  243. g->tobefnz = NULL;
  244. g->sweepgc = g->sweepfin = NULL;
  245. g->gray = g->grayagain = NULL;
  246. g->weak = g->ephemeron = g->allweak = NULL;
  247. g->totalbytes = sizeof(LG);
  248. g->GCdebt = 0;
  249. g->gcpause = LUAI_GCPAUSE;
  250. g->gcstepmul = LUAI_GCMUL;
  251. for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
  252. if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
  253. /* memory allocation error: free partial state */
  254. close_state(L);
  255. L = NULL;
  256. }
  257. else
  258. luai_userstateopen(L);
  259. return L;
  260. }
  261. LUA_API void lua_close (lua_State *L) {
  262. L = G(L)->mainthread; /* only the main thread can be closed */
  263. lua_lock(L);
  264. luai_userstateclose(L);
  265. close_state(L);
  266. }