lstate.h 7.6 KB

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