Some tweaks in mark. Testing of chain references

This commit is contained in:
valtermiari 2023-02-21 17:38:45 +01:00
parent 12a460ae4c
commit 6fff6ef2d1
2 changed files with 76 additions and 44 deletions

View file

@ -200,7 +200,7 @@ namespace GC {
*/
void Heap::sweep(Heap *heap) {
for (auto it = heap->m_allocated_chunks.begin(); it != heap->m_allocated_chunks.end();) {
auto chunk = *it;
Chunk *chunk = *it;
// Unmark the marked chunks for the next iteration.
if (chunk->marked) {
@ -218,16 +218,25 @@ namespace GC {
// This assumes that there are no chains of pointers, will be fixed later on
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
int counter = 0;
// To find adresses thats in the worklist
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();) {
auto chunk = *it;
Chunk *chunk = *it;
uintptr_t c_start = reinterpret_cast<uintptr_t>(chunk->start);
uintptr_t c_end = reinterpret_cast<uintptr_t>(chunk->start + chunk->size);
// Check if the stack pointer aligns with the chunk
if ((c_start <= *start && *start < c_end) && chunk != nullptr) {
cout << "Chunk start:\t" << c_start << endl;
cout << "Chunk end:\t" << c_end << endl;
//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);
@ -240,6 +249,7 @@ namespace GC {
}
}
}
cout << "Counter: " << counter << endl;
}
// Mark child references from the root references
@ -265,7 +275,7 @@ namespace GC {
if (ref != nullptr && !ref->marked) {
ref->marked = true;
worklist.push_back(ref);
//mark_test(worklist)
mark_test(worklist);
}
}
}

View file

@ -2,48 +2,70 @@
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;
} */
struct Node {
int id;
Node *child;
};
Node *create_chain(int depth) {
Node* nodes[depth];
if (depth > 0) {
Node *last_node = static_cast<Node *>(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<Node *>(gc->alloc(sizeof(Node)));
node->id = depth-i;
node->child = nodes[i-1];
nodes[i] = node;
}
return nodes[depth];
}
else
return 0;
}
void create_array(size_t size) {
int *arr = static_cast<int *>(gc->alloc(sizeof(int) * size));
}
void detach_pointer(long **ptr) {
long dummy = 10; // dummy value
long *dummy_ptr = &dummy;
long *dummy_ptr = nullptr;
*ptr = dummy_ptr;
std::cout << "Dummy pointer: \t" << dummy_ptr << std::endl;
std::cout << "Detach pointer result:\t" << ptr << std::endl;
}
void init() {
void test_chain(int depth, bool detach) {
auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
std::cout << "Stack start from init:\t" << stack_start << std::endl;
int *arr = static_cast<int *>(gc->alloc(sizeof(int) * 100));
create_array(100);
//arr = create_array(100);
//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 << "Pointer for arr:\t" << &arr << std::endl;
std::cout << "Stack start from test_chain:\t" << stack_start << std::endl;
Node *node_chain = create_chain(depth);
// This generates a segmentation fault (should be investigated further)
if (detach)
node_chain->child = nullptr;
}
void test_some_types() {
auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
std::cout << "Stack start from test_some_types:\t" << stack_start << std::endl;
long *l = static_cast<long *>(gc->alloc(sizeof(long)));
std::cout << "l points to:\t\t" << l << std::endl;
detach_pointer(&l);
std::cout << "l points to:\t\t" << l << std::endl;
// l still gets marked, which is not supposed to happen
// Some more dummy values of different sizes, to test stack pointer alignment
int *i = static_cast<int *>(gc->alloc(sizeof(int)));
char *c = static_cast<char *>(gc->alloc(sizeof(int)));
short *s = static_cast<short *>(gc->alloc(sizeof(short)));
}
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();
test_chain(10, false);
gc->collect(MARK); // some bug in free (vector out of range)
gc->print_contents();
return 0;