lstate.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. ** $Id: lstate.c,v 2.18 2004/12/06 17:53:42 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. Udata *u; /* head of udata list */
  62. global_State *g = G(L);
  63. UNUSED(ud);
  64. u = luaM_new(L, Udata);
  65. u->uv.len = 0;
  66. u->uv.metatable = NULL;
  67. g->firstudata = obj2gco(u);
  68. luaC_link(L, obj2gco(u), LUA_TUSERDATA);
  69. setbit(u->uv.marked, FIXEDBIT);
  70. setbit(L->marked, FIXEDBIT);
  71. stack_init(L, L); /* init stack */
  72. sethvalue(L, gt(L), luaH_new(L, 0, 4)); /* table of globals */
  73. hvalue(gt(L))->metatable = luaH_new(L, 0, 0); /* globals metatable */
  74. sethvalue(L, registry(L), luaH_new(L, 4, 4)); /* registry */
  75. luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
  76. luaT_init(L);
  77. luaX_init(L);
  78. luaS_fix(luaS_newliteral(L, MEMERRMSG));
  79. g->GCthreshold = 4*g->totalbytes;
  80. g->prevestimate = g->estimate = g->totalbytes;
  81. }
  82. static void preinit_state (lua_State *L, global_State *g) {
  83. L->l_G = g;
  84. L->tt = LUA_TTHREAD;
  85. L->marked = luaC_white(g);
  86. L->stack = NULL;
  87. L->stacksize = 0;
  88. L->errorJmp = NULL;
  89. L->hook = NULL;
  90. L->hookmask = 0;
  91. L->basehookcount = 0;
  92. L->allowhook = 1;
  93. resethookcount(L);
  94. L->openupval = NULL;
  95. L->size_ci = 0;
  96. L->nCcalls = 0;
  97. L->status = 0;
  98. L->base_ci = L->ci = NULL;
  99. L->errfunc = 0;
  100. setnilvalue(gt(L));
  101. }
  102. static void close_state (lua_State *L) {
  103. global_State *g = G(L);
  104. luaF_close(L, L->stack); /* close all upvalues for this thread */
  105. luaC_freeall(L); /* collect all objects */
  106. lua_assert(g->rootgc == NULL);
  107. lua_assert(g->strt.nuse == 0);
  108. luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *);
  109. luaZ_freebuffer(L, &g->buff);
  110. freestack(L, L);
  111. lua_assert(g->totalbytes == sizeof(LG));
  112. (*g->realloc)(g->ud, fromstate(L), state_size(LG), 0);
  113. }
  114. lua_State *luaE_newthread (lua_State *L) {
  115. lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State)));
  116. L1->next = L->next; /* link new thread after `L' */
  117. L->next = obj2gco(L1);
  118. preinit_state(L1, G(L));
  119. stack_init(L1, L); /* init stack */
  120. setobj2n(L, gt(L1), gt(L)); /* share table of globals */
  121. L1->hookmask = L->hookmask;
  122. L1->basehookcount = L->basehookcount;
  123. L1->hook = L->hook;
  124. resethookcount(L1);
  125. lua_assert(iswhite(obj2gco(L1)));
  126. return L1;
  127. }
  128. void luaE_freethread (lua_State *L, lua_State *L1) {
  129. luaF_close(L1, L1->stack); /* close all upvalues for this thread */
  130. lua_assert(L1->openupval == NULL);
  131. freestack(L, L1);
  132. luaM_freemem(L, fromstate(L1), state_size(lua_State));
  133. }
  134. LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
  135. lua_State *L;
  136. global_State *g;
  137. void *l = (*f)(ud, NULL, 0, state_size(LG));
  138. if (l == NULL) return NULL;
  139. L = tostate(l);
  140. g = &((LG *)L)->g;
  141. L->next = NULL;
  142. g->currentwhite = bitmask(WHITE0BIT);
  143. preinit_state(L, g);
  144. g->realloc = f;
  145. g->ud = ud;
  146. g->mainthread = L;
  147. g->GCthreshold = 0; /* mark it as unfinished state */
  148. g->strt.size = 0;
  149. g->strt.nuse = 0;
  150. g->strt.hash = NULL;
  151. setnilvalue(registry(L));
  152. luaZ_initbuffer(L, &g->buff);
  153. g->panic = NULL;
  154. g->gcstate = GCSpause;
  155. g->gcgenerational = 0;
  156. g->rootgc = obj2gco(L);
  157. g->sweepstrgc = 0;
  158. g->sweepgc = &g->rootgc;
  159. g->firstudata = NULL;
  160. g->gray = NULL;
  161. g->grayagain = NULL;
  162. g->weak = NULL;
  163. g->tmudata = NULL;
  164. setnilvalue(gkey(g->dummynode));
  165. setnilvalue(gval(g->dummynode));
  166. gnext(g->dummynode) = NULL;
  167. g->totalbytes = sizeof(LG);
  168. g->gcpace = GCDIV;
  169. g->incgc = 1;
  170. if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) {
  171. /* memory allocation error: free partial state */
  172. close_state(L);
  173. L = NULL;
  174. }
  175. else
  176. lua_userstateopen(L);
  177. return L;
  178. }
  179. static void callallgcTM (lua_State *L, void *ud) {
  180. UNUSED(ud);
  181. luaC_callGCTM(L); /* call GC metamethods for all udata */
  182. }
  183. LUA_API void lua_close (lua_State *L) {
  184. lua_lock(L);
  185. L = G(L)->mainthread; /* only the main thread can be closed */
  186. luaF_close(L, L->stack); /* close all upvalues for this thread */
  187. luaC_separateudata(L, 1); /* separate udata that have GC metamethods */
  188. L->errfunc = 0; /* no error function during GC metamethods */
  189. do { /* repeat until no more errors */
  190. L->ci = L->base_ci;
  191. L->base = L->top = L->ci->base;
  192. L->nCcalls = 0;
  193. } while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0);
  194. lua_assert(G(L)->tmudata == NULL);
  195. close_state(L);
  196. }