From 287f6608519a7a986ef17898a26daddb4b7d3e3a Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Thu, 5 Nov 2015 22:17:42 -0500 Subject: [PATCH] Moving stack limits away from global vars --- gc.c | 19 +++++++++++++++++-- include/cyclone/runtime-main.h | 2 +- include/cyclone/types.h | 16 +++++++--------- runtime.c | 4 ++-- 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/gc.c b/gc.c index 682b06c7..1f9ccebe 100644 --- a/gc.c +++ b/gc.c @@ -657,9 +657,24 @@ void gc_empty_collector_stack() ///////////////////////////////////////////// -// Initialize a thread from scratch -void gc_thread_data_init(gc_thread_data *thd) +// Initialize runtime data structures for a thread. +// Must be called on the target thread itself during startup, +// to verify stack limits are setup correctly. +void gc_thread_data_init(gc_thread_data *thd, char *stack_base, long stack_size) { + char stack_ref; + thd->stack_start = stack_base; +#if STACK_GROWS_DOWNWARD + thd->stack_limit = stack_base - stack_size; +#else + thd->stack_limit = stack_base + stack_size; +#endif + if (check_overflow(stack_base, &stack_ref)){ + fprintf(stderr, + "Error: recompile with STACK_GROWS_DOWNWARD set to %d\n", + (1 - STACK_GROWS_DOWNWARD)); + exit(1); + } thd->moveBufLen = 0; gc_thr_grow_move_buffer(thd); // TODO: depends on collector state: thd->gc_alloc_color = ATOMIC_GET(&gc_; diff --git a/include/cyclone/runtime-main.h b/include/cyclone/runtime-main.h index e189f067..da75c5b4 100644 --- a/include/cyclone/runtime-main.h +++ b/include/cyclone/runtime-main.h @@ -60,7 +60,7 @@ static void Cyc_main (stack_size,heap_size,stack_base) Cyc_heap = gc_heap_create(heap_size / 2, 0, 0); //Cyc_heap = gc_heap_create(1024, 0, 0); Cyc_thread = malloc(sizeof(gc_thread_data)); - gc_thread_data_init(Cyc_thread); + gc_thread_data_init(Cyc_thread, stack_base, stack_size); // JAE TODO: clean up below (and all of this old code, really) diff --git a/include/cyclone/types.h b/include/cyclone/types.h index 9a38d798..88556bdd 100644 --- a/include/cyclone/types.h +++ b/include/cyclone/types.h @@ -22,17 +22,15 @@ typedef void *object; /* Thread data structures */ -typedef struct gc_thread_stack_t gc_thread_stack; -struct gc_thread_stack { - char *begin; - // TODO: move moveBuf stuff over here? -}; - typedef struct gc_thread_data_t gc_thread_data; struct gc_thread_data_t { - void **moveBuf; /* list of objects moved to heap during GC */ + // Data needed for stack-based minor GC + char *stack_start; + char *stack_limit; + // List of objects moved to heap during minor GC + void **moveBuf; int moveBufLen; - // Data needed for tri-color marking + // Data needed for heap GC int gc_alloc_color; int gc_mut_status; int last_write; @@ -118,7 +116,7 @@ size_t gc_sweep(gc_heap *h, size_t *sum_freed_ptr); size_t gc_collect(gc_heap *h, size_t *sum_freed); void gc_thr_grow_move_buffer(gc_thread_data *d); void gc_thr_add_to_move_buffer(gc_thread_data *d, int *alloci, object obj); -void gc_thread_data_init(gc_thread_data *thd); +void gc_thread_data_init(gc_thread_data *thd, char *stack_base, long stack_size); void gc_thread_data_free(gc_thread_data *thd); // Prototypes for mutator/collector: void gc_mark_gray(gc_thread_data *thd, object obj); diff --git a/runtime.c b/runtime.c index 9c2fea1a..7b815a40 100644 --- a/runtime.c +++ b/runtime.c @@ -2594,7 +2594,7 @@ char *gc_move(char *obj, gc_thread_data *thd, int *alloci, int *heap_grown) { temp = obj; \ if (check_overflow(low_limit, temp) && \ check_overflow(temp, high_limit)){ \ - (obj) = (object) gc_move(temp, Cyc_thread, &alloci, &heap_grown); \ + (obj) = (object) gc_move(temp, (gc_thread_data *)data, &alloci, &heap_grown); \ } \ } @@ -2660,7 +2660,7 @@ void GC(void *data, closure cont, object *args, int num_args) // Check allocated objects, moving additional objects as needed while (scani < alloci) { - object obj = Cyc_thread->moveBuf[scani]; + object obj = ((gc_thread_data *)data)->moveBuf[scani]; switch(type_of(obj)) { case cons_tag: { gc_move2heap(car(obj));