Fixed mark skipping

Co-authored-by: ValterMiari <ValterMiari@users.noreply.github.com>
This commit is contained in:
Victor Olin 2023-02-20 11:45:07 +01:00
parent 3473c953b5
commit 6cd6edb594
6 changed files with 73 additions and 21 deletions

View file

@ -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

View file

@ -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<uintptr_t *>(__builtin_frame_address(0));
cout << "GC stack_start:\t" << stack_start << endl;
@ -172,8 +173,9 @@ 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);
@ -210,20 +212,58 @@ namespace GC {
void Heap::mark(uintptr_t *start, const uintptr_t *end, vector<Chunk*> 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<uintptr_t>(chunk->start);
uintptr_t c_end = reinterpret_cast<uintptr_t>(chunk->start + chunk->size);
if (c_start <= *start && *start < c_end) {
uintptr_t c_start = reinterpret_cast<uintptr_t>(chunk->start);
if (!chunk->marked) {
chunk->marked = true;
worklist.erase(worklist.begin() + i);
auto new_stack_start = reinterpret_cast<uintptr_t *>(start);
mark(new_stack_start, end, worklist); //
return;
//worklist.erase(worklist.begin() + i);
it = worklist.erase(it);
//auto new_stack_start = reinterpret_cast<uintptr_t *>(start);
//mark(new_stack_start, end, worklist); //
//return;
}
else {
++it;
}
}
else {
++it;
}
}
}
}
}
// Mark child references from the root references
void mark_test(vector<Chunk *> 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<Chunk *> 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)
}
}
}

Binary file not shown.

Binary file not shown.

View file

@ -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);

View file

@ -2,21 +2,34 @@
GC::Heap *gc = GC::Heap::the();
/* void assign(int *dst) {
int *local = static_cast<int *>(gc->alloc(sizeof(int)));
*local = 10;
dst = reinterpret_cast<int **>(dst);
*dst = local;
} */
void init() {
auto stack_start = reinterpret_cast<uintptr_t *>(__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<int *>(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<long *>(gc->alloc(sizeof(long)));
l = &a;
std::cout << "Pointer for l:\t\t" << &l << std::endl;
int *i = static_cast<int *>(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<uintptr_t *>(__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;