lstate.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. ** $Id: lstate.c,v 2.76 2010/03/29 17:43:14 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 162 /* 162% */
  23. #endif
  24. #if !defined(LUAI_GCMUL)
  25. #define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */
  26. #endif
  27. /*
  28. ** thread state + extra space
  29. */
  30. typedef struct LX {
  31. #if defined(LUAI_EXTRASPACE)
  32. char buff[LUAI_EXTRASPACE];
  33. #endif
  34. lua_State l;
  35. } LX;
  36. /*
  37. ** Main thread combines a thread state and the global state
  38. */
  39. typedef struct LG {
  40. LX l;
  41. global_State g;
  42. } LG;
  43. #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
  44. /*
  45. ** maximum number of nested calls made by error-handling function
  46. */
  47. #define LUAI_EXTRACALLS 10
  48. CallInfo *luaE_extendCI (lua_State *L) {
  49. CallInfo *ci = luaM_new(L, CallInfo);
  50. lua_assert(L->ci->next == NULL);
  51. L->ci->next = ci;
  52. ci->previous = L->ci;
  53. ci->next = NULL;
  54. return ci;
  55. }
  56. void luaE_freeCI (lua_State *L) {
  57. CallInfo *ci = L->ci;
  58. CallInfo *next = ci->next;
  59. ci->next = NULL;
  60. while ((ci = next) != NULL) {
  61. next = ci->next;
  62. luaM_free(L, ci);
  63. }
  64. }
  65. static void stack_init (lua_State *L1, lua_State *L) {
  66. int i;
  67. /* initialize stack array */
  68. L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue);
  69. L1->stacksize = BASIC_STACK_SIZE;
  70. for (i = 0; i < BASIC_STACK_SIZE; i++)
  71. setnilvalue(L1->stack + i); /* erase new stack */
  72. L1->top = L1->stack;
  73. L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;
  74. /* initialize first ci */
  75. L1->ci->func = L1->top;
  76. setnilvalue(L1->top++); /* 'function' entry for this 'ci' */
  77. L1->ci->top = L1->top + LUA_MINSTACK;
  78. L1->ci->callstatus = 0;
  79. }
  80. static void freestack (lua_State *L) {
  81. L->ci = &L->base_ci; /* reset 'ci' list */
  82. luaE_freeCI(L);
  83. luaM_freearray(L, L->stack, L->stacksize);
  84. }
  85. /*
  86. ** Calls the function in variable pointed to by userdata in first argument
  87. ** (Userdata cannot point directly to the function because pointer to
  88. ** function is not compatible with void*.)
  89. */
  90. static int ccall (lua_State *L) {
  91. lua_CFunction f = *(lua_CFunction *)lua_touserdata(L, 1);
  92. lua_remove(L, 1); /* remove f from stack */
  93. return f(L);
  94. }
  95. /*
  96. ** Create registry table and its predefined values
  97. */
  98. static void init_registry (lua_State *L, global_State *g) {
  99. Closure *cp;
  100. TValue mt;
  101. /* create registry */
  102. Table *registry = luaH_new(L);
  103. sethvalue(L, &g->l_registry, registry);
  104. luaH_resize(L, registry, LUA_RIDX_LAST, 0);
  105. /* registry[LUA_RIDX_MAINTHREAD] = L */
  106. setthvalue(L, &mt, L);
  107. setobj2t(L, luaH_setint(L, registry, LUA_RIDX_MAINTHREAD), &mt);
  108. /* registry[LUA_RIDX_CCALL] = ccall */
  109. cp = luaF_newCclosure(L, 0);
  110. cp->c.f = ccall;
  111. setclvalue(L, &mt, cp);
  112. setobj2t(L, luaH_setint(L, registry, LUA_RIDX_CCALL), &mt);
  113. /* registry[LUA_RIDX_GLOBALS] = table of globals */
  114. sethvalue(L, &mt, luaH_new(L));
  115. setobj2t(L, luaH_setint(L, registry, LUA_RIDX_GLOBALS), &mt);
  116. }
  117. /*
  118. ** open parts of the state that may cause memory-allocation errors
  119. */
  120. static void f_luaopen (lua_State *L, void *ud) {
  121. global_State *g = G(L);
  122. UNUSED(ud);
  123. stack_init(L, L); /* init stack */
  124. init_registry(L, g);
  125. luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
  126. luaT_init(L);
  127. luaX_init(L);
  128. luaS_fix(luaS_newliteral(L, MEMERRMSG));
  129. g->GCthreshold = 4*g->totalbytes;
  130. }
  131. /*
  132. ** preinitialize a state with consistent values without allocating
  133. ** any memory (to avoid errors)
  134. */
  135. static void preinit_state (lua_State *L, global_State *g) {
  136. G(L) = g;
  137. L->stack = NULL;
  138. L->stacksize = 0;
  139. L->errorJmp = NULL;
  140. L->hook = NULL;
  141. L->hookmask = 0;
  142. L->basehookcount = 0;
  143. L->allowhook = 1;
  144. resethookcount(L);
  145. L->openupval = NULL;
  146. L->nny = 1;
  147. L->status = LUA_OK;
  148. L->base_ci.next = L->base_ci.previous = NULL;
  149. L->ci = &L->base_ci;
  150. L->errfunc = 0;
  151. }
  152. static void close_state (lua_State *L) {
  153. global_State *g = G(L);
  154. luaF_close(L, L->stack); /* close all upvalues for this thread */
  155. luaC_freeallobjects(L); /* collect all objects */
  156. luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
  157. luaZ_freebuffer(L, &g->buff);
  158. freestack(L);
  159. lua_assert(g->totalbytes == sizeof(LG));
  160. (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0);
  161. }
  162. LUA_API lua_State *lua_newthread (lua_State *L) {
  163. lua_State *L1;
  164. lua_lock(L);
  165. luaC_checkGC(L);
  166. L1 = &luaC_newobj(L, LUA_TTHREAD, sizeof(LX), NULL, offsetof(LX, l))->th;
  167. setthvalue(L, L->top, L1);
  168. api_incr_top(L);
  169. preinit_state(L1, G(L));
  170. stack_init(L1, L); /* init stack */
  171. L1->hookmask = L->hookmask;
  172. L1->basehookcount = L->basehookcount;
  173. L1->hook = L->hook;
  174. resethookcount(L1);
  175. lua_assert(iswhite(obj2gco(L1)));
  176. lua_unlock(L);
  177. luai_userstatethread(L, L1);
  178. return L1;
  179. }
  180. void luaE_freethread (lua_State *L, lua_State *L1) {
  181. LX *l = fromstate(L1);
  182. luaF_close(L1, L1->stack); /* close all upvalues for this thread */
  183. lua_assert(L1->openupval == NULL);
  184. luai_userstatefree(L1);
  185. freestack(L1);
  186. luaM_free(L, l);
  187. }
  188. LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
  189. int i;
  190. lua_State *L;
  191. global_State *g;
  192. LG *l = cast(LG *, (*f)(ud, NULL, 0, sizeof(LG)));
  193. if (l == NULL) return NULL;
  194. L = &l->l.l;
  195. g = &l->g;
  196. L->next = NULL;
  197. L->tt = LUA_TTHREAD;
  198. g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);
  199. L->marked = luaC_white(g);
  200. g->gckind = KGC_NORMAL;
  201. g->nCcalls = 0;
  202. preinit_state(L, g);
  203. g->frealloc = f;
  204. g->ud = ud;
  205. g->mainthread = L;
  206. g->uvhead.u.l.prev = &g->uvhead;
  207. g->uvhead.u.l.next = &g->uvhead;
  208. g->GCthreshold = MAX_LUMEM; /* no GC while building state */
  209. g->lastmajormem = 0;
  210. g->strt.size = 0;
  211. g->strt.nuse = 0;
  212. g->strt.hash = NULL;
  213. setnilvalue(&g->l_registry);
  214. luaZ_initbuffer(L, &g->buff);
  215. g->panic = NULL;
  216. g->version = lua_version(NULL);
  217. g->gcstate = GCSpause;
  218. g->allgc = NULL;
  219. g->udgc = NULL;
  220. g->tobefnz = NULL;
  221. g->totalbytes = sizeof(LG);
  222. g->gcpause = LUAI_GCPAUSE;
  223. g->gcstepmul = LUAI_GCMUL;
  224. for (i=0; i<NUM_TAGS; i++) g->mt[i] = NULL;
  225. if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
  226. /* memory allocation error: free partial state */
  227. close_state(L);
  228. L = NULL;
  229. }
  230. else
  231. luai_userstateopen(L);
  232. return L;
  233. }
  234. LUA_API void lua_close (lua_State *L) {
  235. L = G(L)->mainthread; /* only the main thread can be closed */
  236. lua_lock(L);
  237. luaF_close(L, L->stack); /* close all upvalues for this thread */
  238. luaC_separateudata(L, 1); /* separate all udata with GC metamethods */
  239. lua_assert(L->next == NULL);
  240. luai_userstateclose(L);
  241. close_state(L);
  242. }