lstate.h 11 KB

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