lstate.c 5.5 KB

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