aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2021-02-05 17:51:25 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2021-02-05 17:51:25 -0300
commitc63e5d212bc5dec1b1c749e3f07b42cd83081826 (patch)
tree35c2dc1d828e0ec9c89d60ac2702b847e002a68f
parentdee6433a89b088a1f8da9531a92a2a2693e5dac7 (diff)
downloadlua-c63e5d212bc5dec1b1c749e3f07b42cd83081826.tar.gz
lua-c63e5d212bc5dec1b1c749e3f07b42cd83081826.tar.bz2
lua-c63e5d212bc5dec1b1c749e3f07b42cd83081826.zip
New macro 'completestate'
-rw-r--r--lapi.c2
-rw-r--r--lmem.c4
-rw-r--r--lstate.c6
-rw-r--r--lstate.h6
4 files changed, 11 insertions, 7 deletions
diff --git a/lapi.c b/lapi.c
index 27bf23da..41e6b86d 100644
--- a/lapi.c
+++ b/lapi.c
@@ -39,7 +39,7 @@ const char lua_ident[] =
39 39
40 40
41/* 41/*
42** Test for a valid index. 42** Test for a valid index (one that is not the 'nilvalue').
43** '!ttisnil(o)' implies 'o != &G(L)->nilvalue', so it is not needed. 43** '!ttisnil(o)' implies 'o != &G(L)->nilvalue', so it is not needed.
44** However, it covers the most common cases in a faster way. 44** However, it covers the most common cases in a faster way.
45*/ 45*/
diff --git a/lmem.c b/lmem.c
index 43739bff..4822a0ea 100644
--- a/lmem.c
+++ b/lmem.c
@@ -29,7 +29,7 @@
29** a full GC cycle at every allocation.) 29** a full GC cycle at every allocation.)
30*/ 30*/
31static void *firsttry (global_State *g, void *block, size_t os, size_t ns) { 31static void *firsttry (global_State *g, void *block, size_t os, size_t ns) {
32 if (ttisnil(&g->nilvalue) && ns > os) 32 if (completestate(g) && ns > os)
33 return NULL; /* fail */ 33 return NULL; /* fail */
34 else /* normal allocation */ 34 else /* normal allocation */
35 return (*g->frealloc)(g->ud, block, os, ns); 35 return (*g->frealloc)(g->ud, block, os, ns);
@@ -146,7 +146,7 @@ void luaM_free_ (lua_State *L, void *block, size_t osize) {
146static void *tryagain (lua_State *L, void *block, 146static void *tryagain (lua_State *L, void *block,
147 size_t osize, size_t nsize) { 147 size_t osize, size_t nsize) {
148 global_State *g = G(L); 148 global_State *g = G(L);
149 if (ttisnil(&g->nilvalue)) { /* is state fully build? */ 149 if (completestate(g)) { /* is state fully build? */
150 luaC_fullgc(L, 1); /* try to free some memory... */ 150 luaC_fullgc(L, 1); /* try to free some memory... */
151 return (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ 151 return (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
152 } 152 }
diff --git a/lstate.c b/lstate.c
index c708a162..52336f44 100644
--- a/lstate.c
+++ b/lstate.c
@@ -226,8 +226,6 @@ static void init_registry (lua_State *L, global_State *g) {
226 226
227/* 227/*
228** open parts of the state that may cause memory-allocation errors. 228** open parts of the state that may cause memory-allocation errors.
229** ('g->nilvalue' being a nil value flags that the state was completely
230** build.)
231*/ 229*/
232static void f_luaopen (lua_State *L, void *ud) { 230static void f_luaopen (lua_State *L, void *ud) {
233 global_State *g = G(L); 231 global_State *g = G(L);
@@ -238,7 +236,7 @@ static void f_luaopen (lua_State *L, void *ud) {
238 luaT_init(L); 236 luaT_init(L);
239 luaX_init(L); 237 luaX_init(L);
240 g->gcrunning = 1; /* allow gc */ 238 g->gcrunning = 1; /* allow gc */
241 setnilvalue(&g->nilvalue); 239 setnilvalue(&g->nilvalue); /* now state is complete */
242 luai_userstateopen(L); 240 luai_userstateopen(L);
243} 241}
244 242
@@ -272,7 +270,7 @@ static void close_state (lua_State *L) {
272 global_State *g = G(L); 270 global_State *g = G(L);
273 luaD_closeprotected(L, 0, LUA_OK); /* close all upvalues */ 271 luaD_closeprotected(L, 0, LUA_OK); /* close all upvalues */
274 luaC_freeallobjects(L); /* collect all objects */ 272 luaC_freeallobjects(L); /* collect all objects */
275 if (ttisnil(&g->nilvalue)) /* closing a fully built state? */ 273 if (completestate(g)) /* closing a fully built state? */
276 luai_userstateclose(L); 274 luai_userstateclose(L);
277 luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); 275 luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
278 freestack(L); 276 freestack(L);
diff --git a/lstate.h b/lstate.h
index f3d791ab..5ef55355 100644
--- a/lstate.h
+++ b/lstate.h
@@ -324,6 +324,12 @@ struct lua_State {
324 324
325#define G(L) (L->l_G) 325#define G(L) (L->l_G)
326 326
327/*
328** 'g->nilvalue' being a nil value flags that the state was completely
329** build.
330*/
331#define completestate(g) ttisnil(&g->nilvalue)
332
327 333
328/* 334/*
329** Union of all collectable objects (only for conversions) 335** Union of all collectable objects (only for conversions)