aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lfunc.c31
-rw-r--r--lgc.c16
-rw-r--r--lgc.h5
-rw-r--r--lmem.c18
-rw-r--r--lmem.h6
-rw-r--r--lstring.c16
-rw-r--r--ltable.c5
-rw-r--r--ltests.c14
8 files changed, 59 insertions, 52 deletions
diff --git a/lfunc.c b/lfunc.c
index 4e60ea1a..d0be5e6b 100644
--- a/lfunc.c
+++ b/lfunc.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lfunc.c,v 2.17 2009/11/26 11:39:20 roberto Exp roberto $ 2** $Id: lfunc.c,v 2.18 2009/12/11 13:39:34 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*/
@@ -21,30 +21,27 @@
21 21
22 22
23 23
24Closure *luaF_newCclosure (lua_State *L, int nelems, Table *e) { 24Closure *luaF_newCclosure (lua_State *L, int n, Table *e) {
25 Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems))); 25 Closure *c = &luaC_newobj(L, LUA_TFUNCTION, sizeCclosure(n), NULL, 0)->cl;
26 luaC_link(L, obj2gco(c), LUA_TFUNCTION);
27 c->c.isC = 1; 26 c->c.isC = 1;
28 c->c.env = e; 27 c->c.env = e;
29 c->c.nupvalues = cast_byte(nelems); 28 c->c.nupvalues = cast_byte(n);
30 return c; 29 return c;
31} 30}
32 31
33 32
34Closure *luaF_newLclosure (lua_State *L, int nelems, Table *e) { 33Closure *luaF_newLclosure (lua_State *L, int n, Table *e) {
35 Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems))); 34 Closure *c = &luaC_newobj(L, LUA_TFUNCTION, sizeLclosure(n), NULL, 0)->cl;
36 luaC_link(L, obj2gco(c), LUA_TFUNCTION);
37 c->l.isC = 0; 35 c->l.isC = 0;
38 c->l.env = e; 36 c->l.env = e;
39 c->l.nupvalues = cast_byte(nelems); 37 c->l.nupvalues = cast_byte(n);
40 while (nelems--) c->l.upvals[nelems] = NULL; 38 while (n--) c->l.upvals[n] = NULL;
41 return c; 39 return c;
42} 40}
43 41
44 42
45UpVal *luaF_newupval (lua_State *L) { 43UpVal *luaF_newupval (lua_State *L) {
46 UpVal *uv = luaM_new(L, UpVal); 44 UpVal *uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), NULL, 0)->uv;
47 luaC_link(L, obj2gco(uv), LUA_TUPVAL);
48 uv->v = &uv->u.value; 45 uv->v = &uv->u.value;
49 setnilvalue(uv->v); 46 setnilvalue(uv->v);
50 return uv; 47 return uv;
@@ -65,12 +62,9 @@ UpVal *luaF_findupval (lua_State *L, StkId level) {
65 } 62 }
66 pp = &p->next; 63 pp = &p->next;
67 } 64 }
68 uv = luaM_new(L, UpVal); /* not found: create a new one */ 65 /* not found: create a new one */
69 uv->tt = LUA_TUPVAL; 66 uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), pp, 0)->uv;
70 uv->marked = luaC_white(g);
71 uv->v = level; /* current value lives in the stack */ 67 uv->v = level; /* current value lives in the stack */
72 uv->next = *pp; /* chain it in the proper position */
73 *pp = obj2gco(uv);
74 uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */ 68 uv->u.l.prev = &g->uvhead; /* double link it in `uvhead' list */
75 uv->u.l.next = g->uvhead.u.l.next; 69 uv->u.l.next = g->uvhead.u.l.next;
76 uv->u.l.next->u.l.prev = uv; 70 uv->u.l.next->u.l.prev = uv;
@@ -114,8 +108,7 @@ void luaF_close (lua_State *L, StkId level) {
114 108
115 109
116Proto *luaF_newproto (lua_State *L) { 110Proto *luaF_newproto (lua_State *L) {
117 Proto *f = luaM_new(L, Proto); 111 Proto *f = &luaC_newobj(L, LUA_TPROTO, sizeof(Proto), NULL, 0)->p;
118 luaC_link(L, obj2gco(f), LUA_TPROTO);
119 f->k = NULL; 112 f->k = NULL;
120 f->sizek = 0; 113 f->sizek = 0;
121 f->p = NULL; 114 f->p = NULL;
diff --git a/lgc.c b/lgc.c
index 286b2213..7a3df48a 100644
--- a/lgc.c
+++ b/lgc.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lgc.c,v 2.64 2009/12/11 19:14:59 roberto Exp roberto $ 2** $Id: lgc.c,v 2.65 2009/12/11 21:31:14 roberto Exp roberto $
3** Garbage Collector 3** Garbage Collector
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -116,12 +116,20 @@ void luaC_barrierback (lua_State *L, Table *t) {
116} 116}
117 117
118 118
119void luaC_link (lua_State *L, GCObject *o, lu_byte tt) { 119/*
120** create a new collectable object and link it to '*list'
121*/
122GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, GCObject **list,
123 int offset) {
120 global_State *g = G(L); 124 global_State *g = G(L);
125 GCObject *o = obj2gco(cast(char *, luaM_newobject(L, tt, sz)) + offset);
126 if (list == NULL)
127 list = &g->rootgc; /* standard list for collectable objects */
121 gch(o)->marked = luaC_white(g); 128 gch(o)->marked = luaC_white(g);
122 gch(o)->tt = tt; 129 gch(o)->tt = tt;
123 gch(o)->next = g->rootgc; 130 gch(o)->next = *list;
124 g->rootgc = o; 131 *list = o;
132 return o;
125} 133}
126 134
127 135
diff --git a/lgc.h b/lgc.h
index 976b7442..aa969b38 100644
--- a/lgc.h
+++ b/lgc.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lgc.h,v 2.25 2009/12/11 19:14:59 roberto Exp roberto $ 2** $Id: lgc.h,v 2.26 2009/12/11 21:31:14 roberto Exp roberto $
3** Garbage Collector 3** Garbage Collector
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -98,7 +98,8 @@ LUAI_FUNC void luaC_freeallobjects (lua_State *L);
98LUAI_FUNC void luaC_step (lua_State *L); 98LUAI_FUNC void luaC_step (lua_State *L);
99LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask); 99LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
100LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency); 100LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
101LUAI_FUNC void luaC_link (lua_State *L, GCObject *o, lu_byte tt); 101LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz,
102 GCObject **list, int offset);
102LUAI_FUNC void luaC_linkupval (lua_State *L, UpVal *uv); 103LUAI_FUNC void luaC_linkupval (lua_State *L, UpVal *uv);
103LUAI_FUNC void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v); 104LUAI_FUNC void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v);
104LUAI_FUNC void luaC_barrierback (lua_State *L, Table *t); 105LUAI_FUNC void luaC_barrierback (lua_State *L, Table *t);
diff --git a/lmem.c b/lmem.c
index 85ecead2..63ee8b6a 100644
--- a/lmem.c
+++ b/lmem.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmem.c,v 1.72 2006/09/14 12:59:06 roberto Exp roberto $ 2** $Id: lmem.c,v 1.73 2006/09/14 18:42:28 roberto Exp roberto $
3** Interface to Memory Manager 3** Interface to Memory Manager
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -26,12 +26,11 @@
26** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize); 26** void * frealloc (void *ud, void *ptr, size_t osize, size_t nsize);
27** (`osize' is the old size, `nsize' is the new size) 27** (`osize' is the old size, `nsize' is the new size)
28** 28**
29** Lua ensures that (ptr == NULL) iff (osize == 0). 29** * frealloc(ud, NULL, x, s) creates a new block of size `s' (no
30** 30** matter 'x').
31** * frealloc(ud, NULL, 0, x) creates a new block of size `x'
32** 31**
33** * frealloc(ud, p, x, 0) frees the block `p' 32** * frealloc(ud, p, x, 0) frees the block `p'
34** (in this specific case, frealloc must return NULL). 33** (in this specific case, frealloc must return NULL);
35** particularly, frealloc(ud, NULL, 0, 0) does nothing 34** particularly, frealloc(ud, NULL, 0, 0) does nothing
36** (which is equivalent to free(NULL) in ANSI C) 35** (which is equivalent to free(NULL) in ANSI C)
37** 36**
@@ -77,14 +76,15 @@ void *luaM_toobig (lua_State *L) {
77void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { 76void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
78 void *newblock; 77 void *newblock;
79 global_State *g = G(L); 78 global_State *g = G(L);
80 lua_assert((osize == 0) == (block == NULL)); 79 size_t realosize = (block) ? osize : 0;
80 lua_assert((realosize == 0) == (block == NULL));
81#if defined(HARDMEMTESTS) 81#if defined(HARDMEMTESTS)
82 if (nsize > osize && g->GCthreshold != MAX_LUMEM) 82 if (nsize > realosize && g->GCthreshold != MAX_LUMEM)
83 luaC_fullgc(L, 1); /* force a GC whenever possible */ 83 luaC_fullgc(L, 1); /* force a GC whenever possible */
84#endif 84#endif
85 newblock = (*g->frealloc)(g->ud, block, osize, nsize); 85 newblock = (*g->frealloc)(g->ud, block, osize, nsize);
86 if (newblock == NULL && nsize > 0) { 86 if (newblock == NULL && nsize > 0) {
87 lua_assert(nsize > osize); /* cannot fail when shrinking a block */ 87 lua_assert(nsize > realosize); /* cannot fail when shrinking a block */
88 if (g->GCthreshold != MAX_LUMEM) { 88 if (g->GCthreshold != MAX_LUMEM) {
89 luaC_fullgc(L, 1); /* try to free some memory... */ 89 luaC_fullgc(L, 1); /* try to free some memory... */
90 newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */ 90 newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
@@ -93,7 +93,7 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
93 luaD_throw(L, LUA_ERRMEM); 93 luaD_throw(L, LUA_ERRMEM);
94 } 94 }
95 lua_assert((nsize == 0) == (newblock == NULL)); 95 lua_assert((nsize == 0) == (newblock == NULL));
96 g->totalbytes = (g->totalbytes - osize) + nsize; 96 g->totalbytes = (g->totalbytes - realosize) + nsize;
97 return newblock; 97 return newblock;
98} 98}
99 99
diff --git a/lmem.h b/lmem.h
index d1abcbb4..74f236cd 100644
--- a/lmem.h
+++ b/lmem.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmem.h,v 1.33 2007/02/09 13:04:52 roberto Exp roberto $ 2** $Id: lmem.h,v 1.34 2009/04/17 14:40:13 roberto Exp roberto $
3** Interface to Memory Manager 3** Interface to Memory Manager
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -25,11 +25,13 @@
25#define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 25#define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0)
26#define luaM_freearray(L, b, n) luaM_reallocv(L, (b), n, 0, sizeof((b)[0])) 26#define luaM_freearray(L, b, n) luaM_reallocv(L, (b), n, 0, sizeof((b)[0]))
27 27
28#define luaM_malloc(L,t) luaM_realloc_(L, NULL, 0, (t)) 28#define luaM_malloc(L,s) luaM_realloc_(L, NULL, 0, (s))
29#define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 29#define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t)))
30#define luaM_newvector(L,n,t) \ 30#define luaM_newvector(L,n,t) \
31 cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 31 cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t)))
32 32
33#define luaM_newobject(L,tag,s) luaM_realloc_(L, NULL, tag, (s))
34
33#define luaM_growvector(L,v,nelems,size,t,limit,e) \ 35#define luaM_growvector(L,v,nelems,size,t,limit,e) \
34 if ((nelems)+1 > (size)) \ 36 if ((nelems)+1 > (size)) \
35 ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) 37 ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e)))
diff --git a/lstring.c b/lstring.c
index 4b0d398d..0a6bf342 100644
--- a/lstring.c
+++ b/lstring.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstring.c,v 2.14 2009/12/11 19:14:59 roberto Exp roberto $ 2** $Id: lstring.c,v 2.15 2009/12/11 21:31:14 roberto Exp roberto $
3** String table (keeps all strings handled by Lua) 3** String table (keeps all strings handled by Lua)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -51,23 +51,22 @@ void luaS_resize (lua_State *L, int newsize) {
51 51
52static TString *newlstr (lua_State *L, const char *str, size_t l, 52static TString *newlstr (lua_State *L, const char *str, size_t l,
53 unsigned int h) { 53 unsigned int h) {
54 size_t totalsize; /* total size of TString object */
55 GCObject **list; /* (pointer to) list where it will be inserted */
54 TString *ts; 56 TString *ts;
55 stringtable *tb = &G(L)->strt; 57 stringtable *tb = &G(L)->strt;
56 if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char)) 58 if (l+1 > (MAX_SIZET - sizeof(TString))/sizeof(char))
57 luaM_toobig(L); 59 luaM_toobig(L);
58 if (tb->nuse >= cast(lu_int32, tb->size) && tb->size <= MAX_INT/2) 60 if (tb->nuse >= cast(lu_int32, tb->size) && tb->size <= MAX_INT/2)
59 luaS_resize(L, tb->size*2); /* too crowded */ 61 luaS_resize(L, tb->size*2); /* too crowded */
60 ts = cast(TString *, luaM_malloc(L, (l+1)*sizeof(char)+sizeof(TString))); 62 totalsize = sizeof(TString) + ((l + 1) * sizeof(char));
63 list = &tb->hash[lmod(h, tb->size)];
64 ts = &luaC_newobj(L, LUA_TSTRING, totalsize, list, 0)->ts;
61 ts->tsv.len = l; 65 ts->tsv.len = l;
62 ts->tsv.hash = h; 66 ts->tsv.hash = h;
63 ts->tsv.marked = luaC_white(G(L));
64 ts->tsv.tt = LUA_TSTRING;
65 ts->tsv.reserved = 0; 67 ts->tsv.reserved = 0;
66 memcpy(ts+1, str, l*sizeof(char)); 68 memcpy(ts+1, str, l*sizeof(char));
67 ((char *)(ts+1))[l] = '\0'; /* ending 0 */ 69 ((char *)(ts+1))[l] = '\0'; /* ending 0 */
68 h = lmod(h, tb->size);
69 ts->tsv.next = tb->hash[h]; /* chain new entry */
70 tb->hash[h] = obj2gco(ts);
71 tb->nuse++; 70 tb->nuse++;
72 return ts; 71 return ts;
73} 72}
@@ -99,8 +98,7 @@ Udata *luaS_newudata (lua_State *L, size_t s, Table *e) {
99 Udata *u; 98 Udata *u;
100 if (s > MAX_SIZET - sizeof(Udata)) 99 if (s > MAX_SIZET - sizeof(Udata))
101 luaM_toobig(L); 100 luaM_toobig(L);
102 u = cast(Udata *, luaM_malloc(L, s + sizeof(Udata))); 101 u = &luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s, NULL, 0)->u;
103 luaC_link(L, obj2gco(u), LUA_TUSERDATA);
104 u->uv.len = s; 102 u->uv.len = s;
105 u->uv.metatable = NULL; 103 u->uv.metatable = NULL;
106 u->uv.env = e; 104 u->uv.env = e;
diff --git a/ltable.c b/ltable.c
index d6e8e526..287a5020 100644
--- a/ltable.c
+++ b/ltable.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltable.c,v 2.45 2009/11/19 17:54:07 roberto Exp roberto $ 2** $Id: ltable.c,v 2.46 2009/11/26 11:39:20 roberto Exp roberto $
3** Lua tables (hash) 3** Lua tables (hash)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -357,8 +357,7 @@ static void rehash (lua_State *L, Table *t, const TValue *ek) {
357 357
358 358
359Table *luaH_new (lua_State *L) { 359Table *luaH_new (lua_State *L) {
360 Table *t = luaM_new(L, Table); 360 Table *t = &luaC_newobj(L, LUA_TTABLE, sizeof(Table), NULL, 0)->h;
361 luaC_link(L, obj2gco(t), LUA_TTABLE);
362 t->metatable = NULL; 361 t->metatable = NULL;
363 t->flags = cast_byte(~0); 362 t->flags = cast_byte(~0);
364 t->array = NULL; 363 t->array = NULL;
diff --git a/ltests.c b/ltests.c
index 0e1c9226..7114a175 100644
--- a/ltests.c
+++ b/ltests.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltests.c,v 2.82 2009/12/10 18:21:28 roberto Exp roberto $ 2** $Id: ltests.c,v 2.83 2009/12/11 19:14:59 roberto Exp roberto $
3** Internal Module for Debugging of the Lua Implementation 3** Internal Module for Debugging of the Lua Implementation
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -92,8 +92,7 @@ static int tpanic (lua_State *L) {
92#define fillmem(mem,size) /* empty */ 92#define fillmem(mem,size) /* empty */
93#endif 93#endif
94 94
95 95Memcontrol l_memcontrol = {0L, 0L, 0L, 0L, {0L, 0L, 0L, 0L, 0L}};
96Memcontrol l_memcontrol = {0L, 0L, 0L, 0L};
97 96
98 97
99static void *checkblock (void *block, size_t size) { 98static void *checkblock (void *block, size_t size) {
@@ -119,6 +118,11 @@ static void freeblock (Memcontrol *mc, void *block, size_t size) {
119 118
120void *debug_realloc (void *ud, void *block, size_t oldsize, size_t size) { 119void *debug_realloc (void *ud, void *block, size_t oldsize, size_t size) {
121 Memcontrol *mc = cast(Memcontrol *, ud); 120 Memcontrol *mc = cast(Memcontrol *, ud);
121 if (block == NULL) {
122 if (LUA_TSTRING <= oldsize && oldsize <= LUA_TTHREAD)
123 mc->objcount[oldsize - LUA_TSTRING]++;
124 oldsize = 0;
125 }
122 lua_assert((oldsize == 0) ? block == NULL : 126 lua_assert((oldsize == 0) ? block == NULL :
123 block && checkblocksize(block, oldsize)); 127 block && checkblocksize(block, oldsize));
124 if (mc->memlimit == 0) { /* first time? */ 128 if (mc->memlimit == 0) { /* first time? */
@@ -506,10 +510,12 @@ static int get_limits (lua_State *L) {
506 510
507static int mem_query (lua_State *L) { 511static int mem_query (lua_State *L) {
508 if (lua_isnone(L, 1)) { 512 if (lua_isnone(L, 1)) {
513 int i;
509 lua_pushinteger(L, l_memcontrol.total); 514 lua_pushinteger(L, l_memcontrol.total);
510 lua_pushinteger(L, l_memcontrol.numblocks); 515 lua_pushinteger(L, l_memcontrol.numblocks);
511 lua_pushinteger(L, l_memcontrol.maxmem); 516 lua_pushinteger(L, l_memcontrol.maxmem);
512 return 3; 517 for (i = 0; i < 5; i++) lua_pushinteger(L, l_memcontrol.objcount[i]);
518 return 3 + 5;
513 } 519 }
514 else { 520 else {
515 l_memcontrol.memlimit = luaL_checkint(L, 1); 521 l_memcontrol.memlimit = luaL_checkint(L, 1);