aboutsummaryrefslogtreecommitdiff
path: root/table.c
diff options
context:
space:
mode:
Diffstat (limited to 'table.c')
-rw-r--r--table.c128
1 files changed, 53 insertions, 75 deletions
diff --git a/table.c b/table.c
index abbb8850..5d2f1b28 100644
--- a/table.c
+++ b/table.c
@@ -3,7 +3,7 @@
3** Module to control static tables 3** Module to control static tables
4*/ 4*/
5 5
6char *rcs_table="$Id: table.c,v 1.4 1994/04/06 12:55:08 celes Exp celes $"; 6char *rcs_table="$Id: table.c,v 1.5 1994/04/13 22:10:21 celes Exp celes $";
7 7
8#include <stdlib.h> 8#include <stdlib.h>
9#include <string.h> 9#include <string.h>
@@ -75,18 +75,19 @@ static char *stringbuffer[MAXSTRING];
75char **lua_string = stringbuffer; 75char **lua_string = stringbuffer;
76Word lua_nstring=0; 76Word lua_nstring=0;
77 77
78#ifndef MAXARRAY
79#define MAXARRAY 512
80#endif
81static Hash *arraybuffer[MAXARRAY];
82Hash **lua_array = arraybuffer;
83Word lua_narray=0;
84
85#define MAXFILE 20 78#define MAXFILE 20
86char *lua_file[MAXFILE]; 79char *lua_file[MAXFILE];
87int lua_nfile; 80int lua_nfile;
88 81
89 82
83#define markstring(s) (*((s)-1))
84
85
86/* Variables to controll garbage collection */
87Word lua_block=10; /* to check when garbage collector will be called */
88Word lua_nentity; /* counter of new entities (strings and arrays) */
89
90
90/* 91/*
91** Given a name, search it at symbol table and return its index. If not 92** Given a name, search it at symbol table and return its index. If not
92** found, allocate at end of table, checking oveflow and return its index. 93** found, allocate at end of table, checking oveflow and return its index.
@@ -159,65 +160,64 @@ int lua_findconstant (char *s)
159 160
160 161
161/* 162/*
163** Traverse symbol table objects
164*/
165void lua_travsymbol (void (*fn)(Object *))
166{
167 int i;
168 for (i=0; i<lua_ntable; i++)
169 fn(&s_object(i));
170}
171
172
173/*
162** Mark an object if it is a string or a unmarked array. 174** Mark an object if it is a string or a unmarked array.
163*/ 175*/
164void lua_markobject (Object *o) 176void lua_markobject (Object *o)
165{ 177{
166 if (tag(o) == T_STRING) 178 if (tag(o) == T_STRING)
167 lua_markstring (svalue(o)) = 1; 179 markstring (svalue(o)) = 1;
168 else if (tag(o) == T_ARRAY && markarray(avalue(o)) == 0) 180 else if (tag(o) == T_ARRAY)
169 lua_hashmark (avalue(o)); 181 lua_hashmark (avalue(o));
170} 182}
171 183
184
172/* 185/*
173** Mark all strings and arrays used by any object stored at symbol table. 186** Garbage collection.
187** Delete all unused strings and arrays.
174*/ 188*/
175static void lua_marktable (void) 189void lua_pack (void)
176{ 190{
177 int i; 191 /* mark stack strings */
178 for (i=0; i<lua_ntable; i++) 192 lua_travstack(lua_markobject);
179 lua_markobject (&s_object(i)); 193
194 /* mark symbol table strings */
195 lua_travsymbol(lua_markobject);
196
197 lua_stringcollector();
198 lua_hashcollector();
199
200 lua_nentity = 0; /* reset counter */
180} 201}
181 202
182/* 203/*
183** Simulate a garbage colection. When string table or array table overflows, 204** Garbage collection to atrings.
184** this function check if all allocated strings and arrays are in use. If 205** Delete all unmarked strings
185** there are unused ones, pack (compress) the tables.
186*/ 206*/
187static void lua_pack (void) 207void lua_stringcollector (void)
188{ 208{
189 lua_markstack (); 209 int i, j;
190 lua_marktable (); 210 for (i=j=0; i<lua_nstring; i++)
191 211 if (markstring(lua_string[i]) == 1)
192 { /* pack string */ 212 {
193 int i, j; 213 lua_string[j++] = lua_string[i];
194 for (i=j=0; i<lua_nstring; i++) 214 markstring(lua_string[i]) = 0;
195 if (lua_markstring(lua_string[i]) == 1) 215 }
196 { 216 else
197 lua_string[j++] = lua_string[i]; 217 {
198 lua_markstring(lua_string[i]) = 0; 218 free (lua_string[i]-1);
199 } 219 }
200 else 220 lua_nstring = j;
201 {
202 free (lua_string[i]-1);
203 }
204 lua_nstring = j;
205 }
206
207 { /* pack array */
208 int i, j;
209 for (i=j=0; i<lua_narray; i++)
210 if (markarray(lua_array[i]) == 1)
211 {
212 lua_array[j++] = lua_array[i];
213 markarray(lua_array[i]) = 0;
214 }
215 else
216 {
217 lua_hashdelete (lua_array[i]);
218 }
219 lua_narray = j;
220 }
221} 221}
222 222
223/* 223/*
@@ -237,7 +237,7 @@ char *lua_createstring (char *s)
237 return lua_string[i]; 237 return lua_string[i];
238 } 238 }
239 239
240 if (lua_nstring >= MAXSTRING-1) 240 if (lua_nentity == lua_block || lua_nstring >= MAXSTRING-1)
241 { 241 {
242 lua_pack (); 242 lua_pack ();
243 if (lua_nstring >= MAXSTRING-1) 243 if (lua_nstring >= MAXSTRING-1)
@@ -247,33 +247,11 @@ char *lua_createstring (char *s)
247 } 247 }
248 } 248 }
249 lua_string[lua_nstring++] = s; 249 lua_string[lua_nstring++] = s;
250 lua_nentity++;
250 return s; 251 return s;
251} 252}
252 253
253/* 254/*
254** Allocate a new array, already created, at array table. The function puts
255** it at the end of the table, checking overflow, and returns its own pointer,
256** or NULL on error.
257*/
258void *lua_createarray (void *a)
259{
260 if (a == NULL) return NULL;
261
262 if (lua_narray >= MAXARRAY-1)
263 {
264 lua_pack ();
265 if (lua_narray >= MAXARRAY-1)
266 {
267 lua_error ("indexed table overflow");
268 return NULL;
269 }
270 }
271 lua_array[lua_narray++] = a;
272 return a;
273}
274
275
276/*
277** Add a file name at file table, checking overflow. This function also set 255** Add a file name at file table, checking overflow. This function also set
278** the external variable "lua_filename" with the function filename set. 256** the external variable "lua_filename" with the function filename set.
279** Return 0 on success or 1 on error. 257** Return 0 on success or 1 on error.