static void freeobj (lua_State *L, GCObject *o) { switch (gch(o)->tt) { case LUA_TPROTO: luaF_freeproto(L, gco2p(o)); break; case LUA_TLCL: { luaM_freemem(L, o, sizeLclosure(gco2lcl(o)->nupvalues)); break; } case LUA_TCCL: { luaM_freemem(L, o, sizeCclosure(gco2ccl(o)->nupvalues)); break; } case LUA_TUPVAL: luaF_freeupval(L, gco2uv(o)); break; case LUA_TTABLE: luaH_free(L, gco2t(o)); break; case LUA_TTHREAD: luaE_freethread(L, gco2th(o)); break; case LUA_TUSERDATA: luaM_freemem(L, o, sizeudata(gco2u(o))); break; case LUA_TSHRSTR: G(L)->strt.nuse--; /* go through */ case LUA_TLNGSTR: { luaM_freemem(L, o, sizestring(gco2ts(o))); break; } default: lua_assert(0); } }
Closure *luaF_newLclosure (lua_State *L, int n) { Closure *c = &luaC_newobj(L, LUA_TLCL, sizeLclosure(n), NULL, 0)->cl; c->l.p = NULL; c->l.nupvalues = cast_byte(n); while (n--) c->l.upvals[n] = NULL; return c; }
static lu_mem traverseLclosure (global_State *g, LClosure *cl) { int i; markobject(g, cl->p); /* mark its prototype */ for (i = 0; i < cl->nupvalues; i++) /* mark its upvalues */ markobject(g, cl->upvals[i]); return sizeLclosure(cl->nupvalues); }
int main(int argc, char const* argv[]) { int i; int n = 10; LClosure* c = (LClosure*) malloc(sizeLclosure(n)); assert(c); c->foo = 0; c->goo = 0; for (i = 0; i < n; ++i) { c->num_array[i] = (Num*) malloc(sizeof(Num)); assert(c->num_array[i]); c->num_array[i]->i = i; } dprint(c->foo); dprint(c->goo); for (i = 0; i < n; ++i) { dprint(c->num_array[i]->i); } return 0; }
int debug_getsize(lua_State* L) { TValue* o = L->base; switch (o->tt) { /* Container types */ case LUA_TTABLE: { Table *h = hvalue(o); lua_pushinteger(L, sizeof(Table) + sizeof(TValue) * h->sizearray + sizeof(Node) * sizenode(h)); break; } case LUA_TFUNCTION: { Closure *cl = clvalue(o); lua_pushinteger(L, (cl->c.isC) ? sizeCclosure(cl->c.nupvalues) : sizeLclosure(cl->l.nupvalues)); break; } case LUA_TTHREAD: { lua_State *th = thvalue(o); lua_pushinteger(L, sizeof(lua_State) + sizeof(TValue) * th->stacksize + sizeof(CallInfo) * th->size_ci); break; } case LUA_TPROTO: { Proto *p = pvalue(o); lua_pushinteger(L, sizeof(Proto) + sizeof(Instruction) * p->sizecode + sizeof(Proto *) * p->sizep + sizeof(TValue) * p->sizek + sizeof(int) * p->sizelineinfo + sizeof(LocVar) * p->sizelocvars + sizeof(TString *) * p->sizeupvalues); break; } /* Non-containers */ case LUA_TUSERDATA: { lua_pushnumber(L, uvalue(o)->len); break; } case LUA_TLIGHTUSERDATA: { lua_pushnumber(L, sizeof(void*)); break; } case LUA_TSTRING: { TString *s = rawtsvalue(o); lua_pushinteger(L, sizeof(TString) + s->tsv.len + 1); break; } case LUA_TNUMBER: { lua_pushinteger(L, sizeof(lua_Number)); break; } case LUA_TBOOLEAN: { lua_pushinteger(L, sizeof(int)); break; } default: return 0; } return 1; }
LClosure *luaF_newLclosure (lua_State *L, int n) { GCObject *o = luaC_newobj(L, LUA_TLCL, sizeLclosure(n)); LClosure *c = gco2lcl(o); c->p = NULL; c->nupvalues = cast_byte(n); while (n--) c->upvals[n] = NULL; return c; }
Closure *luaF_newLclosure (lua_State *L, Proto *p) { int n = p->sizeupvalues; Closure *c = &luaC_newobj(L, LUA_TFUNCTION, sizeLclosure(n), NULL, 0)->cl; c->l.isC = 0; c->l.p = p; c->l.nupvalues = cast_byte(n); while (n--) c->l.upvals[n] = NULL; return c; }
static void freeLclosure (lua_State *L, LClosure *cl) { int i; for (i = 0; i < cl->nupvalues; i++) { UpVal *uv = cl->upvals[i]; if (uv) luaC_upvdeccount(L, uv); } luaM_freemem(L, cl, sizeLclosure(cl->nupvalues)); }
/* ** open upvalues point to values in a thread, so those values should ** be marked when the thread is traversed except in the atomic phase ** (because then the value cannot be changed by the thread and the ** thread may not be traversed again) */ static lu_mem traverseLclosure (global_State *g, LClosure *cl) { int i; markobjectN(g, cl->p); /* mark its prototype */ for (i = 0; i < cl->nupvalues; i++) { /* mark its upvalues */ UpVal *uv = cl->upvals[i]; if (uv != NULL) { if (upisopen(uv) && g->gcstate != GCSinsideatomic) uv->u.open.touched = 1; /* can be marked in 'remarkupvals' */ else markvalue(g, uv->v); } } return sizeLclosure(cl->nupvalues); }
Closure *lua_newLclosure(lua_State *luaState, int numElements, Table *elementTable) { Closure *c = (Closure *)lua_malloc(luaState, sizeLclosure(numElements)); lua_linkObjToGC(luaState, obj2gco(c), LUA_TFUNCTION); c->l.isC = 0; c->l.env = elementTable; c->l.nupvalues = cast_byte(numElements); while (numElements--) { c->l.upvals[numElements] = NULL; } return c; }
/* ** traverse one gray object, turning it to black. ** Returns `quantity' traversed. */ static l_mem propagatemark (global_State *g) { GCObject *o = g->gray; lua_assert(isgray(o)); gray2black(o); switch (o->gch.tt) { case LUA_TTABLE: { Table *h = gco2h(o); g->gray = h->gclist; if (traversetable(g, h)) /* table is weak? */ black2gray(o); /* keep it gray */ return sizeof(Table) + sizeof(TValue) * h->sizearray + sizeof(Node) * sizenode(h); } case LUA_TFUNCTION: { Closure *cl = gco2cl(o); g->gray = cl->c.gclist; traverseclosure(g, cl); return (cl->c.isC) ? sizeCclosure(cl->c.nupvalues) : sizeLclosure(cl->l.nupvalues); } case LUA_TTHREAD: { lua_State *th = gco2th(o); g->gray = th->gclist; th->gclist = g->grayagain; g->grayagain = o; black2gray(o); traversestack(g, th); return sizeof(lua_State) + sizeof(TValue) * th->stacksize + sizeof(CallInfo) * th->size_ci; } case LUA_TPROTO: { Proto *p = gco2p(o); g->gray = p->gclist; traverseproto(g, p); return sizeof(Proto) + sizeof(Instruction) * p->sizecode + sizeof(Proto *) * p->sizep + sizeof(TValue) * p->sizek + sizeof(int) * p->sizelineinfo + sizeof(LocVar) * p->sizelocvars + sizeof(TString *) * p->sizeupvalues; } default: lua_assert(0); return 0; } }
void luaF_freeclosure (lua_State *L, Closure *c) { int size = (c->c.isC) ? sizeCclosure(c->c.nupvalues) : sizeLclosure(c->l.nupvalues); luaM_freemem(L, c, size); }