diff --git a/CMakeLists.txt b/CMakeLists.txt index d44b8df..c3d3d4a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,8 @@ set(CMAKE_C_STANDARD 99) add_compile_options(-g -Wall -pedantic) -add_executable(njvm njvm.c) +add_executable(njvm njvm.c + njvm.h) # Include directories for njvm executable target_include_directories(njvm PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/bigint/build/include) @@ -13,7 +14,9 @@ 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 - GC.c) + GC.c + heap.c + heap.h) # Include directories for the bigint library target_include_directories(bigint PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/bigint/build/include) diff --git a/bigint/build/bin/testbip b/bigint/build/bin/testbip index 90a0633..a22dcfb 100755 Binary files a/bigint/build/bin/testbip and b/bigint/build/bin/testbip differ diff --git a/bigint/build/lib/libbigint.a b/bigint/build/lib/libbigint.a index 77e4227..ca54dc3 100644 Binary files a/bigint/build/lib/libbigint.a and b/bigint/build/lib/libbigint.a differ diff --git a/bigint/src/bigint.o b/bigint/src/bigint.o index 4e68f95..162ad9f 100644 Binary files a/bigint/src/bigint.o and b/bigint/src/bigint.o differ diff --git a/bigint/src/libbigint.a b/bigint/src/libbigint.a index 77e4227..ca54dc3 100644 Binary files a/bigint/src/libbigint.a and b/bigint/src/libbigint.a differ diff --git a/bigint/tst/support.o b/bigint/tst/support.o index f5bd62d..7f7b6da 100644 Binary files a/bigint/tst/support.o and b/bigint/tst/support.o differ diff --git a/bigint/tst/testbip b/bigint/tst/testbip index 90a0633..a22dcfb 100755 Binary files a/bigint/tst/testbip and b/bigint/tst/testbip differ diff --git a/bigint/tst/testbip.o b/bigint/tst/testbip.o index 8acdffd..d8aaf39 100644 Binary files a/bigint/tst/testbip.o and b/bigint/tst/testbip.o differ diff --git a/heap.c b/heap.c new file mode 100644 index 0000000..866c867 --- /dev/null +++ b/heap.c @@ -0,0 +1,33 @@ +// +// Created by Elias Bennour on 28.01.24. +// + +#ifndef HEAP +#define HEAP + +#include +#include + +int maxHeapSize = 8192 * 1024; + +void* my_malloc(size_t size) { + static size_t total_allocated = 0; + + if (total_allocated + size > maxHeapSize) { + perror("Memory limit exceeded\n"); + exit(1); + } + + void* ptr = malloc(size); + if (ptr != NULL) { + total_allocated += size; + } + + return ptr; +} + +void initHeap(int size) { + maxHeapSize = size; +} + +#endif //NINJA_NJVM_H \ No newline at end of file diff --git a/heap.h b/heap.h new file mode 100644 index 0000000..3c0475a --- /dev/null +++ b/heap.h @@ -0,0 +1,12 @@ +// +// Created by Elias Bennour on 28.01.24. +// + +#ifndef NINJA_HEAP_H +#define NINJA_HEAP_H + +#include + +void* my_malloc(size_t size); + +#endif //NINJA_HEAP_H diff --git a/njvm.c b/njvm.c index d99c634..8fbcaad 100644 --- a/njvm.c +++ b/njvm.c @@ -1,3 +1,6 @@ +#ifndef NJVM +#define NJVM + #include #include #include @@ -9,6 +12,7 @@ #include "bigint.h" #include "record.c" #include "GC.c" +#include "heap.c" // Debug int debug = 0; @@ -16,7 +20,6 @@ int debug = 0; // Program struct program program; - unsigned fp; void version(void) { @@ -306,8 +309,6 @@ void tests(void) { } int main(int argc, char *argv[]) { - - // Initialize the Stack int size = SIZE; int current = 0; @@ -352,7 +353,7 @@ int main(int argc, char *argv[]) { // TODO: implement stack size } else if (strcmp(argv[i], "--heap") == 0) { i++; - // TODO: implement heap size + initHeap(atoi(argv[i]) * 1024); } else if (strcmp(argv[i], "--gcpurge") == 0) { // TODO: implement gcpurge } else { @@ -379,3 +380,5 @@ int main(int argc, char *argv[]) { return 1; } } + +#endif /* ifndef NJVM */ \ No newline at end of file diff --git a/njvm.o b/njvm.o index 9e0d7e2..7e3b2ac 100644 Binary files a/njvm.o and b/njvm.o differ diff --git a/prog.bin b/prog.bin index a12da6a..4f45bc1 100644 Binary files a/prog.bin and b/prog.bin differ diff --git a/record.c b/record.c index e67ca85..b6d4e7a 100644 --- a/record.c +++ b/record.c @@ -9,7 +9,7 @@ ObjRef newRecord(int size){ ObjRef record; unsigned int objSize; objSize = sizeof(*record) + (size * sizeof(void *)); - if((record = malloc(objSize)) == NULL){ + if((record = my_malloc(objSize)) == NULL){ perror("malloc"); } record->size = MSB; @@ -47,7 +47,7 @@ void setField(ObjRef arr, int point, ObjRef value){ else size = sizeof(*value) + (GET_ELEMENT_COUNT(value) * sizeof(void *)); } - if((GET_REFS_PTR(arr)[point] = malloc(size)) == NULL) perror("malloc"); + if((GET_REFS_PTR(arr)[point] = my_malloc(size)) == NULL) perror("malloc"); GET_REFS_PTR(arr)[point] ->size = size; * (ObjRef *)GET_REFS_PTR(arr)[point]->data = value; } diff --git a/stackslot.c b/stackslot.c index 8e59ee6..77efcc2 100644 --- a/stackslot.c +++ b/stackslot.c @@ -4,6 +4,7 @@ #include #include #include "objref.c" +#include "heap.h" typedef int Object; @@ -19,7 +20,7 @@ typedef struct { ObjRef getIntObj(int val) { ObjRef intObject; unsigned int objSize = sizeof(ObjRef) + sizeof(int); - if ((intObject = malloc(objSize)) == NULL) { + if ((intObject = my_malloc(objSize)) == NULL) { perror("malloc"); } *(int *) intObject->data = val; @@ -47,7 +48,7 @@ void setValIntObj(ObjRef iref, int val) { StackSlot stackSlotWithObjRef(ObjRef val) { StackSlot *stackSlot; - stackSlot = malloc(sizeof(StackSlot)); + stackSlot = my_malloc(sizeof(StackSlot)); if(stackSlot == NULL) perror("malloc"); stackSlot->isObjRef = true; stackSlot->u.objRef = val; @@ -56,7 +57,7 @@ StackSlot stackSlotWithObjRef(ObjRef val) { StackSlot stackSlotWitchNumber(int val) { StackSlot *stackSlot; - stackSlot = malloc(sizeof(StackSlot)); + stackSlot = my_malloc(sizeof(StackSlot)); if(stackSlot == NULL) perror("malloc"); stackSlot->isObjRef = false; stackSlot->u.number = val; diff --git a/support.c b/support.c index cdd2d80..7a56f51 100644 --- a/support.c +++ b/support.c @@ -1,9 +1,9 @@ - #include "support.h" #include #include #include "objref.c" +#include "heap.h" void fatalError(char *msg){ printf("Fatal error: %s\n", msg); @@ -13,7 +13,7 @@ void fatalError(char *msg){ void * newPrimObject(int dataSize) { ObjRef bigObjRef; - bigObjRef = malloc(sizeof(unsigned int) + + bigObjRef = my_malloc(sizeof(unsigned int) + dataSize * sizeof(unsigned char)); if (bigObjRef == NULL) { fatalError("newPrimObject() got no memory"); diff --git a/support.o b/support.o index e0fc0d2..76449c9 100644 Binary files a/support.o and b/support.o differ