diff --git a/src/GC/Makefile b/src/GC/Makefile index 3d57536..f42d28c 100644 --- a/src/GC/Makefile +++ b/src/GC/Makefile @@ -1,16 +1,17 @@ mkfile_path = $(abspath $(lastword $(MAKEFILE_LIST))) current_dir = $(notdir $(patsubst %/,%,$(dir $(mkfile_path)))) +PWD_V = /Users/valtermiari/Desktop/DV/Bachelors/code/language/src/GC/include CC = clang++ PWD = /home/virre/dev/systemF/org/language/src/GC/include CFLAGS = -Wall -Wextra -v -g -std=gnu++20 -stdlib=libc++ -I VGFLAGS = --leak-check=full --show-leak-kinds=all heap: - $(CC) $(CFLAGS)$(PWD) lib/heap.cpp + $(CC) $(CFLAGS)$(PWD_V) lib/heap.cpp h_test: rm -f tests/h_test.out - $(CC) $(CFLAGS)$(PWD) tests/h_test.cpp lib/heap.cpp -o tests/h_test.out + $(CC) $(CFLAGS)$(PWD_V) tests/h_test.cpp lib/heap.cpp -o tests/h_test.out h_test_vg: make h_test @@ -18,7 +19,7 @@ h_test_vg: linker: rm -f tests/linker.out - $(CC) $(CFLAGS)$(PWD) tests/linker.cpp lib/heap.cpp -o tests/linker.out + $(CC) $(CFLAGS)$(PWD_V) tests/linker.cpp lib/heap.cpp -o tests/linker.out linker_vg: make linker diff --git a/src/GC/lib/heap.cpp b/src/GC/lib/heap.cpp index c41c924..19a1911 100644 --- a/src/GC/lib/heap.cpp +++ b/src/GC/lib/heap.cpp @@ -119,13 +119,25 @@ namespace GC { // Not optimal for now, it doesn't have to loop over all objects // but mark needs some refinements before this can be optimised + // sweep apparently only sweeps chunks of odd size, eventhough the're not marked void Heap::sweep() { - for (size_t i = 0; i < m_allocated_chunks.size(); i++) { +/* for (char i = 0; i < m_allocated_chunks.size();) { auto chunk = m_allocated_chunks.at(i); + std::cout << "Current chunk: " << chunk->start << std::endl; if (chunk->marked == false) { + std::cout << "The current chunk was removed" << std::endl; m_allocated_chunks.erase(m_allocated_chunks.begin() + i); m_freed_chunks.push_back(chunk); } + } */ + // This captures the wierd behavior or my lack of knowledge of vectors + for (auto chunk : m_allocated_chunks) { + if (!chunk->marked) { + auto it = std::find(m_allocated_chunks.begin(), m_allocated_chunks.end(), chunk); + if (it != m_allocated_chunks.end()) + m_allocated_chunks.erase(it); + m_freed_chunks.push_back(chunk); + } } } @@ -168,20 +180,20 @@ namespace GC { */ // For testing purposes void Heap::print_line(Chunk *chunk) { - std::cout << "Marked: " << chunk->marked << "\nStart adr: " << chunk->start << "\nSize: " << chunk->size << " B" << std::endl; + std::cout << "Marked: " << chunk->marked << "\nStart adr: " << chunk->start << "\nSize: " << chunk->size << " B\n" << std::endl; } void Heap::print_contents() { if (m_allocated_chunks.size()) { - std::cout << "ALLOCATED CHUNKS" << std::endl; + std::cout << "\nALLOCATED CHUNKS #" << m_allocated_chunks.size() << std::endl; for (auto chunk : m_allocated_chunks) { print_line(chunk); } } else { - std::cout << "NO ALLOCATIONS" << std::endl; + std::cout << "NO ALLOCATIONS\n" << std::endl; } if (m_freed_chunks.size()) { - std::cout << "FREED CHUNKS" << std::endl; + std::cout << "\nFREED CHUNKS #" << m_freed_chunks.size() << std::endl; for (auto fchunk : m_freed_chunks) { print_line(fchunk); } diff --git a/src/GC/tests/h_test.cpp b/src/GC/tests/h_test.cpp index f785077..f26ed55 100644 --- a/src/GC/tests/h_test.cpp +++ b/src/GC/tests/h_test.cpp @@ -12,11 +12,16 @@ void init() { auto temp1 = gc->alloc(sizeof(a_ptr)); auto temp2 = gc->alloc(sizeof(live_int_vec)); auto temp3 = gc->alloc(sizeof(dead_int_vec)); - a_ptr = nullptr; + + // *arr, *temp1, *temp2 and *temp3 should all be on the stack + //a_ptr = nullptr; } int main() { - init(); + //init(); + for (int i = 1; i < 10; i++) { + gc->alloc(sizeof(i)); + } gc->collect(MARK | SWEEP); gc->print_contents(); //delete gc; diff --git a/src/GC/tests/h_test.out.dSYM/Contents/Info.plist b/src/GC/tests/h_test.out.dSYM/Contents/Info.plist new file mode 100644 index 0000000..3db2218 --- /dev/null +++ b/src/GC/tests/h_test.out.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.h_test.out + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/src/GC/tests/h_test.out.dSYM/Contents/Resources/DWARF/h_test.out b/src/GC/tests/h_test.out.dSYM/Contents/Resources/DWARF/h_test.out new file mode 100644 index 0000000..8e80ccd Binary files /dev/null and b/src/GC/tests/h_test.out.dSYM/Contents/Resources/DWARF/h_test.out differ