Compare commits

...

6 Commits

Author SHA1 Message Date
32346f8a28 +++ 2024-01-28 23:21:55 +01:00
240e93a90f +++ 2024-01-28 23:20:45 +01:00
bc41dcda4a --- 2024-01-28 23:15:34 +01:00
f62030772a ,,, 2024-01-28 23:10:28 +01:00
43663714ca ... 2024-01-28 23:06:11 +01:00
38bfd5c7d9 add heap size 2024-01-28 23:02:03 +01:00
17 changed files with 65 additions and 13 deletions

View File

@ -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)

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.

33
heap.c Normal file
View File

@ -0,0 +1,33 @@
//
// Created by Elias Bennour on 28.01.24.
//
#ifndef HEAP
#define HEAP
#include <stdlib.h>
#include <stdio.h>
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

12
heap.h Normal file
View File

@ -0,0 +1,12 @@
//
// Created by Elias Bennour on 28.01.24.
//
#ifndef NINJA_HEAP_H
#define NINJA_HEAP_H
#include <stdlib.h>
void* my_malloc(size_t size);
#endif //NINJA_HEAP_H

11
njvm.c
View File

@ -1,3 +1,6 @@
#ifndef NJVM
#define NJVM
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -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 */

BIN
njvm.o

Binary file not shown.

BIN
prog.bin

Binary file not shown.

View File

@ -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;
}

View File

@ -4,6 +4,7 @@
#include <stdio.h>
#include <stdlib.h>
#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;

View File

@ -1,9 +1,9 @@
#include "support.h"
#include <stdio.h>
#include <stdlib.h>
#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");

BIN
support.o

Binary file not shown.