Almost finished w/ 1st impl of GC

This commit is contained in:
Victor Olin 2023-02-14 11:48:04 +01:00
parent 7fd324a5b2
commit deed239879
4 changed files with 208 additions and 12 deletions

View file

@ -1,6 +1,9 @@
#pragma once
#include <algorithm>
#include <assert.h>
#include <cstring>
#include <execinfo.h>
#include <iostream>
#include <setjmp.h>
#include <stdlib.h>
@ -10,19 +13,106 @@
namespace GC {
size_t Heap::getSize() {
return m_size;
// alloc assumes that after the collect phase, the aligned memory in the heap is compacted from the start,
void *Heap::alloc(size_t size) {
auto heap = Heap::the();
assert(size > 0 && "Heap: Cannot alloc less than 0B");
if (heap.m_size + size > HEAP_SIZE) {
// try collect
collect();
assert(heap.m_size + size <= HEAP_SIZE && "Heap: Out Of Memory");
}
// kolla freed chunks innan
for (size_t i = 0; i < m_freed_chunks.size(); i++) {
auto cp = m_freed_chunks.at(i);
if (cp->size > size)
{
// dela upp chunken och sno ena delen
size_t diff = cp->size - size;
auto chunk_complement = new Chunk;
chunk_complement->size = diff;
chunk_complement->start = cp->start + cp->size;
m_freed_chunks.erase(m_freed_chunks.begin() + i);
m_freed_chunks.push_back(chunk_complement);
m_allocated_chunks.push_back(cp);
return cp->start;
}
else if (cp->size == size)
{
// sno hela chunken
m_freed_chunks.erase(m_freed_chunks.begin()+i);
m_allocated_chunks.push_back(cp);
return cp->start;
}
}
// Om inga free chunks finns, skapa ny chunk
auto new_chunk = new Chunk;
new_chunk->size = size;
new_chunk->start = (void *)m_heap + m_size;
m_size += size;
m_allocated_chunks.push_back(new_chunk);
return new_chunk->start;
}
void *Heap::alloc(size_t size) {
auto heap = Heap::the();
assert(heap.getSize() + size <= HEAP_SIZE);
return m_heap + m_size;
bool compareChunks(Chunk *c1, Chunk *c2) {
return c1->start < c2->start;
}
void Heap::collect() {
// TODO
// 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);
//release free chunks
while (m_freed_chunks.size()) {
auto chunk_pointer = m_freed_chunks.back();
m_freed_chunks.pop_back();
delete chunk_pointer;
}
}
void Heap::compact() {
// sort alloced_chunks by their start-addresses
std::sort(m_allocated_chunks.begin(), m_allocated_chunks.end(), compareChunks);
// move all chunks to the start of the heap
auto heap_curr = (char *)m_heap;
for (auto space : m_allocated_chunks) {
if (space->start != heap_curr) {
memmove(heap_curr, space->start, space->size);
space->start = heap_curr;
heap_curr += space->size;
} else {
heap_curr += space->size;
}
}
}
}
void Heap::mark(uintptr_t *start, uintptr_t *end) {
for (; start < end; start += 1) { // start < end???
for (auto chunk : m_allocated_chunks) {
if (chunk->start <= start && start < chunk->start + chunk->size) {
if (!chunk->marked) {
chunk->marked = true;
//mark(chunk) //
//TODO
}
}
}
}
}
}

15
src/GC/include/chunk.hpp Normal file
View file

@ -0,0 +1,15 @@
#pragma once
#include <stdlib.h>
#define CHUNK_LIST_CAP 1024
namespace GC {
struct Chunk {
bool marked;
void *start;
size_t size;
};
}

View file

@ -5,6 +5,9 @@
#include <setjmp.h>
#include <stdlib.h>
#include <vector>
using namespace std;
#include "include/chunk.hpp"
#define HEAP_SIZE 65536
@ -21,7 +24,10 @@ namespace GC {
return *m_instance;
}
size_t getSize();
~Heap() {
free((char *)m_heap);
}
void *alloc(size_t size);
private:
@ -33,10 +39,18 @@ namespace GC {
}
void collect();
void compact();
void mark(uintptr_t *start, uintptr_t *end);
bool compareChunks(Chunk *c1, Chunk *c2);
inline static Heap *m_instance = nullptr;
char *m_heap;
const char *m_heap;
size_t m_size;
size_t m_allocated_size;
}
std::vector<Chunk*> m_allocated_chunks;
std::vector<Chunk*> m_freed_chunks;
};
}