Compare commits

..

No commits in common. "32346f8a282b90add1500d38075f3dd0c66e3b86" and "f34817422933fb01711261b99f055cd3c631bec5" have entirely different histories.

17 changed files with 13 additions and 65 deletions

View File

@ -5,8 +5,7 @@ set(CMAKE_C_STANDARD 99)
add_compile_options(-g -Wall -pedantic)
add_executable(njvm njvm.c
njvm.h)
add_executable(njvm njvm.c)
# Include directories for njvm executable
target_include_directories(njvm PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/bigint/build/include)
@ -14,9 +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
GC.c
heap.c
heap.h)
GC.c)
# 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
View File

@ -1,33 +0,0 @@
//
// 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
View File

@ -1,12 +0,0 @@
//
// 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,6 +1,3 @@
#ifndef NJVM
#define NJVM
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -12,7 +9,6 @@
#include "bigint.h"
#include "record.c"
#include "GC.c"
#include "heap.c"
// Debug
int debug = 0;
@ -20,6 +16,7 @@ int debug = 0;
// Program
struct program program;
unsigned fp;
void version(void) {
@ -309,6 +306,8 @@ void tests(void) {
}
int main(int argc, char *argv[]) {
// Initialize the Stack
int size = SIZE;
int current = 0;
@ -353,7 +352,7 @@ int main(int argc, char *argv[]) {
// TODO: implement stack size
} else if (strcmp(argv[i], "--heap") == 0) {
i++;
initHeap(atoi(argv[i]) * 1024);
// TODO: implement heap size
} else if (strcmp(argv[i], "--gcpurge") == 0) {
// TODO: implement gcpurge
} else {
@ -380,5 +379,3 @@ 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 = my_malloc(objSize)) == NULL){
if((record = 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] = my_malloc(size)) == NULL) perror("malloc");
if((GET_REFS_PTR(arr)[point] = malloc(size)) == NULL) perror("malloc");
GET_REFS_PTR(arr)[point] ->size = size;
* (ObjRef *)GET_REFS_PTR(arr)[point]->data = value;
}

View File

@ -4,7 +4,6 @@
#include <stdio.h>
#include <stdlib.h>
#include "objref.c"
#include "heap.h"
typedef int Object;
@ -20,7 +19,7 @@ typedef struct {
ObjRef getIntObj(int val) {
ObjRef intObject;
unsigned int objSize = sizeof(ObjRef) + sizeof(int);
if ((intObject = my_malloc(objSize)) == NULL) {
if ((intObject = malloc(objSize)) == NULL) {
perror("malloc");
}
*(int *) intObject->data = val;
@ -48,7 +47,7 @@ void setValIntObj(ObjRef iref, int val) {
StackSlot stackSlotWithObjRef(ObjRef val) {
StackSlot *stackSlot;
stackSlot = my_malloc(sizeof(StackSlot));
stackSlot = malloc(sizeof(StackSlot));
if(stackSlot == NULL) perror("malloc");
stackSlot->isObjRef = true;
stackSlot->u.objRef = val;
@ -57,7 +56,7 @@ StackSlot stackSlotWithObjRef(ObjRef val) {
StackSlot stackSlotWitchNumber(int val) {
StackSlot *stackSlot;
stackSlot = my_malloc(sizeof(StackSlot));
stackSlot = 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 = my_malloc(sizeof(unsigned int) +
bigObjRef = malloc(sizeof(unsigned int) +
dataSize * sizeof(unsigned char));
if (bigObjRef == NULL) {
fatalError("newPrimObject() got no memory");

BIN
support.o

Binary file not shown.