Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
lstate.c File Reference
#include <stddef.h>
#include <string.h>
#include "lua.h"
#include "lapi.h"
#include "ldebug.h"
#include "ldo.h"
#include "lfunc.h"
#include "lgc.h"
#include "llex.h"
#include "lmem.h"
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
#include "ltm.h"
#include <time.h>

Go to the source code of this file.

Data Structures

struct  LX
 
struct  LG
 

Macros

#define lstate_c
 
#define LUA_CORE
 
#define LUAI_GCPAUSE   200 /* 200% */
 
#define LUAI_GCMAJOR   200 /* 200% */
 
#define LUAI_GCMUL   200 /* GC runs 'twice the speed' of memory allocation */
 
#define MEMERRMSG   "not enough memory"
 
#define luai_makeseed()   cast(unsigned int, time(NULL))
 
#define fromstate(L)   (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
 
#define addbuff(b, p, e)
 

Typedefs

typedef struct LX LX
 
typedef struct LG LG
 

Functions

static unsigned int makeseed (lua_State *L)
 
void luaE_setdebt (global_State *g, l_mem debt)
 
CallInfoluaE_extendCI (lua_State *L)
 
void luaE_freeCI (lua_State *L)
 
static void stack_init (lua_State *L1, lua_State *L)
 
static void freestack (lua_State *L)
 
static void init_registry (lua_State *L, global_State *g)
 
static void f_luaopen (lua_State *L, void *ud)
 
static void preinit_state (lua_State *L, global_State *g)
 
static void close_state (lua_State *L)
 
LUA_API lua_Statelua_newthread (lua_State *L)
 
void luaE_freethread (lua_State *L, lua_State *L1)
 
LUA_API lua_Statelua_newstate (lua_Alloc f, void *ud)
 
LUA_API void lua_close (lua_State *L)
 

Macro Definition Documentation

◆ addbuff

#define addbuff ( b,
p,
e )
Value:
{ size_t t = cast(size_t, e); \
memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); }
#define cast(t, exp)

Definition at line 85 of file lua-5.2.4/src/lstate.c.

85#define addbuff(b,p,e) \
86 { size_t t = cast(size_t, e); \
87 memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); }

Referenced by makeseed().

◆ fromstate

#define fromstate ( L)    (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))

Definition at line 77 of file lua-5.2.4/src/lstate.c.

Referenced by close_state(), and luaE_freethread().

◆ lstate_c

#define lstate_c

Definition at line 11 of file lua-5.2.4/src/lstate.c.

◆ LUA_CORE

#define LUA_CORE

Definition at line 12 of file lua-5.2.4/src/lstate.c.

◆ LUAI_GCMAJOR

#define LUAI_GCMAJOR   200 /* 200% */

Definition at line 34 of file lua-5.2.4/src/lstate.c.

Referenced by lua_newstate().

◆ LUAI_GCMUL

#define LUAI_GCMUL   200 /* GC runs 'twice the speed' of memory allocation */

Definition at line 38 of file lua-5.2.4/src/lstate.c.

Referenced by lua_newstate().

◆ LUAI_GCPAUSE

#define LUAI_GCPAUSE   200 /* 200% */

Definition at line 30 of file lua-5.2.4/src/lstate.c.

Referenced by lua_newstate().

◆ luai_makeseed

#define luai_makeseed ( )    cast(unsigned int, time(NULL))

Definition at line 51 of file lua-5.2.4/src/lstate.c.

Referenced by lua_newstate(), and makeseed().

◆ MEMERRMSG

#define MEMERRMSG   "not enough memory"

Definition at line 42 of file lua-5.2.4/src/lstate.c.

Referenced by f_luaopen().

Typedef Documentation

◆ LG

typedef struct LG LG

◆ LX

typedef struct LX LX

Function Documentation

◆ close_state()

static void close_state ( lua_State * L)
static

Definition at line 223 of file lua-5.2.4/src/lstate.c.

