Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
lstate.c File Reference
#include "lprefix.h"
#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_GCMUL   200 /* GC runs 'twice the speed' of memory allocation */
 
#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)
 
void luaE_shrinkCI (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_thread (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(b + p, &t, sizeof(t)); p += sizeof(t); }
#define cast(t, exp)

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

77#define addbuff(b,p,e) \
78 { size_t t = cast(size_t, e); \
79 memcpy(b + 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 70 of file lua-5.3.6/src/lstate.c.

Referenced by close_state(), and luaE_freethread().

◆ lstate_c

#define lstate_c

Definition at line 7 of file lua-5.3.6/src/lstate.c.

◆ LUA_CORE

#define LUA_CORE

Definition at line 8 of file lua-5.3.6/src/lstate.c.

◆ LUAI_GCMUL

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

Definition at line 36 of file lua-5.3.6/src/lstate.c.

Referenced by lua_newstate().

◆ LUAI_GCPAUSE

#define LUAI_GCPAUSE   200 /* 200% */

Definition at line 32 of file lua-5.3.6/src/lstate.c.

Referenced by lua_newstate().

◆ luai_makeseed

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

Definition at line 46 of file lua-5.3.6/src/lstate.c.

Referenced by makeseed().

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 242 of file lua-5.3.6/src/lstate.c.

242 {
243 global_State *g = G(L);
244 luaF_close(L, L->stack); /* close all upvalues for this thread */
245 luaC_freeallobjects(L); /* collect all objects */
246 if (g->version) /* closing a fully built state? */
248 luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
249 freestack(L);
250 lua_assert(gettotalbytes(g) == sizeof(LG));
251 (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
252}
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)
void luaC_freeallobjects(lua_State *L)
#define gettotalbytes(g)
#define fromstate(L)
static void freestack(lua_State *L)
struct LG LG
const lua_Number * version

References global_State::frealloc, freestack(), fromstate, G, gettotalbytes, lua_assert, luaC_freeallobjects(), luaF_close(), luai_userstateclose, luaM_freearray, 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 203 of file lua-5.3.6/src/lstate.c.

203 {
204 global_State *g = G(L);
205 UNUSED(ud);
206 stack_init(L, L); /* init stack */
207 init_registry(L, g);
208 luaS_init(L);
209 luaT_init(L);
210 luaX_init(L);
211 g->gcrunning = 1; /* allow gc */
214}
#define NULL
Definition gmacros.h:924
#define UNUSED(x)
void luaT_init(lua_State *L)
#define luai_userstateopen(L)
LUA_API const lua_Number * lua_version(lua_State *L)
static void init_registry(lua_State *L, global_State *g)
static void stack_init(lua_State *L1, lua_State *L)
void luaS_init(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_init(), luaT_init(), luaX_init(), NULL, stack_init(), UNUSED, and global_State::version.

Referenced by lua_newstate().

◆ freestack()

static void freestack ( lua_State * L)
static

Definition at line 171 of file lua-5.3.6/src/lstate.c.

171 {
172 if (L->stack == NULL)
173 return; /* stack not completely built yet */
174 L->ci = &L->base_ci; /* free the entire 'ci' list */
175 luaE_freeCI(L);
176 lua_assert(L->nci == 0);
177 luaM_freearray(L, L->stack, L->stacksize); /* free stack array */
178}
void luaE_freeCI(lua_State *L)
unsigned short nci

References lua_State::base_ci, lua_State::ci, lua_assert, luaE_freeCI(), luaM_freearray, lua_State::nci, 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 184 of file lua-5.3.6/src/lstate.c.

184 {
185 TValue temp;
186 /* create registry */
187 Table *registry = luaH_new(L);
190 /* registry[LUA_RIDX_MAINTHREAD] = L */
191 setthvalue(L, &temp, L); /* temp = L */
193 /* registry[LUA_RIDX_GLOBALS] = table of globals */
194 sethvalue(L, &temp, luaH_new(L)); /* temp = new table (global table) */
196}
#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 341 of file lua-5.3.6/src/lstate.c.

341 {
342 L = G(L)->mainthread; /* only the main thread can be closed */
343 lua_lock(L);
344 close_state(L);
345}
#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 295 of file lua-5.3.6/src/lstate.c.

295 {
296 int i;
297 lua_State *L;
298 global_State *g;
299 LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
300 if (l == NULL) return NULL;
301 L = &l->l.l;
302 g = &l->g;
303 L->next = NULL;
304 L->tt = LUA_TTHREAD;
306 L->marked = luaC_white(g);
307 preinit_thread(L, g);
308 g->frealloc = f;
309 g->ud = ud;
310 g->mainthread = L;
311 g->seed = makeseed(L);
312 g->gcrunning = 0; /* no GC while building state */
313 g->GCestimate = 0;
314 g->strt.size = g->strt.nuse = 0;
315 g->strt.hash = NULL;
317 g->panic = NULL;
318 g->version = NULL;
319 g->gcstate = GCSpause;
320 g->gckind = KGC_NORMAL;
321 g->allgc = g->finobj = g->tobefnz = g->fixedgc = NULL;
322 g->sweepgc = NULL;
323 g->gray = g->grayagain = NULL;
324 g->weak = g->ephemeron = g->allweak = NULL;
325 g->twups = NULL;
326 g->totalbytes = sizeof(LG);
327 g->GCdebt = 0;
328 g->gcfinnum = 0;
331 for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
333 /* memory allocation error: free partial state */
334 close_state(L);
335 L = NULL;
336 }
337 return L;
338}
int luaD_rawrunprotected(lua_State *L, Pfunc f, void *ud)
#define WHITE0BIT
#define GCSpause
#define bitmask(b)
#define luaC_white(g)
#define setnilvalue(obj)
#define LUA_TTHREAD
#define KGC_NORMAL
#define LUA_OK
#define LUA_NUMTAGS
static unsigned int makeseed(lua_State *L)
#define LUAI_GCPAUSE
static void f_luaopen(lua_State *L, void *ud)
#define LUAI_GCMUL
static void preinit_thread(lua_State *L, global_State *g)
global_State g
lua_State l
struct lua_State * twups
struct lua_State * mainthread
lua_CFunction panic
struct Table * mt[NUM_TAGS]
unsigned int gcfinnum

References global_State::allgc, global_State::allweak, bitmask, cast, close_state(), global_State::currentwhite, global_State::ephemeron, f_luaopen(), global_State::finobj, global_State::fixedgc, global_State::frealloc, LG::g, global_State::GCdebt, global_State::GCestimate, global_State::gcfinnum, global_State::gckind, global_State::gcpause, global_State::gcrunning, GCSpause, global_State::gcstate, global_State::gcstepmul, global_State::gray, global_State::grayagain, stringtable::hash, KGC_NORMAL, LG::l, global_State::l_registry, LUA_NUMTAGS, LUA_OK, LUA_TTHREAD, luaC_white, luaD_rawrunprotected(), LUAI_GCMUL, LUAI_GCPAUSE, global_State::mainthread, makeseed(), global_State::mt, NULL, stringtable::nuse, global_State::panic, preinit_thread(), global_State::seed, setnilvalue, stringtable::size, global_State::strt, global_State::sweepgc, global_State::tobefnz, global_State::totalbytes, global_State::twups, global_State::ud, 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 255 of file lua-5.3.6/src/lstate.c.

255 {
256 global_State *g = G(L);
257 lua_State *L1;
258 lua_lock(L);
259 luaC_checkGC(L);
260 /* create new thread */
261 L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l;
262 L1->marked = luaC_white(g);
263 L1->tt = LUA_TTHREAD;
264 /* link it on list 'allgc' */
265 L1->next = g->allgc;
266 g->allgc = obj2gco(L1);
267 /* anchor it on L stack */
268 setthvalue(L, L->top, L1);
269 api_incr_top(L);
270 preinit_thread(L1, g);
271 L1->hookmask = L->hookmask;
273 L1->hook = L->hook;
274 resethookcount(L1);
275 /* initialize L1 extra space */
279 stack_init(L1, L); /* init stack */
280 lua_unlock(L);
281 return L1;
282}
#define api_incr_top(L)
#define resethookcount(L)
#define luaC_checkGC(L)
#define lua_unlock(L)
#define obj2gco(v)
#define luai_userstatethread(L, L1)
#define luaM_newobject(L, tag, s)
#define lua_getextraspace(L)
#define LUA_EXTRASPACE

References global_State::allgc, api_incr_top, lua_State::basehookcount, cast, G, lua_State::hook, lua_State::hookmask, LUA_EXTRASPACE, lua_getextraspace, lua_lock, LUA_TTHREAD, lua_unlock, luaC_checkGC, luaC_white, luai_userstatethread, luaM_newobject, global_State::mainthread, obj2gco, preinit_thread(), resethookcount, setthvalue, stack_init(), and lua_State::top.

◆ luaE_extendCI()

CallInfo * luaE_extendCI ( lua_State * L)

Definition at line 108 of file lua-5.3.6/src/lstate.c.

108 {
109 CallInfo *ci = luaM_new(L, CallInfo);
110 lua_assert(L->ci->next == NULL);
111 L->ci->next = ci;
112 ci->previous = L->ci;
113 ci->next = NULL;
114 L->nci++;
115 return ci;
116}
#define luaM_new(L, t)
struct CallInfo * previous
struct CallInfo * next

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

◆ luaE_freeCI()

void luaE_freeCI ( lua_State * L)

Definition at line 122 of file lua-5.3.6/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 L->nci--;
130 }
131}
#define next(ls)
#define luaM_free(L, b)

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

Referenced by freestack().

◆ luaE_freethread()

void luaE_freethread ( lua_State * L,
lua_State * L1 )

Definition at line 285 of file lua-5.3.6/src/lstate.c.

285 {
286 LX *l = fromstate(L1);
287 luaF_close(L1, L1->stack); /* close all upvalues for this thread */
288 lua_assert(L1->openupval == NULL);
289 luai_userstatefree(L, L1);
290 freestack(L1);
291 luaM_free(L, l);
292}
#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 98 of file lua-5.3.6/src/lstate.c.

98 {
99 l_mem tb = gettotalbytes(g);
100 lua_assert(tb > 0);
101 if (debt < tb - MAX_LMEM)
102 debt = tb - MAX_LMEM; /* will make 'totalbytes == MAX_LMEM' */
103 g->totalbytes = tb - debt;
104 g->GCdebt = debt;
105}
LUAI_MEM l_mem
#define MAX_LMEM

References global_State::GCdebt, gettotalbytes, lua_assert, MAX_LMEM, and global_State::totalbytes.

◆ luaE_shrinkCI()

void luaE_shrinkCI ( lua_State * L)

Definition at line 137 of file lua-5.3.6/src/lstate.c.

137 {
138 CallInfo *ci = L->ci;
139 CallInfo *next2; /* next's next */
140 /* while there are two nexts */
141 while (ci->next != NULL && (next2 = ci->next->next) != NULL) {
142 luaM_free(L, ci->next); /* free next */
143 L->nci--;
144 ci->next = next2; /* remove 'next' from the list */
145 next2->previous = ci;
146 ci = next2; /* keep next's next */
147 }
148}

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

Referenced by luaD_shrinkstack().

◆ makeseed()

static unsigned int makeseed ( lua_State * L)
static

Definition at line 81 of file lua-5.3.6/src/lstate.c.

81 {
82 char buff[4 * sizeof(size_t)];
83 unsigned int h = luai_makeseed();
84 int p = 0;
85 addbuff(buff, p, L); /* heap variable */
86 addbuff(buff, p, &h); /* local variable */
87 addbuff(buff, p, luaO_nilobject); /* global variable */
88 addbuff(buff, p, &lua_newstate); /* public function */
89 lua_assert(p == sizeof(buff));
90 return luaS_hash(buff, p, h);
91}
#define luaO_nilobject
unsigned int luaS_hash(const char *str, size_t l, unsigned int seed)
LUA_API lua_State * lua_newstate(lua_Alloc f, void *ud)
#define luai_makeseed()
#define addbuff(b, p, e)

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

Referenced by lua_newstate().

◆ preinit_thread()

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

Definition at line 221 of file lua-5.3.6/src/lstate.c.

221 {
222 G(L) = g;
223 L->stack = NULL;
224 L->ci = NULL;
225 L->nci = 0;
226 L->stacksize = 0;
227 L->twups = L; /* thread has no upvalues */
228 L->errorJmp = NULL;
229 L->nCcalls = 0;
230 L->hook = NULL;
231 L->hookmask = 0;
232 L->basehookcount = 0;
233 L->allowhook = 1;
235 L->openupval = NULL;
236 L->nny = 1;
237 L->status = LUA_OK;
238 L->errfunc = 0;
239}
unsigned short nny
struct lua_longjmp * errorJmp
unsigned short nCcalls
struct lua_State * twups

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::nci, lua_State::nny, NULL, lua_State::openupval, resethookcount, lua_State::stack, lua_State::stacksize, lua_State::status, and lua_State::twups.

Referenced by lua_newstate(), and lua_newthread().

◆ stack_init()

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

Definition at line 151 of file lua-5.3.6/src/lstate.c.

151 {
152 int i; CallInfo *ci;
153 /* initialize stack array */
156 for (i = 0; i < BASIC_STACK_SIZE; i++)
157 setnilvalue(L1->stack + i); /* erase new stack */
158 L1->top = L1->stack;
159 L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;
160 /* initialize first ci */
161 ci = &L1->base_ci;
162 ci->next = ci->previous = NULL;
163 ci->callstatus = 0;
164 ci->func = L1->top;
165 setnilvalue(L1->top++); /* 'function' entry for this 'ci' */
166 ci->top = L1->top + LUA_MINSTACK;
167 L1->ci = ci;
168}
#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().