Account for heap header when alloc memory for heap

This commit is contained in:
Justin Ethier 2015-10-22 22:23:43 -04:00
parent ee01b2679b
commit c10d7c7829
3 changed files with 14 additions and 5 deletions

16
gc.c
View file

@ -17,7 +17,7 @@ gc_heap *gc_heap_create(size_t size, size_t max_size, size_t chunk_size)
gc_free_list *free, *next;
gc_heap *h;
// TODO: mmap?
h = malloc(size);
h = malloc(gc_heap_pad_size(size));
if (!h) return NULL;
h->size = size;
h->chunk_size = chunk_size;
@ -32,6 +32,14 @@ printf("DEBUG h->data addr: %p\n", h->data);
free->next = next;
next->size = size - gc_heap_align(gc_free_chunk_size);
next->next = NULL;
//#if GC_DEBUG_PRINTFS
fprintf(stderr, ("heap: %p-%p data: %p-%p"),
h, ((char*)h)+sexp_heap_pad_size(size), h->data, h->data + size);
fprintf(stderr, ("first: %p end: %p"),
(object)sexp_heap_first_block(h), (object)sexp_heap_end(h));
fprintf(stderr, ("free1: %p-%p free2: %p-%p"),
free, ((char*)free)+free->size, next, ((char*)next)+next->size);
//#endif
return h;
}
@ -101,9 +109,9 @@ void *gc_alloc(gc_heap *h, size_t size, int *heap_grown)
exit(1); // TODO: throw error???
}
}
#if GC_DEBUG_PRINTFS
// fprintf(stdout, "alloc %p\n", result);
#endif
//#if GC_DEBUG_PRINTFS
fprintf(stdout, "alloc %p size = %d\n", result, size);
//#endif
return result;
}

View file

@ -58,6 +58,7 @@ struct gc_header_type_t {
#define gc_heap_first_block(h) ((object)(h->data + gc_heap_align(gc_free_chunk_size)))
#define gc_heap_last_block(h) ((object)((char*)h->data + h->size - gc_heap_align(gc_free_chunk_size)))
#define gc_heap_end(h) ((object)((char*)h->data + h->size))
#define gc_heap_pad_size(s) (sizeof(struct gc_heap_t) + (s) + gc_heap_align(1))
#define gc_free_chunk_size (sizeof(gc_free_list))
#define gc_align(n, bits) (((n)+(1<<(bits))-1)&(((unsigned int)-1)-((1<<(bits))-1)))

View file

@ -2716,7 +2716,7 @@ fprintf(stdout, "DEBUG, starting major mark/sweep GC\n"); // JAE DEBUG
}
max_freed = gc_collect(Cyc_heap, &freed);
printf("done, freed = %d, max_freed = %d\n", freed, max_freed);
//exit(1); // JAE DEBUG
exit(1); // JAE DEBUG
}
//fprintf(stdout, "DEBUG, finished minor GC\n"); // JAE DEBUG