@@ -10,6 +10,7 @@
10#include "idris_gc.h"
10#include "idris_gc.h"
11#include "idris_bitstring.h"
11#include "idris_bitstring.h"
13VM* init_vm(int stack_size, size_t heap_size,
14VM* init_vm(int stack_size, size_t heap_size,
14 int max_threads, // not implemented yet
15 int max_threads, // not implemented yet
15 int argc, char* argv[]) {
16 int argc, char* argv[]) {
@@ -26,14 +27,12 @@ VM* init_vm(int stack_size, size_t heap_size,
26 vm->floatstack = floatstack;
27 vm->floatstack = floatstack;
27 vm->floatstack_ptr = floatstack;
28 vm->floatstack_ptr = floatstack;
28 vm->stack_max = valstack + stack_size;
29 vm->stack_max = valstack + stack_size;
29− vm->heap = malloc(heap_size);
31+ alloc_heap(&(vm->heap), heap_size);
30− vm->oldheap = NULL;
32+
31− vm->heap_next = vm->heap;
32− vm->heap_end = vm->heap + heap_size;
33− vm->heap_size = heap_size;
34 vm->collections = 0;
33 vm->collections = 0;
35 vm->allocations = 0;
34 vm->allocations = 0;
36− vm->heap_growth = heap_size;
35+
37 vm->ret = NULL;
36 vm->ret = NULL;
38 vm->reg1 = NULL;
37 vm->reg1 = NULL;
@@ -68,9 +67,11 @@ void terminate(VM* vm) {
68 free(vm->valstack);
67 free(vm->valstack);
69 free(vm->intstack);
68 free(vm->intstack);
70 free(vm->floatstack);
69 free(vm->floatstack);
71− free(vm->heap);
71+ free_heap(&(vm->heap));
72 free(vm->argv);
73 free(vm->argv);
73− if (vm->oldheap != NULL) { free(vm->oldheap); }
74+
74 pthread_mutex_destroy(&(vm -> inbox_lock));
75 pthread_mutex_destroy(&(vm -> inbox_lock));
75 pthread_mutex_destroy(&(vm -> inbox_block));
76 pthread_mutex_destroy(&(vm -> inbox_block));
76 pthread_cond_destroy(&(vm -> inbox_waiting));
77 pthread_cond_destroy(&(vm -> inbox_waiting));
@@ -78,7 +79,7 @@ void terminate(VM* vm) {
80void idris_requireAlloc(VM* vm, size_t size) {
81void 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)) {
82 idris_gc(vm);
83 idris_gc(vm);
@@ -106,11 +107,11 @@ void* allocate(VM* vm, size_t size, int outerlock) {
106 if ((size & 7)!=0) {
107 if ((size & 7)!=0) {
107 size = 8 + ((size >> 3) << 3);
108 size = 8 + ((size >> 3) << 3);
109− if (vm -> heap_next + size < vm -> heap_end) {
110+ if (vm -> heap.next + size < vm -> heap.end) {
110 vm->allocations += size + sizeof(size_t);
111 vm->allocations += size + sizeof(size_t);
111− void* ptr = (void*)(vm->heap_next + sizeof(size_t));
112+ void* ptr = (void*)(vm->heap.next + sizeof(size_t));
112− *((size_t*)(vm->heap_next)) = size + sizeof(size_t);
113+ *((size_t*)(vm->heap.next)) = size + sizeof(size_t);
113− vm -> heap_next += size + sizeof(size_t);
114+ vm -> heap.next += size + sizeof(size_t);
114 memset(ptr, 0, size);
115 memset(ptr, 0, size);
115 if (lock) { // not message passing
116 if (lock) { // not message passing
116 pthread_mutex_unlock(&vm->alloc_lock);
117 pthread_mutex_unlock(&vm->alloc_lock);
@@ -208,7 +209,7 @@ void dumpStack(VM* vm) {
208 for (root = vm->valstack; root < vm->valstack_top; ++root, ++i) {
209 for (root = vm->valstack; root < vm->valstack_top; ++root, ++i) {
209 printf("%d: ", i);
210 printf("%d: ", i);
210 dumpVal(*root);
211 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"); }
212 printf("\n");
213 printf("\n");
214 printf("RET: ");
215 printf("RET: ");
@@ -406,7 +407,7 @@ void* runThread(void* arg) {
408void* vmThread(VM* callvm, func f, VAL arg) {
409void* 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,
410 callvm->max_threads,
411 callvm->max_threads,
411 0, NULL);
412 0, NULL);
412 vm->processes=1; // since it can send and receive messages
413 vm->processes=1; // since it can send and receive messages