Next up is tests

Co-authored-by: ValterMiari <ValterMiari@users.noreply.github.com>
This commit is contained in:
Victor Olin 2023-02-14 15:11:58 +01:00
parent b53f892393
commit 0260b2876c
4 changed files with 80 additions and 16 deletions

4
.gitignore vendored
View file

@ -3,5 +3,9 @@ dist-newstyle
*.x
*.bak
src/Grammar
language
llvm.ll
/language
src/GC/tests/*.out

View file

@ -44,7 +44,7 @@ namespace GC {
else if (cp->size == size)
{
// sno hela chunken
m_freed_chunks.erase(m_freed_chunks.begin()+i);
m_freed_chunks.erase(m_freed_chunks.begin() + i);
m_allocated_chunks.push_back(cp);
return cp->start;
}
@ -67,14 +67,15 @@ namespace GC {
}
void Heap::collect() {
// mark all objs
// compact();
// free all unmarked (m_freed_chunks.add(jalkdsj))
// get the frame adress, whwere local variables and saved registers are located
auto stack_start = reinterpret_cast<const uintptr_t *>(__builtin_frame_address(0));
auto stack_end = reinterpret_cast<const uintptr_t *>(0ul);
// get the frame adress, whwere local variables and saved registers are located
auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
// looking at 10 stack frames back
auto stack_end = reinterpret_cast<const uintptr_t *>(__builtin_frame_address(10));
auto work_list = m_allocated_chunks;
mark(stack_start, stack_end, work_list);
compact();
//release free chunks
while (m_freed_chunks.size()) {
@ -102,14 +103,16 @@ namespace GC {
}
}
void Heap::mark(uintptr_t *start, uintptr_t *end) {
for (; start < end; start += 1) { // start < end???
for (auto chunk : m_allocated_chunks) {
void Heap::mark(uintptr_t *start, const uintptr_t *end, std::vector<Chunk*> work_list) {
for (; start < end; start++) { // to find adresses thats in the worklist
for (size_t i = 0; i < work_list.size(); i++) { // fix this
auto chunk = work_list.at(i);
if (chunk->start <= start && start < chunk->start + chunk->size) {
if (!chunk->marked) {
chunk->marked = true;
//mark(chunk) //
//TODO
work_list.erase(work_list.begin() + i);
mark(reinterpret_cast<uintptr_t *>(chunk->start + chunk->size), end, work_list); //
return;
}
}
}

View file

@ -40,7 +40,7 @@ namespace GC {
void collect();
void compact();
void mark(uintptr_t *start, uintptr_t *end);
void mark(uintptr_t *start, const uintptr_t *end, std::vector<Chunk *> worklist);
bool compareChunks(Chunk *c1, Chunk *c2);
@ -49,8 +49,8 @@ namespace GC {
size_t m_size;
size_t m_allocated_size;
std::vector<Chunk*> m_allocated_chunks;
std::vector<Chunk*> m_freed_chunks;
std::vector<Chunk *> m_allocated_chunks;
std::vector<Chunk *> m_freed_chunks;
};
}

57
src/GC/tests/stack.cpp Normal file
View file

@ -0,0 +1,57 @@
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
std::vector<uintptr_t *> iv;
void collect() {
std::cout << "in collect" << std::endl;
uintptr_t *stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
// uintptr_t *stack_end = reinterpret_cast<uintptr_t *>(__builtin_frame_address(100));
std::cout << "SP1:\t" << stack_start << "\nSP2:\t" << (stack_start - 1*sizeof(int)) << std::endl;
std::cout << "SP-:\t" << --stack_start << std::endl;
const uintptr_t *stack_end = (stack_start + 30*sizeof(int));
int vars_found = 0;
while (stack_start < stack_end) {
if (std::find(iv.begin(), iv.end(), stack_start) != iv.end()) {
vars_found++;
std::cout << "Found " << *(reinterpret_cast<unsigned long *>(stack_start)) << " at " << stack_start << std::endl;
}
// std::cout << "SP address:\t\t" << stack_start << "\nSP value:\t\t" << *(reinterpret_cast<unsigned long *>(stack_start)) << std::endl;
stack_start++;
}
if (vars_found == 0) {
std::cout << "Found nothing" << std::endl;
}
}
int add(unsigned long a, unsigned long b) {
iv.push_back(reinterpret_cast<uintptr_t *>(&a));
iv.push_back(reinterpret_cast<uintptr_t *>(&b));
std::cout << "'a':\t" << &a << "\n'b':\t" << &b << std::endl;
collect();
return a + b;
}
int main() {
unsigned long global_1 = 16;
unsigned long global_2 = 32;
iv.push_back(&global_1);
iv.push_back(&global_2);
std::cout << "'g1':\t" << &global_1 << "\n'g2':\t" << &global_2 << std::endl;
add(3,2);
return 0;
}