@@ -10,6 +10,7 @@
1010#include "idris_gc.h"
1111#include "idris_bitstring.h"
1212
13+
1314VM* init_vm(int stack_size, size_t heap_size,
1415 int max_threads, // not implemented yet
1516 int argc, char* argv[]) {
@@ -26,14 +27,12 @@ VM* init_vm(int stack_size, size_t heap_size,
2627 vm->floatstack = floatstack;
2728 vm->floatstack_ptr = floatstack;
2829 vm->stack_max = valstack + stack_size;
29− vm->heap = malloc(heap_size);
30− vm->oldheap = NULL;
31− vm->heap_next = vm->heap;
32− vm->heap_end = vm->heap + heap_size;
33− vm->heap_size = heap_size;
30+
31+ alloc_heap(&(vm->heap), heap_size);
32+
3433 vm->collections = 0;
3534 vm->allocations = 0;
36− vm->heap_growth = heap_size;
35+
3736 vm->ret = NULL;
3837 vm->reg1 = NULL;
3938
@@ -68,9 +67,11 @@ void terminate(VM* vm) {
6867 free(vm->valstack);
6968 free(vm->intstack);
7069 free(vm->floatstack);
71− free(vm->heap);
70+
71+ free_heap(&(vm->heap));
72+
7273 free(vm->argv);
73− if (vm->oldheap != NULL) { free(vm->oldheap); }
74+
7475 pthread_mutex_destroy(&(vm -> inbox_lock));
7576 pthread_mutex_destroy(&(vm -> inbox_block));
7677 pthread_cond_destroy(&(vm -> inbox_waiting));
@@ -78,7 +79,7 @@ void terminate(VM* vm) {
7879}
7980
8081void idris_requireAlloc(VM* vm, size_t size) {
81− if (!(vm -> heap_next + size < vm -> heap_end)) {
82+ if (!(vm->heap.next + size < vm->heap.end)) {
8283 idris_gc(vm);
8384 }
8485
@@ -106,11 +107,11 @@ void* allocate(VM* vm, size_t size, int outerlock) {
106107 if ((size & 7)!=0) {
107108 size = 8 + ((size >> 3) << 3);
108109 }
109− if (vm -> heap_next + size < vm -> heap_end) {
110+ if (vm -> heap.next + size < vm -> heap.end) {
110111 vm->allocations += size + sizeof(size_t);
111− void* ptr = (void*)(vm->heap_next + sizeof(size_t));
112− *((size_t*)(vm->heap_next)) = size + sizeof(size_t);
113− vm -> heap_next += size + sizeof(size_t);
112+ void* ptr = (void*)(vm->heap.next + sizeof(size_t));
113+ *((size_t*)(vm->heap.next)) = size + sizeof(size_t);
114+ vm -> heap.next += size + sizeof(size_t);
114115 memset(ptr, 0, size);
115116 if (lock) { // not message passing
116117 pthread_mutex_unlock(&vm->alloc_lock);
@@ -208,7 +209,7 @@ void dumpStack(VM* vm) {
208209 for (root = vm->valstack; root < vm->valstack_top; ++root, ++i) {
209210 printf("%d: ", i);
210211 dumpVal(*root);
211− if (*root >= (VAL)(vm->heap) && *root < (VAL)(vm->heap_end)) { printf("OK"); }
212+ if (*root >= (VAL)(vm->heap.heap) && *root < (VAL)(vm->heap.end)) { printf("OK"); }
212213 printf("\n");
213214 }
214215 printf("RET: ");
@@ -406,7 +407,7 @@ void* runThread(void* arg) {
406407}
407408
408409void* vmThread(VM* callvm, func f, VAL arg) {
409− VM* vm = init_vm(callvm->stack_max - callvm->valstack, callvm->heap_size,
410+ VM* vm = init_vm(callvm->stack_max - callvm->valstack, callvm->heap.size,
410411 callvm->max_threads,
411412 0, NULL);
412413 vm->processes=1; // since it can send and receive messages