diff --git a/src/GC/Makefile b/src/GC/Makefile index add6d73..6b33ca8 100644 --- a/src/GC/Makefile +++ b/src/GC/Makefile @@ -40,6 +40,17 @@ game: rm -f tests/game.out $(CC) $(WFLAGS) $(STDFLAGS) $(LIB_INCL) tests/game.cpp lib/heap.cpp lib/profiler.cpp lib/event.cpp -o tests/game.out +wrapper_test: + rm -f lib/event.o lib/profiler.o lib/heap.o lib/coll.a tests/wrapper_test.out +# compile object files + $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -g -c -o lib/event.o lib/event.cpp -fPIC + $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -g -c -o lib/profiler.o lib/profiler.cpp -fPIC + $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -g -c -o lib/heap.o lib/heap.cpp -fPIC + $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -g -c -o lib/cheap.o lib/cheap.cpp -fPIC +# compile object files into library + ar rcs lib/gcoll.a lib/event.o lib/profiler.o lib/heap.o lib/cheap.o + clang -stdlib=libc++ $(WFLAGS) $(LIB_INCL) -o tests/wrapper_test.out tests/wrapper_test.c lib/gcoll.a -lstdc++ + extern_lib: # remove old files rm -f lib/heap.o lib/libheap.so tests/extern_lib.out @@ -55,12 +66,25 @@ static_lib: # remove old files rm -f lib/event.o lib/profiler.o lib/heap.o lib/gcoll.a tests/extern_lib.out # compile object files - $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -g -c -o lib/event.o lib/event.cpp -fPIC - $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -g -c -o lib/profiler.o lib/profiler.cpp -fPIC - $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -g -c -o lib/heap.o lib/heap.cpp -fPIC + $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -c -o lib/event.o lib/event.cpp -fPIC + $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -c -o lib/profiler.o lib/profiler.cpp -fPIC + $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -c -o lib/heap.o lib/heap.cpp -fPIC # create static library ar r lib/gcoll.a lib/event.o lib/profiler.o lib/heap.o # create test program static_lib_test: static_lib - $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -o tests/extern_lib.out tests/extern_lib.cpp lib/gcoll.a \ No newline at end of file + $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -o tests/extern_lib.out tests/extern_lib.cpp lib/gcoll.a + +wrapper: +# remove old files + rm -f lib/event.o lib/profiler.o lib/heap.o lib/coll.a tests/wrapper.out +# compile object files + $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -c -o lib/event.o lib/event.cpp -fPIC + $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -c -o lib/profiler.o lib/profiler.cpp -fPIC + $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -c -o lib/heap.o lib/heap.cpp -fPIC + $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -c -o lib/cheap.o lib/cheap.cpp -fPIC +# compile object files into library + ar rcs lib/gcoll.a lib/event.o lib/profiler.o lib/heap.o lib/cheap.o +# compile test program wrapper.c with normal clang + clang -stdlib=libc++ $(WFLAGS) $(LIB_INCL) -o tests/wrapper.out tests/wrapper.c lib/gcoll.a -lstdc++ diff --git a/src/GC/docs/lib/cheap.md b/src/GC/docs/lib/cheap.md new file mode 100644 index 0000000..e5c5993 --- /dev/null +++ b/src/GC/docs/lib/cheap.md @@ -0,0 +1,40 @@ +# cheap.h & cheap.cpp + +A wrapper interface for the class `GC::Heap` for easier use +in LLVM (no nasty namespaces). This interface is relatively +straight-forward and only defines functions to use the already +public functions in the class `GC::Heap`. + +The functions are declared in a normal C-style header and +defined as "pure" C-functions. Because the public functions +exposed in `GC::Heap` are static, some of the functions +just call the static functions but are wrapped as C-functions. + +For the non-static function `GC::Heap::set_profiler()` and the +singleton get-instance function `GC::Heap::the()` a struct +is used to encapsulate the heap-object. If this library is +compiled with `DEBUG` defined a struct is typedef-ed and +can be used everywhere, otherwise this struct is opaque +and cannot be used explicitly. This struct only contains +a pointer to the heap instance and is called `cheap_t`. + +## Functions +`cheap_t *cheap_the()`: Returns an encapsulated singleton +instance. It is encapsulated in an opaque struct as the +instance itself is not meant to be used outside the C++ +library. + +`void cheap_init()`: Simply calls the `Heap::init()` +function. + +`void cheap_dispose()`: Only calls the `Heap::dispose()` +function. + +`void *cheap_alloc(unsigned long size)`: Calls `Heap::alloc(size_t size)` +and returns whatever `alloc` returns. + +`void cheap_set_profiler(cheap_t *cheap, bool mode)`: +The argument `cheap` is the encapsulated Heap singleton instance. +`mode` is the same as for `Heap::set_profiler(bool mode)`. + +For more documentation on functionality, see `src/GC/docs/lib/heap.md`. \ No newline at end of file diff --git a/src/GC/include/cheap.h b/src/GC/include/cheap.h new file mode 100644 index 0000000..d2c649d --- /dev/null +++ b/src/GC/include/cheap.h @@ -0,0 +1,32 @@ +#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(cheap_t *cheap, bool mode); + +#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 new file mode 100644 index 0000000..29a0b10 --- /dev/null +++ b/src/GC/lib/cheap.cpp @@ -0,0 +1,48 @@ +#include +#include + +#include "heap.hpp" +#include "cheap.h" + +#ifndef DEBUG +struct cheap +{ + void *obj; +}; +#endif + +cheap_t *cheap_the() +{ + cheap_t *c; + GC::Heap *heap; + + c = static_cast(malloc(sizeof(cheap_t))); + heap = &GC::Heap::the(); + c->obj = heap; + + return c; +} + +void cheap_init() +{ + GC::Heap::init(); +} + +void cheap_dispose() +{ + std::cout << "In dispose\n"; + GC::Heap::dispose(); + std::cout << "Out dispose" << std::endl; +} + +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/lib/event.cpp b/src/GC/lib/event.cpp index 2815a77..185c613 100644 --- a/src/GC/lib/event.cpp +++ b/src/GC/lib/event.cpp @@ -1,6 +1,6 @@ -#include -#include -#include +// #include +// #include +// #include #include "chunk.hpp" #include "event.hpp" diff --git a/src/GC/lib/gcoll.a b/src/GC/lib/gcoll.a index 1fec9e8..dd34d64 100644 Binary files a/src/GC/lib/gcoll.a and b/src/GC/lib/gcoll.a differ diff --git a/src/GC/tests/file.cpp b/src/GC/tests/file.cpp index df9e441..f4a0373 100644 --- a/src/GC/tests/file.cpp +++ b/src/GC/tests/file.cpp @@ -8,6 +8,7 @@ void time_string(char *buffer); void print_log_file(const std::string TESTS_PATH); void readlink_test(); +void null_test(); int main() { @@ -17,7 +18,9 @@ int main() // const std::string TESTS_PATH = "/home/virre/dev/systemF/org/language/src/GC/tests/"; // print_log_file(TESTS_PATH); - readlink_test(); + // readlink_test(); + + null_test(); return 0; } @@ -65,4 +68,10 @@ void readlink_test() 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..bcd4859 --- /dev/null +++ b/src/GC/tests/wrapper.c @@ -0,0 +1,95 @@ +#include +#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"); +} + +/* Uncomment ONLY if run with DEBUG defined in cheap.h */ + +// 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() +{ + test_init(); + + /* Uncomment ONLY if run with DEBUG defined in cheap.h */ + // cheap_t *heap = test_the(); + // test_profiler(heap); + + Object *o = test_alloc(); + printf("Object size: %lu\n", sizeof(Object)); + 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(); + + /* Sefault I don't understand, don't uncomment */ + // free(heap); + // free(o); + return 0; +} \ No newline at end of file diff --git a/src/GC/tests/wrapper_test.c b/src/GC/tests/wrapper_test.c new file mode 100644 index 0000000..729cf69 --- /dev/null +++ b/src/GC/tests/wrapper_test.c @@ -0,0 +1,45 @@ +#include +#include + +#include "cheap.h" + +typedef struct node { + int id; + struct node *child; +} Node; + +// Global variables make the test less complex +Node *HEAD = NULL; +Node *CURRENT = NULL; + +// Creates a linked list of length depth. Global head "HEAD" is updated. +void *create_linked_list(int depth) { + HEAD = (Node*)(cheap_alloc(sizeof(Node))); + HEAD->id = 0; + // Purposely omitting adding a child to "last_node", since its the last node + for (int i = 1; i < depth - 1; i++) { + insert_first(i); + } +} + +void *insert_first(int node_id) { + Node *new_head; + new_head = (Node*)(cheap_alloc(sizeof(Node))); + new_head->id = node_id; + new_head->child = HEAD; + + HEAD = new_head; +} + +void test_linked_list(int list_length){ + cheap_init(); + cheap_t *heap = cheap_the(); + cheap_set_profiler(heap, true); + create_linked_list(list_length); + cheap_dispose(); + free(heap); +} + +int main (int argc, char **argv) { + test_linked_list(30); +} \ No newline at end of file