Almost finished with 1st impl of GC

Co-authored-by: ValterMiari <ValterMiari@users.noreply.github.com>
This commit is contained in:
Victor Olin 2023-02-14 11:47:52 +01:00
parent f42ea42273
commit 7fd324a5b2
5 changed files with 48 additions and 123 deletions

View file

@ -1,15 +0,0 @@
#pragma once
#include <assert.h>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include "include/allocator.hpp"
namespace GC {
}

28
src/GC/heap.cpp Normal file
View file

@ -0,0 +1,28 @@
#pragma once
#include <assert.h>
#include <iostream>
#include <setjmp.h>
#include <stdlib.h>
#include <vector>
#include "include/heap.hpp"
namespace GC {
size_t Heap::getSize() {
return m_size;
}
void *Heap::alloc(size_t size) {
auto heap = Heap::the();
assert(heap.getSize() + size <= HEAP_SIZE);
return m_heap + m_size;
}
void Heap::collect() {
// TODO
}
}

View file

@ -1,36 +0,0 @@
#pragma once
#include <assert.h>
#include <memory>
#include <stdlib.h>
#include "include/heap.hpp"
namespace GC {
class Allocator {
public:
Allocator(size_t size) {
alloc_size = size;
}
~Allocator() { }
size_t getSize() {
return alloc_size;
}
void *alloc() {
auto heap = Heap::the();
assert(heap.getHeapSize() + alloc_size <= HEAP_SIZE);
}
private:
size_t alloc_size;
};
}

View file

@ -6,64 +6,37 @@
#include <stdlib.h>
#include <vector>
#include "allocator.hpp"
#define HEAP_SIZE 65536
namespace GC {
class Heap {
public:
class Heap {
public:
// Singleton
static Heap &the() {
if (s_instance)
return *s_instance;
s_instance = new Heap();
return *s_instance;
if (m_instance)
return *m_instance;
m_instance = new Heap();
return *m_instance;
}
~Heap() {
for (auto *alloc : h_allocs)
delete alloc;
}
size_t getSize();
void *alloc(size_t size);
size_t getHeapSize() {
return size;
}
private:
// helt onödig
Allocator *getAllocator(size_t size) {
for (auto *alloc : h_allocs) {
if (alloc->getSize() >= size)
return alloc;
}
// std::cout << "Object too big" << std::endl;
assert(false && "TODO: Object too big");
}
void *alloc(size_t size) {
auto allocator = getAllocator(size);
return allocator->alloc();
Heap() {
m_heap = reinterpret_cast<char *>(malloc(HEAP_SIZE));
m_size = 0;
m_allocated_size = 0;
}
void collect();
private:
inline static Heap *s_instance = nullptr;
Heap() {
h_allocs.push_back(new Allocator(16));
h_allocs.push_back(new Allocator(32));
h_allocs.push_back(new Allocator(64));
h_allocs.push_back(new Allocator(128));
h_allocs.push_back(new Allocator(256));
h_allocs.push_back(new Allocator(512));
h_allocs.push_back(new Allocator(1024));
}
char _heap[HEAP_SIZE] = {0};
size_t size = 0;
std::vector<Allocator *> h_allocs;
};
inline static Heap *m_instance = nullptr;
char *m_heap;
size_t m_size;
size_t m_allocated_size;
}
}

View file

@ -1,25 +0,0 @@
#pragma once
#include <cstdlib>
class HeapObj {
public:
HeapObj(void *start_, size_t size_) {
start = start_;
size = size_;
}
~HeapObj() { }
void *getAddr() { return start; }
size_t getSize() { return size; }
bool isMarked() { return marked; }
void mark() { marked = true; }
private:
void *start = 0;
size_t size { 0 };
bool marked { false };
};