Code cleanup

This commit is contained in:
Victor Olin 2023-03-08 16:47:34 +01:00
parent 7bb64c0489
commit cdc802476d
6 changed files with 503 additions and 459 deletions

View file

@ -2,11 +2,11 @@
#include <stdlib.h> #include <stdlib.h>
#define CHUNK_LIST_CAP 1024 namespace GC
{
namespace GC { struct Chunk
{
struct Chunk {
bool marked; bool marked;
uintptr_t *start; uintptr_t *start;
size_t size; size_t size;

View file

@ -6,11 +6,11 @@
#include "chunk.hpp" #include "chunk.hpp"
using namespace std; namespace GC
{
namespace GC { enum GCEventType
{
enum GCEventType {
CollectStart, CollectStart,
MarkStart, MarkStart,
ChunkMarked, ChunkMarked,
@ -20,23 +20,27 @@ namespace GC {
ReusedChunk ReusedChunk
}; };
using TimeStamp = chrono::_V2::system_clock::time_point; using TimeStamp = std::chrono::_V2::system_clock::time_point;
class GCEvent { class GCEvent
{
private: private:
// make const // make const
GCEventType m_type; GCEventType m_type;
TimeStamp m_timestamp; TimeStamp m_timestamp;
Chunk *m_chunk; Chunk *m_chunk;
public: public:
GCEvent(GCEventType type) { GCEvent(GCEventType type)
{
m_type = type; m_type = type;
m_timestamp = chrono::system_clock::now(); m_timestamp = std::chrono::system_clock::now();
} }
GCEvent(GCEventType type, Chunk *chunk) { GCEvent(GCEventType type, Chunk *chunk)
{
m_type = type; m_type = type;
m_timestamp = chrono::system_clock::now(); m_timestamp = std::chrono::system_clock::now();
m_chunk = chunk; m_chunk = chunk;
} }

View file

@ -17,32 +17,37 @@
#define FREE_THRESH (uint)20 #define FREE_THRESH (uint)20
namespace GC { namespace GC
{
class Heap { class Heap
{
private: private:
// Private constructor according to the singleton pattern // Private constructor according to the singleton pattern
Heap() { Heap()
m_heap = reinterpret_cast<char *>(malloc(HEAP_SIZE)); {
m_heap = static_cast<char *>(malloc(HEAP_SIZE));
m_size = 0; m_size = 0;
m_allocated_size = 0; m_allocated_size = 0;
} }
// BEWARE only for testing, this should be adressed // BEWARE only for testing, this should be adressed
~Heap() { ~Heap()
{
std::free((char *)m_heap); std::free((char *)m_heap);
} }
inline static Heap *the() { // TODO: make private inline static Heap *the()
{ // TODO: make private
if (m_instance) // if m_instance is not a nullptr if (m_instance) // if m_instance is not a nullptr
return m_instance; return m_instance;
m_instance = new Heap(); m_instance = new Heap();
return m_instance; return m_instance;
} }
inline static Chunk *getAt(std::vector<Chunk *> list, size_t n) { inline static Chunk *getAt(std::vector<Chunk *> &list, size_t n)
{
auto iter = list.begin(); auto iter = list.begin();
if (!n) if (!n)
return *iter; return *iter;
@ -55,9 +60,9 @@ namespace GC {
uintptr_t *try_recycle_chunks(size_t size); uintptr_t *try_recycle_chunks(size_t size);
void free(Heap *heap); void free(Heap *heap);
void free_overlap(Heap *heap); void free_overlap(Heap *heap);
void mark(uintptr_t *start, const uintptr_t *end, std::vector<Chunk *> worklist); void mark(uintptr_t *start, const uintptr_t *end, std::vector<Chunk *> &worklist);
void print_line(Chunk *chunk); void print_line(Chunk *chunk);
void print_worklist(std::vector<Chunk *> list); void print_worklist(std::vector<Chunk *> &list);
inline static Heap *m_instance = nullptr; inline static Heap *m_instance = nullptr;
const char *m_heap; const char *m_heap;
@ -71,7 +76,6 @@ namespace GC {
std::vector<Chunk *> m_freed_chunks; std::vector<Chunk *> m_freed_chunks;
public: public:
/** /**
* These are the only two functions which are exposed * These are the only two functions which are exposed
* as the API for LLVM. At the absolute start of the * as the API for LLVM. At the absolute start of the
@ -84,7 +88,8 @@ namespace GC {
static void *alloc(size_t size); static void *alloc(size_t size);
// DEBUG ONLY // DEBUG ONLY
static inline Heap *debug_the() { // TODO: make private static inline Heap *debug_the()
{ // TODO: make private
if (m_instance) // if m_instance is not a nullptr if (m_instance) // if m_instance is not a nullptr
return m_instance; return m_instance;
m_instance = new Heap(); m_instance = new Heap();

View file

@ -6,8 +6,6 @@
#include "event.hpp" #include "event.hpp"
#include "heap.hpp" #include "heap.hpp"
using namespace std;
namespace GC { namespace GC {
class Profiler { class Profiler {
@ -23,7 +21,7 @@ namespace GC {
} }
inline static Profiler *m_instance = nullptr; inline static Profiler *m_instance = nullptr;
vector<GCEvent *> m_events; std::vector<GCEvent *> m_events;
public: public:
static void record(GCEventType type); static void record(GCEventType type);

View file

@ -6,8 +6,6 @@
#include "event.hpp" #include "event.hpp"
#include "heap.hpp" #include "heap.hpp"
// using namespace std;
namespace GC { namespace GC {
GCEventType GCEvent::getType() { GCEventType GCEvent::getType() {

View file

@ -8,9 +8,11 @@
#include <vector> #include <vector>
#include "../include/heap.hpp" #include "../include/heap.hpp"
using namespace std;
namespace GC { using std::cout, std::endl, std::vector, std::hex, std::dec;
namespace GC
{
/** /**
* Initialises the heap singleton and saves the address * Initialises the heap singleton and saves the address
@ -18,15 +20,17 @@ namespace GC {
* this address points to the stack frame of the compiled * this address points to the stack frame of the compiled
* LLVM executable after linking. * LLVM executable after linking.
*/ */
void Heap::init() { void Heap::init()
{
Heap *heap = Heap::the(); Heap *heap = Heap::the();
heap->m_stack_top = reinterpret_cast<uintptr_t *>(__builtin_frame_address(1)); heap->m_stack_top = static_cast<uintptr_t *>(__builtin_frame_address(1));
} }
/** /**
* Disposes the heap at program exit. * Disposes the heap at program exit.
*/ */
void Heap::dispose() { void Heap::dispose()
{
Heap *heap = Heap::the(); Heap *heap = Heap::the();
delete heap; delete heap;
} }
@ -40,17 +44,20 @@ namespace GC {
* has been allocated. This pointer is supposed * has been allocated. This pointer is supposed
* to be casted to and object pointer. * to be casted to and object pointer.
*/ */
void *Heap::alloc(size_t size) { void *Heap::alloc(size_t size)
{
// Singleton // Singleton
Heap *heap = Heap::the(); Heap *heap = Heap::the();
if (size < 0) { if (size < 0)
{
cout << "Heap: Cannot alloc less than 0B. No bytes allocated." << endl; cout << "Heap: Cannot alloc less than 0B. No bytes allocated." << endl;
return nullptr; return nullptr;
} }
if (heap->m_size + size > HEAP_SIZE) { if (heap->m_size + size > HEAP_SIZE)
{
heap->collect(); heap->collect();
// If collect failed, crash with OOM error // If collect failed, crash with OOM error
assert(heap->m_size + size <= HEAP_SIZE && "Heap: Out Of Memory"); assert(heap->m_size + size <= HEAP_SIZE && "Heap: Out Of Memory");
@ -58,8 +65,9 @@ namespace GC {
// If a chunk was recycled, return the old chunk address // If a chunk was recycled, return the old chunk address
uintptr_t *reused_chunk = heap->try_recycle_chunks(size); uintptr_t *reused_chunk = heap->try_recycle_chunks(size);
if (reused_chunk != nullptr) { if (reused_chunk != nullptr)
return (void *)reused_chunk; {
return static_cast<void *>(reused_chunk);
} }
// If no free chunks was found (reused_chunk is a nullptr), // If no free chunks was found (reused_chunk is a nullptr),
@ -92,36 +100,37 @@ namespace GC {
* nullptr is returned to signify no * nullptr is returned to signify no
* chunks were found. * chunks were found.
*/ */
uintptr_t *Heap::try_recycle_chunks(size_t size) { uintptr_t *Heap::try_recycle_chunks(size_t size)
{
auto heap = Heap::the(); auto heap = Heap::the();
// Check if there are any freed chunks large enough for current request // Check if there are any freed chunks large enough for current request
for (size_t i = 0; i < heap->m_freed_chunks.size(); i++) { for (size_t i = 0; i < heap->m_freed_chunks.size(); i++)
// auto cp = heap->m_freed_chunks.at(i); {
auto cp = getAt(heap->m_freed_chunks, i); auto chunk = getAt(heap->m_freed_chunks, i);
auto iter = heap->m_freed_chunks.begin(); auto iter = heap->m_freed_chunks.begin();
advance(iter, i); advance(iter, i);
if (cp->size > size) if (chunk->size > size)
{ {
// Split the chunk, use one part and add the remaining part to // Split the chunk, use one part and add the remaining part to
// the list of freed chunks // the list of freed chunks
size_t diff = cp->size - size; size_t diff = chunk->size - size;
auto chunk_complement = new Chunk; auto chunk_complement = new Chunk;
chunk_complement->size = diff; chunk_complement->size = diff;
chunk_complement->start = cp->start + cp->size; chunk_complement->start = chunk->start + chunk->size;
heap->m_freed_chunks.erase(iter); heap->m_freed_chunks.erase(iter);
heap->m_freed_chunks.push_back(chunk_complement); heap->m_freed_chunks.push_back(chunk_complement);
heap->m_allocated_chunks.push_back(cp); heap->m_allocated_chunks.push_back(chunk);
return cp->start; return chunk->start;
} }
else if (cp->size == size) else if (chunk->size == size)
{ {
// Reuse the whole chunk // Reuse the whole chunk
heap->m_freed_chunks.erase(iter); heap->m_freed_chunks.erase(iter);
heap->m_allocated_chunks.push_back(cp); heap->m_allocated_chunks.push_back(chunk);
return cp->start; return chunk->start;
} }
} }
return nullptr; return nullptr;
@ -134,7 +143,8 @@ namespace GC {
* function is private so that the user cannot trigger * function is private so that the user cannot trigger
* a collection unneccessarily. * a collection unneccessarily.
*/ */
void Heap::collect() { void Heap::collect()
{
// Get instance // Get instance
auto heap = Heap::the(); auto heap = Heap::the();
@ -163,15 +173,18 @@ namespace GC {
* @param end Pointer to the end of the stack frame. * @param end Pointer to the end of the stack frame.
* @param worklist The currently allocated chunks, which haven't been marked. * @param worklist The currently allocated chunks, which haven't been marked.
*/ */
void Heap::mark(uintptr_t *start, const uintptr_t *end, vector<Chunk*> worklist) { void Heap::mark(uintptr_t *start, const uintptr_t *end, vector<Chunk *> &worklist)
{
int counter = 0; int counter = 0;
// To find adresses thats in the worklist // To find adresses thats in the worklist
for (; start < end; start++) { for (; start < end; start++)
{
counter++; counter++;
auto it = worklist.begin(); auto it = worklist.begin();
auto stop = worklist.end(); auto stop = worklist.end();
// for (auto it = worklist.begin(); it != worklist.end();) { // for (auto it = worklist.begin(); it != worklist.end();) {
while (it != stop) { while (it != stop)
{
Chunk *chunk = *it; Chunk *chunk = *it;
auto c_start = reinterpret_cast<uintptr_t>(chunk->start); auto c_start = reinterpret_cast<uintptr_t>(chunk->start);
@ -180,23 +193,28 @@ namespace GC {
cout << "Start points to:\t" << hex << *start << endl; cout << "Start points to:\t" << hex << *start << endl;
cout << "Chunk start:\t\t" << hex << c_start << endl; cout << "Chunk start:\t\t" << hex << c_start << endl;
cout << "Chunk end:\t\t" << hex << c_end << "\n" << endl; cout << "Chunk end:\t\t" << hex << c_end << "\n"
<< endl;
// Check if the stack pointer aligns with the chunk // Check if the stack pointer aligns with the chunk
if (c_start <= *start && *start < c_end) { if (c_start <= *start && *start < c_end)
{
if (!chunk->marked) { if (!chunk->marked)
{
chunk->marked = true; chunk->marked = true;
// Remove the marked chunk from the worklist // Remove the marked chunk from the worklist
it = worklist.erase(it); it = worklist.erase(it);
// Recursively call mark, to see if the reachable chunk further points to another chunk // Recursively call mark, to see if the reachable chunk further points to another chunk
mark((uintptr_t *)c_start, (uintptr_t *)c_end, worklist); mark((uintptr_t *)c_start, (uintptr_t *)c_end, worklist);
} }
else { else
{
++it; ++it;
} }
} }
else { else
{
++it; ++it;
} }
} }
@ -210,19 +228,23 @@ namespace GC {
* *
* @param heap Pointer to the heap singleton instance. * @param heap Pointer to the heap singleton instance.
*/ */
void Heap::sweep(Heap *heap) { void Heap::sweep(Heap *heap)
{
auto iter = heap->m_allocated_chunks.begin(); auto iter = heap->m_allocated_chunks.begin();
auto stop = heap->m_allocated_chunks.end(); auto stop = heap->m_allocated_chunks.end();
// for (auto it = heap->m_allocated_chunks.begin(); it != heap->m_allocated_chunks.end();) { // for (auto it = heap->m_allocated_chunks.begin(); it != heap->m_allocated_chunks.end();) {
while (iter != stop) { while (iter != stop)
{
Chunk *chunk = *iter; Chunk *chunk = *iter;
// Unmark the marked chunks for the next iteration. // Unmark the marked chunks for the next iteration.
if (chunk->marked) { if (chunk->marked)
{
chunk->marked = false; chunk->marked = false;
++iter; ++iter;
} }
else { else
{
// Add the unmarked chunks to freed chunks and remove from // Add the unmarked chunks to freed chunks and remove from
// the list of allocated chunks // the list of allocated chunks
heap->m_freed_chunks.push_back(chunk); heap->m_freed_chunks.push_back(chunk);
@ -240,16 +262,20 @@ namespace GC {
* @param heap Heap singleton instance, only for avoiding * @param heap Heap singleton instance, only for avoiding
* redundant calls to the singleton get * redundant calls to the singleton get
*/ */
void Heap::free(Heap *heap) { void Heap::free(Heap *heap)
if (heap->m_freed_chunks.size() > FREE_THRESH) { {
while (heap->m_freed_chunks.size()) { if (heap->m_freed_chunks.size() > FREE_THRESH)
{
while (heap->m_freed_chunks.size())
{
auto chunk = heap->m_freed_chunks.back(); auto chunk = heap->m_freed_chunks.back();
heap->m_freed_chunks.pop_back(); heap->m_freed_chunks.pop_back();
delete chunk; delete chunk;
} }
} }
// if there are chunks but not more than FREE_THRESH // if there are chunks but not more than FREE_THRESH
else if (heap->m_freed_chunks.size()) { else if (heap->m_freed_chunks.size())
{
// essentially, always check for overlap between // essentially, always check for overlap between
// chunks before finishing the allocation // chunks before finishing the allocation
free_overlap(heap); free_overlap(heap);
@ -267,20 +293,23 @@ namespace GC {
* @note Maybe this should be changed to prioritizing * @note Maybe this should be changed to prioritizing
* larger chunks. * larger chunks.
*/ */
void Heap::free_overlap(Heap *heap) { void Heap::free_overlap(Heap *heap)
{
std::vector<Chunk *> filtered; std::vector<Chunk *> filtered;
size_t i = 0; 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++)); filtered.push_back(getAt(heap->m_freed_chunks, i++));
cout << filtered.back()->start << endl; cout << filtered.back()->start << endl;
for (; i < heap->m_freed_chunks.size(); i++) { for (; i < heap->m_freed_chunks.size(); i++)
{
auto prev = filtered.back(); 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 next = getAt(heap->m_freed_chunks, i);
auto p_start = (uintptr_t)(prev->start); auto p_start = (uintptr_t)(prev->start);
auto p_size = (uintptr_t)(prev->size); auto p_size = (uintptr_t)(prev->size);
auto n_start = (uintptr_t)(next->start); auto n_start = (uintptr_t)(next->start);
if (n_start >= (p_start + p_size)) { if (n_start >= (p_start + p_size))
{
filtered.push_back(next); filtered.push_back(next);
} }
} }
@ -293,10 +322,11 @@ namespace GC {
* Prints the result of Heap::init() and a dummy value * Prints the result of Heap::init() and a dummy value
* for the current stack frame for reference. * for the current stack frame for reference.
*/ */
void Heap::check_init() { void Heap::check_init()
{
auto heap = Heap::the(); auto heap = Heap::the();
cout << "Heap addr:\t" << heap << endl; cout << "Heap addr:\t" << heap << "\n";
cout << "GC m_stack_top:\t" << heap->m_stack_top << endl; cout << "GC m_stack_top:\t" << heap->m_stack_top << "\n";
auto stack_bottom = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0)); auto stack_bottom = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
cout << "GC stack_bottom:\t" << stack_bottom << endl; cout << "GC stack_bottom:\t" << stack_bottom << endl;
} }
@ -306,7 +336,8 @@ namespace GC {
* *
* @param flags Bitmap of flags * @param flags Bitmap of flags
*/ */
void Heap::collect(uint flags) { void Heap::collect(uint flags)
{
cout << "DEBUG COLLECT\nFLAGS: "; cout << "DEBUG COLLECT\nFLAGS: ";
if (flags & MARK) if (flags & MARK)
@ -315,43 +346,38 @@ namespace GC {
cout << "\n - SWEEP"; cout << "\n - SWEEP";
if (flags & FREE) if (flags & FREE)
cout << "\n - FREE"; cout << "\n - FREE";
cout << endl; cout << "\n";
auto heap = Heap::the(); auto heap = Heap::the();
// get the frame adress, whwere local variables and saved registers are located // get the frame adress, whwere local variables and saved registers are located
auto stack_bottom = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0)); auto stack_bottom = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
cout << "Stack bottom in collect:\t" << stack_bottom << endl; cout << "Stack bottom in collect:\t" << stack_bottom << "\n";
uintptr_t *stack_top; uintptr_t *stack_top = heap->m_stack_top;
if (heap->m_stack_top != nullptr)
stack_top = heap->m_stack_top;
else
stack_top = (uintptr_t *) stack_bottom + 80; // dummy value
cout << "Stack end in collect:\t " << stack_top << endl; cout << "Stack end in collect:\t " << stack_top << endl;
auto work_list = heap->m_allocated_chunks; auto work_list = heap->m_allocated_chunks;
if (flags & MARK) { if (flags & MARK)
mark(stack_bottom, stack_top, work_list); mark(stack_bottom, stack_top, work_list);
}
if (flags & SWEEP) { if (flags & SWEEP)
sweep(heap); sweep(heap);
}
if (flags & FREE) { if (flags & FREE)
free(heap); free(heap);
} }
}
// Mark child references from the root references // Mark child references from the root references
void mark_test(vector<Chunk *> worklist) { void mark_test(vector<Chunk *> &worklist)
while (worklist.size() > 0) { {
while (worklist.size() > 0)
{
Chunk *ref = worklist.back(); Chunk *ref = worklist.back();
worklist.pop_back(); worklist.pop_back();
Chunk *child = (Chunk *)ref; // this is probably not correct Chunk *child = (Chunk *)ref; // this is probably not correct
if (child != nullptr && !child->marked) { if (child != nullptr && !child->marked)
{
child->marked = true; child->marked = true;
worklist.push_back(child); worklist.push_back(child);
mark_test(worklist); mark_test(worklist);
@ -360,12 +386,16 @@ namespace GC {
} }
// Mark the root references and look for child references to them // Mark the root references and look for child references to them
void mark_from_roots(uintptr_t *start, const uintptr_t *end) { void mark_from_roots(uintptr_t *start, const uintptr_t *end)
{
vector<Chunk *> worklist; vector<Chunk *> worklist;
for (;start > end; start --) { for (; start > end; start--)
if (*start % 8 == 0) { // all pointers must be aligned as double words {
if (*start % 8 == 0)
{ // all pointers must be aligned as double words
Chunk *ref = (Chunk *)*start; Chunk *ref = (Chunk *)*start;
if (ref != nullptr && !ref->marked) { if (ref != nullptr && !ref->marked)
{
ref->marked = true; ref->marked = true;
worklist.push_back(ref); worklist.push_back(ref);
mark_test(worklist); mark_test(worklist);
@ -375,37 +405,46 @@ namespace GC {
} }
// For testing purposes // For testing purposes
void Heap::print_line(Chunk *chunk) { void Heap::print_line(Chunk *chunk)
cout << "Marked: " << chunk->marked << "\nStart adr: " << chunk->start << "\nSize: " << chunk->size << " B\n" << endl; {
cout << "Marked: " << chunk->marked << "\nStart adr: " << chunk->start << "\nSize: " << chunk->size << " B\n"
<< endl;
} }
void Heap::print_worklist(std::vector<Chunk *> list) { void Heap::print_worklist(std::vector<Chunk *> &list)
for (auto cp : list) { {
cout << "Chunk at:\t" << cp->start << "\nSize:\t\t" << cp->size << endl; for (auto cp : list)
} cout << "Chunk at:\t" << cp->start << "\nSize:\t\t" << cp->size << "\n";
cout << endl;
} }
void Heap::print_contents() { void Heap::print_contents()
{
auto heap = Heap::the(); auto heap = Heap::the();
if (heap->m_allocated_chunks.size()) { if (heap->m_allocated_chunks.size())
{
cout << "\nALLOCATED CHUNKS #" << dec << heap->m_allocated_chunks.size() << endl; cout << "\nALLOCATED CHUNKS #" << dec << heap->m_allocated_chunks.size() << endl;
for (auto chunk : heap->m_allocated_chunks) { for (auto chunk : heap->m_allocated_chunks)
print_line(chunk); print_line(chunk);
} }
} else { else
{
cout << "NO ALLOCATIONS\n" << endl; cout << "NO ALLOCATIONS\n" << endl;
} }
if (heap->m_freed_chunks.size()) { if (heap->m_freed_chunks.size())
{
cout << "\nFREED CHUNKS #" << dec << heap->m_freed_chunks.size() << endl; cout << "\nFREED CHUNKS #" << dec << heap->m_freed_chunks.size() << endl;
for (auto fchunk : heap->m_freed_chunks) { for (auto fchunk : heap->m_freed_chunks)
print_line(fchunk); print_line(fchunk);
} }
} else { else
{
cout << "NO FREED CHUNKS" << endl; cout << "NO FREED CHUNKS" << endl;
} }
} }
void Heap::set_profiler(bool mode) { void Heap::set_profiler(bool mode)
{
auto heap = Heap::the(); auto heap = Heap::the();
heap->m_profiler_enable = mode; heap->m_profiler_enable = mode;
} }