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; struct stack reg;
heapPartRef primary; //heapPartRef primary;
heapPartRef secondary; //heapPartRef secondary;
unsigned int heapSize; //unsigned int heapSize;
void initHeap(int size){ //void initHeap(int size){
heapSize = 2 * sizeof (unsigned int) + size; // heapSize = 2 * sizeof (unsigned int) + size;
if ((primary = malloc(heapSize)) == NULL) perror("malloc"); // if ((primary = malloc(heapSize)) == NULL) perror("malloc");
if ((secondary = malloc(heapSize)) == NULL) perror("malloc"); // if ((secondary = malloc(heapSize)) == NULL) perror("malloc");
} //}
//
//
// nimmt obj und copier es in den secondary Speicher //// nimmt obj und copier es in den secondary Speicher
void copy(ObjRef obj){ //void copy(ObjRef obj){
if(obj->brokenHeart == true) return; // if(obj->brokenHeart == true) return;
obj->brokenHeart = true; // obj->brokenHeart = true;
if (IS_PRIMITIVE(obj)){ // if (IS_PRIMITIVE(obj)){
if(obj->size > secondary->size-secondary->freiZeiger) perror("Heap is to Small"); // if(obj->size > secondary->size-secondary->freiZeiger) perror("Heap is to Small");
obj->forward_pointer = memcpy((void *) secondary->data[secondary->freiZeiger], obj, obj->size); // obj->forward_pointer = memcpy((void *) secondary->data[secondary->freiZeiger], obj, obj->size);
secondary->freiZeiger += obj->size; // secondary->freiZeiger += obj->size;
} else { // } else {
if(sizeof(*obj) + (GET_ELEMENT_COUNT(obj) * sizeof(void *)) > secondary->size-secondary->freiZeiger) perror("Heap is to Small"); // if(sizeof(*obj) + (GET_ELEMENT_COUNT(obj) * sizeof(void *)) > secondary->size-secondary->freiZeiger) perror("Heap is to Small");
GET_ELEMENT_COUNT(obj); // GET_ELEMENT_COUNT(obj);
obj->forward_pointer = memcpy((void *) 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 *)); // secondary->freiZeiger += sizeof(*obj) + (GET_ELEMENT_COUNT(obj) * sizeof(void *));
for (int i = 0; i < GET_ELEMENT_COUNT(obj); ++i) { // for (int i = 0; i < GET_ELEMENT_COUNT(obj); ++i) {
copy(getField(obj,i)); // copy(getField(obj,i));
} // }
} // }
} //}
//
void runGC(){ //void runGC(){
int rootSize = *sda.size + *reg.size + *stack.size + 4; // int rootSize = *sda.size + *reg.size + *stack.size + 4;
ObjRef* rootObjs = malloc(sizeof(ObjRef) * rootSize); // ObjRef* rootObjs = malloc(sizeof(ObjRef) * rootSize);
if(rootObjs == NULL) perror("malloc"); // if(rootObjs == NULL) perror("malloc");
int counter = 0; // int counter = 0;
//Bip // //Bip
rootObjs[0] = bip.op1; // rootObjs[0] = bip.op1;
rootObjs[counter++] = bip.op2; // rootObjs[counter++] = bip.op2;
rootObjs[counter++] = bip.res; // rootObjs[counter++] = bip.res;
rootObjs[counter++] = bip.rem; // rootObjs[counter++] = bip.rem;
//SDA // //SDA
for (int i = 0; i < *sda.size; ++i) { // for (int i = 0; i < *sda.size; ++i) {
rootObjs[counter++] = sda.sda[i]; // rootObjs[counter++] = sda.sda[i];
} // }
//REG // //REG
for (int i = 0; i < *reg.size; ++i) { // for (int i = 0; i < *reg.size; ++i) {
rootObjs[counter++] = reg.stack[i].u.objRef; // rootObjs[counter++] = reg.stack[i].u.objRef;
} // }
//STACK // //STACK
for (int i = 0; i < *stack.size; ++i) { // for (int i = 0; i < *stack.size; ++i) {
rootObjs[counter++] = stack.stack[i].u.objRef; // rootObjs[counter++] = stack.stack[i].u.objRef;
} // }
//
for (int i = 0; i < rootSize; ++i) { // for (int i = 0; i < rootSize; ++i) {
if(rootObjs[i] == NULL) continue; // if(rootObjs[i] == NULL) continue;
copy(rootObjs[i]); // copy(rootObjs[i]);
} // }
heapPartRef temp = primary; // heapPartRef temp = primary;
primary = secondary; // primary = secondary;
secondary = temp; // secondary = temp;
//
} //}
//
void *alloc(size_t size){ //void *alloc(size_t size){
if(primary->size-primary->freiZeiger < size){ // if(primary->size-primary->freiZeiger < size){
runGC(); // runGC();
} // }
primary->freiZeiger += size; // primary->freiZeiger += size;
return (void *) primary->data[primary->freiZeiger - size]; // return (void *) primary->data[primary->freiZeiger - size];
} //}
#endif #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) { void * newPrimObject(int dataSize) {
ObjRef bigObjRef; ObjRef bigObjRef;
int size = sizeof(unsigned int) + dataSize * sizeof(unsigned char);
bigObjRef = malloc(sizeof(unsigned int) + bigObjRef = malloc(size);
dataSize * sizeof(unsigned char)); bigObjRef->size = size;
if (bigObjRef == NULL) { if (bigObjRef == NULL) {
fatalError("newPrimObject() got no memory"); fatalError("newPrimObject() got no memory");
} }
bigObjRef->size = dataSize; bigObjRef->size = size;
return bigObjRef; 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'){ if (input[0] == 'o'){
scanf("%19s",ref); scanf("%19s",ref);
ObjRefContainer container; ObjRefContainer container;
container = getRefs(s);
for (int i = 0; i<= container.size; i++) { for (int i = 0; i<= container.size; i++) {
sprintf(refStr, "%p", (void *)&container.refs[i]); sprintf(refStr, "%p", (void *)&container.refs[i]);
if(strcmp(ref, refStr) == 0) printf("Adress exists\n"); 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){ if((record = malloc(objSize)) == NULL){
perror("malloc"); perror("malloc");
} }
record->size = (1 << ((sizeof(int) * 8) - 1)); record->size = MSB;
record->size = record->size + size; record->size = record->size + size;
for(int i = 0; i < size; i++) { for(int i = 0; i < size; i++) {
GET_REFS_PTR(record)[i] = NULL; 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)); printf("Index %i out of bounds for length %i\n",point, getSize(arr));
} }
if(IS_PRIMITIVE(value)){ 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] = malloc(size);
GET_REFS_PTR(arr)[point]->size = size;
}else{ }else{
int size = sizeof(*value) + (GET_ELEMENT_COUNT(value) * sizeof(void *)); int size = sizeof(*value) + (GET_ELEMENT_COUNT(value) * sizeof(void *));
GET_REFS_PTR(arr)[point] = malloc(size); GET_REFS_PTR(arr)[point] = malloc(size);
GET_REFS_PTR(arr)[point] ->size = size;
} }
* (ObjRef *)GET_REFS_PTR(arr)[point]->data = value; * (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 getRefs(struct stack stack){
ObjRefContainer continer; // ObjRefContainer continer;
int counter = 0; // int counter = 0;
for (int i = 0; i <= *stack.current; i++) { // for (int i = 0; i <= *stack.current; i++) {
if(stack.stack[i].isObjRef == true) counter++; // if(stack.stack[i].isObjRef == true) counter++;
} // }
continer.size = counter; // continer.size = counter;
ObjRef *list = (ObjRef *)malloc(counter * sizeof(ObjRef)); // ObjRef *list = (ObjRef *)malloc(counter * sizeof(ObjRef));
for (int i = 0; i<= *stack.current; i++) // for (int i = 0; i<= *stack.current; i++)
if(stack.stack[i].isObjRef == true) // if(stack.stack[i].isObjRef == true)
list[counter--] = stack.stack[i].u.objRef; // list[counter--] = stack.stack[i].u.objRef;
continer.refs = list; // continer.refs = list;
return continer; // return continer;
} //}
void push(struct stack s, StackSlot value) { void push(struct stack s, StackSlot value) {
if (*s.current >= *s.size) { if (*s.current >= *s.size) {

View File

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

View File

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

BIN
support.o

Binary file not shown.