Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4680 right-hear 1
/*
2
** $Id: lstate.h,v 2.68 2010/10/29 17:52:46 roberto Exp $
3
** Global State
4
** See Copyright Notice in lua.h
5
*/
6
 
7
#ifndef lstate_h
8
#define lstate_h
9
 
10
#include "lua.h"
11
 
12
#include "lobject.h"
13
#include "ltm.h"
14
#include "lzio.h"
15
 
16
 
17
/*
18
 
19
** Some notes about garbage-collected objects:  All objects in Lua must
20
** be kept somehow accessible until being freed.
21
**
22
** Lua keeps most objects linked in list g->allgc. The link uses field
23
** 'next' of the CommonHeader.
24
**
25
** Strings are kept in several lists headed by the array g->strt.hash.
26
**
27
** Open upvalues are not subject to independent garbage collection. They
28
** are collected together with their respective threads. Lua keeps a
29
** double-linked list with all open upvalues (g->uvhead) so that it can
30
** mark objects referred by them. (They are always gray, so they must
31
** be remarked in the atomic step. Usually their contents would be marked
32
** when traversing the respective threads, but the thread may already be
33
** dead, while the upvalue is still accessible through closures.)
34
**
35
** Userdata with finalizers are kept in the list g->udgc.
36
**
37
** The list g->tobefnz links all userdata being finalized.
38
 
39
*/
40
 
41
 
42
struct lua_longjmp;  /* defined in ldo.c */
43
 
44
 
45
 
46
/* extra stack space to handle TM calls and some other extras */
47
#define EXTRA_STACK   5
48
 
49
 
50
#define BASIC_CI_SIZE           8
51
 
52
#define BASIC_STACK_SIZE        (2*LUA_MINSTACK)
53
 
54
 
55
/* kinds of Garbage Collection */
56
#define KGC_NORMAL	0
57
#define KGC_EMERGENCY	1	/* gc was forced by an allocation failure */
58
#define KGC_GEN		2	/* generational collection */
59
 
60
 
61
typedef struct stringtable {
62
  GCObject **hash;
63
  lu_int32 nuse;  /* number of elements */
64
  int size;
65
} stringtable;
66
 
67
 
68
/*
69
** information about a call
70
*/
71
typedef struct CallInfo {
72
  StkId func;  /* function index in the stack */
73
  StkId	top;  /* top for this function */
74
  struct CallInfo *previous, *next;  /* dynamic call link */
75
  short nresults;  /* expected number of results from this function */
76
  lu_byte callstatus;
77
  union {
78
    struct {  /* only for Lua functions */
79
      StkId base;  /* base for this function */
80
      const Instruction *savedpc;
81
    } l;
82
    struct {  /* only for C functions */
83
      int ctx;  /* context info. in case of yields */
84
      lua_CFunction k;  /* continuation in case of yields */
85
      ptrdiff_t old_errfunc;
86
      ptrdiff_t extra;
87
      lu_byte old_allowhook;
88
      lu_byte status;
89
    } c;
90
  } u;
91
} CallInfo;
92
 
93
 
94
/*
95
** Bits in CallInfo status
96
*/
97
#define CIST_LUA	(1<<0)	/* call is running a Lua function */
98
#define CIST_HOOKED	(1<<1)	/* call is running a debug hook */
99
#define CIST_REENTRY	(1<<2)	/* call is running on same invocation of
100
                                   luaV_execute of previous call */
101
#define CIST_YIELDED	(1<<3)	/* call reentered after suspension */
102
#define CIST_YPCALL	(1<<4)	/* call is a yieldable protected call */
103
#define CIST_STAT	(1<<5)	/* call has an error status (pcall) */
104
#define CIST_TAIL	(1<<6)	/* call was tail called */
105
 
106
 
107
#define ci_func(ci)	(clvalue((ci)->func))
108
#define isLua(ci)	((ci)->callstatus & CIST_LUA)
109
 
110
 
111
/*
112
** `global state', shared by all threads of this state
113
*/
114
typedef struct global_State {
115
  lua_Alloc frealloc;  /* function to reallocate memory */
116
  void *ud;         /* auxiliary data to `frealloc' */
117
  lu_mem totalbytes;  /* number of bytes currently allocated */
118
  l_mem GCdebt;  /* when positive, run a GC step */
119
  lu_mem lastmajormem;  /* memory in use after last major collection */
120
  stringtable strt;  /* hash table for strings */
121
  TValue l_registry;
122
  unsigned short nCcalls;  /* number of nested C calls */
123
  lu_byte currentwhite;
124
  lu_byte gcstate;  /* state of garbage collector */
125
  lu_byte gckind;  /* kind of GC running */
126
  int sweepstrgc;  /* position of sweep in `strt' */
127
  GCObject *allgc;  /* list of all collectable objects */
128
  GCObject *udgc;  /* list of collectable userdata with finalizers */
129
  GCObject **sweepgc;  /* current position of sweep */
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;  /* how much to wait for a major GC (only in gen. mode) */
140
  int gcstepmul;  /* GC `granularity' */
141
  lua_CFunction panic;  /* to be called in unprotected errors */
142
  struct lua_State *mainthread;
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 {
154
  CommonHeader;
155
  lu_byte status;
156
  StkId top;  /* first free slot in the stack */
157
  global_State *l_G;
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 */
162
  int stacksize;
163
  unsigned short nny;  /* number of non-yieldable calls in stack */
164
  lu_byte hookmask;
165
  lu_byte allowhook;
166
  int basehookcount;
167
  int hookcount;
168
  lua_Hook hook;
169
  GCObject *openupval;  /* list of open upvalues in this stack */
170
  GCObject *gclist;
171
  struct lua_longjmp *errorJmp;  /* current error recover point */
172
  ptrdiff_t errfunc;  /* current error handling function (stack index) */
173
  CallInfo base_ci;  /* CallInfo for first level (C calling Lua) */
174
};
175
 
176
 
177
#define G(L)	(L->l_G)
178
 
179
 
180
/*
181
** Union of all collectable objects
182
*/
183
union GCObject {
184
  GCheader gch;  /* common header */
185
  union TString ts;
186
  union Udata u;
187
  union Closure cl;
188
  struct Table h;
189
  struct Proto p;
190
  struct UpVal uv;
191
  struct lua_State th;  /* thread */
192
};
193
 
194
 
195
#define gch(o)		(&(o)->gch)
196
 
197
/* macros to convert a GCObject into a specific value */
198
#define rawgco2ts(o)	check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts))
199
#define gco2ts(o)	(&rawgco2ts(o)->tsv)
200
#define rawgco2u(o)	check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u))
201
#define gco2u(o)	(&rawgco2u(o)->uv)
202
#define gco2cl(o)	check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl))
203
#define gco2t(o)	check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h))
204
#define gco2p(o)	check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p))
205
#define gco2uv(o)	check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv))
206
#define gco2th(o)	check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))
207
 
208
/* macro to convert any Lua object into a GCObject */
209
#define obj2gco(v)	(cast(GCObject *, (v)))
210
 
211
 
212
LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
213
LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
214
LUAI_FUNC void luaE_freeCI (lua_State *L);
215
 
216
 
217
#endif
218