changed stack size

This commit is contained in:
nilspolek 2024-01-26 19:13:42 +01:00
parent 169217fd2c
commit 91b36a53d5
15 changed files with 60 additions and 84 deletions

115
GC.c
View File

@ -1,77 +1,52 @@
#ifndef GC
#define GC
#include <stdlib.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include "stackslot.c"
#include "stack.c"
#include "bigint.h"
#include "instruktion.c"
#include "record.c"
typedef struct { typedef struct {
unsigned int freiZeiger;
unsigned int size; unsigned int size;
unsigned int current; ObjRef *data;
unsigned char data[1]; } *heapPartRef;
} heapPart;
typedef struct {
heapPart *primary;
heapPart *secondary;
unsigned int size;
unsigned int current;
} heap;
void scan(heap *h) { // Stack
heapPart *part = h->primary; struct stack stack;
unsigned int i; #define SIZE 64
for (i = 0; i < h->current; i++) {
unsigned int j; //Register
for (j = 0; j < part->current; j++) { struct stack reg;
void *p = *(void **) (part->data + j);
if (p >= part->data && p < part->data + part->size) {
*(void **) (part->data + j) = h->secondary + h->current; heapPartRef primary;
memcpy(h->secondary + h->current, p, sizeof(void *)); heapPartRef secondary;
h->current++; 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)){
obj->forward_pointer = memcpy(secondary->data[secondary->freiZeiger],obj,obj->size);
secondary->freiZeiger += obj->size;
} else {
GET_ELEMENT_COUNT(obj);
obj->forward_pointer = memcpy(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));
} }
part++;
} }
} }
#endif
void collect(heap *h) {
scan(h);
heapPart *tmp = h->primary;
h->primary = h->secondary;
h->secondary = tmp;
h->current = 0;
}
void *alloc(heap *h, unsigned int size) {
if (h->current == h->size) {
collect(h);
}
heapPart *part = h->primary + h->current;
if (part->current + size > part->size) {
collect(h);
part = h->primary + h->current;
}
void *p = part->data + part->current;
part->current += size;
return p;
}
void *reallocate(heap *h, void *p, unsigned int size) {
if (p == NULL) {
return alloc(h, size);
}
heapPart *part = h->primary;
unsigned int i;
for (i = 0; i < h->current; i++) {
if (p >= part->data && p < part->data + part->size) {
break;
}
part++;
}
if (i == h->current) {
return NULL;
}
if (size <= part->size) {
return p;
}
void *newP = alloc(h, size);
if (newP == NULL) {
return NULL;
}
memcpy(newP, p, part->size);
return newP;
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

11
njvm.c
View File

@ -9,22 +9,15 @@
#include "debugMenu.c" #include "debugMenu.c"
#include "bigint.h" #include "bigint.h"
#include "record.c" #include "record.c"
#include "GC.c"
// Debug // Debug
int debug = 0; int debug = 0;
// Stack
struct stack stack;
#define SIZE 64
//Register
struct stack reg;
// Program // Program
struct program program; struct program program;
// SDA
struct sda sda;
unsigned fp; unsigned fp;
void version(void) { void version(void) {

BIN
njvm.o

Binary file not shown.

View File

@ -1,9 +1,11 @@
#ifndef OBJREF #ifndef OBJREF
#define OBJREF #define OBJREF
typedef struct { #include <stdbool.h>
unsigned int size;
unsigned char data[1]; typedef struct ObjRef{
unsigned int size;
unsigned char data[1];
} *ObjRef; } *ObjRef;
#endif /* ifndef OBJREF #endif /* ifndef OBJREF

View File

@ -7,6 +7,10 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "stackslot.c" #include "stackslot.c"
#include "sda.c"
// SDA
struct sda sda;
struct stack { struct stack {
int* size; int* size;
@ -17,7 +21,7 @@ struct stack {
typedef struct { typedef struct {
unsigned int size; unsigned int size;
ObjRef *refs; ObjRef *refs;
}ObjRefContainer; }ObjRefContainer;
void printStack(struct stack stack, int fp) { void printStack(struct stack stack, int fp) {
printf("Stack\nSize:\t\t%i\nCurrent:\t%i\n", *stack.size, *stack.current); printf("Stack\nSize:\t\t%i\nCurrent:\t%i\n", *stack.size, *stack.current);

View File

@ -5,8 +5,9 @@
#ifndef STACKSLOT #ifndef STACKSLOT
#define STACKSLOT #define STACKSLOT
typedef int Object; typedef int Object;
typedef struct { typedef struct ObjRef{
bool brokenHeart; bool brokenHeart;
struct ObjRef *forward_pointer;
unsigned int size; unsigned int size;
unsigned char data[1]; unsigned char data[1];
} *ObjRef; } *ObjRef;

View File

@ -1,3 +1,4 @@
#include "support.h" #include "support.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>

BIN
support.o

Binary file not shown.