lstate.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. ** $Id: lstate.c,v 2.26 2005/03/18 18:02:04 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 "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. L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TValue);
  32. L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK;
  33. L1->top = L1->stack;
  34. L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1;
  35. L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo);
  36. L1->ci = L1->base_ci;
  37. L1->ci->func = L1->top;
  38. setnilvalue(L1->top++); /* `function' entry for this `ci' */
  39. L1->base = L1->ci->base = L1->top;
  40. L1->ci->top = L1->top + LUA_MINSTACK;
  41. L1->size_ci = BASIC_CI_SIZE;
  42. L1->end_ci = L1->base_ci + L1->size_ci - 1;
  43. }
  44. static void freestack (lua_State *L, lua_State *L1) {
  45. luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo);
  46. luaM_freearray(L, L1->stack, L1->stacksize, TValue);
  47. }
  48. /*
  49. ** open parts that may cause memory-allocation errors
  50. */
  51. static void f_luaopen (lua_State *L, void *ud) {
  52. global_State *g = G(L);
  53. UNUSED(ud);
  54. stack_init(L, L); /* init stack */
  55. sethvalue(L, gt(L), luaH_new(L, 0, 20)); /* table of globals */
  56. hvalue(gt(L))->metatable = luaH_new(L, 0, 0); /* globals metatable */
  57. sethvalue(L, registry(L), luaH_new(L, 6, 20)); /* registry */
  58. luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
  59. luaT_init(L);
  60. luaX_init(L);
  61. luaS_fix(luaS_newliteral(L, MEMERRMSG));
  62. g->GCthreshold = 4*g->totalbytes;
  63. }
  64. static void preinit_state (lua_State *L, global_State *g) {
  65. L->l_G = g;
  66. L->stack = NULL;
  67. L->stacksize = 0;
  68. L->errorJmp = NULL;
  69. L->hook = NULL;
  70. L->hookmask = 0;
  71. L->basehookcount = 0;
  72. L->allowhook = 1;
  73. resethookcount(L);
  74. L->openupval = NULL;
  75. L->size_ci = 0;
  76. L->nCcalls = 0;
  77. L->status = 0;
  78. L->base_ci = L->ci = NULL;
  79. L->errfunc = 0;
  80. setnilvalue(gt(L));
  81. }
  82. static void close_state (lua_State *L) {
  83. global_State *g = G(L);
  84. luaF_close(L, L->stack); /* close all upvalues for this thread */
  85. luaC_freeall(L); /* collect all objects */
  86. lua_assert(g->rootgc == obj2gco(L));
  87. lua_assert(g->strt.nuse == 0);
  88. luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *);
  89. luaZ_freebuffer(L, &g->buff);
  90. freestack(L, L);
  91. lua_assert(g->totalbytes == sizeof(LG));
  92. (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0);
  93. }
  94. lua_State *luaE_newthread (lua_State *L) {
  95. lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State)));
  96. luaC_link(L, obj2gco(L1), LUA_TTHREAD);
  97. preinit_state(L1, G(L));
  98. stack_init(L1, L); /* init stack */
  99. setobj2n(L, gt(L1), gt(L)); /* share table of globals */
  100. L1->hookmask = L->hookmask;
  101. L1->basehookcount = L->basehookcount;
  102. L1->hook = L->hook;
  103. resethookcount(L1);
  104. lua_assert(iswhite(obj2gco(L1)));
  105. return L1;
  106. }
  107. void luaE_freethread (lua_State *L, lua_State *L1) {
  108. luaF_close(L1, L1->stack); /* close all upvalues for this thread */
  109. lua_assert(L1->openupval == NULL);
  110. freestack(L, L1);
  111. luaM_freemem(L, fromstate(L1), state_size(lua_State));
  112. }
  113. LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
  114. lua_State *L;
  115. global_State *g;
  116. void *l = (*f)(ud, NULL, 0, state_size(LG));
  117. if (l == NULL) return NULL;
  118. L = tostate(l);
  119. g = &((LG *)L)->g;
  120. L->next = NULL;
  121. L->tt = LUA_TTHREAD;
  122. g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);
  123. L->marked = luaC_white(g);
  124. set2bits(L->marked, FIXEDBIT, SFIXEDBIT);
  125. preinit_state(L, g);
  126. g->frealloc = f;
  127. g->ud = ud;
  128. g->mainthread = L;
  129. g->uvhead.u.l.prev = &g->uvhead;
  130. g->uvhead.u.l.next = &g->uvhead;
  131. g->GCthreshold = 0; /* mark it as unfinished state */
  132. g->strt.size = 0;
  133. g->strt.nuse = 0;
  134. g->strt.hash = NULL;
  135. setnilvalue(registry(L));
  136. luaZ_initbuffer(L, &g->buff);
  137. g->panic = NULL;
  138. g->gcstate = GCSpause;
  139. g->rootgc = obj2gco(L);
  140. g->sweepstrgc = 0;
  141. g->sweepgc = &g->rootgc;
  142. g->gray = NULL;
  143. g->grayagain = NULL;
  144. g->weak = NULL;
  145. g->tmudata = NULL;
  146. g->totalbytes = sizeof(LG);
  147. g->gcpace = 200; /* 200% (wait memory to double before next collection) */
  148. g->gcstepmul = 200; /* GC runs `twice the speed' of memory allocation */
  149. g->gcdept = 0;
  150. if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) {
  151. /* memory allocation error: free partial state */
  152. close_state(L);
  153. L = NULL;
  154. }
  155. else
  156. luai_userstateopen(L);
  157. return L;
  158. }
  159. static void callallgcTM (lua_State *L, void *ud) {
  160. UNUSED(ud);
  161. luaC_callGCTM(L); /* call GC metamethods for all udata */
  162. }
  163. LUA_API void lua_close (lua_State *L) {
  164. lua_lock(L);
  165. L = G(L)->mainthread; /* only the main thread can be closed */
  166. luaF_close(L, L->stack); /* close all upvalues for this thread */
  167. luaC_separateudata(L, 1); /* separate udata that have GC metamethods */
  168. L->errfunc = 0; /* no error function during GC metamethods */
  169. do { /* repeat until no more errors */
  170. L->ci = L->base_ci;
  171. L->base = L->top = L->ci->base;
  172. L->nCcalls = 0;
  173. } while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0);
  174. lua_assert(G(L)->tmudata == NULL);
  175. close_state(L);
  176. }