From 07bb53930b8b04cd8a16b76df6cf602a0b5cf6f9 Mon Sep 17 00:00:00 2001 From: Victor Olin Date: Wed, 29 Mar 2023 15:13:24 +0200 Subject: [PATCH] Wrapper for GC finished, untested --- src/GC/include/cheap.h | 26 ++++++++---- src/GC/lib/cheap.cpp | 30 +++++++++++--- src/GC/tests/file.cpp | 77 ++++++++++++++++++++++++++++++++++++ src/GC/tests/wrapper.c | 90 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 209 insertions(+), 14 deletions(-) create mode 100644 src/GC/tests/file.cpp create mode 100644 src/GC/tests/wrapper.c diff --git a/src/GC/include/cheap.h b/src/GC/include/cheap.h index 2db2b8d..1e11ae2 100644 --- a/src/GC/include/cheap.h +++ b/src/GC/include/cheap.h @@ -1,22 +1,32 @@ -#ifndef __CHEAP_H__ -#define __CHEAP_H__ +#ifndef CHEAP_H +#define CHEAP_H + +#include #ifdef __cplusplus extern "C" { #endif +#define DEBUG + +#ifdef DEBUG +typedef struct cheap +{ + void *obj; +} cheap_t; +#else struct cheap; typedef struct cheap cheap_t; +#endif -cheap_t *cheap_the(); -void cheap_init(); -void cheap_dispose(); -void *cheap_alloc(unsigned long size); -void cheap_set_profiler(bool mode); +cheap_t *cheap_the() noexcept; +void cheap_init() noexcept; +void cheap_dispose() noexcept; +void *cheap_alloc(unsigned long size) noexcept; +void cheap_set_profiler(cheap_t *cheap, bool mode) noexcept; #ifdef __cplusplus } #endif - #endif /* __CHEAP_H__ */ \ No newline at end of file diff --git a/src/GC/lib/cheap.cpp b/src/GC/lib/cheap.cpp index f9f7172..1643e17 100644 --- a/src/GC/lib/cheap.cpp +++ b/src/GC/lib/cheap.cpp @@ -1,25 +1,43 @@ -#pragma once - #include #include "heap.hpp" #include "cheap.h" -struct cheap { +struct cheap +{ void *obj; }; -cheap_t *cheap_the() { +cheap_t *cheap_the() +{ cheap_t *c; GC::Heap *heap; - c = (cheap_t *)malloc(sizeof(*c)); + c = static_cast(malloc(sizeof(cheap_t))); heap = &GC::Heap::the(); c->obj = heap; return c; } -void cheap_init() { +void cheap_init() +{ GC::Heap::init(); +} + +void cheap_dispose() +{ + GC::Heap::dispose(); +} + +void *cheap_alloc(unsigned long size) +{ + return GC::Heap::alloc(size); +} + +void cheap_set_profiler(cheap_t *cheap, bool mode) +{ + GC::Heap *heap = static_cast(cheap->obj); + + heap->set_profiler(mode); } \ No newline at end of file diff --git a/src/GC/tests/file.cpp b/src/GC/tests/file.cpp new file mode 100644 index 0000000..f4a0373 --- /dev/null +++ b/src/GC/tests/file.cpp @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include + +void time_string(char *buffer); +void print_log_file(const std::string TESTS_PATH); +void readlink_test(); +void null_test(); + +int main() +{ + // char time_buffer[31]; + // time_string(time_buffer); + + // const std::string TESTS_PATH = "/home/virre/dev/systemF/org/language/src/GC/tests/"; + // print_log_file(TESTS_PATH); + + // readlink_test(); + + null_test(); + + return 0; +} + +void time_string(char *const buffer) +{ + std::time_t tt = std::time(NULL); + std::tm *ptm = std::localtime(&tt); + std::strftime(buffer, 31, "/logs/log_%a_%H_%M_%S.txt", ptm); + std::cout << buffer << std::endl; +} + +void print_log_file(const std::string TESTS_PATH) +{ + std::string path = TESTS_PATH + "/testlog.txt"; + + std::ofstream testF(path); + + testF << "hellow york"; + + testF.close(); +} + +void readlink_test() +{ + char buffer[1024]; + ssize_t len = readlink("/proc/self/exe", buffer, sizeof(buffer)-1); + if (len == -1) + { + std::cout << "readlink error" << std::endl; + return; + } + + buffer[len] = '\0'; + std::cout << "readlink:\n" << "'''" << buffer << "'''"; // << std::endl; + + auto path = std::string(buffer); + std::cout << path << "\nlen: " << path.size() << "\ncap:" << path.capacity(); + + size_t last_slash = path.find_last_of('/'); + std::string folder = path.substr(0, last_slash); + + std::cout << "\n" << folder; + + std::string log_path = folder + "/log_file_bla.txt"; + std::cout << "\n" << log_path << std::endl; + +} + +void null_test() { + int *p = nullptr; + + std::cout << "P: " << nullptr << std::endl; +} \ No newline at end of file diff --git a/src/GC/tests/wrapper.c b/src/GC/tests/wrapper.c new file mode 100644 index 0000000..1a77472 --- /dev/null +++ b/src/GC/tests/wrapper.c @@ -0,0 +1,90 @@ +#include +#include + +#include "cheap.h" + +typedef struct object +{ + int x, y, z; + double velocity; +} Object; + +void test_init() +{ + printf("----- IN TEST_INIT ----------------------------\n"); + + cheap_init(); + + printf("----- EXIT TEST_INIT --------------------------\n"); +} + +cheap_t *test_the() +{ + printf("----- IN TEST_THE -----------------------------\n"); + + cheap_t *fst_heap = cheap_the(); + + printf("Heap 1:\t%p\n", fst_heap->obj); + + cheap_t *snd_heap = cheap_the(); + + printf("Heap 2:\t%p\n", snd_heap->obj); + + printf("----- EXIT TEST_THE ---------------------------\n"); + + free(snd_heap); + return fst_heap; +} + +void test_profiler(cheap_t *heap) +{ + printf("----- IN TEST_PROFILER ------------------------\n"); + + cheap_set_profiler(heap, false); + cheap_set_profiler(heap, true); + + printf("----- EXIT TEST_PROFILER ----------------------\n"); +} + +Object *test_alloc() +{ + printf("----- IN TEST_ALLOC ---------------------------\n"); + + Object *o; + o = (Object *)(cheap_alloc(sizeof(Object))); + + o->x = 3; + o->y = 4; + o->z = 5; + o->velocity = 1.0f; + + printf("----- EXIT TEST_ALLOC -------------------------\n"); + return o; +} + +void test_dispose() +{ + printf("----- IN TEST_DISPOSE -------------------------\n"); + + cheap_dispose(); + + printf("----- EXIT TEST_DISPOSE -----------------------\n"); +} + +int main(int argc, char **argv) +{ + test_init(); + + cheap_t *heap = test_the(); + + test_profiler(heap); + + Object *o = test_alloc(); + printf("Object:\n\tx: %d\n\ty: %d\n\tz: %d\n\tvel: %f\n", o->x, o->y, o->z, o->velocity); + + test_dispose(); + + free(heap); + free(o); + return 0; +} \ No newline at end of file