From a684fe1ea07edbbf502165fa349548befbd673fb Mon Sep 17 00:00:00 2001 From: Victor Olin Date: Fri, 24 Feb 2023 12:01:37 +0100 Subject: [PATCH] Made exposed endpoints static --- src/GC/Makefile | 4 +-- src/GC/include/heap.hpp | 61 ++++++++++++++++++-------------- src/GC/lib/heap.cpp | 76 +++++++++++++++++++++++++--------------- src/GC/tests/advance.cpp | 33 +++++++++++++++++ src/GC/tests/h_test.cpp | 6 ++-- src/GC/todo.md | 3 +- 6 files changed, 121 insertions(+), 62 deletions(-) create mode 100644 src/GC/tests/advance.cpp diff --git a/src/GC/Makefile b/src/GC/Makefile index 354adad..92b02e8 100644 --- a/src/GC/Makefile +++ b/src/GC/Makefile @@ -9,8 +9,8 @@ STDFLAGS = -std=gnu++20 -stdlib=libc++ WFLAGS = -Wall -Wextra DBGFLAGS = -g -test_test: - echo "$(shell pwd)" +advance: + $(CC) $(WFLAGS) $(STDFLAGS) tests/advance.cpp -o tests/advance.out heap: $(CC) $(WFLAGS) $(STDFLAGS) $(LIB_INCL) lib/heap.cpp diff --git a/src/GC/include/heap.hpp b/src/GC/include/heap.hpp index 30f1a7a..cd0b142 100644 --- a/src/GC/include/heap.hpp +++ b/src/GC/include/heap.hpp @@ -2,9 +2,9 @@ #include #include +#include #include #include -#include #include "chunk.hpp" @@ -30,26 +30,10 @@ namespace GC { m_allocated_size = 0; } - void collect(Heap *heap); - void sweep(Heap *heap); - uintptr_t *try_recycle_chunks(Heap *heap, size_t size); - void free(Heap *heap); - void free_overlap(Heap *heap); - void mark(uintptr_t *start, const uintptr_t *end, std::vector worklist); - void print_line(Chunk *chunk); - void print_worklist(std::vector list); - - inline static Heap *m_instance = nullptr; - const char *m_heap; - size_t m_size; - size_t m_allocated_size; - uintptr_t *m_stack_end = nullptr; - - // maybe change to std::list - std::vector m_allocated_chunks; - std::vector m_freed_chunks; - - public: + // BEWARE only for testing, this should be adressed + ~Heap() { + std::free((char *)m_heap); + } static inline Heap *the() { // TODO: make private if (m_instance) // if m_instance is not a nullptr @@ -58,11 +42,35 @@ namespace GC { return m_instance; } - // BEWARE only for testing, this should be adressed - ~Heap() { - std::free((char *)m_heap); + static inline Chunk *getAt(std::list list, size_t n) { + auto iter = list.begin(); + if (!n) + return *iter; + std::advance(iter, n); + return *iter; } + void collect(); + void sweep(Heap *heap); + uintptr_t *try_recycle_chunks(size_t size); + void free(Heap* heap); + void free_overlap(Heap *heap); + void mark(uintptr_t *start, const uintptr_t *end, std::list worklist); + void print_line(Chunk *chunk); + void print_worklist(std::list list); + + inline static Heap *m_instance = nullptr; + const char *m_heap; + size_t m_size; + size_t m_allocated_size; + uintptr_t *m_stack_end = nullptr; + + // maybe change to std::list + std::list m_allocated_chunks; + std::list m_freed_chunks; + + public: + /** * These are the only two functions which are exposed * as the API for LLVM. At the absolute start of the @@ -70,8 +78,9 @@ namespace GC { * that the address of the topmost stack frame is * saved as the limit for scanning the stack in collect. */ - void *alloc(size_t size); // TODO: make static - void init(); // TODO: make static + static void init(); // TODO: make static + static void dispose(); // -||- + static void *alloc(size_t size); // -||- // DEBUG ONLY void collect(uint flags); // conditional collection diff --git a/src/GC/lib/heap.cpp b/src/GC/lib/heap.cpp index 3d37a3c..cf12a24 100644 --- a/src/GC/lib/heap.cpp +++ b/src/GC/lib/heap.cpp @@ -23,6 +23,14 @@ namespace GC { heap->m_stack_end = reinterpret_cast(__builtin_frame_address(1)); } + /** + * Disposes the heap at program exit. + */ + void Heap::dispose() { + Heap *heap = Heap::the(); + delete heap; + } + /** * Allocates a given amount of bytes on the heap. * @@ -43,13 +51,13 @@ namespace GC { } if (heap->m_size + size > HEAP_SIZE) { - collect(heap); + heap->collect(); // If collect failed, crash with OOM error assert(heap->m_size + size <= HEAP_SIZE && "Heap: Out Of Memory"); } // If a chunk was recycled, return the old chunk address - uintptr_t *reused_chunk = try_recycle_chunks(heap, size); + uintptr_t *reused_chunk = heap->try_recycle_chunks(size); if (reused_chunk != nullptr) { return (void *)reused_chunk; } @@ -58,7 +66,7 @@ namespace GC { // then create a new chunk auto new_chunk = new Chunk; new_chunk->size = size; - new_chunk->start = (uintptr_t *)(heap->m_heap + m_size); + new_chunk->start = (uintptr_t *)(heap->m_heap + heap->m_size); heap->m_size += size; @@ -75,8 +83,6 @@ namespace GC { * objects slightly which saves time from malloc'ing * memory from the OS. * - * @param heap Pointer to the singleton Heap instance - * * @param size Amount of bytes needed for the object * which is about to be allocated. * @@ -86,10 +92,14 @@ namespace GC { * nullptr is returned to signify no * chunks were found. */ - uintptr_t *Heap::try_recycle_chunks(Heap *heap, size_t size) { + uintptr_t *Heap::try_recycle_chunks(size_t size) { + auto heap = Heap::the(); // Check if there are any freed chunks large enough for current request for (size_t i = 0; i < heap->m_freed_chunks.size(); i++) { - auto cp = heap->m_freed_chunks.at(i); + // auto cp = heap->m_freed_chunks.at(i); + auto cp = getAt(heap->m_freed_chunks, i); + auto iter = heap->m_freed_chunks.begin(); + advance(iter, i); if (cp->size > size) { // Split the chunk, use one part and add the remaining part to @@ -100,7 +110,7 @@ namespace GC { chunk_complement->size = diff; chunk_complement->start = cp->start + cp->size; - heap->m_freed_chunks.erase(m_freed_chunks.begin() + i); + heap->m_freed_chunks.erase(iter); heap->m_freed_chunks.push_back(chunk_complement); heap->m_allocated_chunks.push_back(cp); @@ -109,7 +119,7 @@ namespace GC { else if (cp->size == size) { // Reuse the whole chunk - heap->m_freed_chunks.erase(m_freed_chunks.begin() + i); + heap->m_freed_chunks.erase(iter); heap->m_allocated_chunks.push_back(cp); return cp->start; } @@ -123,11 +133,10 @@ namespace GC { * left on the heap, a collection is triggered. This * function is private so that the user cannot trigger * a collection unneccessarily. - * - * @param heap Heap singleton instance, only for avoiding - * redundant calls to the singleton get */ - void Heap::collect(Heap *heap) { + void Heap::collect() { + // Get instance + auto heap = Heap::the(); // get current stack auto stack_start = reinterpret_cast(__builtin_frame_address(0)); @@ -148,24 +157,26 @@ namespace GC { } /** - * Iterates through the stack, if an element on the stack points to a chunk - * that chunk is marked (i.e. reachable). It only marks element which are directly - * reachable from the chunk, so no chain of pointers from the stack are detected. + * Iterates through the stack, if an element on the stack points to a chunk, + * called a root chunk, that chunk is marked (i.e. reachable). + * Then it recursively follows all chunks which are possibly reachable from + * the root chunk and mark those chunks. * If a chunk is marked it is removed from the worklist, since it's no longer of * concern for this method. * * @param start Pointer to the start of the stack frame. * @param end Pointer to the end of the stack frame. - * @param worklist The currently allocated chunks. + * @param worklist The currently allocated chunks, which haven't been marked. */ - void Heap::mark(uintptr_t *start, const uintptr_t *end, vector worklist) { + void Heap::mark(uintptr_t *start, const uintptr_t *end, list worklist) { int counter = 0; // To find adresses thats in the worklist for (; start < end; start++) { counter++; - // all pointers must be aligned as double words - - for (auto it = worklist.begin(); it != worklist.end();) { + auto it = worklist.begin(); + auto stop = worklist.end(); + // for (auto it = worklist.begin(); it != worklist.end();) { + while (it != stop) { Chunk *chunk = *it; auto c_start = reinterpret_cast(chunk->start); @@ -181,7 +192,9 @@ namespace GC { if (!chunk->marked) { chunk->marked = true; + // Remove the marked chunk from the worklist it = worklist.erase(it); + // Recursively call mark, to see if the reachable chunk further points to another chunk mark((uintptr_t*) c_start, (uintptr_t*) c_end, worklist); } else { @@ -203,19 +216,22 @@ namespace GC { * @param heap Pointer to the heap to oporate on. */ void Heap::sweep(Heap *heap) { - for (auto it = heap->m_allocated_chunks.begin(); it != heap->m_allocated_chunks.end();) { - Chunk *chunk = *it; + auto iter = heap->m_allocated_chunks.begin(); + auto stop = heap->m_allocated_chunks.end(); + // for (auto it = heap->m_allocated_chunks.begin(); it != heap->m_allocated_chunks.end();) { + while (iter != stop) { + Chunk *chunk = *iter; // Unmark the marked chunks for the next iteration. if (chunk->marked) { chunk->marked = false; - ++it; + ++iter; } else { // Add the unmarked chunks to freed chunks and remove from // the list of allocated chunks heap->m_freed_chunks.push_back(chunk); - it = heap->m_allocated_chunks.erase(it); + iter = heap->m_allocated_chunks.erase(iter); } } } @@ -257,13 +273,15 @@ namespace GC { * larger chunks. */ void Heap::free_overlap(Heap *heap) { - std::vector filtered; + std::list filtered; size_t i = 0; - filtered.push_back(heap->m_freed_chunks.at(i++)); + // filtered.push_back(heap->m_freed_chunks.at(i++)); + filtered.push_back(getAt(heap->m_freed_chunks, i++)); cout << filtered.back()->start << endl; for (; i < heap->m_freed_chunks.size(); i++) { auto prev = filtered.back(); - auto next = heap->m_freed_chunks.at(i); + // auto next = heap->m_freed_chunks.at(i); + auto next = getAt(heap->m_freed_chunks, i); auto p_start = (uintptr_t)(prev->start); auto p_size = (uintptr_t)(prev->size); auto n_start = (uintptr_t)(next->start); @@ -366,7 +384,7 @@ namespace GC { cout << "Marked: " << chunk->marked << "\nStart adr: " << chunk->start << "\nSize: " << chunk->size << " B\n" << endl; } - void Heap::print_worklist(std::vector list) { + void Heap::print_worklist(std::list list) { for (auto cp : list) { cout << "Chunk at:\t" << cp->start << "\nSize:\t\t" << cp->size << endl; } diff --git a/src/GC/tests/advance.cpp b/src/GC/tests/advance.cpp new file mode 100644 index 0000000..632b447 --- /dev/null +++ b/src/GC/tests/advance.cpp @@ -0,0 +1,33 @@ +#include +#include +#include + +using namespace std; + +int main() { + list l; + char c = 'a'; + for (int i = 1; i <= 5; i++) { + l.push_back(c++); + } + + auto iter = l.begin(); + auto stop = l.end(); + + while (iter != stop) { + cout << *iter << " "; + + iter++; + } + cout << endl; + iter = l.begin(); + while (*iter != *stop) { + cout << *iter << " "; + iter++; + } + cout << endl; + + // cout << "iter: " << *iter << "\nstop: " << *stop << endl; + + return 0; +} \ No newline at end of file diff --git a/src/GC/tests/h_test.cpp b/src/GC/tests/h_test.cpp index dfba75c..ac55bf7 100644 --- a/src/GC/tests/h_test.cpp +++ b/src/GC/tests/h_test.cpp @@ -17,7 +17,7 @@ Node *create_chain(int depth) { for (int i = 0; i < depth; i++) { Node *node = static_cast(gc->alloc(sizeof(Node))); node->id = depth-i; - node->child = nodes[i-1]; + node->child = nodes[i]; nodes.push_back(node); } for (size_t i = 0; i < nodes.size(); i++) { @@ -83,8 +83,8 @@ int main() { longs[i] = static_cast(gc->alloc(sizeof(long))); } */ - Node *root = static_cast(gc->alloc(sizeof(Node))); - root = test_chain(3, false); + //Node *root = static_cast(gc->alloc(sizeof(Node))); + Node *root = test_chain(100, true); std::cout << "Adress of root:\t" << &root << std::endl; std::cout << "Root points to:\t" << root << std::endl; std::cout << "Root child:\t" << root->child << std::endl; diff --git a/src/GC/todo.md b/src/GC/todo.md index dba3eee..c948b6e 100644 --- a/src/GC/todo.md +++ b/src/GC/todo.md @@ -9,10 +9,9 @@ Goal for next week (24/2): ## GC TODO: - Merge to main branch -- Switch std::vector to std::list -- Make alloc and init static, move the() to private - stack_end, stack_start -> stack_top, stack_bottom - Double check m_heap_size functionality and when a collection is triggered +- Kolla vektor vs list complexity ## Tests TODO