added some things to GC

This commit is contained in:
nilspolek 2024-01-27 10:25:27 +01:00
parent 91b36a53d5
commit 03b88a8a76
10 changed files with 53 additions and 9 deletions

52
GC.c
View File

@ -12,9 +12,12 @@
typedef struct { typedef struct {
unsigned int freiZeiger; unsigned int freiZeiger;
unsigned int size; unsigned int size;
ObjRef *data; unsigned char *data;
} *heapPartRef; } *heapPartRef;
// SDA
struct sda sda;
// Stack // Stack
struct stack stack; struct stack stack;
#define SIZE 64 #define SIZE 64
@ -38,15 +41,58 @@ 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)){
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; secondary->freiZeiger += obj->size;
} else { } else {
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(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(){
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 #endif

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

3
njvm.c
View File

@ -5,7 +5,6 @@
#include "stack.c" #include "stack.c"
#include "program.c" #include "program.c"
#include "codeReader.c" #include "codeReader.c"
#include "SDA.c"
#include "debugMenu.c" #include "debugMenu.c"
#include "bigint.h" #include "bigint.h"
#include "record.c" #include "record.c"
@ -311,7 +310,7 @@ int main(int argc, char *argv[]) {
int current = 0; int current = 0;
stack.size = &size; stack.size = &size;
stack.current = &current; stack.current = &current;
stack.stack = malloc(SIZE * 10024); stack.stack = malloc(SIZE * 1024);
// Initialize the registery // Initialize the registery
int rSize = 1000; int rSize = 1000;

BIN
njvm.o

Binary file not shown.

View File

@ -32,7 +32,7 @@ void setField(ObjRef arr, int point, ObjRef value){
if(0 > point || point > getSize(arr)){ if(0 > point || point > getSize(arr)){
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));
} }
GET_REFS_PTR(arr)[point] = malloc(8); GET_REFS_PTR(arr)[point] = malloc(20);
* (ObjRef *)GET_REFS_PTR(arr)[point]->data = value; * (ObjRef *)GET_REFS_PTR(arr)[point]->data = value;
} }

View File

@ -9,8 +9,7 @@
#include "stackslot.c" #include "stackslot.c"
#include "sda.c" #include "sda.c"
// SDA
struct sda sda;
struct stack { struct stack {
int* size; int* size;

View File

@ -22,7 +22,7 @@ typedef struct {
ObjRef getIntObj(int val) { ObjRef getIntObj(int val) {
ObjRef intObject; ObjRef intObject;
unsigned int objSize = sizeof(unsigned int) + sizeof(int); unsigned int objSize = sizeof(ObjRef) + sizeof(int);
if ((intObject = malloc(objSize)) == NULL) { if ((intObject = malloc(objSize)) == NULL) {
perror("malloc"); perror("malloc");
} }