made some updates

This commit is contained in:
nilspolek 2024-01-27 19:59:01 +01:00
parent 460def3fc6
commit fd15e54cdd
14 changed files with 95 additions and 95 deletions

138
GC.c
View File

@ -27,73 +27,73 @@ struct stack stack;
struct stack reg;
heapPartRef primary;
heapPartRef secondary;
unsigned int heapSize;
void initHeap(int size){
heapSize = 2 * sizeof (unsigned int) + size;
if ((primary = malloc(heapSize)) == NULL) perror("malloc");
if ((secondary = malloc(heapSize)) == NULL) perror("malloc");
}
// nimmt obj und copier es in den secondary Speicher
void copy(ObjRef obj){
if(obj->brokenHeart == true) return;
obj->brokenHeart = true;
if (IS_PRIMITIVE(obj)){
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((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];
}
//heapPartRef primary;
//heapPartRef secondary;
//unsigned int heapSize;
//void initHeap(int size){
// heapSize = 2 * sizeof (unsigned int) + size;
// if ((primary = malloc(heapSize)) == NULL) perror("malloc");
// if ((secondary = malloc(heapSize)) == NULL) perror("malloc");
//}
//
//
//// nimmt obj und copier es in den secondary Speicher
//void copy(ObjRef obj){
// if(obj->brokenHeart == true) return;
// obj->brokenHeart = true;
// if (IS_PRIMITIVE(obj)){
// 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((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

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -38,13 +38,13 @@ void fatalError(char *msg) {
*/
void * newPrimObject(int dataSize) {
ObjRef bigObjRef;
bigObjRef = malloc(sizeof(unsigned int) +
dataSize * sizeof(unsigned char));
int size = sizeof(unsigned int) + dataSize * sizeof(unsigned char);
bigObjRef = malloc(size);
bigObjRef->size = size;
if (bigObjRef == NULL) {
fatalError("newPrimObject() got no memory");
}
bigObjRef->size = dataSize;
bigObjRef->size = size;
return bigObjRef;
}

Binary file not shown.

Binary file not shown.

View File

@ -18,7 +18,6 @@ void inspect(struct stack s, int fp){
if (input[0] == 'o'){
scanf("%19s",ref);
ObjRefContainer container;
container = getRefs(s);
for (int i = 0; i<= container.size; i++) {
sprintf(refStr, "%p", (void *)&container.refs[i]);
if(strcmp(ref, refStr) == 0) printf("Adress exists\n");

BIN
njvm.o

Binary file not shown.

View File

@ -12,7 +12,7 @@ ObjRef newRecord(int size){
if((record = malloc(objSize)) == NULL){
perror("malloc");
}
record->size = (1 << ((sizeof(int) * 8) - 1));
record->size = MSB;
record->size = record->size + size;
for(int i = 0; i < size; i++) {
GET_REFS_PTR(record)[i] = NULL;
@ -33,11 +33,14 @@ void setField(ObjRef arr, int point, ObjRef value){
printf("Index %i out of bounds for length %i\n",point, getSize(arr));
}
if(IS_PRIMITIVE(value)){
int size = sizeof(*value);
// printf("Size of value is %i\n", value->size);
int size = value->size;
GET_REFS_PTR(arr)[point] = malloc(size);
GET_REFS_PTR(arr)[point]->size = size;
}else{
int size = sizeof(*value) + (GET_ELEMENT_COUNT(value) * sizeof(void *));
GET_REFS_PTR(arr)[point] = malloc(size);
GET_REFS_PTR(arr)[point] ->size = size;
}
* (ObjRef *)GET_REFS_PTR(arr)[point]->data = value;
}

28
stack.c
View File

@ -39,20 +39,20 @@ void printStack(struct stack stack, int fp) {
}
}
ObjRefContainer getRefs(struct stack stack){
ObjRefContainer continer;
int counter = 0;
for (int i = 0; i <= *stack.current; i++) {
if(stack.stack[i].isObjRef == true) counter++;
}
continer.size = counter;
ObjRef *list = (ObjRef *)malloc(counter * sizeof(ObjRef));
for (int i = 0; i<= *stack.current; i++)
if(stack.stack[i].isObjRef == true)
list[counter--] = stack.stack[i].u.objRef;
continer.refs = list;
return continer;
}
//ObjRefContainer getRefs(struct stack stack){
// ObjRefContainer continer;
// int counter = 0;
// for (int i = 0; i <= *stack.current; i++) {
// if(stack.stack[i].isObjRef == true) counter++;
// }
// continer.size = counter;
// ObjRef *list = (ObjRef *)malloc(counter * sizeof(ObjRef));
// for (int i = 0; i<= *stack.current; i++)
// if(stack.stack[i].isObjRef == true)
// list[counter--] = stack.stack[i].u.objRef;
// continer.refs = list;
// return continer;
//}
void push(struct stack s, StackSlot value) {
if (*s.current >= *s.size) {

View File

@ -6,8 +6,6 @@
#define STACKSLOT
typedef int Object;
typedef struct ObjRef{
bool brokenHeart;
struct ObjRef *forward_pointer;
unsigned int size;
unsigned char data[1];
} *ObjRef;
@ -27,7 +25,7 @@ ObjRef getIntObj(int val) {
perror("malloc");
}
*(int *) intObject->data = val;
intObject->size = sizeof(int);
intObject->size = objSize;
return intObject;
}
@ -43,7 +41,7 @@ int getIntValfromStackSlot(StackSlot s) {
}
void setValIntObj(ObjRef iref, int val) {
iref->size = sizeof(int);
iref->size = sizeof(int)+sizeof(ObjRef);
*(int *) iref->data = val;
}

View File

@ -18,7 +18,7 @@ void * newPrimObject(int dataSize) {
if (bigObjRef == NULL) {
fatalError("newPrimObject() got no memory");
}
bigObjRef->size = dataSize;
bigObjRef->size = sizeof(unsigned int) + dataSize * sizeof(unsigned char);
return bigObjRef;
}

BIN
support.o

Binary file not shown.