]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/third_party/prometheus-cpp/3rdparty/civetweb/src/third_party/lua-5.4.3/src/lstate.c
4 ** See Copyright Notice in lua.h
33 ** thread state + extra space
36 lu_byte extra_
[LUA_EXTRASPACE
];
42 ** Main thread combines a thread state and the global state
51 #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
55 ** A macro to create a "random" seed when a state is created;
56 ** the seed is used to randomize string hashes.
58 #if !defined(luai_makeseed)
63 ** Compute an initial seed with some level of randomness.
64 ** Rely on Address Space Layout Randomization (if present) and
67 #define addbuff(b,p,e) \
68 { size_t t = cast_sizet(e); \
69 memcpy(b + p, &t, sizeof(t)); p += sizeof(t); }
71 static unsigned int luai_makeseed (lua_State
*L
) {
72 char buff
[3 * sizeof(size_t)];
73 unsigned int h
= cast_uint(time(NULL
));
75 addbuff(buff
, p
, L
); /* heap variable */
76 addbuff(buff
, p
, &h
); /* local variable */
77 addbuff(buff
, p
, &lua_newstate
); /* public function */
78 lua_assert(p
== sizeof(buff
));
79 return luaS_hash(buff
, p
, h
);
86 ** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
87 ** invariant (and avoiding underflows in 'totalbytes')
89 void luaE_setdebt (global_State
*g
, l_mem debt
) {
90 l_mem tb
= gettotalbytes(g
);
92 if (debt
< tb
- MAX_LMEM
)
93 debt
= tb
- MAX_LMEM
; /* will make 'totalbytes == MAX_LMEM' */
94 g
->totalbytes
= tb
- debt
;
99 LUA_API
int lua_setcstacklimit (lua_State
*L
, unsigned int limit
) {
100 UNUSED(L
); UNUSED(limit
);
101 return LUAI_MAXCCALLS
; /* warning?? */
105 CallInfo
*luaE_extendCI (lua_State
*L
) {
107 lua_assert(L
->ci
->next
== NULL
);
108 ci
= luaM_new(L
, CallInfo
);
109 lua_assert(L
->ci
->next
== NULL
);
111 ci
->previous
= L
->ci
;
120 ** free all CallInfo structures not in use by a thread
122 void luaE_freeCI (lua_State
*L
) {
123 CallInfo
*ci
= L
->ci
;
124 CallInfo
*next
= ci
->next
;
126 while ((ci
= next
) != NULL
) {
135 ** free half of the CallInfo structures not in use by a thread,
136 ** keeping the first one.
138 void luaE_shrinkCI (lua_State
*L
) {
139 CallInfo
*ci
= L
->ci
->next
; /* first free CallInfo */
142 return; /* no extra elements */
143 while ((next
= ci
->next
) != NULL
) { /* two extra elements? */
144 CallInfo
*next2
= next
->next
; /* next's next */
145 ci
->next
= next2
; /* remove next from the list */
147 luaM_free(L
, next
); /* free next */
149 break; /* no more elements */
151 next2
->previous
= ci
;
152 ci
= next2
; /* continue */
159 ** Called when 'getCcalls(L)' larger or equal to LUAI_MAXCCALLS.
160 ** If equal, raises an overflow error. If value is larger than
161 ** LUAI_MAXCCALLS (which means it is handling an overflow) but
162 ** not much larger, does not report an error (to allow overflow
163 ** handling to work).
165 void luaE_checkcstack (lua_State
*L
) {
166 if (getCcalls(L
) == LUAI_MAXCCALLS
)
167 luaG_runerror(L
, "C stack overflow");
168 else if (getCcalls(L
) >= (LUAI_MAXCCALLS
/ 10 * 11))
169 luaD_throw(L
, LUA_ERRERR
); /* error while handing stack error */
173 LUAI_FUNC
void luaE_incCstack (lua_State
*L
) {
175 if (l_unlikely(getCcalls(L
) >= LUAI_MAXCCALLS
))
180 static void stack_init (lua_State
*L1
, lua_State
*L
) {
182 /* initialize stack array */
183 L1
->stack
= luaM_newvector(L
, BASIC_STACK_SIZE
+ EXTRA_STACK
, StackValue
);
184 L1
->tbclist
= L1
->stack
;
185 for (i
= 0; i
< BASIC_STACK_SIZE
+ EXTRA_STACK
; i
++)
186 setnilvalue(s2v(L1
->stack
+ i
)); /* erase new stack */
188 L1
->stack_last
= L1
->stack
+ BASIC_STACK_SIZE
;
189 /* initialize first ci */
191 ci
->next
= ci
->previous
= NULL
;
192 ci
->callstatus
= CIST_C
;
196 setnilvalue(s2v(L1
->top
)); /* 'function' entry for this 'ci' */
198 ci
->top
= L1
->top
+ LUA_MINSTACK
;
203 static void freestack (lua_State
*L
) {
204 if (L
->stack
== NULL
)
205 return; /* stack not completely built yet */
206 L
->ci
= &L
->base_ci
; /* free the entire 'ci' list */
208 lua_assert(L
->nci
== 0);
209 luaM_freearray(L
, L
->stack
, stacksize(L
) + EXTRA_STACK
); /* free stack */
214 ** Create registry table and its predefined values
216 static void init_registry (lua_State
*L
, global_State
*g
) {
217 /* create registry */
218 Table
*registry
= luaH_new(L
);
219 sethvalue(L
, &g
->l_registry
, registry
);
220 luaH_resize(L
, registry
, LUA_RIDX_LAST
, 0);
221 /* registry[LUA_RIDX_MAINTHREAD] = L */
222 setthvalue(L
, ®istry
->array
[LUA_RIDX_MAINTHREAD
- 1], L
);
223 /* registry[LUA_RIDX_GLOBALS] = new table (table of globals) */
224 sethvalue(L
, ®istry
->array
[LUA_RIDX_GLOBALS
- 1], luaH_new(L
));
229 ** open parts of the state that may cause memory-allocation errors.
231 static void f_luaopen (lua_State
*L
, void *ud
) {
232 global_State
*g
= G(L
);
234 stack_init(L
, L
); /* init stack */
239 g
->gcrunning
= 1; /* allow gc */
240 setnilvalue(&g
->nilvalue
); /* now state is complete */
241 luai_userstateopen(L
);
246 ** preinitialize a thread with consistent values without allocating
247 ** any memory (to avoid errors)
249 static void preinit_thread (lua_State
*L
, global_State
*g
) {
254 L
->twups
= L
; /* thread has no upvalues */
259 L
->basehookcount
= 0;
269 static void close_state (lua_State
*L
) {
270 global_State
*g
= G(L
);
271 if (!completestate(g
)) /* closing a partially built state? */
272 luaC_freeallobjects(L
); /* jucst collect its objects */
273 else { /* closing a fully built state */
274 luaD_closeprotected(L
, 1, LUA_OK
); /* close all upvalues */
275 luaC_freeallobjects(L
); /* collect all objects */
276 luai_userstateclose(L
);
278 luaM_freearray(L
, G(L
)->strt
.hash
, G(L
)->strt
.size
);
280 lua_assert(gettotalbytes(g
) == sizeof(LG
));
281 (*g
->frealloc
)(g
->ud
, fromstate(L
), sizeof(LG
), 0); /* free main block */
285 LUA_API lua_State
*lua_newthread (lua_State
*L
) {
291 /* create new thread */
292 L1
= &cast(LX
*, luaM_newobject(L
, LUA_TTHREAD
, sizeof(LX
)))->l
;
293 L1
->marked
= luaC_white(g
);
294 L1
->tt
= LUA_VTHREAD
;
295 /* link it on list 'allgc' */
297 g
->allgc
= obj2gco(L1
);
298 /* anchor it on L stack */
299 setthvalue2s(L
, L
->top
, L1
);
301 preinit_thread(L1
, g
);
302 L1
->hookmask
= L
->hookmask
;
303 L1
->basehookcount
= L
->basehookcount
;
306 /* initialize L1 extra space */
307 memcpy(lua_getextraspace(L1
), lua_getextraspace(g
->mainthread
),
309 luai_userstatethread(L
, L1
);
310 stack_init(L1
, L
); /* init stack */
316 void luaE_freethread (lua_State
*L
, lua_State
*L1
) {
317 LX
*l
= fromstate(L1
);
318 luaF_closeupval(L1
, L1
->stack
); /* close all upvalues */
319 lua_assert(L1
->openupval
== NULL
);
320 luai_userstatefree(L
, L1
);
326 int luaE_resetthread (lua_State
*L
, int status
) {
327 CallInfo
*ci
= L
->ci
= &L
->base_ci
; /* unwind CallInfo list */
328 setnilvalue(s2v(L
->stack
)); /* 'function' entry for basic 'ci' */
330 ci
->callstatus
= CIST_C
;
331 if (status
== LUA_YIELD
)
333 status
= luaD_closeprotected(L
, 1, status
);
334 if (status
!= LUA_OK
) /* errors? */
335 luaD_seterrorobj(L
, status
, L
->stack
+ 1);
337 L
->top
= L
->stack
+ 1;
338 ci
->top
= L
->top
+ LUA_MINSTACK
;
339 L
->status
= cast_byte(status
);
340 luaD_reallocstack(L
, cast_int(ci
->top
- L
->stack
), 0);
345 LUA_API
int lua_resetthread (lua_State
*L
) {
348 status
= luaE_resetthread(L
, L
->status
);
354 LUA_API lua_State
*lua_newstate (lua_Alloc f
, void *ud
) {
358 LG
*l
= cast(LG
*, (*f
)(ud
, NULL
, LUA_TTHREAD
, sizeof(LG
)));
359 if (l
== NULL
) return NULL
;
363 g
->currentwhite
= bitmask(WHITE0BIT
);
364 L
->marked
= luaC_white(g
);
365 preinit_thread(L
, g
);
366 g
->allgc
= obj2gco(L
); /* by now, only object is the main thread */
368 incnny(L
); /* main thread is always non yieldable */
374 g
->seed
= luai_makeseed(L
);
375 g
->gcrunning
= 0; /* no GC while building state */
376 g
->strt
.size
= g
->strt
.nuse
= 0;
378 setnilvalue(&g
->l_registry
);
380 g
->gcstate
= GCSpause
;
384 g
->finobj
= g
->tobefnz
= g
->fixedgc
= NULL
;
385 g
->firstold1
= g
->survival
= g
->old1
= g
->reallyold
= NULL
;
386 g
->finobjsur
= g
->finobjold1
= g
->finobjrold
= NULL
;
388 g
->gray
= g
->grayagain
= NULL
;
389 g
->weak
= g
->ephemeron
= g
->allweak
= NULL
;
391 g
->totalbytes
= sizeof(LG
);
394 setivalue(&g
->nilvalue
, 0); /* to signal that state is not yet built */
395 setgcparam(g
->gcpause
, LUAI_GCPAUSE
);
396 setgcparam(g
->gcstepmul
, LUAI_GCMUL
);
397 g
->gcstepsize
= LUAI_GCSTEPSIZE
;
398 setgcparam(g
->genmajormul
, LUAI_GENMAJORMUL
);
399 g
->genminormul
= LUAI_GENMINORMUL
;
400 for (i
=0; i
< LUA_NUMTAGS
; i
++) g
->mt
[i
] = NULL
;
401 if (luaD_rawrunprotected(L
, f_luaopen
, NULL
) != LUA_OK
) {
402 /* memory allocation error: free partial state */
410 LUA_API
void lua_close (lua_State
*L
) {
412 L
= G(L
)->mainthread
; /* only the main thread can be closed */
417 void luaE_warning (lua_State
*L
, const char *msg
, int tocont
) {
418 lua_WarnFunction wf
= G(L
)->warnf
;
420 wf(G(L
)->ud_warn
, msg
, tocont
);
425 ** Generate a warning from an error message
427 void luaE_warnerror (lua_State
*L
, const char *where
) {
428 TValue
*errobj
= s2v(L
->top
- 1); /* error object */
429 const char *msg
= (ttisstring(errobj
))
431 : "error object is not a string";
432 /* produce warning "error in %s (%s)" (where, msg) */
433 luaE_warning(L
, "error in ", 1);
434 luaE_warning(L
, where
, 1);
435 luaE_warning(L
, " (", 1);
436 luaE_warning(L
, msg
, 1);
437 luaE_warning(L
, ")", 0);