lstate.h 7.5 KB

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