2222#include "lstate.h"
2323
2424
25- #if defined(EMERGENCYGCTESTS )
26- /*
27- ** First allocation will fail whenever not building initial state.
28- ** (This fail will trigger 'tryagain' and a full GC cycle at every
29- ** allocation.)
30- */
31- static void * firsttry (global_State * g , void * block , size_t os , size_t ns ) {
32- if (completestate (g ) && ns > 0 ) /* frees never fail */
33- return NULL ; /* fail */
34- else /* normal allocation */
35- return (* g -> frealloc )(g -> ud , block , os , ns );
36- }
37- #else
38- #define firsttry (g ,block ,os ,ns ) ((*g->frealloc)(g->ud, block, os, ns))
39- #endif
40-
41-
42-
43-
4425
4526/*
4627** About the realloc function:
@@ -60,6 +41,43 @@ static void *firsttry (global_State *g, void *block, size_t os, size_t ns) {
6041*/
6142
6243
44+ /*
45+ ** Macro to call the allocation function.
46+ */
47+ #define callfrealloc (g ,block ,os ,ns ) ((*g->frealloc)(g->ud, block, os, ns))
48+
49+
50+ /*
51+ ** When an allocation fails, it will try again after an emergency
52+ ** collection, except when it cannot run a collection. The GC should
53+ ** not be called while the state is not fully built, as the collector
54+ ** is not yet fully initialized. Also, it should not be called when
55+ ** 'gcstopem' is true, because then the interpreter is in the middle of
56+ ** a collection step.
57+ */
58+ #define cantryagain (g ) (completestate(g) && !g->gcstopem)
59+
60+
61+
62+
63+ #if defined(EMERGENCYGCTESTS )
64+ /*
65+ ** First allocation will fail except when freeing a block (frees never
66+ ** fail) and when it cannot try again; this fail will trigger 'tryagain'
67+ ** and a full GC cycle at every allocation.
68+ */
69+ static void * firsttry (global_State * g , void * block , size_t os , size_t ns ) {
70+ if (ns > 0 && cantryagain (g ))
71+ return NULL ; /* fail */
72+ else /* normal allocation */
73+ return callfrealloc (g , block , os , ns );
74+ }
75+ #else
76+ #define firsttry (g ,block ,os ,ns ) callfrealloc(g, block, os, ns)
77+ #endif
78+
79+
80+
6381
6482
6583/*
@@ -132,27 +150,23 @@ l_noret luaM_toobig (lua_State *L) {
132150void luaM_free_ (lua_State * L , void * block , size_t osize ) {
133151 global_State * g = G (L );
134152 lua_assert ((osize == 0 ) == (block == NULL ));
135- ( * g -> frealloc )( g -> ud , block , osize , 0 );
153+ callfrealloc ( g , block , osize , 0 );
136154 g -> GCdebt -= osize ;
137155}
138156
139157
140158/*
141159** In case of allocation fail, this function will do an emergency
142160** collection to free some memory and then try the allocation again.
143- ** The GC should not be called while state is not fully built, as the
144- ** collector is not yet fully initialized. Also, it should not be called
145- ** when 'gcstopem' is true, because then the interpreter is in the
146- ** middle of a collection step.
147161*/
148162static void * tryagain (lua_State * L , void * block ,
149163 size_t osize , size_t nsize ) {
150164 global_State * g = G (L );
151- if (completestate (g ) && ! g -> gcstopem ) {
165+ if (cantryagain (g )) {
152166 luaC_fullgc (L , 1 ); /* try to free some memory... */
153- return ( * g -> frealloc )( g -> ud , block , osize , nsize ); /* try again */
167+ return callfrealloc ( g , block , osize , nsize ); /* try again */
154168 }
155- else return NULL ; /* cannot free any memory without a full state */
169+ else return NULL ; /* cannot run an emergency collection */
156170}
157171
158172
0 commit comments