--- stack/stack.c 2002/03/10 07:55:13 1.96 +++ stack/stack.c 2002/03/10 12:05:20 1.100 @@ -48,10 +48,9 @@ { int i; - env->gc_limit= 200; + env->gc_limit= 400000; env->gc_count= 0; env->gc_ref= NULL; - env->gc_protect= NULL; env->head= NULL; for(i= 0; ihead= env->head->next; /* Remove the top stack item */ free(temp); /* Free the old top stack item */ - - env->gc_limit--; } /* Returns a pointer to a pointer to an element in the hash table. */ @@ -126,7 +123,9 @@ nitem->next= env->gc_ref; env->gc_ref= nitem; - env->gc_count++; + env->gc_count+=sizeof(value); + nval->gc.flag.mark= 0; + nval->gc.flag.protect= 0; return nval; } @@ -137,10 +136,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,18 +166,8 @@ 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; + if(env->interactive){ + printf("Garbage collecting.", env->gc_count, env->gc_limit); } /* Mark values on stack */ @@ -188,20 +177,29 @@ iterator= iterator->next; } + if(env->interactive){ + printf("."); + } + /* Mark values in hashtable */ for(i= 0; isymbols[i]; while(tsymb!=NULL) { - gc_mark(tsymb->val); + if (tsymb->val != NULL) + gc_mark(tsymb->val); tsymb= tsymb->next; } } + if(env->interactive){ + printf("."); + } + env->gc_count= 0; 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,42 +211,73 @@ 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; + } else { + env->gc_count += sizeof(value); } + + /* 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_limit= env->gc_count*2; + if (env->gc_limit < env->gc_count*2) + env->gc_limit= env->gc_count*2; env->gc_ref= new_head; + + if(env->interactive){ + printf("done\n"); + } + } /* 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 +383,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 +431,7 @@ } push_val(env, new_value); - unprotect(env); unprotect(env); + unprotect(new_value); unprotect(new_fvalue); } /* Print newline. */ @@ -595,11 +624,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 +670,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; @@ -650,7 +679,7 @@ push_val(env, iterator->item); if(env->head->item->type==symb - && strcmp(";", ((symbol*)(env->head->item->content.ptr))->id)==0) { + && (((symbol*)(env->head->item->content.ptr))->id[0] == ';')) { toss(env); if(env->err) return; @@ -662,7 +691,7 @@ } iterator= iterator->next; } - unprotect(env); + unprotect(temp_val); return; default: @@ -706,7 +735,7 @@ iterator= env->head; pack= new_val(env); - protect(env, pack); + protect(pack); if(iterator==NULL || (iterator->item->type==symb @@ -737,7 +766,7 @@ push_val(env, pack); rev(env); - unprotect(env); + unprotect(pack); } /* Relocate elements of the list on the stack. */ @@ -982,7 +1011,7 @@ } if(myenv.interactive) { - printf("Stack version $Revision: 1.96 $\n\ + printf("Stack version $Revision: 1.100 $\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 +1064,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 +1072,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 +1273,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 +1310,7 @@ break; } - unprotect(env); unprotect(env); + unprotect(old_value); unprotect(new_value); return new_value; } @@ -1377,11 +1406,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 +1435,7 @@ } while(truth); - unprotect(env); unprotect(env); + unprotect(loop); unprotect(test); } @@ -1431,7 +1460,7 @@ } loop= env->head->item; - protect(env, loop); + protect(loop); toss(env); if(env->err) return; foo2= env->head->item->content.i; @@ -1455,7 +1484,7 @@ foo1--; } } - unprotect(env); + unprotect(loop); } /* Variant of for-loop */ @@ -1477,11 +1506,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 +1521,7 @@ eval(env); if(env->err) return; iterator= iterator->next; } - unprotect(env); unprotect(env); + unprotect(loop); unprotect(foo); } /* "to" */ @@ -1532,7 +1561,7 @@ iterator= env->head; pack= new_val(env); - protect(env, pack); + protect(pack); if(iterator==NULL || (iterator->item->type==symb @@ -1562,7 +1591,7 @@ push_val(env, pack); - unprotect(env); + unprotect(pack); } /* Read a string */