lstate.h 9.0 KB

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