From 6e9145a882ea70fe438d59959ac4e65481fe5e85 Mon Sep 17 00:00:00 2001 From: Mike Pall Date: Sat, 20 Dec 2014 00:17:50 +0100 Subject: [PATCH] Cleanup of memory vs. GC sizes. No functional changes. --- src/lib_package.c | 4 ++-- src/lj_api.c | 2 +- src/lj_bcread.c | 2 +- src/lj_buf.c | 6 +++--- src/lj_def.h | 11 +++++++---- src/lj_gc.c | 24 ++++++++++++------------ src/lj_gc.h | 10 +++++----- src/lj_ir.c | 2 +- src/lj_obj.h | 11 ++++++----- src/lj_state.c | 2 +- 10 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/lib_package.c b/src/lib_package.c index d3229110..dc9f61ab 100644 --- a/src/lib_package.c +++ b/src/lib_package.c @@ -226,7 +226,7 @@ static int ll_loadfunc(lua_State *L, const char *path, const char *name, int r) const char *bcdata = ll_bcsym(*reg, mksymname(L, name, SYMPREFIX_BC)); lua_pop(L, 1); if (bcdata) { - if (luaL_loadbuffer(L, bcdata, LJ_MAX_MEM, name) != 0) + if (luaL_loadbuffer(L, bcdata, LJ_MAX_BUF, name) != 0) return PACKAGE_ERR_LOAD; return 0; } @@ -383,7 +383,7 @@ static int lj_cf_package_loader_preload(lua_State *L) if (lua_isnil(L, -1)) { /* Not found? */ const char *bcname = mksymname(L, name, SYMPREFIX_BC); const char *bcdata = ll_bcsym(NULL, bcname); - if (bcdata == NULL || luaL_loadbuffer(L, bcdata, LJ_MAX_MEM, name) != 0) + if (bcdata == NULL || luaL_loadbuffer(L, bcdata, LJ_MAX_BUF, name) != 0) lua_pushfstring(L, "\n\tno field package.preload['%s']", name); } return 1; diff --git a/src/lj_api.c b/src/lj_api.c index 1ccd7be1..03be80f9 100644 --- a/src/lj_api.c +++ b/src/lj_api.c @@ -1150,7 +1150,7 @@ LUA_API int lua_gc(lua_State *L, int what, int data) res = (int)(g->gc.total & 0x3ff); break; case LUA_GCSTEP: { - MSize a = (MSize)data << 10; + GCSize a = (GCSize)data << 10; g->gc.threshold = (a <= g->gc.total) ? (g->gc.total - a) : 0; while (g->gc.total >= g->gc.threshold) if (lj_gc_step(L) > 0) { diff --git a/src/lj_bcread.c b/src/lj_bcread.c index 2360bf40..519164ca 100644 --- a/src/lj_bcread.c +++ b/src/lj_bcread.c @@ -48,7 +48,7 @@ static LJ_NOINLINE void bcread_error(LexState *ls, ErrMsg em) static LJ_NOINLINE void bcread_fill(LexState *ls, MSize len, int need) { lua_assert(len != 0); - if (len > LJ_MAX_MEM || ls->c < 0) + if (len > LJ_MAX_BUF || ls->c < 0) bcread_error(ls, LJ_ERR_BCBAD); do { const char *buf; diff --git a/src/lj_buf.c b/src/lj_buf.c index 05ff1f51..05b35615 100644 --- a/src/lj_buf.c +++ b/src/lj_buf.c @@ -31,7 +31,7 @@ static void buf_grow(SBuf *sb, MSize sz) LJ_NOINLINE char *LJ_FASTCALL lj_buf_need2(SBuf *sb, MSize sz) { lua_assert(sz > sbufsz(sb)); - if (LJ_UNLIKELY(sz > LJ_MAX_MEM)) + if (LJ_UNLIKELY(sz > LJ_MAX_BUF)) lj_err_mem(sbufL(sb)); buf_grow(sb, sz); return sbufB(sb); @@ -41,7 +41,7 @@ LJ_NOINLINE char *LJ_FASTCALL lj_buf_more2(SBuf *sb, MSize sz) { MSize len = sbuflen(sb); lua_assert(sz > sbufleft(sb)); - if (LJ_UNLIKELY(sz > LJ_MAX_MEM || len + sz > LJ_MAX_MEM)) + if (LJ_UNLIKELY(sz > LJ_MAX_BUF || len + sz > LJ_MAX_BUF)) lj_err_mem(sbufL(sb)); buf_grow(sb, len + sz); return sbufP(sb); @@ -178,7 +178,7 @@ SBuf *lj_buf_puttab(SBuf *sb, GCtab *t, GCstr *sep, int32_t i, int32_t e) char *p; if (!o) { badtype: /* Error: bad element type. */ - setsbufP(sb, (intptr_t)i); /* Store failing index. */ + setsbufP(sb, (void *)(intptr_t)i); /* Store failing index. */ return NULL; } else if (tvisstr(o)) { MSize len = strV(o)->len; diff --git a/src/lj_def.h b/src/lj_def.h index 8624aed2..93420ba5 100644 --- a/src/lj_def.h +++ b/src/lj_def.h @@ -46,10 +46,12 @@ typedef unsigned int uintptr_t; #include /* Various VM limits. */ -#define LJ_MAX_MEM 0x7fffff00 /* Max. total memory allocation. */ +#define LJ_MAX_MEM32 0x7fffff00 /* Max. 32 bit memory allocation. */ +#define LJ_MAX_MEM LJ_MAX_MEM32 /* Max. total memory allocation. */ #define LJ_MAX_ALLOC LJ_MAX_MEM /* Max. individual allocation length. */ -#define LJ_MAX_STR LJ_MAX_MEM /* Max. string length. */ -#define LJ_MAX_UDATA LJ_MAX_MEM /* Max. userdata length. */ +#define LJ_MAX_STR LJ_MAX_MEM32 /* Max. string length. */ +#define LJ_MAX_BUF LJ_MAX_MEM32 /* Max. buffer length. */ +#define LJ_MAX_UDATA LJ_MAX_MEM32 /* Max. userdata length. */ #define LJ_MAX_STRTAB (1<<26) /* Max. string table size. */ #define LJ_MAX_HBITS 26 /* Max. hash bits. */ @@ -57,7 +59,7 @@ typedef unsigned int uintptr_t; #define LJ_MAX_ASIZE ((1<<(LJ_MAX_ABITS-1))+1) /* Max. array part size. */ #define LJ_MAX_COLOSIZE 16 /* Max. elems for colocated array. */ -#define LJ_MAX_LINE LJ_MAX_MEM /* Max. source code line number. */ +#define LJ_MAX_LINE LJ_MAX_MEM32 /* Max. source code line number. */ #define LJ_MAX_XLEVEL 200 /* Max. syntactic nesting level. */ #define LJ_MAX_BCINS (1<<26) /* Max. # of bytecode instructions. */ #define LJ_MAX_SLOTS 250 /* Max. # of slots in a Lua func. */ @@ -99,6 +101,7 @@ typedef unsigned int uintptr_t; #define checki32(x) ((x) == (int32_t)(x)) #define checku32(x) ((x) == (uint32_t)(x)) #define checkptr32(x) ((uintptr_t)(x) == (uint32_t)(uintptr_t)(x)) +#define checkptrGC(x) (checkptr32(x)) /* Every half-decent C compiler transforms this into a rotate instruction. */ #define lj_rol(x, n) (((x)<<(n)) | ((x)>>(-(int)(n)&(8*sizeof(x)-1)))) diff --git a/src/lj_gc.c b/src/lj_gc.c index 376c9d09..5a7127c2 100644 --- a/src/lj_gc.c +++ b/src/lj_gc.c @@ -374,7 +374,7 @@ static const GCFreeFunc gc_freefunc[] = { }; /* Full sweep of a GC list. */ -#define gc_fullsweep(g, p) gc_sweep(g, (p), LJ_MAX_MEM) +#define gc_fullsweep(g, p) gc_sweep(g, (p), ~(uint32_t)0) /* Partial sweep of a GC list. */ static GCRef *gc_sweep(global_State *g, GCRef *p, uint32_t lim) @@ -452,7 +452,7 @@ static void gc_call_finalizer(global_State *g, lua_State *L, { /* Save and restore lots of state around the __gc callback. */ uint8_t oldh = hook_save(g); - MSize oldt = g->gc.threshold; + GCSize oldt = g->gc.threshold; int errcode; TValue *top; lj_trace_abort(g); @@ -590,7 +590,7 @@ static void atomic(global_State *g, lua_State *L) g->gc.currentwhite = (uint8_t)otherwhite(g); /* Flip current white. */ g->strempty.marked = g->gc.currentwhite; setmref(g->gc.sweep, &g->gc.root); - g->gc.estimate = g->gc.total - (MSize)udsize; /* Initial estimate. */ + g->gc.estimate = g->gc.total - (GCSize)udsize; /* Initial estimate. */ } /* GC state machine. Returns a cost estimate for each step performed. */ @@ -614,7 +614,7 @@ static size_t gc_onestep(lua_State *L) g->gc.sweepstr = 0; return 0; case GCSsweepstring: { - MSize old = g->gc.total; + GCSize old = g->gc.total; gc_fullsweep(g, &g->strhash[g->gc.sweepstr++]); /* Sweep one chain. */ if (g->gc.sweepstr > g->strmask) g->gc.state = GCSsweep; /* All string hash chains sweeped. */ @@ -623,7 +623,7 @@ static size_t gc_onestep(lua_State *L) return GCSWEEPCOST; } case GCSsweep: { - MSize old = g->gc.total; + GCSize old = g->gc.total; setmref(g->gc.sweep, gc_sweep(g, mref(g->gc.sweep, GCRef), GCSWEEPMAX)); lua_assert(old >= g->gc.total); g->gc.estimate -= old - g->gc.total; @@ -667,7 +667,7 @@ static size_t gc_onestep(lua_State *L) int LJ_FASTCALL lj_gc_step(lua_State *L) { global_State *g = G(L); - MSize lim; + GCSize lim; int32_t ostate = g->vmstate; setvmstate(g, GC); lim = (GCSTEPSIZE/100) * g->gc.stepmul; @@ -676,13 +676,13 @@ int LJ_FASTCALL lj_gc_step(lua_State *L) if (g->gc.total > g->gc.threshold) g->gc.debt += g->gc.total - g->gc.threshold; do { - lim -= (MSize)gc_onestep(L); + lim -= (GCSize)gc_onestep(L); if (g->gc.state == GCSpause) { g->gc.threshold = (g->gc.estimate/100) * g->gc.pause; g->vmstate = ostate; return 1; /* Finished a GC cycle. */ } - } while ((int32_t)lim > 0); + } while (sizeof(lim) == 8 ? ((int64_t)lim > 0) : ((int32_t)lim > 0)); if (g->gc.debt < GCSTEPSIZE) { g->gc.threshold = g->gc.total + GCSTEPSIZE; g->vmstate = ostate; @@ -801,7 +801,7 @@ void lj_gc_barriertrace(global_State *g, uint32_t traceno) /* -- Allocator ----------------------------------------------------------- */ /* Call pluggable memory allocator to allocate or resize a fragment. */ -void *lj_mem_realloc(lua_State *L, void *p, MSize osz, MSize nsz) +void *lj_mem_realloc(lua_State *L, void *p, GCSize osz, GCSize nsz) { global_State *g = G(L); lua_assert((osz == 0) == (p == NULL)); @@ -809,19 +809,19 @@ void *lj_mem_realloc(lua_State *L, void *p, MSize osz, MSize nsz) if (p == NULL && nsz > 0) lj_err_mem(L); lua_assert((nsz == 0) == (p == NULL)); - lua_assert(checkptr32(p)); + lua_assert(checkptrGC(p)); g->gc.total = (g->gc.total - osz) + nsz; return p; } /* Allocate new GC object and link it to the root set. */ -void * LJ_FASTCALL lj_mem_newgco(lua_State *L, MSize size) +void * LJ_FASTCALL lj_mem_newgco(lua_State *L, GCSize size) { global_State *g = G(L); GCobj *o = (GCobj *)g->allocf(g->allocd, NULL, 0, size); if (o == NULL) lj_err_mem(L); - lua_assert(checkptr32(o)); + lua_assert(checkptrGC(o)); g->gc.total += size; setgcrefr(o->gch.nextgc, g->gc.root); setgcref(g->gc.root, o); diff --git a/src/lj_gc.h b/src/lj_gc.h index c85d0756..fa415a21 100644 --- a/src/lj_gc.h +++ b/src/lj_gc.h @@ -107,8 +107,8 @@ static LJ_AINLINE void lj_gc_barrierback(global_State *g, GCtab *t) lj_gc_barrierf(G(L), obj2gco(p), obj2gco(o)); } /* Allocator. */ -LJ_FUNC void *lj_mem_realloc(lua_State *L, void *p, MSize osz, MSize nsz); -LJ_FUNC void * LJ_FASTCALL lj_mem_newgco(lua_State *L, MSize size); +LJ_FUNC void *lj_mem_realloc(lua_State *L, void *p, GCSize osz, GCSize nsz); +LJ_FUNC void * LJ_FASTCALL lj_mem_newgco(lua_State *L, GCSize size); LJ_FUNC void *lj_mem_grow(lua_State *L, void *p, MSize *szp, MSize lim, MSize esz); @@ -116,13 +116,13 @@ LJ_FUNC void *lj_mem_grow(lua_State *L, void *p, static LJ_AINLINE void lj_mem_free(global_State *g, void *p, size_t osize) { - g->gc.total -= (MSize)osize; + g->gc.total -= (GCSize)osize; g->allocf(g->allocd, p, osize, 0); } -#define lj_mem_newvec(L, n, t) ((t *)lj_mem_new(L, (MSize)((n)*sizeof(t)))) +#define lj_mem_newvec(L, n, t) ((t *)lj_mem_new(L, (GCSize)((n)*sizeof(t)))) #define lj_mem_reallocvec(L, p, on, n, t) \ - ((p) = (t *)lj_mem_realloc(L, p, (on)*sizeof(t), (MSize)((n)*sizeof(t)))) + ((p) = (t *)lj_mem_realloc(L, p, (on)*sizeof(t), (GCSize)((n)*sizeof(t)))) #define lj_mem_growvec(L, p, n, m, t) \ ((p) = (t *)lj_mem_grow(L, (p), &(n), (m), (MSize)sizeof(t))) #define lj_mem_freevec(g, p, n, t) lj_mem_free(g, (p), (n)*sizeof(t)) diff --git a/src/lj_ir.c b/src/lj_ir.c index 460cd307..2eabdb4b 100644 --- a/src/lj_ir.c +++ b/src/lj_ir.c @@ -253,7 +253,7 @@ TRef lj_ir_k64(jit_State *J, IROp op, cTValue *tv) goto found; ref = ir_nextk(J); ir = IR(ref); - lua_assert(checkptr32(tv)); + lua_assert(checkptrGC(tv)); setmref(ir->ptr, tv); ir->t.irt = t; ir->o = op; diff --git a/src/lj_obj.h b/src/lj_obj.h index daa62e34..99e2d819 100644 --- a/src/lj_obj.h +++ b/src/lj_obj.h @@ -15,8 +15,9 @@ /* -- Memory references (32 bit address space) ---------------------------- */ -/* Memory size. */ +/* Memory and GC object sizes. */ typedef uint32_t MSize; +typedef uint32_t GCSize; /* Memory reference */ typedef struct MRef { @@ -490,8 +491,8 @@ typedef enum { #define mmname_str(g, mm) (strref((g)->gcroot[GCROOT_MMNAME+(mm)])) typedef struct GCState { - MSize total; /* Memory currently allocated. */ - MSize threshold; /* Memory threshold. */ + GCSize total; /* Memory currently allocated. */ + GCSize threshold; /* Memory threshold. */ uint8_t currentwhite; /* Current white color. */ uint8_t state; /* GC state. */ uint8_t nocdatafin; /* No cdata finalizer called. */ @@ -503,9 +504,9 @@ typedef struct GCState { GCRef grayagain; /* List of objects for atomic traversal. */ GCRef weak; /* List of weak tables (to be cleared). */ GCRef mmudata; /* List of userdata (to be finalized). */ + GCSize debt; /* Debt (how much GC is behind schedule). */ + GCSize estimate; /* Estimate of memory actually in use. */ MSize stepmul; /* Incremental GC step granularity. */ - MSize debt; /* Debt (how much GC is behind schedule). */ - MSize estimate; /* Estimate of memory actually in use. */ MSize pause; /* Pause between successive GC cycles. */ } GCState; diff --git a/src/lj_state.c b/src/lj_state.c index 444f269d..344f8fe1 100644 --- a/src/lj_state.c +++ b/src/lj_state.c @@ -187,7 +187,7 @@ LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud) GG_State *GG = (GG_State *)f(ud, NULL, 0, sizeof(GG_State)); lua_State *L = &GG->L; global_State *g = &GG->g; - if (GG == NULL || !checkptr32(GG)) return NULL; + if (GG == NULL || !checkptrGC(GG)) return NULL; memset(GG, 0, sizeof(GG_State)); L->gct = ~LJ_TTHREAD; L->marked = LJ_GC_WHITE0 | LJ_GC_FIXED | LJ_GC_SFIXED; /* Prevent free. */