diff --git a/GC.c b/GC.c index b6a65fa..de25be0 100644 --- a/GC.c +++ b/GC.c @@ -12,9 +12,12 @@ typedef struct { unsigned int freiZeiger; unsigned int size; - ObjRef *data; + unsigned char *data; } *heapPartRef; +// SDA +struct sda sda; + // Stack struct stack stack; #define SIZE 64 @@ -38,15 +41,58 @@ void copy(ObjRef obj){ if(obj->brokenHeart == true) return; obj->brokenHeart = true; if (IS_PRIMITIVE(obj)){ - obj->forward_pointer = memcpy(secondary->data[secondary->freiZeiger],obj,obj->size); + if(obj->size > secondary->size-secondary->freiZeiger) perror("Heap is to Small"); + obj->forward_pointer = memcpy((void *) secondary->data[secondary->freiZeiger], obj, obj->size); secondary->freiZeiger += obj->size; } else { + if(sizeof(*obj) + (GET_ELEMENT_COUNT(obj) * sizeof(void *)) > secondary->size-secondary->freiZeiger) perror("Heap is to Small"); GET_ELEMENT_COUNT(obj); - obj->forward_pointer = memcpy(secondary->data[secondary->freiZeiger],obj,sizeof(*obj) + (GET_ELEMENT_COUNT(obj) * sizeof(void *))); + obj->forward_pointer = memcpy((void *) secondary->data[secondary->freiZeiger], obj, sizeof(*obj) + (GET_ELEMENT_COUNT(obj) * sizeof(void *))); secondary->freiZeiger += sizeof(*obj) + (GET_ELEMENT_COUNT(obj) * sizeof(void *)); for (int i = 0; i < GET_ELEMENT_COUNT(obj); ++i) { copy(getField(obj,i)); } } } + +void runGC(){ + int rootSize = *sda.size + *reg.size + *stack.size + 4; + ObjRef* rootObjs = malloc(sizeof(ObjRef) * rootSize); + if(rootObjs == NULL) perror("malloc"); + int counter = 0; + //Bip + rootObjs[0] = bip.op1; + rootObjs[counter++] = bip.op2; + rootObjs[counter++] = bip.res; + rootObjs[counter++] = bip.rem; + //SDA + for (int i = 0; i < *sda.size; ++i) { + rootObjs[counter++] = sda.sda[i]; + } + //REG + for (int i = 0; i < *reg.size; ++i) { + rootObjs[counter++] = reg.stack[i].u.objRef; + } + //STACK + for (int i = 0; i < *stack.size; ++i) { + rootObjs[counter++] = stack.stack[i].u.objRef; + } + + for (int i = 0; i < rootSize; ++i) { + if(rootObjs[i] == NULL) continue; + copy(rootObjs[i]); + } + heapPartRef temp = primary; + primary = secondary; + secondary = temp; + +} + +void *alloc(size_t size){ + if(primary->size-primary->freiZeiger < size){ + runGC(); + } + primary->freiZeiger += size; + return (void *) primary->data[primary->freiZeiger - size]; +} #endif diff --git a/bigint/build/bin/testbip b/bigint/build/bin/testbip index 84ced15..b026b0c 100755 Binary files a/bigint/build/bin/testbip and b/bigint/build/bin/testbip differ diff --git a/bigint/build/lib/libbigint.a b/bigint/build/lib/libbigint.a index 3d8c43a..84a5d14 100644 Binary files a/bigint/build/lib/libbigint.a and b/bigint/build/lib/libbigint.a differ diff --git a/bigint/src/libbigint.a b/bigint/src/libbigint.a index 3d8c43a..84a5d14 100644 Binary files a/bigint/src/libbigint.a and b/bigint/src/libbigint.a differ diff --git a/bigint/tst/testbip b/bigint/tst/testbip index 84ced15..b026b0c 100755 Binary files a/bigint/tst/testbip and b/bigint/tst/testbip differ diff --git a/njvm.c b/njvm.c index 4d19931..f16e77a 100644 --- a/njvm.c +++ b/njvm.c @@ -5,7 +5,6 @@ #include "stack.c" #include "program.c" #include "codeReader.c" -#include "SDA.c" #include "debugMenu.c" #include "bigint.h" #include "record.c" @@ -311,7 +310,7 @@ int main(int argc, char *argv[]) { int current = 0; stack.size = &size; stack.current = ¤t; - stack.stack = malloc(SIZE * 10024); + stack.stack = malloc(SIZE * 1024); // Initialize the registery int rSize = 1000; diff --git a/njvm.o b/njvm.o index b389ffd..d5a90df 100644 Binary files a/njvm.o and b/njvm.o differ diff --git a/record.c b/record.c index a9c6eb1..3428583 100644 --- a/record.c +++ b/record.c @@ -32,7 +32,7 @@ void setField(ObjRef arr, int point, ObjRef value){ if(0 > point || point > getSize(arr)){ printf("Index %i out of bounds for length %i\n",point, getSize(arr)); } - GET_REFS_PTR(arr)[point] = malloc(8); + GET_REFS_PTR(arr)[point] = malloc(20); * (ObjRef *)GET_REFS_PTR(arr)[point]->data = value; } diff --git a/stack.c b/stack.c index 0b2ae46..2079fbe 100755 --- a/stack.c +++ b/stack.c @@ -9,8 +9,7 @@ #include "stackslot.c" #include "sda.c" -// SDA -struct sda sda; + struct stack { int* size; diff --git a/stackslot.c b/stackslot.c index 875f81d..24d2140 100644 --- a/stackslot.c +++ b/stackslot.c @@ -22,7 +22,7 @@ typedef struct { ObjRef getIntObj(int val) { ObjRef intObject; - unsigned int objSize = sizeof(unsigned int) + sizeof(int); + unsigned int objSize = sizeof(ObjRef) + sizeof(int); if ((intObject = malloc(objSize)) == NULL) { perror("malloc"); }