lstate.h 7.4 KB

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