From 0260b2876cd34188c28e10b3c45ca9dffa84e85f Mon Sep 17 00:00:00 2001 From: Victor Olin Date: Tue, 14 Feb 2023 15:11:58 +0100 Subject: [PATCH] Next up is tests Co-authored-by: ValterMiari --- .gitignore | 4 +++ src/GC/heap.cpp | 29 +++++++++++---------- src/GC/include/heap.hpp | 6 ++--- src/GC/tests/stack.cpp | 57 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 16 deletions(-) create mode 100644 src/GC/tests/stack.cpp diff --git a/.gitignore b/.gitignore index 8d1bad3..d156fe8 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,9 @@ dist-newstyle *.x *.bak src/Grammar + language llvm.ll +/language + +src/GC/tests/*.out diff --git a/src/GC/heap.cpp b/src/GC/heap.cpp index 7cdb7dc..9b03815 100644 --- a/src/GC/heap.cpp +++ b/src/GC/heap.cpp @@ -44,7 +44,7 @@ namespace GC { else if (cp->size == size) { // sno hela chunken - m_freed_chunks.erase(m_freed_chunks.begin()+i); + m_freed_chunks.erase(m_freed_chunks.begin() + i); m_allocated_chunks.push_back(cp); return cp->start; } @@ -67,14 +67,15 @@ namespace GC { } void Heap::collect() { - // mark all objs - // compact(); - // free all unmarked (m_freed_chunks.add(jalkdsj)) - // get the frame adress, whwere local variables and saved registers are located - auto stack_start = reinterpret_cast(__builtin_frame_address(0)); - auto stack_end = reinterpret_cast(0ul); - + // get the frame adress, whwere local variables and saved registers are located + auto stack_start = reinterpret_cast(__builtin_frame_address(0)); + // looking at 10 stack frames back + auto stack_end = reinterpret_cast(__builtin_frame_address(10)); + auto work_list = m_allocated_chunks; + mark(stack_start, stack_end, work_list); + + compact(); //release free chunks while (m_freed_chunks.size()) { @@ -102,14 +103,16 @@ namespace GC { } } - void Heap::mark(uintptr_t *start, uintptr_t *end) { - for (; start < end; start += 1) { // start < end??? - for (auto chunk : m_allocated_chunks) { + void Heap::mark(uintptr_t *start, const uintptr_t *end, std::vector work_list) { + for (; start < end; start++) { // to find adresses thats in the worklist + for (size_t i = 0; i < work_list.size(); i++) { // fix this + auto chunk = work_list.at(i); if (chunk->start <= start && start < chunk->start + chunk->size) { if (!chunk->marked) { chunk->marked = true; - //mark(chunk) // - //TODO + work_list.erase(work_list.begin() + i); + mark(reinterpret_cast(chunk->start + chunk->size), end, work_list); // + return; } } } diff --git a/src/GC/include/heap.hpp b/src/GC/include/heap.hpp index 142af33..59a78d7 100644 --- a/src/GC/include/heap.hpp +++ b/src/GC/include/heap.hpp @@ -40,7 +40,7 @@ namespace GC { void collect(); void compact(); - void mark(uintptr_t *start, uintptr_t *end); + void mark(uintptr_t *start, const uintptr_t *end, std::vector worklist); bool compareChunks(Chunk *c1, Chunk *c2); @@ -49,8 +49,8 @@ namespace GC { size_t m_size; size_t m_allocated_size; - std::vector m_allocated_chunks; - std::vector m_freed_chunks; + std::vector m_allocated_chunks; + std::vector m_freed_chunks; }; } \ No newline at end of file diff --git a/src/GC/tests/stack.cpp b/src/GC/tests/stack.cpp new file mode 100644 index 0000000..6cdf2d0 --- /dev/null +++ b/src/GC/tests/stack.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include + +std::vector iv; + +void collect() { + std::cout << "in collect" << std::endl; + + uintptr_t *stack_start = reinterpret_cast(__builtin_frame_address(0)); + // uintptr_t *stack_end = reinterpret_cast(__builtin_frame_address(100)); + + std::cout << "SP1:\t" << stack_start << "\nSP2:\t" << (stack_start - 1*sizeof(int)) << std::endl; + std::cout << "SP-:\t" << --stack_start << std::endl; + + const uintptr_t *stack_end = (stack_start + 30*sizeof(int)); + int vars_found = 0; + + while (stack_start < stack_end) { + + if (std::find(iv.begin(), iv.end(), stack_start) != iv.end()) { + vars_found++; + std::cout << "Found " << *(reinterpret_cast(stack_start)) << " at " << stack_start << std::endl; + } + + // std::cout << "SP address:\t\t" << stack_start << "\nSP value:\t\t" << *(reinterpret_cast(stack_start)) << std::endl; + + stack_start++; + } + + if (vars_found == 0) { + std::cout << "Found nothing" << std::endl; + } +} + +int add(unsigned long a, unsigned long b) { + iv.push_back(reinterpret_cast(&a)); + iv.push_back(reinterpret_cast(&b)); + std::cout << "'a':\t" << &a << "\n'b':\t" << &b << std::endl; + collect(); + return a + b; +} + +int main() { + + unsigned long global_1 = 16; + unsigned long global_2 = 32; + + iv.push_back(&global_1); + iv.push_back(&global_2); + + std::cout << "'g1':\t" << &global_1 << "\n'g2':\t" << &global_2 << std::endl; + + add(3,2); + return 0; +} \ No newline at end of file