~widelands-dev/widelands/remove-savegame-compatibility-after-economy-change

« back to all changes in this revision

Viewing changes to src/third_party/eris/lstate.h

Merged trunk.

Show diffs side-by-side

added added

removed removed

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