lstate.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. ** $Id: lstate.c,v 2.99.1.1 2013/04/12 18:48:47 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_GCMAJOR)
  26. #define LUAI_GCMAJOR 200 /* 200% */
  27. #endif
  28. #if !defined(LUAI_GCMUL)
  29. #define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */
  30. #endif
  31. #define MEMERRMSG "not enough memory"
  32. /*
  33. ** a macro to help the creation of a unique random seed when a state is
  34. ** created; the seed is used to randomize hashes.
  35. */
  36. #if !defined(luai_makeseed)
  37. #include <time.h>
  38. #define luai_makeseed() cast(unsigned int, time(NULL))
  39. #endif
  40. /*
  41. ** thread state + extra space
  42. */
  43. typedef struct LX {
  44. #if defined(LUAI_EXTRASPACE)
  45. char buff[LUAI_EXTRASPACE];
  46. #endif
  47. lua_State l;
  48. } LX;
  49. /*
  50. ** Main thread combines a thread state and the global state
  51. */
  52. typedef struct LG {
  53. LX l;
  54. global_State g;
  55. } LG;
  56. #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
  57. /*
  58. ** Compute an initial seed as random as possible. In ANSI, rely on
  59. ** Address Space Layout Randomization (if present) to increase
  60. ** randomness..
  61. */
  62. #define addbuff(b,p,e) \
  63. { size_t t = cast(size_t, e); \
  64. memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); }
  65. static unsigned int makeseed (lua_State *L) {
  66. char buff[4 * sizeof(size_t)];
  67. unsigned int h = luai_makeseed();
  68. int p = 0;
  69. addbuff(buff, p, L); /* heap variable */
  70. addbuff(buff, p, &h); /* local variable */
  71. addbuff(buff, p, luaO_nilobject); /* global variable */
  72. addbuff(buff, p, &lua_newstate); /* public function */
  73. lua_assert(p == sizeof(buff));
  74. return luaS_hash(buff, p, h);
  75. }
  76. /*
  77. ** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
  78. ** invariant
  79. */
  80. void luaE_setdebt (global_State *g, l_mem debt) {
  81. g->totalbytes -= (debt - g->GCdebt);
  82. g->GCdebt = debt;
  83. }
  84. CallInfo *luaE_extendCI (lua_State *L) {
  85. CallInfo *ci = luaM_new(L, CallInfo);
  86. lua_assert(L->ci->next == NULL);
  87. L->ci->next = ci;
  88. ci->previous = L->ci;
  89. ci->next = NULL;
  90. return ci;
  91. }
  92. void luaE_freeCI (lua_State *L) {
  93. CallInfo *ci = L->ci;
  94. CallInfo *next = ci->next;
  95. ci->next = NULL;
  96. while ((ci = next) != NULL) {
  97. next = ci->next;
  98. luaM_free(L, ci);
  99. }
  100. }
  101. static void stack_init (lua_State *L1, lua_State *L) {
  102. int i; CallInfo *ci;
  103. /* initialize stack array */
  104. L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue);
  105. L1->stacksize = BASIC_STACK_SIZE;
  106. for (i = 0; i < BASIC_STACK_SIZE; i++)
  107. setnilvalue(L1->stack + i); /* erase new stack */
  108. L1->top = L1->stack;
  109. L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;
  110. /* initialize first ci */
  111. ci = &L1->base_ci;
  112. ci->next = ci->previous = NULL;
  113. ci->callstatus = 0;
  114. ci->func = L1->top;
  115. setnilvalue(L1->top++); /* 'function' entry for this 'ci' */
  116. ci->top = L1->top + LUA_MINSTACK;
  117. L1->ci = ci;
  118. }
  119. static void freestack (lua_State *L) {
  120. if (L->stack == NULL)
  121. return; /* stack not completely built yet */
  122. L->ci = &L->base_ci; /* free the entire 'ci' list */
  123. luaE_freeCI(L);
  124. luaM_freearray(L, L->stack, L->stacksize); /* free stack array */
  125. }
  126. /*
  127. ** Create registry table and its predefined values
  128. */
  129. static void init_registry (lua_State *L, global_State *g) {
  130. TValue mt;
  131. /* create registry */
  132. Table *registry = luaH_new(L);
  133. sethvalue(L, &g->l_registry, registry);
  134. luaH_resize(L, registry, LUA_RIDX_LAST, 0);
  135. /* registry[LUA_RIDX_MAINTHREAD] = L */
  136. setthvalue(L, &mt, L);
  137. luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &mt);
  138. /* registry[LUA_RIDX_GLOBALS] = table of globals */
  139. sethvalue(L, &mt, luaH_new(L));
  140. luaH_setint(L, registry, LUA_RIDX_GLOBALS, &mt);
  141. }
  142. /*
  143. ** open parts of the state that may cause memory-allocation errors
  144. */
  145. static void f_luaopen (lua_State *L, void *ud) {
  146. global_State *g = G(L);
  147. UNUSED(ud);
  148. stack_init(L, L); /* init stack */
  149. init_registry(L, g);
  150. luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
  151. luaT_init(L);
  152. luaX_init(L);
  153. /* pre-create memory-error message */
  154. g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
  155. luaS_fix(g->memerrmsg); /* it should never be collected */
  156. g->gcrunning = 1; /* allow gc */
  157. g->version = lua_version(NULL);
  158. luai_userstateopen(L);
  159. }
  160. /*
  161. ** preinitialize a state with consistent values without allocating
  162. ** any memory (to avoid errors)
  163. */
  164. static void preinit_state (lua_State *L, global_State *g) {
  165. G(L) = g;
  166. L->stack = NULL;
  167. L->ci = NULL;
  168. L->stacksize = 0;
  169. L->errorJmp = NULL;
  170. L->nCcalls = 0;
  171. L->hook = NULL;
  172. L->hookmask = 0;
  173. L->basehookcount = 0;
  174. L->allowhook = 1;
  175. resethookcount(L);
  176. L->openupval = NULL;
  177. L->nny = 1;
  178. L->status = LUA_OK;
  179. L->errfunc = 0;
  180. }
  181. static void close_state (lua_State *L) {
  182. global_State *g = G(L);
  183. luaF_close(L, L->stack); /* close all upvalues for this thread */
  184. luaC_freeallobjects(L); /* collect all objects */
  185. if (g->version) /* closing a fully built state? */
  186. luai_userstateclose(L);
  187. luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
  188. luaZ_freebuffer(L, &g->buff);
  189. freestack(L);
  190. lua_assert(gettotalbytes(g) == sizeof(LG));
  191. (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
  192. }
  193. LUA_API lua_State *lua_newthread (lua_State *L) {
  194. lua_State *L1;
  195. lua_lock(L);
  196. luaC_checkGC(L);
  197. L1 = &luaC_newobj(L, LUA_TTHREAD, sizeof(LX), NULL, offsetof(LX, l))->th;
  198. setthvalue(L, L->top, L1);
  199. api_incr_top(L);
  200. preinit_state(L1, G(L));
  201. L1->hookmask = L->hookmask;
  202. L1->basehookcount = L->basehookcount;
  203. L1->hook = L->hook;
  204. resethookcount(L1);
  205. luai_userstatethread(L, L1);
  206. stack_init(L1, L); /* init stack */
  207. lua_unlock(L);
  208. return L1;
  209. }
  210. void luaE_freethread (lua_State *L, lua_State *L1) {
  211. LX *l = fromstate(L1);
  212. luaF_close(L1, L1->stack); /* close all upvalues for this thread */
  213. lua_assert(L1->openupval == NULL);
  214. luai_userstatefree(L, L1);
  215. freestack(L1);
  216. luaM_free(L, l);
  217. }
  218. LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
  219. int i;
  220. lua_State *L;
  221. global_State *g;
  222. LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
  223. if (l == NULL) return NULL;
  224. L = &l->l.l;
  225. g = &l->g;
  226. L->next = NULL;
  227. L->tt = LUA_TTHREAD;
  228. g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);
  229. L->marked = luaC_white(g);
  230. g->gckind = KGC_NORMAL;
  231. preinit_state(L, g);
  232. g->frealloc = f;
  233. g->ud = ud;
  234. g->mainthread = L;
  235. g->seed = makeseed(L);
  236. g->uvhead.u.l.prev = &g->uvhead;
  237. g->uvhead.u.l.next = &g->uvhead;
  238. g->gcrunning = 0; /* no GC while building state */
  239. g->GCestimate = 0;
  240. g->strt.size = 0;
  241. 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 = NULL;
  247. g->gcstate = GCSpause;
  248. g->allgc = NULL;
  249. g->finobj = NULL;
  250. g->tobefnz = NULL;
  251. g->sweepgc = g->sweepfin = 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->gcmajorinc = LUAI_GCMAJOR;
  258. g->gcstepmul = LUAI_GCMUL;
  259. for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
  260. if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
  261. /* memory allocation error: free partial state */
  262. close_state(L);
  263. L = NULL;
  264. }
  265. return L;
  266. }
  267. LUA_API void lua_close (lua_State *L) {
  268. L = G(L)->mainthread; /* only the main thread can be closed */
  269. lua_lock(L);
  270. close_state(L);
  271. }