aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-06-05 17:07:53 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-06-05 17:07:53 -0300
commitc542aac0b935a65203244c130cdfa700113f0bc8 (patch)
tree83719377490646a17c31ceb3cabb3111b713151a
parentdbfb810267d8fd7ecf546ea1e1edc8892547e664 (diff)
downloadlua-c542aac0b935a65203244c130cdfa700113f0bc8.tar.gz
lua-c542aac0b935a65203244c130cdfa700113f0bc8.tar.bz2
lua-c542aac0b935a65203244c130cdfa700113f0bc8.zip
collect dead indices in tables
-rw-r--r--lgc.c4
-rw-r--r--ltable.c24
-rw-r--r--ltable.h3
3 files changed, 28 insertions, 3 deletions
diff --git a/lgc.c b/lgc.c
index b8cbc54f..f1296cde 100644
--- a/lgc.c
+++ b/lgc.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lgc.c,v 1.53 2000/05/30 19:00:31 roberto Exp roberto $ 2** $Id: lgc.c,v 1.54 2000/06/05 14:56:18 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*/
@@ -64,6 +64,8 @@ static void tablemark (lua_State *L, Hash *h) {
64 for (i=h->size-1; i>=0; i--) { 64 for (i=h->size-1; i>=0; i--) {
65 Node *n = node(h,i); 65 Node *n = node(h,i);
66 if (ttype(key(n)) != TAG_NIL) { 66 if (ttype(key(n)) != TAG_NIL) {
67 if (ttype(val(n)) == TAG_NIL)
68 luaH_remove(h, key(n)); /* dead element; try to remove it */
67 markobject(L, &n->key); 69 markobject(L, &n->key);
68 markobject(L, &n->val); 70 markobject(L, &n->val);
69 } 71 }
diff --git a/ltable.c b/ltable.c
index a7d4f36a..9929146d 100644
--- a/ltable.c
+++ b/ltable.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltable.c,v 1.42 2000/05/11 18:57:19 roberto Exp roberto $ 2** $Id: ltable.c,v 1.43 2000/05/24 13:54:49 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*/
@@ -121,6 +121,28 @@ int luaH_pos (lua_State *L, const Hash *t, const TObject *key) {
121 (int)(((const char *)v - (const char *)(&t->node[0].val))/sizeof(Node)); 121 (int)(((const char *)v - (const char *)(&t->node[0].val))/sizeof(Node));
122} 122}
123 123
124/*
125** try to remove a key without value from a table. To avoid problems with
126** hash, change `key' for a number with the same hash.
127*/
128void luaH_remove (Hash *t, TObject *key) {
129 /* do not remove numbers */
130 if (ttype(key) != TAG_NUMBER) {
131 /* try to find a number `n' with the same hash as `key' */
132 Node *mp = luaH_mainposition(t, key);
133 int n = mp - &t->node[0];
134 /* make sure `n' is not in `t' */
135 while (luaH_getnum(t, n) != &luaO_nilobject) {
136 if (t->size >= MAX_INT-n)
137 return; /* give up; (to avoid overflow) */
138 n += t->size;
139 }
140 ttype(key) = TAG_NUMBER;
141 nvalue(key) = n;
142 LUA_ASSERT(L, luaH_mainposition(t, key) == mp, "cannot change hash");
143 }
144}
145
124 146
125static void setnodevector (lua_State *L, Hash *t, lint32 size) { 147static void setnodevector (lua_State *L, Hash *t, lint32 size) {
126 int i; 148 int i;
diff --git a/ltable.h b/ltable.h
index 3d1f674a..df36e8a6 100644
--- a/ltable.h
+++ b/ltable.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltable.h,v 1.19 2000/04/25 16:55:09 roberto Exp roberto $ 2** $Id: ltable.h,v 1.20 2000/05/08 19:32:53 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*/
@@ -21,6 +21,7 @@ void luaH_free (lua_State *L, Hash *t);
21const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key); 21const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key);
22const TObject *luaH_getnum (const Hash *t, Number key); 22const TObject *luaH_getnum (const Hash *t, Number key);
23const TObject *luaH_getstr (const Hash *t, TString *key); 23const TObject *luaH_getstr (const Hash *t, TString *key);
24void luaH_remove (Hash *t, TObject *key);
24void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val); 25void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val);
25int luaH_pos (lua_State *L, const Hash *t, const TObject *r); 26int luaH_pos (lua_State *L, const Hash *t, const TObject *r);
26void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val); 27void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val);