Skip to content

Commit

Permalink
`free' gets size of the block: complete control over memory use
Browse files Browse the repository at this point in the history
  • Loading branch information
roberto-ieru committed Dec 28, 2000
1 parent 8c49e19 commit 0183b80
Show file tree
Hide file tree
Showing 20 changed files with 207 additions and 224 deletions.
3 changes: 1 addition & 2 deletions lapi.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: lapi.c,v 1.112 2000/12/04 18:33:40 roberto Exp roberto $
** $Id: lapi.c,v 1.113 2000/12/26 18:46:09 roberto Exp roberto $
** Lua API
** See Copyright Notice in lua.h
*/
Expand Down Expand Up @@ -362,7 +362,6 @@ LUA_API int lua_ref (lua_State *L, int lock) {
else { /* no more free places */
luaM_growvector(L, L->refArray, L->nref, L->sizeref, struct Ref,
MAX_INT, "reference table overflow");
L->nblocks += sizeof(struct Ref);
ref = L->nref++;
}
L->refArray[ref].o = *(L->top-1);
Expand Down
24 changes: 13 additions & 11 deletions lcode.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: lcode.c,v 1.53 2000/12/04 18:33:40 roberto Exp roberto $
** $Id: lcode.c,v 1.54 2000/12/26 18:46:09 roberto Exp roberto $
** Code generator for Lua
** See Copyright Notice in lua.h
*/
Expand Down Expand Up @@ -102,14 +102,14 @@ void luaK_kstr (LexState *ls, int c) {
static int number_constant (FuncState *fs, lua_Number r) {
/* check whether `r' has appeared within the last LOOKBACKNUMS entries */
Proto *f = fs->f;
int c = f->nknum;
int c = fs->nknum;
int lim = c < LOOKBACKNUMS ? 0 : c-LOOKBACKNUMS;
while (--c >= lim)
if (f->knum[c] == r) return c;
/* not found; create a new entry */
luaM_growvector(fs->L, f->knum, f->nknum, fs->sizeknum, lua_Number,
luaM_growvector(fs->L, f->knum, fs->nknum, f->sizeknum, lua_Number,
MAXARG_U, "constant table overflow");
c = f->nknum++;
c = fs->nknum++;
f->knum[c] = r;
return c;
}
Expand Down Expand Up @@ -424,13 +424,13 @@ static void codelineinfo (FuncState *fs) {
LexState *ls = fs->ls;
if (ls->lastline > fs->lastline) {
if (ls->lastline > fs->lastline+1) {
luaM_growvector(fs->L, f->lineinfo, f->nlineinfo, fs->sizelineinfo, int,
luaM_growvector(fs->L, f->lineinfo, fs->nlineinfo, f->sizelineinfo, int,
MAX_INT, "line info overflow");
f->lineinfo[f->nlineinfo++] = -(ls->lastline - (fs->lastline+1));
f->lineinfo[fs->nlineinfo++] = -(ls->lastline - (fs->lastline+1));
}
luaM_growvector(fs->L, f->lineinfo, f->nlineinfo, fs->sizelineinfo, int,
luaM_growvector(fs->L, f->lineinfo, fs->nlineinfo, f->sizelineinfo, int,
MAX_INT, "line info overflow");
f->lineinfo[f->nlineinfo++] = fs->pc;
f->lineinfo[fs->nlineinfo++] = fs->pc;
fs->lastline = ls->lastline;
}
}
Expand All @@ -447,6 +447,7 @@ int luaK_code1 (FuncState *fs, OpCode o, int arg1) {


int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2) {
Proto *f;
Instruction i = previous_instruction(fs);
int delta = (int)luaK_opproperties[o].push - (int)luaK_opproperties[o].pop;
int optm = 0; /* 1 when there is an optimization */
Expand Down Expand Up @@ -629,9 +630,10 @@ int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2) {
break;
}
}
f = fs->f;
luaK_deltastack(fs, delta);
if (optm) { /* optimize: put instruction in place of last one */
fs->f->code[fs->pc-1] = i; /* change previous instruction */
f->code[fs->pc-1] = i; /* change previous instruction */
return fs->pc-1; /* do not generate new instruction */
}
/* else build new instruction */
Expand All @@ -643,9 +645,9 @@ int luaK_code2 (FuncState *fs, OpCode o, int arg1, int arg2) {
}
codelineinfo(fs);
/* put new instruction in code array */
luaM_growvector(fs->L, fs->f->code, fs->pc, fs->sizecode, Instruction,
luaM_growvector(fs->L, f->code, fs->pc, f->sizecode, Instruction,
MAX_INT, "code size overflow");
fs->f->code[fs->pc] = i;
f->code[fs->pc] = i;
return fs->pc++;
}

Expand Down
26 changes: 14 additions & 12 deletions ldo.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: ldo.c,v 1.109 2000/10/30 12:38:50 roberto Exp roberto $
** $Id: ldo.c,v 1.110 2000/11/24 17:39:56 roberto Exp roberto $
** Stack and Call structure of Lua
** See Copyright Notice in lua.h
*/
Expand Down Expand Up @@ -28,38 +28,40 @@


/* space to handle stack overflow errors */
#define EXTRA_STACK (2*LUA_MINSTACK)
#define EXTRA_STACK (2*LUA_MINSTACK)


static void restore_stack_limit (lua_State *L) {
StkId limit = L->stack+(L->stacksize-EXTRA_STACK)-1;
if (L->top < limit)
L->stack_last = limit;
}


void luaD_init (lua_State *L, int stacksize) {
L->stack = luaM_newvector(L, stacksize+EXTRA_STACK, TObject);
L->nblocks += stacksize*sizeof(TObject);
L->stack_last = L->stack+(stacksize-1);
stacksize += EXTRA_STACK;
L->stack = luaM_newvector(L, stacksize, TObject);
L->stacksize = stacksize;
L->Cbase = L->top = L->stack;
restore_stack_limit(L);
}


void luaD_checkstack (lua_State *L, int n) {
if (L->stack_last - L->top <= n) { /* stack overflow? */
if (L->stack_last-L->stack > (L->stacksize-1)) {
if (L->stack_last == L->stack+L->stacksize-1) {
/* overflow while handling overflow */
luaD_breakrun(L, LUA_ERRERR); /* break run without error message */
}
else {
L->stack_last += EXTRA_STACK; /* to be used by error message */
LUA_ASSERT(L->stack_last == L->stack+L->stacksize-1, "wrong stack limit");
lua_error(L, "stack overflow");
}
}
}


static void restore_stack_limit (lua_State *L) {
if (L->top - L->stack < L->stacksize - 1)
L->stack_last = L->stack + (L->stacksize-1);
}


/*
** Adjust stack. Set top to base+extra, pushing NILs if needed.
** (we cannot add base+extra unless we are sure it fits in the stack;
Expand Down
56 changes: 17 additions & 39 deletions lfunc.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: lfunc.c,v 1.34 2000/10/30 12:20:29 roberto Exp roberto $
** $Id: lfunc.c,v 1.35 2000/12/04 18:33:40 roberto Exp roberto $
** Auxiliary functions to manipulate prototypes and closures
** See Copyright Notice in lua.h
*/
Expand All @@ -18,34 +18,32 @@


Closure *luaF_newclosure (lua_State *L, int nelems) {
int size = sizeclosure(nelems);
Closure *c = (Closure *)luaM_malloc(L, size);
Closure *c = (Closure *)luaM_malloc(L, sizeclosure(nelems));
c->next = L->rootcl;
L->rootcl = c;
c->mark = c;
c->nupvalues = nelems;
L->nblocks += size;
return c;
}


Proto *luaF_newproto (lua_State *L) {
Proto *f = luaM_new(L, Proto);
f->knum = NULL;
f->nknum = 0;
f->sizeknum = 0;
f->kstr = NULL;
f->nkstr = 0;
f->sizekstr = 0;
f->kproto = NULL;
f->nkproto = 0;
f->sizekproto = 0;
f->code = NULL;
f->ncode = 0;
f->sizecode = 0;
f->numparams = 0;
f->is_vararg = 0;
f->maxstacksize = 0;
f->marked = 0;
f->lineinfo = NULL;
f->nlineinfo = 0;
f->nlocvars = 0;
f->sizelocvars = 0;
f->sizelineinfo = 0;
f->locvars = NULL;
f->lineDefined = 0;
f->source = NULL;
Expand All @@ -55,39 +53,19 @@ Proto *luaF_newproto (lua_State *L) {
}


static size_t protosize (Proto *f) {
return sizeof(Proto)
+ f->nknum*sizeof(lua_Number)
+ f->nkstr*sizeof(TString *)
+ f->nkproto*sizeof(Proto *)
+ f->ncode*sizeof(Instruction)
+ f->nlocvars*sizeof(struct LocVar)
+ f->nlineinfo*sizeof(int);
}


void luaF_protook (lua_State *L, Proto *f, int pc) {
f->ncode = pc; /* signal that proto was properly created */
L->nblocks += protosize(f);
}


void luaF_freeproto (lua_State *L, Proto *f) {
if (f->ncode > 0) /* function was properly created? */
L->nblocks -= protosize(f);
luaM_free(L, f->code);
luaM_free(L, f->locvars);
luaM_free(L, f->kstr);
luaM_free(L, f->knum);
luaM_free(L, f->kproto);
luaM_free(L, f->lineinfo);
luaM_free(L, f);
luaM_freearray(L, f->code, f->sizecode, Instruction);
luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar);
luaM_freearray(L, f->kstr, f->sizekstr, TString *);
luaM_freearray(L, f->knum, f->sizeknum, lua_Number);
luaM_freearray(L, f->kproto, f->sizekproto, Proto *);
luaM_freearray(L, f->lineinfo, f->sizelineinfo, int);
luaM_freelem(L, f, Proto);
}


void luaF_freeclosure (lua_State *L, Closure *c) {
L->nblocks -= sizeclosure(c->nupvalues);
luaM_free(L, c);
luaM_free(L, c, sizeclosure(c->nupvalues));
}


Expand All @@ -97,7 +75,7 @@ void luaF_freeclosure (lua_State *L, Closure *c) {
*/
const char *luaF_getlocalname (const Proto *f, int local_number, int pc) {
int i;
for (i = 0; i<f->nlocvars && f->locvars[i].startpc <= pc; i++) {
for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
if (pc < f->locvars[i].endpc) { /* is variable active? */
local_number--;
if (local_number == 0)
Expand Down
3 changes: 1 addition & 2 deletions lfunc.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: lfunc.h,v 1.12 2000/06/26 19:28:31 roberto Exp roberto $
** $Id: lfunc.h,v 1.13 2000/09/29 12:42:13 roberto Exp roberto $
** Auxiliary functions to manipulate prototypes and closures
** See Copyright Notice in lua.h
*/
Expand All @@ -13,7 +13,6 @@


Proto *luaF_newproto (lua_State *L);
void luaF_protook (lua_State *L, Proto *f, int pc);
Closure *luaF_newclosure (lua_State *L, int nelems);
void luaF_freeproto (lua_State *L, Proto *f);
void luaF_freeclosure (lua_State *L, Closure *c);
Expand Down
17 changes: 7 additions & 10 deletions lgc.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
** $Id: lgc.c,v 1.73 2000/11/24 17:39:56 roberto Exp roberto $
** $Id: lgc.c,v 1.74 2000/12/26 18:46:09 roberto Exp roberto $
** Garbage Collector
** See Copyright Notice in lua.h
*/
Expand Down Expand Up @@ -37,11 +37,11 @@ static void protomark (Proto *f) {
int i;
f->marked = 1;
strmark(f->source);
for (i=0; i<f->nkstr; i++)
for (i=0; i<f->sizekstr; i++)
strmark(f->kstr[i]);
for (i=0; i<f->nkproto; i++)
for (i=0; i<f->sizekproto; i++)
protomark(f->kproto[i]);
for (i=0; i<f->nlocvars; i++) /* mark local-variable names */
for (i=0; i<f->sizelocvars; i++) /* mark local-variable names */
strmark(f->locvars[i].varname);
}
}
Expand Down Expand Up @@ -248,8 +248,7 @@ static void collectstrings (lua_State *L, int all) {
else { /* collect */
*p = next->nexthash;
L->strt.nuse--;
L->nblocks -= sizestring(next->len);
luaM_free(L, next);
luaM_free(L, next, sizestring(next->len));
}
}
}
Expand All @@ -273,7 +272,6 @@ static void collectudata (lua_State *L, int all) {
*p = next->nexthash;
next->nexthash = L->TMtable[tag].collected; /* chain udata */
L->TMtable[tag].collected = next;
L->nblocks -= sizestring(next->len);
L->udt.nuse--;
}
}
Expand All @@ -286,9 +284,8 @@ static void collectudata (lua_State *L, int all) {
static void checkMbuffer (lua_State *L) {
if (L->Mbuffsize > MINBUFFER*2) { /* is buffer too big? */
size_t newsize = L->Mbuffsize/2; /* still larger than MINBUFFER */
L->nblocks -= (L->Mbuffsize - newsize)*sizeof(char);
luaM_reallocvector(L, L->Mbuffer, L->Mbuffsize, newsize, char);
L->Mbuffsize = newsize;
luaM_reallocvector(L, L->Mbuffer, newsize, char);
}
}

Expand Down Expand Up @@ -320,7 +317,7 @@ static void callgcTMudata (lua_State *L) {
L->TMtable[tag].collected = udata->nexthash; /* remove it from list */
tsvalue(&o) = udata;
callgcTM(L, &o);
luaM_free(L, udata);
luaM_free(L, udata, sizeudata(udata->len));
}
}
}
Expand Down
Loading

0 comments on commit 0183b80

Please sign in to comment.