gc
This commit is contained in:
parent
221a08e7dc
commit
35f051b5da
180
GC.c
180
GC.c
@ -3,97 +3,103 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "stackslot.c"
|
#include <stdbool.h>
|
||||||
#include "stack.c"
|
#include "objref.c"
|
||||||
#include "bigint.h"
|
|
||||||
#include "instruktion.c"
|
#include "instruktion.c"
|
||||||
#include "record.c"
|
|
||||||
#include "SDA.c"
|
|
||||||
|
|
||||||
typedef struct {
|
bool halbspeicher1IsActiv = true;
|
||||||
unsigned int freiZeiger;
|
unsigned char* halbspeicher1;
|
||||||
unsigned int size;
|
unsigned char* halbspeicher2;
|
||||||
unsigned char *data;
|
unsigned char* freizeiger;
|
||||||
} *heapPartRef;
|
unsigned int groesse;
|
||||||
|
|
||||||
// SDA
|
void initHeap(unsigned int size){
|
||||||
struct sda sda;
|
groesse = size*1024/2;
|
||||||
|
if((halbspeicher1 = malloc(groesse)) == NULL) perror("malloc");
|
||||||
|
if((halbspeicher2 = malloc(groesse)) == NULL) perror("malloc");
|
||||||
|
freizeiger = halbspeicher1;
|
||||||
|
}
|
||||||
|
ObjRef copyObjToFreeMem(ObjRef obj){
|
||||||
|
ObjRef copy = (ObjRef) halbspeicher2[freizeiger];
|
||||||
|
unsigned int size = (IS_PRIMITIVE(obj))? obj->size : GET_ELEMENT_COUNT(obj)* sizeof(void *);
|
||||||
|
memcpy((void *) halbspeicher2[freizeiger], obj, size);
|
||||||
|
freizeiger += size;
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
void setBrokenHeart(ObjRef obj, bool val){
|
||||||
|
//TODO
|
||||||
|
}
|
||||||
|
bool getBrokenHeart(ObjRef obj){
|
||||||
|
//TODO
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
void setForwardPointer(ObjRef obj, void* ref){
|
||||||
|
//TODO
|
||||||
|
}
|
||||||
|
void *getForwardPointer(ObjRef obj){
|
||||||
|
//TODO
|
||||||
|
return malloc(0);
|
||||||
|
}
|
||||||
|
|
||||||
// Stack
|
ObjRef reallocate(ObjRef orig){
|
||||||
struct stack stack;
|
ObjRef copy;
|
||||||
#define SIZE 64
|
if(orig == NULL){
|
||||||
|
copy == NULL;
|
||||||
|
}else{
|
||||||
|
if(getBrokenHeart(orig)){
|
||||||
|
setForwardPointer(copy,orig);
|
||||||
|
}else{
|
||||||
|
copy = copyObjToFreeMem(orig);
|
||||||
|
setBrokenHeart(orig,true);
|
||||||
|
setForwardPointer(orig,copy);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
unsigned int getSize(ObjRef obj){
|
||||||
|
return (IS_PRIMITIVE(obj))? obj->size : GET_ELEMENT_COUNT(obj)* sizeof(void *);
|
||||||
|
}
|
||||||
|
void scan(void){
|
||||||
|
void *scan = halbspeicher1;
|
||||||
|
while (scan < freizeiger){
|
||||||
|
if(!IS_PRIMITIVE((ObjRef)scan)){
|
||||||
|
for (unsigned int i = 0; i < GET_ELEMENT_COUNT((ObjRef)scan); i += getSize(GET_REFS_PTR((ObjRef)scan)[i])) {
|
||||||
|
GET_REFS_PTR((ObjRef)scan)[i] = reallocate(GET_REFS_PTR((ObjRef)scan)[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
scan += getSize((ObjRef)scan);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void *alloc(unsigned int size){
|
||||||
|
if((&freizeiger - &halbspeicher1)/8 + size > groesse) {
|
||||||
|
printf("Heap Overflow\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
// Set broken heart to false
|
||||||
|
setBrokenHeart((ObjRef)freizeiger,false);
|
||||||
|
freizeiger += sizeof(bool) + sizeof(void *);
|
||||||
|
void *ptr = &freizeiger;
|
||||||
|
freizeiger += size;
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
void swap(void){
|
||||||
|
unsigned char *tmp = halbspeicher1;
|
||||||
|
halbspeicher1 = halbspeicher2;
|
||||||
|
halbspeicher2 = tmp;
|
||||||
|
freizeiger = halbspeicher1;
|
||||||
|
halbspeicher1IsActiv = !halbspeicher1IsActiv;
|
||||||
|
}
|
||||||
|
void triggerGC(void){
|
||||||
|
printf("GC triggered\n");
|
||||||
|
//TODO
|
||||||
|
}
|
||||||
|
void copy(){
|
||||||
|
ObjRef *test;
|
||||||
|
unsigned int objResSize = 2;
|
||||||
|
if((test = malloc(2*sizeof(test)))==NULL) perror("malloc");
|
||||||
|
for (int i = 0; i < objResSize; ++i) {
|
||||||
|
|
||||||
//Register
|
}
|
||||||
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];
|
|
||||||
//}
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
18
njvm.c
18
njvm.c
@ -9,6 +9,7 @@
|
|||||||
#include "bigint.h"
|
#include "bigint.h"
|
||||||
#include "record.c"
|
#include "record.c"
|
||||||
#include "GC.c"
|
#include "GC.c"
|
||||||
|
#include "SDA.c"
|
||||||
|
|
||||||
// Debug
|
// Debug
|
||||||
int debug = 0;
|
int debug = 0;
|
||||||
@ -16,6 +17,15 @@ int debug = 0;
|
|||||||
// Program
|
// Program
|
||||||
struct program program;
|
struct program program;
|
||||||
|
|
||||||
|
// SDA
|
||||||
|
struct sda sda;
|
||||||
|
|
||||||
|
// Stack
|
||||||
|
struct stack stack;
|
||||||
|
#define SIZE 64
|
||||||
|
|
||||||
|
//Register
|
||||||
|
struct stack reg;
|
||||||
|
|
||||||
unsigned fp;
|
unsigned fp;
|
||||||
|
|
||||||
@ -306,7 +316,8 @@ void tests(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
// Init the Heap
|
||||||
|
initHeap(1000);
|
||||||
|
|
||||||
// Initialize the Stack
|
// Initialize the Stack
|
||||||
int size = SIZE;
|
int size = SIZE;
|
||||||
@ -337,6 +348,7 @@ int main(int argc, char *argv[]) {
|
|||||||
int run = 0;
|
int run = 0;
|
||||||
int sizeSDA;
|
int sizeSDA;
|
||||||
|
|
||||||
|
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
for (int i = 1; i < argc; ++i) {
|
for (int i = 1; i < argc; ++i) {
|
||||||
if (strcmp(argv[i], "--debug") == 0) {
|
if (strcmp(argv[i], "--debug") == 0) {
|
||||||
@ -360,6 +372,10 @@ int main(int argc, char *argv[]) {
|
|||||||
run = 1;
|
run = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Init the sda
|
||||||
|
ObjRef s[sizeSDA];
|
||||||
|
sda.size = &sizeSDA;
|
||||||
|
sda.sda = s;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (debug) {
|
if (debug) {
|
||||||
|
|||||||
8
objref.c
8
objref.c
@ -3,11 +3,17 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
typedef struct ObjRef{
|
typedef struct ObjRef{
|
||||||
bool brokenHeart;
|
// if brkokenHeart and forward_pointer added at this point bip throws errors
|
||||||
|
// bool brokenHeart;
|
||||||
// struct ObjRef *forward_pointer;
|
// struct ObjRef *forward_pointer;
|
||||||
unsigned int size;
|
unsigned int size;
|
||||||
unsigned char data[1];
|
unsigned char data[1];
|
||||||
} *ObjRef;
|
} *ObjRef;
|
||||||
|
|
||||||
|
typedef struct{
|
||||||
|
unsigned char brokenHeart;
|
||||||
|
ObjRef forward_pointer;
|
||||||
|
} *ObjRefGC;
|
||||||
|
|
||||||
#endif /* ifndef OBJREF
|
#endif /* ifndef OBJREF
|
||||||
#define OBJREF */
|
#define OBJREF */
|
||||||
|
|||||||
1
record.c
1
record.c
@ -5,6 +5,7 @@
|
|||||||
#define RECORD
|
#define RECORD
|
||||||
#include "stackslot.c"
|
#include "stackslot.c"
|
||||||
#include "instruktion.c"
|
#include "instruktion.c"
|
||||||
|
#include "GC.c"
|
||||||
ObjRef newRecord(int size){
|
ObjRef newRecord(int size){
|
||||||
ObjRef record;
|
ObjRef record;
|
||||||
unsigned int objSize;
|
unsigned int objSize;
|
||||||
|
|||||||
@ -4,7 +4,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "objref.c"
|
#include "objref.c"
|
||||||
|
#include "GC.c"
|
||||||
void fatalError(char *msg){
|
void fatalError(char *msg){
|
||||||
printf("Fatal error: %s\n", msg);
|
printf("Fatal error: %s\n", msg);
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user