223 {
224 global_State *g = G(L);
225 luaF_close(L, L->stack); /* close all upvalues for this thread */
226 luaC_freeallobjects(L); /* collect all objects */
227 if (g->version) /* closing a fully built state? */
229 luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
230 luaZ_freebuffer(L, &g->buff);
231 freestack(L);
232 lua_assert(gettotalbytes(g) == sizeof(LG));
233 (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
234}
void luaF_close(lua_State *L, StkId level)
#define lua_assert(c)
#define luaM_freearray(L, b, n, t)
#define G(L)
#define luai_userstateclose(L)
#define luaZ_freebuffer(L, buff)
void luaC_freeallobjects(lua_State *L)
#define fromstate(L)
static void freestack(lua_State *L)
struct LG LG
#define gettotalbytes(g)
const lua_Number * version

References global_State::buff, global_State::frealloc, freestack(), fromstate, G, gettotalbytes, lua_assert, luaC_freeallobjects(), luaF_close(), luai_userstateclose, luaM_freearray, luaZ_freebuffer, lua_State::stack, global_State::ud, and global_State::version.

Referenced by lua_close(), and lua_newstate().

◆ f_luaopen()

static void f_luaopen ( lua_State * L,
void * ud )
static

Definition at line 183 of file lua-5.2.4/src/lstate.c.

183 {
184 global_State *g = G(L);
185 UNUSED(ud);
186 stack_init(L, L); /* init stack */
187 init_registry(L, g);
188 luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
189 luaT_init(L);
190 luaX_init(L);
191 /* pre-create memory-error message */
193 luaS_fix(g->memerrmsg); /* it should never be collected */
194 g->gcrunning = 1; /* allow gc */
197}
#define NULL
Definition gmacros.h:924
#define UNUSED(x)
#define MINSTRTABSIZE
void luaS_resize(lua_State *L, int newsize)
#define luaS_newliteral(L, s)
#define luaS_fix(s)
void luaT_init(lua_State *L)
#define luai_userstateopen(L)
LUA_API const lua_Number * lua_version(lua_State *L)
#define MEMERRMSG
static void init_registry(lua_State *L, global_State *g)
static void stack_init(lua_State *L1, lua_State *L)
LUAI_FUNC void luaX_init(lua_State *L)
Definition noparser.c:21

References G, global_State::gcrunning, init_registry(), lua_version(), luai_userstateopen, luaS_fix, luaS_newliteral, luaS_resize(), luaT_init(), luaX_init(), MEMERRMSG, global_State::memerrmsg, MINSTRTABSIZE, NULL, stack_init(), UNUSED, and global_State::version.

Referenced by lua_newstate().

◆ freestack()

static void freestack ( lua_State * L)
static

Definition at line 153 of file lua-5.2.4/src/lstate.c.

153 {
154 if (L->stack == NULL)
155 return; /* stack not completely built yet */
156 L->ci = &L->base_ci; /* free the entire 'ci' list */
157 luaE_freeCI(L);
158 luaM_freearray(L, L->stack, L->stacksize); /* free stack array */
159}
void luaE_freeCI(lua_State *L)

References lua_State::base_ci, lua_State::ci, luaE_freeCI(), luaM_freearray, NULL, lua_State::stack, and lua_State::stacksize.

Referenced by close_state(), and luaE_freethread().

◆ init_registry()

static void init_registry ( lua_State * L,
global_State * g )
static

Definition at line 165 of file lua-5.2.4/src/lstate.c.

165 {
166 TValue mt;
167 /* create registry */
168 Table *registry = luaH_new(L);
171 /* registry[LUA_RIDX_MAINTHREAD] = L */
172 setthvalue(L, &mt, L);
174 /* registry[LUA_RIDX_GLOBALS] = table of globals */
175 sethvalue(L, &mt, luaH_new(L));
177}
#define setthvalue(L, obj, x)
#define sethvalue(L, obj, x)
#define registry(L)
Table * luaH_new(lua_State *L, int narray, int nhash)
void luaH_resize(lua_State *L, Table *t, int nasize, int nhsize)
void luaH_setint(lua_State *L, Table *t, int key, TValue *value)
#define LUA_RIDX_GLOBALS
#define LUA_RIDX_LAST
#define LUA_RIDX_MAINTHREAD

References global_State::l_registry, LUA_RIDX_GLOBALS, LUA_RIDX_LAST, LUA_RIDX_MAINTHREAD, luaH_new(), luaH_resize(), luaH_setint(), registry, sethvalue, and setthvalue.

Referenced by f_luaopen().

◆ lua_close()

LUA_API void lua_close ( lua_State * L)

Definition at line 317 of file lua-5.2.4/src/lstate.c.

317 {
318 L = G(L)->mainthread; /* only the main thread can be closed */
319 lua_lock(L);
320 close_state(L);
321}
#define lua_lock(L)
static void close_state(lua_State *L)

References close_state(), G, and lua_lock.

◆ lua_newstate()

LUA_API lua_State * lua_newstate ( lua_Alloc f,
void * ud )

Definition at line 266 of file lua-5.2.4/src/lstate.c.

266 {
267 int i;
268 lua_State *L;
269 global_State *g;
270 LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
271 if (l == NULL) return NULL;
272 L = &l->l.l;
273 g = &l->g;
274 L->next = NULL;
275 L->tt = LUA_TTHREAD;
277 L->marked = luaC_white(g);
278 g->gckind = KGC_NORMAL;
279 preinit_state(L, g);
280 g->frealloc = f;
281 g->ud = ud;
282 g->mainthread = L;
283 g->seed = makeseed(L);
284 g->uvhead.u.l.prev = &g->uvhead;
285 g->uvhead.u.l.next = &g->uvhead;
286 g->gcrunning = 0; /* no GC while building state */
287 g->GCestimate = 0;
288 g->strt.size = 0;
289 g->strt.nuse = 0;
290 g->strt.hash = NULL;
292 luaZ_initbuffer(L, &g->buff);
293 g->panic = NULL;
294 g->version = NULL;
295 g->gcstate = GCSpause;
296 g->allgc = NULL;
297 g->finobj = NULL;
298 g->tobefnz = NULL;
299 g->sweepgc = g->sweepfin = NULL;
300 g->gray = g->grayagain = NULL;
301 g->weak = g->ephemeron = g->allweak = NULL;
302 g->totalbytes = sizeof(LG);
303 g->GCdebt = 0;
307 for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
309 /* memory allocation error: free partial state */
310 close_state(L);
311 L = NULL;
312 }
313 return L;
314}
int luaD_rawrunprotected(lua_State *L, Pfunc f, void *ud)
#define bit2mask(b1, b2)
#define WHITE0BIT
#define FIXEDBIT
#define GCSpause
#define luaC_white(g)
#define setnilvalue(obj)
#define LUA_TTHREAD
#define luaZ_initbuffer(L, buff)
#define LUAI_GCMAJOR
static unsigned int makeseed(lua_State *L)
#define LUAI_GCPAUSE
static void f_luaopen(lua_State *L, void *ud)
static void preinit_state(lua_State *L, global_State *g)
#define LUAI_GCMUL
#define KGC_NORMAL
#define LUA_OK
#define LUA_NUMTAGS
global_State g
lua_State l
struct UpVal * prev
union UpVal::@48 u
struct UpVal * next
struct UpVal::@48::@49 l
struct lua_State * mainthread
lua_CFunction panic
struct Table * mt[NUM_TAGS]

