diff --git a/src/GC/lib/heap.cpp b/src/GC/lib/heap.cpp index e178a38..efc7c80 100644 --- a/src/GC/lib/heap.cpp +++ b/src/GC/lib/heap.cpp @@ -133,18 +133,27 @@ namespace GC { heap->m_freed_chunks.pop_back(); delete chunk; } - } else { + } + else if (heap->m_freed_chunks.size()) { free_overlap(heap); } + // No freed chunks, nothing to free + else { + return; + } } void Heap::free_overlap(Heap *heap) { std::vector filtered; size_t i = 0; filtered.push_back(heap->m_freed_chunks.at(i++)); + //cout << filtered.back()->start << endl; for (; i < heap->m_freed_chunks.size(); i++) { auto prev = filtered.back(); + cout << "Previous start:\t" << prev->start << endl; + cout << "Previous end:\t" << prev->start + prev->size << endl; auto next = heap->m_freed_chunks.at(i); + cout << "Next start:\t" << next->start << endl; if (next->start > (prev->start + prev->size)) { filtered.push_back(next); } @@ -167,7 +176,7 @@ namespace GC { // get the frame adress, whwere local variables and saved registers are located auto stack_start = reinterpret_cast(__builtin_frame_address(0)); - cout << "Stack start:\t" << stack_start << endl; + cout << "Stack start in collect:\t" << stack_start << endl; uintptr_t *stack_end; if (heap->m_stack_end != nullptr) @@ -175,7 +184,7 @@ namespace GC { else stack_end = (uintptr_t *) stack_start - 80; // dummy value - cout << "Stack end in collect: " << stack_end << endl; + cout << "Stack end in collect:\t " << stack_end << endl; auto work_list = heap->m_allocated_chunks; //print_worklist(work_list); diff --git a/src/GC/tests/h_test.cpp b/src/GC/tests/h_test.cpp index ddbf29f..97ebdec 100644 --- a/src/GC/tests/h_test.cpp +++ b/src/GC/tests/h_test.cpp @@ -65,8 +65,25 @@ void test_some_types() { int main() { auto stack_start = reinterpret_cast(__builtin_frame_address(0)); std::cout << "Stack start from main:\t" << stack_start << std::endl; - test_chain(10, false); - gc->collect(MARK); // some bug in free (vector out of range) + // This is allocated outside of the scope of the GC, thus garbage + for (int i = 0; i < 10; i++) { + gc->alloc(sizeof(int)); + } + /* int depth = 10; + Node* nodes[depth]; + 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; + } */ + //test_chain(10, false); + //test_some_types(); + gc->collect(MARK | SWEEP | FREE); // free misses some chunks gc->print_contents(); return 0; } \ No newline at end of file diff --git a/src/GC/todo.md b/src/GC/todo.md index f1e60a2..7011610 100644 --- a/src/GC/todo.md +++ b/src/GC/todo.md @@ -8,6 +8,9 @@ Goal for next week (24/2): ## GC TODO: - Merge to main branch - Fix singleton references +- Get a good grasp of how the adressing of stack frames actually works. +- Debug "free()", sometimes it skips chunks that should be freed. +- Check alignment of chunks. - Think about how we want to determine if some object is a pointer or not, probably will have to discuss that with Samuel. Since it is not ideal to determine in the GC if an object is a pointer or not. It should preferably be done in a previous stage. ## Tests TODO