aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-05-14 15:38:29 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-05-14 15:38:29 -0300
commitbd9e68cfcdbbae2fd49ab51a9df4a93ec56eff45 (patch)
treef11ad6c42c0bb42eaa1b45548684fbc60c27996b
parent9747f3c87a205d9aa914a650686d6dc4accde1d9 (diff)
downloadlua-bd9e68cfcdbbae2fd49ab51a9df4a93ec56eff45.tar.gz
lua-bd9e68cfcdbbae2fd49ab51a9df4a93ec56eff45.tar.bz2
lua-bd9e68cfcdbbae2fd49ab51a9df4a93ec56eff45.zip
new implementation of gc: "Pre-collect" garbage in temporary lists and
then call fallbacks.
-rw-r--r--func.c31
-rw-r--r--func.h5
-rw-r--r--hash.c70
-rw-r--r--hash.h7
-rw-r--r--table.c46
-rw-r--r--tree.c41
-rw-r--r--tree.h8
7 files changed, 121 insertions, 87 deletions
diff --git a/func.c b/func.c
index 5b15228e..3c922043 100644
--- a/func.c
+++ b/func.c
@@ -49,35 +49,44 @@ void luaI_freefunc (TFunc *f)
49 luaI_free (f); 49 luaI_free (f);
50} 50}
51 51
52
53void luaI_funcfree (TFunc *l)
54{
55 while (l) {
56 TFunc *next = l->next;
57 luaI_freefunc(l);
58 l = next;
59 }
60}
61
52/* 62/*
53** Garbage collection function. 63** Garbage collection function.
54** This function traverse the function list freeing unindexed functions
55*/ 64*/
56Long luaI_funccollector (void) 65TFunc *luaI_funccollector (long *acum)
57{ 66{
58 TFunc *curr = function_root; 67 TFunc *curr = function_root;
59 TFunc *prev = NULL; 68 TFunc *prev = NULL;
60 Long counter = 0; 69 TFunc *frees = NULL;
61 while (curr) 70 long counter = 0;
62 { 71 while (curr) {
63 TFunc *next = curr->next; 72 TFunc *next = curr->next;
64 if (!curr->marked) 73 if (!curr->marked) {
65 {
66 if (prev == NULL) 74 if (prev == NULL)
67 function_root = next; 75 function_root = next;
68 else 76 else
69 prev->next = next; 77 prev->next = next;
70 luaI_freefunc (curr); 78 curr->next = frees;
79 frees = curr;
71 ++counter; 80 ++counter;
72 } 81 }
73 else 82 else {
74 {
75 curr->marked = 0; 83 curr->marked = 0;
76 prev = curr; 84 prev = curr;
77 } 85 }
78 curr = next; 86 curr = next;
79 } 87 }
80 return counter; 88 *acum += counter;
89 return frees;
81} 90}
82 91
83 92
diff --git a/func.h b/func.h
index 74e954c7..97185b9e 100644
--- a/func.h
+++ b/func.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: func.h,v 1.7 1996/03/08 12:04:04 roberto Exp roberto $ 2** $Id: func.h,v 1.8 1996/03/14 15:54:20 roberto Exp roberto $
3*/ 3*/
4 4
5#ifndef func_h 5#ifndef func_h
@@ -30,7 +30,8 @@ typedef struct TFunc
30 LocVar *locvars; 30 LocVar *locvars;
31} TFunc; 31} TFunc;
32 32
33Long luaI_funccollector (void); 33TFunc *luaI_funccollector (long *cont);
34void luaI_funcfree (TFunc *l);
34void luaI_insertfunction (TFunc *f); 35void luaI_insertfunction (TFunc *f);
35 36
36void luaI_initTFunc (TFunc *f); 37void luaI_initTFunc (TFunc *f);
diff --git a/hash.c b/hash.c
index 9c543955..5ff64c84 100644
--- a/hash.c
+++ b/hash.c
@@ -3,7 +3,7 @@
3** hash manager for lua 3** hash manager for lua
4*/ 4*/
5 5
6char *rcs_hash="$Id: hash.c,v 2.41 1997/04/06 14:08:08 roberto Exp roberto $"; 6char *rcs_hash="$Id: hash.c,v 2.42 1997/05/08 20:43:30 roberto Exp roberto $";
7 7
8 8
9#include "luamem.h" 9#include "luamem.h"
@@ -167,46 +167,50 @@ void lua_hashmark (Hash *h)
167} 167}
168 168
169 169
170void luaI_hashcallIM (void) 170void luaI_hashcallIM (Hash *l)
171{ 171{
172 Hash *curr_array;
173 TObject t; 172 TObject t;
174 ttype(&t) = LUA_T_ARRAY; 173 ttype(&t) = LUA_T_ARRAY;
175 for (curr_array = listhead; curr_array; curr_array = curr_array->next) 174 for (; l; l=l->next) {
176 if (markarray(curr_array) != 1) 175 avalue(&t) = l;
177 { 176 luaI_gcIM(&t);
178 avalue(&t) = curr_array; 177 }
179 luaI_gcIM(&t);
180 }
181} 178}
182 179
183 180
184/* 181void luaI_hashfree (Hash *frees)
185** Garbage collection to arrays
186** Delete all unmarked arrays.
187*/
188Long lua_hashcollector (void)
189{ 182{
190 Hash *curr_array = listhead, *prev = NULL; 183 while (frees) {
191 Long counter = 0; 184 Hash *next = frees->next;
192 while (curr_array != NULL) 185 hashdelete(frees);
193 { 186 frees = next;
194 Hash *next = curr_array->next;
195 if (markarray(curr_array) != 1)
196 {
197 if (prev == NULL) listhead = next;
198 else prev->next = next;
199 hashdelete(curr_array);
200 ++counter;
201 } 187 }
202 else 188}
203 { 189
204 markarray(curr_array) = 0; 190
205 prev = curr_array; 191Hash *luaI_hashcollector (long *acum)
192{
193 Hash *curr_array = listhead, *prev = NULL, *frees = NULL;
194 long counter = 0;
195 while (curr_array != NULL) {
196 Hash *next = curr_array->next;
197 if (markarray(curr_array) != 1) {
198 if (prev == NULL)
199 listhead = next;
200 else
201 prev->next = next;
202 curr_array->next = frees;
203 frees = curr_array;
204 ++counter;
205 }
206 else {
207 markarray(curr_array) = 0;
208 prev = curr_array;
209 }
210 curr_array = next;
206 } 211 }
207 curr_array = next; 212 *acum += counter;
208 } 213 return frees;
209 return counter;
210} 214}
211 215
212 216
diff --git a/hash.h b/hash.h
index a91e1e15..4fbde55c 100644
--- a/hash.h
+++ b/hash.h
@@ -1,7 +1,7 @@
1/* 1/*
2** hash.h 2** hash.h
3** hash manager for lua 3** hash manager for lua
4** $Id: hash.h,v 2.14 1997/03/19 19:41:10 roberto Exp roberto $ 4** $Id: hash.h,v 2.15 1997/03/31 14:02:58 roberto Exp roberto $
5*/ 5*/
6 6
7#ifndef hash_h 7#ifndef hash_h
@@ -29,8 +29,9 @@ int lua_equalObj (TObject *t1, TObject *t2);
29int luaI_redimension (int nhash); 29int luaI_redimension (int nhash);
30Hash *lua_createarray (int nhash); 30Hash *lua_createarray (int nhash);
31void lua_hashmark (Hash *h); 31void lua_hashmark (Hash *h);
32Long lua_hashcollector (void); 32Hash *luaI_hashcollector (long *count);
33void luaI_hashcallIM (void); 33void luaI_hashcallIM (Hash *l);
34void luaI_hashfree (Hash *frees);
34TObject *lua_hashget (Hash *t, TObject *ref); 35TObject *lua_hashget (Hash *t, TObject *ref);
35TObject *lua_hashdefine (Hash *t, TObject *ref); 36TObject *lua_hashdefine (Hash *t, TObject *ref);
36void lua_next (void); 37void lua_next (void);
diff --git a/table.c b/table.c
index 1d6afbfc..86266a96 100644
--- a/table.c
+++ b/table.c
@@ -3,10 +3,11 @@
3** Module to control static tables 3** Module to control static tables
4*/ 4*/
5 5
6char *rcs_table="$Id: table.c,v 2.67 1997/04/06 14:08:08 roberto Exp roberto $"; 6char *rcs_table="$Id: table.c,v 2.68 1997/04/07 14:48:53 roberto Exp roberto $";
7 7
8#include "luamem.h" 8#include "luamem.h"
9#include "auxlib.h" 9#include "auxlib.h"
10#include "func.h"
10#include "opcode.h" 11#include "opcode.h"
11#include "tree.h" 12#include "tree.h"
12#include "hash.h" 13#include "hash.h"
@@ -176,32 +177,43 @@ static void call_nilIM (void)
176** Garbage collection. 177** Garbage collection.
177** Delete all unused strings and arrays. 178** Delete all unused strings and arrays.
178*/ 179*/
179Long luaI_collectgarbage (void) 180static long gc_block = GARBAGE_BLOCK;
181static long gc_nentity = 0; /* total of strings, arrays, etc */
182
183static void markall (void)
180{ 184{
181 Long recovered = 0;
182 lua_travstack(lua_markobject); /* mark stack objects */ 185 lua_travstack(lua_markobject); /* mark stack objects */
183 lua_travsymbol(lua_markobject); /* mark symbol table objects */ 186 lua_travsymbol(lua_markobject); /* mark symbol table objects */
184 luaI_travlock(lua_markobject); /* mark locked objects */ 187 luaI_travlock(lua_markobject); /* mark locked objects */
185 luaI_travfallbacks(lua_markobject); /* mark fallbacks */ 188 luaI_travfallbacks(lua_markobject); /* mark fallbacks */
186 luaI_hashcallIM(); 189}
187 luaI_strcallIM(); 190
191
192static void lua_collectgarbage (void)
193{
194 long recovered = 0;
195 Hash *freetable;
196 TaggedString *freestr;
197 TFunc *freefunc;
198 markall();
199 freetable = luaI_hashcollector(&recovered);
200 freestr = luaI_strcollector(&recovered);
201 freefunc = luaI_funccollector(&recovered);
202 gc_block = 2*(gc_block-recovered);
203 gc_nentity -= recovered;
204 luaI_hashcallIM(freetable);
205 luaI_strcallIM(freestr);
188 call_nilIM(); 206 call_nilIM();
189 luaI_invalidaterefs(); 207 luaI_hashfree(freetable);
190 recovered += lua_strcollector(); 208 luaI_strfree(freestr);
191 recovered += lua_hashcollector(); 209 luaI_funcfree(freefunc);
192 recovered += luaI_funccollector();
193 return recovered;
194} 210}
195 211
212
196void lua_pack (void) 213void lua_pack (void)
197{ 214{
198 static unsigned long block = GARBAGE_BLOCK; 215 if (gc_nentity++ >= gc_block)
199 static unsigned long nentity = 0; /* total of strings, arrays, etc */ 216 lua_collectgarbage();
200 unsigned long recovered = 0;
201 if (nentity++ < block) return;
202 recovered = luaI_collectgarbage();
203 block = 2*(block-recovered);
204 nentity -= recovered;
205} 217}
206 218
207 219
diff --git a/tree.c b/tree.c
index 0141a99c..ea2b2dec 100644
--- a/tree.c
+++ b/tree.c
@@ -3,7 +3,7 @@
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4*/ 4*/
5 5
6char *rcs_tree="$Id: tree.c,v 1.24 1997/03/31 14:17:09 roberto Exp roberto $"; 6char *rcs_tree="$Id: tree.c,v 1.25 1997/05/05 20:21:23 roberto Exp roberto $";
7 7
8 8
9#include <string.h> 9#include <string.h>
@@ -29,7 +29,8 @@ static int initialized = 0;
29 29
30static stringtable string_root[NUM_HASHS]; 30static stringtable string_root[NUM_HASHS];
31 31
32static TaggedString EMPTY = {LUA_T_STRING, 0, NOT_USED, NOT_USED, 0, 2, {0}}; 32static TaggedString EMPTY = {LUA_T_STRING, NULL, 0, NOT_USED, NOT_USED,
33 0, 2, {0}};
33 34
34 35
35static unsigned long hash (char *buff, long size) 36static unsigned long hash (char *buff, long size)
@@ -134,32 +135,34 @@ TaggedString *lua_createstring (char *str)
134} 135}
135 136
136 137
137void luaI_strcallIM (void) 138void luaI_strcallIM (TaggedString *l)
138{ 139{
139 int i;
140 TObject o; 140 TObject o;
141 ttype(&o) = LUA_T_USERDATA; 141 ttype(&o) = LUA_T_USERDATA;
142 for (i=0; i<NUM_HASHS; i++) { 142 for (; l; l=l->next) {
143 stringtable *tb = &string_root[i]; 143 tsvalue(&o) = l;
144 int j; 144 luaI_gcIM(&o);
145 for (j=0; j<tb->size; j++) { 145 }
146 TaggedString *t = tb->hash[j]; 146}
147 if (t != NULL && t->tag != LUA_T_STRING && t->marked == 0) { 147
148 tsvalue(&o) = t; 148
149 luaI_gcIM(&o); 149void luaI_strfree (TaggedString *l)
150 } 150{
151 } 151 while (l) {
152 TaggedString *next = l->next;
153 luaI_free(l);
154 l = next;
152 } 155 }
153} 156}
154 157
155 158
156/* 159/*
157** Garbage collection function. 160** Garbage collection function.
158** This function traverse the string list freeing unindexed strings
159*/ 161*/
160Long lua_strcollector (void) 162TaggedString *luaI_strcollector (long *acum)
161{ 163{
162 Long counter = 0; 164 Long counter = 0;
165 TaggedString *frees = NULL;
163 int i; 166 int i;
164 for (i=0; i<NUM_HASHS; i++) 167 for (i=0; i<NUM_HASHS; i++)
165 { 168 {
@@ -174,13 +177,15 @@ Long lua_strcollector (void)
174 t->marked = 0; 177 t->marked = 0;
175 else 178 else
176 { 179 {
177 luaI_free(t); 180 t->next = frees;
181 frees = t;
178 tb->hash[j] = &EMPTY; 182 tb->hash[j] = &EMPTY;
179 counter++; 183 counter++;
180 } 184 }
181 } 185 }
182 } 186 }
183 } 187 }
184 return counter; 188 *acum += counter;
189 return frees;
185} 190}
186 191
diff --git a/tree.h b/tree.h
index ea9422ea..15b77b5f 100644
--- a/tree.h
+++ b/tree.h
@@ -1,7 +1,7 @@
1/* 1/*
2** tree.h 2** tree.h
3** TecCGraf - PUC-Rio 3** TecCGraf - PUC-Rio
4** $Id: tree.h,v 1.15 1997/02/11 11:35:05 roberto Exp roberto $ 4** $Id: tree.h,v 1.16 1997/03/19 19:41:10 roberto Exp roberto $
5*/ 5*/
6 6
7#ifndef tree_h 7#ifndef tree_h
@@ -15,6 +15,7 @@
15typedef struct TaggedString 15typedef struct TaggedString
16{ 16{
17 int tag; /* if != LUA_T_STRING, this is a userdata */ 17 int tag; /* if != LUA_T_STRING, this is a userdata */
18 struct TaggedString *next;
18 long size; 19 long size;
19 Word varindex; /* != NOT_USED if this is a symbol */ 20 Word varindex; /* != NOT_USED if this is a symbol */
20 Word constindex; /* != NOT_USED if this is a constant */ 21 Word constindex; /* != NOT_USED if this is a constant */
@@ -26,7 +27,8 @@ typedef struct TaggedString
26 27
27TaggedString *lua_createstring (char *str); 28TaggedString *lua_createstring (char *str);
28TaggedString *luaI_createuserdata (char *buff, long size, int tag); 29TaggedString *luaI_createuserdata (char *buff, long size, int tag);
29Long lua_strcollector (void); 30TaggedString *luaI_strcollector (long *cont);
30void luaI_strcallIM (void); 31void luaI_strfree (TaggedString *l);
32void luaI_strcallIM (TaggedString *l);
31 33
32#endif 34#endif