Started with a rough structure
This commit is contained in:
parent
fadb9eed0d
commit
b6ca1781ea
6 changed files with 153 additions and 11 deletions
0
src/GC/allocator.cpp
Normal file
0
src/GC/allocator.cpp
Normal file
27
src/GC/include/allocator.hpp
Normal file
27
src/GC/include/allocator.hpp
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <memory>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
namespace GC {
|
||||||
|
|
||||||
|
class Allocator {
|
||||||
|
public:
|
||||||
|
Allocator(size_t size) {
|
||||||
|
alloc_size = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t getSize() {
|
||||||
|
return alloc_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *alloc();
|
||||||
|
|
||||||
|
private:
|
||||||
|
size_t alloc_size;
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
62
src/GC/include/heap.hpp
Normal file
62
src/GC/include/heap.hpp
Normal file
|
|
@ -0,0 +1,62 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <setjmp.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "allocator.hpp"
|
||||||
|
|
||||||
|
#define HEAP_SIZE 65536
|
||||||
|
|
||||||
|
namespace GC {
|
||||||
|
|
||||||
|
class Heap {
|
||||||
|
public:
|
||||||
|
|
||||||
|
static Heap &the() {
|
||||||
|
if (s_instance)
|
||||||
|
return *s_instance;
|
||||||
|
s_instance = new Heap();
|
||||||
|
return *s_instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
~Heap() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t getHeapSize() {
|
||||||
|
return heap_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 heap_size = 0;
|
||||||
|
std::vector<Allocator *> h_allocs;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
16
src/GC/include/heap_obj.hpp
Normal file
16
src/GC/include/heap_obj.hpp
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
class HeapObj {
|
||||||
|
public:
|
||||||
|
HeapObj(size_t size_) {
|
||||||
|
size = size_;
|
||||||
|
}
|
||||||
|
|
||||||
|
~HeapObj() { }
|
||||||
|
|
||||||
|
private:
|
||||||
|
size_t size { 0 };
|
||||||
|
bool marked { false };
|
||||||
|
};
|
||||||
34
src/GC/todo.md
Normal file
34
src/GC/todo.md
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
# Garbage collection
|
||||||
|
|
||||||
|
## Algorithm
|
||||||
|
|
||||||
|
Potential algorithms:
|
||||||
|
- mark & sweep
|
||||||
|
- easy to implement
|
||||||
|
- slow
|
||||||
|
- mark & compact
|
||||||
|
- no memory fragmentation
|
||||||
|
- slow
|
||||||
|
- stop-copy algorithms (?)
|
||||||
|
- no memory fragmentation
|
||||||
|
- slow
|
||||||
|
- maybe good for FP langs?
|
||||||
|
|
||||||
|
## Type hierarchy
|
||||||
|
|
||||||
|
- Heap class
|
||||||
|
- Holds all memory
|
||||||
|
- Resizeable array?
|
||||||
|
- Singleton instance
|
||||||
|
|
||||||
|
- Allocator class
|
||||||
|
- Allocates chunks of memory
|
||||||
|
- keeps track of chunks that are available and their sizes
|
||||||
|
- Several instances of allocator class with different sizes?
|
||||||
|
|
||||||
|
- HeapObj class
|
||||||
|
- parent of all heap objects
|
||||||
|
- contains metadata
|
||||||
|
- size
|
||||||
|
- location
|
||||||
|
- marked bit
|
||||||
|
|
@ -15,8 +15,11 @@ struct ObjectHeader {
|
||||||
struct Object : ObjectHeader {
|
struct Object : ObjectHeader {
|
||||||
char name; // should be something like id, but for testing sake its char
|
char name; // should be something like id, but for testing sake its char
|
||||||
Object* child;
|
Object* child;
|
||||||
//Object(char name) {}
|
// Object(char name_) {}
|
||||||
//Object(char name, Object* child) {}
|
Object(char name_, Object* child_) {
|
||||||
|
name = name_;
|
||||||
|
child = child_;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Representing the heap as a simple struct for now
|
// Representing the heap as a simple struct for now
|
||||||
|
|
@ -57,15 +60,15 @@ class MarkSweep {
|
||||||
};
|
};
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Object* b = new Object();
|
Object* b = new Object('B', nullptr);
|
||||||
b->name = 'B';
|
// b->name = 'B';
|
||||||
b->child = nullptr;
|
// b->child = nullptr;
|
||||||
Object* c = new Object();
|
Object* c = new Object('C', b);
|
||||||
c->name = 'C';
|
// c->name = 'C';
|
||||||
c->child = b; // c -> d
|
// c->child = b; // c -> d
|
||||||
Object* d = new Object();
|
Object* d = new Object('D', nullptr);
|
||||||
d->name = 'D';
|
// d->name = 'D';
|
||||||
d->child = nullptr;
|
// d->child = nullptr;
|
||||||
|
|
||||||
//Heap* heap = new Heap{*c, *b, *d};
|
//Heap* heap = new Heap{*c, *b, *d};
|
||||||
vector<Object*> worklist = {c, b, d};
|
vector<Object*> worklist = {c, b, d};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue