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

@ -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;
}
}