From f42ea42273cb516324ac8c0037db5a8115013d69 Mon Sep 17 00:00:00 2001 From: Victor Olin Date: Fri, 10 Feb 2023 13:47:09 +0100 Subject: [PATCH] Goal for next week --- src/GC/allocator.cpp | 15 +++++++++++++++ src/GC/include/heap.hpp | 17 ++++++++++++----- src/GC/include/heap_obj.hpp | 11 ++++++++++- src/GC/todo.md | 25 ++++++++++++++----------- 4 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/GC/allocator.cpp b/src/GC/allocator.cpp index e69de29..19face2 100644 --- a/src/GC/allocator.cpp +++ b/src/GC/allocator.cpp @@ -0,0 +1,15 @@ +#pragma once + +#include +#include +#include +#include + +#include "include/allocator.hpp" + +namespace GC { + + + + +} \ No newline at end of file diff --git a/src/GC/include/heap.hpp b/src/GC/include/heap.hpp index a8a1304..c957fa8 100644 --- a/src/GC/include/heap.hpp +++ b/src/GC/include/heap.hpp @@ -23,20 +23,27 @@ public: } ~Heap() { - + for (auto *alloc : h_allocs) + delete alloc; } size_t getHeapSize() { - return heap_size; + return size; } + // 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); + // 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(); @@ -55,7 +62,7 @@ private: } char _heap[HEAP_SIZE] = {0}; - size_t heap_size = 0; + size_t size = 0; std::vector h_allocs; }; diff --git a/src/GC/include/heap_obj.hpp b/src/GC/include/heap_obj.hpp index 21490ae..db0b98c 100644 --- a/src/GC/include/heap_obj.hpp +++ b/src/GC/include/heap_obj.hpp @@ -4,13 +4,22 @@ class HeapObj { public: - HeapObj(size_t size_) { + 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 }; }; \ No newline at end of file diff --git a/src/GC/todo.md b/src/GC/todo.md index 436ae30..5e5f0b9 100644 --- a/src/GC/todo.md +++ b/src/GC/todo.md @@ -1,5 +1,14 @@ # Garbage collection +## Project + +Goal for next week (17/2): +- Functioning garbage collector +- Test it with valgrind + +TODO: +- Merge to main branch + ## Algorithm Potential algorithms: @@ -20,15 +29,9 @@ Potential algorithms: - Holds all memory - Resizeable array? - Singleton instance + - exposes alloc -- 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 +- Heap chunks + - size + - addr + - marked bit \ No newline at end of file