References global_State::allgc, global_State::allweak, bit2mask, global_State::buff, cast, close_state(), global_State::currentwhite, global_State::ephemeron, f_luaopen(), global_State::finobj, FIXEDBIT, global_State::frealloc, LG::g, global_State::GCdebt, global_State::GCestimate, global_State::gckind, global_State::gcmajorinc, global_State::gcpause, global_State::gcrunning, GCSpause, global_State::gcstate, global_State::gcstepmul, global_State::gray, global_State::grayagain, stringtable::hash, KGC_NORMAL, UpVal::l, LG::l, global_State::l_registry, LUA_NUMTAGS, LUA_OK, LUA_TTHREAD, luaC_white, luaD_rawrunprotected(), LUAI_GCMAJOR, LUAI_GCMUL, LUAI_GCPAUSE, luaZ_initbuffer, global_State::mainthread, makeseed(), global_State::mt, UpVal::next, NULL, stringtable::nuse, global_State::panic, preinit_state(), UpVal::prev, global_State::seed, setnilvalue, stringtable::size, global_State::strt, global_State::sweepfin, global_State::sweepgc, global_State::tobefnz, global_State::totalbytes, UpVal::u, global_State::ud, global_State::uvhead, global_State::version, global_State::weak, and WHITE0BIT.

