--- stack/stack.c 2002/03/10 06:34:01 1.95 +++ stack/stack.c 2002/03/10 13:00:01 1.101 @@ -48,10 +48,9 @@ { int i; - env->gc_limit= 20; + 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--; - gc_init(env); } /* Returns a pointer to a pointer to an element in the hash table. */ @@ -127,25 +123,23 @@ nitem->next= env->gc_ref; env->gc_ref= nitem; - env->gc_count++; - - protect(env, nval); - gc_init(env); - unprotect(env); + env->gc_count += sizeof(value); + nval->gc.flag.mark= 0; + nval->gc.flag.protect= 0; return nval; } /* Mark values recursively. Marked values are not collected by the GC. */ -void gc_mark(value *val) +inline void gc_mark(value *val) { 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; @@ -157,6 +151,14 @@ } } +inline void gc_maybe(environment *env) +{ + if(env->gc_count < env->gc_limit) + return; + else + return gc_init(env); +} + /* Start GC */ extern void gc_init(environment *env) { @@ -164,21 +166,8 @@ symbol *tsymb; int i; - 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; - } - - /* Mark protected values */ - iterator= env->gc_protect; - while(iterator!=NULL) { - gc_mark(iterator->item); - iterator= iterator->next; + if(env->interactive){ + printf("Garbage collecting."); } /* 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,76 @@ 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); + if(env->gc_ref->item->type == string) + env->gc_count += strlen(env->gc_ref->item->content.ptr); } + + /* 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 */ @@ -286,8 +318,10 @@ void push_cstring(environment *env, const char *in_string) { value *new_value= new_val(env); + int length= strlen(in_string)+1; - new_value->content.ptr= malloc(strlen(in_string)+1); + new_value->content.ptr= malloc(length); + env->gc_count += length; strcpy(new_value->content.ptr, in_string); new_value->type= string; @@ -354,9 +388,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 +436,7 @@ } push_val(env, new_value); - unprotect(env); unprotect(env); + unprotect(new_value); unprotect(new_fvalue); } /* Print newline. */ @@ -595,11 +629,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 @@ -613,6 +647,8 @@ eval_start: + gc_maybe(env); + if(env->head==NULL) { printerr("Too Few Arguments"); env->err=1; @@ -639,7 +675,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; @@ -648,7 +684,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; @@ -660,7 +696,7 @@ } iterator= iterator->next; } - unprotect(env); + unprotect(temp_val); return; default: @@ -704,7 +740,7 @@ iterator= env->head; pack= new_val(env); - protect(env, pack); + protect(pack); if(iterator==NULL || (iterator->item->type==symb @@ -735,7 +771,7 @@ push_val(env, pack); rev(env); - unprotect(env); + unprotect(pack); } /* Relocate elements of the list on the stack. */ @@ -869,7 +905,7 @@ } env->gc_limit= 0; - gc_init(env); + gc_maybe(env); if(env->free_string!=NULL) free(env->free_string); @@ -980,7 +1016,7 @@ } if(myenv.interactive) { - printf("Stack version $Revision: 1.95 $\n\ + printf("Stack version $Revision: 1.101 $\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\ @@ -1008,7 +1044,7 @@ toss(&myenv); /* No error check in main */ eval(&myenv); } - gc_init(&myenv); + gc_maybe(&myenv); } quit(&myenv); return EXIT_FAILURE; @@ -1033,7 +1069,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; @@ -1041,7 +1077,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; @@ -1242,9 +1278,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){ @@ -1279,7 +1315,7 @@ break; } - unprotect(env); unprotect(env); + unprotect(old_value); unprotect(new_value); return new_value; } @@ -1375,11 +1411,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 { @@ -1404,7 +1440,7 @@ } while(truth); - unprotect(env); unprotect(env); + unprotect(loop); unprotect(test); } @@ -1429,7 +1465,7 @@ } loop= env->head->item; - protect(env, loop); + protect(loop); toss(env); if(env->err) return; foo2= env->head->item->content.i; @@ -1453,7 +1489,7 @@ foo1--; } } - unprotect(env); + unprotect(loop); } /* Variant of for-loop */ @@ -1475,11 +1511,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; @@ -1490,7 +1526,7 @@ eval(env); if(env->err) return; iterator= iterator->next; } - unprotect(env); unprotect(env); + unprotect(loop); unprotect(foo); } /* "to" */ @@ -1530,7 +1566,7 @@ iterator= env->head; pack= new_val(env); - protect(env, pack); + protect(pack); if(iterator==NULL || (iterator->item->type==symb @@ -1560,7 +1596,7 @@ push_val(env, pack); - unprotect(env); + unprotect(pack); } /* Read a string */