lstate.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. ** $Id: lstate.c,v 2.112 2013/09/11 12:26:14 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 temp;
  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. nolocal(obj2gco(registry));
  133. /* registry[LUA_RIDX_MAINTHREAD] = L */
  134. setthvalue(L, &temp, L); /* temp = L */
  135. luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &temp);
  136. /* registry[LUA_RIDX_GLOBALS] = table of globals */
  137. sethvalue(L, &temp, luaH_new(L)); /* temp = new table (global table) */
  138. luaH_setint(L, registry, LUA_RIDX_GLOBALS, &temp);
  139. valnolocal(&temp); /* keep local invariant */
  140. }
  141. /*
  142. ** open parts of the state that may cause memory-allocation errors
  143. */
  144. static void f_luaopen (lua_State *L, void *ud) {
  145. global_State *g = G(L);
  146. UNUSED(ud);
  147. stack_init(L, L); /* init stack */
  148. init_registry(L, g);
  149. luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
  150. luaT_init(L);
  151. luaX_init(L);
  152. /* pre-create memory-error message */
  153. g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
  154. luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
  155. g->gcrunning = 1; /* allow gc */
  156. }
  157. /*
  158. ** preinitialize a state with consistent values without allocating
  159. ** any memory (to avoid errors)
  160. */
  161. static void preinit_state (lua_State *L, global_State *g) {
  162. G(L) = g;
  163. L->stack = NULL;
  164. L->ci = NULL;
  165. L->stacksize = 0;
  166. L->errorJmp = NULL;
  167. L->nCcalls = 0;
  168. L->hook = NULL;
  169. L->hookmask = 0;
  170. L->basehookcount = 0;
  171. L->allowhook = 1;
  172. resethookcount(L);
  173. L->openupval = NULL;
  174. L->nny = 1;
  175. L->status = LUA_OK;
  176. L->errfunc = 0;
  177. }
  178. static void close_state (lua_State *L) {
  179. global_State *g = G(L);
  180. luaF_close(L, L->stack); /* close all upvalues for this thread */
  181. luaC_freeallobjects(L); /* collect all objects */
  182. luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
  183. luaZ_freebuffer(L, &g->buff);
  184. freestack(L);
  185. lua_assert(gettotalbytes(g) == sizeof(LG));
  186. (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
  187. }
  188. LUA_API lua_State *lua_newthread (lua_State *L) {
  189. global_State *g = G(L);
  190. lua_State *L1;
  191. lua_lock(L);
  192. luaC_checkGC(L);
  193. /* create new thread */
  194. L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l;
  195. L1->marked = luaC_white(g) | bit2mask(NOLOCALBIT, LOCALMARK);
  196. L1->tt = LUA_TTHREAD;
  197. /* link it on list of threads */
  198. L1->next = g->mainthread->next;
  199. g->mainthread->next = obj2gco(L1);
  200. setthvalue(L, L->top, L1);
  201. api_incr_top(L);
  202. preinit_state(L1, g);
  203. L1->hookmask = L->hookmask;
  204. L1->basehookcount = L->basehookcount;
  205. L1->hook = L->hook;
  206. resethookcount(L1);
  207. luai_userstatethread(L, L1);
  208. stack_init(L1, L); /* init stack */
  209. lua_unlock(L);
  210. return L1;
  211. }
  212. void luaE_freethread (lua_State *L, lua_State *L1) {
  213. LX *l = fromstate(L1);
  214. luaF_close(L1, L1->stack); /* close all upvalues for this thread */
  215. lua_assert(L1->openupval == NULL);
  216. luai_userstatefree(L, L1);
  217. freestack(L1);
  218. luaM_free(L, l);
  219. }
  220. LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
  221. int i;
  222. lua_State *L;
  223. global_State *g;
  224. LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
  225. if (l == NULL) return NULL;
  226. L = &l->l.l;
  227. g = &l->g;
  228. L->next = NULL;
  229. L->tt = LUA_TTHREAD;
  230. g->currentwhite = bitmask(WHITE0BIT);
  231. L->marked = luaC_white(g) | bit2mask(NOLOCALBIT, LOCALMARK);
  232. g->gckind = KGC_NORMAL;
  233. preinit_state(L, g);
  234. g->frealloc = f;
  235. g->ud = ud;
  236. g->mainthread = L;
  237. g->seed = makeseed(L);
  238. g->gcrunning = 0; /* no GC while building state */
  239. g->GCestimate = 0;
  240. g->GCthreshold = 10000;
  241. g->strt.size = g->strt.nuse = 0;
  242. g->strt.hash = NULL;
  243. setnilvalue(&g->l_registry);
  244. luaZ_initbuffer(L, &g->buff);
  245. g->panic = NULL;
  246. g->version = lua_version(NULL);
  247. g->gcstate = GCSpause;
  248. g->localgc = g->localfin = g->allgc = g->finobj = NULL;
  249. g->tobefnz = NULL;
  250. g->fixedgc = NULL;
  251. g->sweepgc = NULL;
  252. g->gray = g->grayagain = NULL;
  253. g->weak = g->ephemeron = g->allweak = NULL;
  254. g->totalbytes = sizeof(LG);
  255. g->GCdebt = 0;
  256. g->gcpause = LUAI_GCPAUSE;
  257. g->gcstepmul = LUAI_GCMUL;
  258. for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
  259. if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
  260. /* memory allocation error: free partial state */
  261. close_state(L);
  262. L = NULL;
  263. }
  264. else
  265. luai_userstateopen(L);
  266. return L;
  267. }
  268. LUA_API void lua_close (lua_State *L) {
  269. L = G(L)->mainthread; /* only the main thread can be closed */
  270. lua_lock(L);
  271. luai_userstateclose(L);
  272. close_state(L);
  273. }