lstate.h 6.7 KB

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