Referenced by makeseed().

◆ lua_newthread()

LUA_API lua_State * lua_newthread ( lua_State * L)

Definition at line 237 of file lua-5.2.4/src/lstate.c.

237 {
238 lua_State *L1;
239 lua_lock(L);
240 luaC_checkGC(L);
241 L1 = &luaC_newobj(L, LUA_TTHREAD, sizeof(LX), NULL, offsetof(LX, l))->th;
242 setthvalue(L, L->top, L1);
243 api_incr_top(L);
244 preinit_state(L1, G(L));
245 L1->hookmask = L->hookmask;
247 L1->hook = L->hook;
248 resethookcount(L1);
250 stack_init(L1, L); /* init stack */
251 lua_unlock(L);
252 return L1;
253}
#define api_incr_top(L)
#define resethookcount(L)
#define luaC_checkGC(L)
#define lua_unlock(L)
#define luai_userstatethread(L, L1)
GCObject * luaC_newobj(lua_State *L, int tt, size_t sz, GCObject **list, int offset)
#define offsetof(STRUCTURE, FIELD)
Definition sqlite3.c:14256
struct lua_State th

References api_incr_top, lua_State::basehookcount, G, lua_State::hook, lua_State::hookmask, lua_lock, LUA_TTHREAD, lua_unlock, luaC_checkGC, luaC_newobj(), luai_userstatethread, NULL, offsetof, preinit_state(), resethookcount, setthvalue, stack_init(), GCObject::th, and lua_State::top.

◆ luaE_extendCI()

CallInfo * luaE_extendCI ( lua_State * L)

Definition at line 112 of file lua-5.2.4/src/lstate.c.

112 {
113 CallInfo *ci = luaM_new(L, CallInfo);
114 lua_assert(L->ci->next == NULL);
115 L->ci->next = ci;
116 ci->previous = L->ci;
117 ci->next = NULL;
118 return ci;
119}
#define luaM_new(L, t)
struct CallInfo * previous
struct CallInfo * next

References lua_State::ci, lua_assert, luaM_new, CallInfo::next, NULL, and CallInfo::previous.

◆ luaE_freeCI()

void luaE_freeCI ( lua_State * L)

Definition at line 122 of file lua-5.2.4/src/lstate.c.

122 {
123 CallInfo *ci = L->ci;
124 CallInfo *next = ci->next;
125 ci->next = NULL;
126 while ((ci = next) != NULL) {
127 next = ci->next;
128 luaM_free(L, ci);
129 }
130}
#define next(ls)
#define luaM_free(L, b)

References lua_State::ci, luaM_free, next, CallInfo::next, and NULL.

Referenced by freestack(), luaD_shrinkstack(), and sweepthread().

◆ luaE_freethread()

void luaE_freethread ( lua_State * L,
lua_State * L1 )

Definition at line 256 of file lua-5.2.4/src/lstate.c.

256 {
257 LX *l = fromstate(L1);
258 luaF_close(L1, L1->stack); /* close all upvalues for this thread */
259 lua_assert(L1->openupval == NULL);
260 luai_userstatefree(L, L1);
261 freestack(L1);
262 luaM_free(L, l);
263}
#define luai_userstatefree(L)
GCObject * openupval

References freestack(), fromstate, lua_assert, luaF_close(), luai_userstatefree, luaM_free, NULL, lua_State::openupval, and lua_State::stack.

