diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-12-28 10:55:41 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-12-28 10:55:41 -0200 |
| commit | 0183b8030c80f57b87874ff7867ccdb172d9d3dc (patch) | |
| tree | 1033b5a84489a2f1f1bd210b1b120155cd7aeed7 /lfunc.c | |
| parent | 8c49e198654567f770a7d5081b886a7c35201d81 (diff) | |
| download | lua-0183b8030c80f57b87874ff7867ccdb172d9d3dc.tar.gz lua-0183b8030c80f57b87874ff7867ccdb172d9d3dc.tar.bz2 lua-0183b8030c80f57b87874ff7867ccdb172d9d3dc.zip | |
`free' gets size of the block: complete control over memory use
Diffstat (limited to 'lfunc.c')
| -rw-r--r-- | lfunc.c | 56 |
1 files changed, 17 insertions, 39 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lfunc.c,v 1.34 2000/10/30 12:20:29 roberto Exp roberto $ | 2 | ** $Id: lfunc.c,v 1.35 2000/12/04 18:33:40 roberto Exp roberto $ |
| 3 | ** Auxiliary functions to manipulate prototypes and closures | 3 | ** Auxiliary functions to manipulate prototypes and closures |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -18,13 +18,11 @@ | |||
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | Closure *luaF_newclosure (lua_State *L, int nelems) { | 20 | Closure *luaF_newclosure (lua_State *L, int nelems) { |
| 21 | int size = sizeclosure(nelems); | 21 | Closure *c = (Closure *)luaM_malloc(L, sizeclosure(nelems)); |
| 22 | Closure *c = (Closure *)luaM_malloc(L, size); | ||
| 23 | c->next = L->rootcl; | 22 | c->next = L->rootcl; |
| 24 | L->rootcl = c; | 23 | L->rootcl = c; |
| 25 | c->mark = c; | 24 | c->mark = c; |
| 26 | c->nupvalues = nelems; | 25 | c->nupvalues = nelems; |
| 27 | L->nblocks += size; | ||
| 28 | return c; | 26 | return c; |
| 29 | } | 27 | } |
| 30 | 28 | ||
| @@ -32,20 +30,20 @@ Closure *luaF_newclosure (lua_State *L, int nelems) { | |||
| 32 | Proto *luaF_newproto (lua_State *L) { | 30 | Proto *luaF_newproto (lua_State *L) { |
| 33 | Proto *f = luaM_new(L, Proto); | 31 | Proto *f = luaM_new(L, Proto); |
| 34 | f->knum = NULL; | 32 | f->knum = NULL; |
| 35 | f->nknum = 0; | 33 | f->sizeknum = 0; |
| 36 | f->kstr = NULL; | 34 | f->kstr = NULL; |
| 37 | f->nkstr = 0; | 35 | f->sizekstr = 0; |
| 38 | f->kproto = NULL; | 36 | f->kproto = NULL; |
| 39 | f->nkproto = 0; | 37 | f->sizekproto = 0; |
| 40 | f->code = NULL; | 38 | f->code = NULL; |
| 41 | f->ncode = 0; | 39 | f->sizecode = 0; |
| 42 | f->numparams = 0; | 40 | f->numparams = 0; |
| 43 | f->is_vararg = 0; | 41 | f->is_vararg = 0; |
| 44 | f->maxstacksize = 0; | 42 | f->maxstacksize = 0; |
| 45 | f->marked = 0; | 43 | f->marked = 0; |
| 46 | f->lineinfo = NULL; | 44 | f->lineinfo = NULL; |
| 47 | f->nlineinfo = 0; | 45 | f->sizelocvars = 0; |
| 48 | f->nlocvars = 0; | 46 | f->sizelineinfo = 0; |
| 49 | f->locvars = NULL; | 47 | f->locvars = NULL; |
| 50 | f->lineDefined = 0; | 48 | f->lineDefined = 0; |
| 51 | f->source = NULL; | 49 | f->source = NULL; |
| @@ -55,39 +53,19 @@ Proto *luaF_newproto (lua_State *L) { | |||
| 55 | } | 53 | } |
| 56 | 54 | ||
| 57 | 55 | ||
| 58 | static size_t protosize (Proto *f) { | ||
| 59 | return sizeof(Proto) | ||
| 60 | + f->nknum*sizeof(lua_Number) | ||
| 61 | + f->nkstr*sizeof(TString *) | ||
| 62 | + f->nkproto*sizeof(Proto *) | ||
| 63 | + f->ncode*sizeof(Instruction) | ||
| 64 | + f->nlocvars*sizeof(struct LocVar) | ||
| 65 | + f->nlineinfo*sizeof(int); | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | void luaF_protook (lua_State *L, Proto *f, int pc) { | ||
| 70 | f->ncode = pc; /* signal that proto was properly created */ | ||
| 71 | L->nblocks += protosize(f); | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | void luaF_freeproto (lua_State *L, Proto *f) { | 56 | void luaF_freeproto (lua_State *L, Proto *f) { |
| 76 | if (f->ncode > 0) /* function was properly created? */ | 57 | luaM_freearray(L, f->code, f->sizecode, Instruction); |
| 77 | L->nblocks -= protosize(f); | 58 | luaM_freearray(L, f->locvars, f->sizelocvars, struct LocVar); |
| 78 | luaM_free(L, f->code); | 59 | luaM_freearray(L, f->kstr, f->sizekstr, TString *); |
| 79 | luaM_free(L, f->locvars); | 60 | luaM_freearray(L, f->knum, f->sizeknum, lua_Number); |
| 80 | luaM_free(L, f->kstr); | 61 | luaM_freearray(L, f->kproto, f->sizekproto, Proto *); |
| 81 | luaM_free(L, f->knum); | 62 | luaM_freearray(L, f->lineinfo, f->sizelineinfo, int); |
| 82 | luaM_free(L, f->kproto); | 63 | luaM_freelem(L, f, Proto); |
| 83 | luaM_free(L, f->lineinfo); | ||
| 84 | luaM_free(L, f); | ||
| 85 | } | 64 | } |
| 86 | 65 | ||
| 87 | 66 | ||
| 88 | void luaF_freeclosure (lua_State *L, Closure *c) { | 67 | void luaF_freeclosure (lua_State *L, Closure *c) { |
| 89 | L->nblocks -= sizeclosure(c->nupvalues); | 68 | luaM_free(L, c, sizeclosure(c->nupvalues)); |
| 90 | luaM_free(L, c); | ||
| 91 | } | 69 | } |
| 92 | 70 | ||
| 93 | 71 | ||
| @@ -97,7 +75,7 @@ void luaF_freeclosure (lua_State *L, Closure *c) { | |||
| 97 | */ | 75 | */ |
| 98 | const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { | 76 | const char *luaF_getlocalname (const Proto *f, int local_number, int pc) { |
| 99 | int i; | 77 | int i; |
| 100 | for (i = 0; i<f->nlocvars && f->locvars[i].startpc <= pc; i++) { | 78 | for (i = 0; i<f->sizelocvars && f->locvars[i].startpc <= pc; i++) { |
| 101 | if (pc < f->locvars[i].endpc) { /* is variable active? */ | 79 | if (pc < f->locvars[i].endpc) { /* is variable active? */ |
| 102 | local_number--; | 80 | local_number--; |
| 103 | if (local_number == 0) | 81 | if (local_number == 0) |
