lstate.h 9.0 KB

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