lstate.c 5.7 KB

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