Code cleanup
This commit is contained in:
parent
7bb64c0489
commit
cdc802476d
6 changed files with 503 additions and 459 deletions
|
|
@ -2,14 +2,14 @@
|
||||||
|
|
||||||
#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;
|
};
|
||||||
};
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,91 +8,96 @@
|
||||||
|
|
||||||
#include "chunk.hpp"
|
#include "chunk.hpp"
|
||||||
|
|
||||||
#define HEAP_SIZE 65536
|
#define HEAP_SIZE 65536
|
||||||
|
|
||||||
#define MARK (uint) 0x1
|
#define MARK (uint)0x1
|
||||||
#define SWEEP (uint) 0x2
|
#define SWEEP (uint)0x2
|
||||||
#define FREE (uint) 0x4
|
#define FREE (uint)0x4
|
||||||
#define COLLECT_ALL (uint) 0x7
|
#define COLLECT_ALL (uint)0x7
|
||||||
|
|
||||||
#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
|
||||||
|
Heap()
|
||||||
|
{
|
||||||
|
m_heap = static_cast<char *>(malloc(HEAP_SIZE));
|
||||||
|
m_size = 0;
|
||||||
|
m_allocated_size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
//Private constructor according to the singleton pattern
|
// BEWARE only for testing, this should be adressed
|
||||||
Heap() {
|
~Heap()
|
||||||
m_heap = reinterpret_cast<char *>(malloc(HEAP_SIZE));
|
{
|
||||||
m_size = 0;
|
std::free((char *)m_heap);
|
||||||
m_allocated_size = 0;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// BEWARE only for testing, this should be adressed
|
inline static Heap *the()
|
||||||
~Heap() {
|
{ // TODO: make private
|
||||||
std::free((char *)m_heap);
|
if (m_instance) // if m_instance is not a nullptr
|
||||||
}
|
return m_instance;
|
||||||
|
m_instance = new Heap();
|
||||||
|
return m_instance;
|
||||||
|
}
|
||||||
|
|
||||||
inline static Heap *the() { // TODO: make private
|
inline static Chunk *getAt(std::vector<Chunk *> &list, size_t n)
|
||||||
if (m_instance) // if m_instance is not a nullptr
|
{
|
||||||
return m_instance;
|
auto iter = list.begin();
|
||||||
m_instance = new Heap();
|
if (!n)
|
||||||
return m_instance;
|
return *iter;
|
||||||
}
|
std::advance(iter, n);
|
||||||
|
return *iter;
|
||||||
|
}
|
||||||
|
|
||||||
inline static Chunk *getAt(std::vector<Chunk *> list, size_t n) {
|
void collect();
|
||||||
auto iter = list.begin();
|
void sweep(Heap *heap);
|
||||||
if (!n)
|
uintptr_t *try_recycle_chunks(size_t size);
|
||||||
return *iter;
|
void free(Heap *heap);
|
||||||
std::advance(iter, n);
|
void free_overlap(Heap *heap);
|
||||||
return *iter;
|
void mark(uintptr_t *start, const uintptr_t *end, std::vector<Chunk *> &worklist);
|
||||||
}
|
void print_line(Chunk *chunk);
|
||||||
|
void print_worklist(std::vector<Chunk *> &list);
|
||||||
|
|
||||||
void collect();
|
inline static Heap *m_instance = nullptr;
|
||||||
void sweep(Heap *heap);
|
const char *m_heap;
|
||||||
uintptr_t *try_recycle_chunks(size_t size);
|
size_t m_size;
|
||||||
void free(Heap* heap);
|
size_t m_allocated_size;
|
||||||
void free_overlap(Heap *heap);
|
uintptr_t *m_stack_top = nullptr;
|
||||||
void mark(uintptr_t *start, const uintptr_t *end, std::vector<Chunk *> worklist);
|
bool m_profiler_enable = false;
|
||||||
void print_line(Chunk *chunk);
|
|
||||||
void print_worklist(std::vector<Chunk *> list);
|
|
||||||
|
|
||||||
inline static Heap *m_instance = nullptr;
|
// maybe change to std::list
|
||||||
const char *m_heap;
|
std::vector<Chunk *> m_allocated_chunks;
|
||||||
size_t m_size;
|
std::vector<Chunk *> m_freed_chunks;
|
||||||
size_t m_allocated_size;
|
|
||||||
uintptr_t *m_stack_top = nullptr;
|
|
||||||
bool m_profiler_enable = false;
|
|
||||||
|
|
||||||
// maybe change to std::list
|
public:
|
||||||
std::vector<Chunk *> m_allocated_chunks;
|
/**
|
||||||
std::vector<Chunk *> m_freed_chunks;
|
* These are the only two functions which are exposed
|
||||||
|
* as the API for LLVM. At the absolute start of the
|
||||||
|
* program the developer has to call init() to ensure
|
||||||
|
* that the address of the topmost stack frame is
|
||||||
|
* saved as the limit for scanning the stack in collect.
|
||||||
|
*/
|
||||||
|
static void init();
|
||||||
|
static void dispose();
|
||||||
|
static void *alloc(size_t size);
|
||||||
|
|
||||||
public:
|
// DEBUG ONLY
|
||||||
|
static inline Heap *debug_the()
|
||||||
/**
|
{ // TODO: make private
|
||||||
* These are the only two functions which are exposed
|
if (m_instance) // if m_instance is not a nullptr
|
||||||
* as the API for LLVM. At the absolute start of the
|
return m_instance;
|
||||||
* program the developer has to call init() to ensure
|
m_instance = new Heap();
|
||||||
* that the address of the topmost stack frame is
|
return m_instance;
|
||||||
* saved as the limit for scanning the stack in collect.
|
}
|
||||||
*/
|
void collect(uint flags); // conditional collection
|
||||||
static void init();
|
void check_init(); // print dummy things
|
||||||
static void dispose();
|
void print_contents(); // print dummy things
|
||||||
static void *alloc(size_t size);
|
void set_profiler(bool mode);
|
||||||
|
};
|
||||||
// DEBUG ONLY
|
|
||||||
static inline Heap *debug_the() { // TODO: make private
|
|
||||||
if (m_instance) // if m_instance is not a nullptr
|
|
||||||
return m_instance;
|
|
||||||
m_instance = new Heap();
|
|
||||||
return m_instance;
|
|
||||||
}
|
|
||||||
void collect(uint flags); // conditional collection
|
|
||||||
void check_init(); // print dummy things
|
|
||||||
void print_contents(); // print dummy things
|
|
||||||
void set_profiler(bool mode);
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
@ -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);
|
||||||
|
|
|
||||||
|
|
@ -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() {
|
||||||
|
|
|
||||||
|
|
@ -8,405 +8,444 @@
|
||||||
#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
|
{
|
||||||
* of the calling stack frame as the stack_top. Presumeably
|
|
||||||
* this address points to the stack frame of the compiled
|
|
||||||
* LLVM executable after linking.
|
|
||||||
*/
|
|
||||||
void Heap::init() {
|
|
||||||
Heap *heap = Heap::the();
|
|
||||||
heap->m_stack_top = reinterpret_cast<uintptr_t *>(__builtin_frame_address(1));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disposes the heap at program exit.
|
* Initialises the heap singleton and saves the address
|
||||||
*/
|
* of the calling stack frame as the stack_top. Presumeably
|
||||||
void Heap::dispose() {
|
* this address points to the stack frame of the compiled
|
||||||
Heap *heap = Heap::the();
|
* LLVM executable after linking.
|
||||||
delete heap;
|
*/
|
||||||
}
|
void Heap::init()
|
||||||
|
{
|
||||||
|
Heap *heap = Heap::the();
|
||||||
|
heap->m_stack_top = static_cast<uintptr_t *>(__builtin_frame_address(1));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allocates a given amount of bytes on the heap.
|
* Disposes the heap at program exit.
|
||||||
*
|
*/
|
||||||
* @param size The amount of bytes to be allocated.
|
void Heap::dispose()
|
||||||
*
|
{
|
||||||
* @return A pointer to the address where the memory
|
Heap *heap = Heap::the();
|
||||||
* has been allocated. This pointer is supposed
|
delete heap;
|
||||||
* to be casted to and object pointer.
|
}
|
||||||
*/
|
|
||||||
void *Heap::alloc(size_t size) {
|
|
||||||
|
|
||||||
// Singleton
|
/**
|
||||||
Heap *heap = Heap::the();
|
* Allocates a given amount of bytes on the heap.
|
||||||
|
*
|
||||||
if (size < 0) {
|
* @param size The amount of bytes to be allocated.
|
||||||
cout << "Heap: Cannot alloc less than 0B. No bytes allocated." << endl;
|
*
|
||||||
return nullptr;
|
* @return A pointer to the address where the memory
|
||||||
}
|
* has been allocated. This pointer is supposed
|
||||||
|
* to be casted to and object pointer.
|
||||||
|
*/
|
||||||
|
void *Heap::alloc(size_t size)
|
||||||
|
{
|
||||||
|
|
||||||
if (heap->m_size + size > HEAP_SIZE) {
|
// Singleton
|
||||||
heap->collect();
|
Heap *heap = Heap::the();
|
||||||
// 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
|
if (size < 0)
|
||||||
uintptr_t *reused_chunk = heap->try_recycle_chunks(size);
|
{
|
||||||
if (reused_chunk != nullptr) {
|
cout << "Heap: Cannot alloc less than 0B. No bytes allocated." << endl;
|
||||||
return (void *)reused_chunk;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no free chunks was found (reused_chunk is a nullptr),
|
|
||||||
// then create a new chunk
|
|
||||||
auto new_chunk = new Chunk;
|
|
||||||
new_chunk->size = size;
|
|
||||||
new_chunk->start = (uintptr_t *)(heap->m_heap + heap->m_size);
|
|
||||||
|
|
||||||
heap->m_size += size;
|
if (heap->m_size + size > HEAP_SIZE)
|
||||||
|
{
|
||||||
|
heap->collect();
|
||||||
|
// If collect failed, crash with OOM error
|
||||||
|
assert(heap->m_size + size <= HEAP_SIZE && "Heap: Out Of Memory");
|
||||||
|
}
|
||||||
|
|
||||||
heap->m_allocated_chunks.push_back(new_chunk);
|
// If a chunk was recycled, return the old chunk address
|
||||||
|
uintptr_t *reused_chunk = heap->try_recycle_chunks(size);
|
||||||
|
if (reused_chunk != nullptr)
|
||||||
|
{
|
||||||
|
return static_cast<void *>(reused_chunk);
|
||||||
|
}
|
||||||
|
|
||||||
// new_chunk should probably be a unique pointer, if that isn't implicit already
|
// If no free chunks was found (reused_chunk is a nullptr),
|
||||||
return new_chunk->start;
|
// then create a new chunk
|
||||||
}
|
auto new_chunk = new Chunk;
|
||||||
|
new_chunk->size = size;
|
||||||
|
new_chunk->start = (uintptr_t *)(heap->m_heap + heap->m_size);
|
||||||
|
|
||||||
/**
|
heap->m_size += size;
|
||||||
* Tries to recycle used and freed chunks that are
|
|
||||||
* already allocated objects by the OS but freed
|
|
||||||
* from our Heap. This reduces the amount of GC
|
|
||||||
* objects slightly which saves time from malloc'ing
|
|
||||||
* memory from the OS.
|
|
||||||
*
|
|
||||||
* @param size Amount of bytes needed for the object
|
|
||||||
* which is about to be allocated.
|
|
||||||
*
|
|
||||||
* @returns If a chunk is found and recycled, a
|
|
||||||
* pointer to the allocated memory for
|
|
||||||
* the object is returned. If not, a
|
|
||||||
* nullptr is returned to signify no
|
|
||||||
* chunks were found.
|
|
||||||
*/
|
|
||||||
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 = 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
|
|
||||||
// the list of freed chunks
|
|
||||||
size_t diff = cp->size - size;
|
|
||||||
|
|
||||||
auto chunk_complement = new Chunk;
|
|
||||||
chunk_complement->size = diff;
|
|
||||||
chunk_complement->start = cp->start + cp->size;
|
|
||||||
|
|
||||||
heap->m_freed_chunks.erase(iter);
|
heap->m_allocated_chunks.push_back(new_chunk);
|
||||||
heap->m_freed_chunks.push_back(chunk_complement);
|
|
||||||
heap->m_allocated_chunks.push_back(cp);
|
|
||||||
|
|
||||||
return cp->start;
|
|
||||||
}
|
|
||||||
else if (cp->size == size)
|
|
||||||
{
|
|
||||||
// Reuse the whole chunk
|
|
||||||
heap->m_freed_chunks.erase(iter);
|
|
||||||
heap->m_allocated_chunks.push_back(cp);
|
|
||||||
return cp->start;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// new_chunk should probably be a unique pointer, if that isn't implicit already
|
||||||
* Collection phase of the garbage collector. When
|
return new_chunk->start;
|
||||||
* an allocation is requested and there is no space
|
}
|
||||||
* left on the heap, a collection is triggered. This
|
|
||||||
* function is private so that the user cannot trigger
|
|
||||||
* a collection unneccessarily.
|
|
||||||
*/
|
|
||||||
void Heap::collect() {
|
|
||||||
// Get instance
|
|
||||||
auto heap = Heap::the();
|
|
||||||
|
|
||||||
// get current stack
|
/**
|
||||||
auto stack_bottom = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
|
* Tries to recycle used and freed chunks that are
|
||||||
|
* already allocated objects by the OS but freed
|
||||||
|
* from our Heap. This reduces the amount of GC
|
||||||
|
* objects slightly which saves time from malloc'ing
|
||||||
|
* memory from the OS.
|
||||||
|
*
|
||||||
|
* @param size Amount of bytes needed for the object
|
||||||
|
* which is about to be allocated.
|
||||||
|
*
|
||||||
|
* @returns If a chunk is found and recycled, a
|
||||||
|
* pointer to the allocated memory for
|
||||||
|
* the object is returned. If not, a
|
||||||
|
* nullptr is returned to signify no
|
||||||
|
* chunks were found.
|
||||||
|
*/
|
||||||
|
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 chunk = getAt(heap->m_freed_chunks, i);
|
||||||
|
auto iter = heap->m_freed_chunks.begin();
|
||||||
|
advance(iter, i);
|
||||||
|
if (chunk->size > size)
|
||||||
|
{
|
||||||
|
// Split the chunk, use one part and add the remaining part to
|
||||||
|
// the list of freed chunks
|
||||||
|
size_t diff = chunk->size - size;
|
||||||
|
|
||||||
uintptr_t *stack_top = heap->m_stack_top != nullptr ? heap->m_stack_top : (uintptr_t *)0;
|
auto chunk_complement = new Chunk;
|
||||||
|
chunk_complement->size = diff;
|
||||||
|
chunk_complement->start = chunk->start + chunk->size;
|
||||||
|
|
||||||
auto work_list = heap->m_allocated_chunks;
|
heap->m_freed_chunks.erase(iter);
|
||||||
mark(stack_bottom, stack_top, work_list);
|
heap->m_freed_chunks.push_back(chunk_complement);
|
||||||
|
heap->m_allocated_chunks.push_back(chunk);
|
||||||
|
|
||||||
sweep(heap);
|
return chunk->start;
|
||||||
|
}
|
||||||
|
else if (chunk->size == size)
|
||||||
|
{
|
||||||
|
// Reuse the whole chunk
|
||||||
|
heap->m_freed_chunks.erase(iter);
|
||||||
|
heap->m_allocated_chunks.push_back(chunk);
|
||||||
|
return chunk->start;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
free(heap);
|
/**
|
||||||
}
|
* Collection phase of the garbage collector. When
|
||||||
|
* an allocation is requested and there is no space
|
||||||
|
* left on the heap, a collection is triggered. This
|
||||||
|
* function is private so that the user cannot trigger
|
||||||
|
* a collection unneccessarily.
|
||||||
|
*/
|
||||||
|
void Heap::collect()
|
||||||
|
{
|
||||||
|
// Get instance
|
||||||
|
auto heap = Heap::the();
|
||||||
|
|
||||||
/**
|
// get current stack
|
||||||
* Iterates through the stack, if an element on the stack points to a chunk,
|
auto stack_bottom = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
|
||||||
* 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, which haven't been marked.
|
|
||||||
*/
|
|
||||||
void Heap::mark(uintptr_t *start, const uintptr_t *end, vector<Chunk*> worklist) {
|
|
||||||
int counter = 0;
|
|
||||||
// To find adresses thats in the worklist
|
|
||||||
for (; start < end; start++) {
|
|
||||||
counter++;
|
|
||||||
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<uintptr_t>(chunk->start);
|
uintptr_t *stack_top = heap->m_stack_top != nullptr ? heap->m_stack_top : (uintptr_t *)0;
|
||||||
auto c_size = reinterpret_cast<uintptr_t>(chunk->size);
|
|
||||||
auto c_end = reinterpret_cast<uintptr_t>(c_start + c_size);
|
|
||||||
|
|
||||||
cout << "Start points to:\t" << hex << *start << endl;
|
auto work_list = heap->m_allocated_chunks;
|
||||||
cout << "Chunk start:\t\t" << hex << c_start << endl;
|
mark(stack_bottom, stack_top, work_list);
|
||||||
cout << "Chunk end:\t\t" << hex << c_end << "\n" << endl;
|
|
||||||
|
|
||||||
// Check if the stack pointer aligns with the chunk
|
sweep(heap);
|
||||||
if (c_start <= *start && *start < c_end) {
|
|
||||||
|
|
||||||
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 {
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cout << "Counter: " << counter << endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sweeps the heap, unmarks the marked chunks for the next cycle,
|
|
||||||
* adds the unmarked nodes to the list of freed chunks; to be freed.
|
|
||||||
*
|
|
||||||
* @param heap Pointer to the heap singleton instance.
|
|
||||||
*/
|
|
||||||
void Heap::sweep(Heap *heap) {
|
|
||||||
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.
|
free(heap);
|
||||||
if (chunk->marked) {
|
}
|
||||||
chunk->marked = false;
|
|
||||||
++iter;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// Add the unmarked chunks to freed chunks and remove from
|
|
||||||
// the list of allocated chunks
|
|
||||||
heap->m_freed_chunks.push_back(chunk);
|
|
||||||
iter = heap->m_allocated_chunks.erase(iter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Frees chunks that was moved to the list m_freed_chunks
|
* Iterates through the stack, if an element on the stack points to a chunk,
|
||||||
* by the sweep phase. If there are more than a certain
|
* called a root chunk, that chunk is marked (i.e. reachable).
|
||||||
* amount of free chunks, delete the free chunks to
|
* Then it recursively follows all chunks which are possibly reachable from
|
||||||
* avoid cluttering.
|
* the root chunk and mark those chunks.
|
||||||
*
|
* If a chunk is marked it is removed from the worklist, since it's no longer of
|
||||||
* @param heap Heap singleton instance, only for avoiding
|
* concern for this method.
|
||||||
* redundant calls to the singleton get
|
*
|
||||||
*/
|
* @param start Pointer to the start of the stack frame.
|
||||||
void Heap::free(Heap *heap) {
|
* @param end Pointer to the end of the stack frame.
|
||||||
if (heap->m_freed_chunks.size() > FREE_THRESH) {
|
* @param worklist The currently allocated chunks, which haven't been marked.
|
||||||
while (heap->m_freed_chunks.size()) {
|
*/
|
||||||
auto chunk = heap->m_freed_chunks.back();
|
void Heap::mark(uintptr_t *start, const uintptr_t *end, vector<Chunk *> &worklist)
|
||||||
heap->m_freed_chunks.pop_back();
|
{
|
||||||
delete chunk;
|
int counter = 0;
|
||||||
}
|
// To find adresses thats in the worklist
|
||||||
}
|
for (; start < end; start++)
|
||||||
// if there are chunks but not more than FREE_THRESH
|
{
|
||||||
else if (heap->m_freed_chunks.size()) {
|
counter++;
|
||||||
// essentially, always check for overlap between
|
auto it = worklist.begin();
|
||||||
// chunks before finishing the allocation
|
auto stop = worklist.end();
|
||||||
free_overlap(heap);
|
// for (auto it = worklist.begin(); it != worklist.end();) {
|
||||||
}
|
while (it != stop)
|
||||||
}
|
{
|
||||||
|
Chunk *chunk = *it;
|
||||||
|
|
||||||
/**
|
auto c_start = reinterpret_cast<uintptr_t>(chunk->start);
|
||||||
* Checks for overlaps between freed chunks of memory
|
auto c_size = reinterpret_cast<uintptr_t>(chunk->size);
|
||||||
* and removes overlapping chunks while prioritizing
|
auto c_end = reinterpret_cast<uintptr_t>(c_start + c_size);
|
||||||
* the chunks at lower addresses.
|
|
||||||
*
|
|
||||||
* @param heap Heap singleton instance, only for avoiding
|
|
||||||
* redundant calls to the singleton get
|
|
||||||
*
|
|
||||||
* @note Maybe this should be changed to prioritizing
|
|
||||||
* larger chunks.
|
|
||||||
*/
|
|
||||||
void Heap::free_overlap(Heap *heap) {
|
|
||||||
std::vector<Chunk *> filtered;
|
|
||||||
size_t i = 0;
|
|
||||||
// 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 = 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);
|
|
||||||
if (n_start >= (p_start + p_size)) {
|
|
||||||
filtered.push_back(next);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
heap->m_freed_chunks.swap(filtered);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ----- ONLY DEBUGGING -----------------------------------------------------------------------
|
cout << "Start points to:\t" << hex << *start << endl;
|
||||||
|
cout << "Chunk start:\t\t" << hex << c_start << endl;
|
||||||
|
cout << "Chunk end:\t\t" << hex << c_end << "\n"
|
||||||
|
<< endl;
|
||||||
|
|
||||||
/**
|
// Check if the stack pointer aligns with the chunk
|
||||||
* Prints the result of Heap::init() and a dummy value
|
if (c_start <= *start && *start < c_end)
|
||||||
* for the current stack frame for reference.
|
{
|
||||||
*/
|
|
||||||
void Heap::check_init() {
|
|
||||||
auto heap = Heap::the();
|
|
||||||
cout << "Heap addr:\t" << heap << endl;
|
|
||||||
cout << "GC m_stack_top:\t" << heap->m_stack_top << endl;
|
|
||||||
auto stack_bottom = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
|
|
||||||
cout << "GC stack_bottom:\t" << stack_bottom << endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Conditional collection, only to be used in debugging
|
|
||||||
*
|
|
||||||
* @param flags Bitmap of flags
|
|
||||||
*/
|
|
||||||
void Heap::collect(uint flags) {
|
|
||||||
|
|
||||||
cout << "DEBUG COLLECT\nFLAGS: ";
|
if (!chunk->marked)
|
||||||
if (flags & MARK)
|
{
|
||||||
cout << "\n - MARK";
|
chunk->marked = true;
|
||||||
if (flags & SWEEP)
|
// Remove the marked chunk from the worklist
|
||||||
cout << "\n - SWEEP";
|
it = worklist.erase(it);
|
||||||
if (flags & FREE)
|
// Recursively call mark, to see if the reachable chunk further points to another chunk
|
||||||
cout << "\n - FREE";
|
mark((uintptr_t *)c_start, (uintptr_t *)c_end, worklist);
|
||||||
cout << endl;
|
}
|
||||||
|
else
|
||||||
auto heap = Heap::the();
|
{
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cout << "Counter: " << counter << endl;
|
||||||
|
}
|
||||||
|
|
||||||
// get the frame adress, whwere local variables and saved registers are located
|
/**
|
||||||
auto stack_bottom = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
|
* Sweeps the heap, unmarks the marked chunks for the next cycle,
|
||||||
cout << "Stack bottom in collect:\t" << stack_bottom << endl;
|
* adds the unmarked nodes to the list of freed chunks; to be freed.
|
||||||
uintptr_t *stack_top;
|
*
|
||||||
|
* @param heap Pointer to the heap singleton instance.
|
||||||
|
*/
|
||||||
|
void Heap::sweep(Heap *heap)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
|
||||||
if (heap->m_stack_top != nullptr)
|
// Unmark the marked chunks for the next iteration.
|
||||||
stack_top = heap->m_stack_top;
|
if (chunk->marked)
|
||||||
else
|
{
|
||||||
stack_top = (uintptr_t *) stack_bottom + 80; // dummy value
|
chunk->marked = false;
|
||||||
|
++iter;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Add the unmarked chunks to freed chunks and remove from
|
||||||
|
// the list of allocated chunks
|
||||||
|
heap->m_freed_chunks.push_back(chunk);
|
||||||
|
iter = heap->m_allocated_chunks.erase(iter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cout << "Stack end in collect:\t " << stack_top << endl;
|
/**
|
||||||
auto work_list = heap->m_allocated_chunks;
|
* Frees chunks that was moved to the list m_freed_chunks
|
||||||
|
* by the sweep phase. If there are more than a certain
|
||||||
|
* amount of free chunks, delete the free chunks to
|
||||||
|
* avoid cluttering.
|
||||||
|
*
|
||||||
|
* @param heap Heap singleton instance, only for avoiding
|
||||||
|
* redundant calls to the singleton get
|
||||||
|
*/
|
||||||
|
void Heap::free(Heap *heap)
|
||||||
|
{
|
||||||
|
if (heap->m_freed_chunks.size() > FREE_THRESH)
|
||||||
|
{
|
||||||
|
while (heap->m_freed_chunks.size())
|
||||||
|
{
|
||||||
|
auto chunk = heap->m_freed_chunks.back();
|
||||||
|
heap->m_freed_chunks.pop_back();
|
||||||
|
delete chunk;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// if there are chunks but not more than FREE_THRESH
|
||||||
|
else if (heap->m_freed_chunks.size())
|
||||||
|
{
|
||||||
|
// essentially, always check for overlap between
|
||||||
|
// chunks before finishing the allocation
|
||||||
|
free_overlap(heap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (flags & MARK) {
|
/**
|
||||||
mark(stack_bottom, stack_top, work_list);
|
* Checks for overlaps between freed chunks of memory
|
||||||
}
|
* and removes overlapping chunks while prioritizing
|
||||||
|
* the chunks at lower addresses.
|
||||||
|
*
|
||||||
|
* @param heap Heap singleton instance, only for avoiding
|
||||||
|
* redundant calls to the singleton get
|
||||||
|
*
|
||||||
|
* @note Maybe this should be changed to prioritizing
|
||||||
|
* larger chunks.
|
||||||
|
*/
|
||||||
|
void Heap::free_overlap(Heap *heap)
|
||||||
|
{
|
||||||
|
std::vector<Chunk *> filtered;
|
||||||
|
size_t i = 0;
|
||||||
|
// 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 = 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);
|
||||||
|
if (n_start >= (p_start + p_size))
|
||||||
|
{
|
||||||
|
filtered.push_back(next);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
heap->m_freed_chunks.swap(filtered);
|
||||||
|
}
|
||||||
|
|
||||||
if (flags & SWEEP) {
|
// ----- ONLY DEBUGGING -----------------------------------------------------------------------
|
||||||
sweep(heap);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (flags & FREE) {
|
|
||||||
free(heap);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mark child references from the root references
|
/**
|
||||||
void mark_test(vector<Chunk *> worklist) {
|
* Prints the result of Heap::init() and a dummy value
|
||||||
while (worklist.size() > 0) {
|
* for the current stack frame for reference.
|
||||||
Chunk *ref = worklist.back();
|
*/
|
||||||
worklist.pop_back();
|
void Heap::check_init()
|
||||||
Chunk *child = (Chunk*) ref; // this is probably not correct
|
{
|
||||||
if (child != nullptr && !child->marked) {
|
auto heap = Heap::the();
|
||||||
child->marked = true;
|
cout << "Heap addr:\t" << heap << "\n";
|
||||||
worklist.push_back(child);
|
cout << "GC m_stack_top:\t" << heap->m_stack_top << "\n";
|
||||||
mark_test(worklist);
|
auto stack_bottom = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
|
||||||
}
|
cout << "GC stack_bottom:\t" << stack_bottom << endl;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Mark the root references and look for child references to them
|
/**
|
||||||
void mark_from_roots(uintptr_t *start, const uintptr_t *end) {
|
* Conditional collection, only to be used in debugging
|
||||||
vector<Chunk *> worklist;
|
*
|
||||||
for (;start > end; start --) {
|
* @param flags Bitmap of flags
|
||||||
if (*start % 8 == 0) { // all pointers must be aligned as double words
|
*/
|
||||||
Chunk *ref = (Chunk*) *start;
|
void Heap::collect(uint flags)
|
||||||
if (ref != nullptr && !ref->marked) {
|
{
|
||||||
ref->marked = true;
|
|
||||||
worklist.push_back(ref);
|
|
||||||
mark_test(worklist);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// For testing purposes
|
cout << "DEBUG COLLECT\nFLAGS: ";
|
||||||
void Heap::print_line(Chunk *chunk) {
|
if (flags & MARK)
|
||||||
cout << "Marked: " << chunk->marked << "\nStart adr: " << chunk->start << "\nSize: " << chunk->size << " B\n" << endl;
|
cout << "\n - MARK";
|
||||||
}
|
if (flags & SWEEP)
|
||||||
|
cout << "\n - SWEEP";
|
||||||
|
if (flags & FREE)
|
||||||
|
cout << "\n - FREE";
|
||||||
|
cout << "\n";
|
||||||
|
|
||||||
void Heap::print_worklist(std::vector<Chunk *> list) {
|
auto heap = Heap::the();
|
||||||
for (auto cp : list) {
|
|
||||||
cout << "Chunk at:\t" << cp->start << "\nSize:\t\t" << cp->size << endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Heap::print_contents() {
|
// get the frame adress, whwere local variables and saved registers are located
|
||||||
auto heap = Heap::the();
|
auto stack_bottom = reinterpret_cast<uintptr_t *>(__builtin_frame_address(0));
|
||||||
if (heap->m_allocated_chunks.size()) {
|
cout << "Stack bottom in collect:\t" << stack_bottom << "\n";
|
||||||
cout << "\nALLOCATED CHUNKS #" << dec << heap->m_allocated_chunks.size() << endl;
|
uintptr_t *stack_top = heap->m_stack_top;
|
||||||
for (auto chunk : heap->m_allocated_chunks) {
|
|
||||||
print_line(chunk);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
cout << "NO ALLOCATIONS\n" << endl;
|
|
||||||
}
|
|
||||||
if (heap->m_freed_chunks.size()) {
|
|
||||||
cout << "\nFREED CHUNKS #" << dec << heap->m_freed_chunks.size() << endl;
|
|
||||||
for (auto fchunk : heap->m_freed_chunks) {
|
|
||||||
print_line(fchunk);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
cout << "NO FREED CHUNKS" << endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Heap::set_profiler(bool mode) {
|
cout << "Stack end in collect:\t " << stack_top << endl;
|
||||||
auto heap = Heap::the();
|
auto work_list = heap->m_allocated_chunks;
|
||||||
heap->m_profiler_enable = mode;
|
|
||||||
}
|
if (flags & MARK)
|
||||||
|
mark(stack_bottom, stack_top, work_list);
|
||||||
|
|
||||||
|
if (flags & SWEEP)
|
||||||
|
sweep(heap);
|
||||||
|
|
||||||
|
if (flags & FREE)
|
||||||
|
free(heap);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark child references from the root references
|
||||||
|
void mark_test(vector<Chunk *> &worklist)
|
||||||
|
{
|
||||||
|
while (worklist.size() > 0)
|
||||||
|
{
|
||||||
|
Chunk *ref = worklist.back();
|
||||||
|
worklist.pop_back();
|
||||||
|
Chunk *child = (Chunk *)ref; // this is probably not correct
|
||||||
|
if (child != nullptr && !child->marked)
|
||||||
|
{
|
||||||
|
child->marked = true;
|
||||||
|
worklist.push_back(child);
|
||||||
|
mark_test(worklist);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark the root references and look for child references to them
|
||||||
|
void mark_from_roots(uintptr_t *start, const uintptr_t *end)
|
||||||
|
{
|
||||||
|
vector<Chunk *> worklist;
|
||||||
|
for (; start > end; start--)
|
||||||
|
{
|
||||||
|
if (*start % 8 == 0)
|
||||||
|
{ // all pointers must be aligned as double words
|
||||||
|
Chunk *ref = (Chunk *)*start;
|
||||||
|
if (ref != nullptr && !ref->marked)
|
||||||
|
{
|
||||||
|
ref->marked = true;
|
||||||
|
worklist.push_back(ref);
|
||||||
|
mark_test(worklist);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// For testing purposes
|
||||||
|
void Heap::print_line(Chunk *chunk)
|
||||||
|
{
|
||||||
|
cout << "Marked: " << chunk->marked << "\nStart adr: " << chunk->start << "\nSize: " << chunk->size << " B\n"
|
||||||
|
<< endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Heap::print_worklist(std::vector<Chunk *> &list)
|
||||||
|
{
|
||||||
|
for (auto cp : list)
|
||||||
|
cout << "Chunk at:\t" << cp->start << "\nSize:\t\t" << cp->size << "\n";
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Heap::print_contents()
|
||||||
|
{
|
||||||
|
auto heap = Heap::the();
|
||||||
|
if (heap->m_allocated_chunks.size())
|
||||||
|
{
|
||||||
|
cout << "\nALLOCATED CHUNKS #" << dec << heap->m_allocated_chunks.size() << endl;
|
||||||
|
for (auto chunk : heap->m_allocated_chunks)
|
||||||
|
print_line(chunk);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cout << "NO ALLOCATIONS\n" << endl;
|
||||||
|
}
|
||||||
|
if (heap->m_freed_chunks.size())
|
||||||
|
{
|
||||||
|
cout << "\nFREED CHUNKS #" << dec << heap->m_freed_chunks.size() << endl;
|
||||||
|
for (auto fchunk : heap->m_freed_chunks)
|
||||||
|
print_line(fchunk);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cout << "NO FREED CHUNKS" << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Heap::set_profiler(bool mode)
|
||||||
|
{
|
||||||
|
auto heap = Heap::the();
|
||||||
|
heap->m_profiler_enable = mode;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue