lstate.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. ** $Id: lstate.c,v 2.54 2009/04/28 19:04:36 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 "lapi.h"
  11. #include "ldebug.h"
  12. #include "ldo.h"
  13. #include "lfunc.h"
  14. #include "lgc.h"
  15. #include "llex.h"
  16. #include "lmem.h"
  17. #include "lstate.h"
  18. #include "lstring.h"
  19. #include "ltable.h"
  20. #include "ltm.h"
  21. #define state_size(x) (sizeof(x) + LUAI_EXTRASPACE)
  22. #define fromstate(l) (cast(lu_byte *, (l)) - LUAI_EXTRASPACE)
  23. #define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE))
  24. /*
  25. ** Main thread combines a thread state and the global state
  26. */
  27. typedef struct LG {
  28. lua_State l;
  29. global_State g;
  30. } LG;
  31. /*
  32. ** maximum number of nested calls made by error-handling function
  33. */
  34. #define LUAI_EXTRACALLS 10
  35. CallInfo *luaE_extendCI (lua_State *L) {
  36. CallInfo *ci = luaM_new(L, CallInfo);
  37. lua_assert(L->ci->next == NULL);
  38. L->ci->next = ci;
  39. ci->previous = L->ci;
  40. ci->next = NULL;
  41. if (++L->nci >= LUAI_MAXCALLS) {
  42. if (L->nci == LUAI_MAXCALLS) /* overflow? */
  43. luaG_runerror(L, "stack overflow");
  44. if (L->nci >= LUAI_MAXCALLS + LUAI_EXTRACALLS) /* again? */
  45. luaD_throw(L, LUA_ERRERR); /* error while handling overflow */
  46. }
  47. return ci;
  48. }
  49. void luaE_freeCI (lua_State *L) {
  50. CallInfo *ci = L->ci;
  51. CallInfo *next = ci->next;
  52. ci->next = NULL;
  53. while ((ci = next) != NULL) {
  54. next = ci->next;
  55. luaM_free(L, ci);
  56. L->nci--;
  57. }
  58. }
  59. static void stack_init (lua_State *L1, lua_State *L) {
  60. int i;
  61. /* initialize stack array */
  62. L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TValue);
  63. L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK;
  64. for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++)
  65. setnilvalue(L1->stack + i); /* erase new stack */
  66. L1->top = L1->stack;
  67. L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1;
  68. /* initialize first ci */
  69. L1->ci->func = L1->top;
  70. setnilvalue(L1->top++); /* 'function' entry for this 'ci' */
  71. L1->ci->top = L1->top + LUA_MINSTACK;
  72. L1->ci->callstatus = 0;
  73. }
  74. static void freestack (lua_State *L) {
  75. L->ci = &L->base_ci; /* reset 'ci' list */
  76. luaE_freeCI(L);
  77. lua_assert(L->nci == 0);
  78. luaM_freearray(L, L->stack, L->stacksize);
  79. }
  80. /*
  81. ** open parts that may cause memory-allocation errors
  82. */
  83. static void f_luaopen (lua_State *L, void *ud) {
  84. global_State *g = G(L);
  85. UNUSED(ud);
  86. stack_init(L, L); /* init stack */
  87. sethvalue(L, gt(L), luaH_new(L)); /* table of globals */
  88. sethvalue(L, registry(L), luaH_new(L)); /* registry */
  89. luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
  90. luaT_init(L);
  91. luaX_init(L);
  92. luaS_fix(luaS_newliteral(L, MEMERRMSG));
  93. g->GCthreshold = 4*g->totalbytes;
  94. }
  95. static void preinit_state (lua_State *L, global_State *g) {
  96. G(L) = g;
  97. L->stack = NULL;
  98. L->stacksize = 0;
  99. L->errorJmp = NULL;
  100. L->hook = NULL;
  101. L->hookmask = 0;
  102. L->basehookcount = 0;
  103. L->allowhook = 1;
  104. resethookcount(L);
  105. L->openupval = NULL;
  106. L->nny = 1;
  107. L->status = LUA_OK;
  108. L->base_ci.next = L->base_ci.previous = NULL;
  109. L->ci = &L->base_ci;
  110. L->nci = 0;
  111. L->errfunc = 0;
  112. setnilvalue(gt(L));
  113. }
  114. static void close_state (lua_State *L) {
  115. global_State *g = G(L);
  116. luaF_close(L, L->stack); /* close all upvalues for this thread */
  117. luaC_freeall(L); /* collect all objects */
  118. luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
  119. luaZ_freebuffer(L, &g->buff);
  120. freestack(L);
  121. lua_assert(g->totalbytes == sizeof(LG));
  122. (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0);
  123. }
  124. LUA_API lua_State *lua_newthread (lua_State *L) {
  125. lua_State *L1;
  126. lua_lock(L);
  127. luaC_checkGC(L);
  128. L1 = tostate(luaM_malloc(L, state_size(lua_State)));
  129. luaC_link(L, obj2gco(L1), LUA_TTHREAD);
  130. setthvalue(L, L->top, L1);
  131. api_incr_top(L);
  132. preinit_state(L1, G(L));
  133. stack_init(L1, L); /* init stack */
  134. setobj2n(L, gt(L1), gt(L)); /* share table of globals */
  135. L1->hookmask = L->hookmask;
  136. L1->basehookcount = L->basehookcount;
  137. L1->hook = L->hook;
  138. resethookcount(L1);
  139. lua_assert(iswhite(obj2gco(L1)));
  140. lua_unlock(L);
  141. luai_userstatethread(L, L1);
  142. return L1;
  143. }
  144. void luaE_freethread (lua_State *L, lua_State *L1) {
  145. luaF_close(L1, L1->stack); /* close all upvalues for this thread */
  146. lua_assert(L1->openupval == NULL);
  147. luai_userstatefree(L1);
  148. freestack(L1);
  149. luaM_freemem(L, fromstate(L1), state_size(lua_State));
  150. }
  151. LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
  152. int i;
  153. lua_State *L;
  154. global_State *g;
  155. void *l = (*f)(ud, NULL, 0, state_size(LG));
  156. if (l == NULL) return NULL;
  157. L = tostate(l);
  158. g = &((LG *)L)->g;
  159. L->next = NULL;
  160. L->tt = LUA_TTHREAD;
  161. g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);
  162. L->marked = luaC_white(g);
  163. g->gckind = KGC_NORMAL;
  164. g->nCcalls = 0;
  165. set2bits(L->marked, FIXEDBIT, SFIXEDBIT);
  166. preinit_state(L, g);
  167. g->frealloc = f;
  168. g->ud = ud;
  169. g->mainthread = L;
  170. g->uvhead.u.l.prev = &g->uvhead;
  171. g->uvhead.u.l.next = &g->uvhead;
  172. g->GCthreshold = MAX_LUMEM; /* no GC while building state */
  173. g->strt.size = 0;
  174. g->strt.nuse = 0;
  175. g->strt.hash = NULL;
  176. setnilvalue(registry(L));
  177. luaZ_initbuffer(L, &g->buff);
  178. g->panic = NULL;
  179. g->nilobjp = luaO_nilobject;
  180. g->gcstate = GCSpause;
  181. g->rootgc = obj2gco(L);
  182. g->sweepstrgc = 0;
  183. g->sweepgc = &g->rootgc;
  184. g->gray = NULL;
  185. g->grayagain = NULL;
  186. g->weak = g->ephemeron = g->allweak = NULL;
  187. g->tobefnz = NULL;
  188. g->totalbytes = sizeof(LG);
  189. g->gcpause = LUAI_GCPAUSE;
  190. g->gcstepmul = LUAI_GCMUL;
  191. g->gcdept = 0;
  192. for (i=0; i<NUM_TAGS; i++) g->mt[i] = NULL;
  193. if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
  194. /* memory allocation error: free partial state */
  195. close_state(L);
  196. L = NULL;
  197. }
  198. else
  199. luai_userstateopen(L);
  200. return L;
  201. }
  202. LUA_API void lua_close (lua_State *L) {
  203. L = G(L)->mainthread; /* only the main thread can be closed */
  204. lua_lock(L);
  205. luaF_close(L, L->stack); /* close all upvalues for this thread */
  206. luaC_separateudata(L, 1); /* separate all udata with GC metamethods */
  207. lua_assert(L->next == NULL);
  208. luaC_callAllGCTM(L); /* call GC metamethods for all udata */
  209. luai_userstateclose(L);
  210. close_state(L);
  211. }