lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Jun 25, 2014, at 11:26 PM, Daurnimator <quae@daurnimator.com> wrote:

> Today I was reading the lua source, and came across this code in lmem.c ( http://www.lua.org/source/5.2/lmem.c.html#luaM_realloc_ )
> 
>     if (g->gcrunning) {
>       luaC_fullgc(L, 1);  /* try to free some memory... */
> 
> Is that conditional accidentally reversed?
> Why run the gc again ONLY if the gc is mid-run?
> Wouldn't it make sense to run the gc if it's NOT already running?
> 
> Sorry if this is a stupid question >.<
> 
> Daurn.

If the realloc routine fails to allocate memory, and the garbage collector is running, then it will attempt to do a full garbage collection cycle (in emergency mode) in the effort to free memory.

The allocation is then tried again, and if it fails a memory error is thrown. If the garbage collector is not active then this emergency collection will not occur.

Note that the `g->gcrunning` flag only indicates if the garbage collector is running, not if it is in the middle of a run or any other status other than active or inactive.

~pmd