diff --git a/src/GC/allocator.cpp b/src/GC/allocator.cpp deleted file mode 100644 index 19face2..0000000 --- a/src/GC/allocator.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include "include/allocator.hpp" - -namespace GC { - - - - -} \ No newline at end of file diff --git a/src/GC/heap.cpp b/src/GC/heap.cpp new file mode 100644 index 0000000..194a553 --- /dev/null +++ b/src/GC/heap.cpp @@ -0,0 +1,28 @@ +#pragma once + +#include +#include +#include +#include +#include + +#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 + } + +} diff --git a/src/GC/include/allocator.hpp b/src/GC/include/allocator.hpp deleted file mode 100644 index 2f8a991..0000000 --- a/src/GC/include/allocator.hpp +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once - -#include -#include -#include - -#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; - - -}; - -} \ No newline at end of file diff --git a/src/GC/include/heap.hpp b/src/GC/include/heap.hpp index c957fa8..16c4989 100644 --- a/src/GC/include/heap.hpp +++ b/src/GC/include/heap.hpp @@ -6,64 +6,37 @@ #include #include -#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(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 h_allocs; -}; - + + inline static Heap *m_instance = nullptr; + char *m_heap; + size_t m_size; + size_t m_allocated_size; + } } \ No newline at end of file diff --git a/src/GC/include/heap_obj.hpp b/src/GC/include/heap_obj.hpp deleted file mode 100644 index db0b98c..0000000 --- a/src/GC/include/heap_obj.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include - -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 }; -}; \ No newline at end of file