From 169217fd2cb9e7af611e381e2dc8b74af763274b Mon Sep 17 00:00:00 2001 From: nilspolek Date: Fri, 26 Jan 2024 17:00:50 +0100 Subject: [PATCH] changed stack size --- CMakeLists.txt | 2 +- GC.c | 77 ++++++++++++++++++++++++++++++++++++++++++++ heap.c | 11 ------- njvm.c | 1 - stackslot.c | 86 +++++++++++++++++++++++++++----------------------- 5 files changed, 125 insertions(+), 52 deletions(-) create mode 100644 GC.c delete mode 100644 heap.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 78c3ffb..d44b8df 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ target_include_directories(njvm PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/bigint/build # Add the library add_library(bigint STATIC ${CMAKE_SOURCE_DIR}/bigint/src/bigint.c support.c - heap.c) + GC.c) # Include directories for the bigint library target_include_directories(bigint PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/bigint/build/include) diff --git a/GC.c b/GC.c new file mode 100644 index 0000000..211037c --- /dev/null +++ b/GC.c @@ -0,0 +1,77 @@ +#include +typedef struct { + unsigned int size; + unsigned int current; + unsigned char data[1]; +} heapPart; +typedef struct { + heapPart *primary; + heapPart *secondary; + unsigned int size; + unsigned int current; +} heap; + +void scan(heap *h) { + heapPart *part = h->primary; + unsigned int i; + for (i = 0; i < h->current; i++) { + unsigned int j; + for (j = 0; j < part->current; j++) { + void *p = *(void **) (part->data + j); + if (p >= part->data && p < part->data + part->size) { + *(void **) (part->data + j) = h->secondary + h->current; + memcpy(h->secondary + h->current, p, sizeof(void *)); + h->current++; + } + } + part++; + } +} + +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; +} diff --git a/heap.c b/heap.c deleted file mode 100644 index aee1c76..0000000 --- a/heap.c +++ /dev/null @@ -1,11 +0,0 @@ -// -// Created by Nils Polek on 23.01.24. -// -typedef struct { - int size; - int hcurrent; -}heap; - -//void* alloc(int size){ -// return {} -//} \ No newline at end of file diff --git a/njvm.c b/njvm.c index f46233c..c95f6ae 100644 --- a/njvm.c +++ b/njvm.c @@ -313,7 +313,6 @@ void tests(void) { int main(int argc, char *argv[]) { - // Initialize the Stack int size = SIZE; int current = 0; diff --git a/stackslot.c b/stackslot.c index 95380b2..937ecb9 100644 --- a/stackslot.c +++ b/stackslot.c @@ -1,57 +1,65 @@ #include #include #include + #ifndef STACKSLOT #define STACKSLOT typedef int Object; typedef struct { - unsigned int size; - unsigned char data[1]; + bool brokenHeart; + unsigned int size; + unsigned char data[1]; } *ObjRef; -typedef struct{ - bool isObjRef; - union { - ObjRef objRef; - int number; - } u; +typedef struct { + bool isObjRef; + union { + ObjRef objRef; + int number; + } u; } StackSlot; -ObjRef getIntObj(int val){ - ObjRef intObject; - unsigned int objSize = sizeof(unsigned int) + sizeof(int); - if ((intObject = malloc(objSize)) == NULL) { - perror("malloc"); - } - *(int *)intObject->data=val; - intObject->size=sizeof(int); - return intObject; +ObjRef getIntObj(int val) { + ObjRef intObject; + unsigned int objSize = sizeof(unsigned int) + sizeof(int); + if ((intObject = malloc(objSize)) == NULL) { + perror("malloc"); + } + *(int *) intObject->data = val; + intObject->size = sizeof(int); + return intObject; } -int getValFromIntObj(ObjRef iref){ - return *(int *)iref->data; + +int getValFromIntObj(ObjRef iref) { + return *(int *) iref->data; } -int getIntValfromStackSlot(StackSlot s){ - if(s.isObjRef){ - return *(int *)s.u.objRef->data; - } - return s.u.number; + +int getIntValfromStackSlot(StackSlot s) { + if (s.isObjRef) { + return *(int *) s.u.objRef->data; + } + return s.u.number; } -void setValIntObj(ObjRef iref, int val){ - iref->size=sizeof(int); - *(int *)iref->data=val; + +void setValIntObj(ObjRef iref, int val) { + iref->size = sizeof(int); + *(int *) iref->data = val; } -StackSlot stackSlotWithObjRef(ObjRef val){ - StackSlot *stackSlot; - stackSlot=malloc(sizeof(StackSlot)); - stackSlot->isObjRef=true; - stackSlot->u.objRef=val; - return *stackSlot; + +StackSlot stackSlotWithObjRef(ObjRef val) { + StackSlot *stackSlot; + stackSlot = malloc(sizeof(StackSlot)); + stackSlot->isObjRef = true; + stackSlot->u.objRef = val; + return *stackSlot; } -StackSlot stackSlotWitchNumber(int val){ - StackSlot *stackSlot; - stackSlot= malloc(sizeof(StackSlot)); - stackSlot->isObjRef=false; - stackSlot->u.number=val; - return *stackSlot; + +StackSlot stackSlotWitchNumber(int val) { + StackSlot *stackSlot; + stackSlot = malloc(sizeof(StackSlot)); + stackSlot->isObjRef = false; + stackSlot->u.number = val; + return *stackSlot; } + #endif