lstate.c 5.7 KB

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