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