From 1e012587242537230b5917acf0b01efba5612490 Mon Sep 17 00:00:00 2001 From: Alex Shinn Date: Mon, 31 Oct 2011 00:10:56 +0900 Subject: [PATCH] fixing bytecode offsets in images, allowing the image heap size and init heap size to differ --- gc.c | 37 +++++++++++++++++-- include/chibi/eval.h | 87 -------------------------------------------- include/chibi/sexp.h | 87 ++++++++++++++++++++++++++++++++++++++++++++ main.c | 50 ++++++++++++++++++------- 4 files changed, 157 insertions(+), 104 deletions(-) diff --git a/gc.c b/gc.c index 184b79db..7e0687f9 100644 --- a/gc.c +++ b/gc.c @@ -384,8 +384,8 @@ sexp sexp_gc (sexp ctx, size_t *sum_freed) { sexp_conservative_mark(ctx); sexp_reset_weak_references(ctx); res = sexp_sweep(ctx, sum_freed); - sexp_debug_printf("%p (freed: %lu max_freed: %lu)", ctx, *sum_freed, - sexp_unbox_fixnum(res)); + sexp_debug_printf("%p (freed: %lu max_freed: %lu)", ctx, + (sum_freed ? *sum_freed : 0), sexp_unbox_fixnum(res)); return res; } @@ -519,10 +519,34 @@ void sexp_offset_heap_pointers (sexp_heap heap, sexp_heap from_heap, sexp* types /* adjust context heaps, don't copy saved sexp_gc_vars */ if (sexp_contextp(p)) { sexp_context_ip(p) += off; + sexp_context_last_fp(p) += off; + sexp_stack_top(sexp_context_stack(p)) = 0; sexp_context_saves(p) = NULL; - /* if (sexp_context_heap(p) - off != from_heap) */ - /* fprintf(stderr, "unexpected heap: %p\n", sexp_context_heap(p)); */ sexp_context_heap(p) = heap; + } else if (sexp_bytecodep(p)) { + for (i=0; isize < heap_size) { + for (q=(sexp_free_list)((char*)heap->free_list + offset); q->next; + q=(sexp_free_list)((char*)q->next + offset)) + ; + if ((char*)q + q->size >= (char*)heap->data + heap->size) { + /* last free chunk at end of heap */ + q->size += heap_size - heap->size; + } else { + /* last free chunk in the middle of the heap */ + q->next = (sexp_free_list)((char*)heap->data + heap->size); + q = (sexp_free_list)((char*)q->next + offset); + q->size = heap_size - heap->size; + q->next = NULL; + } + heap->size += (heap_size - heap->size); + } ctx = (sexp)(header.context + offset); globals = sexp_vector_data((sexp)((char*)sexp_context_globals(ctx) + offset)); types = sexp_vector_data((sexp)((char*)(globals[SEXP_G_TYPES]) + offset)); - sexp_offset_heap_pointers((sexp_heap)image, (sexp_heap)header.base, types, sexp_fx_add(SEXP_COPY_LOADP, SEXP_COPY_FREEP)); + flags = sexp_fx_add(SEXP_COPY_LOADP, SEXP_COPY_FREEP); + sexp_offset_heap_pointers(heap, header.base, types, flags); close(fd); return ctx; } @@ -77,6 +100,7 @@ static int sexp_save_image (sexp ctx, const char* path) { sexp_heap heap; FILE* file; struct sexp_image_header_t header; + sexp_free_list q; file = fopen(path, "w"); if (!file) { fprintf(stderr, "couldn't open image file for writing: %s\n", path); @@ -87,8 +111,8 @@ static int sexp_save_image (sexp ctx, const char* path) { header.major = SEXP_IMAGE_MAJOR_VERSION; header.minor = SEXP_IMAGE_MINOR_VERSION; header.size = heap->size; - header.base = (sexp_uint_t)heap; - header.context = (sexp_uint_t)ctx; + header.base = heap; + header.context = ctx; sexp_gc(ctx, NULL); if (! (fwrite(&header, sizeof(header), 1, file) == 1 && fwrite(heap, heap->size, 1, file) == 1)) { @@ -301,7 +325,7 @@ void run_main (int argc, char **argv) { fprintf(stderr, "-:i : image files must be loaded first\n"); exit_failure(); } - ctx = sexp_load_image(arg); + ctx = sexp_load_image(arg, heap_size, heap_max_size); if (!ctx) { fprintf(stderr, "-:i : couldn't open file for reading: %s\n", arg); exit_failure();