--- stack/stack.c 2002/03/10 07:55:13 1.96 +++ stack/stack.c 2002/03/10 10:06:36 1.99 @@ -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_ref= nitem; env->gc_count++; + nval->gc.flag.mark= 0; + nval->gc.flag.protect= 0; return nval; } @@ -137,10 +138,10 @@ { stackitem *iterator; - if(val==NULL || val->gc_garb==0) + if(val->gc.flag.mark) return; - val->gc_garb= 0; + val->gc.flag.mark= 1; if(val->type==list) { iterator= val->content.ptr; @@ -167,20 +168,6 @@ symbol *tsymb; int i; - /* Garb by default */ - iterator= env->gc_ref; - while(iterator!=NULL) { - iterator->item->gc_garb= 1; - iterator= iterator->next; - } - - /* Mark protected values */ - iterator= env->gc_protect; - while(iterator!=NULL) { - gc_mark(iterator->item); - iterator= iterator->next; - } - /* Mark values on stack */ iterator= env->head; while(iterator!=NULL) { @@ -192,7 +179,8 @@ for(i= 0; isymbols[i]; while(tsymb!=NULL) { - gc_mark(tsymb->val); + if (tsymb->val != NULL) + gc_mark(tsymb->val); tsymb= tsymb->next; } } @@ -201,7 +189,7 @@ while(env->gc_ref!=NULL) { /* Sweep unused values */ - if(env->gc_ref->item->gc_garb) { + if(!(env->gc_ref->item->gc.no_gc)){ /* neither mark nor protect */ switch(env->gc_ref->item->type) { /* Remove content */ case string: @@ -213,21 +201,22 @@ env->gc_ref->item->content.ptr= titem->next; free(titem); } - break; default: - break; } free(env->gc_ref->item); /* Remove from gc_ref */ titem= env->gc_ref->next; free(env->gc_ref); /* Remove value */ env->gc_ref= titem; - } else { /* Keep values */ - titem= env->gc_ref->next; - env->gc_ref->next= new_head; - new_head= env->gc_ref; - env->gc_ref= titem; - env->gc_count++; + continue; } + + /* Keep values */ + titem= env->gc_ref->next; + env->gc_ref->next= new_head; + new_head= env->gc_ref; + new_head->item->gc.flag.mark= 0; + env->gc_ref= titem; + env->gc_count++; } env->gc_limit= env->gc_count*2; @@ -235,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.flag.protect) + return; + + val->gc.flag.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.flag.protect)) + return; + + val->gc.flag.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 */ @@ -354,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; @@ -402,7 +414,7 @@ } push_val(env, new_value); - unprotect(env); unprotect(env); + unprotect(new_value); unprotect(new_fvalue); } /* Print newline. */ @@ -595,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 @@ -641,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; @@ -662,7 +674,7 @@ } iterator= iterator->next; } - unprotect(env); + unprotect(temp_val); return; default: @@ -706,7 +718,7 @@ iterator= env->head; pack= new_val(env); - protect(env, pack); + protect(pack); if(iterator==NULL || (iterator->item->type==symb @@ -737,7 +749,7 @@ push_val(env, pack); rev(env); - unprotect(env); + unprotect(pack); } /* Relocate elements of the list on the stack. */ @@ -982,7 +994,7 @@ } if(myenv.interactive) { - printf("Stack version $Revision: 1.96 $\n\ + printf("Stack version $Revision: 1.99 $\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\ @@ -1035,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; @@ -1043,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; @@ -1244,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){ @@ -1281,7 +1293,7 @@ break; } - unprotect(env); unprotect(env); + unprotect(old_value); unprotect(new_value); return new_value; } @@ -1377,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 { @@ -1406,7 +1418,7 @@ } while(truth); - unprotect(env); unprotect(env); + unprotect(loop); unprotect(test); } @@ -1431,7 +1443,7 @@ } loop= env->head->item; - protect(env, loop); + protect(loop); toss(env); if(env->err) return; foo2= env->head->item->content.i; @@ -1455,7 +1467,7 @@ foo1--; } } - unprotect(env); + unprotect(loop); } /* Variant of for-loop */ @@ -1477,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; @@ -1492,7 +1504,7 @@ eval(env); if(env->err) return; iterator= iterator->next; } - unprotect(env); unprotect(env); + unprotect(loop); unprotect(foo); } /* "to" */ @@ -1532,7 +1544,7 @@ iterator= env->head; pack= new_val(env); - protect(env, pack); + protect(pack); if(iterator==NULL || (iterator->item->type==symb @@ -1562,7 +1574,7 @@ push_val(env, pack); - unprotect(env); + unprotect(pack); } /* Read a string */