lstate.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. ** $Id: lstate.c,v 2.106 2013/08/26 12:41: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[LUA_RIDX_MAINTHREAD] = L */
  134. setthvalue(L, &temp, L); /* temp = L */
  135. luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &temp);
  136. /* registry[LUA_RIDX_GLOBALS] = table of globals */
  137. sethvalue(L, &temp, luaH_new(L)); /* temp = new table (global table) */
  138. luaH_setint(L, registry, LUA_RIDX_GLOBALS, &temp);
  139. valnolocal(&temp); /* keep local invariant */
  140. }
  141. /*
  142. ** open parts of the state that may cause memory-allocation errors
  143. */
  144. static void f_luaopen (lua_State *L, void *ud) {
  145. global_State *g = G(L);
  146. UNUSED(ud);
  147. stack_init(L, L); /* init stack */
  148. init_registry(L, g);
  149. luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
  150. luaT_init(L);
  151. luaX_init(L);
  152. /* pre-create memory-error message */
  153. g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
  154. luaC_fix(L, obj2gco(g->memerrmsg)); /* it should never be collected */
  155. g->gcrunning = 1; /* allow gc */
  156. }
  157. /*
  158. ** preinitialize a state with consistent values without allocating
  159. ** any memory (to avoid errors)
  160. */
  161. static void preinit_state (lua_State *L, global_State *g) {
  162. G(L) = g;
  163. L->stack = NULL;
  164. L->ci = NULL;
  165. L->stacksize = 0;
  166. L->errorJmp = NULL;
  167. L->nCcalls = 0;
  168. L->hook = NULL;
  169. L->hookmask = 0;
  170. L->basehookcount = 0;
  171. L->allowhook = 1;
  172. resethookcount(L);
  173. L->openupval = NULL;
  174. L->nny = 1;
  175. L->status = LUA_OK;
  176. L->errfunc = 0;
  177. }
  178. static void close_state (lua_State *L) {
  179. global_State *g = G(L);
  180. luaF_close(L, L->stack); /* close all upvalues for this thread */
  181. luaC_freeallobjects(L); /* collect all objects */
  182. luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
  183. luaZ_freebuffer(L, &g->buff);
  184. freestack(L);
  185. lua_assert(gettotalbytes(g) == sizeof(LG));
  186. (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
  187. }
  188. LUA_API lua_State *lua_newthread (lua_State *L) {
  189. lua_State *L1;
  190. lua_lock(L);
  191. luaC_checkGC(L);
  192. /* create new thread, linked after 'l_registry' */
  193. L1 = &luaC_newobj(L, LUA_TTHREAD, sizeof(LX),
  194. &hvalue(&G(L)->l_registry)->next, offsetof(LX, l))->th;
  195. setthvalue(L, L->top, L1);
  196. api_incr_top(L);
  197. preinit_state(L1, G(L));
  198. nolocal(obj2gco(L1)); /* threads are never local */
  199. L1->hookmask = L->hookmask;
  200. L1->basehookcount = L->basehookcount;
  201. L1->hook = L->hook;
  202. resethookcount(L1);
  203. luai_userstatethread(L, L1);
  204. stack_init(L1, L); /* init stack */
  205. lua_unlock(L);
  206. return L1;
  207. }
  208. void luaE_freethread (lua_State *L, lua_State *L1) {
  209. LX *l = fromstate(L1);
  210. luaF_close(L1, L1->stack); /* close all upvalues for this thread */
  211. lua_assert(L1->openupval == NULL);
  212. luai_userstatefree(L, L1);
  213. freestack(L1);
  214. luaM_free(L, l);
  215. }
  216. LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
  217. int i;
  218. lua_State *L;
  219. global_State *g;
  220. LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
  221. if (l == NULL) return NULL;
  222. L = &l->l.l;
  223. g = &l->g;
  224. L->next = NULL;
  225. L->tt = LUA_TTHREAD;
  226. g->currentwhite = bitmask(WHITE0BIT);
  227. L->marked = luaC_white(g) | bitmask(LOCALBIT);
  228. g->gckind = KGC_NORMAL;
  229. preinit_state(L, g);
  230. g->frealloc = f;
  231. g->ud = ud;
  232. g->mainthread = L;
  233. g->seed = makeseed(L);
  234. g->gcrunning = 0; /* no GC while building state */
  235. g->GCestimate = 0;
  236. g->GCthreshold = 10000;
  237. g->strt.size = g->strt.nuse = g->strt.empty = 0;
  238. g->strt.hash = NULL;
  239. setnilvalue(&g->l_registry);
  240. luaZ_initbuffer(L, &g->buff);
  241. g->panic = NULL;
  242. g->version = lua_version(NULL);
  243. g->gcstate = GCSpause;
  244. g->allgc = NULL;
  245. g->localgc = NULL;
  246. g->finobj = NULL;
  247. g->tobefnz = NULL;
  248. g->fixedgc = NULL;
  249. g->sweepgc = g->sweepfin = NULL;
  250. g->gray = g->grayagain = NULL;
  251. g->weak = g->ephemeron = g->allweak = NULL;
  252. g->totalbytes = sizeof(LG);
  253. g->GCdebt = 0;
  254. g->gcpause = LUAI_GCPAUSE;
  255. g->gcstepmul = LUAI_GCMUL;
  256. for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
  257. if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
  258. /* memory allocation error: free partial state */
  259. close_state(L);
  260. L = NULL;
  261. }
  262. else
  263. luai_userstateopen(L);
  264. return L;
  265. }
  266. LUA_API void lua_close (lua_State *L) {
  267. L = G(L)->mainthread; /* only the main thread can be closed */
  268. lua_lock(L);
  269. luai_userstateclose(L);
  270. close_state(L);
  271. }