diff --git a/.gitignore b/.gitignore index 934c3da..1daddd6 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,6 @@ llvm.ll /language .vscode/ +src/GC/lib/*.o +src/GC/lib/*.so src/GC/tests/*.out diff --git a/src/GC/Makefile b/src/GC/Makefile index 1039403..354adad 100644 --- a/src/GC/Makefile +++ b/src/GC/Makefile @@ -1,18 +1,17 @@ -mkfile_path = $(abspath $(lastword $(MAKEFILE_LIST))) -current_dir = $(notdir $(patsubst %/,%,$(dir $(mkfile_path)))) CC = clang++ - -PWD_V = /Users/valtermiari/Desktop/DV/Bachelors/code/language/src/GC/include -LIB_INCL = -I/home/virre/dev/systemF/org/language/src/GC/include -LIB_SO = -L/home/virre/dev/systemF/org/language/src/GC/lib -LIB_LINK = /home/virre/dev/systemF/org/language/src/GC/lib - +CWD = $(shell pwd) +LIB_INCL = -I$(CWD)/include +LIB_SO = -L$(CWD)/lib +LIB_LINK = $(CWD)/lib CFLAGS = -Wall -Wextra -v -g -std=gnu++20 -stdlib=libc++ -I VGFLAGS = --leak-check=full --show-leak-kinds=all STDFLAGS = -std=gnu++20 -stdlib=libc++ WFLAGS = -Wall -Wextra DBGFLAGS = -g +test_test: + echo "$(shell pwd)" + heap: $(CC) $(WFLAGS) $(STDFLAGS) $(LIB_INCL) lib/heap.cpp diff --git a/src/GC/include/heap.hpp b/src/GC/include/heap.hpp index d0f805d..575d6f7 100644 --- a/src/GC/include/heap.hpp +++ b/src/GC/include/heap.hpp @@ -44,6 +44,7 @@ namespace GC { size_t m_allocated_size; uintptr_t *m_stack_end = nullptr; + // maybe change to std::list std::vector m_allocated_chunks; std::vector m_freed_chunks; diff --git a/src/GC/lib/heap.cpp b/src/GC/lib/heap.cpp index efc7c80..58c4966 100644 --- a/src/GC/lib/heap.cpp +++ b/src/GC/lib/heap.cpp @@ -147,14 +147,14 @@ namespace GC { std::vector filtered; size_t i = 0; filtered.push_back(heap->m_freed_chunks.at(i++)); - //cout << filtered.back()->start << endl; + 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)) { + auto p_start = (uintptr_t)(prev->start); + auto p_size = (uintptr_t)(prev->size); + auto n_start = (uintptr_t)(next->start); + if (n_start >= (p_start + p_size)) { filtered.push_back(next); } } @@ -186,7 +186,6 @@ namespace GC { cout << "Stack end in collect:\t " << stack_end << endl; auto work_list = heap->m_allocated_chunks; - //print_worklist(work_list); if (flags & MARK) { mark(stack_start, stack_end, work_list); @@ -229,33 +228,35 @@ namespace GC { void Heap::mark(uintptr_t *start, const uintptr_t *end, vector worklist) { int counter = 0; // To find adresses thats in the worklist - for (; start > end; start--) { + 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();) { - 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) { - 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 + for (auto it = worklist.begin(); it != worklist.end();) { + Chunk *chunk = *it; + if (chunk == nullptr) { + assert(false && "EPIC FAIL"); + } + 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) { + //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; diff --git a/src/GC/lib/heap.o b/src/GC/lib/heap.o deleted file mode 100644 index 0e5421d..0000000 Binary files a/src/GC/lib/heap.o and /dev/null differ diff --git a/src/GC/lib/libheap.so b/src/GC/lib/libheap.so deleted file mode 100755 index 4e7c0eb..0000000 Binary files a/src/GC/lib/libheap.so and /dev/null differ diff --git a/src/GC/tests/h_test.cpp b/src/GC/tests/h_test.cpp index 97ebdec..5c263f1 100644 --- a/src/GC/tests/h_test.cpp +++ b/src/GC/tests/h_test.cpp @@ -63,26 +63,23 @@ void test_some_types() { } int main() { + //gc->init(); + //gc->check_init(); auto stack_start = reinterpret_cast(__builtin_frame_address(0)); std::cout << "Stack start from main:\t" << stack_start << std::endl; - // This is allocated outside of the scope of the GC, thus garbage - for (int i = 0; i < 10; i++) { - gc->alloc(sizeof(int)); + + // char *c = static_cast(gc->alloc(sizeof(char))); // 0x0 | 0x0 + // int *i = static_cast(gc->alloc(sizeof(int))); // 0x1-0x4 | 0x4-0x8 + // char *c2 = static_cast(gc->alloc(sizeof(char)));// 0x5 | 0x9-0x + // long *l = static_cast(gc->alloc(sizeof(long))); // 0x6-0xd | 0x + + // This is allocated outside of the scope of the GC (if gc->init() isn't called), thus garbage + long *longs[21]; + std::cout << "Pointer to ints:\t" << longs << std::endl; + for (int i = 0; i < 21; i++) { + longs[i] = static_cast(gc->alloc(sizeof(long))); } - /* 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;