lstate.h 7.7 KB

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