From 6fff6ef2d118ab748d2fddb8e6441f8c9c427b66 Mon Sep 17 00:00:00 2001 From: valtermiari Date: Tue, 21 Feb 2023 17:38:45 +0100 Subject: [PATCH] Some tweaks in mark. Testing of chain references --- src/GC/lib/heap.cpp | 54 +++++++++++++++++++-------------- src/GC/tests/h_test.cpp | 66 +++++++++++++++++++++++++++-------------- 2 files changed, 76 insertions(+), 44 deletions(-) diff --git a/src/GC/lib/heap.cpp b/src/GC/lib/heap.cpp index f2d04ee..e178a38 100644 --- a/src/GC/lib/heap.cpp +++ b/src/GC/lib/heap.cpp @@ -200,46 +200,56 @@ namespace GC { */ void Heap::sweep(Heap *heap) { for (auto it = heap->m_allocated_chunks.begin(); it != heap->m_allocated_chunks.end();) { - auto chunk = *it; + Chunk *chunk = *it; - // Unmark the marked chunks for the next iteration. - if (chunk->marked) { - chunk->marked = false; - ++it; - } - else { - // Add the unmarked chunks to freed chunks and remove from - // the list of allocated chunks - heap->m_freed_chunks.push_back(chunk); - it = heap->m_allocated_chunks.erase(it); - } + // Unmark the marked chunks for the next iteration. + if (chunk->marked) { + chunk->marked = false; + ++it; + } + else { + // Add the unmarked chunks to freed chunks and remove from + // the list of allocated chunks + heap->m_freed_chunks.push_back(chunk); + it = heap->m_allocated_chunks.erase(it); + } } } // This assumes that there are no chains of pointers, will be fixed later on void Heap::mark(uintptr_t *start, const uintptr_t *end, vector worklist) { - for (; start > end; start--) { // to find adresses thats in the worklist - if (*start % 8 == 0) { // all pointers must be aligned as double words + int counter = 0; + // To find adresses thats in the worklist + for (; start > end; start--) { + counter++; + // all pointers must be aligned as double words + if (*start % 8 == 0) { + for (auto it = worklist.begin(); it != worklist.end();) { - auto chunk = *it; + Chunk *chunk = *it; uintptr_t c_start = reinterpret_cast(chunk->start); uintptr_t c_end = reinterpret_cast(chunk->start + chunk->size); + // Check if the stack pointer aligns with the chunk - if ((c_start <= *start && *start < c_end) && chunk != nullptr) { - cout << "Chunk start:\t" << c_start << endl; - cout << "Chunk end:\t" << c_end << endl; - if (!chunk->marked) { + //if ((c_start <= *start && *start < c_end) && chunk != nullptr) { + if (c_start == *start) { + cout << "Start points to:\t" << hex << *start << endl; + cout << "Chunk start:\t\t" << hex << c_start << endl; + cout << "Chunk end:\t\t" << hex << c_end << "\n" << endl; + + if (!chunk->marked) { chunk->marked = true; it = worklist.erase(it); } else ++it; - } + } else ++it; - } + } } } + cout << "Counter: " << counter << endl; } // Mark child references from the root references @@ -265,7 +275,7 @@ namespace GC { if (ref != nullptr && !ref->marked) { ref->marked = true; worklist.push_back(ref); - //mark_test(worklist) + mark_test(worklist); } } } diff --git a/src/GC/tests/h_test.cpp b/src/GC/tests/h_test.cpp index cd93f7e..ddbf29f 100644 --- a/src/GC/tests/h_test.cpp +++ b/src/GC/tests/h_test.cpp @@ -2,48 +2,70 @@ GC::Heap *gc = GC::Heap::the(); -/* void assign(int *dst) { - int *local = static_cast(gc->alloc(sizeof(int))); - *local = 10; - dst = reinterpret_cast(dst); - *dst = local; -} */ +struct Node { + int id; + Node *child; +}; + +Node *create_chain(int depth) { + Node* nodes[depth]; + if (depth > 0) { + Node *last_node = static_cast(gc->alloc(sizeof(Node))); + last_node->id = depth; + last_node->child = nullptr; + nodes[0] = last_node; + for (int i = 1; i < depth; i++) { + Node *node = static_cast(gc->alloc(sizeof(Node))); + node->id = depth-i; + node->child = nodes[i-1]; + nodes[i] = node; + } + return nodes[depth]; + } + else + return 0; +} void create_array(size_t size) { int *arr = static_cast(gc->alloc(sizeof(int) * size)); } void detach_pointer(long **ptr) { - long dummy = 10; // dummy value - long *dummy_ptr = &dummy; + long *dummy_ptr = nullptr; *ptr = dummy_ptr; - std::cout << "Dummy pointer: \t" << dummy_ptr << std::endl; - std::cout << "Detach pointer result:\t" << ptr << std::endl; } -void init() { - +void test_chain(int depth, bool detach) { auto stack_start = reinterpret_cast(__builtin_frame_address(0)); - std::cout << "Stack start from init:\t" << stack_start << std::endl; - int *arr = static_cast(gc->alloc(sizeof(int) * 100)); - create_array(100); - //arr = create_array(100); - //std::cout << "Arr_ptr" << std::hex << arr << "\n\n\n" << std::endl; - for (int i = 0; i < (sizeof(int) * 100); i++) { - arr[i] = i; - } - std::cout << "Pointer for arr:\t" << &arr << std::endl; + std::cout << "Stack start from test_chain:\t" << stack_start << std::endl; + + Node *node_chain = create_chain(depth); + // This generates a segmentation fault (should be investigated further) + if (detach) + node_chain->child = nullptr; + +} + +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))); std::cout << "l points to:\t\t" << l << std::endl; detach_pointer(&l); std::cout << "l points to:\t\t" << l << std::endl; // l still gets marked, which is not supposed to happen + + // 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 main() { auto stack_start = reinterpret_cast(__builtin_frame_address(0)); std::cout << "Stack start from main:\t" << stack_start << std::endl; - init(); + test_chain(10, false); gc->collect(MARK); // some bug in free (vector out of range) gc->print_contents(); return 0;