lstate.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. ** $Id: lstate.h,v 2.67 2010/09/30 17:21:31 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.
  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->udgc.
  30. **
  31. ** The list g->tobefnz links all userdata 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_CI_SIZE 8
  37. #define BASIC_STACK_SIZE (2*LUA_MINSTACK)
  38. /* kinds of Garbage Collection */
  39. #define KGC_NORMAL 0
  40. #define KGC_EMERGENCY 1 /* gc was forced by an allocation failure */
  41. #define KGC_GEN 2 /* generational collection */
  42. typedef struct stringtable {
  43. GCObject **hash;
  44. lu_int32 nuse; /* number of elements */
  45. int size;
  46. } stringtable;
  47. /*
  48. ** information about a call
  49. */
  50. typedef struct CallInfo {
  51. StkId func; /* function index in the stack */
  52. StkId top; /* top for this function */
  53. struct CallInfo *previous, *next; /* dynamic call link */
  54. short nresults; /* expected number of results from this function */
  55. lu_byte callstatus;
  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. ptrdiff_t extra;
  66. lu_byte old_allowhook;
  67. lu_byte status;
  68. } c;
  69. } u;
  70. } CallInfo;
  71. /*
  72. ** Bits in CallInfo status
  73. */
  74. #define CIST_LUA (1<<0) /* call is running a Lua function */
  75. #define CIST_HOOKED (1<<1) /* call is running a debug hook */
  76. #define CIST_REENTRY (1<<2) /* call is running on same invocation of
  77. luaV_execute of previous call */
  78. #define CIST_YIELDED (1<<3) /* call reentered after suspension */
  79. #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */
  80. #define CIST_STAT (1<<5) /* call has an error status (pcall) */
  81. #define CIST_TAIL (1<<6) /* call was tail called */
  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. lua_Alloc frealloc; /* function to reallocate memory */
  89. void *ud; /* auxiliary data to `frealloc' */
  90. lu_mem totalbytes; /* number of bytes currently allocated */
  91. l_mem GCdebt; /* when positive, run a GC step */
  92. lu_mem lastmajormem; /* memory in use after last major collection */
  93. stringtable strt; /* hash table for strings */
  94. TValue l_registry;
  95. unsigned short nCcalls; /* number of nested C calls */
  96. lu_byte currentwhite;
  97. lu_byte gcstate; /* state of garbage collector */
  98. lu_byte gckind; /* kind of GC running */
  99. int sweepstrgc; /* position of sweep in `strt' */
  100. GCObject *allgc; /* list of all collectable objects */
  101. GCObject *udgc; /* list of collectable userdata with finalizers */
  102. GCObject **sweepgc; /* current position of sweep */
  103. GCObject *gray; /* list of gray objects */
  104. GCObject *grayagain; /* list of objects to be traversed atomically */
  105. GCObject *weak; /* list of tables with weak values */
  106. GCObject *ephemeron; /* list of ephemeron tables (weak keys) */
  107. GCObject *allweak; /* list of all-weak tables */
  108. GCObject *tobefnz; /* list of userdata to be GC */
  109. UpVal uvhead; /* head of double-linked list of all open upvalues */
  110. Mbuffer buff; /* temporary buffer for string concatenation */
  111. int gcpause; /* size of pause between successive GCs */
  112. int gcmajorinc; /* how much to wait for a major GC (only in gen. mode) */
  113. int gcstepmul; /* GC `granularity' */
  114. lua_CFunction panic; /* to be called in unprotected errors */
  115. struct lua_State *mainthread;
  116. const lua_Number *version; /* pointer to version number */
  117. TString *memerrmsg; /* memory-error message */
  118. TString *tmname[TM_N]; /* array with tag-method names */
  119. struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */
  120. } global_State;
  121. /*
  122. ** `per thread' state
  123. */
  124. struct lua_State {
  125. CommonHeader;
  126. lu_byte status;
  127. StkId top; /* first free slot in the stack */
  128. global_State *l_G;
  129. CallInfo *ci; /* call info for 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. int stacksize;
  134. unsigned short nny; /* number of non-yieldable calls in stack */
  135. lu_byte hookmask;
  136. lu_byte allowhook;
  137. int basehookcount;
  138. int hookcount;
  139. lua_Hook hook;
  140. GCObject *openupval; /* list of open upvalues in this stack */
  141. GCObject *gclist;
  142. struct lua_longjmp *errorJmp; /* current error recover point */
  143. ptrdiff_t errfunc; /* current error handling function (stack index) */
  144. CallInfo base_ci; /* CallInfo for first level (C calling Lua) */
  145. };
  146. #define G(L) (L->l_G)
  147. /*
  148. ** Union of all collectable objects
  149. */
  150. union GCObject {
  151. GCheader gch; /* common header */
  152. union TString ts;
  153. union Udata u;
  154. union Closure cl;
  155. struct Table h;
  156. struct Proto p;
  157. struct UpVal uv;
  158. struct lua_State th; /* thread */
  159. };
  160. #define gch(o) (&(o)->gch)
  161. /* macros to convert a GCObject into a specific value */
  162. #define rawgco2ts(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts))
  163. #define gco2ts(o) (&rawgco2ts(o)->tsv)
  164. #define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u))
  165. #define gco2u(o) (&rawgco2u(o)->uv)
  166. #define gco2cl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl))
  167. #define gco2t(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h))
  168. #define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p))
  169. #define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv))
  170. #define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))
  171. /* macro to convert any Lua object into a GCObject */
  172. #define obj2gco(v) (cast(GCObject *, (v)))
  173. LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
  174. LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
  175. LUAI_FUNC void luaE_freeCI (lua_State *L);
  176. #endif