diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-06-05 17:07:53 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-06-05 17:07:53 -0300 |
commit | c542aac0b935a65203244c130cdfa700113f0bc8 (patch) | |
tree | 83719377490646a17c31ceb3cabb3111b713151a | |
parent | dbfb810267d8fd7ecf546ea1e1edc8892547e664 (diff) | |
download | lua-c542aac0b935a65203244c130cdfa700113f0bc8.tar.gz lua-c542aac0b935a65203244c130cdfa700113f0bc8.tar.bz2 lua-c542aac0b935a65203244c130cdfa700113f0bc8.zip |
collect dead indices in tables
-rw-r--r-- | lgc.c | 4 | ||||
-rw-r--r-- | ltable.c | 24 | ||||
-rw-r--r-- | ltable.h | 3 |
3 files changed, 28 insertions, 3 deletions
@@ -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 | } |
@@ -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 | */ | ||
128 | void 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 | ||
125 | static void setnodevector (lua_State *L, Hash *t, lint32 size) { | 147 | static void setnodevector (lua_State *L, Hash *t, lint32 size) { |
126 | int i; | 148 | int i; |
@@ -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); | |||
21 | const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key); | 21 | const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key); |
22 | const TObject *luaH_getnum (const Hash *t, Number key); | 22 | const TObject *luaH_getnum (const Hash *t, Number key); |
23 | const TObject *luaH_getstr (const Hash *t, TString *key); | 23 | const TObject *luaH_getstr (const Hash *t, TString *key); |
24 | void luaH_remove (Hash *t, TObject *key); | ||
24 | void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val); | 25 | void luaH_set (lua_State *L, Hash *t, const TObject *key, const TObject *val); |
25 | int luaH_pos (lua_State *L, const Hash *t, const TObject *r); | 26 | int luaH_pos (lua_State *L, const Hash *t, const TObject *r); |
26 | void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val); | 27 | void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val); |