diff options
author | Waldemar Celes <celes@tecgraf.puc-rio.br> | 1994-04-20 19:07:57 -0300 |
---|---|---|
committer | Waldemar Celes <celes@tecgraf.puc-rio.br> | 1994-04-20 19:07:57 -0300 |
commit | 44521b21e542831a95de0c63271cd38d1cd4d394 (patch) | |
tree | 0fd861510cd5c0a1880410442c642c2388a02e57 | |
parent | f8fb7b39478c3468192c69fcb2154f9022dbab64 (diff) | |
download | lua-44521b21e542831a95de0c63271cd38d1cd4d394.tar.gz lua-44521b21e542831a95de0c63271cd38d1cd4d394.tar.bz2 lua-44521b21e542831a95de0c63271cd38d1cd4d394.zip |
Implementacao da nova estrategia para armazenar os arrays
em lista encadeada.
-rw-r--r-- | hash.c | 120 | ||||
-rw-r--r-- | hash.h | 10 | ||||
-rw-r--r-- | opcode.c | 10 | ||||
-rw-r--r-- | opcode.h | 3 | ||||
-rw-r--r-- | table.c | 128 | ||||
-rw-r--r-- | table.h | 26 |
6 files changed, 175 insertions, 122 deletions
@@ -4,7 +4,7 @@ | |||
4 | ** Luiz Henrique de Figueiredo - 17 Aug 90 | 4 | ** Luiz Henrique de Figueiredo - 17 Aug 90 |
5 | */ | 5 | */ |
6 | 6 | ||
7 | char *rcs_hash="$Id: hash.c,v 1.1 1993/12/17 18:41:19 celes Exp celes $"; | 7 | char *rcs_hash="$Id: hash.c,v 1.2 1994/03/28 15:14:02 celes Exp celes $"; |
8 | 8 | ||
9 | #include <string.h> | 9 | #include <string.h> |
10 | #include <stdlib.h> | 10 | #include <stdlib.h> |
@@ -26,10 +26,23 @@ char *rcs_hash="$Id: hash.c,v 1.1 1993/12/17 18:41:19 celes Exp celes $"; | |||
26 | #define nhash(t) ((t)->nhash) | 26 | #define nhash(t) ((t)->nhash) |
27 | #define nodelist(t) ((t)->list) | 27 | #define nodelist(t) ((t)->list) |
28 | #define list(t,i) ((t)->list[i]) | 28 | #define list(t,i) ((t)->list[i]) |
29 | #define markarray(t) ((t)->mark) | ||
29 | #define ref_tag(n) (tag(&(n)->ref)) | 30 | #define ref_tag(n) (tag(&(n)->ref)) |
30 | #define ref_nvalue(n) (nvalue(&(n)->ref)) | 31 | #define ref_nvalue(n) (nvalue(&(n)->ref)) |
31 | #define ref_svalue(n) (svalue(&(n)->ref)) | 32 | #define ref_svalue(n) (svalue(&(n)->ref)) |
32 | 33 | ||
34 | #ifndef ARRAYBLOCK | ||
35 | #define ARRAYBLOCK 50 | ||
36 | #endif | ||
37 | |||
38 | typedef struct ArrayList | ||
39 | { | ||
40 | Hash *array; | ||
41 | struct ArrayList *next; | ||
42 | } ArrayList; | ||
43 | |||
44 | static ArrayList *listhead = NULL; | ||
45 | |||
33 | static int head (Hash *t, Object *ref) /* hash function */ | 46 | static int head (Hash *t, Object *ref) /* hash function */ |
34 | { | 47 | { |
35 | if (tag(ref) == T_NUMBER) return (((int)nvalue(ref))%nhash(t)); | 48 | if (tag(ref) == T_NUMBER) return (((int)nvalue(ref))%nhash(t)); |
@@ -91,7 +104,7 @@ static void freelist (Node *n) | |||
91 | /* | 104 | /* |
92 | ** Create a new hash. Return the hash pointer or NULL on error. | 105 | ** Create a new hash. Return the hash pointer or NULL on error. |
93 | */ | 106 | */ |
94 | Hash *lua_hashcreate (unsigned int nhash) | 107 | static Hash *hashcreate (unsigned int nhash) |
95 | { | 108 | { |
96 | Hash *t = new (Hash); | 109 | Hash *t = new (Hash); |
97 | if (t == NULL) | 110 | if (t == NULL) |
@@ -113,7 +126,7 @@ Hash *lua_hashcreate (unsigned int nhash) | |||
113 | /* | 126 | /* |
114 | ** Delete a hash | 127 | ** Delete a hash |
115 | */ | 128 | */ |
116 | void lua_hashdelete (Hash *h) | 129 | static void hashdelete (Hash *h) |
117 | { | 130 | { |
118 | int i; | 131 | int i; |
119 | for (i=0; i<nhash(h); i++) | 132 | for (i=0; i<nhash(h); i++) |
@@ -122,6 +135,86 @@ void lua_hashdelete (Hash *h) | |||
122 | free(h); | 135 | free(h); |
123 | } | 136 | } |
124 | 137 | ||
138 | |||
139 | /* | ||
140 | ** Mark a hash and check its elements | ||
141 | */ | ||
142 | void lua_hashmark (Hash *h) | ||
143 | { | ||
144 | if (markarray(h) == 0) | ||
145 | { | ||
146 | int i; | ||
147 | markarray(h) = 1; | ||
148 | for (i=0; i<nhash(h); i++) | ||
149 | { | ||
150 | Node *n; | ||
151 | for (n = list(h,i); n != NULL; n = n->next) | ||
152 | { | ||
153 | lua_markobject(&n->ref); | ||
154 | lua_markobject(&n->val); | ||
155 | } | ||
156 | } | ||
157 | } | ||
158 | } | ||
159 | |||
160 | /* | ||
161 | ** Garbage collection to arrays | ||
162 | ** Delete all unmarked arrays. | ||
163 | */ | ||
164 | void lua_hashcollector (void) | ||
165 | { | ||
166 | ArrayList *curr = listhead, *prev = NULL; | ||
167 | while (curr != NULL) | ||
168 | { | ||
169 | ArrayList *next = curr->next; | ||
170 | if (markarray(curr->array) != 1) | ||
171 | { | ||
172 | if (prev == NULL) listhead = next; | ||
173 | else prev->next = next; | ||
174 | hashdelete(curr->array); | ||
175 | free(curr); | ||
176 | } | ||
177 | else | ||
178 | { | ||
179 | markarray(curr->array) = 0; | ||
180 | prev = curr; | ||
181 | } | ||
182 | curr = next; | ||
183 | } | ||
184 | } | ||
185 | |||
186 | |||
187 | /* | ||
188 | ** Create a new array | ||
189 | ** This function insert the new array at array list. It also | ||
190 | ** execute garbage collection if the number of array created | ||
191 | ** exceed a pre-defined range. | ||
192 | */ | ||
193 | Hash *lua_createarray (int nhash) | ||
194 | { | ||
195 | ArrayList *new = new(ArrayList); | ||
196 | if (new == NULL) | ||
197 | { | ||
198 | lua_error ("not enough memory"); | ||
199 | return NULL; | ||
200 | } | ||
201 | new->array = hashcreate(nhash); | ||
202 | if (new->array == NULL) | ||
203 | { | ||
204 | lua_error ("not enough memory"); | ||
205 | return NULL; | ||
206 | } | ||
207 | |||
208 | if (lua_nentity == lua_block) | ||
209 | lua_pack(); | ||
210 | |||
211 | lua_nentity++; | ||
212 | new->next = listhead; | ||
213 | listhead = new; | ||
214 | return new->array; | ||
215 | } | ||
216 | |||
217 | |||
125 | /* | 218 | /* |
126 | ** If the hash node is present, return its pointer, otherwise create a new | 219 | ** If the hash node is present, return its pointer, otherwise create a new |
127 | ** node for the given reference and also return its pointer. | 220 | ** node for the given reference and also return its pointer. |
@@ -151,26 +244,6 @@ Object *lua_hashdefine (Hash *t, Object *ref) | |||
151 | return (&n->val); | 244 | return (&n->val); |
152 | } | 245 | } |
153 | 246 | ||
154 | /* | ||
155 | ** Mark a hash and check its elements | ||
156 | */ | ||
157 | void lua_hashmark (Hash *h) | ||
158 | { | ||
159 | int i; | ||
160 | |||
161 | markarray(h) = 1; | ||
162 | |||
163 | for (i=0; i<nhash(h); i++) | ||
164 | { | ||
165 | Node *n; | ||
166 | for (n = list(h,i); n != NULL; n = n->next) | ||
167 | { | ||
168 | lua_markobject (&n->ref); | ||
169 | lua_markobject (&n->val); | ||
170 | } | ||
171 | } | ||
172 | } | ||
173 | |||
174 | 247 | ||
175 | /* | 248 | /* |
176 | ** Internal function to manipulate arrays. | 249 | ** Internal function to manipulate arrays. |
@@ -178,7 +251,6 @@ void lua_hashmark (Hash *h) | |||
178 | ** in the hash. | 251 | ** in the hash. |
179 | ** This function pushs the element value and its reference to the stack. | 252 | ** This function pushs the element value and its reference to the stack. |
180 | */ | 253 | */ |
181 | #include "lua.h" | ||
182 | static void firstnode (Hash *a, int h) | 254 | static void firstnode (Hash *a, int h) |
183 | { | 255 | { |
184 | if (h < nhash(a)) | 256 | if (h < nhash(a)) |
@@ -2,7 +2,7 @@ | |||
2 | ** hash.h | 2 | ** hash.h |
3 | ** hash manager for lua | 3 | ** hash manager for lua |
4 | ** Luiz Henrique de Figueiredo - 17 Aug 90 | 4 | ** Luiz Henrique de Figueiredo - 17 Aug 90 |
5 | ** $Id: $ | 5 | ** $Id: hash.h,v 1.1 1993/12/17 18:41:19 celes Exp celes $ |
6 | */ | 6 | */ |
7 | 7 | ||
8 | #ifndef hash_h | 8 | #ifndef hash_h |
@@ -22,13 +22,11 @@ typedef struct Hash | |||
22 | Node **list; | 22 | Node **list; |
23 | } Hash; | 23 | } Hash; |
24 | 24 | ||
25 | #define markarray(t) ((t)->mark) | ||
26 | 25 | ||
27 | Hash *lua_hashcreate (unsigned int nhash); | 26 | Hash *lua_createarray (int nhash); |
28 | void lua_hashdelete (Hash *h); | 27 | void lua_hashmark (Hash *h); |
28 | void lua_hashcollector (void); | ||
29 | Object *lua_hashdefine (Hash *t, Object *ref); | 29 | Object *lua_hashdefine (Hash *t, Object *ref); |
30 | void lua_hashmark (Hash *h); | ||
31 | |||
32 | void lua_next (void); | 30 | void lua_next (void); |
33 | 31 | ||
34 | #endif | 32 | #endif |
@@ -3,7 +3,7 @@ | |||
3 | ** TecCGraf - PUC-Rio | 3 | ** TecCGraf - PUC-Rio |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_opcode="$Id: opcode.c,v 1.3 1994/03/28 15:14:02 celes Exp celes $"; | 6 | char *rcs_opcode="$Id: opcode.c,v 1.4 1994/04/13 21:37:20 celes Exp celes $"; |
7 | 7 | ||
8 | #include <stdio.h> | 8 | #include <stdio.h> |
9 | #include <stdlib.h> | 9 | #include <stdlib.h> |
@@ -319,7 +319,7 @@ int lua_execute (Byte *pc) | |||
319 | if (tonumber(top-1)) return 1; | 319 | if (tonumber(top-1)) return 1; |
320 | if (nvalue(top-1) <= 0) nvalue(top-1) = 101; | 320 | if (nvalue(top-1) <= 0) nvalue(top-1) = 101; |
321 | } | 321 | } |
322 | avalue(top-1) = lua_createarray(lua_hashcreate(nvalue(top-1))); | 322 | avalue(top-1) = lua_createarray(nvalue(top-1)); |
323 | if (avalue(top-1) == NULL) | 323 | if (avalue(top-1) == NULL) |
324 | return 1; | 324 | return 1; |
325 | tag(top-1) = T_ARRAY; | 325 | tag(top-1) = T_ARRAY; |
@@ -603,13 +603,13 @@ int lua_execute (Byte *pc) | |||
603 | 603 | ||
604 | 604 | ||
605 | /* | 605 | /* |
606 | ** Mark all strings and arrays used by any object stored at stack. | 606 | ** Traverse all objects on stack |
607 | */ | 607 | */ |
608 | void lua_markstack (void) | 608 | void lua_travstack (void (*fn)(Object *)) |
609 | { | 609 | { |
610 | Object *o; | 610 | Object *o; |
611 | for (o = top-1; o >= stack; o--) | 611 | for (o = top-1; o >= stack; o--) |
612 | lua_markobject (o); | 612 | fn (o); |
613 | } | 613 | } |
614 | 614 | ||
615 | /* | 615 | /* |
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | ** TeCGraf - PUC-Rio | 2 | ** TeCGraf - PUC-Rio |
3 | ** $Id: opcode.h,v 1.3 1994/02/13 20:35:53 roberto Exp celes $ | 3 | ** $Id: opcode.h,v 1.4 1994/04/13 21:37:20 celes Exp celes $ |
4 | */ | 4 | */ |
5 | 5 | ||
6 | #ifndef opcode_h | 6 | #ifndef opcode_h |
@@ -159,5 +159,6 @@ void lua_obj2number (void); | |||
159 | void lua_print (void); | 159 | void lua_print (void); |
160 | void lua_internaldofile (void); | 160 | void lua_internaldofile (void); |
161 | void lua_internaldostring (void); | 161 | void lua_internaldostring (void); |
162 | void lua_travstack (void (*fn)(Object *)); | ||
162 | 163 | ||
163 | #endif | 164 | #endif |
@@ -3,7 +3,7 @@ | |||
3 | ** Module to control static tables | 3 | ** Module to control static tables |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_table="$Id: table.c,v 1.4 1994/04/06 12:55:08 celes Exp celes $"; | 6 | char *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]; | |||
75 | char **lua_string = stringbuffer; | 75 | char **lua_string = stringbuffer; |
76 | Word lua_nstring=0; | 76 | Word lua_nstring=0; |
77 | 77 | ||
78 | #ifndef MAXARRAY | ||
79 | #define MAXARRAY 512 | ||
80 | #endif | ||
81 | static Hash *arraybuffer[MAXARRAY]; | ||
82 | Hash **lua_array = arraybuffer; | ||
83 | Word lua_narray=0; | ||
84 | |||
85 | #define MAXFILE 20 | 78 | #define MAXFILE 20 |
86 | char *lua_file[MAXFILE]; | 79 | char *lua_file[MAXFILE]; |
87 | int lua_nfile; | 80 | int lua_nfile; |
88 | 81 | ||
89 | 82 | ||
83 | #define markstring(s) (*((s)-1)) | ||
84 | |||
85 | |||
86 | /* Variables to controll garbage collection */ | ||
87 | Word lua_block=10; /* to check when garbage collector will be called */ | ||
88 | Word 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 | */ | ||
165 | void 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 | */ |
164 | void lua_markobject (Object *o) | 176 | void 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 | */ |
175 | static void lua_marktable (void) | 189 | void 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 | */ |
187 | static void lua_pack (void) | 207 | void 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 | */ | ||
258 | void *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. |
@@ -1,7 +1,7 @@ | |||
1 | /* | 1 | /* |
2 | ** Module to control static tables | 2 | ** Module to control static tables |
3 | ** TeCGraf - PUC-Rio | 3 | ** TeCGraf - PUC-Rio |
4 | ** $Id: table.h,v 1.1 1993/12/17 18:41:19 celes Exp roberto $ | 4 | ** $Id: table.h,v 1.2 1993/12/22 21:15:16 roberto Exp celes $ |
5 | */ | 5 | */ |
6 | 6 | ||
7 | #ifndef table_h | 7 | #ifndef table_h |
@@ -22,17 +22,21 @@ extern Word lua_narray; | |||
22 | extern char *lua_file[]; | 22 | extern char *lua_file[]; |
23 | extern int lua_nfile; | 23 | extern int lua_nfile; |
24 | 24 | ||
25 | #define lua_markstring(s) (*((s)-1)) | 25 | extern Word lua_block; |
26 | extern Word lua_nentity; | ||
26 | 27 | ||
27 | 28 | ||
28 | int lua_findsymbol (char *s); | 29 | |
29 | int lua_findconstant (char *s); | 30 | int lua_findsymbol (char *s); |
30 | void lua_markobject (Object *o); | 31 | int lua_findconstant (char *s); |
31 | char *lua_createstring (char *s); | 32 | void lua_travsymbol (void (*fn)(Object *)); |
32 | void *lua_createarray (void *a); | 33 | void lua_markobject (Object *o); |
33 | int lua_addfile (char *fn); | 34 | void lua_pack (void); |
34 | int lua_delfile (void); | 35 | void lua_stringcollector (void); |
35 | char *lua_filename (void); | 36 | char *lua_createstring (char *s); |
36 | void lua_nextvar (void); | 37 | int lua_addfile (char *fn); |
38 | int lua_delfile (void); | ||
39 | char *lua_filename (void); | ||
40 | void lua_nextvar (void); | ||
37 | 41 | ||
38 | #endif | 42 | #endif |