lstate.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. ** $Id: lstate.c,v 2.41 2007/10/29 16:51:20 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. #define state_size(x) (sizeof(x) + LUAI_EXTRASPACE)
  22. #define fromstate(l) (cast(lu_byte *, (l)) - LUAI_EXTRASPACE)
  23. #define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE))
  24. /*
  25. ** Main thread combines a thread state and the global state
  26. */
  27. typedef struct LG {
  28. lua_State l;
  29. global_State g;
  30. } LG;
  31. static void stack_init (lua_State *L1, lua_State *L) {
  32. /* initialize CallInfo array */
  33. L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo);
  34. L1->ci = L1->base_ci;
  35. L1->size_ci = BASIC_CI_SIZE;
  36. L1->end_ci = L1->base_ci + L1->size_ci - 1;
  37. /* initialize stack array */
  38. L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TValue);
  39. L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK;
  40. L1->top = L1->stack;
  41. L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1;
  42. /* initialize first ci */
  43. L1->ci->func = L1->top;
  44. setnilvalue(L1->top++); /* `function' entry for this `ci' */
  45. L1->base = L1->ci->base = L1->top;
  46. L1->ci->top = L1->top + LUA_MINSTACK;
  47. }
  48. static void freestack (lua_State *L, lua_State *L1) {
  49. luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo);
  50. luaM_freearray(L, L1->stack, L1->stacksize, TValue);
  51. }
  52. /*
  53. ** open parts that may cause memory-allocation errors
  54. */
  55. static void f_luaopen (lua_State *L, void *ud) {
  56. global_State *g = G(L);
  57. UNUSED(ud);
  58. stack_init(L, L); /* init stack */
  59. sethvalue(L, gt(L), luaH_new(L)); /* table of globals */
  60. sethvalue(L, registry(L), luaH_new(L)); /* registry */
  61. luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
  62. luaT_init(L);
  63. luaX_init(L);
  64. luaS_fix(luaS_newliteral(L, MEMERRMSG));
  65. g->GCthreshold = 4*g->totalbytes;
  66. }
  67. static void preinit_state (lua_State *L, global_State *g) {
  68. G(L) = g;
  69. L->stack = NULL;
  70. L->stacksize = 0;
  71. L->errorJmp = NULL;
  72. L->hook = NULL;
  73. L->hookmask = 0;
  74. L->basehookcount = 0;
  75. L->allowhook = 1;
  76. resethookcount(L);
  77. L->openupval = NULL;
  78. L->size_ci = 0;
  79. L->baseCcalls = 0;
  80. L->status = LUA_OK;
  81. L->base_ci = L->ci = NULL;
  82. L->savedpc = NULL;
  83. L->errfunc = 0;
  84. setnilvalue(gt(L));
  85. }
  86. static void close_state (lua_State *L) {
  87. global_State *g = G(L);
  88. luaF_close(L, L->stack); /* close all upvalues for this thread */
  89. luaC_freeall(L); /* collect all objects */
  90. lua_assert(g->rootgc == obj2gco(L));
  91. lua_assert(g->strt.nuse == 0);
  92. luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *);
  93. luaZ_freebuffer(L, &g->buff);
  94. freestack(L, L);
  95. lua_assert(g->totalbytes == sizeof(LG));
  96. (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0);
  97. }
  98. LUA_API lua_State *lua_newthread (lua_State *L) {
  99. lua_State *L1;
  100. lua_lock(L);
  101. luaC_checkGC(L);
  102. L1 = tostate(luaM_malloc(L, state_size(lua_State)));
  103. luaC_link(L, obj2gco(L1), LUA_TTHREAD);
  104. setthvalue(L, L->top, L1);
  105. api_incr_top(L);
  106. preinit_state(L1, G(L));
  107. stack_init(L1, L); /* init stack */
  108. setobj2n(L, gt(L1), gt(L)); /* share table of globals */
  109. L1->hookmask = L->hookmask;
  110. L1->basehookcount = L->basehookcount;
  111. L1->hook = L->hook;
  112. resethookcount(L1);
  113. lua_assert(iswhite(obj2gco(L1)));
  114. lua_unlock(L);
  115. luai_userstatethread(L, L1);
  116. return L1;
  117. }
  118. void luaE_freethread (lua_State *L, lua_State *L1) {
  119. luaF_close(L1, L1->stack); /* close all upvalues for this thread */
  120. lua_assert(L1->openupval == NULL);
  121. luai_userstatefree(L1);
  122. freestack(L, L1);
  123. luaM_freemem(L, fromstate(L1), state_size(lua_State));
  124. }
  125. LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
  126. int i;
  127. lua_State *L;
  128. global_State *g;
  129. void *l = (*f)(ud, NULL, 0, state_size(LG));
  130. if (l == NULL) return NULL;
  131. L = tostate(l);
  132. g = &((LG *)L)->g;
  133. L->next = NULL;
  134. L->tt = LUA_TTHREAD;
  135. g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);
  136. L->marked = luaC_white(g);
  137. g->emergencygc = 0;
  138. g->nCcalls = 0;
  139. set2bits(L->marked, FIXEDBIT, SFIXEDBIT);
  140. preinit_state(L, g);
  141. g->frealloc = f;
  142. g->ud = ud;
  143. g->mainthread = L;
  144. g->uvhead.u.l.prev = &g->uvhead;
  145. g->uvhead.u.l.next = &g->uvhead;
  146. g->GCthreshold = MAX_LUMEM; /* no GC while building state */
  147. g->strt.size = 0;
  148. g->strt.nuse = 0;
  149. g->strt.hash = NULL;
  150. setnilvalue(registry(L));
  151. luaZ_initbuffer(L, &g->buff);
  152. g->panic = NULL;
  153. g->gcstate = GCSpause;
  154. g->rootgc = obj2gco(L);
  155. g->sweepstrgc = 0;
  156. g->sweepgc = &g->rootgc;
  157. g->gray = NULL;
  158. g->grayagain = NULL;
  159. g->weak = g->ephemeron = g->allweak = NULL;
  160. g->tmudata = NULL;
  161. g->totalbytes = sizeof(LG);
  162. g->gcpause = LUAI_GCPAUSE;
  163. g->gcstepmul = LUAI_GCMUL;
  164. g->gcdept = 0;
  165. for (i=0; i<NUM_TAGS; i++) g->mt[i] = NULL;
  166. if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
  167. /* memory allocation error: free partial state */
  168. close_state(L);
  169. L = NULL;
  170. }
  171. else
  172. luai_userstateopen(L);
  173. return L;
  174. }
  175. static void callallgcTM (lua_State *L, void *ud) {
  176. UNUSED(ud);
  177. luaC_callGCTM(L); /* call GC metamethods for all udata */
  178. }
  179. LUA_API void lua_close (lua_State *L) {
  180. L = G(L)->mainthread; /* only the main thread can be closed */
  181. lua_lock(L);
  182. luaF_close(L, L->stack); /* close all upvalues for this thread */
  183. luaC_separateudata(L, 1); /* separate udata that have GC metamethods */
  184. L->errfunc = 0; /* no error function during GC metamethods */
  185. do { /* repeat until no more errors */
  186. L->ci = L->base_ci;
  187. L->base = L->top = L->ci->base;
  188. G(L)->nCcalls = 0;
  189. } while (luaD_rawrunprotected(L, callallgcTM, NULL) != LUA_OK);
  190. lua_assert(G(L)->tmudata == NULL);
  191. luai_userstateclose(L);
  192. close_state(L);
  193. }