From 17d41a408dd7ae4604c030080beb3384d9a1184a Mon Sep 17 00:00:00 2001 From: Victor Olin Date: Thu, 9 Feb 2023 13:18:58 +0100 Subject: [PATCH] Started with a rough structure --- src/GC/allocator.cpp | 0 src/GC/include/allocator.hpp | 27 ++++++++++++++++ src/GC/include/heap.hpp | 62 ++++++++++++++++++++++++++++++++++++ src/GC/include/heap_obj.hpp | 16 ++++++++++ src/GC/todo.md | 34 ++++++++++++++++++++ src/MarkSweep.cpp | 25 ++++++++------- 6 files changed, 153 insertions(+), 11 deletions(-) create mode 100644 src/GC/allocator.cpp create mode 100644 src/GC/include/allocator.hpp create mode 100644 src/GC/include/heap.hpp create mode 100644 src/GC/include/heap_obj.hpp create mode 100644 src/GC/todo.md diff --git a/src/GC/allocator.cpp b/src/GC/allocator.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/GC/include/allocator.hpp b/src/GC/include/allocator.hpp new file mode 100644 index 0000000..bd30e17 --- /dev/null +++ b/src/GC/include/allocator.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include +#include +#include + +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; + + +}; + +} \ No newline at end of file diff --git a/src/GC/include/heap.hpp b/src/GC/include/heap.hpp new file mode 100644 index 0000000..a8a1304 --- /dev/null +++ b/src/GC/include/heap.hpp @@ -0,0 +1,62 @@ +#pragma once + +#include +#include +#include +#include +#include + +#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 h_allocs; +}; + +} \ No newline at end of file diff --git a/src/GC/include/heap_obj.hpp b/src/GC/include/heap_obj.hpp new file mode 100644 index 0000000..21490ae --- /dev/null +++ b/src/GC/include/heap_obj.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include + +class HeapObj { +public: + HeapObj(size_t size_) { + size = size_; + } + + ~HeapObj() { } + +private: + size_t size { 0 }; + bool marked { false }; +}; \ No newline at end of file diff --git a/src/GC/todo.md b/src/GC/todo.md new file mode 100644 index 0000000..436ae30 --- /dev/null +++ b/src/GC/todo.md @@ -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 \ No newline at end of file diff --git a/src/MarkSweep.cpp b/src/MarkSweep.cpp index 706f24f..ab219d2 100644 --- a/src/MarkSweep.cpp +++ b/src/MarkSweep.cpp @@ -15,8 +15,11 @@ struct ObjectHeader { struct Object : ObjectHeader { char name; // should be something like id, but for testing sake its char Object* child; - //Object(char name) {} - //Object(char name, Object* child) {} + // Object(char name_) {} + Object(char name_, Object* child_) { + name = name_; + child = child_; + } }; // Representing the heap as a simple struct for now @@ -57,15 +60,15 @@ class MarkSweep { }; int main() { - Object* b = new Object(); - b->name = 'B'; - b->child = nullptr; - Object* c = new Object(); - c->name = 'C'; - c->child = b; // c -> d - Object* d = new Object(); - d->name = 'D'; - d->child = nullptr; + Object* b = new Object('B', nullptr); + // b->name = 'B'; + // b->child = nullptr; + Object* c = new Object('C', b); + // c->name = 'C'; + // c->child = b; // c -> d + Object* d = new Object('D', nullptr); + // d->name = 'D'; + // d->child = nullptr; //Heap* heap = new Heap{*c, *b, *d}; vector worklist = {c, b, d};