diff --git a/TODO.FACTOR.txt b/TODO.FACTOR.txt index 9e592052ae..fa1d246337 100644 --- a/TODO.FACTOR.txt +++ b/TODO.FACTOR.txt @@ -2,9 +2,7 @@ - inline float allocation needs a gc check - docs: don't pass volatile aliens to callbacks -- don't save big free chunk at the end of the code heap - some instability remains -- overhaul alien docs + ui: diff --git a/vm/code_gc.c b/vm/code_gc.c index 8dae12ffde..be3b3b3335 100644 --- a/vm/code_gc.c +++ b/vm/code_gc.c @@ -118,7 +118,7 @@ CELL heap_allot(F_HEAP *heap, CELL size) return (CELL)(scan + 1); } - return 0; /* can't happen */ + return 0; } /* After code GC, all referenced code blocks have status set to B_MARKED, so any @@ -173,13 +173,20 @@ CELL heap_free_space(F_HEAP *heap) return size; } +/* The size of the heap, not including the last block if it's free */ CELL heap_size(F_HEAP *heap) { - CELL start = heap->base; - F_BLOCK *scan = (F_BLOCK *)start; - while(next_block(heap,scan)) + F_BLOCK *scan = (F_BLOCK *)heap->base; + + while(next_block(heap,scan) != NULL) scan = next_block(heap,scan); - return (CELL)scan - (CELL)start; + + /* this is the last block in the heap, and it is free */ + if(scan->status == B_FREE) + return (CELL)scan - heap->base; + /* otherwise the last block is allocated */ + else + return heap->limit - heap->base; } /* Apply a function to every code block */ diff --git a/vm/image.c b/vm/image.c index 444d71b3d9..00b0deb100 100644 --- a/vm/image.c +++ b/vm/image.c @@ -103,6 +103,7 @@ bool save_image(const char* filename) h.bignum_zero = bignum_zero; h.bignum_pos_one = bignum_pos_one; h.bignum_neg_one = bignum_neg_one; + h.code_size = heap_size(&compiling); h.code_relocation_base = compiling.base; fwrite(&h,sizeof(F_HEADER),1,file);