Almost finished with 1st impl of GC
Co-authored-by: ValterMiari <ValterMiari@users.noreply.github.com>
This commit is contained in:
parent
7d91cf871a
commit
920ae04595
5 changed files with 48 additions and 123 deletions
|
|
@ -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
28
src/GC/heap.cpp
Normal 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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -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;
|
|
||||||
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -6,64 +6,37 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "allocator.hpp"
|
|
||||||
|
|
||||||
#define HEAP_SIZE 65536
|
#define HEAP_SIZE 65536
|
||||||
|
|
||||||
namespace GC {
|
namespace GC {
|
||||||
|
|
||||||
class Heap {
|
class Heap {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
// Singleton
|
||||||
static Heap &the() {
|
static Heap &the() {
|
||||||
if (s_instance)
|
if (m_instance)
|
||||||
return *s_instance;
|
return *m_instance;
|
||||||
s_instance = new Heap();
|
m_instance = new Heap();
|
||||||
return *s_instance;
|
return *m_instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
~Heap() {
|
size_t getSize();
|
||||||
for (auto *alloc : h_allocs)
|
void *alloc(size_t size);
|
||||||
delete alloc;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t getHeapSize() {
|
private:
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
// helt onödig
|
Heap() {
|
||||||
Allocator *getAllocator(size_t size) {
|
m_heap = reinterpret_cast<char *>(malloc(HEAP_SIZE));
|
||||||
for (auto *alloc : h_allocs) {
|
m_size = 0;
|
||||||
if (alloc->getSize() >= size)
|
m_allocated_size = 0;
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void collect();
|
void collect();
|
||||||
|
|
||||||
private:
|
inline static Heap *m_instance = nullptr;
|
||||||
inline static Heap *s_instance = nullptr;
|
char *m_heap;
|
||||||
|
size_t m_size;
|
||||||
Heap() {
|
size_t m_allocated_size;
|
||||||
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -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 };
|
|
||||||
};
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue