lstate.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. ** $Id: lstate.h $
  3. ** Global State
  4. ** See Copyright Notice in lua.h
  5. */
  6. #ifndef lstate_h
  7. #define lstate_h
  8. #include "lua.h"
  9. #include "lobject.h"
  10. #include "ltm.h"
  11. #include "lzio.h"
  12. /*
  13. ** Some notes about garbage-collected objects: All objects in Lua must
  14. ** be kept somehow accessible until being freed, so all objects always
  15. ** belong to one (and only one) of these lists, using field 'next' of
  16. ** the 'CommonHeader' for the link:
  17. **
  18. ** 'allgc': all objects not marked for finalization;
  19. ** 'finobj': all objects marked for finalization;
  20. ** 'tobefnz': all objects ready to be finalized;
  21. ** 'fixedgc': all objects that are not to be collected (currently
  22. ** only small strings, such as reserved words).
  23. **
  24. ** For the generational collector, some of these lists have marks for
  25. ** generations. Each mark points to the first element in the list for
  26. ** that particular generation; that generation goes until the next mark.
  27. **
  28. ** 'allgc' -> 'survival': new objects;
  29. ** 'survival' -> 'old': objects that survived one collection;
  30. ** 'old' -> 'reallyold': objects that became old in last collection;
  31. ** 'reallyold' -> NULL: objects old for more than one cycle.
  32. **
  33. ** 'finobj' -> 'finobjsur': new objects marked for finalization;
  34. ** 'finobjsur' -> 'finobjold': survived """";
  35. ** 'finobjold' -> 'finobjrold': just old """";
  36. ** 'finobjrold' -> NULL: really old """".
  37. */
  38. /*
  39. ** Moreover, there is another set of lists that control gray objects.
  40. ** These lists are linked by fields 'gclist'. (All objects that
  41. ** can become gray have such a field. The field is not the same
  42. ** in all objects, but it always has this name.) Any gray object
  43. ** must belong to one of these lists, and all objects in these lists
  44. ** must be gray:
  45. **
  46. ** 'gray': regular gray objects, still waiting to be visited.
  47. ** 'grayagain': objects that must be revisited at the atomic phase.
  48. ** That includes
  49. ** - black objects got in a write barrier;
  50. ** - all kinds of weak tables during propagation phase;
  51. ** - all threads.
  52. ** 'weak': tables with weak values to be cleared;
  53. ** 'ephemeron': ephemeron tables with white->white entries;
  54. ** 'allweak': tables with weak keys and/or weak values to be cleared.
  55. */
  56. /*
  57. ** About 'nCcalls': each thread in Lua (a lua_State) keeps a count of
  58. ** how many "C calls" it can do in the C stack, to avoid C-stack overflow.
  59. ** This count is very rough approximation; it considers only recursive
  60. ** functions inside the interpreter, as non-recursive calls can be
  61. ** considered using a fixed (although unknown) amount of stack space.
  62. **
  63. ** The count itself has two parts: the lower part is the count itself;
  64. ** the higher part counts the number of non-yieldable calls in the stack.
  65. **
  66. ** Because calls to external C functions can use of unkown amount
  67. ** of space (e.g., functions using an auxiliary buffer), calls
  68. ** to these functions add more than one to the count.
  69. **
  70. ** The proper count also includes the number of CallInfo structures
  71. ** allocated by Lua, as a kind of "potential" calls. So, when Lua
  72. ** calls a function (and "consumes" one CallInfo), it needs neither to
  73. ** increment nor to check 'nCcalls', as its use of C stack is already
  74. ** accounted for.
  75. */
  76. /* number of "C stack slots" used by an external C function */
  77. #define CSTACKCF 10
  78. /* true if this thread does not have non-yieldable calls in the stack */
  79. #define yieldable(L) (((L)->nCcalls & 0xffff0000) == 0)
  80. /* real number of C calls */
  81. #define getCcalls(L) ((L)->nCcalls & 0xffff)
  82. /* Increment the number of non-yieldable calls */
  83. #define incnny(L) ((L)->nCcalls += 0x10000)
  84. /* Decrement the number of non-yieldable calls */
  85. #define decnny(L) ((L)->nCcalls -= 0x10000)
  86. /* Increment the number of non-yieldable calls and nCcalls */
  87. #define incXCcalls(L) ((L)->nCcalls += 0x10000 + CSTACKCF)
  88. /* Decrement the number of non-yieldable calls and nCcalls */
  89. #define decXCcalls(L) ((L)->nCcalls -= 0x10000 + CSTACKCF)
  90. struct lua_longjmp; /* defined in ldo.c */
  91. /*
  92. ** Atomic type (relative to signals) to better ensure that 'lua_sethook'
  93. ** is thread safe
  94. */
  95. #if !defined(l_signalT)
  96. #include <signal.h>
  97. #define l_signalT sig_atomic_t
  98. #endif
  99. /* extra stack space to handle TM calls and some other extras */
  100. #define EXTRA_STACK 5
  101. #define BASIC_STACK_SIZE (2*LUA_MINSTACK)
  102. /* kinds of Garbage Collection */
  103. #define KGC_INC 0 /* incremental gc */
  104. #define KGC_GEN 1 /* generational gc */
  105. typedef struct stringtable {
  106. TString **hash;
  107. int nuse; /* number of elements */
  108. int size;
  109. } stringtable;
  110. /*
  111. ** Information about a call.
  112. */
  113. typedef struct CallInfo {
  114. StkId func; /* function index in the stack */
  115. StkId top; /* top for this function */
  116. struct CallInfo *previous, *next; /* dynamic call link */
  117. union {
  118. struct { /* only for Lua functions */
  119. const Instruction *savedpc;
  120. l_signalT trap;
  121. int nextraargs; /* # of extra arguments in vararg functions */
  122. } l;
  123. struct { /* only for C functions */
  124. lua_KFunction k; /* continuation in case of yields */
  125. ptrdiff_t old_errfunc;
  126. lua_KContext ctx; /* context info. in case of yields */
  127. } c;
  128. } u;
  129. union {
  130. int funcidx; /* called-function index */
  131. int nyield; /* number of values yielded */
  132. struct { /* info about transfered values (for call/return hooks) */
  133. unsigned short ftransfer; /* offset of first value transfered */
  134. unsigned short ntransfer; /* number of values transfered */
  135. } transferinfo;
  136. } u2;
  137. short nresults; /* expected number of results from this function */
  138. unsigned short callstatus;
  139. } CallInfo;
  140. /*
  141. ** Bits in CallInfo status
  142. */
  143. #define CIST_OAH (1<<0) /* original value of 'allowhook' */
  144. #define CIST_C (1<<1) /* call is running a C function */
  145. #define CIST_HOOKED (1<<2) /* call is running a debug hook */
  146. #define CIST_YPCALL (1<<3) /* call is a yieldable protected call */
  147. #define CIST_TAIL (1<<4) /* call was tail called */
  148. #define CIST_HOOKYIELD (1<<5) /* last hook called yielded */
  149. #define CIST_FIN (1<<6) /* call is running a finalizer */
  150. #define CIST_TRAN (1<<7) /* 'ci' has transfer information */
  151. #if defined(LUA_COMPAT_LT_LE)
  152. #define CIST_LEQ (1<<8) /* using __lt for __le */
  153. #endif
  154. /* active function is a Lua function */
  155. #define isLua(ci) (!((ci)->callstatus & CIST_C))
  156. /* call is running Lua code (not a hook) */
  157. #define isLuacode(ci) (!((ci)->callstatus & (CIST_C | CIST_HOOKED)))
  158. /* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */
  159. #define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v))
  160. #define getoah(st) ((st) & CIST_OAH)
  161. /*
  162. ** 'global state', shared by all threads of this state
  163. */
  164. typedef struct global_State {
  165. lua_Alloc frealloc; /* function to reallocate memory */
  166. void *ud; /* auxiliary data to 'frealloc' */
  167. l_mem totalbytes; /* number of bytes currently allocated - GCdebt */
  168. l_mem GCdebt; /* bytes allocated not yet compensated by the collector */
  169. lu_mem GCestimate; /* an estimate of the non-garbage memory in use */
  170. lu_mem lastatomic; /* see function 'genstep' in file 'lgc.c' */
  171. stringtable strt; /* hash table for strings */
  172. TValue l_registry;
  173. TValue nilvalue; /* a nil value */
  174. unsigned int seed; /* randomized seed for hashes */
  175. lu_byte currentwhite;
  176. lu_byte gcstate; /* state of garbage collector */
  177. lu_byte gckind; /* kind of GC running */
  178. lu_byte genminormul; /* control for minor generational collections */
  179. lu_byte genmajormul; /* control for major generational collections */
  180. lu_byte gcrunning; /* true if GC is running */
  181. lu_byte gcemergency; /* true if this is an emergency collection */
  182. lu_byte gcpause; /* size of pause between successive GCs */
  183. lu_byte gcstepmul; /* GC "speed" */
  184. lu_byte gcstepsize; /* (log2 of) GC granularity */
  185. GCObject *allgc; /* list of all collectable objects */
  186. GCObject **sweepgc; /* current position of sweep in list */
  187. GCObject *finobj; /* list of collectable objects with finalizers */
  188. GCObject *gray; /* list of gray objects */
  189. GCObject *grayagain; /* list of objects to be traversed atomically */
  190. GCObject *weak; /* list of tables with weak values */
  191. GCObject *ephemeron; /* list of ephemeron tables (weak keys) */
  192. GCObject *allweak; /* list of all-weak tables */
  193. GCObject *tobefnz; /* list of userdata to be GC */
  194. GCObject *fixedgc; /* list of objects not to be collected */
  195. /* fields for generational collector */
  196. GCObject *survival; /* start of objects that survived one GC cycle */
  197. GCObject *old; /* start of old objects */
  198. GCObject *reallyold; /* old objects with more than one cycle */
  199. GCObject *finobjsur; /* list of survival objects with finalizers */
  200. GCObject *finobjold; /* list of old objects with finalizers */
  201. GCObject *finobjrold; /* list of really old objects with finalizers */
  202. struct lua_State *twups; /* list of threads with open upvalues */
  203. lua_CFunction panic; /* to be called in unprotected errors */
  204. struct lua_State *mainthread;
  205. TString *memerrmsg; /* message for memory-allocation errors */
  206. TString *tmname[TM_N]; /* array with tag-method names */
  207. struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */
  208. TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */
  209. lua_WarnFunction warnf; /* warning function */
  210. void *ud_warn; /* auxiliary data to 'warnf' */
  211. } global_State;
  212. /*
  213. ** 'per thread' state
  214. */
  215. struct lua_State {
  216. CommonHeader;
  217. lu_byte status;
  218. lu_byte allowhook;
  219. unsigned short nci; /* number of items in 'ci' list */
  220. StkId top; /* first free slot in the stack */
  221. global_State *l_G;
  222. CallInfo *ci; /* call info for current function */
  223. const Instruction *oldpc; /* last pc traced */
  224. StkId stack_last; /* last free slot in the stack */
  225. StkId stack; /* stack base */
  226. UpVal *openupval; /* list of open upvalues in this stack */
  227. GCObject *gclist;
  228. struct lua_State *twups; /* list of threads with open upvalues */
  229. struct lua_longjmp *errorJmp; /* current error recover point */
  230. CallInfo base_ci; /* CallInfo for first level (C calling Lua) */
  231. volatile lua_Hook hook;
  232. ptrdiff_t errfunc; /* current error handling function (stack index) */
  233. l_uint32 nCcalls; /* number of allowed nested C calls - 'nci' */
  234. int stacksize;
  235. int basehookcount;
  236. int hookcount;
  237. l_signalT hookmask;
  238. };
  239. #define G(L) (L->l_G)
  240. /*
  241. ** Union of all collectable objects (only for conversions)
  242. */
  243. union GCUnion {
  244. GCObject gc; /* common header */
  245. struct TString ts;
  246. struct Udata u;
  247. union Closure cl;
  248. struct Table h;
  249. struct Proto p;
  250. struct lua_State th; /* thread */
  251. struct UpVal upv;
  252. };
  253. #define cast_u(o) cast(union GCUnion *, (o))
  254. /* macros to convert a GCObject into a specific value */
  255. #define gco2ts(o) \
  256. check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts))
  257. #define gco2u(o) check_exp((o)->tt == LUA_TUSERDATA, &((cast_u(o))->u))
  258. #define gco2lcl(o) check_exp((o)->tt == LUA_TLCL, &((cast_u(o))->cl.l))
  259. #define gco2ccl(o) check_exp((o)->tt == LUA_TCCL, &((cast_u(o))->cl.c))
  260. #define gco2cl(o) \
  261. check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl))
  262. #define gco2t(o) check_exp((o)->tt == LUA_TTABLE, &((cast_u(o))->h))
  263. #define gco2p(o) check_exp((o)->tt == LUA_TPROTO, &((cast_u(o))->p))
  264. #define gco2th(o) check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th))
  265. #define gco2upv(o) \
  266. check_exp(novariant((o)->tt) == LUA_TUPVAL, &((cast_u(o))->upv))
  267. /*
  268. ** macro to convert a Lua object into a GCObject
  269. ** (The access to 'tt' tries to ensure that 'v' is actually a Lua object.)
  270. */
  271. #define obj2gco(v) check_exp((v)->tt >= LUA_TSTRING, &(cast_u(v)->gc))
  272. /* actual number of total bytes allocated */
  273. #define gettotalbytes(g) cast(lu_mem, (g)->totalbytes + (g)->GCdebt)
  274. LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
  275. LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
  276. LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
  277. LUAI_FUNC void luaE_freeCI (lua_State *L);
  278. LUAI_FUNC void luaE_shrinkCI (lua_State *L);
  279. LUAI_FUNC void luaE_enterCcall (lua_State *L);
  280. LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont);
  281. #define luaE_exitCcall(L) ((L)->nCcalls--)
  282. #endif