This commit is contained in:
Justin Ethier 2015-10-22 22:51:20 -04:00
parent 25303fc4a5
commit a9c0411a16
3 changed files with 21 additions and 12 deletions

23
gc.c
View file

@ -33,8 +33,8 @@ gc_heap *gc_heap_create(size_t size, size_t max_size, size_t chunk_size)
next->size = size - gc_heap_align(gc_free_chunk_size); next->size = size - gc_heap_align(gc_free_chunk_size);
next->next = NULL; next->next = NULL;
#if GC_DEBUG_PRINTFS #if GC_DEBUG_PRINTFS
fprintf(stderr, ("heap: %p-%p data: %p-%p\n"), fprintf(stderr, ("heap: %p-%p data: %p-%p size: %d\n"),
h, ((char*)h)+gc_heap_pad_size(size), h->data, h->data + size); h, ((char*)h)+gc_heap_pad_size(size), h->data, h->data + size, size);
fprintf(stderr, ("first: %p end: %p\n"), fprintf(stderr, ("first: %p end: %p\n"),
(object)gc_heap_first_block(h), (object)gc_heap_end(h)); (object)gc_heap_first_block(h), (object)gc_heap_end(h));
fprintf(stderr, ("free1: %p-%p free2: %p-%p\n"), fprintf(stderr, ("free1: %p-%p free2: %p-%p\n"),
@ -49,8 +49,8 @@ int gc_grow_heap(gc_heap *h, size_t size, size_t chunk_size)
gc_heap *h_last = gc_heap_last(h); gc_heap *h_last = gc_heap_last(h);
cur_size = h_last->size; cur_size = h_last->size;
new_size = gc_heap_align(((cur_size > size) ? cur_size : size) * 2); new_size = gc_heap_align(((cur_size > size) ? cur_size : size) * 2);
h->next = gc_heap_create(new_size, h->max_size, chunk_size); h_last->next = gc_heap_create(new_size, h_last->max_size, chunk_size);
return (h->next != NULL); return (h_last->next != NULL);
} }
void *gc_try_alloc(gc_heap *h, size_t size) void *gc_try_alloc(gc_heap *h, size_t size)
@ -142,9 +142,10 @@ size_t gc_allocated_bytes(object obj)
if (t == port_tag) return gc_heap_align(sizeof(port_type)); if (t == port_tag) return gc_heap_align(sizeof(port_type));
if (t == cvar_tag) return gc_heap_align(sizeof(cvar_type)); if (t == cvar_tag) return gc_heap_align(sizeof(cvar_type));
#if GC_DEBUG_PRINTFS //#if GC_DEBUG_PRINTFS
fprintf(stderr, "gc_allocated_bytes: unexpected object %p of type %ld\n", obj, t); fprintf(stderr, "gc_allocated_bytes: unexpected object %p of type %ld\n", obj, t);
#endif exit(1);
//#endif
return 0; return 0;
} }
@ -214,6 +215,7 @@ size_t gc_sweep(gc_heap *h, size_t *sum_freed_ptr)
object p, end; object p, end;
gc_free_list *q, *r, *s; gc_free_list *q, *r, *s;
for (; h; h = h->next) { // All heaps for (; h; h = h->next) { // All heaps
fprintf(stdout, "sweep heap %p, size = %d\n", h, h->size);
p = gc_heap_first_block(h); p = gc_heap_first_block(h);
q = h->free_list; q = h->free_list;
end = gc_heap_end(h); end = gc_heap_end(h);
@ -223,9 +225,11 @@ size_t gc_sweep(gc_heap *h, size_t *sum_freed_ptr)
if ((char *)r == (char *)p) { // this is a free block, skip it if ((char *)r == (char *)p) { // this is a free block, skip it
p = (object) (((char *)p) + r->size); p = (object) (((char *)p) + r->size);
fprintf(stdout, "skip free block %p size = %d\n", p, r->size);
continue; continue;
} }
size = gc_heap_align(gc_allocated_bytes(p)); size = gc_heap_align(gc_allocated_bytes(p));
//fprintf(stdout, "check object %p, size = %d\n", p, size);
//#if GC_DEBUG_PRINTFS //#if GC_DEBUG_PRINTFS
// DEBUG // DEBUG
@ -240,7 +244,7 @@ size_t gc_sweep(gc_heap *h, size_t *sum_freed_ptr)
if (!mark(p)) { if (!mark(p)) {
#if GC_DEBUG_PRINTFS #if GC_DEBUG_PRINTFS
// fprintf(stdout, "sweep: object is not marked %p\n", p); fprintf(stdout, "sweep: object is not marked %p\n", p);
#endif #endif
// free p // free p
sum_freed += size; sum_freed += size;
@ -278,6 +282,11 @@ size_t gc_sweep(gc_heap *h, size_t *sum_freed_ptr)
#if GC_DEBUG_PRINTFS #if GC_DEBUG_PRINTFS
// fprintf(stdout, "sweep: object is marked %p\n", p); // fprintf(stdout, "sweep: object is marked %p\n", p);
#endif #endif
//if (mark(p) != 1) {
// printf("unexpected mark value %d\n", mark(p));
// exit(1);
//}
((list)p)->hdr.mark = 0; ((list)p)->hdr.mark = 0;
p = (object)(((char *)p) + size); p = (object)(((char *)p) + size);
} }

View file

@ -57,8 +57,8 @@ static void Cyc_main (stack_size,heap_size,stack_base)
printf("main: Allocating and initializing heap...\n"); printf("main: Allocating and initializing heap...\n");
#endif #endif
//Cyc_heap = gc_heap_create(heap_size / 2, 0, 0); Cyc_heap = gc_heap_create(heap_size / 2, 0, 0);
Cyc_heap = gc_heap_create(1024, 0, 0); //Cyc_heap = gc_heap_create(1024, 0, 0);
Cyc_thread = (gc_thread_data *)malloc(sizeof(gc_thread_data)); Cyc_thread = (gc_thread_data *)malloc(sizeof(gc_thread_data));
Cyc_thread->moveBufLen = 0; Cyc_thread->moveBufLen = 0;
gc_thr_grow_move_buffer(Cyc_thread); // Initialize the buffer gc_thr_grow_move_buffer(Cyc_thread); // Initialize the buffer

View file

@ -9,7 +9,7 @@
#include "cyclone/types.h" #include "cyclone/types.h"
#include "cyclone/runtime.h" #include "cyclone/runtime.h"
int JAE_DEBUG = 0; //int JAE_DEBUG = 0;
/* Error checking section - type mismatch, num args, etc */ /* Error checking section - type mismatch, num args, etc */
/* Type names to use for error messages */ /* Type names to use for error messages */
@ -2718,8 +2718,8 @@ fprintf(stdout, "DEBUG, starting major mark/sweep GC\n"); // JAE DEBUG
} }
max_freed = gc_collect(Cyc_heap, &freed); max_freed = gc_collect(Cyc_heap, &freed);
printf("done, freed = %d, max_freed = %d\n", freed, max_freed); printf("done, freed = %d, max_freed = %d\n", freed, max_freed);
JAE_DEBUG++; //JAE_DEBUG++;
if (JAE_DEBUG == 2) exit(1); // JAE DEBUG //if (JAE_DEBUG == 2) exit(1); // JAE DEBUG
} }
//fprintf(stdout, "DEBUG, finished minor GC\n"); // JAE DEBUG //fprintf(stdout, "DEBUG, finished minor GC\n"); // JAE DEBUG