Path: blob/main/sys/contrib/openzfs/module/lua/lstate.h
48383 views
// SPDX-License-Identifier: MIT1/*2** $Id: lstate.h,v 2.82.1.1 2013/04/12 18:48:47 roberto Exp $3** Global State4** See Copyright Notice in lua.h5*/67#ifndef lstate_h8#define lstate_h910#include <sys/lua/lua.h>1112#include "lobject.h"13#include "ltm.h"14#include "lzio.h"151617/*1819** Some notes about garbage-collected objects: All objects in Lua must20** be kept somehow accessible until being freed.21**22** Lua keeps most objects linked in list g->allgc. The link uses field23** '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. They28** are collected together with their respective threads. Lua keeps a29** double-linked list with all open upvalues (g->uvhead) so that it can30** mark objects referred by them. (They are always gray, so they must31** be remarked in the atomic step. Usually their contents would be marked32** when traversing the respective threads, but the thread may already be33** dead, while the upvalue is still accessible through closures.)34**35** Objects with finalizers are kept in the list g->finobj.36**37** The list g->tobefnz links all objects being finalized.3839*/404142struct lua_longjmp; /* defined in ldo.c */43444546/* extra stack space to handle TM calls and some other extras */47#define EXTRA_STACK 5484950#define BASIC_STACK_SIZE (2*LUA_MINSTACK)515253/* kinds of Garbage Collection */54#define KGC_NORMAL 055#define KGC_EMERGENCY 1 /* gc was forced by an allocation failure */56#define KGC_GEN 2 /* generational collection */575859typedef struct stringtable {60GCObject **hash;61lu_int32 nuse; /* number of elements */62int size;63} stringtable;646566/*67** information about a call68*/69typedef struct CallInfo {70StkId func; /* function index in the stack */71StkId top; /* top for this function */72struct CallInfo *previous, *next; /* dynamic call link */73short nresults; /* expected number of results from this function */74lu_byte callstatus;75ptrdiff_t extra;76union {77struct { /* only for Lua functions */78StkId base; /* base for this function */79const Instruction *savedpc;80} l;81struct { /* only for C functions */82int ctx; /* context info. in case of yields */83lua_CFunction k; /* continuation in case of yields */84ptrdiff_t old_errfunc;85lu_byte old_allowhook;86lu_byte status;87} c;88} u;89} CallInfo;909192/*93** Bits in CallInfo status94*/95#define CIST_LUA (1<<0) /* call is running a Lua function */96#define CIST_HOOKED (1<<1) /* call is running a debug hook */97#define CIST_REENTRY (1<<2) /* call is running on same invocation of98luaV_execute of previous call */99#define CIST_YIELDED (1<<3) /* call reentered after suspension */100#define CIST_YPCALL (1<<4) /* call is a yieldable protected call */101#define CIST_STAT (1<<5) /* call has an error status (pcall) */102#define CIST_TAIL (1<<6) /* call was tail called */103#define CIST_HOOKYIELD (1<<7) /* last hook called yielded */104105106#define isLua(ci) ((ci)->callstatus & CIST_LUA)107108109/*110** `global state', shared by all threads of this state111*/112typedef struct global_State {113lua_Alloc frealloc; /* function to reallocate memory */114void *ud; /* auxiliary data to `frealloc' */115lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */116l_mem GCdebt; /* bytes allocated not yet compensated by the collector */117lu_mem GCmemtrav; /* memory traversed by the GC */118lu_mem GCestimate; /* an estimate of the non-garbage memory in use */119stringtable strt; /* hash table for strings */120TValue l_registry;121unsigned int seed; /* randomized seed for hashes */122lu_byte currentwhite;123lu_byte gcstate; /* state of garbage collector */124lu_byte gckind; /* kind of GC running */125lu_byte gcrunning; /* true if GC is running */126int sweepstrgc; /* position of sweep in `strt' */127GCObject *allgc; /* list of all collectable objects */128GCObject *finobj; /* list of collectable objects with finalizers */129GCObject **sweepgc; /* current position of sweep in list 'allgc' */130GCObject **sweepfin; /* current position of sweep in list 'finobj' */131GCObject *gray; /* list of gray objects */132GCObject *grayagain; /* list of objects to be traversed atomically */133GCObject *weak; /* list of tables with weak values */134GCObject *ephemeron; /* list of ephemeron tables (weak keys) */135GCObject *allweak; /* list of all-weak tables */136GCObject *tobefnz; /* list of userdata to be GC */137UpVal uvhead; /* head of double-linked list of all open upvalues */138Mbuffer buff; /* temporary buffer for string concatenation */139int gcpause; /* size of pause between successive GCs */140int gcmajorinc; /* pause between major collections (only in gen. mode) */141int gcstepmul; /* GC `granularity' */142lua_CFunction panic; /* to be called in unprotected errors */143struct lua_State *mainthread;144const lua_Number *version; /* pointer to version number */145TString *memerrmsg; /* memory-error message */146TString *tmname[TM_N]; /* array with tag-method names */147struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */148} global_State;149150151/*152** `per thread' state153*/154struct lua_State {155CommonHeader;156lu_byte status;157StkId top; /* first free slot in the stack */158global_State *l_G;159CallInfo *ci; /* call info for current function */160const Instruction *oldpc; /* last pc traced */161StkId stack_last; /* last free slot in the stack */162StkId stack; /* stack base */163int stacksize;164unsigned short nny; /* number of non-yieldable calls in stack */165unsigned short nCcalls; /* number of nested C calls */166lu_byte hookmask;167lu_byte allowhook;168lu_byte runerror; /* handling a runtime error */169int basehookcount;170int hookcount;171lua_Hook hook;172GCObject *openupval; /* list of open upvalues in this stack */173GCObject *gclist;174struct lua_longjmp *errorJmp; /* current error recover point */175ptrdiff_t errfunc; /* current error handling function (stack index) */176CallInfo base_ci; /* CallInfo for first level (C calling Lua) */177};178179180#define G(L) (L->l_G)181182183/*184** Union of all collectable objects185*/186union GCObject {187GCheader gch; /* common header */188struct TString ts;189union Udata u;190union Closure cl;191struct Table h;192struct Proto p;193struct UpVal uv;194struct lua_State th; /* thread */195};196197198#define gch(o) (&(o)->gch)199200/* macros to convert a GCObject into a specific value */201#define rawgco2ts(o) \202check_exp(novariant((o)->gch.tt) == LUA_TSTRING, &((o)->ts))203#define gco2ts(o) (&rawgco2ts(o)->tsv)204#define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u))205#define gco2u(o) (&rawgco2u(o)->uv)206#define gco2lcl(o) check_exp((o)->gch.tt == LUA_TLCL, &((o)->cl.l))207#define gco2ccl(o) check_exp((o)->gch.tt == LUA_TCCL, &((o)->cl.c))208#define gco2cl(o) \209check_exp(novariant((o)->gch.tt) == LUA_TFUNCTION, &((o)->cl))210#define gco2t(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h))211#define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p))212#define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv))213#define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))214215/* macro to convert any Lua object into a GCObject */216#define obj2gco(v) (cast(GCObject *, (v)))217218219/* actual number of total bytes allocated */220#define gettotalbytes(g) ((g)->totalbytes + (g)->GCdebt)221222LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);223LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);224LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);225LUAI_FUNC void luaE_freeCI (lua_State *L);226227228#endif229230231