Fixed bugs on freeing chunks and stack-scanning

Co-authored-by: ValterMiari <ValterMiari@users.noreply.github.com>
This commit is contained in:
Victor Olin 2023-02-22 18:30:15 +01:00
parent 07bf2c8f48
commit ea6eab0bcf
7 changed files with 53 additions and 53 deletions

2
.gitignore vendored
View file

@ -9,4 +9,6 @@ llvm.ll
/language
.vscode/
src/GC/lib/*.o
src/GC/lib/*.so
src/GC/tests/*.out

View file

@ -1,18 +1,17 @@
mkfile_path = $(abspath $(lastword $(MAKEFILE_LIST)))
current_dir = $(notdir $(patsubst %/,%,$(dir $(mkfile_path))))
CC = clang++
PWD_V = /Users/valtermiari/Desktop/DV/Bachelors/code/language/src/GC/include
LIB_INCL = -I/home/virre/dev/systemF/org/language/src/GC/include
LIB_SO = -L/home/virre/dev/systemF/org/language/src/GC/lib
LIB_LINK = /home/virre/dev/systemF/org/language/src/GC/lib
CWD = $(shell pwd)
LIB_INCL = -I$(CWD)/include
LIB_SO = -L$(CWD)/lib
LIB_LINK = $(CWD)/lib
CFLAGS = -Wall -Wextra -v -g -std=gnu++20 -stdlib=libc++ -I
VGFLAGS = --leak-check=full --show-leak-kinds=all
STDFLAGS = -std=gnu++20 -stdlib=libc++
WFLAGS = -Wall -Wextra
DBGFLAGS = -g
test_test:
echo "$(shell pwd)"
heap:
$(CC) $(WFLAGS) $(STDFLAGS) $(LIB_INCL) lib/heap.cpp

View file

@ -44,6 +44,7 @@ namespace GC {
size_t m_allocated_size;
uintptr_t *m_stack_end = nullptr;
// maybe change to std::list
std::vector<Chunk *> m_allocated_chunks;
std::vector<Chunk *> m_freed_chunks;

View file

@ -147,14 +147,14 @@ namespace GC {
std::vector<Chunk *> filtered;
size_t i = 0;
filtered.push_back(heap->m_freed_chunks.at(i++));
//cout << filtered.back()->start << endl;
cout << filtered.back()->start << endl;
for (; i < heap->m_freed_chunks.size(); i++) {
auto prev = filtered.back();
cout << "Previous start:\t" << prev->start << endl;
cout << "Previous end:\t" << prev->start + prev->size << endl;
auto next = heap->m_freed_chunks.at(i);
cout << "Next start:\t" << next->start << endl;
if (next->start > (prev->start + prev->size)) {
auto p_start = (uintptr_t)(prev->start);
auto p_size = (uintptr_t)(prev->size);
auto n_start = (uintptr_t)(next->start);
if (n_start >= (p_start + p_size)) {
filtered.push_back(next);
}
}
@ -186,7 +186,6 @@ namespace GC {
cout << "Stack end in collect:\t " << stack_end << endl;
auto work_list = heap->m_allocated_chunks;
//print_worklist(work_list);
if (flags & MARK) {
mark(stack_start, stack_end, work_list);
@ -229,33 +228,35 @@ namespace GC {
void Heap::mark(uintptr_t *start, const uintptr_t *end, vector<Chunk*> worklist) {
int counter = 0;
// To find adresses thats in the worklist
for (; start > end; start--) {
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();) {
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) {
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);
}
else
++it;
}
else
for (auto it = worklist.begin(); it != worklist.end();) {
Chunk *chunk = *it;
if (chunk == nullptr) {
assert(false && "EPIC FAIL");
}
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) {
//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);
}
else {
++it;
}
}
else {
++it;
}
}
}
cout << "Counter: " << counter << endl;

Binary file not shown.

Binary file not shown.

View file

@ -63,26 +63,23 @@ void test_some_types() {
}
int main() {
//gc->init();
//gc->check_init();
auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
std::cout << "Stack start from main:\t" << stack_start << std::endl;
// This is allocated outside of the scope of the GC, thus garbage
for (int i = 0; i < 10; i++) {
gc->alloc(sizeof(int));
// char *c = static_cast<char *>(gc->alloc(sizeof(char))); // 0x0 | 0x0
// int *i = static_cast<int *>(gc->alloc(sizeof(int))); // 0x1-0x4 | 0x4-0x8
// char *c2 = static_cast<char *>(gc->alloc(sizeof(char)));// 0x5 | 0x9-0x
// long *l = static_cast<long *>(gc->alloc(sizeof(long))); // 0x6-0xd | 0x
// This is allocated outside of the scope of the GC (if gc->init() isn't called), thus garbage
long *longs[21];
std::cout << "Pointer to ints:\t" << longs << std::endl;
for (int i = 0; i < 21; i++) {
longs[i] = static_cast<long *>(gc->alloc(sizeof(long)));
}
/* int depth = 10;
Node* nodes[depth];
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;
} */
//test_chain(10, false);
//test_some_types();
gc->collect(MARK | SWEEP | FREE); // free misses some chunks
gc->print_contents();
return 0;