aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-08-30 16:09:21 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-08-30 16:09:21 -0300
commitfdafd4f4a88d1584dea900517445a17d87f8b82f (patch)
tree52e38e886d569e77c1df82ebe2bcfbaafd4c6f75
parentbeeff4ccafe5877d00119cb3d93f1f937d46dcfb (diff)
downloadlua-fdafd4f4a88d1584dea900517445a17d87f8b82f.tar.gz
lua-fdafd4f4a88d1584dea900517445a17d87f8b82f.tar.bz2
lua-fdafd4f4a88d1584dea900517445a17d87f8b82f.zip
new structure for collectable objects, sharing a common header
-rw-r--r--lapi.c8
-rw-r--r--ldo.c8
-rw-r--r--lfunc.c45
-rw-r--r--lgc.c279
-rw-r--r--lgc.h3
-rw-r--r--lobject.h125
-rw-r--r--lparser.c5
-rw-r--r--lstate.c14
-rw-r--r--lstate.h15
-rw-r--r--lstring.c31
-rw-r--r--ltable.c7
-rw-r--r--ltests.c8
-rw-r--r--ltm.c6
-rw-r--r--lua.h18
14 files changed, 273 insertions, 299 deletions
diff --git a/lapi.c b/lapi.c
index d87bf134..012db0aa 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.210 2002/08/07 14:24:24 roberto Exp roberto $ 2** $Id: lapi.c,v 1.211 2002/08/08 20:08:41 roberto Exp roberto $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -211,6 +211,12 @@ LUA_API int lua_isstring (lua_State *L, int index) {
211} 211}
212 212
213 213
214LUA_API int lua_isuserdata (lua_State *L, int index) {
215 const TObject *o = luaA_indexAcceptable(L, index);
216 return (o != NULL && (ttisuserdata(o) || ttislightuserdata(o)));
217}
218
219
214LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { 220LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
215 StkId o1 = luaA_indexAcceptable(L, index1); 221 StkId o1 = luaA_indexAcceptable(L, index1);
216 StkId o2 = luaA_indexAcceptable(L, index2); 222 StkId o2 = luaA_indexAcceptable(L, index2);
diff --git a/ldo.c b/ldo.c
index cb7a18c7..205203dc 100644
--- a/ldo.c
+++ b/ldo.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.c,v 1.191 2002/08/07 19:22:39 roberto Exp roberto $ 2** $Id: ldo.c,v 1.192 2002/08/07 20:55:00 roberto Exp roberto $
3** Stack and Call structure of Lua 3** Stack and Call structure of Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -101,10 +101,10 @@ static void restore_stack_limit (lua_State *L) {
101 101
102static void correctstack (lua_State *L, TObject *oldstack) { 102static void correctstack (lua_State *L, TObject *oldstack) {
103 CallInfo *ci; 103 CallInfo *ci;
104 UpVal *up; 104 GCObject *up;
105 L->top = (L->top - oldstack) + L->stack; 105 L->top = (L->top - oldstack) + L->stack;
106 for (up = L->openupval; up != NULL; up = up->next) 106 for (up = L->openupval; up != NULL; up = up->gch.next)
107 up->v = (up->v - oldstack) + L->stack; 107 (&up->uv)->v = ((&up->uv)->v - oldstack) + L->stack;
108 for (ci = L->base_ci; ci <= L->ci; ci++) { 108 for (ci = L->base_ci; ci <= L->ci; ci++) {
109 ci->base = (ci->base - oldstack) + L->stack; 109 ci->base = (ci->base - oldstack) + L->stack;
110 ci->top = (ci->top - oldstack) + L->stack; 110 ci->top = (ci->top - oldstack) + L->stack;
diff --git a/lfunc.c b/lfunc.c
index 71cff734..ebdfa086 100644
--- a/lfunc.c
+++ b/lfunc.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lfunc.c,v 1.57 2002/06/20 20:41:46 roberto Exp roberto $ 2** $Id: lfunc.c,v 1.58 2002/08/16 14:45:55 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*/
@@ -10,6 +10,7 @@
10#include "lua.h" 10#include "lua.h"
11 11
12#include "lfunc.h" 12#include "lfunc.h"
13#include "lgc.h"
13#include "lmem.h" 14#include "lmem.h"
14#include "lobject.h" 15#include "lobject.h"
15#include "lstate.h" 16#include "lstate.h"
@@ -25,10 +26,8 @@
25 26
26Closure *luaF_newCclosure (lua_State *L, int nelems) { 27Closure *luaF_newCclosure (lua_State *L, int nelems) {
27 Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems))); 28 Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems)));
29 luaC_link(L, cast(GCObject *, c), LUA_TFUNCTION);
28 c->c.isC = 1; 30 c->c.isC = 1;
29 c->c.next = G(L)->rootcl;
30 G(L)->rootcl = c;
31 c->c.marked = 0;
32 c->c.nupvalues = cast(lu_byte, nelems); 31 c->c.nupvalues = cast(lu_byte, nelems);
33 return c; 32 return c;
34} 33}
@@ -36,10 +35,8 @@ Closure *luaF_newCclosure (lua_State *L, int nelems) {
36 35
37Closure *luaF_newLclosure (lua_State *L, int nelems, TObject *gt) { 36Closure *luaF_newLclosure (lua_State *L, int nelems, TObject *gt) {
38 Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems))); 37 Closure *c = cast(Closure *, luaM_malloc(L, sizeLclosure(nelems)));
38 luaC_link(L, cast(GCObject *, c), LUA_TFUNCTION);
39 c->l.isC = 0; 39 c->l.isC = 0;
40 c->c.next = G(L)->rootcl;
41 G(L)->rootcl = c;
42 c->l.marked = 0;
43 c->l.g = *gt; 40 c->l.g = *gt;
44 c->l.nupvalues = cast(lu_byte, nelems); 41 c->l.nupvalues = cast(lu_byte, nelems);
45 return c; 42 return c;
@@ -47,37 +44,36 @@ Closure *luaF_newLclosure (lua_State *L, int nelems, TObject *gt) {
47 44
48 45
49UpVal *luaF_findupval (lua_State *L, StkId level) { 46UpVal *luaF_findupval (lua_State *L, StkId level) {
50 UpVal **pp = &L->openupval; 47 GCObject **pp = &L->openupval;
51 UpVal *p; 48 GCObject *p;
52 while ((p = *pp) != NULL && p->v >= level) { 49 UpVal *v;
53 if (p->v == level) return p; 50 while ((p = *pp) != NULL && (&p->uv)->v >= level) {
54 pp = &p->next; 51 if ((&p->uv)->v == level) return &p->uv;
52 pp = &p->gch.next;
55 } 53 }
56 p = luaM_new(L, UpVal); /* not found: create a new one */ 54 v = luaM_new(L, UpVal); /* not found: create a new one */
57 p->marked = 1; /* open upvalues should not be collected */ 55 v->marked = 1; /* open upvalues should not be collected */
58 p->v = level; /* current value lives in the stack */ 56 v->v = level; /* current value lives in the stack */
59 p->next = *pp; /* chain it in the proper position */ 57 v->next = *pp; /* chain it in the proper position */
60 *pp = p; 58 *pp = cast(GCObject *, v);
61 return p; 59 return v;
62} 60}
63 61
64 62
65void luaF_close (lua_State *L, StkId level) { 63void luaF_close (lua_State *L, StkId level) {
66 UpVal *p; 64 UpVal *p;
67 while ((p = L->openupval) != NULL && p->v >= level) { 65 while ((p = &(L->openupval)->uv) != NULL && p->v >= level) {
68 lua_assert(p->marked);
69 p->marked = 0;
70 setobj(&p->value, p->v); /* save current value */ 66 setobj(&p->value, p->v); /* save current value */
71 p->v = &p->value; /* now current value lives here */ 67 p->v = &p->value; /* now current value lives here */
72 L->openupval = p->next; /* remove from `open' list */ 68 L->openupval = p->next; /* remove from `open' list */
73 p->next = G(L)->rootupval; /* chain in `closed' list */ 69 luaC_link(L, cast(GCObject *, p), LUA_TUPVAL);
74 G(L)->rootupval = p;
75 } 70 }
76} 71}
77 72
78 73
79Proto *luaF_newproto (lua_State *L) { 74Proto *luaF_newproto (lua_State *L) {
80 Proto *f = luaM_new(L, Proto); 75 Proto *f = luaM_new(L, Proto);
76 luaC_link(L, cast(GCObject *, f), LUA_TPROTO);
81 f->k = NULL; 77 f->k = NULL;
82 f->sizek = 0; 78 f->sizek = 0;
83 f->p = NULL; 79 f->p = NULL;
@@ -88,14 +84,11 @@ Proto *luaF_newproto (lua_State *L) {
88 f->numparams = 0; 84 f->numparams = 0;
89 f->is_vararg = 0; 85 f->is_vararg = 0;
90 f->maxstacksize = 0; 86 f->maxstacksize = 0;
91 f->marked = 0;
92 f->lineinfo = NULL; 87 f->lineinfo = NULL;
93 f->sizelocvars = 0; 88 f->sizelocvars = 0;
94 f->locvars = NULL; 89 f->locvars = NULL;
95 f->lineDefined = 0; 90 f->lineDefined = 0;
96 f->source = NULL; 91 f->source = NULL;
97 f->next = G(L)->rootproto; /* chain in list of protos */
98 G(L)->rootproto = f;
99 return f; 92 return f;
100} 93}
101 94
diff --git a/lgc.c b/lgc.c
index 9b37a972..4eee1486 100644
--- a/lgc.c
+++ b/lgc.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lgc.c,v 1.146 2002/08/16 14:45:55 roberto Exp roberto $ 2** $Id: lgc.c,v 1.147 2002/08/16 20:00:28 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*/
@@ -34,28 +34,24 @@ typedef struct GCState {
34#define resetbit(x,b) ((x) &= cast(lu_byte, ~(1<<(b)))) 34#define resetbit(x,b) ((x) &= cast(lu_byte, ~(1<<(b))))
35#define testbit(x,b) ((x) & (1<<(b))) 35#define testbit(x,b) ((x) & (1<<(b)))
36 36
37#define mark(x) setbit((x)->gch.marked, 0)
38#define unmark(x) resetbit((x)->gch.marked, 0)
39#define ismarked(x) ((x)->gch.marked & ((1<<4)|1))
37 40
38#define strmark(s) setbit((s)->tsv.marked, 0) 41#define strmark(s) setbit((s)->tsv.marked, 0)
39#define strunmark(s) resetbit((s)->tsv.marked, 0) 42#define strunmark(s) resetbit((s)->tsv.marked, 0)
40 43
41 44
45#define isfinalized(u) (!testbit((u)->uv.marked, 1))
46#define markfinalized(u) resetbit((u)->uv.marked, 1)
42 47
43/* mark tricks for userdata */
44#define isudmarked(u) testbit(u->uv.marked, 0)
45#define markud(u) setbit(u->uv.marked, 0)
46#define unmarkud(u) resetbit(u->uv.marked, 0)
47 48
48#define isfinalized(u) testbit(u->uv.marked, 1) 49static void reallymarkobject (GCState *st, GCObject *o);
49#define markfinalized(u) setbit(u->uv.marked, 1)
50 50
51#define markobject(st,o) \
52 if (iscollectable(o)) reallymarkobject(st,(o)->value.gc)
51 53
52#define ismarkable(o) (!((1 << ttype(o)) & \ 54#define marktable(st,t) reallymarkobject(st, cast(GCObject *, (t)))
53 ((1 << LUA_TNIL) | (1 << LUA_TNUMBER) | \
54 (1 << LUA_TBOOLEAN) | (1 << LUA_TLIGHTUSERDATA))))
55
56static void reallymarkobject (GCState *st, TObject *o);
57
58#define markobject(st,o) if (ismarkable(o)) reallymarkobject(st,o)
59 55
60 56
61static void markproto (Proto *f) { 57static void markproto (Proto *f) {
@@ -76,63 +72,46 @@ static void markproto (Proto *f) {
76} 72}
77 73
78 74
79static void marktable (GCState *st, Table *h) {
80 if (!h->marked) {
81 h->marked = 1;
82 h->gclist = st->tmark; /* chain it for later traversal */
83 st->tmark = h;
84 }
85}
86
87 75
88static void markclosure (GCState *st, Closure *cl) { 76static void markclosure (GCState *st, Closure *cl) {
89 if (!cl->c.marked) { 77 if (cl->c.isC) {
90 cl->c.marked = 1; 78 int i;
91 if (cl->c.isC) { 79 for (i=0; i<cl->c.nupvalues; i++) /* mark its upvalues */
92 int i; 80 markobject(st, &cl->c.upvalue[i]);
93 for (i=0; i<cl->c.nupvalues; i++) /* mark its upvalues */ 81 }
94 markobject(st, &cl->c.upvalue[i]); 82 else {
95 } 83 int i;
96 else { 84 lua_assert(cl->l.nupvalues == cl->l.p->nupvalues);
97 int i; 85 marktable(st, hvalue(&cl->l.g));
98 lua_assert(cl->l.nupvalues == cl->l.p->nupvalues); 86 markproto(cl->l.p);
99 marktable(st, hvalue(&cl->l.g)); 87 for (i=0; i<cl->l.nupvalues; i++) { /* mark its upvalues */
100 markproto(cl->l.p); 88 UpVal *u = cl->l.upvals[i];
101 for (i=0; i<cl->l.nupvalues; i++) { /* mark its upvalues */ 89 if (!u->marked) {
102 UpVal *u = cl->l.upvals[i]; 90 markobject(st, &u->value);
103 if (!u->marked) { 91 u->marked = 1;
104 markobject(st, &u->value);
105 u->marked = 1;
106 }
107 } 92 }
108 } 93 }
109 } 94 }
110} 95}
111 96
112 97
113static void markudata (GCState *st, Udata *u) { 98static void reallymarkobject (GCState *st, GCObject *o) {
114 markud(u); 99 if (ismarked(o)) return;
115 marktable(st, u->uv.metatable); 100 mark(o);
116} 101 switch (o->gch.tt) {
117 102 case LUA_TFUNCTION: {
118 103 markclosure(st, &o->cl);
119static void reallymarkobject (GCState *st, TObject *o) {
120 switch (ttype(o)) {
121 case LUA_TSTRING:
122 strmark(tsvalue(o));
123 break;
124 case LUA_TUSERDATA:
125 if (!isudmarked(uvalue(o)))
126 markudata(st, uvalue(o));
127 break; 104 break;
128 case LUA_TFUNCTION: 105 }
129 markclosure(st, clvalue(o)); 106 case LUA_TUSERDATA: {
107 marktable(st, (&o->u)->uv.metatable);
130 break; 108 break;
109 }
131 case LUA_TTABLE: { 110 case LUA_TTABLE: {
132 marktable(st, hvalue(o)); 111 (&o->h)->gclist = st->tmark; /* chain it for later traversal */
112 st->tmark = &o->h;
133 break; 113 break;
134 } 114 }
135 default: lua_assert(0); /* should not be called with other types */
136 } 115 }
137} 116}
138 117
@@ -177,22 +156,29 @@ static void traversestacks (GCState *st) {
177 156
178 157
179static void marktmu (GCState *st) { 158static void marktmu (GCState *st) {
180 Udata *u; 159 GCObject *u;
181 for (u = G(st->L)->tmudata; u; u = u->uv.next) 160 for (u = G(st->L)->tmudata; u; u = u->uv.next) {
182 markudata(st, u); 161 mark(u);
162 marktable(st, (&u->u)->uv.metatable);
163 }
183} 164}
184 165
185 166
186/* move `dead' udata that need finalization to list `tmudata' */ 167/* move `dead' udata that need finalization to list `tmudata' */
187static void separateudata (lua_State *L) { 168static void separateudata (lua_State *L) {
188 Udata **p = &G(L)->rootudata; 169 GCObject **p = &G(L)->rootudata;
189 Udata *curr; 170 GCObject *curr;
190 Udata *collected = NULL; /* to collect udata with gc event */ 171 GCObject *collected = NULL; /* to collect udata with gc event */
191 Udata **lastcollected = &collected; 172 GCObject **lastcollected = &collected;
192 while ((curr = *p) != NULL) { 173 while ((curr = *p) != NULL) {
193 if (isudmarked(curr) || isfinalized(curr) || 174 lua_assert(curr->gch.tt == LUA_TUSERDATA);
194 (fasttm(L, curr->uv.metatable, TM_GC) == NULL)) 175 if (ismarked(curr) || isfinalized(&curr->u))
176 p = &curr->uv.next; /* don't bother with them */
177
178 else if (fasttm(L, (&curr->u)->uv.metatable, TM_GC) == NULL) {
179 markfinalized(&curr->u); /* don't need finalization */
195 p = &curr->uv.next; 180 p = &curr->uv.next;
181 }
196 else { /* must call its gc method */ 182 else { /* must call its gc method */
197 *p = curr->uv.next; 183 *p = curr->uv.next;
198 curr->uv.next = NULL; /* link `curr' at the end of `collected' list */ 184 curr->uv.next = NULL; /* link `curr' at the end of `collected' list */
@@ -208,7 +194,7 @@ static void separateudata (lua_State *L) {
208 194
209static void removekey (Node *n) { 195static void removekey (Node *n) {
210 setnilvalue(val(n)); /* remove corresponding value ... */ 196 setnilvalue(val(n)); /* remove corresponding value ... */
211 if (ismarkable(key(n))) 197 if (iscollectable(key(n)))
212 setttype(key(n), LUA_TNONE); /* dead key; remove it */ 198 setttype(key(n), LUA_TNONE); /* dead key; remove it */
213} 199}
214 200
@@ -251,20 +237,10 @@ static void propagatemarks (GCState *st) {
251} 237}
252 238
253 239
254static int ismarked (const TObject *o) { 240static int valismarked (const TObject *o) {
255 switch (ttype(o)) { 241 if (ttisstring(o))
256 case LUA_TUSERDATA: 242 strmark(tsvalue(o)); /* strings are `values', so are never weak */
257 return isudmarked(uvalue(o)); 243 return !iscollectable(o) || testbit(o->value.gc->gch.marked, 0);
258 case LUA_TTABLE:
259 return hvalue(o)->marked;
260 case LUA_TFUNCTION:
261 return clvalue(o)->c.marked;
262 case LUA_TSTRING:
263 strmark(tsvalue(o)); /* strings are `values', so are never weak */
264 /* go through */
265 default: /* number, nil, boolean, udataval */
266 return 1;
267 }
268} 244}
269 245
270 246
@@ -279,7 +255,7 @@ static void cleartablekeys (GCState *st) {
279 int i = sizenode(h); 255 int i = sizenode(h);
280 while (i--) { 256 while (i--) {
281 Node *n = node(h, i); 257 Node *n = node(h, i);
282 if (!ismarked(key(n))) /* key was collected? */ 258 if (!valismarked(key(n))) /* key was collected? */
283 removekey(n); /* remove entry from table */ 259 removekey(n); /* remove entry from table */
284 } 260 }
285 } 261 }
@@ -297,13 +273,13 @@ static void cleartablevalues (GCState *st) {
297 int i = sizearray(h); 273 int i = sizearray(h);
298 while (i--) { 274 while (i--) {
299 TObject *o = &h->array[i]; 275 TObject *o = &h->array[i];
300 if (!ismarked(o)) /* value was collected? */ 276 if (!valismarked(o)) /* value was collected? */
301 setnilvalue(o); /* remove value */ 277 setnilvalue(o); /* remove value */
302 } 278 }
303 i = sizenode(h); 279 i = sizenode(h);
304 while (i--) { 280 while (i--) {
305 Node *n = node(h, i); 281 Node *n = node(h, i);
306 if (!ismarked(val(n))) /* value was collected? */ 282 if (!valismarked(val(n))) /* value was collected? */
307 removekey(n); /* remove entry from table */ 283 removekey(n); /* remove entry from table */
308 } 284 }
309 } 285 }
@@ -311,103 +287,47 @@ static void cleartablevalues (GCState *st) {
311} 287}
312 288
313 289
314static void sweepproto (lua_State *L) { 290static void freeobj (lua_State *L, GCObject *o) {
315 Proto **p = &G(L)->rootproto; 291 switch (o->gch.tt) {
316 Proto *curr; 292 case LUA_TPROTO: luaF_freeproto(L, &o->p); break;
317 while ((curr = *p) != NULL) { 293 case LUA_TFUNCTION: luaF_freeclosure(L, &o->cl); break;
318 if (curr->marked) { 294 case LUA_TUPVAL: luaM_freelem(L, &o->uv); break;
319 curr->marked = 0; 295 case LUA_TTABLE: luaH_free(L, &o->h); break;
320 p = &curr->next; 296 case LUA_TSTRING: {
321 } 297 luaM_free(L, o, sizestring((&o->ts)->tsv.len));
322 else { 298 break;
323 *p = curr->next;
324 luaF_freeproto(L, curr);
325 }
326 }
327}
328
329
330static void sweepclosures (lua_State *L) {
331 Closure **p = &G(L)->rootcl;
332 Closure *curr;
333 while ((curr = *p) != NULL) {
334 if (curr->c.marked) {
335 curr->c.marked = 0;
336 p = &curr->c.next;
337 }
338 else {
339 *p = curr->c.next;
340 luaF_freeclosure(L, curr);
341 }
342 }
343}
344
345
346static void sweepupval (lua_State *L) {
347 UpVal **v = &G(L)->rootupval;
348 UpVal *curr;
349 while ((curr = *v) != NULL) {
350 if (curr->marked) {
351 curr->marked = 0; /* unmark */
352 v = &curr->next; /* next */
353 }
354 else {
355 *v = curr->next; /* next */
356 luaM_freelem(L, curr);
357 }
358 }
359}
360
361
362static void sweeptable (lua_State *L) {
363 Table **p = &G(L)->roottable;
364 Table *curr;
365 while ((curr = *p) != NULL) {
366 if (curr->marked) {
367 curr->marked = 0;
368 p = &curr->next;
369 } 299 }
370 else { 300 case LUA_TUSERDATA: {
371 *p = curr->next; 301 luaM_free(L, o, sizeudata((&o->u)->uv.len));
372 luaH_free(L, curr); 302 break;
373 } 303 }
304 default: lua_assert(0);
374 } 305 }
375} 306}
376 307
377 308
378 309static int sweeplist (lua_State *L, GCObject **p, int limit) {
379static void sweepudata (lua_State *L) { 310 GCObject *curr;
380 Udata **p = &G(L)->rootudata; 311 int count = 0; /* number of collected items */
381 Udata *curr;
382 while ((curr = *p) != NULL) { 312 while ((curr = *p) != NULL) {
383 if (isudmarked(curr)) { 313 if (curr->gch.marked > limit) {
384 unmarkud(curr); 314 unmark(curr);
385 p = &curr->uv.next; 315 p = &curr->gch.next;
386 } 316 }
387 else { 317 else {
388 *p = curr->uv.next; 318 count++;
389 luaM_free(L, curr, sizeudata(curr->uv.len)); 319 *p = curr->gch.next;
320 freeobj(L, curr);
390 } 321 }
391 } 322 }
323 return count;
392} 324}
393 325
394 326
395static void sweepstrings (lua_State *L, int all) { 327static void sweepstrings (lua_State *L, int all) {
396 int i; 328 int i;
397 for (i=0; i<G(L)->strt.size; i++) { /* for each list */ 329 for (i=0; i<G(L)->strt.size; i++) { /* for each list */
398 TString **p = &G(L)->strt.hash[i]; 330 G(L)->strt.nuse -= sweeplist(L, &G(L)->strt.hash[i], all);
399 TString *curr;
400 while ((curr = *p) != NULL) {
401 if (curr->tsv.marked && !all) { /* preserve? */
402 strunmark(curr);
403 p = &curr->tsv.nexthash;
404 }
405 else { /* collect */
406 *p = curr->tsv.nexthash;
407 G(L)->strt.nuse--;
408 luaM_free(L, curr, sizestring(curr->tsv.len));
409 }
410 }
411 } 331 }
412 if (G(L)->strt.nuse < cast(ls_nstr, G(L)->strt.size/4) && 332 if (G(L)->strt.nuse < cast(ls_nstr, G(L)->strt.size/4) &&
413 G(L)->strt.size > MINSTRTABSIZE*2) 333 G(L)->strt.size > MINSTRTABSIZE*2)
@@ -442,14 +362,14 @@ static void callGCTM (lua_State *L) {
442 setallowhook(L, 0); /* stop debug hooks during GC tag methods */ 362 setallowhook(L, 0); /* stop debug hooks during GC tag methods */
443 L->top++; /* reserve space to keep udata while runs its gc method */ 363 L->top++; /* reserve space to keep udata while runs its gc method */
444 while (G(L)->tmudata != NULL) { 364 while (G(L)->tmudata != NULL) {
445 Udata *udata = G(L)->tmudata; 365 GCObject *udata = G(L)->tmudata;
446 G(L)->tmudata = udata->uv.next; /* remove udata from `tmudata' */ 366 G(L)->tmudata = udata->uv.next; /* remove udata from `tmudata' */
447 udata->uv.next = G(L)->rootudata; /* return it to `root' list */ 367 udata->uv.next = G(L)->rootudata; /* return it to `root' list */
448 G(L)->rootudata = udata; 368 G(L)->rootudata = udata;
449 setuvalue(L->top - 1, udata); /* keep a reference to it */ 369 setuvalue(L->top - 1, &udata->u); /* keep a reference to it */
450 unmarkud(udata); 370 unmark(udata);
451 markfinalized(udata); 371 markfinalized(&udata->u);
452 do1gcTM(L, udata); 372 do1gcTM(L, &udata->u);
453 } 373 }
454 L->top--; 374 L->top--;
455 setallowhook(L, oldah); /* restore hooks */ 375 setallowhook(L, oldah); /* restore hooks */
@@ -463,12 +383,10 @@ void luaC_callallgcTM (lua_State *L) {
463 383
464 384
465void luaC_sweep (lua_State *L, int all) { 385void luaC_sweep (lua_State *L, int all) {
466 sweepudata(L); 386 if (all) all = 256; /* larger than any mark */
387 sweeplist(L, &G(L)->rootudata, all);
467 sweepstrings(L, all); 388 sweepstrings(L, all);
468 sweeptable(L); 389 sweeplist(L, &G(L)->rootgc, all);
469 sweepproto(L);
470 sweepupval(L);
471 sweepclosures(L);
472} 390}
473 391
474 392
@@ -482,7 +400,8 @@ void luaC_collectgarbage (lua_State *L) {
482 cleartablevalues(&st); 400 cleartablevalues(&st);
483 separateudata(L); /* separate userdata to be preserved */ 401 separateudata(L); /* separate userdata to be preserved */
484 marktmu(&st); /* mark `preserved' userdata */ 402 marktmu(&st); /* mark `preserved' userdata */
485 propagatemarks(&st); /* remark */ 403 propagatemarks(&st); /* remark, to propagate `preserveness' */
404 cleartablevalues(&st); /* again, for eventual weak preserved tables */
486 cleartablekeys(&st); 405 cleartablekeys(&st);
487 luaC_sweep(L, 0); 406 luaC_sweep(L, 0);
488 checkMbuffer(L); 407 checkMbuffer(L);
@@ -490,3 +409,11 @@ void luaC_collectgarbage (lua_State *L) {
490 callGCTM(L); 409 callGCTM(L);
491} 410}
492 411
412
413void luaC_link (lua_State *L, GCObject *o, int tt) {
414 o->gch.next = G(L)->rootgc;
415 G(L)->rootgc = o;
416 o->gch.marked = 0;
417 o->gch.tt = tt;
418}
419
diff --git a/lgc.h b/lgc.h
index 7a456d85..272e7584 100644
--- a/lgc.h
+++ b/lgc.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lgc.h,v 1.14 2001/12/10 22:11:23 roberto Exp roberto $ 2** $Id: lgc.h,v 1.15 2002/08/16 20:00:28 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*/
@@ -18,6 +18,7 @@
18void luaC_callallgcTM (lua_State *L); 18void luaC_callallgcTM (lua_State *L);
19void luaC_sweep (lua_State *L, int all); 19void luaC_sweep (lua_State *L, int all);
20void luaC_collectgarbage (lua_State *L); 20void luaC_collectgarbage (lua_State *L);
21void luaC_link (lua_State *L, GCObject *o, int tt);
21 22
22 23
23#endif 24#endif
diff --git a/lobject.h b/lobject.h
index 36a5b323..af47f57f 100644
--- a/lobject.h
+++ b/lobject.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.h,v 1.142 2002/08/06 17:06:56 roberto Exp roberto $ 2** $Id: lobject.h,v 1.143 2002/08/16 14:45:55 roberto Exp roberto $
3** Type definitions for Lua objects 3** Type definitions for Lua objects
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -13,20 +13,42 @@
13 13
14 14
15/* tags for values visible from Lua */ 15/* tags for values visible from Lua */
16#define NUM_TAGS LUA_TFUNCTION 16#define NUM_TAGS LUA_TUSERDATA
17 17
18 18
19/*
20** Extra tags for non-values
21*/
22#define LUA_TPROTO (NUM_TAGS+1)
23#define LUA_TUPVAL (NUM_TAGS+2)
24
25
26/*
27** Common header for all collectable objects
28*/
29typedef struct GCheader {
30 union GCObject *next; /* pointer to next object */
31 lu_byte tt; /* object type */
32 lu_byte marked; /* GC informations */
33} GCheader;
34
35
36
37
38/*
39** Union of all Lua values
40*/
19typedef union { 41typedef union {
42 union GCObject *gc;
20 void *p; 43 void *p;
21 union TString *ts;
22 union Udata *u;
23 union Closure *cl;
24 struct Table *h;
25 lua_Number n; 44 lua_Number n;
26 int b; 45 int b;
27} Value; 46} Value;
28 47
29 48
49/*
50** Lua values (or `tagged objects')
51*/
30typedef struct lua_TObject { 52typedef struct lua_TObject {
31 int tt; 53 int tt;
32 Value value; 54 Value value;
@@ -47,10 +69,10 @@ typedef struct lua_TObject {
47#define ttype(o) ((o)->tt) 69#define ttype(o) ((o)->tt)
48#define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p) 70#define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)
49#define nvalue(o) check_exp(ttisnumber(o), (o)->value.n) 71#define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)
50#define tsvalue(o) check_exp(ttisstring(o), (o)->value.ts) 72#define tsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
51#define uvalue(o) check_exp(ttisuserdata(o), (o)->value.u) 73#define uvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)
52#define clvalue(o) check_exp(ttisfunction(o), (o)->value.cl) 74#define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl)
53#define hvalue(o) check_exp(ttistable(o), (o)->value.h) 75#define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h)
54#define bvalue(o) check_exp(ttisboolean(o), (o)->value.b) 76#define bvalue(o) check_exp(ttisboolean(o), (o)->value.b)
55 77
56#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) 78#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
@@ -69,26 +91,43 @@ typedef struct lua_TObject {
69 { TObject *i_o=(obj); i_o->tt=LUA_TBOOLEAN; i_o->value.b=(x); } 91 { TObject *i_o=(obj); i_o->tt=LUA_TBOOLEAN; i_o->value.b=(x); }
70 92
71#define setsvalue(obj,x) \ 93#define setsvalue(obj,x) \
72 { TObject *i_o=(obj); i_o->tt=LUA_TSTRING; i_o->value.ts=(x); } 94 { TObject *i_o=(obj); i_o->tt=LUA_TSTRING; \
95 i_o->value.gc=cast(GCObject *, (x)); \
96 lua_assert(i_o->value.gc->gch.tt == LUA_TSTRING); }
73 97
74#define setuvalue(obj,x) \ 98#define setuvalue(obj,x) \
75 { TObject *i_o=(obj); i_o->tt=LUA_TUSERDATA; i_o->value.u=(x); } 99 { TObject *i_o=(obj); i_o->tt=LUA_TUSERDATA; \
100 i_o->value.gc=cast(GCObject *, (x)); \
101 lua_assert(i_o->value.gc->gch.tt == LUA_TUSERDATA); }
76 102
77#define setclvalue(obj,x) \ 103#define setclvalue(obj,x) \
78 { TObject *i_o=(obj); i_o->tt=LUA_TFUNCTION; i_o->value.cl=(x); } 104 { TObject *i_o=(obj); i_o->tt=LUA_TFUNCTION; \
105 i_o->value.gc=cast(GCObject *, (x)); \
106 lua_assert(i_o->value.gc->gch.tt == LUA_TFUNCTION); }
79 107
80#define sethvalue(obj,x) \ 108#define sethvalue(obj,x) \
81 { TObject *i_o=(obj); i_o->tt=LUA_TTABLE; i_o->value.h=(x); } 109 { TObject *i_o=(obj); i_o->tt=LUA_TTABLE; \
110 i_o->value.gc=cast(GCObject *, (x)); \
111 lua_assert(i_o->value.gc->gch.tt == LUA_TTABLE); }
82 112
83#define setnilvalue(obj) ((obj)->tt=LUA_TNIL) 113#define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
84 114
115
116#define checkconsistency(obj) \
117 lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))
118
119
85#define setobj(obj1,obj2) \ 120#define setobj(obj1,obj2) \
86 { const TObject *o2=(obj2); TObject *o1=(obj1); \ 121 { const TObject *o2=(obj2); TObject *o1=(obj1); \
122 checkconsistency(o2); \
87 o1->tt=o2->tt; o1->value = o2->value; } 123 o1->tt=o2->tt; o1->value = o2->value; }
88 124
89#define setttype(obj, tt) (ttype(obj) = (tt)) 125#define setttype(obj, tt) (ttype(obj) = (tt))
90 126
91 127
128#define iscollectable(o) (ttype(o) >= LUA_TSTRING)
129
130
92 131
93typedef TObject *StkId; /* index to stack elements */ 132typedef TObject *StkId; /* index to stack elements */
94 133
@@ -99,11 +138,12 @@ typedef TObject *StkId; /* index to stack elements */
99typedef union TString { 138typedef union TString {
100 union L_Umaxalign dummy; /* ensures maximum alignment for strings */ 139 union L_Umaxalign dummy; /* ensures maximum alignment for strings */
101 struct { 140 struct {
141 union GCObject *next; /* pointer to next object */
142 lu_byte tt; /* object type */
143 lu_byte marked; /* GC informations */
144 lu_byte reserved;
102 lu_hash hash; 145 lu_hash hash;
103 size_t len; 146 size_t len;
104 union TString *nexthash; /* chain for hash table */
105 lu_byte marked;
106 lu_byte reserved;
107 } tsv; 147 } tsv;
108} TString; 148} TString;
109 149
@@ -116,10 +156,11 @@ typedef union TString {
116typedef union Udata { 156typedef union Udata {
117 union L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */ 157 union L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
118 struct { 158 struct {
159 union GCObject *next; /* pointer to next object */
160 lu_byte tt; /* object type */
161 lu_byte marked; /* GC informations */
119 struct Table *metatable; 162 struct Table *metatable;
120 union Udata *next; /* chain for list of all udata */
121 size_t len; 163 size_t len;
122 lu_byte marked;
123 } uv; 164 } uv;
124} Udata; 165} Udata;
125 166
@@ -130,10 +171,12 @@ typedef union Udata {
130** Function Prototypes 171** Function Prototypes
131*/ 172*/
132typedef struct Proto { 173typedef struct Proto {
174 union GCObject *next; /* pointer to next object */
175 lu_byte tt; /* object type */
176 lu_byte marked; /* GC informations */
133 TObject *k; /* constants used by the function */ 177 TObject *k; /* constants used by the function */
134 Instruction *code; 178 Instruction *code;
135 struct Proto **p; /* functions defined inside the function */ 179 struct Proto **p; /* functions defined inside the function */
136 struct Proto *next;
137 int *lineinfo; /* map from opcodes to source lines */ 180 int *lineinfo; /* map from opcodes to source lines */
138 struct LocVar *locvars; /* information about local variables */ 181 struct LocVar *locvars; /* information about local variables */
139 TString *source; 182 TString *source;
@@ -146,7 +189,6 @@ typedef struct Proto {
146 lu_byte numparams; 189 lu_byte numparams;
147 lu_byte is_vararg; 190 lu_byte is_vararg;
148 lu_byte maxstacksize; 191 lu_byte maxstacksize;
149 lu_byte marked;
150} Proto; 192} Proto;
151 193
152 194
@@ -163,10 +205,11 @@ typedef struct LocVar {
163*/ 205*/
164 206
165typedef struct UpVal { 207typedef struct UpVal {
208 union GCObject *next; /* pointer to next object */
209 lu_byte tt; /* object type */
210 lu_byte marked; /* GC informations */
166 TObject *v; /* points to stack or to its own value */ 211 TObject *v; /* points to stack or to its own value */
167 struct UpVal *next;
168 TObject value; /* the value (when closed) */ 212 TObject value; /* the value (when closed) */
169 lu_byte marked;
170} UpVal; 213} UpVal;
171 214
172 215
@@ -175,20 +218,22 @@ typedef struct UpVal {
175*/ 218*/
176 219
177typedef struct CClosure { 220typedef struct CClosure {
221 union GCObject *next; /* pointer to next object */
222 lu_byte tt; /* object type */
223 lu_byte marked; /* GC informations */
178 lu_byte isC; /* 0 for Lua functions, 1 for C functions */ 224 lu_byte isC; /* 0 for Lua functions, 1 for C functions */
179 lu_byte nupvalues; 225 lu_byte nupvalues;
180 lu_byte marked;
181 union Closure *next;
182 lua_CFunction f; 226 lua_CFunction f;
183 TObject upvalue[1]; 227 TObject upvalue[1];
184} CClosure; 228} CClosure;
185 229
186 230
187typedef struct LClosure { 231typedef struct LClosure {
232 union GCObject *next; /* pointer to next object */
233 lu_byte tt; /* object type */
234 lu_byte marked; /* GC informations */
188 lu_byte isC; 235 lu_byte isC;
189 lu_byte nupvalues; 236 lu_byte nupvalues; /* first five fields must be equal to CClosure!! */
190 lu_byte marked;
191 union Closure *next; /* first four fields must be equal to CClosure!! */
192 struct Proto *p; 237 struct Proto *p;
193 TObject g; /* global table for this closure */ 238 TObject g; /* global table for this closure */
194 UpVal *upvals[1]; 239 UpVal *upvals[1];
@@ -217,17 +262,18 @@ typedef struct Node {
217 262
218 263
219typedef struct Table { 264typedef struct Table {
265 union GCObject *next; /* pointer to next object */
266 lu_byte tt; /* object type */
267 lu_byte marked; /* GC informations */
268 lu_byte flags; /* 1<<p means tagmethod(p) is not present */
269 lu_byte mode;
270 lu_byte lsizenode; /* log2 of size of `node' array */
220 struct Table *metatable; 271 struct Table *metatable;
221 TObject *array; /* array part */ 272 TObject *array; /* array part */
222 Node *node; 273 Node *node;
223 Node *firstfree; /* this position is free; all positions after it are full */ 274 Node *firstfree; /* this position is free; all positions after it are full */
224 struct Table *next;
225 struct Table *gclist; 275 struct Table *gclist;
226 int sizearray; /* size of `array' array */ 276 int sizearray; /* size of `array' array */
227 lu_byte flags; /* 1<<p means tagmethod(p) is not present */
228 lu_byte lsizenode; /* log2 of size of `node' array */
229 lu_byte mode;
230 lu_byte marked;
231} Table; 277} Table;
232 278
233/* bit masks for `mode' */ 279/* bit masks for `mode' */
@@ -247,6 +293,19 @@ typedef struct Table {
247#define sizearray(t) ((t)->sizearray) 293#define sizearray(t) ((t)->sizearray)
248 294
249 295
296/*
297** Union of all collectable objects
298*/
299typedef union GCObject {
300 GCheader gch;
301 union TString ts;
302 union Udata u;
303 union Closure cl;
304 struct Table h;
305 struct Proto p;
306 struct UpVal uv;
307} GCObject;
308
250 309
251extern const TObject luaO_nilobject; 310extern const TObject luaO_nilobject;
252 311
diff --git a/lparser.c b/lparser.c
index 3331d673..e5dc2054 100644
--- a/lparser.c
+++ b/lparser.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lparser.c,v 1.192 2002/08/20 20:03:05 roberto Exp roberto $ 2** $Id: lparser.c,v 1.193 2002/08/22 19:51:08 roberto Exp roberto $
3** Lua Parser 3** Lua Parser
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -336,9 +336,6 @@ static void close_func (LexState *ls) {
336 Proto *f = fs->f; 336 Proto *f = fs->f;
337 removevars(ls, 0); 337 removevars(ls, 0);
338 luaK_codeABC(fs, OP_RETURN, 0, 1, 0); /* final return */ 338 luaK_codeABC(fs, OP_RETURN, 0, 1, 0); /* final return */
339 lua_assert(G(L)->roottable == fs->h);
340 G(L)->roottable = fs->h->next;
341 luaH_free(L, fs->h);
342 luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction); 339 luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction);
343 luaM_reallocvector(L, f->lineinfo, f->sizecode, fs->pc, int); 340 luaM_reallocvector(L, f->lineinfo, f->sizecode, fs->pc, int);
344 f->sizecode = fs->pc; 341 f->sizecode = fs->pc;
diff --git a/lstate.c b/lstate.c
index 41a0f997..a2334cca 100644
--- a/lstate.c
+++ b/lstate.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.c,v 1.103 2002/08/07 19:22:39 roberto Exp roberto $ 2** $Id: lstate.c,v 1.104 2002/08/16 20:00:28 roberto Exp roberto $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -64,10 +64,7 @@ static void f_luaopen (lua_State *L, void *ud) {
64 G(L)->Mbuffer = NULL; 64 G(L)->Mbuffer = NULL;
65 G(L)->Mbuffsize = 0; 65 G(L)->Mbuffsize = 0;
66 G(L)->panic = &default_panic; 66 G(L)->panic = &default_panic;
67 G(L)->rootproto = NULL; 67 G(L)->rootgc = NULL;
68 G(L)->rootcl = NULL;
69 G(L)->roottable = NULL;
70 G(L)->rootupval = NULL;
71 G(L)->rootudata = NULL; 68 G(L)->rootudata = NULL;
72 G(L)->tmudata = NULL; 69 G(L)->tmudata = NULL;
73 setnilvalue(key(G(L)->dummynode)); 70 setnilvalue(key(G(L)->dummynode));
@@ -76,7 +73,7 @@ static void f_luaopen (lua_State *L, void *ud) {
76 G(L)->nblocks = sizeof(lua_State) + sizeof(global_State); 73 G(L)->nblocks = sizeof(lua_State) + sizeof(global_State);
77 stack_init(L, L); /* init stack */ 74 stack_init(L, L); /* init stack */
78 /* create default meta table with a dummy table, and then close the loop */ 75 /* create default meta table with a dummy table, and then close the loop */
79 sethvalue(defaultmeta(L), NULL); 76 defaultmeta(L)->tt = LUA_TTABLE;
80 sethvalue(defaultmeta(L), luaH_new(L, 0, 4)); 77 sethvalue(defaultmeta(L), luaH_new(L, 0, 4));
81 hvalue(defaultmeta(L))->metatable = hvalue(defaultmeta(L)); 78 hvalue(defaultmeta(L))->metatable = hvalue(defaultmeta(L));
82 sethvalue(gt(L), luaH_new(L, 0, 4)); /* table of globals */ 79 sethvalue(gt(L), luaH_new(L, 0, 4)); /* table of globals */
@@ -160,11 +157,8 @@ static void close_state (lua_State *L) {
160 luaF_close(L, L->stack); /* close all upvalues for this thread */ 157 luaF_close(L, L->stack); /* close all upvalues for this thread */
161 if (G(L)) { /* close global state */ 158 if (G(L)) { /* close global state */
162 luaC_sweep(L, 1); /* collect all elements */ 159 luaC_sweep(L, 1); /* collect all elements */
163 lua_assert(G(L)->rootproto == NULL); 160 lua_assert(G(L)->rootgc == NULL);
164 lua_assert(G(L)->rootudata == NULL); 161 lua_assert(G(L)->rootudata == NULL);
165 lua_assert(G(L)->rootcl == NULL);
166 lua_assert(G(L)->rootupval == NULL);
167 lua_assert(G(L)->roottable == NULL);
168 luaS_freeall(L); 162 luaS_freeall(L);
169 luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, char); 163 luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, char);
170 luaM_freelem(NULL, L->l_G); 164 luaM_freelem(NULL, L->l_G);
diff --git a/lstate.h b/lstate.h
index 372f2b37..5f592a9e 100644
--- a/lstate.h
+++ b/lstate.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.h,v 1.93 2002/08/07 19:22:39 roberto Exp roberto $ 2** $Id: lstate.h,v 1.94 2002/08/12 17:23:12 roberto Exp roberto $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -75,7 +75,7 @@ struct lua_longjmp; /* defined in ldo.c */
75 75
76 76
77typedef struct stringtable { 77typedef struct stringtable {
78 TString **hash; 78 GCObject **hash;
79 ls_nstr nuse; /* number of elements */ 79 ls_nstr nuse; /* number of elements */
80 int size; 80 int size;
81} stringtable; 81} stringtable;
@@ -121,12 +121,9 @@ typedef struct CallInfo {
121*/ 121*/
122typedef struct global_State { 122typedef struct global_State {
123 stringtable strt; /* hash table for strings */ 123 stringtable strt; /* hash table for strings */
124 Proto *rootproto; /* list of all prototypes */ 124 GCObject *rootgc; /* list of (almost) all collectable objects */
125 Closure *rootcl; /* list of all closures */ 125 GCObject *rootudata; /* (separated) list of all userdata */
126 Table *roottable; /* list of all tables */ 126 GCObject *tmudata; /* list of userdata to be GC */
127 UpVal *rootupval; /* list of closed up values */
128 Udata *rootudata; /* list of all userdata */
129 Udata *tmudata; /* list of userdata to be GC */
130 void *Mbuffer; /* global buffer */ 127 void *Mbuffer; /* global buffer */
131 size_t Mbuffsize; /* size of Mbuffer */ 128 size_t Mbuffsize; /* size of Mbuffer */
132 lu_mem GCthreshold; 129 lu_mem GCthreshold;
@@ -154,7 +151,7 @@ struct lua_State {
154 unsigned long hookmask; 151 unsigned long hookmask;
155 ls_count hookcount; 152 ls_count hookcount;
156 lua_Hook hook; 153 lua_Hook hook;
157 UpVal *openupval; /* list of open upvalues in this stack */ 154 GCObject *openupval; /* list of open upvalues in this stack */
158 struct lua_longjmp *errorJmp; /* current error recover point */ 155 struct lua_longjmp *errorJmp; /* current error recover point */
159 ptrdiff_t errfunc; /* current error handling function (stack index) */ 156 ptrdiff_t errfunc; /* current error handling function (stack index) */
160 lua_State *next; /* circular double linked list of states */ 157 lua_State *next; /* circular double linked list of states */
diff --git a/lstring.c b/lstring.c
index ebc12388..c36839e3 100644
--- a/lstring.c
+++ b/lstring.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstring.c,v 1.74 2002/04/05 18:54:31 roberto Exp roberto $ 2** $Id: lstring.c,v 1.75 2002/08/16 14:45:55 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*/
@@ -23,19 +23,19 @@ void luaS_freeall (lua_State *L) {
23 23
24 24
25void luaS_resize (lua_State *L, int newsize) { 25void luaS_resize (lua_State *L, int newsize) {
26 TString **newhash = luaM_newvector(L, newsize, TString *); 26 GCObject **newhash = luaM_newvector(L, newsize, GCObject *);
27 stringtable *tb = &G(L)->strt; 27 stringtable *tb = &G(L)->strt;
28 int i; 28 int i;
29 for (i=0; i<newsize; i++) newhash[i] = NULL; 29 for (i=0; i<newsize; i++) newhash[i] = NULL;
30 /* rehash */ 30 /* rehash */
31 for (i=0; i<tb->size; i++) { 31 for (i=0; i<tb->size; i++) {
32 TString *p = tb->hash[i]; 32 GCObject *p = tb->hash[i];
33 while (p) { /* for each node in the list */ 33 while (p) { /* for each node in the list */
34 TString *next = p->tsv.nexthash; /* save next */ 34 GCObject *next = p->gch.next; /* save next */
35 lu_hash h = p->tsv.hash; 35 lu_hash h = (&p->ts)->tsv.hash;
36 int h1 = lmod(h, newsize); /* new position */ 36 int h1 = lmod(h, newsize); /* new position */
37 lua_assert(cast(int, h%newsize) == lmod(h, newsize)); 37 lua_assert(cast(int, h%newsize) == lmod(h, newsize));
38 p->tsv.nexthash = newhash[h1]; /* chain it in new position */ 38 p->gch.next = newhash[h1]; /* chain it */
39 newhash[h1] = p; 39 newhash[h1] = p;
40 p = next; 40 p = next;
41 } 41 }
@@ -49,17 +49,17 @@ void luaS_resize (lua_State *L, int newsize) {
49static TString *newlstr (lua_State *L, const char *str, size_t l, lu_hash h) { 49static TString *newlstr (lua_State *L, const char *str, size_t l, lu_hash h) {
50 TString *ts = cast(TString *, luaM_malloc(L, sizestring(l))); 50 TString *ts = cast(TString *, luaM_malloc(L, sizestring(l)));
51 stringtable *tb; 51 stringtable *tb;
52 ts->tsv.nexthash = NULL;
53 ts->tsv.len = l; 52 ts->tsv.len = l;
54 ts->tsv.hash = h; 53 ts->tsv.hash = h;
55 ts->tsv.marked = 0; 54 ts->tsv.marked = 0;
55 ts->tsv.tt = LUA_TSTRING;
56 ts->tsv.reserved = 0; 56 ts->tsv.reserved = 0;
57 memcpy(ts+1, str, l*sizeof(char)); 57 memcpy(ts+1, str, l*sizeof(char));
58 ((char *)(ts+1))[l] = '\0'; /* ending 0 */ 58 ((char *)(ts+1))[l] = '\0'; /* ending 0 */
59 tb = &G(L)->strt; 59 tb = &G(L)->strt;
60 h = lmod(h, tb->size); 60 h = lmod(h, tb->size);
61 ts->tsv.nexthash = tb->hash[h]; /* chain new entry */ 61 ts->tsv.next = tb->hash[h]; /* chain new entry */
62 tb->hash[h] = ts; 62 tb->hash[h] = cast(GCObject *, ts);
63 tb->nuse++; 63 tb->nuse++;
64 if (tb->nuse > cast(ls_nstr, tb->size) && tb->size <= MAX_INT/2) 64 if (tb->nuse > cast(ls_nstr, tb->size) && tb->size <= MAX_INT/2)
65 luaS_resize(L, tb->size*2); /* too crowded */ 65 luaS_resize(L, tb->size*2); /* too crowded */
@@ -68,7 +68,7 @@ static TString *newlstr (lua_State *L, const char *str, size_t l, lu_hash h) {
68 68
69 69
70TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { 70TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
71 TString *ts; 71 GCObject *ts;
72 lu_hash h = (lu_hash)l; /* seed */ 72 lu_hash h = (lu_hash)l; /* seed */
73 size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */ 73 size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */
74 size_t l1; 74 size_t l1;
@@ -76,9 +76,9 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
76 h = h ^ ((h<<5)+(h>>2)+(unsigned char)(str[l1-1])); 76 h = h ^ ((h<<5)+(h>>2)+(unsigned char)(str[l1-1]));
77 for (ts = G(L)->strt.hash[lmod(h, G(L)->strt.size)]; 77 for (ts = G(L)->strt.hash[lmod(h, G(L)->strt.size)];
78 ts != NULL; 78 ts != NULL;
79 ts = ts->tsv.nexthash) { 79 ts = ts->gch.next) {
80 if (ts->tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) 80 if ((&ts->ts)->tsv.len == l && (memcmp(str, getstr(&ts->ts), l) == 0))
81 return ts; 81 return &ts->ts;
82 } 82 }
83 return newlstr(L, str, l, h); /* not found */ 83 return newlstr(L, str, l, h); /* not found */
84} 84}
@@ -87,12 +87,13 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) {
87Udata *luaS_newudata (lua_State *L, size_t s) { 87Udata *luaS_newudata (lua_State *L, size_t s) {
88 Udata *u; 88 Udata *u;
89 u = cast(Udata *, luaM_malloc(L, sizeudata(s))); 89 u = cast(Udata *, luaM_malloc(L, sizeudata(s)));
90 u->uv.marked = 0; 90 u->uv.marked = (1<<1); /* is not finalized */
91 u->uv.tt = LUA_TUSERDATA;
91 u->uv.len = s; 92 u->uv.len = s;
92 u->uv.metatable = hvalue(defaultmeta(L)); 93 u->uv.metatable = hvalue(defaultmeta(L));
93 /* chain it on udata list */ 94 /* chain it on udata list */
94 u->uv.next = G(L)->rootudata; 95 u->uv.next = G(L)->rootudata;
95 G(L)->rootudata = u; 96 G(L)->rootudata = cast(GCObject *, u);
96 return u; 97 return u;
97} 98}
98 99
diff --git a/ltable.c b/ltable.c
index 64afbef9..2363d8ad 100644
--- a/ltable.c
+++ b/ltable.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltable.c,v 1.116 2002/08/06 17:06:56 roberto Exp roberto $ 2** $Id: ltable.c,v 1.117 2002/08/16 14:45:55 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*/
@@ -27,6 +27,7 @@
27 27
28#include "ldebug.h" 28#include "ldebug.h"
29#include "ldo.h" 29#include "ldo.h"
30#include "lgc.h"
30#include "lmem.h" 31#include "lmem.h"
31#include "lobject.h" 32#include "lobject.h"
32#include "lstate.h" 33#include "lstate.h"
@@ -304,11 +305,9 @@ static void rehash (lua_State *L, Table *t) {
304 305
305Table *luaH_new (lua_State *L, int narray, int lnhash) { 306Table *luaH_new (lua_State *L, int narray, int lnhash) {
306 Table *t = luaM_new(L, Table); 307 Table *t = luaM_new(L, Table);
308 luaC_link(L, cast(GCObject *, t), LUA_TTABLE);
307 t->metatable = hvalue(defaultmeta(L)); 309 t->metatable = hvalue(defaultmeta(L));
308 t->next = G(L)->roottable;
309 G(L)->roottable = t;
310 t->flags = cast(lu_byte, ~0); 310 t->flags = cast(lu_byte, ~0);
311 t->marked = 0;
312 t->mode = 0; 311 t->mode = 0;
313 /* temporary values (kept only if some malloc fails) */ 312 /* temporary values (kept only if some malloc fails) */
314 t->array = NULL; 313 t->array = NULL;
diff --git a/ltests.c b/ltests.c
index cb11b4e1..886996b4 100644
--- a/ltests.c
+++ b/ltests.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltests.c,v 1.132 2002/08/06 15:32:22 roberto Exp roberto $ 2** $Id: ltests.c,v 1.133 2002/08/06 18:01:50 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*/
@@ -324,10 +324,10 @@ static int string_query (lua_State *L) {
324 return 2; 324 return 2;
325 } 325 }
326 else if (s < tb->size) { 326 else if (s < tb->size) {
327 TString *ts; 327 GCObject *ts;
328 int n = 0; 328 int n = 0;
329 for (ts = tb->hash[s]; ts; ts = ts->tsv.nexthash) { 329 for (ts = tb->hash[s]; ts; ts = ts->gch.next) {
330 setsvalue(L->top, ts); 330 setsvalue(L->top, &ts->ts);
331 incr_top(L); 331 incr_top(L);
332 n++; 332 n++;
333 } 333 }
diff --git a/ltm.c b/ltm.c
index cd70f6ba..65e0a8da 100644
--- a/ltm.c
+++ b/ltm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltm.c,v 1.99 2002/08/05 14:09:06 roberto Exp roberto $ 2** $Id: ltm.c,v 1.100 2002/08/06 17:06:56 roberto Exp roberto $
3** Tag methods 3** Tag methods
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -18,8 +18,8 @@
18 18
19 19
20const char *const luaT_typenames[] = { 20const char *const luaT_typenames[] = {
21 "nil", "number", "string", "boolean", "table", 21 "nil", "boolean", "userdata", "number",
22 "function", "userdata", "userdata" 22 "string", "table", "function", "userdata"
23}; 23};
24 24
25 25
diff --git a/lua.h b/lua.h
index aa7fd893..28b7c504 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.153 2002/08/06 18:54:18 roberto Exp roberto $ 2** $Id: lua.h,v 1.154 2002/08/12 17:23:12 roberto Exp roberto $
3** Lua - An Extensible Extension Language 3** Lua - An Extensible Extension Language
4** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil 4** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil
5** http://www.lua.org mailto:info@lua.org 5** http://www.lua.org mailto:info@lua.org
@@ -63,13 +63,13 @@ typedef const char * (*lua_Chunkreader) (lua_State *L, void *ud, size_t *size);
63#define LUA_TNONE (-1) 63#define LUA_TNONE (-1)
64 64
65#define LUA_TNIL 0 65#define LUA_TNIL 0
66#define LUA_TNUMBER 1 66#define LUA_TBOOLEAN 1
67#define LUA_TSTRING 2 67#define LUA_TLIGHTUSERDATA 2
68#define LUA_TBOOLEAN 3 68#define LUA_TNUMBER 3
69#define LUA_TTABLE 4 69#define LUA_TSTRING 4
70#define LUA_TFUNCTION 5 70#define LUA_TTABLE 5
71#define LUA_TUSERDATA 6 71#define LUA_TFUNCTION 6
72#define LUA_TLIGHTUSERDATA 7 72#define LUA_TUSERDATA 7
73 73
74 74
75/* minimum Lua stack available to a C function */ 75/* minimum Lua stack available to a C function */
@@ -127,6 +127,7 @@ LUA_API int lua_checkstack (lua_State *L, int size);
127LUA_API int lua_isnumber (lua_State *L, int index); 127LUA_API int lua_isnumber (lua_State *L, int index);
128LUA_API int lua_isstring (lua_State *L, int index); 128LUA_API int lua_isstring (lua_State *L, int index);
129LUA_API int lua_iscfunction (lua_State *L, int index); 129LUA_API int lua_iscfunction (lua_State *L, int index);
130LUA_API int lua_isuserdata (lua_State *L, int index);
130LUA_API int lua_type (lua_State *L, int index); 131LUA_API int lua_type (lua_State *L, int index);
131LUA_API const char *lua_typename (lua_State *L, int type); 132LUA_API const char *lua_typename (lua_State *L, int type);
132 133
@@ -240,7 +241,6 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size);
240 241
241#define lua_isfunction(L,n) (lua_type(L,n) == LUA_TFUNCTION) 242#define lua_isfunction(L,n) (lua_type(L,n) == LUA_TFUNCTION)
242#define lua_istable(L,n) (lua_type(L,n) == LUA_TTABLE) 243#define lua_istable(L,n) (lua_type(L,n) == LUA_TTABLE)
243#define lua_isuserdata(L,n) (lua_type(L,n) >= LUA_TUSERDATA)
244#define lua_islightuserdata(L,n) (lua_type(L,n) == LUA_TLIGHTUSERDATA) 244#define lua_islightuserdata(L,n) (lua_type(L,n) == LUA_TLIGHTUSERDATA)
245#define lua_isnil(L,n) (lua_type(L,n) == LUA_TNIL) 245#define lua_isnil(L,n) (lua_type(L,n) == LUA_TNIL)
246#define lua_isboolean(L,n) (lua_type(L,n) == LUA_TBOOLEAN) 246#define lua_isboolean(L,n) (lua_type(L,n) == LUA_TBOOLEAN)