--- stack/stack.c 2002/03/10 08:30:43 1.97 +++ stack/stack.c 2002/03/10 09:13:36 1.98 @@ -51,7 +51,6 @@ env->gc_limit= 200; env->gc_count= 0; env->gc_ref= NULL; - env->gc_protect= NULL; env->head= NULL; for(i= 0; igc_protect; - while(iterator!=NULL) { - gc_mark(iterator->item); - iterator= iterator->next; - } - /* Mark values on stack */ iterator= env->head; while(iterator!=NULL) { @@ -196,7 +188,8 @@ while(env->gc_ref!=NULL) { /* Sweep unused values */ - if(env->gc_ref->item->gc_garb) { + if(env->gc_ref->item->gc_garb + && !(env->gc_ref->item->gc_protect)) { switch(env->gc_ref->item->type) { /* Remove content */ case string: @@ -231,20 +224,43 @@ } /* Protect values from GC */ -void protect(environment *env, value *val) +void protect(value *val) { - stackitem *new_item= malloc(sizeof(stackitem)); - new_item->item= val; - new_item->next= env->gc_protect; - env->gc_protect= new_item; + stackitem *iterator; + + if(val->gc_protect) + return; + + val->gc_protect= 1; + + if(val->type==list) { + iterator= val->content.ptr; + + while(iterator!=NULL) { + protect(iterator->item); + iterator= iterator->next; + } + } } /* Unprotect values from GC */ -void unprotect(environment *env) +void unprotect(value *val) { - stackitem *temp= env->gc_protect; - env->gc_protect= env->gc_protect->next; - free(temp); + stackitem *iterator; + + if(!(val->gc_protect)) + return; + + val->gc_protect= 0; + + if(val->type==list) { + iterator= val->content.ptr; + + while(iterator!=NULL) { + unprotect(iterator->item); + iterator= iterator->next; + } + } } /* Push a value onto the stack */ @@ -350,9 +366,9 @@ char *mangled; /* Mangled function name */ new_value= new_val(env); - protect(env, new_value); + protect(new_value); new_fvalue= new_val(env); - protect(env, new_fvalue); + protect(new_fvalue); /* The new value is a symbol */ new_value->type= symb; @@ -398,7 +414,7 @@ } push_val(env, new_value); - unprotect(env); unprotect(env); + unprotect(new_value); unprotect(new_fvalue); } /* Print newline. */ @@ -591,11 +607,11 @@ env->err=3; return; } - protect(env, val); + protect(val); toss(env); /* toss the symbol */ if(env->err) return; push_val(env, val); /* Return its bound value */ - unprotect(env); + unprotect(val); } /* If the top element is a symbol, determine if it's bound to a @@ -637,7 +653,7 @@ /* If it's a list */ case list: temp_val= env->head->item; - protect(env, temp_val); + protect(temp_val); toss(env); if(env->err) return; iterator= (stackitem*)temp_val->content.ptr; @@ -658,7 +674,7 @@ } iterator= iterator->next; } - unprotect(env); + unprotect(temp_val); return; default: @@ -702,7 +718,7 @@ iterator= env->head; pack= new_val(env); - protect(env, pack); + protect(pack); if(iterator==NULL || (iterator->item->type==symb @@ -733,7 +749,7 @@ push_val(env, pack); rev(env); - unprotect(env); + unprotect(pack); } /* Relocate elements of the list on the stack. */ @@ -978,7 +994,7 @@ } if(myenv.interactive) { - printf("Stack version $Revision: 1.97 $\n\ + printf("Stack version $Revision: 1.98 $\n\ Copyright (C) 2002 Mats Alritzson and Teddy Hogeborn\n\ Stack comes with ABSOLUTELY NO WARRANTY; for details type `warranty;'.\n\ This is free software, and you are welcome to redistribute it\n\ @@ -1031,7 +1047,7 @@ && env->head->next->item->type==string) { a_val= env->head->item; b_val= env->head->next->item; - protect(env, a_val); protect(env, b_val); + protect(a_val); protect(b_val); toss(env); if(env->err) return; toss(env); if(env->err) return; len= strlen(a_val->content.ptr)+strlen(b_val->content.ptr)+1; @@ -1039,7 +1055,7 @@ strcpy(new_string, b_val->content.ptr); strcat(new_string, a_val->content.ptr); push_cstring(env, new_string); - unprotect(env); unprotect(env); + unprotect(a_val); unprotect(b_val); free(new_string); return; @@ -1240,9 +1256,9 @@ stackitem *old_item, *new_item, *prev_item; value *new_value; - protect(env, old_value); + protect(old_value); new_value= new_val(env); - protect(env, new_value); + protect(new_value); new_value->type= old_value->type; switch(old_value->type){ @@ -1277,7 +1293,7 @@ break; } - unprotect(env); unprotect(env); + unprotect(old_value); unprotect(new_value); return new_value; } @@ -1373,11 +1389,11 @@ } loop= env->head->item; - protect(env, loop); + protect(loop); toss(env); if(env->err) return; test= env->head->item; - protect(env, test); + protect(test); toss(env); if(env->err) return; do { @@ -1402,7 +1418,7 @@ } while(truth); - unprotect(env); unprotect(env); + unprotect(loop); unprotect(test); } @@ -1427,7 +1443,7 @@ } loop= env->head->item; - protect(env, loop); + protect(loop); toss(env); if(env->err) return; foo2= env->head->item->content.i; @@ -1451,7 +1467,7 @@ foo1--; } } - unprotect(env); + unprotect(loop); } /* Variant of for-loop */ @@ -1473,11 +1489,11 @@ } loop= env->head->item; - protect(env, loop); + protect(loop); toss(env); if(env->err) return; foo= env->head->item; - protect(env, foo); + protect(foo); toss(env); if(env->err) return; iterator= foo->content.ptr; @@ -1488,7 +1504,7 @@ eval(env); if(env->err) return; iterator= iterator->next; } - unprotect(env); unprotect(env); + unprotect(loop); unprotect(foo); } /* "to" */ @@ -1528,7 +1544,7 @@ iterator= env->head; pack= new_val(env); - protect(env, pack); + protect(pack); if(iterator==NULL || (iterator->item->type==symb @@ -1558,7 +1574,7 @@ push_val(env, pack); - unprotect(env); + unprotect(pack); } /* Read a string */