New macro 'completestate'

This commit is contained in:
Roberto Ierusalimschy 2021-02-05 17:51:25 -03:00
parent dee6433a89
commit c63e5d212b
4 changed files with 11 additions and 7 deletions

2
lapi.c
View File

@ -39,7 +39,7 @@ const char lua_ident[] =
/* /*
** Test for a valid index. ** Test for a valid index (one that is not the 'nilvalue').
** '!ttisnil(o)' implies 'o != &G(L)->nilvalue', so it is not needed. ** '!ttisnil(o)' implies 'o != &G(L)->nilvalue', so it is not needed.
** However, it covers the most common cases in a faster way. ** However, it covers the most common cases in a faster way.
*/ */

4
lmem.c
View File

@ -29,7 +29,7 @@
** a full GC cycle at every allocation.) ** a full GC cycle at every allocation.)
*/ */
static void *firsttry (global_State *g, void *block, size_t os, size_t ns) { static void *firsttry (global_State *g, void *block, size_t os, size_t ns) {
if (ttisnil(&g->nilvalue) && ns > os) if (completestate(g) && ns > os)
return NULL; /* fail */ return NULL; /* fail */
else /* normal allocation */ else /* normal allocation */
return (*g->frealloc)(g->ud, block, os, ns); return (*g->frealloc)(g->ud, block, os, ns);
@ -146,7 +146,7 @@ void luaM_free_ (lua_State *L, void *block, size_t osize) {
static void *tryagain (lua_State *L, void *block, static void *tryagain (lua_State *L, void *block,
size_t osize, size_t nsize) { size_t osize, size_t nsize) {
global_State *g = G(L); global_State *g = G(L);
if (ttisnil(&g->nilvalue)) { /* is state fully build? */ if (completestate(g)) { /* is state fully build? */
luaC_fullgc(L, 1); /* try to free some memory... */ luaC_fullgc(L, 1); /* try to free some memory... */
return (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ return (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
} }

View File

@ -226,8 +226,6 @@ static void init_registry (lua_State *L, global_State *g) {
/* /*
** open parts of the state that may cause memory-allocation errors. ** open parts of the state that may cause memory-allocation errors.
** ('g->nilvalue' being a nil value flags that the state was completely
** build.)
*/ */
static void f_luaopen (lua_State *L, void *ud) { static void f_luaopen (lua_State *L, void *ud) {
global_State *g = G(L); global_State *g = G(L);
@ -238,7 +236,7 @@ static void f_luaopen (lua_State *L, void *ud) {
luaT_init(L); luaT_init(L);
luaX_init(L); luaX_init(L);
g->gcrunning = 1; /* allow gc */ g->gcrunning = 1; /* allow gc */
setnilvalue(&g->nilvalue); setnilvalue(&g->nilvalue); /* now state is complete */
luai_userstateopen(L); luai_userstateopen(L);
} }
@ -272,7 +270,7 @@ static void close_state (lua_State *L) {
global_State *g = G(L); global_State *g = G(L);
luaD_closeprotected(L, 0, LUA_OK); /* close all upvalues */ luaD_closeprotected(L, 0, LUA_OK); /* close all upvalues */
luaC_freeallobjects(L); /* collect all objects */ luaC_freeallobjects(L); /* collect all objects */
if (ttisnil(&g->nilvalue)) /* closing a fully built state? */ if (completestate(g)) /* closing a fully built state? */
luai_userstateclose(L); luai_userstateclose(L);
luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
freestack(L); freestack(L);

View File

@ -324,6 +324,12 @@ struct lua_State {
#define G(L) (L->l_G) #define G(L) (L->l_G)
/*
** 'g->nilvalue' being a nil value flags that the state was completely
** build.
*/
#define completestate(g) ttisnil(&g->nilvalue)
/* /*
** Union of all collectable objects (only for conversions) ** Union of all collectable objects (only for conversions)