lstate.c 6.5 KB

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