holy cleanup

This commit is contained in:
Victor Olin 2023-05-15 23:14:08 +02:00
parent 4d427ea00e
commit 51ffd88727
5 changed files with 26 additions and 306 deletions

View file

@ -2,11 +2,7 @@
#include <stdexcept>
#include <stdlib.h>
#include <vector>
<<<<<<< HEAD
<<<<<<< HEAD
=======
#include <unordered_map>
>>>>>>> 7e93aab6268ac896dea03dcd045b8bc249f2576c
#include <chrono>
#include <queue>
#include <set>
@ -16,18 +12,7 @@
#define time_now std::chrono::high_resolution_clock::now()
#define to_us std::chrono::duration_cast<std::chrono::microseconds>
<<<<<<< HEAD
using std::cout, std::endl, std::vector, std::hex, std::dec;
=======
#include <unordered_map>
#include "heap.hpp"
using std::cout, std::endl, std::vector, std::hex, std::dec, std::unordered_map;
>>>>>>> 74e0282 (Added Hash map marking)
=======
using std::cout, std::endl, std::vector, std::hex, std::dec, std::unordered_map;
>>>>>>> 7e93aab6268ac896dea03dcd045b8bc249f2576c
namespace GC
{
@ -111,38 +96,17 @@ namespace GC
auto stack_bottom = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
heap.collect(stack_bottom);
// If memory is not enough after collect, crash with OOM error
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
if (heap.m_size + size > HEAP_SIZE)
{
if (profiler_enabled)
Profiler::dispose();
throw std::runtime_error(std::string("Error: Heap out of memory"));
}
=======
=======
>>>>>>> 208ff86 (Fixed bug in size handling and mark hash)
=======
>>>>>>> 7e93aab6268ac896dea03dcd045b8bc249f2576c
if (heap.m_size > HEAP_SIZE)
{
throw std::runtime_error(std::string("Error: Heap out of memory"));
}
//throw std::runtime_error(std::string("Error: Heap out of memory"));
<<<<<<< HEAD
<<<<<<< HEAD
>>>>>>> 74e0282 (Added Hash map marking)
=======
=======
>>>>>>> 7e93aab6268ac896dea03dcd045b8bc249f2576c
}
if (heap.m_size + size > HEAP_SIZE)
{
if (profiler_enabled)
Profiler::dispose();
throw std::runtime_error(std::string("Error: Heap out of memory"));
>>>>>>> 208ff86 (Fixed bug in size handling and mark hash)
}
// If a chunk was recycled, return the old chunk address
@ -270,15 +234,6 @@ namespace GC
// create_table();
// mark_hash(stack_bottom, stack_top);
<<<<<<< HEAD
vector<uintptr_t *> roots;
// cout << "\nb4 find_roots\n";
find_roots(stack_bottom, roots);
// cout << "b4 mark\n";''
mark(roots);
=======
create_table();
vector<uintptr_t *> roots;
// cout << "\nb4 find_roots\n";
@ -287,7 +242,6 @@ namespace GC
// cout << "b4 mark\n";''
mark(roots);
>>>>>>> 7e93aab6268ac896dea03dcd045b8bc249f2576c
// cout << "b4 sweep\n";
sweep(heap);
@ -345,35 +299,6 @@ namespace GC
}
void Heap::find_chunks(uintptr_t *stack_addr, std::queue<std::pair<uintptr_t, uintptr_t>> &chunk_spaces)
<<<<<<< HEAD
{
auto iter = m_allocated_chunks.begin();
auto end = m_allocated_chunks.end();
while (iter != end)
{
auto chunk = *iter++;
if (chunk->m_marked)
continue;
auto c_start = reinterpret_cast<uintptr_t>(chunk->m_start);
auto c_size = reinterpret_cast<uintptr_t>(chunk->m_size);
auto c_end = reinterpret_cast<uintptr_t>(c_start + c_size);
if (c_start < *stack_addr && *stack_addr < c_end)
{
chunk->m_marked = true;
chunk_spaces.push(std::make_pair(c_start, c_end));
}
}
}
<<<<<<< HEAD
<<<<<<< HEAD
void Heap::mark_range(vector<AddrRange *> &ranges, vector<Chunk *> &worklist)
=======
>>>>>>> 7e93aab6268ac896dea03dcd045b8bc249f2576c
{
Heap &heap = Heap::the();
@ -427,139 +352,6 @@ namespace GC
}
}
void Heap::mark_hash(uintptr_t *start, const uintptr_t* const end)
{
Heap &heap = Heap::the();
bool profiler_enabled = heap.m_profiler_enable;
if (profiler_enabled)
Profiler::record(MarkStart);
<<<<<<< HEAD
auto iter = ranges.begin();
auto stop = ranges.end();
while (iter != stop)
{
auto range = *iter++;
uintptr_t *start = (uintptr_t *)range->start;
const uintptr_t *end = range->end;
if (start == nullptr)
cout << "\nstart is null\n";
for (; start <= end; start++)
{
auto wliter = worklist.begin();
auto wlstop = worklist.end();
while (wliter != wlstop)
{
Chunk *chunk = *wliter;
auto c_start = reinterpret_cast<uintptr_t>(chunk->m_start);
auto c_size = reinterpret_cast<uintptr_t>(chunk->m_size);
auto c_end = reinterpret_cast<uintptr_t>(c_start + c_size);
if (c_start <= *start && *start < c_end)
{
if (!chunk->m_marked)
{
chunk->m_marked = true;
wliter = worklist.erase(wliter);
ranges.push_back(new AddrRange((uintptr_t *)c_start, (uintptr_t *)c_end));
stop = ranges.end();
}
else
{
wliter++;
}
}
else
{
wliter++;
}
=======
void Heap::create_table()
{
Heap &heap = Heap::the();
unordered_map<uintptr_t, Chunk*> chunk_table;
for (auto chunk : heap.m_allocated_chunks) {
auto pair = std::make_pair(reinterpret_cast<uintptr_t>(chunk->m_start), chunk);
heap.m_chunk_table.insert(pair);
}
}
void Heap::mark_hash(uintptr_t *start, const uintptr_t* const end)
{
Heap &heap = Heap::the();
for (; start <= end; start++)
{
auto search = heap.m_chunk_table.find(*start);
if (search != heap.m_chunk_table.end())
{
Chunk *chunk = search->second;
auto c_start = reinterpret_cast<uintptr_t>(chunk->m_start);
auto c_size = reinterpret_cast<uintptr_t>(chunk->m_size);
auto c_end = reinterpret_cast<uintptr_t*>(c_start + c_size);
if (!chunk->m_marked)
{
chunk->m_marked = true;
mark_hash(chunk->m_start, c_end);
>>>>>>> 74e0282 (Added Hash map marking)
=======
for (; start <= end; start++)
{
auto search = heap.m_chunk_table.find(*start);
if (search != heap.m_chunk_table.end())
{
Chunk *chunk = search->second;
auto c_start = reinterpret_cast<uintptr_t>(chunk->m_start);
auto c_size = reinterpret_cast<uintptr_t>(chunk->m_size);
auto c_end = reinterpret_cast<uintptr_t*>(c_start + c_size);
if (!chunk->m_marked)
{
chunk->m_marked = true;
if (profiler_enabled)
Profiler::record(ChunkMarked, chunk);
//mark_hash(chunk->m_start, c_end);
Chunk *next = find_pointer_hash((uintptr_t *) c_start, (uintptr_t *) c_end);
while (next != NULL)
{
if (!next->m_marked)
{
next->m_marked = true;
if (profiler_enabled)
Profiler::record(ChunkMarked, chunk);
auto c_start = reinterpret_cast<uintptr_t>(next->m_start);
auto c_size = reinterpret_cast<uintptr_t>(next->m_size);
auto c_end = reinterpret_cast<uintptr_t>(c_start + c_size);
next = find_pointer_hash((uintptr_t *) c_start, (uintptr_t *) c_end);
}
}
>>>>>>> 7e93aab6268ac896dea03dcd045b8bc249f2576c
}
}
}
}
<<<<<<< HEAD
=======
>>>>>>> 74e0282 (Added Hash map marking)
=======
>>>>>>> 830d863 (bugfix)
void Heap::create_table()
{
Heap &heap = Heap::the();
unordered_map<uintptr_t, Chunk*> chunk_table;
for (auto chunk : heap.m_allocated_chunks) {
auto pair = std::make_pair(reinterpret_cast<uintptr_t>(chunk->m_start), chunk);
heap.m_chunk_table.insert(pair);
}
}
<<<<<<< HEAD
void Heap::mark_hash(uintptr_t *start, const uintptr_t* const end)
{
Heap &heap = Heap::the();
@ -606,8 +398,6 @@ namespace GC
}
}
=======
>>>>>>> 7e93aab6268ac896dea03dcd045b8bc249f2576c
/**
* Sweeps the heap, unmarks the marked chunks for the next cycle,
* adds the unmarked nodes to the list of freed chunks; to be freed.