diff options
| -rw-r--r-- | lmem.c | 11 | ||||
| -rw-r--r-- | lstate.c | 31 |
2 files changed, 26 insertions, 16 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lmem.c,v 1.74 2009/12/16 16:42:58 roberto Exp roberto $ | 2 | ** $Id: lmem.c,v 1.75 2010/04/02 14:37:41 roberto Exp roberto $ |
| 3 | ** Interface to Memory Manager | 3 | ** Interface to Memory Manager |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -79,13 +79,14 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { | |||
| 79 | size_t realosize = (block) ? osize : 0; | 79 | size_t realosize = (block) ? osize : 0; |
| 80 | lua_assert((realosize == 0) == (block == NULL)); | 80 | lua_assert((realosize == 0) == (block == NULL)); |
| 81 | #if defined(HARDMEMTESTS) | 81 | #if defined(HARDMEMTESTS) |
| 82 | if (nsize > realosize && g->GCthreshold != MAX_LUMEM) | 82 | if (nsize > realosize && !gcstopped(g)) |
| 83 | luaC_fullgc(L, 1); /* force a GC whenever possible */ | 83 | luaC_fullgc(L, 1); /* force a GC whenever possible */ |
| 84 | #endif | 84 | #endif |
| 85 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); | 85 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); |
| 86 | if (newblock == NULL && nsize > 0) { | 86 | if (newblock == NULL && nsize > 0) { |
| 87 | lua_assert(nsize > realosize); /* cannot fail when shrinking a block */ | 87 | api_check(L, nsize > realosize, |
| 88 | if (g->GCthreshold != MAX_LUMEM) { | 88 | "realloc cannot fail when shrinking a block"); |
| 89 | if (!gcstopped(g)) { | ||
| 89 | luaC_fullgc(L, 1); /* try to free some memory... */ | 90 | luaC_fullgc(L, 1); /* try to free some memory... */ |
| 90 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ | 91 | newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ |
| 91 | } | 92 | } |
| @@ -94,6 +95,8 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { | |||
| 94 | } | 95 | } |
| 95 | lua_assert((nsize == 0) == (newblock == NULL)); | 96 | lua_assert((nsize == 0) == (newblock == NULL)); |
| 96 | g->totalbytes = (g->totalbytes - realosize) + nsize; | 97 | g->totalbytes = (g->totalbytes - realosize) + nsize; |
| 98 | if (!gcstopped(g)) /* new object? */ | ||
| 99 | g->GCdebt += nsize; /* give some credit to garbage collector */ | ||
| 97 | #if defined(TRACEMEM) | 100 | #if defined(TRACEMEM) |
| 98 | { /* auxiliar patch to monitor garbage collection. | 101 | { /* auxiliar patch to monitor garbage collection. |
| 99 | ** To plot, gnuplot with following command: | 102 | ** To plot, gnuplot with following command: |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstate.c,v 2.81 2010/04/19 16:34:46 roberto Exp roberto $ | 2 | ** $Id: lstate.c,v 2.82 2010/04/19 17:40:13 roberto Exp roberto $ |
| 3 | ** Global State | 3 | ** Global State |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -90,7 +90,7 @@ void luaE_freeCI (lua_State *L) { | |||
| 90 | 90 | ||
| 91 | 91 | ||
| 92 | static void stack_init (lua_State *L1, lua_State *L) { | 92 | static void stack_init (lua_State *L1, lua_State *L) { |
| 93 | int i; | 93 | int i; CallInfo *ci; |
| 94 | /* initialize stack array */ | 94 | /* initialize stack array */ |
| 95 | L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue); | 95 | L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue); |
| 96 | L1->stacksize = BASIC_STACK_SIZE; | 96 | L1->stacksize = BASIC_STACK_SIZE; |
| @@ -99,17 +99,22 @@ static void stack_init (lua_State *L1, lua_State *L) { | |||
| 99 | L1->top = L1->stack; | 99 | L1->top = L1->stack; |
| 100 | L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK; | 100 | L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK; |
| 101 | /* initialize first ci */ | 101 | /* initialize first ci */ |
| 102 | L1->ci->func = L1->top; | 102 | ci = &L1->base_ci; |
| 103 | ci->next = ci->previous = NULL; | ||
| 104 | ci->callstatus = 0; | ||
| 105 | ci->func = L1->top; | ||
| 103 | setnilvalue(L1->top++); /* 'function' entry for this 'ci' */ | 106 | setnilvalue(L1->top++); /* 'function' entry for this 'ci' */ |
| 104 | L1->ci->top = L1->top + LUA_MINSTACK; | 107 | ci->top = L1->top + LUA_MINSTACK; |
| 105 | L1->ci->callstatus = 0; | 108 | L1->ci = ci; |
| 106 | } | 109 | } |
| 107 | 110 | ||
| 108 | 111 | ||
| 109 | static void freestack (lua_State *L) { | 112 | static void freestack (lua_State *L) { |
| 110 | L->ci = &L->base_ci; /* reset 'ci' list */ | 113 | if (L->ci != NULL) { /* is there a 'ci' list? */ |
| 111 | luaE_freeCI(L); | 114 | L->ci = &L->base_ci; /* free the entire list */ |
| 112 | luaM_freearray(L, L->stack, L->stacksize); | 115 | luaE_freeCI(L); |
| 116 | } | ||
| 117 | luaM_freearray(L, L->stack, L->stacksize); /* free stack array */ | ||
| 113 | } | 118 | } |
| 114 | 119 | ||
| 115 | 120 | ||
| @@ -145,7 +150,7 @@ static void f_luaopen (lua_State *L, void *ud) { | |||
| 145 | /* pre-create memory-error message */ | 150 | /* pre-create memory-error message */ |
| 146 | g->memerrmsg = luaS_newliteral(L, MEMERRMSG); | 151 | g->memerrmsg = luaS_newliteral(L, MEMERRMSG); |
| 147 | luaS_fix(g->memerrmsg); /* it should never be collected */ | 152 | luaS_fix(g->memerrmsg); /* it should never be collected */ |
| 148 | g->GCthreshold = 4*g->totalbytes; | 153 | g->GCdebt = 0; |
| 149 | } | 154 | } |
| 150 | 155 | ||
| 151 | 156 | ||
| @@ -156,6 +161,7 @@ static void f_luaopen (lua_State *L, void *ud) { | |||
| 156 | static void preinit_state (lua_State *L, global_State *g) { | 161 | static void preinit_state (lua_State *L, global_State *g) { |
| 157 | G(L) = g; | 162 | G(L) = g; |
| 158 | L->stack = NULL; | 163 | L->stack = NULL; |
| 164 | L->ci = NULL; | ||
| 159 | L->stacksize = 0; | 165 | L->stacksize = 0; |
| 160 | L->errorJmp = NULL; | 166 | L->errorJmp = NULL; |
| 161 | L->hook = NULL; | 167 | L->hook = NULL; |
| @@ -166,8 +172,6 @@ static void preinit_state (lua_State *L, global_State *g) { | |||
| 166 | L->openupval = NULL; | 172 | L->openupval = NULL; |
| 167 | L->nny = 1; | 173 | L->nny = 1; |
| 168 | L->status = LUA_OK; | 174 | L->status = LUA_OK; |
| 169 | L->base_ci.next = L->base_ci.previous = NULL; | ||
| 170 | L->ci = &L->base_ci; | ||
| 171 | L->errfunc = 0; | 175 | L->errfunc = 0; |
| 172 | } | 176 | } |
| 173 | 177 | ||
| @@ -233,7 +237,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { | |||
| 233 | g->mainthread = L; | 237 | g->mainthread = L; |
| 234 | g->uvhead.u.l.prev = &g->uvhead; | 238 | g->uvhead.u.l.prev = &g->uvhead; |
| 235 | g->uvhead.u.l.next = &g->uvhead; | 239 | g->uvhead.u.l.next = &g->uvhead; |
| 236 | g->GCthreshold = MAX_LUMEM; /* no GC while building state */ | 240 | stopgc(g); /* no GC while building state */ |
| 237 | g->lastmajormem = 0; | 241 | g->lastmajormem = 0; |
| 238 | g->strt.size = 0; | 242 | g->strt.size = 0; |
| 239 | g->strt.nuse = 0; | 243 | g->strt.nuse = 0; |
| @@ -246,6 +250,9 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { | |||
| 246 | g->allgc = NULL; | 250 | g->allgc = NULL; |
| 247 | g->udgc = NULL; | 251 | g->udgc = NULL; |
| 248 | g->tobefnz = NULL; | 252 | g->tobefnz = NULL; |
| 253 | g->gray = NULL; | ||
| 254 | g->grayagain = NULL; | ||
| 255 | g->weak = g->ephemeron = g->allweak = NULL; | ||
| 249 | g->totalbytes = sizeof(LG); | 256 | g->totalbytes = sizeof(LG); |
| 250 | g->gcpause = LUAI_GCPAUSE; | 257 | g->gcpause = LUAI_GCPAUSE; |
| 251 | g->gcstepmul = LUAI_GCMUL; | 258 | g->gcstepmul = LUAI_GCMUL; |
