lstate.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. ** $Id: lstate.h,v 2.39 2009/03/10 17:14:37 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->rootgc. 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. ** Userdata with finalizers are kept in the list g->rootgc, but after
  30. ** the mainthread, which should be otherwise the last element in the
  31. ** list, as it was the first one inserted there.
  32. **
  33. ** The list g->tobefnz links all userdata being finalized.
  34. */
  35. struct lua_longjmp; /* defined in ldo.c */
  36. /* table of globals */
  37. #define gt(L) (&L->l_gt)
  38. /* registry */
  39. #define registry(L) (&G(L)->l_registry)
  40. /* extra stack space to handle TM calls and some other extras */
  41. #define EXTRA_STACK 5
  42. #define BASIC_CI_SIZE 8
  43. #define BASIC_STACK_SIZE (2*LUA_MINSTACK)
  44. /* kinds of Garbage Collection */
  45. #define KGC_NORMAL 0
  46. #define KGC_FORCED 1 /* gc was forced by the program */
  47. #define KGC_EMERGENCY 2 /* gc was forced by an allocation failure */
  48. typedef struct stringtable {
  49. GCObject **hash;
  50. lu_int32 nuse; /* number of elements */
  51. int size;
  52. } stringtable;
  53. /*
  54. ** informations about a call
  55. */
  56. typedef struct CallInfo {
  57. StkId base; /* base for this function */
  58. StkId func; /* function index in the stack */
  59. StkId top; /* top for this function */
  60. const Instruction *savedpc;
  61. short nresults; /* expected number of results from this function */
  62. lu_byte callstatus;
  63. union {
  64. struct { /* only for Lua functions */
  65. int tailcalls; /* number of tail calls lost under this entry */
  66. } l;
  67. struct { /* only for C functions */
  68. int ctx; /* context info. in case of yields */
  69. lua_CFunction k; /* continuation in case of yields */
  70. } c;
  71. } u;
  72. } CallInfo;
  73. /*
  74. ** Bits in CallInfo status
  75. */
  76. #define CIST_LUA 1 /* call is running a Lua function */
  77. #define CIST_HOOKED 2 /* call is running a debug hook */
  78. #define CIST_REENTRY 4 /* call is running on same invocation of
  79. luaV_execute of previous call */
  80. #define CIST_CTX 8 /* call has a ctx value */
  81. #define curr_func(L) (clvalue(L->ci->func))
  82. #define ci_func(ci) (clvalue((ci)->func))
  83. #define isLua(ci) ((ci)->callstatus & CIST_LUA)
  84. /*
  85. ** `global state', shared by all threads of this state
  86. */
  87. typedef struct global_State {
  88. stringtable strt; /* hash table for strings */
  89. lua_Alloc frealloc; /* function to reallocate memory */
  90. void *ud; /* auxiliary data to `frealloc' */
  91. unsigned short nCcalls; /* number of nested C calls */
  92. lu_byte currentwhite;
  93. lu_byte gcstate; /* state of garbage collector */
  94. lu_byte gckind; /* kind of GC running */
  95. int sweepstrgc; /* position of sweep in `strt' */
  96. GCObject *rootgc; /* list of all collectable objects */
  97. GCObject **sweepgc; /* current position of sweep */
  98. GCObject *gray; /* list of gray objects */
  99. GCObject *grayagain; /* list of objects to be traversed atomically */
  100. GCObject *weak; /* list of tables with weak values */
  101. GCObject *ephemeron; /* list of ephemeron tables (weak keys) */
  102. GCObject *allweak; /* list of all-weak tables */
  103. GCObject *tobefnz; /* list of userdata to be GC */
  104. Mbuffer buff; /* temporary buffer for string concatentation */
  105. lu_mem GCthreshold;
  106. lu_mem totalbytes; /* number of bytes currently allocated */
  107. lu_mem estimate; /* an estimate of number of bytes actually in use */
  108. lu_mem gcdept; /* how much GC is `behind schedule' */
  109. int gcpause; /* size of pause between successive GCs */
  110. int gcstepmul; /* GC `granularity' */
  111. lua_CFunction panic; /* to be called in unprotected errors */
  112. TValue l_registry;
  113. struct lua_State *mainthread;
  114. UpVal uvhead; /* head of double-linked list of all open upvalues */
  115. struct Table *mt[NUM_TAGS]; /* metatables for basic types */
  116. TString *tmname[TM_N]; /* array with tag-method names */
  117. const TValue *nilobjp; /* pointer to nil object (to check consistency) */
  118. } global_State;
  119. /*
  120. ** `per thread' state
  121. */
  122. struct lua_State {
  123. CommonHeader;
  124. lu_byte status;
  125. StkId top; /* first free slot in the stack */
  126. StkId base; /* base of current function */
  127. global_State *l_G;
  128. CallInfo *ci; /* call info for current function */
  129. const Instruction *savedpc; /* `savedpc' of 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. CallInfo *end_ci; /* points after end of ci array*/
  134. CallInfo *base_ci; /* array of CallInfo's */
  135. int stacksize;
  136. int size_ci; /* size of array `base_ci' */
  137. unsigned short nny; /* number of non-yieldable calls in stack */
  138. lu_byte hookmask;
  139. lu_byte allowhook;
  140. int basehookcount;
  141. int hookcount;
  142. lua_Hook hook;
  143. TValue l_gt; /* table of globals */
  144. TValue env; /* temporary place for environments */
  145. GCObject *openupval; /* list of open upvalues in this stack */
  146. GCObject *gclist;
  147. struct lua_longjmp *errorJmp; /* current error recover point */
  148. ptrdiff_t errfunc; /* current error handling function (stack index) */
  149. };
  150. #define G(L) (L->l_G)
  151. /*
  152. ** Union of all collectable objects
  153. */
  154. union GCObject {
  155. GCheader gch; /* common header */
  156. union TString ts;
  157. union Udata u;
  158. union Closure cl;
  159. struct Table h;
  160. struct Proto p;
  161. struct UpVal uv;
  162. struct lua_State th; /* thread */
  163. };
  164. #define gch(o) (&(o)->gch)
  165. /* macros to convert a GCObject into a specific value */
  166. #define rawgco2ts(o) check_exp((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 gco2cl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl))
  171. #define gco2t(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h))
  172. #define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p))
  173. #define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv))
  174. #define ngcotouv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv))
  175. #define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))
  176. /* macro to convert any Lua object into a GCObject */
  177. #define obj2gco(v) (cast(GCObject *, (v)))
  178. LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
  179. #endif