Merge branch 'g-collection' of https://github.com/bachelor-group-66-systemf/language into g-collection

This commit is contained in:
valtermiari 2023-02-24 14:25:08 +01:00
commit 87f5d7fe74
22 changed files with 1406 additions and 149 deletions

View file

@ -9,8 +9,8 @@ STDFLAGS = -std=gnu++20 -stdlib=libc++
WFLAGS = -Wall -Wextra
DBGFLAGS = -g
test_test:
echo "$(shell pwd)"
advance:
$(CC) $(WFLAGS) $(STDFLAGS) tests/advance.cpp -o tests/advance.out
heap:
$(CC) $(WFLAGS) $(STDFLAGS) $(LIB_INCL) lib/heap.cpp

View file

@ -2,9 +2,9 @@
#include <assert.h>
#include <iostream>
#include <list>
#include <setjmp.h>
#include <stdlib.h>
#include <vector>
#include "chunk.hpp"
@ -30,26 +30,10 @@ namespace GC {
m_allocated_size = 0;
}
void collect(Heap *heap);
void sweep(Heap *heap);
uintptr_t *try_recycle_chunks(Heap *heap, size_t size);
void free(Heap *heap);
void free_overlap(Heap *heap);
void mark(uintptr_t *start, const uintptr_t *end, std::vector<Chunk *> worklist);
void print_line(Chunk *chunk);
void print_worklist(std::vector<Chunk *> list);
inline static Heap *m_instance = nullptr;
const char *m_heap;
size_t m_size;
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;
public:
// BEWARE only for testing, this should be adressed
~Heap() {
std::free((char *)m_heap);
}
static inline Heap *the() { // TODO: make private
if (m_instance) // if m_instance is not a nullptr
@ -58,11 +42,35 @@ namespace GC {
return m_instance;
}
// BEWARE only for testing, this should be adressed
~Heap() {
std::free((char *)m_heap);
static inline Chunk *getAt(std::list<Chunk *> list, size_t n) {
auto iter = list.begin();
if (!n)
return *iter;
std::advance(iter, n);
return *iter;
}
void collect();
void sweep(Heap *heap);
uintptr_t *try_recycle_chunks(size_t size);
void free(Heap* heap);
void free_overlap(Heap *heap);
void mark(uintptr_t *start, const uintptr_t *end, std::list<Chunk *> worklist);
void print_line(Chunk *chunk);
void print_worklist(std::list<Chunk *> list);
inline static Heap *m_instance = nullptr;
const char *m_heap;
size_t m_size;
size_t m_allocated_size;
uintptr_t *m_stack_top = nullptr;
// maybe change to std::list
std::list<Chunk *> m_allocated_chunks;
std::list<Chunk *> m_freed_chunks;
public:
/**
* These are the only two functions which are exposed
* as the API for LLVM. At the absolute start of the
@ -70,8 +78,9 @@ namespace GC {
* that the address of the topmost stack frame is
* saved as the limit for scanning the stack in collect.
*/
void *alloc(size_t size); // TODO: make static
void init(); // TODO: make static
static void init(); // TODO: make static
static void dispose(); // -||-
static void *alloc(size_t size); // -||-
// DEBUG ONLY
void collect(uint flags); // conditional collection

View file

@ -14,13 +14,21 @@ namespace GC {
/**
* Initialises the heap singleton and saves the address
* of the calling stack frame as the stack_end. Presumeably
* of the calling stack frame as the stack_top. Presumeably
* this address points to the stack frame of the compiled
* LLVM executable after linking.
*/
void Heap::init() {
Heap *heap = Heap::the();
heap->m_stack_end = reinterpret_cast<uintptr_t *>(__builtin_frame_address(1));
heap->m_stack_top = reinterpret_cast<uintptr_t *>(__builtin_frame_address(1));
}
/**
* Disposes the heap at program exit.
*/
void Heap::dispose() {
Heap *heap = Heap::the();
delete heap;
}
/**
@ -43,13 +51,13 @@ namespace GC {
}
if (heap->m_size + size > HEAP_SIZE) {
collect(heap);
heap->collect();
// If collect failed, crash with OOM error
assert(heap->m_size + size <= HEAP_SIZE && "Heap: Out Of Memory");
}
// If a chunk was recycled, return the old chunk address
uintptr_t *reused_chunk = try_recycle_chunks(heap, size);
uintptr_t *reused_chunk = heap->try_recycle_chunks(size);
if (reused_chunk != nullptr) {
return (void *)reused_chunk;
}
@ -58,7 +66,7 @@ namespace GC {
// then create a new chunk
auto new_chunk = new Chunk;
new_chunk->size = size;
new_chunk->start = (uintptr_t *)(heap->m_heap + m_size);
new_chunk->start = (uintptr_t *)(heap->m_heap + heap->m_size);
heap->m_size += size;
@ -75,8 +83,6 @@ namespace GC {
* objects slightly which saves time from malloc'ing
* memory from the OS.
*
* @param heap Pointer to the singleton Heap instance
*
* @param size Amount of bytes needed for the object
* which is about to be allocated.
*
@ -86,10 +92,14 @@ namespace GC {
* nullptr is returned to signify no
* chunks were found.
*/
uintptr_t *Heap::try_recycle_chunks(Heap *heap, size_t size) {
uintptr_t *Heap::try_recycle_chunks(size_t size) {
auto heap = Heap::the();
// Check if there are any freed chunks large enough for current request
for (size_t i = 0; i < heap->m_freed_chunks.size(); i++) {
auto cp = heap->m_freed_chunks.at(i);
// auto cp = heap->m_freed_chunks.at(i);
auto cp = getAt(heap->m_freed_chunks, i);
auto iter = heap->m_freed_chunks.begin();
advance(iter, i);
if (cp->size > size)
{
// Split the chunk, use one part and add the remaining part to
@ -100,7 +110,7 @@ namespace GC {
chunk_complement->size = diff;
chunk_complement->start = cp->start + cp->size;
heap->m_freed_chunks.erase(m_freed_chunks.begin() + i);
heap->m_freed_chunks.erase(iter);
heap->m_freed_chunks.push_back(chunk_complement);
heap->m_allocated_chunks.push_back(cp);
@ -109,7 +119,7 @@ namespace GC {
else if (cp->size == size)
{
// Reuse the whole chunk
heap->m_freed_chunks.erase(m_freed_chunks.begin() + i);
heap->m_freed_chunks.erase(iter);
heap->m_allocated_chunks.push_back(cp);
return cp->start;
}
@ -123,24 +133,23 @@ namespace GC {
* left on the heap, a collection is triggered. This
* function is private so that the user cannot trigger
* a collection unneccessarily.
*
* @param heap Heap singleton instance, only for avoiding
* redundant calls to the singleton get
*/
void Heap::collect(Heap *heap) {
void Heap::collect() {
// Get instance
auto heap = Heap::the();
// get current stack
auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
auto stack_bottom = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
// fix this block, it's nästy
uintptr_t *stack_end;
if (heap->m_stack_end != nullptr)
stack_end = heap->m_stack_end;
uintptr_t *stack_top;
if (heap->m_stack_top != nullptr)
stack_top = heap->m_stack_top;
else
stack_end = (uintptr_t *)0; // temporary
stack_top = (uintptr_t *)0; // temporary
auto work_list = heap->m_allocated_chunks;
mark(stack_start, stack_end, work_list);
mark(stack_bottom, stack_top, work_list);
sweep(heap);
@ -148,24 +157,26 @@ namespace GC {
}
/**
* Iterates through the stack, if an element on the stack points to a chunk
* that chunk is marked (i.e. reachable). It only marks element which are directly
* reachable from the chunk, so no chain of pointers from the stack are detected.
* Iterates through the stack, if an element on the stack points to a chunk,
* called a root chunk, that chunk is marked (i.e. reachable).
* Then it recursively follows all chunks which are possibly reachable from
* the root chunk and mark those chunks.
* If a chunk is marked it is removed from the worklist, since it's no longer of
* concern for this method.
*
* @param start Pointer to the start of the stack frame.
* @param end Pointer to the end of the stack frame.
* @param worklist The currently allocated chunks.
* @param worklist The currently allocated chunks, which haven't been marked.
*/
void Heap::mark(uintptr_t *start, const uintptr_t *end, vector<Chunk*> worklist) {
void Heap::mark(uintptr_t *start, const uintptr_t *end, list<Chunk*> worklist) {
int counter = 0;
// To find adresses thats in the worklist
for (; start < end; start++) {
counter++;
// all pointers must be aligned as double words
for (auto it = worklist.begin(); it != worklist.end();) {
auto it = worklist.begin();
auto stop = worklist.end();
// for (auto it = worklist.begin(); it != worklist.end();) {
while (it != stop) {
Chunk *chunk = *it;
auto c_start = reinterpret_cast<uintptr_t>(chunk->start);
@ -181,7 +192,9 @@ namespace GC {
if (!chunk->marked) {
chunk->marked = true;
// Remove the marked chunk from the worklist
it = worklist.erase(it);
// Recursively call mark, to see if the reachable chunk further points to another chunk
mark((uintptr_t*) c_start, (uintptr_t*) c_end, worklist);
}
else {
@ -198,24 +211,27 @@ namespace GC {
/**
* Sweeps the heap, unmarks the marked chunks for the next cycle,
* adds the unmarked nodes to the vector of freed chunks; to be freed.
* adds the unmarked nodes to the list of freed chunks; to be freed.
*
* @param heap Pointer to the heap to oporate on.
* @param heap Pointer to the heap singleton instance.
*/
void Heap::sweep(Heap *heap) {
for (auto it = heap->m_allocated_chunks.begin(); it != heap->m_allocated_chunks.end();) {
Chunk *chunk = *it;
auto iter = heap->m_allocated_chunks.begin();
auto stop = heap->m_allocated_chunks.end();
// for (auto it = heap->m_allocated_chunks.begin(); it != heap->m_allocated_chunks.end();) {
while (iter != stop) {
Chunk *chunk = *iter;
// Unmark the marked chunks for the next iteration.
if (chunk->marked) {
chunk->marked = false;
++it;
++iter;
}
else {
// Add the unmarked chunks to freed chunks and remove from
// the list of allocated chunks
heap->m_freed_chunks.push_back(chunk);
it = heap->m_allocated_chunks.erase(it);
iter = heap->m_allocated_chunks.erase(iter);
}
}
}
@ -257,13 +273,15 @@ namespace GC {
* larger chunks.
*/
void Heap::free_overlap(Heap *heap) {
std::vector<Chunk *> filtered;
std::list<Chunk *> filtered;
size_t i = 0;
filtered.push_back(heap->m_freed_chunks.at(i++));
// filtered.push_back(heap->m_freed_chunks.at(i++));
filtered.push_back(getAt(heap->m_freed_chunks, i++));
cout << filtered.back()->start << endl;
for (; i < heap->m_freed_chunks.size(); i++) {
auto prev = filtered.back();
auto next = heap->m_freed_chunks.at(i);
// auto next = heap->m_freed_chunks.at(i);
auto next = getAt(heap->m_freed_chunks, i);
auto p_start = (uintptr_t)(prev->start);
auto p_size = (uintptr_t)(prev->size);
auto n_start = (uintptr_t)(next->start);
@ -283,9 +301,9 @@ 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;
cout << "GC m_stack_top:\t" << heap->m_stack_top << endl;
auto stack_bottom = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
cout << "GC stack_bottom:\t" << stack_bottom << endl;
}
/**
@ -307,20 +325,20 @@ namespace GC {
auto heap = Heap::the();
// get the frame adress, whwere local variables and saved registers are located
auto stack_start = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
cout << "Stack start in collect:\t" << stack_start << endl;
uintptr_t *stack_end;
auto stack_bottom = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
cout << "Stack bottom in collect:\t" << stack_bottom << endl;
uintptr_t *stack_top;
if (heap->m_stack_end != nullptr)
stack_end = heap->m_stack_end;
if (heap->m_stack_top != nullptr)
stack_top = heap->m_stack_top;
else
stack_end = (uintptr_t *) stack_start - 80; // dummy value
stack_top = (uintptr_t *) stack_bottom + 80; // dummy value
cout << "Stack end in collect:\t " << stack_end << endl;
cout << "Stack end in collect:\t " << stack_top << endl;
auto work_list = heap->m_allocated_chunks;
if (flags & MARK) {
mark(stack_start, stack_end, work_list);
mark(stack_bottom, stack_top, work_list);
}
if (flags & SWEEP) {
@ -366,7 +384,7 @@ namespace GC {
cout << "Marked: " << chunk->marked << "\nStart adr: " << chunk->start << "\nSize: " << chunk->size << " B\n" << endl;
}
void Heap::print_worklist(std::vector<Chunk *> list) {
void Heap::print_worklist(std::list<Chunk *> list) {
for (auto cp : list) {
cout << "Chunk at:\t" << cp->start << "\nSize:\t\t" << cp->size << endl;
}

34
src/GC/tests/advance.cpp Normal file
View file

@ -0,0 +1,34 @@
#include <iostream>
#include <list>
#include <stdlib.h>
using namespace std;
int main() {
list<char> l;
char c = 'a';
for (int i = 1; i <= 5; i++) {
l.push_back(c++);
}
auto iter = l.begin();
auto stop = l.end();
while (iter != stop) {
cout << *iter << " ";
iter++;
}
cout << endl;
iter = l.begin();
while (*iter != *stop) {
cout << *iter << " ";
iter++;
}
cout << endl;
cout << "rebased" << endl;
// cout << "iter: " << *iter << "\nstop: " << *stop << endl;
return 0;
}

View file

@ -1,18 +1,15 @@
# Garbage collection
## Project
Goal for next week (24/2):
- Debug
- Write more complex tests
## GC TODO:
- Merge to main branch
- Switch std::vector to std::list
- Make alloc and init static, move the() to private
- stack_end, stack_start -> stack_top, stack_bottom
- Double check m_heap_size functionality and when a collection is triggered
- Kolla vektor vs list complexity
## Tests TODO
- Write complex datastructures for tests with larger programs