2
0

lstate.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. ** $Id: lstate.c,v 2.91 2011/08/23 17:24:34 roberto Exp roberto $
  3. ** Global State
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stddef.h>
  7. #define lstate_c
  8. #define LUA_CORE
  9. #include "lua.h"
  10. #include "lapi.h"
  11. #include "ldebug.h"
  12. #include "ldo.h"
  13. #include "lfunc.h"
  14. #include "lgc.h"
  15. #include "llex.h"
  16. #include "lmem.h"
  17. #include "lstate.h"
  18. #include "lstring.h"
  19. #include "ltable.h"
  20. #include "ltm.h"
  21. #if !defined(LUAI_GCPAUSE)
  22. #define LUAI_GCPAUSE 200 /* 200% */
  23. #endif
  24. #if !defined(LUAI_GCMAJOR)
  25. #define LUAI_GCMAJOR 200 /* 200% */
  26. #endif
  27. #if !defined(LUAI_GCMUL)
  28. #define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */
  29. #endif
  30. #define MEMERRMSG "not enough memory"
  31. /*
  32. ** thread state + extra space
  33. */
  34. typedef struct LX {
  35. #if defined(LUAI_EXTRASPACE)
  36. char buff[LUAI_EXTRASPACE];
  37. #endif
  38. lua_State l;
  39. } LX;
  40. /*
  41. ** Main thread combines a thread state and the global state
  42. */
  43. typedef struct LG {
  44. LX l;
  45. global_State g;
  46. } LG;
  47. #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
  48. /*
  49. ** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
  50. ** invariant
  51. */
  52. void luaE_setdebt (global_State *g, l_mem debt) {
  53. g->totalbytes -= (debt - g->GCdebt);
  54. g->GCdebt = debt;
  55. }
  56. CallInfo *luaE_extendCI (lua_State *L) {
  57. CallInfo *ci = luaM_new(L, CallInfo);
  58. lua_assert(L->ci->next == NULL);
  59. L->ci->next = ci;
  60. ci->previous = L->ci;
  61. ci->next = NULL;
  62. return ci;
  63. }
  64. void luaE_freeCI (lua_State *L) {
  65. CallInfo *ci = L->ci;
  66. CallInfo *next = ci->next;
  67. ci->next = NULL;
  68. while ((ci = next) != NULL) {
  69. next = ci->next;
  70. luaM_free(L, ci);
  71. }
  72. }
  73. static void stack_init (lua_State *L1, lua_State *L) {
  74. int i; CallInfo *ci;
  75. /* initialize stack array */
  76. L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue);
  77. L1->stacksize = BASIC_STACK_SIZE;
  78. for (i = 0; i < BASIC_STACK_SIZE; i++)
  79. setnilvalue(L1->stack + i); /* erase new stack */
  80. L1->top = L1->stack;
  81. L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;
  82. /* initialize first ci */
  83. ci = &L1->base_ci;
  84. ci->next = ci->previous = NULL;
  85. ci->callstatus = 0;
  86. ci->func = L1->top;
  87. setnilvalue(L1->top++); /* 'function' entry for this 'ci' */
  88. ci->top = L1->top + LUA_MINSTACK;
  89. L1->ci = ci;
  90. }
  91. static void freestack (lua_State *L) {
  92. if (L->stack == NULL)
  93. return; /* stack not completely built yet */
  94. L->ci = &L->base_ci; /* free the entire 'ci' list */
  95. luaE_freeCI(L);
  96. luaM_freearray(L, L->stack, L->stacksize); /* free stack array */
  97. }
  98. /*
  99. ** Create registry table and its predefined values
  100. */
  101. static void init_registry (lua_State *L, global_State *g) {
  102. TValue mt;
  103. /* create registry */
  104. Table *registry = luaH_new(L);
  105. sethvalue(L, &g->l_registry, registry);
  106. luaH_resize(L, registry, LUA_RIDX_LAST, 0);
  107. /* registry[LUA_RIDX_MAINTHREAD] = L */
  108. setthvalue(L, &mt, L);
  109. luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &mt);
  110. /* registry[LUA_RIDX_GLOBALS] = table of globals */
  111. sethvalue(L, &mt, luaH_new(L));
  112. luaH_setint(L, registry, LUA_RIDX_GLOBALS, &mt);
  113. }
  114. /*
  115. ** open parts of the state that may cause memory-allocation errors
  116. */
  117. static void f_luaopen (lua_State *L, void *ud) {
  118. global_State *g = G(L);
  119. UNUSED(ud);
  120. stack_init(L, L); /* init stack */
  121. init_registry(L, g);
  122. luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
  123. luaT_init(L);
  124. luaX_init(L);
  125. /* pre-create memory-error message */
  126. g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
  127. luaS_fix(g->memerrmsg); /* it should never be collected */
  128. g->gcrunning = 1; /* allow gc */
  129. }
  130. /*
  131. ** preinitialize a state with consistent values without allocating
  132. ** any memory (to avoid errors)
  133. */
  134. static void preinit_state (lua_State *L, global_State *g) {
  135. G(L) = g;
  136. L->stack = NULL;
  137. L->ci = NULL;
  138. L->stacksize = 0;
  139. L->errorJmp = NULL;
  140. L->nCcalls = 0;
  141. L->hook = NULL;
  142. L->hookmask = 0;
  143. L->basehookcount = 0;
  144. L->allowhook = 1;
  145. resethookcount(L);
  146. L->openupval = NULL;
  147. L->nny = 1;
  148. L->status = LUA_OK;
  149. L->errfunc = 0;
  150. }
  151. static void close_state (lua_State *L) {
  152. global_State *g = G(L);
  153. luaF_close(L, L->stack); /* close all upvalues for this thread */
  154. luaC_freeallobjects(L); /* collect all objects */
  155. luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
  156. luaZ_freebuffer(L, &g->buff);
  157. freestack(L);
  158. lua_assert(gettotalbytes(g) == sizeof(LG));
  159. (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
  160. }
  161. LUA_API lua_State *lua_newthread (lua_State *L) {
  162. lua_State *L1;
  163. lua_lock(L);
  164. luaC_checkGC(L);
  165. L1 = &luaC_newobj(L, LUA_TTHREAD, sizeof(LX), NULL, offsetof(LX, l))->th;
  166. setthvalue(L, L->top, L1);
  167. api_incr_top(L);
  168. preinit_state(L1, G(L));
  169. L1->hookmask = L->hookmask;
  170. L1->basehookcount = L->basehookcount;
  171. L1->hook = L->hook;
  172. resethookcount(L1);
  173. luai_userstatethread(L, L1);
  174. stack_init(L1, L); /* init stack */
  175. lua_unlock(L);
  176. return L1;
  177. }
  178. void luaE_freethread (lua_State *L, lua_State *L1) {
  179. LX *l = fromstate(L1);
  180. luaF_close(L1, L1->stack); /* close all upvalues for this thread */
  181. lua_assert(L1->openupval == NULL);
  182. luai_userstatefree(L, L1);
  183. freestack(L1);
  184. luaM_free(L, l);
  185. }
  186. LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
  187. int i;
  188. lua_State *L;
  189. global_State *g;
  190. LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
  191. if (l == NULL) return NULL;
  192. L = &l->l.l;
  193. g = &l->g;
  194. L->next = NULL;
  195. L->tt = LUA_TTHREAD;
  196. g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);
  197. L->marked = luaC_white(g);
  198. g->gckind = KGC_NORMAL;
  199. preinit_state(L, g);
  200. g->frealloc = f;
  201. g->ud = ud;
  202. g->mainthread = L;
  203. g->uvhead.u.l.prev = &g->uvhead;
  204. g->uvhead.u.l.next = &g->uvhead;
  205. g->gcrunning = 0; /* no GC while building state */
  206. g->lastmajormem = 0;
  207. g->strt.size = 0;
  208. g->strt.nuse = 0;
  209. g->strt.hash = NULL;
  210. setnilvalue(&g->l_registry);
  211. luaZ_initbuffer(L, &g->buff);
  212. g->panic = NULL;
  213. g->version = lua_version(NULL);
  214. g->gcstate = GCSpause;
  215. g->allgc = NULL;
  216. g->finobj = NULL;
  217. g->tobefnz = NULL;
  218. g->gray = g->grayagain = NULL;
  219. g->weak = g->ephemeron = g->allweak = NULL;
  220. g->totalbytes = sizeof(LG);
  221. g->GCdebt = 0;
  222. g->gcpause = LUAI_GCPAUSE;
  223. g->gcmajorinc = LUAI_GCMAJOR;
  224. g->gcstepmul = LUAI_GCMUL;
  225. for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
  226. if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
  227. /* memory allocation error: free partial state */
  228. close_state(L);
  229. L = NULL;
  230. }
  231. else
  232. luai_userstateopen(L);
  233. return L;
  234. }
  235. LUA_API void lua_close (lua_State *L) {
  236. L = G(L)->mainthread; /* only the main thread can be closed */
  237. lua_lock(L);
  238. luai_userstateclose(L);
  239. close_state(L);
  240. }