lstate.c 6.4 KB

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