lstate.c 5.5 KB

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