Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

326 Zeilen
7.5 KiB

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