lstate.c 6.8 KB

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