lstate.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. ** $Id: lstate.c,v 1.134 2003/12/04 18:52:23 roberto Exp roberto $
  3. ** Global State
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stddef.h>
  7. #define lstate_c
  8. #include "lua.h"
  9. #include "ldebug.h"
  10. #include "ldo.h"
  11. #include "lfunc.h"
  12. #include "lgc.h"
  13. #include "llex.h"
  14. #include "lmem.h"
  15. #include "lstate.h"
  16. #include "lstring.h"
  17. #include "ltable.h"
  18. #include "ltm.h"
  19. /*
  20. ** macro to allow the inclusion of user information in Lua state
  21. */
  22. #ifndef LUA_USERSTATE
  23. #define EXTRASPACE 0
  24. #else
  25. union UEXTRASPACE {L_Umaxalign a; LUA_USERSTATE b;};
  26. #define EXTRASPACE (sizeof(union UEXTRASPACE))
  27. #endif
  28. #define state_size(x) (sizeof(x) + EXTRASPACE)
  29. #define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + EXTRASPACE))
  30. #define fromstate(l) (cast(lu_byte *, (l)) - EXTRASPACE)
  31. /*
  32. ** Main thread combines a thread state and the global state
  33. */
  34. typedef struct LG {
  35. lua_State l;
  36. global_State g;
  37. } LG;
  38. static void stack_init (lua_State *L1, lua_State *L) {
  39. L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TValue);
  40. L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK;
  41. L1->top = L1->stack;
  42. L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1;
  43. L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo);
  44. L1->ci = L1->base_ci;
  45. setnilvalue(L1->top++); /* `function' entry for this `ci' */
  46. L1->base = L1->ci->base = L1->top;
  47. L1->ci->top = L1->top + LUA_MINSTACK;
  48. L1->size_ci = BASIC_CI_SIZE;
  49. L1->end_ci = L1->base_ci + L1->size_ci;
  50. }
  51. static void freestack (lua_State *L, lua_State *L1) {
  52. luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo);
  53. luaM_freearray(L, L1->stack, L1->stacksize, TValue);
  54. }
  55. /*
  56. ** open parts that may cause memory-allocation errors
  57. */
  58. static void f_luaopen (lua_State *L, void *ud) {
  59. Udata *u; /* head of udata list */
  60. UNUSED(ud);
  61. u = cast(Udata *, luaM_malloc(L, sizeudata(0)));
  62. u->uv.len = 0;
  63. u->uv.metatable = NULL;
  64. G(L)->firstudata = obj2gco(u);
  65. luaC_link(L, obj2gco(u), LUA_TUSERDATA);
  66. setbit(u->uv.marked, FIXEDBIT);
  67. stack_init(L, L); /* init stack */
  68. sethvalue(L, gt(L), luaH_new(L, 0, 4)); /* table of globals */
  69. sethvalue(L, registry(L), luaH_new(L, 4, 4)); /* registry */
  70. luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
  71. luaT_init(L);
  72. luaX_init(L);
  73. luaS_fix(luaS_newliteral(L, MEMERRMSG));
  74. G(L)->GCthreshold = 4*G(L)->nblocks;
  75. }
  76. static void preinit_state (lua_State *L) {
  77. L->stack = NULL;
  78. L->stacksize = 0;
  79. L->errorJmp = NULL;
  80. L->hook = NULL;
  81. L->hookmask = 0;
  82. L->basehookcount = 0;
  83. L->allowhook = 1;
  84. resethookcount(L);
  85. L->openupval = NULL;
  86. L->size_ci = 0;
  87. L->nCcalls = 0;
  88. L->isSuspended = 0;
  89. L->base_ci = L->ci = NULL;
  90. L->errfunc = 0;
  91. setnilvalue(gt(L));
  92. }
  93. static void close_state (lua_State *L) {
  94. global_State *g = G(L);
  95. luaF_close(L, L->stack); /* close all upvalues for this thread */
  96. luaC_sweepall(L); /* collect all elements */
  97. lua_assert(g->rootgc == NULL);
  98. luaS_freeall(L);
  99. luaZ_freebuffer(L, &g->buff);
  100. freestack(L, L);
  101. lua_assert(g->nblocks == sizeof(LG));
  102. (*g->realloc)(g->ud, fromstate(L), state_size(LG), 0);
  103. }
  104. lua_State *luaE_newthread (lua_State *L) {
  105. lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State)));
  106. luaC_link(L, obj2gco(L1), LUA_TTHREAD);
  107. preinit_state(L1);
  108. L1->l_G = L->l_G;
  109. stack_init(L1, L); /* init stack */
  110. setobj2n(L, gt(L1), gt(L)); /* share table of globals */
  111. lua_assert(iswhite(obj2gco(L1)));
  112. return L1;
  113. }
  114. void luaE_freethread (lua_State *L, lua_State *L1) {
  115. luaF_close(L1, L1->stack); /* close all upvalues for this thread */
  116. lua_assert(L1->openupval == NULL);
  117. freestack(L, L1);
  118. luaM_free(L, fromstate(L1), state_size(lua_State));
  119. }
  120. LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
  121. lua_State *L;
  122. global_State *g;
  123. void *l = (*f)(ud, NULL, 0, state_size(LG));
  124. if (l == NULL) return NULL;
  125. L = tostate(l);
  126. g = &((LG *)L)->g;
  127. L->tt = LUA_TTHREAD;
  128. L->marked = 0;
  129. L->next = L->gclist = NULL;
  130. preinit_state(L);
  131. L->l_G = g;
  132. g->realloc = f;
  133. g->ud = ud;
  134. g->mainthread = L;
  135. g->GCthreshold = 0; /* mark it as unfinished state */
  136. g->strt.size = 0;
  137. g->strt.nuse = 0;
  138. g->strt.hash = NULL;
  139. setnilvalue(registry(L));
  140. luaZ_initbuffer(L, &g->buff);
  141. g->panic = NULL;
  142. g->gcstate = 0;
  143. g->rootgc = NULL;
  144. g->sweepstrgc = 0;
  145. g->currentwhite = bitmask(WHITE0BIT);
  146. g->firstudata = NULL;
  147. g->gray = NULL;
  148. g->grayagain = NULL;
  149. g->weak = NULL;
  150. g->tmudata = NULL;
  151. setnilvalue(gkey(g->dummynode));
  152. setnilvalue(gval(g->dummynode));
  153. g->dummynode->next = NULL;
  154. g->nblocks = sizeof(LG);
  155. if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) {
  156. /* memory allocation error: free partial state */
  157. close_state(L);
  158. L = NULL;
  159. }
  160. lua_userstateopen(L);
  161. return L;
  162. }
  163. static void callallgcTM (lua_State *L, void *ud) {
  164. UNUSED(ud);
  165. luaC_callGCTM(L); /* call GC metamethods for all udata */
  166. }
  167. LUA_API void lua_close (lua_State *L) {
  168. lua_lock(L);
  169. L = G(L)->mainthread; /* only the main thread can be closed */
  170. luaF_close(L, L->stack); /* close all upvalues for this thread */
  171. luaC_separateudata(L); /* separate udata that have GC metamethods */
  172. L->errfunc = 0; /* no error function during GC metamethods */
  173. do { /* repeat until no more errors */
  174. L->ci = L->base_ci;
  175. L->base = L->top = L->ci->base;
  176. L->nCcalls = 0;
  177. } while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0);
  178. lua_assert(G(L)->tmudata == NULL);
  179. close_state(L);
  180. }