48 |
{ |
{ |
49 |
int i; |
int i; |
50 |
|
|
51 |
env->gc_limit= 20; |
env->gc_limit= 200; |
52 |
env->gc_count= 0; |
env->gc_count= 0; |
53 |
env->gc_ref= NULL; |
env->gc_ref= NULL; |
54 |
env->gc_protect= NULL; |
env->gc_protect= NULL; |
83 |
free(temp); /* Free the old top stack item */ |
free(temp); /* Free the old top stack item */ |
84 |
|
|
85 |
env->gc_limit--; |
env->gc_limit--; |
|
gc_init(env); |
|
86 |
} |
} |
87 |
|
|
88 |
/* Returns a pointer to a pointer to an element in the hash table. */ |
/* Returns a pointer to a pointer to an element in the hash table. */ |
127 |
env->gc_ref= nitem; |
env->gc_ref= nitem; |
128 |
|
|
129 |
env->gc_count++; |
env->gc_count++; |
130 |
|
nval->gc_garb= 1; |
|
protect(env, nval); |
|
|
gc_init(env); |
|
|
unprotect(env); |
|
131 |
|
|
132 |
return nval; |
return nval; |
133 |
} |
} |
134 |
|
|
135 |
/* Mark values recursively. |
/* Mark values recursively. |
136 |
Marked values are not collected by the GC. */ |
Marked values are not collected by the GC. */ |
137 |
void gc_mark(value *val) |
inline void gc_mark(value *val) |
138 |
{ |
{ |
139 |
stackitem *iterator; |
stackitem *iterator; |
140 |
|
|
141 |
if(val==NULL || val->gc_garb==0) |
if(val->gc_garb==0) |
142 |
return; |
return; |
143 |
|
|
144 |
val->gc_garb= 0; |
val->gc_garb= 0; |
153 |
} |
} |
154 |
} |
} |
155 |
|
|
156 |
|
inline void gc_maybe(environment *env) |
157 |
|
{ |
158 |
|
if(env->gc_count < env->gc_limit) |
159 |
|
return; |
160 |
|
else |
161 |
|
return gc_init(env); |
162 |
|
} |
163 |
|
|
164 |
/* Start GC */ |
/* Start GC */ |
165 |
extern void gc_init(environment *env) |
extern void gc_init(environment *env) |
166 |
{ |
{ |
168 |
symbol *tsymb; |
symbol *tsymb; |
169 |
int i; |
int i; |
170 |
|
|
|
if(env->gc_count < env->gc_limit) |
|
|
return; |
|
|
|
|
|
/* Garb by default */ |
|
|
iterator= env->gc_ref; |
|
|
while(iterator!=NULL) { |
|
|
iterator->item->gc_garb= 1; |
|
|
iterator= iterator->next; |
|
|
} |
|
|
|
|
171 |
/* Mark protected values */ |
/* Mark protected values */ |
172 |
iterator= env->gc_protect; |
iterator= env->gc_protect; |
173 |
while(iterator!=NULL) { |
while(iterator!=NULL) { |
186 |
for(i= 0; i<HASHTBLSIZE; i++) { |
for(i= 0; i<HASHTBLSIZE; i++) { |
187 |
tsymb= env->symbols[i]; |
tsymb= env->symbols[i]; |
188 |
while(tsymb!=NULL) { |
while(tsymb!=NULL) { |
189 |
gc_mark(tsymb->val); |
if (tsymb->val != NULL) |
190 |
|
gc_mark(tsymb->val); |
191 |
tsymb= tsymb->next; |
tsymb= tsymb->next; |
192 |
} |
} |
193 |
} |
} |
208 |
env->gc_ref->item->content.ptr= titem->next; |
env->gc_ref->item->content.ptr= titem->next; |
209 |
free(titem); |
free(titem); |
210 |
} |
} |
|
break; |
|
211 |
default: |
default: |
|
break; |
|
212 |
} |
} |
213 |
free(env->gc_ref->item); /* Remove from gc_ref */ |
free(env->gc_ref->item); /* Remove from gc_ref */ |
214 |
titem= env->gc_ref->next; |
titem= env->gc_ref->next; |
215 |
free(env->gc_ref); /* Remove value */ |
free(env->gc_ref); /* Remove value */ |
216 |
env->gc_ref= titem; |
env->gc_ref= titem; |
217 |
} else { /* Keep values */ |
continue; |
|
titem= env->gc_ref->next; |
|
|
env->gc_ref->next= new_head; |
|
|
new_head= env->gc_ref; |
|
|
env->gc_ref= titem; |
|
|
env->gc_count++; |
|
218 |
} |
} |
219 |
|
|
220 |
|
/* Keep values */ |
221 |
|
titem= env->gc_ref->next; |
222 |
|
env->gc_ref->next= new_head; |
223 |
|
new_head= env->gc_ref; |
224 |
|
new_head->item->gc_garb= 1; |
225 |
|
env->gc_ref= titem; |
226 |
|
env->gc_count++; |
227 |
} |
} |
228 |
|
|
229 |
env->gc_limit= env->gc_count*2; |
env->gc_limit= env->gc_count*2; |
609 |
|
|
610 |
eval_start: |
eval_start: |
611 |
|
|
612 |
|
gc_maybe(env); |
613 |
|
|
614 |
if(env->head==NULL) { |
if(env->head==NULL) { |
615 |
printerr("Too Few Arguments"); |
printerr("Too Few Arguments"); |
616 |
env->err=1; |
env->err=1; |
867 |
} |
} |
868 |
|
|
869 |
env->gc_limit= 0; |
env->gc_limit= 0; |
870 |
gc_init(env); |
gc_maybe(env); |
871 |
|
|
872 |
if(env->free_string!=NULL) |
if(env->free_string!=NULL) |
873 |
free(env->free_string); |
free(env->free_string); |
1006 |
toss(&myenv); /* No error check in main */ |
toss(&myenv); /* No error check in main */ |
1007 |
eval(&myenv); |
eval(&myenv); |
1008 |
} |
} |
1009 |
gc_init(&myenv); |
gc_maybe(&myenv); |
1010 |
} |
} |
1011 |
quit(&myenv); |
quit(&myenv); |
1012 |
return EXIT_FAILURE; |
return EXIT_FAILURE; |