lstate.h 10 KB

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