From 5e52de10bdd4da3488332bc9bd694f2795d5ee08 Mon Sep 17 00:00:00 2001 From: Victor Olin Date: Tue, 28 Feb 2023 16:01:39 +0100 Subject: [PATCH] Fixed broken tests --- .gitignore | 2 +- .vscode/settings.json | 77 ------------------------------------- src/GC/include/heap.hpp | 12 ++++-- src/GC/tests/alloc_free.cpp | 7 +++- src/GC/tests/extern_lib.cpp | 4 +- src/GC/tests/h_test.cpp | 21 +++++----- src/GC/tests/linker.cpp | 4 +- 7 files changed, 29 insertions(+), 98 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.gitignore b/.gitignore index 1daddd6..9f824a0 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,4 @@ llvm.ll src/GC/lib/*.o src/GC/lib/*.so -src/GC/tests/*.out +src/GC/tests/*.out \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 8057192..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,77 +0,0 @@ -{ - "files.associations": { - "array": "cpp", - "bitset": "cpp", - "string_view": "cpp", - "initializer_list": "cpp", - "ranges": "cpp", - "span": "cpp", - "utility": "cpp", - "__hash_table": "cpp", - "__split_buffer": "cpp", - "deque": "cpp", - "queue": "cpp", - "string": "cpp", - "unordered_map": "cpp", - "vector": "cpp", - "atomic": "cpp", - "bit": "cpp", - "*.tcc": "cpp", - "cctype": "cpp", - "charconv": "cpp", - "chrono": "cpp", - "clocale": "cpp", - "cmath": "cpp", - "compare": "cpp", - "concepts": "cpp", - "condition_variable": "cpp", - "cstdarg": "cpp", - "cstddef": "cpp", - "cstdint": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "cstring": "cpp", - "ctime": "cpp", - "cwchar": "cpp", - "cwctype": "cpp", - "exception": "cpp", - "algorithm": "cpp", - "functional": "cpp", - "iterator": "cpp", - "memory": "cpp", - "memory_resource": "cpp", - "numeric": "cpp", - "optional": "cpp", - "random": "cpp", - "ratio": "cpp", - "system_error": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "iosfwd": "cpp", - "iostream": "cpp", - "istream": "cpp", - "limits": "cpp", - "mutex": "cpp", - "new": "cpp", - "ostream": "cpp", - "sstream": "cpp", - "stdexcept": "cpp", - "stop_token": "cpp", - "streambuf": "cpp", - "thread": "cpp", - "typeinfo": "cpp", - "variant": "cpp", - "__bit_reference": "cpp", - "__config": "cpp", - "__debug": "cpp", - "__errc": "cpp", - "__locale": "cpp", - "__mutex_base": "cpp", - "__node_handle": "cpp", - "__threading_support": "cpp", - "__verbose_abort": "cpp", - "ios": "cpp", - "locale": "cpp", - "semaphore": "cpp" - } -} \ No newline at end of file diff --git a/src/GC/include/heap.hpp b/src/GC/include/heap.hpp index 128f6a3..bfa167d 100644 --- a/src/GC/include/heap.hpp +++ b/src/GC/include/heap.hpp @@ -78,11 +78,17 @@ namespace GC { * that the address of the topmost stack frame is * saved as the limit for scanning the stack in collect. */ - static void init(); // TODO: make static - static void dispose(); // -||- - static void *alloc(size_t size); // -||- + static void init(); + static void dispose(); + static void *alloc(size_t size); // DEBUG ONLY + static inline Heap *debug_the() { // TODO: make private + if (m_instance) // if m_instance is not a nullptr + return m_instance; + m_instance = new Heap(); + return m_instance; + } void collect(uint flags); // conditional collection void check_init(); // print dummy things void print_contents(); // print dummy things diff --git a/src/GC/tests/alloc_free.cpp b/src/GC/tests/alloc_free.cpp index 0e277dc..4a0f6f8 100644 --- a/src/GC/tests/alloc_free.cpp +++ b/src/GC/tests/alloc_free.cpp @@ -9,21 +9,24 @@ struct Obj { }; int main() { - GC::Heap *heap = GC::Heap::the2(); + GC::Heap::init(); Obj *obj; for (int i = 0; i < 4; i++) { - obj = static_cast(heap->alloc(sizeof(Obj))); + obj = static_cast(GC::Heap::alloc(sizeof(Obj))); obj->a = i * i + 1; obj->b = i * i + 2; obj->c = i * i + 3; } // heap->force_collect(); + auto heap = GC::Heap::debug_the(); + heap->collect(COLLECT_ALL); std::cout << obj->a << ", " << obj->b << ", " << obj->c << std::endl; //delete heap; + GC::Heap::dispose(); return 0; } \ No newline at end of file diff --git a/src/GC/tests/extern_lib.cpp b/src/GC/tests/extern_lib.cpp index 9ee3a5b..6c80c32 100644 --- a/src/GC/tests/extern_lib.cpp +++ b/src/GC/tests/extern_lib.cpp @@ -29,8 +29,8 @@ int main() { GC::Heap *singleton_test() { std::cout << "TESTING SINGLETON INSTANCES" << std::endl; std::cout << "===========================" << std::endl; - std::cout << "Call 1:\t" << GC::Heap::the() << std::endl; // First call which initializes the singleton instance - GC::Heap *heap = GC::Heap::the(); // Second call which should return the initialized instance + std::cout << "Call 1:\t" << GC::Heap::debug_the() << std::endl; // First call which initializes the singleton instance + GC::Heap *heap = GC::Heap::debug_the(); // Second call which should return the initialized instance std::cout << "Call 2:\t" << heap << std::endl; std::cout << "===========================" << std::endl; return heap; diff --git a/src/GC/tests/h_test.cpp b/src/GC/tests/h_test.cpp index dfba75c..8292734 100644 --- a/src/GC/tests/h_test.cpp +++ b/src/GC/tests/h_test.cpp @@ -1,6 +1,4 @@ -#include "../include/heap.hpp" - -GC::Heap *gc = GC::Heap::the(); +#include "heap.hpp" struct Node { int id; @@ -10,12 +8,12 @@ struct Node { Node *create_chain(int depth) { std::vector nodes; if (depth > 0) { - Node *last_node = static_cast(gc->alloc(sizeof(Node))); + Node *last_node = static_cast(GC::Heap::alloc(sizeof(Node))); last_node->id = depth; last_node->child = nullptr; nodes.push_back(last_node); for (int i = 0; i < depth; i++) { - Node *node = static_cast(gc->alloc(sizeof(Node))); + Node *node = static_cast(GC::Heap::alloc(sizeof(Node))); node->id = depth-i; node->child = nodes[i-1]; nodes.push_back(node); @@ -30,7 +28,7 @@ Node *create_chain(int depth) { } void create_array(size_t size) { - int *arr = static_cast(gc->alloc(sizeof(int) * size)); + int *arr = static_cast(GC::Heap::alloc(sizeof(int) * size)); } void detach_pointer(long **ptr) { @@ -54,19 +52,20 @@ void test_some_types() { auto stack_start = reinterpret_cast(__builtin_frame_address(0)); std::cout << "Stack start from test_some_types:\t" << stack_start << std::endl; - long *l = static_cast(gc->alloc(sizeof(long))); + long *l = static_cast(GC::Heap::alloc(sizeof(long))); std::cout << "l points to:\t\t" << l << std::endl; detach_pointer(&l); std::cout << "l points to:\t\t" << l << std::endl; // Some more dummy values of different sizes, to test stack pointer alignment - int *i = static_cast(gc->alloc(sizeof(int))); - char *c = static_cast(gc->alloc(sizeof(int))); - short *s = static_cast(gc->alloc(sizeof(short))); + 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))); } int main() { - gc->init(); + GC::Heap::init(); + GC::Heap *gc = GC::Heap::debug_the(); gc->check_init(); auto stack_start = reinterpret_cast(__builtin_frame_address(0)); std::cout << "Stack start from main:\t" << stack_start << std::endl; diff --git a/src/GC/tests/linker.cpp b/src/GC/tests/linker.cpp index f3b12f0..36717c5 100644 --- a/src/GC/tests/linker.cpp +++ b/src/GC/tests/linker.cpp @@ -9,11 +9,11 @@ struct Obj { }; int main() { - auto heap = GC::Heap::the2(); + auto heap = GC::Heap::debug_the(); std::cout << "heap:\t" << heap << std::endl; - auto obj = static_cast(heap->alloc(sizeof(Obj))); + auto obj = static_cast(GC::Heap::alloc(sizeof(Obj))); std::cout << "obj: \t" << obj << std::endl;