removed gc

This commit is contained in:
Sven Vogel 2024-05-08 22:10:14 +02:00
parent eac3b23432
commit efeb482128
5 changed files with 32 additions and 182 deletions

View File

@ -1,113 +0,0 @@
//
// Created by servostar on 5/7/24.
//
#include <gc/gc.h>
#include <sys/log.h>
#include <string.h>
#undef malloc
#undef free
#undef realloc
#define GC_HEAP_PREALLOC_CNT 10
typedef struct GC_Heap_Statistics_t {
size_t bytes;
} GC_Heap_Statistics;
static struct GC_Heap_t {
RAW_PTR* blocks;
size_t cap;
size_t len;
GC_Heap_Statistics statistics;
} GC_GLOBAL_HEAP;
static void __GC_teardown(void) {
INFO("Used %ld bytes in total", GC_GLOBAL_HEAP.statistics.bytes);
if (GC_GLOBAL_HEAP.blocks == NULL) {
return;
}
for (size_t i = 0; i < GC_GLOBAL_HEAP.len; i++) {
free(GC_GLOBAL_HEAP.blocks[i]);
}
free(GC_GLOBAL_HEAP.blocks);
GC_GLOBAL_HEAP.blocks = NULL;
}
static void __GC_check_init(void) {
if (GC_GLOBAL_HEAP.blocks == NULL) {
GC_GLOBAL_HEAP.cap = GC_HEAP_PREALLOC_CNT;
const size_t bytes = sizeof(RAW_PTR) * GC_GLOBAL_HEAP.cap;
GC_GLOBAL_HEAP.blocks = malloc(bytes);
GC_GLOBAL_HEAP.len = 0;
}
}
void GC_init(void) {
atexit(__GC_teardown);
}
static void __GC_check_overflow(void) {
if (GC_GLOBAL_HEAP.len >= GC_GLOBAL_HEAP.cap) {
GC_GLOBAL_HEAP.cap += GC_HEAP_PREALLOC_CNT;
const size_t bytes = sizeof(RAW_PTR) * GC_GLOBAL_HEAP.cap;
GC_GLOBAL_HEAP.blocks = realloc(GC_GLOBAL_HEAP.blocks, bytes);
}
}
RAW_PTR GC_malloc(const size_t bytes) {
const RAW_PTR ptr = malloc(bytes);
if (ptr == NULL) {
FATAL("unable to allocate memory");
}
__GC_check_init();
__GC_check_overflow();
GC_GLOBAL_HEAP.blocks[GC_GLOBAL_HEAP.len++] = ptr;
GC_GLOBAL_HEAP.statistics.bytes += bytes;
return ptr;
}
static void __GC_swap_ptr(RAW_PTR old, RAW_PTR new) {
for (size_t i = 0; i < GC_GLOBAL_HEAP.len; i++) {
if (GC_GLOBAL_HEAP.blocks[i] == old) {
GC_GLOBAL_HEAP.blocks[i] = new;
}
}
}
RAW_PTR GC_realloc(RAW_PTR ptr, size_t bytes) {
const RAW_PTR new_ptr = (RAW_PTR) realloc(ptr, bytes);
if (new_ptr == NULL) {
FATAL("unable to reallocate memory");
}
__GC_swap_ptr(ptr, new_ptr);
return new_ptr;
}
void GC_free(RAW_PTR ptr) {
DEBUG("freeing memory: %p", ptr);
for (size_t i = 0; i < GC_GLOBAL_HEAP.len; i++) {
if (ptr == GC_GLOBAL_HEAP.blocks[i]) {
free(GC_GLOBAL_HEAP.blocks[i]);
GC_GLOBAL_HEAP.len--;
memcpy(&GC_GLOBAL_HEAP.blocks[i], &GC_GLOBAL_HEAP.blocks[i + 1], GC_GLOBAL_HEAP.len - i);
}
}
}

View File

@ -1,24 +0,0 @@
//
// Created by servostar on 5/7/24.
//
#ifndef GC_H
#define GC_H
#include <stdlib.h>
#define malloc GC_malloc
#define free GC_free
#define realloc GC_realloc
typedef void* RAW_PTR;
void GC_init(void);
RAW_PTR GC_malloc(size_t bytes);
RAW_PTR GC_realloc(RAW_PTR ptr, size_t bytes);
void GC_free(RAW_PTR ptr);
#endif //GC_H

View File

@ -2,7 +2,6 @@
%{ %{
#include <yacc/parser.tab.h> #include <yacc/parser.tab.h>
#include <sys/log.h> #include <sys/log.h>
#include <gc/gc.h>
int yyLineNumber = 1; int yyLineNumber = 1;
int yylex(); int yylex();

View File

@ -1,8 +1,7 @@
#include <ast/ast.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/log.h> #include <sys/log.h>
#include <yacc/parser.tab.h> #include <yacc/parser.tab.h>
#include <ast/ast.h>
#include <gc/gc.h>
#define LOG_LEVEL LOG_LEVEL_DEBUG #define LOG_LEVEL LOG_LEVEL_DEBUG
@ -12,20 +11,15 @@ extern FILE* yyin;
* @brief Log a debug message to inform about beginning exit procedures * @brief Log a debug message to inform about beginning exit procedures
* *
*/ */
void notify_exit(void) void notify_exit(void) { DEBUG("Exiting gemstone..."); }
{
DEBUG("Exiting gemstone...");
}
/** /**
* @brief Closes File after compiling. * @brief Closes File after compiling.
* *
*/ */
void close_file(void) void close_file(void) {
{ if (NULL != yyin) {
if (NULL != yyin)
{
fclose(yyin); fclose(yyin);
} }
} }
@ -34,8 +28,7 @@ void close_file(void)
* @brief Run compiler setup here * @brief Run compiler setup here
* *
*/ */
void setup(void) void setup(void) {
{
// setup preample // setup preample
log_init(); log_init();
@ -48,8 +41,6 @@ void setup(void)
// actual setup // actual setup
AST_init(); AST_init();
GC_init();
DEBUG("finished starting up gemstone..."); DEBUG("finished starting up gemstone...");
} }
@ -59,8 +50,7 @@ int main(int argc, char *argv[]) {
atexit(close_file); atexit(close_file);
// Check for file input as argument // Check for file input as argument
if (2 != argc) if (2 != argc) {
{
INFO("Usage: %s <filename>\n", argv[0]); INFO("Usage: %s <filename>\n", argv[0]);
PANIC("No File could be found"); PANIC("No File could be found");
} }
@ -70,8 +60,7 @@ int main(int argc, char *argv[]) {
FILE *file = fopen(filename, "r"); FILE *file = fopen(filename, "r");
if (NULL == file) if (NULL == file) {
{
PANIC("File couldn't be opened!"); PANIC("File couldn't be opened!");
} }
yyin = file; yyin = file;

View File

@ -2,7 +2,6 @@
#define _SYS_ERR_H_ #define _SYS_ERR_H_
#include <stdio.h> #include <stdio.h>
#include <gc/gc.h>
#define LOG_DEFAULT_STREAM stderr #define LOG_DEFAULT_STREAM stderr