diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-09-29 09:42:13 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-09-29 09:42:13 -0300 |
commit | dad808a73a98a23729614b8814728d76b4e5d577 (patch) | |
tree | 945fabce1906c5f08fe6512476d7ca3d84017bca /lfunc.c | |
parent | ca7fd50a4ec2f1b41292f859ba0d5e52a2b22a5c (diff) | |
download | lua-dad808a73a98a23729614b8814728d76b4e5d577.tar.gz lua-dad808a73a98a23729614b8814728d76b4e5d577.tar.bz2 lua-dad808a73a98a23729614b8814728d76b4e5d577.zip |
new way to count `nblocks' for GC (try to count bytes).
Diffstat (limited to 'lfunc.c')
-rw-r--r-- | lfunc.c | 36 |
1 files changed, 27 insertions, 9 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lfunc.c,v 1.29 2000/08/09 19:16:57 roberto Exp roberto $ | 2 | ** $Id: lfunc.c,v 1.30 2000/08/22 17:44:17 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 | */ |
@@ -13,19 +13,18 @@ | |||
13 | #include "lmem.h" | 13 | #include "lmem.h" |
14 | #include "lstate.h" | 14 | #include "lstate.h" |
15 | 15 | ||
16 | #define gcsizeproto(L, p) numblocks(L, 0, sizeof(Proto)) | ||
17 | #define gcsizeclosure(L, c) numblocks(L, c->nupvalues, sizeof(Closure)) | ||
18 | 16 | ||
17 | #define sizeclosure(n) (sizeof(Closure) + (lint32)sizeof(TObject)*((n)-1)) | ||
19 | 18 | ||
20 | 19 | ||
21 | Closure *luaF_newclosure (lua_State *L, int nelems) { | 20 | Closure *luaF_newclosure (lua_State *L, int nelems) { |
22 | Closure *c = (Closure *)luaM_malloc(L, sizeof(Closure) + | 21 | lint32 size = sizeclosure(nelems); |
23 | (lint32)sizeof(TObject)*(nelems-1)); | 22 | Closure *c = (Closure *)luaM_malloc(L, size); |
24 | c->next = L->rootcl; | 23 | c->next = L->rootcl; |
25 | L->rootcl = c; | 24 | L->rootcl = c; |
26 | c->mark = c; | 25 | c->mark = c; |
27 | c->nupvalues = nelems; | 26 | c->nupvalues = nelems; |
28 | L->nblocks += gcsizeclosure(L, c); | 27 | L->nblocks += size; |
29 | return c; | 28 | return c; |
30 | } | 29 | } |
31 | 30 | ||
@@ -33,7 +32,9 @@ Closure *luaF_newclosure (lua_State *L, int nelems) { | |||
33 | Proto *luaF_newproto (lua_State *L) { | 32 | Proto *luaF_newproto (lua_State *L) { |
34 | Proto *f = luaM_new(L, Proto); | 33 | Proto *f = luaM_new(L, Proto); |
35 | f->code = NULL; | 34 | f->code = NULL; |
35 | f->ncode = 0; | ||
36 | f->lineinfo = NULL; | 36 | f->lineinfo = NULL; |
37 | f->nlineinfo = 0; | ||
37 | f->lineDefined = 0; | 38 | f->lineDefined = 0; |
38 | f->source = NULL; | 39 | f->source = NULL; |
39 | f->kstr = NULL; | 40 | f->kstr = NULL; |
@@ -47,13 +48,30 @@ Proto *luaF_newproto (lua_State *L) { | |||
47 | f->next = L->rootproto; | 48 | f->next = L->rootproto; |
48 | L->rootproto = f; | 49 | L->rootproto = f; |
49 | f->marked = 0; | 50 | f->marked = 0; |
50 | L->nblocks += gcsizeproto(L, f); | ||
51 | return f; | 51 | return f; |
52 | } | 52 | } |
53 | 53 | ||
54 | 54 | ||
55 | static size_t protosize (Proto *f) { | ||
56 | return sizeof(Proto) | ||
57 | + f->nknum*sizeof(Number) | ||
58 | + f->nkstr*sizeof(TString *) | ||
59 | + f->nkproto*sizeof(Proto *) | ||
60 | + f->ncode*sizeof(Instruction) | ||
61 | + f->nlocvars*sizeof(struct LocVar) | ||
62 | + f->nlineinfo*sizeof(int); | ||
63 | } | ||
64 | |||
65 | |||
66 | void luaF_protook (lua_State *L, Proto *f, int pc) { | ||
67 | f->ncode = pc; /* signal that proto was properly created */ | ||
68 | L->nblocks += protosize(f); | ||
69 | } | ||
70 | |||
71 | |||
55 | void luaF_freeproto (lua_State *L, Proto *f) { | 72 | void luaF_freeproto (lua_State *L, Proto *f) { |
56 | L->nblocks -= gcsizeproto(L, f); | 73 | if (f->ncode > 0) /* function was properly created? */ |
74 | L->nblocks -= protosize(f); | ||
57 | luaM_free(L, f->code); | 75 | luaM_free(L, f->code); |
58 | luaM_free(L, f->locvars); | 76 | luaM_free(L, f->locvars); |
59 | luaM_free(L, f->kstr); | 77 | luaM_free(L, f->kstr); |
@@ -65,7 +83,7 @@ void luaF_freeproto (lua_State *L, Proto *f) { | |||
65 | 83 | ||
66 | 84 | ||
67 | void luaF_freeclosure (lua_State *L, Closure *c) { | 85 | void luaF_freeclosure (lua_State *L, Closure *c) { |
68 | L->nblocks -= gcsizeclosure(L, c); | 86 | L->nblocks -= sizeclosure(c->nupvalues); |
69 | luaM_free(L, c); | 87 | luaM_free(L, c); |
70 | } | 88 | } |
71 | 89 | ||