lstate.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. ** $Id: lstate.c,v 2.110 2013/09/03 15:37:10 roberto Exp roberto $
  3. ** Global State
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stddef.h>
  7. #include <string.h>
  8. #define lstate_c
  9. #define LUA_CORE
  10. #include "lua.h"
  11. #include "lapi.h"
  12. #include "ldebug.h"
  13. #include "ldo.h"
  14. #include "lfunc.h"
  15. #include "lgc.h"
  16. #include "llex.h"
  17. #include "lmem.h"
  18. #include "lstate.h"
  19. #include "lstring.h"
  20. #include "ltable.h"
  21. #include "ltm.h"
  22. #if !defined(LUAI_GCPAUSE)
  23. #define LUAI_GCPAUSE 200 /* 200% */
  24. #endif
  25. #if !defined(LUAI_GCMUL)
  26. #define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */
  27. #endif
  28. #define MEMERRMSG "not enough memory"
  29. /*
  30. ** a macro to help the creation of a unique random seed when a state is
  31. ** created; the seed is used to randomize hashes.
  32. */
  33. #if !defined(luai_makeseed)
  34. #include <time.h>
  35. #define luai_makeseed() cast(unsigned int, time(NULL))
  36. #endif
  37. /*
  38. ** thread state + extra space
  39. */
  40. typedef struct LX {
  41. #if defined(LUAI_EXTRASPACE)
  42. char buff[LUAI_EXTRASPACE];
  43. #endif
  44. lua_State l;
  45. } LX;
  46. /*
  47. ** Main thread combines a thread state and the global state
  48. */
  49. typedef struct LG {
  50. LX l;
  51. global_State g;
  52. } LG;
  53. #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
  54. /*
  55. ** Compute an initial seed as random as possible. In ANSI, rely on
  56. ** Address Space Layout Randomization (if present) to increase
  57. ** randomness..
  58. */
  59. #define addbuff(b,p,e) \
  60. { size_t t = cast(size_t, e); \
  61. memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); }
  62. static unsigned int makeseed (lua_State *L) {
  63. char buff[4 * sizeof(size_t)];
  64. unsigned int h = luai_makeseed();
  65. int p = 0;
  66. addbuff(buff, p, L); /* heap variable */
  67. addbuff(buff, p, &h); /* local variable */
  68. addbuff(buff, p, luaO_nilobject); /* global variable */
  69. addbuff(buff, p, &lua_newstate); /* public function */
  70. lua_assert(p == sizeof(buff));
  71. return luaS_hash(buff, p, h);
  72. }
  73. /*
  74. ** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
  75. ** invariant
  76. */
  77. void luaE_setdebt (global_State *g, l_mem debt) {
  78. g->totalbytes -= (debt - g->GCdebt);
  79. g->GCdebt = debt;
  80. }
  81. CallInfo *luaE_extendCI (lua_State *L) {
  82. CallInfo *ci = luaM_new(L, CallInfo);
  83. lua_assert(L->ci->next == NULL);
  84. L->ci->next = ci;
  85. ci->previous = L->ci;
  86. ci->next = NULL;
  87. return ci;
  88. }
  89. void luaE_freeCI (lua_State *L) {
  90. CallInfo *ci = L->ci;
  91. CallInfo *next = ci->next;
  92. ci->next = NULL;
  93. while ((ci = next) != NULL) {
  94. next = ci->next;
  95. luaM_free(L, ci);
  96. }
  97. }
  98. static void stack_init (lua_State *L1, lua_State *L) {
  99. int i; CallInfo *ci;
  100. /* initialize stack array */
  101. L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue);
  102. L1->stacksize = BASIC_STACK_SIZE;
  103. for (i = 0; i < BASIC_STACK_SIZE; i++)
  104. setnilvalue(L1->stack + i); /* erase new stack */
  105. L1->top = L1->stack;
  106. L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;
  107. /* initialize first ci */
  108. ci = &L1->base_ci;
  109. ci->next = ci->previous = NULL;
  110. ci->callstatus = 0;
  111. ci->func = L1->top;
  112. setnilvalue(L1->top++); /* 'function' entry for this 'ci' */
  113. ci->top = L1->top + LUA_MINSTACK;
  114. L1->ci = ci;
  115. }
  116. static void freestack (lua_State *L) {
  117. if (L->stack == NULL)
  118. return; /* stack not completely built yet */
  119. L->ci = &L->base_ci; /* free the entire 'ci' list */
  120. luaE_freeCI(L);
  121. luaM_freearray(L, L->stack, L->stacksize); /* free stack array */
  122. }
  123. /*
  124. ** Create registry table and its predefined values
  125. */
  126. static void init_registry (lua_State *L, global_State *g) {
  127. TValue temp;
  128. /* create registry */
  129. Table *registry = luaH_new(L);
  130. sethvalue(L, &g->l_registry, registry);
  131. luaH_resize(L, registry, LUA_RIDX_LAST, 0);
  132. nolocal(obj2gco(registry));
  133. /* registry is the first "regular" object created by a state; move it
  134. from 'localgc' to 'allgc' so that it act as a "sentinel" there */
  135. lua_assert(g->allgc == NULL &&
  136. registry->next == NULL &&
  137. g->localgc == obj2gco(registry));
  138. g->allgc = g->localgc;
  139. g->localgc = NULL;
  140. l_setbit(registry->marked, LOCALMARK); /* mark that it is not in 'localgc' */
  141. /* registry[LUA_RIDX_MAINTHREAD] = L */
  142. setthvalue(L, &temp, L); /* temp = L */
  143. luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &temp);
  144. /* registry[LUA_RIDX_GLOBALS] = table of globals */
  145. sethvalue(L, &temp, luaH_new(L)); /* temp = new table (global table) */
  146. luaH_setint(L, registry, LUA_RIDX_GLOBALS, &temp);
  147. valnolocal(&temp); /* keep local invariant */
  148. }
  149. /*
  150. ** open parts of the state that may cause memory-allocation errors
  151. */
  152. static void f_luaopen (lua_State *L, void *ud) {
  153. global_State *g = G(L);
  154. UNUSED(ud);
  155. stack_init(L, L); /* init stack */
  156. init_registry(L, g);
  157. luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
  158. luaT_init(L);
  159. luaX_init(L);
  160. /* pre-create memory-error message */
  161. g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
  162. luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
  163. g->gcrunning = 1; /* allow gc */
  164. }
  165. /*
  166. ** preinitialize a state with consistent values without allocating
  167. ** any memory (to avoid errors)
  168. */
  169. static void preinit_state (lua_State *L, global_State *g) {
  170. G(L) = g;
  171. L->stack = NULL;
  172. L->ci = NULL;
  173. L->stacksize = 0;
  174. L->errorJmp = NULL;
  175. L->nCcalls = 0;
  176. L->hook = NULL;
  177. L->hookmask = 0;
  178. L->basehookcount = 0;
  179. L->allowhook = 1;
  180. resethookcount(L);
  181. L->openupval = NULL;
  182. L->nny = 1;
  183. L->status = LUA_OK;
  184. L->errfunc = 0;
  185. }
  186. static void close_state (lua_State *L) {
  187. global_State *g = G(L);
  188. luaF_close(L, L->stack); /* close all upvalues for this thread */
  189. luaC_freeallobjects(L); /* collect all objects */
  190. luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
  191. luaZ_freebuffer(L, &g->buff);
  192. freestack(L);
  193. lua_assert(gettotalbytes(g) == sizeof(LG));
  194. (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
  195. }
  196. LUA_API lua_State *lua_newthread (lua_State *L) {
  197. lua_State *L1;
  198. lua_lock(L);
  199. luaC_checkGC(L);
  200. /* create new thread, linked after 'l_registry' */
  201. L1 = &luaC_newobj(L, LUA_TTHREAD, sizeof(LX),
  202. &hvalue(&G(L)->l_registry)->next, offsetof(LX, l))->th;
  203. setthvalue(L, L->top, L1);
  204. api_incr_top(L);
  205. preinit_state(L1, G(L));
  206. nolocal(obj2gco(L1)); /* threads are never local */
  207. L1->hookmask = L->hookmask;
  208. L1->basehookcount = L->basehookcount;
  209. L1->hook = L->hook;
  210. resethookcount(L1);
  211. luai_userstatethread(L, L1);
  212. stack_init(L1, L); /* init stack */
  213. lua_unlock(L);
  214. return L1;
  215. }
  216. void luaE_freethread (lua_State *L, lua_State *L1) {
  217. LX *l = fromstate(L1);
  218. luaF_close(L1, L1->stack); /* close all upvalues for this thread */
  219. lua_assert(L1->openupval == NULL);
  220. luai_userstatefree(L, L1);
  221. freestack(L1);
  222. luaM_free(L, l);
  223. }
  224. LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
  225. int i;
  226. lua_State *L;
  227. global_State *g;
  228. LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
  229. if (l == NULL) return NULL;
  230. L = &l->l.l;
  231. g = &l->g;
  232. L->next = NULL;
  233. L->tt = LUA_TTHREAD;
  234. g->currentwhite = bitmask(WHITE0BIT);
  235. L->marked = luaC_white(g) | bitmask(LOCALBIT);
  236. g->gckind = KGC_NORMAL;
  237. preinit_state(L, g);
  238. g->frealloc = f;
  239. g->ud = ud;
  240. g->mainthread = L;
  241. g->seed = makeseed(L);
  242. g->gcrunning = 0; /* no GC while building state */
  243. g->GCestimate = 0;
  244. g->GCthreshold = 10000;
  245. g->strt.size = g->strt.nuse = 0;
  246. g->strt.hash = NULL;
  247. setnilvalue(&g->l_registry);
  248. luaZ_initbuffer(L, &g->buff);
  249. g->panic = NULL;
  250. g->version = lua_version(NULL);
  251. g->gcstate = GCSpause;
  252. g->localgc = g->localfin = g->allgc = g->finobj = NULL;
  253. g->tobefnz = NULL;
  254. g->fixedgc = NULL;
  255. g->sweepgc = NULL;
  256. g->gray = g->grayagain = NULL;
  257. g->weak = g->ephemeron = g->allweak = NULL;
  258. g->totalbytes = sizeof(LG);
  259. g->GCdebt = 0;
  260. g->gcpause = LUAI_GCPAUSE;
  261. g->gcstepmul = LUAI_GCMUL;
  262. for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
  263. if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
  264. /* memory allocation error: free partial state */
  265. close_state(L);
  266. L = NULL;
  267. }
  268. else
  269. luai_userstateopen(L);
  270. return L;
  271. }
  272. LUA_API void lua_close (lua_State *L) {
  273. L = G(L)->mainthread; /* only the main thread can be closed */
  274. lua_lock(L);
  275. luai_userstateclose(L);
  276. close_state(L);
  277. }