@@ -0,0 +1,90 @@
4+VAL copy(VM* vm, VAL x) {
8+ if (x==NULL || ISINT(x)) {
13+ cl = allocCon(vm, x->info.c.arity);
14+ cl->info.c.tag = x->info.c.tag;
15+ cl->info.c.arity = x->info.c.arity;
17+ argptr = (VAL*)(cl->info.c.args);
18+ for(i = 0; i < x->info.c.arity; ++i) {
19+// *argptr = copy(vm, *((VAL*)(x->info.c.args)+i)); // recursive version
20+ *argptr = *((VAL*)(x->info.c.args)+i);
25+ cl = MKFLOAT(vm, x->info.f);
28+ cl = MKSTR(vm, x->info.str);
31+ cl = MKPTR(vm, x->info.ptr);
45+ char* scan = vm->heap;
47+ while(scan < vm->heap_next) {
48+ size_t inc = *((size_t*)scan);
49+ VAL heap_item = (VAL)(scan+sizeof(size_t)*2);
50+ // If it's a CON, copy its arguments
51+ switch(heap_item->ty) {
53+ argptr = (VAL*)(heap_item->info.c.args);
54+ for(i = 0; i < heap_item->info.c.arity; ++i) {
55+ *argptr = copy(vm, *argptr);
65+ char* newheap = malloc(vm -> heap_size);
66+ char* oldheap = vm -> heap;
69+ vm->heap_next = newheap;
70+ vm->heap_end = newheap + vm->heap_size;
76+ for(root = vm->valstack; root < vm->valstack_top; ++root) {
77+ *root = copy(vm, *root);
79+ vm->ret = copy(vm, vm->ret);
82+ // After reallocation, if we've still more than half filled the new heap, grow the heap
85+ if ((vm->heap_next - vm->heap) > vm->heap_size >> 1) {
86+ vm->heap_size += vm->heap_growth;