lstate.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. ** $Id: lstate.h,v 2.133.1.1 2017/04/19 17:39:34 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, 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. ** 'allgc': all objects not marked for finalization;
  19. ** 'finobj': all objects marked for finalization;
  20. ** 'tobefnz': all objects ready to be finalized;
  21. ** 'fixedgc': all objects that are not to be collected (currently
  22. ** only small strings, such as reserved words).
  23. **
  24. ** Moreover, there is another set of lists that control gray objects.
  25. ** These lists are linked by fields 'gclist'. (All objects that
  26. ** can become gray have such a field. The field is not the same
  27. ** in all objects, but it always has this name.) Any gray object
  28. ** must belong to one of these lists, and all objects in these lists
  29. ** must be gray:
  30. **
  31. ** 'gray': regular gray objects, still waiting to be visited.
  32. ** 'grayagain': objects that must be revisited at the atomic phase.
  33. ** That includes
  34. ** - black objects got in a write barrier;
  35. ** - all kinds of weak tables during propagation phase;
  36. ** - all threads.
  37. ** 'weak': tables with weak values to be cleared;
  38. ** 'ephemeron': ephemeron tables with white->white entries;
  39. ** 'allweak': tables with weak keys and/or weak values to be cleared.
  40. ** The last three lists are used only during the atomic phase.
  41. */
  42. struct lua_longjmp; /* defined in ldo.c */
  43. /*
  44. ** Atomic type (relative to signals) to better ensure that 'lua_sethook'
  45. ** is thread safe
  46. */
  47. #ifdef LUA_USE_ESP8266
  48. # define l_define l_signal_t size_t
  49. #endif
  50. #if !defined(l_signalT)
  51. #include <signal.h>
  52. #define l_signalT sig_atomic_t
  53. #endif
  54. /* extra stack space to handle TM calls and some other extras */
  55. #define EXTRA_STACK 5
  56. #define BASIC_STACK_SIZE (2*LUA_MINSTACK)
  57. /* kinds of Garbage Collection */
  58. #define KGC_NORMAL 0
  59. #define KGC_EMERGENCY 1 /* gc was forced by an allocation failure */
  60. typedef struct stringtable {
  61. TString **hash;
  62. int nuse; /* number of elements */
  63. int size;
  64. } stringtable;
  65. /*
  66. ** Information about a call.
  67. ** When a thread yields, 'func' is adjusted to pretend that the
  68. ** top function has only the yielded values in its stack; in that
  69. ** case, the actual 'func' value is saved in field 'extra'.
  70. ** When a function calls another with a continuation, 'extra' keeps
  71. ** the function index so that, in case of errors, the continuation
  72. ** function can be called with the correct top.
  73. */
  74. typedef struct CallInfo {
  75. StkId func; /* function index in the stack */
  76. StkId top; /* top for this function */
  77. struct CallInfo *previous, *next; /* dynamic call link */
  78. union {
  79. struct { /* only for Lua functions */
  80. StkId base; /* base for this function */
  81. const Instruction *savedpc;
  82. } l;
  83. struct { /* only for C functions */
  84. lua_KFunction k; /* continuation in case of yields */
  85. ptrdiff_t old_errfunc;
  86. lua_KContext ctx; /* context info. in case of yields */
  87. } c;
  88. } u;
  89. ptrdiff_t extra;
  90. short nresults; /* expected number of results from this function */
  91. unsigned short callstatus;
  92. } CallInfo;
  93. /*
  94. ** Bits in CallInfo status
  95. */
  96. #define CIST_OAH (1<<0) /* original value of 'allowhook' */
  97. #define CIST_LUA (1<<1) /* call is running a Lua function */
  98. #define CIST_HOOKED (1<<2) /* call is running a debug hook */
  99. #define CIST_FRESH (1<<3) /* call is running on a fresh invocation
  100. of luaV_execute */
  101. #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */
  102. #define CIST_TAIL (1<<5) /* call was tail called */
  103. #define CIST_HOOKYIELD (1<<6) /* last hook called yielded */
  104. #define CIST_LEQ (1<<7) /* using __lt for __le */
  105. #define CIST_FIN (1<<8) /* call is running a finalizer */
  106. #define isLua(ci) ((ci)->callstatus & CIST_LUA)
  107. /* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */
  108. #define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v))
  109. #define getoah(st) ((st) & CIST_OAH)
  110. /*
  111. ** KeyCache used for resolution of ROTable entries and Cstrings
  112. */
  113. typedef size_t KeyCache;
  114. typedef KeyCache KeyCacheLine[KEYCACHE_M];
  115. /*
  116. ** 'global state', shared by all threads of this state
  117. */
  118. typedef struct FlashHeader LFSHeader;
  119. typedef struct global_State {
  120. lua_Alloc frealloc; /* function to reallocate memory */
  121. void *ud; /* auxiliary data to 'frealloc' */
  122. l_mem totalbytes; /* number of bytes currently allocated - GCdebt */
  123. l_mem GCdebt; /* bytes allocated not yet compensated by the collector */
  124. lu_mem GCmemtrav; /* memory traversed by the GC */
  125. lu_mem GCestimate; /* an estimate of the non-garbage memory in use */
  126. stringtable strt; /* hash table for strings */
  127. TValue l_registry;
  128. unsigned int seed; /* randomized seed for hashes */
  129. lu_byte currentwhite;
  130. lu_byte gcstate; /* state of garbage collector */
  131. lu_byte gckind; /* kind of GC running */
  132. lu_byte gcrunning; /* true if GC is running */
  133. GCObject *allgc; /* list of all collectable objects */
  134. GCObject **sweepgc; /* current position of sweep in list */
  135. GCObject *finobj; /* list of collectable objects with finalizers */
  136. GCObject *gray; /* list of gray objects */
  137. GCObject *grayagain; /* list of objects to be traversed atomically */
  138. GCObject *weak; /* list of tables with weak values */
  139. GCObject *ephemeron; /* list of ephemeron tables (weak keys) */
  140. GCObject *allweak; /* list of all-weak tables */
  141. GCObject *tobefnz; /* list of userdata to be GC */
  142. GCObject *fixedgc; /* list of objects not to be collected */
  143. struct lua_State *twups; /* list of threads with open upvalues */
  144. unsigned int gcfinnum; /* number of finalizers to call in each GC step */
  145. int gcpause; /* size of pause between successive GCs */
  146. int gcstepmul; /* GC 'granularity' */
  147. int stripdefault; /* default stripping level for compilation */
  148. l_mem gcmemfreeboard; /* Free board which triggers EGC */
  149. lua_CFunction panic; /* to be called in unprotected errors */
  150. struct lua_State *mainthread;
  151. const lua_Number *version; /* pointer to version number */
  152. TString *memerrmsg; /* memory-error message */
  153. TString *tmname[TM_N]; /* array with tag-method names */
  154. struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */
  155. stringtable ROstrt; /* Flash-based hash table for RO strings */
  156. TValue LFStable; /* Flash-based Proto main */
  157. LFSHeader *l_LFS; /* Lua Flash Store header */
  158. unsigned int LFSsize; /* size of LFS partition */
  159. KeyCacheLine *cache; /* cache for strings in API */
  160. } global_State;
  161. /*
  162. ** 'per thread' state
  163. */
  164. struct lua_State {
  165. CommonHeader;
  166. unsigned short nci; /* number of items in 'ci' list */
  167. lu_byte status;
  168. StkId top; /* first free slot in the stack */
  169. global_State *l_G;
  170. CallInfo *ci; /* call info for current function */
  171. const Instruction *oldpc; /* last pc traced */
  172. StkId stack_last; /* last free slot in the stack */
  173. StkId stack; /* stack base */
  174. UpVal *openupval; /* list of open upvalues in this stack */
  175. GCObject *gclist;
  176. struct lua_State *twups; /* list of threads with open upvalues */
  177. struct lua_longjmp *errorJmp; /* current error recover point */
  178. CallInfo base_ci; /* CallInfo for first level (C calling Lua) */
  179. volatile lua_Hook hook;
  180. ptrdiff_t errfunc; /* current error handling function (stack index) */
  181. int stacksize;
  182. int basehookcount;
  183. int hookcount;
  184. unsigned short nny; /* number of non-yieldable calls in stack */
  185. unsigned short nCcalls; /* number of nested C calls */
  186. l_signalT hookmask;
  187. lu_byte allowhook;
  188. };
  189. #define G(L) (L->l_G)
  190. /*
  191. ** Union of all collectable objects (only for conversions)
  192. */
  193. union GCUnion {
  194. GCObject gc; /* common header */
  195. struct TString ts;
  196. struct Udata u;
  197. union Closure cl;
  198. struct Table h;
  199. struct ROTable roh;
  200. struct Proto p;
  201. struct lua_State th; /* thread */
  202. };
  203. #define cast_u(o) cast(union GCUnion *, (o))
  204. /* macros to convert a GCObject into a specific value */
  205. #define gco2ts(o) \
  206. check_exp(novariant((gettt(o))) == LUA_TSTRING, &((cast_u(o))->ts))
  207. #define gco2u(o) check_exp((gettt(o)) == LUA_TUSERDATA, &((cast_u(o))->u))
  208. #define gco2lcl(o) check_exp((gettt(o)) == LUA_TLCL, &((cast_u(o))->cl.l))
  209. #define gco2ccl(o) check_exp((gettt(o)) == LUA_TCCL, &((cast_u(o))->cl.c))
  210. #define gco2cl(o) \
  211. check_exp(novariant((gettt(o))) == LUA_TFUNCTION, &((cast_u(o))->cl))
  212. #define gco2t(o) \
  213. check_exp(novariant((gettt(o))) == LUA_TTABLE, &((cast_u(o))->h))
  214. #define gco2rwt(o) check_exp((gettt(o)) == LUA_TTBLRAM, &((cast_u(o))->h))
  215. #define gco2rot(o) check_exp((gettt(o)) == LUA_TTBLROF, &((cast_u(o))->roh))
  216. #define gco2p(o) check_exp((gettt(o)) == LUA_TPROTO, &((cast_u(o))->p))
  217. #define gco2th(o) check_exp((gettt(o)) == LUA_TTHREAD, &((cast_u(o))->th))
  218. /* macro to convert a Lua object into a GCObject */
  219. #define obj2gco(v) \
  220. check_exp(novariant((v)->tt) < LUA_TDEADKEY, (&(cast_u(v)->gc)))
  221. /* actual number of total bytes allocated */
  222. #define gettotalbytes(g) cast(lu_mem, (g)->totalbytes + (g)->GCdebt)
  223. LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
  224. LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
  225. LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
  226. LUAI_FUNC void luaE_freeCI (lua_State *L);
  227. LUAI_FUNC void luaE_shrinkCI (lua_State *L);
  228. LUAI_FUNC KeyCache *luaE_getcache (int cl);
  229. #endif