lstate.c 5.5 KB

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