lstate.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. ** $Id: lstate.h,v 2.121 2015/04/10 17:56:25 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. short nresults; /* expected number of results from this function */
  62. lu_byte callstatus;
  63. } CallInfo;
  64. /*
  65. ** Bits in CallInfo status
  66. */
  67. #define CIST_OAH (1<<0) /* original value of 'allowhook' */
  68. #define CIST_LUA (1<<1) /* call is running a Lua function */
  69. #define CIST_HOOKED (1<<2) /* call is running a debug hook */
  70. #define CIST_REENTRY (1<<3) /* call is running on same invocation of
  71. luaV_execute of previous call */
  72. #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */
  73. #define CIST_TAIL (1<<5) /* call was tail called */
  74. #define CIST_HOOKYIELD (1<<6) /* last hook called yielded */
  75. #define CIST_LEQ (1<<7) /* using __lt for __le */
  76. #define isLua(ci) ((ci)->callstatus & CIST_LUA)
  77. /* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */
  78. #define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v))
  79. #define getoah(st) ((st) & CIST_OAH)
  80. /*
  81. ** 'global state', shared by all threads of this state
  82. */
  83. typedef struct global_State {
  84. lua_Alloc frealloc; /* function to reallocate memory */
  85. void *ud; /* auxiliary data to 'frealloc' */
  86. lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */
  87. l_mem GCdebt; /* bytes allocated not yet compensated by the collector */
  88. lu_mem GCmemtrav; /* memory traversed by the GC */
  89. lu_mem GCestimate; /* an estimate of the non-garbage memory in use */
  90. stringtable strt; /* hash table for strings */
  91. TValue l_registry;
  92. unsigned int seed; /* randomized seed for hashes */
  93. lu_byte currentwhite;
  94. lu_byte gcstate; /* state of garbage collector */
  95. lu_byte gckind; /* kind of GC running */
  96. lu_byte gcrunning; /* true if GC is running */
  97. GCObject *allgc; /* list of all collectable objects */
  98. GCObject **sweepgc; /* current position of sweep in list */
  99. GCObject *finobj; /* list of collectable objects with finalizers */
  100. GCObject *gray; /* list of gray objects */
  101. GCObject *grayagain; /* list of objects to be traversed atomically */
  102. GCObject *weak; /* list of tables with weak values */
  103. GCObject *ephemeron; /* list of ephemeron tables (weak keys) */
  104. GCObject *allweak; /* list of all-weak tables */
  105. GCObject *tobefnz; /* list of userdata to be GC */
  106. GCObject *fixedgc; /* list of objects not to be collected */
  107. struct lua_State *twups; /* list of threads with open upvalues */
  108. Mbuffer buff; /* temporary buffer for string concatenation */
  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_SIZE][1]; /* cache for strings in API */
  119. } global_State;
  120. /*
  121. ** 'per thread' state
  122. */
  123. struct lua_State {
  124. CommonHeader;
  125. lu_byte status;
  126. StkId top; /* first free slot in the stack */
  127. global_State *l_G;
  128. CallInfo *ci; /* call info for current function */
  129. const Instruction *oldpc; /* last pc traced */
  130. StkId stack_last; /* last free slot in the stack */
  131. StkId stack; /* stack base */
  132. UpVal *openupval; /* list of open upvalues in this stack */
  133. GCObject *gclist;
  134. struct lua_State *twups; /* list of threads with open upvalues */
  135. struct lua_longjmp *errorJmp; /* current error recover point */
  136. CallInfo base_ci; /* CallInfo for first level (C calling Lua) */
  137. lua_Hook hook;
  138. ptrdiff_t errfunc; /* current error handling function (stack index) */
  139. int stacksize;
  140. int basehookcount;
  141. int hookcount;
  142. unsigned short nny; /* number of non-yieldable calls in stack */
  143. unsigned short nCcalls; /* number of nested C calls */
  144. lu_byte hookmask;
  145. lu_byte allowhook;
  146. };
  147. #define G(L) (L->l_G)
  148. /*
  149. ** Union of all collectable objects (only for conversions)
  150. */
  151. union GCUnion {
  152. GCObject gc; /* common header */
  153. struct TString ts;
  154. struct Udata u;
  155. union Closure cl;
  156. struct Table h;
  157. struct Proto p;
  158. struct lua_State th; /* thread */
  159. };
  160. #define cast_u(o) cast(union GCUnion *, (o))
  161. /* macros to convert a GCObject into a specific value */
  162. #define gco2ts(o) \
  163. check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts))
  164. #define gco2u(o) check_exp((o)->tt == LUA_TUSERDATA, &((cast_u(o))->u))
  165. #define gco2lcl(o) check_exp((o)->tt == LUA_TLCL, &((cast_u(o))->cl.l))
  166. #define gco2ccl(o) check_exp((o)->tt == LUA_TCCL, &((cast_u(o))->cl.c))
  167. #define gco2cl(o) \
  168. check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl))
  169. #define gco2t(o) check_exp((o)->tt == LUA_TTABLE, &((cast_u(o))->h))
  170. #define gco2p(o) check_exp((o)->tt == LUA_TPROTO, &((cast_u(o))->p))
  171. #define gco2th(o) check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th))
  172. /* macro to convert a Lua object into a GCObject */
  173. #define obj2gco(v) \
  174. check_exp(novariant((v)->tt) < LUA_TDEADKEY, (&(cast_u(v)->gc)))
  175. /* actual number of total bytes allocated */
  176. #define gettotalbytes(g) ((g)->totalbytes + (g)->GCdebt)
  177. LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
  178. LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
  179. LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
  180. LUAI_FUNC void luaE_freeCI (lua_State *L);
  181. LUAI_FUNC void luaE_shrinkCI (lua_State *L);
  182. #endif