Skip to content

Commit

Permalink
Merge pull request #1968 from Chobbes/heap_fixes
Browse files Browse the repository at this point in the history
Generalized alloc_heap so it could be reused for garbage collection.
  • Loading branch information
edwinb committed Mar 19, 2015
2 parents ab3b3db + 8f9ac37 commit e5b50de
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 19 deletions.
17 changes: 3 additions & 14 deletions rts/idris_gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,8 @@ void idris_gc(VM* vm) {
if (vm->heap.old != NULL)
free(vm->heap.old);

char* newheap = malloc(vm->heap.size);
char* oldheap = vm->heap.heap;

vm->heap.heap = newheap;
#ifdef FORCE_ALIGNMENT
if (((i_int)(vm->heap.heap)&1) == 1) {
vm->heap.next = newheap + 1;
} else
#endif
{
vm->heap.next = newheap;
}
vm->heap.end = newheap + vm->heap.size;
/* Allocate swap heap. */
alloc_heap(&vm->heap, vm->heap.size, vm->heap.growth, vm->heap.heap);

VAL* root;

Expand All @@ -139,6 +128,7 @@ void idris_gc(VM* vm) {
msg->msg = copy(vm, msg->msg);
}
#endif

vm->ret = copy(vm, vm->ret);
vm->reg1 = copy(vm, vm->reg1);

Expand All @@ -150,7 +140,6 @@ void idris_gc(VM* vm) {
if ((vm->heap.next - vm->heap.heap) > vm->heap.size >> 1) {
vm->heap.size += vm->heap.growth;
}
vm->heap.old = oldheap;

STATS_LEAVE_GC(vm->stats, vm->heap.size, vm->heap.next - vm->heap.heap)
HEAP_CHECK(vm)
Expand Down
9 changes: 6 additions & 3 deletions rts/idris_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include <stddef.h>
#include <stdio.h>

void alloc_heap(Heap * h, size_t heap_size)

/* Used for initializing the heap. */
void alloc_heap(Heap * h, size_t heap_size, size_t growth, char * old)
{
char * mem = malloc(heap_size);
if (mem == NULL) {
Expand All @@ -27,9 +29,9 @@ void alloc_heap(Heap * h, size_t heap_size)
h->end = h->heap + heap_size;

h->size = heap_size;
h->growth = heap_size;
h->growth = growth;

h->old = NULL;
h->old = old;
}

void free_heap(Heap * h) {
Expand All @@ -40,6 +42,7 @@ void free_heap(Heap * h) {
}
}


// TODO: more testing
/******************** Heap testing ********************************************/
void heap_check_underflow(Heap * heap) {
Expand Down
2 changes: 1 addition & 1 deletion rts/idris_heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ typedef struct {
} Heap;


void alloc_heap(Heap * heap, size_t heap_size);
void alloc_heap(Heap * heap, size_t heap_size, size_t growth, char * old);
void free_heap(Heap * heap);


Expand Down
2 changes: 1 addition & 1 deletion rts/idris_rts.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ VM* init_vm(int stack_size, size_t heap_size,
vm->valstack_base = valstack;
vm->stack_max = valstack + stack_size;

alloc_heap(&(vm->heap), heap_size);
alloc_heap(&(vm->heap), heap_size, heap_size, NULL);

vm->ret = NULL;
vm->reg1 = NULL;
Expand Down

0 comments on commit e5b50de

Please sign in to comment.