From 35b2aa4a2f8fc64fca44c0811fe232ded48ded37 Mon Sep 17 00:00:00 2001 From: Victor Olin Date: Fri, 24 Mar 2023 13:41:14 +0100 Subject: [PATCH] Testing works, stack-overflow fixed --- src/GC/Makefile | 8 ++++---- src/GC/include/heap.hpp | 2 +- src/GC/lib/heap.cpp | 16 ++++++++-------- src/GC/lib/profiler.cpp | 4 ++-- src/GC/tests/h_test.cpp | 24 +++++++++++++++++++----- 5 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/GC/Makefile b/src/GC/Makefile index 393b627..add6d73 100644 --- a/src/GC/Makefile +++ b/src/GC/Makefile @@ -21,7 +21,7 @@ heap: h_test: rm -f tests/h_test.out # $(CC) $(WFLAGS) $(STDFLAGS) $(LIB_INCL) tests/h_test.cpp lib/heap.cpp lib/profiler.cpp lib/event.cpp -o tests/h_test.out - $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -o tests/h_test.out tests/h_test.cpp lib/gcoll.a + $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -g -o tests/h_test.out tests/h_test.cpp lib/gcoll.a h_test_vg: h_test valgrind $(VGFLAGS) tests/h_test.out @@ -55,9 +55,9 @@ 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) -c -o lib/event.o lib/event.cpp -fPIC - $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -c -o lib/profiler.o lib/profiler.cpp -fPIC - $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -c -o lib/heap.o lib/heap.cpp -fPIC + $(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 # create static library ar r lib/gcoll.a lib/event.o lib/profiler.o lib/heap.o diff --git a/src/GC/include/heap.hpp b/src/GC/include/heap.hpp index c373d3c..365a838 100644 --- a/src/GC/include/heap.hpp +++ b/src/GC/include/heap.hpp @@ -55,7 +55,7 @@ namespace GC std::vector m_freed_chunks; static bool profiler_enabled(); - static Chunk *get_at(std::vector &list, size_t n); + // static Chunk *get_at(std::vector &list, size_t n); void collect(); void sweep(Heap &heap); Chunk *try_recycle_chunks(size_t size); diff --git a/src/GC/lib/heap.cpp b/src/GC/lib/heap.cpp index 325443c..579f421 100644 --- a/src/GC/lib/heap.cpp +++ b/src/GC/lib/heap.cpp @@ -169,14 +169,14 @@ namespace GC * * @returns The pointer to the chunk at position n in list. */ - Chunk *Heap::get_at(std::vector &list, size_t n) - { - auto iter = list.begin(); - if (!n) - return *iter; - std::advance(iter, n); - return *iter; - } + // Chunk *Heap::get_at(std::vector &list, size_t n) + // { + // auto iter = list.begin(); + // if (!n) + // return *iter; + // std::advance(iter, n); + // return *iter; + // } /** * Returns a bool whether the profiler is enabled diff --git a/src/GC/lib/profiler.cpp b/src/GC/lib/profiler.cpp index 5d9a216..29abad4 100644 --- a/src/GC/lib/profiler.cpp +++ b/src/GC/lib/profiler.cpp @@ -11,7 +11,7 @@ #include "event.hpp" #include "profiler.hpp" -#define MAC_OS +// #define MAC_OS namespace GC { @@ -186,7 +186,7 @@ namespace GC std::string folder = path.substr(0, last_slash); #else auto folder = std::string("/Users/valtermiari/Desktop/DV/Bachelors/code/language/src/GC/tests"); - return folder + "/logs"; #endif + return folder + "/logs"; } } \ No newline at end of file diff --git a/src/GC/tests/h_test.cpp b/src/GC/tests/h_test.cpp index 2428383..c871721 100644 --- a/src/GC/tests/h_test.cpp +++ b/src/GC/tests/h_test.cpp @@ -1,11 +1,17 @@ +#include +#include + #include "heap.hpp" +using std::cout, std::endl; + struct Node { int id; Node *child; }; Node *create_chain(int depth) { + cout << "entering create_chain"; std::vector nodes; if (depth > 0) { Node *last_node = static_cast(GC::Heap::alloc(sizeof(Node))); @@ -18,6 +24,7 @@ Node *create_chain(int depth) { node->child = nodes[i]; nodes.push_back(node); } + cout << "\nexiting create_chain" << endl; return nodes[depth]; } else @@ -29,21 +36,26 @@ void create_array(size_t size) { } void detach_pointer(long **ptr) { + cout << "entering detach_pointer"; long *dummy_ptr = nullptr; *ptr = dummy_ptr; + cout << "\nexiting detach_pointer" << endl; } Node *test_chain(int depth, bool detach) { + cout << "entering test_chain"; auto stack_start = reinterpret_cast(__builtin_frame_address(0)); Node *node_chain = create_chain(depth); if (detach) node_chain->child = nullptr; - return node_chain; + cout << "\nexiting test_chain" << endl; + return node_chain; } void test_some_types() { + cout << "entering test_some_types" << endl; auto stack_start = reinterpret_cast(__builtin_frame_address(0)); std::cout << "Stack start from test_some_types:\t" << stack_start << std::endl; @@ -56,9 +68,11 @@ void test_some_types() { int *i = static_cast(GC::Heap::alloc(sizeof(int))); char *c = static_cast(GC::Heap::alloc(sizeof(int))); short *s = static_cast(GC::Heap::alloc(sizeof(short))); + cout << "exiting test_some_types" << endl; } int main() { + cout << "entering main" << endl; using namespace std::literals; auto start = std::chrono::high_resolution_clock::now(); @@ -70,9 +84,9 @@ int main() { auto stack_start = reinterpret_cast(__builtin_frame_address(0)); Node *root1 = static_cast(gc.alloc(sizeof(Node))); - //Node *root2 = static_cast(gc.alloc(sizeof(Node))); - root1 = test_chain(60000, false); - //root2 = test_chain(50000, true); + Node *root2 = static_cast(gc.alloc(sizeof(Node))); + root1 = test_chain(58000, false); + root2 = test_chain(58000, false); gc.collect(GC::COLLECT_ALL); @@ -80,7 +94,7 @@ int main() { //std::cout << "Value of end: " << end.time_since_epoch().count() << std::endl; gc.print_summary(); - //gc.dispose(); + gc.dispose(); std::cout << "Execution time: "