diff --git a/src/GC/Makefile b/src/GC/Makefile index b99d2a4..00389c3 100644 --- a/src/GC/Makefile +++ b/src/GC/Makefile @@ -36,8 +36,7 @@ linker_vg: valgrind $(VGFLAGS) tests/linker.out extern_lib: - rm -f lib/heap.o - rm -f lib/libheap.so + rm -f lib/heap.o lib/libheap.so tests/extern_lib.out $(CC) $(STDFLAGS) -c -fPIC -o lib/heap.o lib/heap.cpp $(CC) $(STDFLAGS) -shared -o lib/libheap.so lib/heap.o $(CC) $(LIB_INCL) $(LIB_SO) -v -Wall -o tests/extern_lib.out tests/extern_lib.cpp \ No newline at end of file diff --git a/src/GC/lib/heap.cpp b/src/GC/lib/heap.cpp index a222833..d13cec6 100644 --- a/src/GC/lib/heap.cpp +++ b/src/GC/lib/heap.cpp @@ -20,6 +20,7 @@ namespace GC { */ void Heap::check_init() { auto heap = Heap::the(); + cout << "Heap addr:\t" << heap << endl; cout << "GC m_stack_end:\t" << heap->m_stack_end << endl; auto stack_start = reinterpret_cast(__builtin_frame_address(0)); cout << "GC stack_start:\t" << stack_start << endl; @@ -172,10 +173,11 @@ namespace GC { if (heap->m_stack_end != nullptr) stack_end = heap->m_stack_end; else - stack_end = (uintptr_t *) stack_start - 40; // dummy value + stack_end = (uintptr_t *) stack_start - 80; // dummy value + cout << "Stack end in collect: " << stack_end << endl; auto work_list = heap->m_allocated_chunks; - // print_worklist(work_list); + //print_worklist(work_list); if (flags & MARK) { mark(stack_start, stack_end, work_list); @@ -210,20 +212,58 @@ namespace GC { 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 - for (size_t i = 0; i < worklist.size(); i++) { // fix this - auto chunk = worklist.at(i); + for (auto it = worklist.begin(); it != worklist.end();) { + //for (size_t i = 0; i < worklist.size(); i++) { // fix this + //auto chunk = worklist.at(i); + auto chunk = *it; uintptr_t c_start = reinterpret_cast(chunk->start); uintptr_t c_end = reinterpret_cast(chunk->start + chunk->size); if (c_start <= *start && *start < c_end) { uintptr_t c_start = reinterpret_cast(chunk->start); if (!chunk->marked) { chunk->marked = true; - worklist.erase(worklist.begin() + i); - auto new_stack_start = reinterpret_cast(start); - mark(new_stack_start, end, worklist); // - return; + //worklist.erase(worklist.begin() + i); + it = worklist.erase(it); + //auto new_stack_start = reinterpret_cast(start); + //mark(new_stack_start, end, worklist); // + //return; + } + else { + ++it; } } + else { + ++it; + } + } + } + } + } + + // Mark child references from the root references + void mark_test(vector worklist) { + while (worklist.size() > 0) { + Chunk *ref = worklist.back(); + worklist.pop_back(); + Chunk *child = (Chunk*) ref; // this is probably not correct + if (child != nullptr && !child->marked) { + child->marked = true; + worklist.push_back(child); + mark_test(worklist); + } + } + } + + // Mark the root references and look for child references to them + void mark_from_roots(uintptr_t *start, const uintptr_t *end) { + vector worklist; + for (;start > end; start --) { + if (*start % 8 == 0) { // all pointers must be aligned as double words + Chunk *ref = (Chunk*) *start; + if (ref != nullptr && !ref->marked) { + ref->marked = true; + worklist.push_back(ref); + //mark_test(worklist) } } } diff --git a/src/GC/lib/heap.o b/src/GC/lib/heap.o index 2f2a154..0e5421d 100644 Binary files a/src/GC/lib/heap.o and b/src/GC/lib/heap.o differ diff --git a/src/GC/lib/libheap.so b/src/GC/lib/libheap.so index 83a12f5..4e7c0eb 100755 Binary files a/src/GC/lib/libheap.so and b/src/GC/lib/libheap.so differ diff --git a/src/GC/tests/extern_lib.cpp b/src/GC/tests/extern_lib.cpp index 5f3e0c7..2d47576 100644 --- a/src/GC/tests/extern_lib.cpp +++ b/src/GC/tests/extern_lib.cpp @@ -3,11 +3,9 @@ #include "heap.hpp" -// using namespace std; - -// GC::Heap *singleton_test(); -// void init_gc(GC::Heap *heap); -// void frame_test(GC::Heap *heap); +GC::Heap *singleton_test(); +void init_gc(GC::Heap *heap); +void frame_test(GC::Heap *heap); GC::Heap *singleton_test() { std::cout << "TESTING SINGLETON INSTANCES" << std::endl; @@ -38,11 +36,13 @@ void frame_test(GC::Heap *heap) { std::cout << "Previous stack frame:\t" << prev_frame << std::endl; heap->check_init(); + // auto alloced = heap->alloc(sizeof(unsigned long)); std::cout << "===========================" << std::endl; } int main() { + std::cout << "in main" << std::endl; auto heap = singleton_test(); init_gc(heap); diff --git a/src/GC/tests/h_test.cpp b/src/GC/tests/h_test.cpp index 774f091..65f51d6 100644 --- a/src/GC/tests/h_test.cpp +++ b/src/GC/tests/h_test.cpp @@ -2,21 +2,34 @@ 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; +} */ + void init() { auto stack_start = reinterpret_cast(__builtin_frame_address(0)); - auto stack_end = stack_start - 40; + //auto stack_end = stack_start - 40; std::cout << "Stack start from init:\t" << stack_start << std::endl; - std::cout << "Imaginary stack end:\t" << stack_end << std::endl; + //std::cout << "Imaginary stack end:\t" << stack_end << std::endl; int *arr = static_cast(gc->alloc(sizeof(int) * 100)); - std::cout << "Arr_ptr" << std::hex << arr << "\n\n\n" << std::endl; + //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 << "First stack pointer:\t" << &arr << std::endl; + for (int i = 0; i < (sizeof(int) * 20); i++) { + gc->alloc(sizeof(int)); + } + std::cout << "Pointer for arr:\t" << &arr << std::endl; long a = 20; long *l = static_cast(gc->alloc(sizeof(long))); - l = &a; + std::cout << "Pointer for l:\t\t" << &l << std::endl; + int *i = static_cast(gc->alloc(sizeof(int))); + std::cout << "Pointer for i:\t\t" << &i << std::endl; + //l = &a; //*l = 20; } @@ -24,7 +37,7 @@ int main() { auto stack_start = reinterpret_cast(__builtin_frame_address(0)); std::cout << "Stack start from main:\t" << stack_start << std::endl; init(); - gc->collect(MARK | SWEEP | FREE); + gc->collect(MARK | SWEEP); // some bug in free (vector out of range) gc->print_contents(); //delete gc; return 0;