Some progress on marking

This commit is contained in:
valtermiari 2023-02-17 10:29:48 +01:00 committed by Victor Olin
parent 5fcab54fe4
commit fb5f283dfd
3 changed files with 51 additions and 20 deletions

View file

@ -67,7 +67,7 @@ namespace GC {
// get the frame adress, whwere local variables and saved registers are located // get the frame adress, whwere local variables and saved registers are located
auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0)); auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
// looking at 10 stack frames back // looking at 10 stack frames back
const uintptr_t *stack_end = (uintptr_t *)0; const uintptr_t *stack_end = (uintptr_t *)0; // temporary
// denna segfaultar om arg för __b_f_a är > 2 // denna segfaultar om arg för __b_f_a är > 2
// reinterpret_cast<const uintptr_t *>(__builtin_frame_address(10)); // reinterpret_cast<const uintptr_t *>(__builtin_frame_address(10));
@ -93,8 +93,9 @@ namespace GC {
cout << "DEBUG COLLECT\nFLAGS: " << flags << endl; cout << "DEBUG COLLECT\nFLAGS: " << flags << endl;
// get the frame adress, whwere local variables and saved registers are located // get the frame adress, whwere local variables and saved registers are located
auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0)); auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
cout << "Stack start:\t" << stack_start << endl;
const uintptr_t *stack_end = (uintptr_t *)0; // dummy value const uintptr_t *stack_end = (uintptr_t *) stack_start - 40; // dummy value
// denna segfaultar om arg för __b_f_a är > 2 // denna segfaultar om arg för __b_f_a är > 2
// reinterpret_cast<const uintptr_t *>(__builtin_frame_address(10)); // reinterpret_cast<const uintptr_t *>(__builtin_frame_address(10));
@ -136,9 +137,12 @@ namespace GC {
// TODO: return the worklist filtered on mark = true // TODO: return the worklist filtered on mark = true
void Heap::mark(uintptr_t *start, const uintptr_t *end, vector<Chunk*> work_list) { void Heap::mark(uintptr_t *start, const uintptr_t *end, vector<Chunk*> work_list) {
for (; start < end; start++) { // to find adresses thats in the worklist for (; start > end; start--) { // to find adresses thats in the worklist
cout << "Value of start pointer:\t" << start << endl;
for (size_t i = 0; i < work_list.size(); i++) { // fix this for (size_t i = 0; i < work_list.size(); i++) { // fix this
auto chunk = work_list.at(i); auto chunk = work_list.at(i);
cout << "Chunk value:\t" << chunk->start << endl;
cout << "Chunk pointer value:\t" << &chunk << endl;
if (chunk->start <= start && start < chunk->start + chunk->size) { if (chunk->start <= start && start < chunk->start + chunk->size) {
if (!chunk->marked) { if (!chunk->marked) {
chunk->marked = true; chunk->marked = true;
@ -151,6 +155,29 @@ namespace GC {
} }
} }
/* void mark_test(vector<Chunk *> worklist) {
while (worklist.size() > 0) {
Chunk *ref = worklist.pop_back();
Chunk *child = (Chunk*) *ref;
if (child != NULL && !child->marked) {
child->marked = true;
worklist.push_back(child);
mark_test(worklist);
}
}
}
void mark_from_roots(uintptr_t *start, const uintptr_t *end) {
vector<Chunk *> worklist;
for (;start > end; start--) {
Chunk *ref = *start;
if (ref != NULL && !ref->marked) {
ref->marked = true;
worklist.push_back(ref);
mark_test(worklist);
}
}
} */
/* Alternative marking, pseudocode /* Alternative marking, pseudocode
mark_from_roots(): mark_from_roots():

View file

@ -1,27 +1,31 @@
#include "heap.hpp" #include "../include/heap.hpp"
GC::Heap *gc = GC::Heap::the2(); GC::Heap *gc = GC::Heap::the2();
void init() { void init() {
std::vector<int> live_int_vec{1, 2, 3, 4, 5};
std::vector<int> dead_int_vec(1000000, 1);
int *arr = static_cast<int *>(gc->alloc(sizeof(int) * 300));
int *a_ptr;
int a = 10;
a_ptr = &a;
auto temp1 = gc->alloc(sizeof(a_ptr));
auto temp2 = gc->alloc(sizeof(live_int_vec));
auto temp3 = gc->alloc(sizeof(dead_int_vec));
// *arr, *temp1, *temp2 and *temp3 should all be on the stack auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
//a_ptr = nullptr; auto stack_end = stack_start - 160;
std::cout << "Stack start from init:\t" << stack_start << std::endl;
std::cout << "Imaginary stack end:\t" << stack_end << std::endl;
int *arr = static_cast<int *>(gc->alloc(sizeof(int) * 300));
for (int i = 0; i < (sizeof(int) * 300); i++) {
arr[i] = i;
}
std::cout << "First stack pointer:\t" << &arr << std::endl;
long *l = static_cast<long *>(gc->alloc(sizeof(long)));
*l = 20;
// This doesn't get allocated on our heap, but how is it viewed on valgr?
int *arr2 = new int[1000];
for (int i = 0; i < 1000; i++) {
arr2[i] = i;
}
} }
int main() { int main() {
//init(); auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
for (int i = 1; i < 10; i++) { std::cout << "Stack start from main:\t" << stack_start << std::endl;
gc->alloc(sizeof(i)); init();
}
gc->collect(MARK | SWEEP); gc->collect(MARK | SWEEP);
gc->print_contents(); gc->print_contents();
//delete gc; //delete gc;