◆ luaE_setdebt()

void luaE_setdebt ( global_State * g,
l_mem debt )

Definition at line 106 of file lua-5.2.4/src/lstate.c.

106 {
107 g->totalbytes -= (debt - g->GCdebt);
108 g->GCdebt = debt;
109}

References global_State::GCdebt, and global_State::totalbytes.

Referenced by incstep(), incstep(), lua_gc(), lua_gc(), luaC_step(), setminordebt(), setpause(), setpause(), and setpause().

◆ makeseed()

static unsigned int makeseed ( lua_State * L)
static

Definition at line 89 of file lua-5.2.4/src/lstate.c.

89 {
90 char buff[4 * sizeof(size_t)];
91 unsigned int h = luai_makeseed();
92 int p = 0;
93 addbuff(buff, p, L); /* heap variable */
94 addbuff(buff, p, &h); /* local variable */
95 addbuff(buff, p, luaO_nilobject); /* global variable */
96 addbuff(buff, p, &lua_newstate); /* public function */
97 lua_assert(p == sizeof(buff));
98 return luaS_hash(buff, p, h);
99}
#define luaO_nilobject
LUA_API lua_State * lua_newstate(lua_Alloc f, void *ud)
#define luai_makeseed()
#define addbuff(b, p, e)
unsigned int luaS_hash(const char *str, size_t l, unsigned int seed)

References addbuff, lua_assert, lua_newstate(), luai_makeseed, luaO_nilobject, and luaS_hash().

Referenced by lua_newstate().

◆ preinit_state()

static void preinit_state ( lua_State * L,
global_State * g )
static

Definition at line 204 of file lua-5.2.4/src/lstate.c.

204 {
205 G(L) = g;
206 L->stack = NULL;
207 L->ci = NULL;
208 L->stacksize = 0;
209 L->errorJmp = NULL;
210 L->nCcalls = 0;
211 L->hook = NULL;
212 L->hookmask = 0;
213 L->basehookcount = 0;
214 L->allowhook = 1;
216 L->openupval = NULL;
217 L->nny = 1;
218 L->status = LUA_OK;
219 L->errfunc = 0;
220}
unsigned short nny
struct lua_longjmp * errorJmp
unsigned short nCcalls

References lua_State::allowhook, lua_State::basehookcount, lua_State::ci, lua_State::errfunc, lua_State::errorJmp, G, lua_State::hook, lua_State::hookmask, LUA_OK, lua_State::nCcalls, lua_State::nny, NULL, lua_State::openupval, resethookcount, lua_State::stack, lua_State::stacksize, and lua_State::status.

Referenced by lua_newstate(), and lua_newthread().

◆ stack_init()

static void stack_init ( lua_State * L1,
lua_State * L )
static

Definition at line 133 of file lua-5.2.4/src/lstate.c.

133 {
134 int i; CallInfo *ci;
135 /* initialize stack array */
138 for (i = 0; i < BASIC_STACK_SIZE; i++)
139 setnilvalue(L1->stack + i); /* erase new stack */
140 L1->top = L1->stack;
141 L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;
142 /* initialize first ci */
143 ci = &L1->base_ci;
144 ci->next = ci->previous = NULL;
145 ci->callstatus = 0;
146 ci->func = L1->top;
147 setnilvalue(L1->top++); /* 'function' entry for this 'ci' */
148 ci->top = L1->top + LUA_MINSTACK;
149 L1->ci = ci;
150}
#define luaM_newvector(L, n, t)
#define BASIC_STACK_SIZE
#define EXTRA_STACK
#define LUA_MINSTACK

References lua_State::base_ci, BASIC_STACK_SIZE, CallInfo::callstatus, lua_State::ci, EXTRA_STACK, CallInfo::func, LUA_MINSTACK, luaM_newvector, CallInfo::next, NULL, CallInfo::previous, setnilvalue, lua_State::stack, lua_State::stack_last, lua_State::stacksize, CallInfo::top, and lua_State::top.

Referenced by f_luaopen(), and lua_newthread().