testing testing...

This commit is contained in:
Victor Olin 2023-05-01 14:36:44 +02:00
parent fda9e6728f
commit e51ba7679b
10 changed files with 933 additions and 42 deletions

View file

@ -43,10 +43,10 @@ game:
wrapper_test:
rm -f lib/event.o lib/profiler.o lib/heap.o lib/coll.a tests/wrapper_test.out
# compile object files
$(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -g -c -o lib/event.o lib/event.cpp -fPIC
$(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -g -c -o lib/profiler.o lib/profiler.cpp -fPIC
$(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -g -c -o lib/heap.o lib/heap.cpp -fPIC
$(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -g -c -o lib/cheap.o lib/cheap.cpp -fPIC
$(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -g -c -o lib/event.o lib/event.cpp -fPIC
$(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -g -c -o lib/profiler.o lib/profiler.cpp -fPIC
$(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -g -c -o lib/heap.o lib/heap.cpp -fPIC
$(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -g -c -o lib/cheap.o lib/cheap.cpp -fPIC
# compile object files into library
ar rcs lib/gcoll.a lib/event.o lib/profiler.o lib/heap.o lib/cheap.o
clang -stdlib=libc++ $(WFLAGS) $(LIB_INCL) -o tests/wrapper_test.out tests/wrapper_test.c lib/gcoll.a -lstdc++
@ -76,6 +76,7 @@ static_lib:
static_lib_test: static_lib
$(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -o tests/extern_lib.out tests/extern_lib.cpp lib/gcoll.a
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
static_lib_c:
@ -95,6 +96,14 @@ wrapper:
ar r lib/gcoll.a lib/event.o lib/profiler.o lib/heap.o
=======
=======
=======
alloc_free_list: static_lib
$(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -o tests/alloc_fl.out tests/alloc_free_list.cpp lib/gcoll.a
linked_list_test: static_lib
$(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -o tests/linkedlist.out tests/linkedlist.cpp lib/gcoll.a
>>>>>>> d7ea27e (testing testing...)
wrapper:
# remove old files
rm -f lib/event.o lib/profiler.o lib/heap.o lib/coll.a tests/wrapper.out

View file

@ -11,16 +11,18 @@ namespace GC
*/
enum GCEventType
{
HeapInit,
AllocStart,
CollectStart,
MarkStart,
ChunkMarked,
ChunkSwept,
ChunkFreed,
NewChunk,
ReusedChunk,
ProfilerDispose
HeapInit = 1 << 0,
AllocStart = 1 << 1,
CollectStart = 1 << 2,
MarkStart = 1 << 3,
SweepStart = 1 << 4,
ChunkMarked = 1 << 5,
ChunkSwept = 1 << 6,
ChunkFreed = 1 << 7,
NewChunk = 1 << 8,
ReusedChunk = 1 << 9,
ProfilerDispose = 1 << 10,
FreeStart = 1 << 11
};
/**

View file

@ -1,14 +1,15 @@
#pragma once
#include <list>
#include <stdlib.h>
#include <vector>
#include "chunk.hpp"
#include "profiler.hpp"
#define HEAP_SIZE 2097152 //65536
#define FREE_THRESH (uint) 100000
// #define HEAP_DEBUG
#define HEAP_SIZE 160 //65536
#define FREE_THRESH (uint) 0
#define HEAP_DEBUG
namespace GC
{
@ -17,11 +18,11 @@ namespace GC
* collection (mark/sweep/free/all).
*/
enum CollectOption {
MARK=0x1,
SWEEP=0x2,
MARK_SWEEP = 0x3,
FREE=0x4,
COLLECT_ALL=0x7
MARK = 1 << 0,
SWEEP = 1 << 1,
MARK_SWEEP = 1 << 2,
FREE = 1 << 3,
COLLECT_ALL = 0b1111 // all flags above
};
/**
@ -44,12 +45,14 @@ namespace GC
char *const m_heap;
size_t m_size {0};
char *m_heap_top {nullptr};
// static Heap *m_instance {nullptr};
uintptr_t *m_stack_top {nullptr};
bool m_profiler_enable {false};
std::vector<Chunk *> m_allocated_chunks;
std::vector<Chunk *> m_freed_chunks;
std::list<Chunk *> m_free_list;
static bool profiler_enabled();
// static Chunk *get_at(std::vector<Chunk *> &list, size_t n);
@ -66,7 +69,9 @@ namespace GC
// Temporary
Chunk *try_recycle_chunks_new(size_t size);
void free_overlap_new(Heap &heap);
#ifndef HEAP_DEBUG
void add_to_free_list(Chunk *chunk);
#endif
public:
/**
* These are the only five functions which are exposed
@ -80,6 +85,7 @@ namespace GC
static void init();
static void dispose();
static void *alloc(size_t size);
static void *alloc_free_list(size_t size);
void set_profiler(bool mode);
// Stop the compiler from generating copy-methods
@ -87,6 +93,7 @@ namespace GC
Heap& operator=(Heap const&) = delete;
#ifdef HEAP_DEBUG
void add_to_free_list(Chunk *chunk);
void collect(CollectOption flags); // conditional collection
void check_init(); // print dummy things
void print_contents(); // print dummy things

View file

@ -2,12 +2,31 @@
#include <iostream>
#include <vector>
#include <chrono>
#include "chunk.hpp"
#include "event.hpp"
// #define FunctionCallTypes
// #define ChunkOpsTypes
namespace GC {
enum RecordOption
{
FunctionCalls = (GC::AllocStart | GC::CollectStart | GC::MarkStart | GC::SweepStart),
ChunkOps = (GC::ChunkMarked | GC::ChunkSwept | GC::ChunkFreed | GC::NewChunk | GC::ReusedChunk),
AllOps = ~0
};
struct ProfilerEvent
{
uint m_n {1};
const GCEventType m_type;
ProfilerEvent(GCEventType type) : m_type(type) {}
};
class Profiler {
private:
Profiler() {}
@ -17,31 +36,27 @@ namespace GC {
delete c;
}
/**
* Returns the instance of the Profiler singleton.
* If m_instance is the nullptr and the profiler
* is not initialized yet, initialize it and return
* the pointer to it. Otherwise return the previously
* initialized pointer.
*
* @returns The pointer to the profiler singleton.
*/
static Profiler *the()
{
if (m_instance)
return m_instance;
m_instance = new Profiler();
return m_instance;
}
static Profiler &the();
inline static Profiler *m_instance {nullptr};
std::vector<GCEvent *> m_events;
ProfilerEvent *m_last_prof_event {new ProfilerEvent(HeapInit)};
std::vector<ProfilerEvent *> m_prof_events;
RecordOption flags;
static void record_data(GCEvent *type);
std::ofstream create_file_stream();
std::string get_log_folder();
static void dump_trace();
static void dump_prof_trace();
static void dump_chunk_trace();
// static void dump_trace_short();
// static void dump_trace_full();
static void print_chunk_event(GCEvent *event, char buffer[22]);
static const char *type_to_string(GCEventType type);
public:
static RecordOption log_options();
static void set_log_options(RecordOption flags);
static void record(GCEventType type);
static void record(GCEventType type, size_t size);
static void record(GCEventType type, Chunk *chunk);

View file

@ -36,6 +36,7 @@ namespace GC
// clang complains because arg for __b_f_a is not 0 which is "unsafe"
#pragma clang diagnostic ignored "-Wframe-address"
heap.m_stack_top = static_cast<uintptr_t *>(__builtin_frame_address(1));
heap.m_heap_top = heap.m_heap;
}
/**
@ -50,6 +51,131 @@ namespace GC
Profiler::dispose();
}
void *Heap::alloc_free_list(size_t size)
{
// Singleton
Heap &heap = Heap::the();
bool profiler_enabled = heap.profiler_enabled();
if (profiler_enabled)
Profiler::record(AllocStart, size);
if (size == 0)
{
cout << "Heap: Cannot alloc 0B. No bytes allocated." << endl;
return nullptr;
}
// Try to find a fragmented section to recycle
Chunk *recycle = nullptr;
auto iter = heap.m_free_list.begin();
while (iter != heap.m_free_list.end())
{
if ((*iter)->m_size >= size)
{
recycle = *iter;
heap.m_free_list.erase(++iter);
break;
}
iter++;
}
// If memory fragment was found
if (recycle != nullptr)
{
heap.m_size += size;
// If fragment is larger than request, split it
if (recycle->m_size > size)
{
auto new_part = new Chunk(size, recycle->m_start);
auto complement = new Chunk(
recycle->m_size - size,
(recycle->m_start + size)
);
delete recycle;
// TODO: add complement to free_list
heap.add_to_free_list(complement);
heap.m_allocated_chunks.push_back(new_part);
return (void *)(new_part->m_start);
}
heap.m_allocated_chunks.push_back(recycle);
return (void *)(recycle->m_start);
}
uintptr_t *max_size = (uintptr_t *)(heap.m_heap + HEAP_SIZE);
uintptr_t *new_size = (uintptr_t *)(heap.m_heap_top + size);
if (new_size <= max_size)
{
auto new_chunk = new Chunk(size, (uintptr_t *)(heap.m_heap_top));
heap.m_allocated_chunks.push_back(new_chunk);
if (profiler_enabled)
Profiler::record(NewChunk, new_chunk);
heap.m_heap_top += size;
heap.m_size += size;
}
// Only throws if the allocation failed
throw std::runtime_error(std::string("Error: Heap out of memory"));
}
void Heap::add_to_free_list(Chunk *chunk)
{
Chunk *curr;
auto iter = m_free_list.begin();
uintptr_t *prev_start = nullptr;
uintptr_t *prev_end = nullptr;
while (iter != m_free_list.end())
{
curr = *iter;
// If the curr chunk is aligned before param
if (curr->m_start + curr->m_size == chunk->m_start)
{
Chunk *merged = new Chunk(
curr->m_size + chunk->m_size,
curr->m_start
);
iter = m_free_list.erase(iter);
m_free_list.insert(iter, merged);
return;
}
// If the curr chunk is aligned after param
if (chunk->m_start + chunk->m_size == curr->m_start)
{
Chunk *merged = new Chunk(
curr->m_size + chunk->m_size,
chunk->m_start
);
iter = m_free_list.erase(iter);
m_free_list.insert(iter, merged);
return;
}
// If the first chunk starts after param
if (prev_start == nullptr && curr->m_start > chunk->m_start)
{
m_free_list.insert(iter, chunk);
return;
}
prev_start = curr->m_start;
prev_end = prev_start + curr->m_size;
iter++;
}
// This is only reachable if the chunk is at the end
m_free_list.push_back(chunk);
}
/**
* Allocates a given amount of bytes on the heap.
*
@ -79,7 +205,11 @@ namespace GC
heap.collect();
// If memory is not enough after collect, crash with OOM error
if (heap.m_size + size > HEAP_SIZE)
{
if (profiler_enabled)
Profiler::dispose();
throw std::runtime_error(std::string("Error: Heap out of memory"));
}
}
// If a chunk was recycled, return the old chunk address
@ -271,8 +401,10 @@ namespace GC
*/
void Heap::sweep(Heap &heap)
{
auto iter = heap.m_allocated_chunks.begin();
bool profiler_enabled = heap.m_profiler_enable;
if (profiler_enabled)
Profiler::record(SweepStart);
auto iter = heap.m_allocated_chunks.begin();
// This cannot "iter != stop", results in seg fault, since the end gets updated, I think.
while (iter != heap.m_allocated_chunks.end())
{
@ -292,6 +424,7 @@ namespace GC
Profiler::record(ChunkSwept, chunk);
heap.m_freed_chunks.push_back(chunk);
iter = heap.m_allocated_chunks.erase(iter);
heap.m_size -= chunk->m_size;
}
}
}
@ -311,6 +444,9 @@ namespace GC
*/
void Heap::free(Heap &heap)
{
bool profiler_enabled = heap.m_profiler_enable;
if (profiler_enabled)
Profiler::record(FreeStart);
if (heap.m_freed_chunks.size() > FREE_THRESH)
{
bool profiler_enabled = heap.profiler_enabled();

286
src/GC/lib/profiler.cpp Normal file
View file

@ -0,0 +1,286 @@
#include <ctime>
#include <cstring>
#include <iostream>
#include <fstream>
#include <time.h>
#include <vector>
#include <unistd.h>
#include <stdexcept>
#include "chunk.hpp"
#include "event.hpp"
#include "profiler.hpp"
// #define MAC_OS
namespace GC
{
Profiler& Profiler::the()
{
static Profiler instance;
return instance;
}
RecordOption Profiler::log_options()
{
Profiler &prof = Profiler::the();
return prof.flags;
}
void Profiler::set_log_options(RecordOption flags)
{
Profiler &prof = Profiler::the();
prof.flags = flags;
}
void Profiler::record_data(GCEvent *event)
{
Profiler &prof = Profiler::the();
prof.m_events.push_back(event);
if (prof.m_last_prof_event->m_type == event->get_type())
prof.m_last_prof_event->m_n++;
else
{
prof.m_prof_events.push_back(prof.m_last_prof_event);
prof.m_last_prof_event = new ProfilerEvent(event->get_type());
}
}
/**
* Records an event independent of a chunk.
*
* @param type The type of event to record.
*/
void Profiler::record(GCEventType type)
{
Profiler &prof = Profiler::the();
if (prof.flags & type)
Profiler::record_data(new GCEvent(type));
// auto event = new GCEvent(type);
// auto profiler = Profiler::the();
// profiler.m_events.push_back(event);
}
/**
* This overload is only used with an AllocStart
* event.
*
* @param type The type of event to record.
*
* @param size The size of requested to alloc().
*/
void Profiler::record(GCEventType type, size_t size)
{
Profiler &prof = Profiler::the();
if (prof.flags & type)
Profiler::record_data(new GCEvent(type, size));
// auto event = new GCEvent(type, size);
// auto profiler = Profiler::the();
// profiler.m_events.push_back(event);
}
void Profiler::dump_trace()
{
Profiler &prof = Profiler::the();
if (prof.flags & FunctionCalls)
dump_prof_trace();
else
dump_chunk_trace();
}
/**
* Records an event related to a chunk.
*
* @param type The type of event to record.
*
* @param chunk The chunk the event is connected
* to.
*/
void Profiler::record(GCEventType type, Chunk *chunk)
{
// Create a copy of chunk to store in the profiler
// because in free() chunks are deleted and cannot
// be referenced by the profiler. These copied
// chunks are deleted by the profiler on dispose().
Profiler &prof = Profiler::the();
if (prof.flags & type)
{
auto chunk_copy = new Chunk(chunk);
auto event = new GCEvent(type, chunk_copy);
Profiler::record_data(event);
}
// auto profiler = Profiler::the();
// profiler.m_events.push_back(event);
}
void Profiler::dump_prof_trace()
{
Profiler &prof = Profiler::the();
prof.m_prof_events.push_back(prof.m_last_prof_event);
auto start = prof.m_prof_events.begin();
auto end = prof.m_prof_events.end();
char buffer[22];
std::ofstream fstr = prof.create_file_stream();
while (start != end)
{
auto event = *start++;
fstr << "\n--------------------------------\n"
<< Profiler::type_to_string(event->m_type)
<< "\nTimes:\t" << event->m_n;
}
fstr << "\n--------------------------------";
}
/**
* Prints the history of the recorded events
* to a log file in the /tests/logs folder.
*/
void Profiler::dump_chunk_trace()
{
Profiler &prof = Profiler::the();
auto start = prof.m_events.begin();
auto end = prof.m_events.end();
// Buffer for timestamp
char buffer[22];
while (start != end)
{
auto event = *start++;
auto e_type = event->get_type();
prof.print_chunk_event(event, buffer);
}
}
void Profiler::print_chunk_event(GCEvent *event, char buffer[22])
{
Profiler &prof = Profiler::the();
// File output stream
std::ofstream fstr = prof.create_file_stream();
std::time_t tt = event->get_time_stamp();
std::tm *btm = std::localtime(&tt);
std::strftime(buffer, 22, "%a %T", btm);
fstr << "--------------------------------\n"
<< buffer
<< "\nEvent:\t" << Profiler::type_to_string(event->get_type());
// event->type_to_string();
const Chunk *chunk = event->get_chunk();
if (event->get_type() == AllocStart)
{
fstr << "\nSize: " << event->get_size();
}
else if (chunk)
{
fstr << "\nChunk: " << chunk->m_start
<< "\n Size: " << chunk->m_size
<< "\n Mark: " << chunk->m_marked;
}
fstr << "\n";
}
/**
* Deletes the profiler singleton and all
* the events recorded after recording
* the ProfilerDispose event and dumping
* the history to a log file.
*/
void Profiler::dispose()
{
Profiler::record(ProfilerDispose);
Profiler::dump_trace();
}
/**
* Creates a filestream for the future
* log file to print the history to in
* dump_trace().
*
* @returns The output stream to the file.
*/
std::ofstream Profiler::create_file_stream()
{
// get current time
std::time_t tt = std::time(NULL);
std::tm *ptm = std::localtime(&tt);
// format to string
char buffer[32];
std::strftime(buffer, 32, "/log_%a_%H_%M_%S.txt", ptm);
std::string filename(buffer);
// const std::string ABS_PATH = "/home/virre/dev/systemF/org/language/src/GC/";
// // const std::string ABS_PATH = "/Users/valtermiari/Desktop/DV/Bachelors/code/language/src/GC";
// std::string fullpath = ABS_PATH + filename;
const std::string fullpath = get_log_folder() + filename;
std::ofstream fstr(fullpath);
return fstr;
}
/**
* This function retrieves the path to the folder
* of the executable to use for log files.
*
* @returns The path to the logs folder.
*
* @throws A runtime error if the call
* to readlink() fails.
*/
std::string Profiler::get_log_folder()
{
#ifndef MAC_OS
char buffer[1024];
// chars read from path
ssize_t len = readlink("/proc/self/exe", buffer, sizeof(buffer)-1);
// if readlink fails
if (len == -1)
{
throw std::runtime_error(std::string("Error: readlink failed on '/proc/self/exe/'"));
}
buffer[len] = '\0';
// convert to string for string operators
auto path = std::string(buffer);
// remove filename
size_t last_slash = path.find_last_of('/');
std::string folder = path.substr(0, last_slash);
#else
auto folder = std::string("/Users/valtermiari/Desktop/DV/Bachelors/code/language/src/GC/tests");
#endif
return folder + "/logs";
}
const char *Profiler::type_to_string(GCEventType type)
{
switch (type)
{
case HeapInit: return "HeapInit";
case AllocStart: return "AllocStart";
case CollectStart: return "CollectStart";
case MarkStart: return "MarkStart";
case ChunkMarked: return "ChunkMarked";
case ChunkSwept: return "ChunkSwept";
case ChunkFreed: return "ChunkFreed";
case NewChunk: return "NewChunk";
case ReusedChunk: return "ReusedChunk";
case ProfilerDispose: return "ProfilerDispose";
case SweepStart: return "SweepStart";
case FreeStart: return "FreeStart";
default: return "[Unknown]";
}
}
}

83
src/GC/tests/advance.cpp Normal file
View file

@ -0,0 +1,83 @@
#include <chrono>
#include <cstring>
#include <iostream>
#include <list>
#include <time.h>
#include <stdlib.h>
// void time_test()
// {
// using TimeStamp = std::chrono::_V2::system_clock::time_point;
// std::list<char> 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) {
// std::cout << *iter << " ";
// iter++;
// }
// std::cout << std::endl;
// iter = l.begin();
// while (*iter != *stop) {
// std::cout << *iter << " ";
// iter++;
// }
// std::cout << std::endl;
// std::cout << "rebased" << std::endl;
// std::cout << "iter: " << *iter << "\nstop: " << *stop << std::endl;
// TimeStamp ts = std::chrono::system_clock::now();
// std::time_t tt = std::chrono::system_clock::to_time_t(ts);
// std::string tstr = std::ctime(&tt);
// tstr.resize(tstr.size()-1);
// std::cout << tstr << std::endl;
// }
void iter_test()
{
std::list<int> list;
list.push_back(1);
list.push_back(2);
list.push_back(4);
list.push_back(5);
auto iter = list.begin();
while (iter != list.end())
{
if (*iter == 4)
{
iter = list.erase(iter);
std::cout << *iter << "\n";
list.insert(iter, 3);
// list.insert(iter, 3);
// std::cout << "n: " << *(++iter) << "\n";
// iter = list.erase(++iter);
}
iter++;
}
for (int i : list)
{
std::cout << i << " ";
}
std::cout << std::endl;
}
int main() {
std::cout << "hello" << std::endl;
iter_test();
return 0;
}

View file

@ -0,0 +1,250 @@
#include <iostream>
#include <list>
#include "heap.hpp"
using GC::Chunk;
void alloc_test();
void add_to_free_list(Chunk *chunk);
void merge_free_list(Chunk *chunk, bool do_merge);
void do_merge_list();
void print_free_list();
std::list<Chunk *> m_free_list;
int main()
{
alloc_test();
// std::list<int> test;
// test.push_back(1);
// test.push_back(2);
// test.push_back(3);
// test.push_back(4);
// test.push_back(5);
// auto iter = test.begin();
// std::cout << "First? " << *(iter++) << "\n";
// std::cout << "Second? " << *(iter--) << "\n";
// std::cout << "First? " << *iter << std::endl;
// auto i = test.begin();
// while (i != test.end())
// {
// std::cout << *i << " ";
// ++i;
// }
// if (i == test.end())
// std::cout << "great success!";
// std::cout << std::endl;
return 0;
}
void alloc_test()
{
auto tmp = static_cast<uintptr_t *>(__builtin_frame_address(0));
auto c1 = new Chunk((size_t)(8), tmp);
auto c2 = new Chunk((size_t)(4), c1->m_start + (size_t)(8));
auto c3 = new Chunk((size_t)(16), c2->m_start + (size_t)(4));
auto c4 = new Chunk((size_t)(4), c3->m_start + (size_t)(16));
auto c5 = new Chunk((size_t)(32), c4->m_start + (size_t)(4));
// std::cout << "test: " << (uintptr_t *)(tmp + (size_t)(2)) << std::endl;
std::cout << "tmp: " << tmp << "\ntmp: " << (tmp + (size_t)(28)) << std::endl;
// add_to_free_list(c1);
// add_to_free_list(c2);
// add_to_free_list(c3);
// add_to_free_list(c4);
// add_to_free_list(c5);
merge_free_list(c1, false);
merge_free_list(c2, false);
merge_free_list(c3, false);
merge_free_list(c4, false);
merge_free_list(c5, false);
std::cout << "----- BEFORE MERGE ----------------------";
// print_free_list();
do_merge_list();
std::cout << "----- AFTER MERGE -----------------------";
// print_free_list();
}
void add_to_free_list(Chunk *chunk)
{
Chunk *curr;
auto iter = m_free_list.begin();
uintptr_t *prev_start = nullptr;
uintptr_t *prev_end = nullptr;
if (m_free_list.size() == 0)
{
m_free_list.push_back(chunk);
return;
}
while (iter != m_free_list.end())
{
curr = *iter;
// If the curr chunk is aligned before param
if (curr->m_start + curr->m_size == chunk->m_start)
{
Chunk *merged = new Chunk(
curr->m_size + chunk->m_size,
curr->m_start);
iter = m_free_list.erase(iter);
m_free_list.insert(iter, merged);
return;
}
// If the curr chunk is aligned after param
if (chunk->m_start + chunk->m_size == curr->m_start)
{
Chunk *merged = new Chunk(
curr->m_size + chunk->m_size,
chunk->m_start);
iter = m_free_list.erase(iter);
m_free_list.insert(iter, merged);
return;
}
// If the first chunk starts after param
if (prev_start == nullptr && curr->m_start > chunk->m_start)
{
m_free_list.insert(iter, chunk);
return;
}
if (prev_end < chunk->m_start && (chunk->m_start + chunk->m_size) < curr->m_start)
{
m_free_list.insert(iter, chunk);
return;
}
prev_start = curr->m_start;
prev_end = prev_start + curr->m_size;
iter++;
}
// This is only reachable if the chunk is at the end
m_free_list.push_back(chunk);
}
void merge_free_list(Chunk *chunk, bool do_merge)
{
auto i = m_free_list.begin();
uintptr_t *prev_start = nullptr, *prev_end;
bool chunk_inserted = false;
while (i != m_free_list.end())
{
// if chunk is left-aligned
if ((*i)->m_start + (*i)->m_size == chunk->m_start)
{
m_free_list.insert(++i, chunk);
chunk_inserted = true;
break;
}
// if chunk is right-aligned
if (chunk->m_start + chunk->m_size == (*i)->m_start)
{
m_free_list.insert(i, chunk);
chunk_inserted = true;
break;
}
// is new first
if (prev_start == nullptr && (*i)->m_start > chunk->m_start)
{
m_free_list.insert(i, chunk);
chunk_inserted = true;
break;
}
// if between chunks
if (prev_end < chunk->m_start && (chunk->m_start + chunk->m_size) < (*i)->m_start)
{
m_free_list.insert(i, chunk);
chunk_inserted = true;
break;
}
prev_start = (*i)->m_start;
prev_end = (*i)->m_start + (*i)->m_size;
i++;
}
// is new last
if (!chunk_inserted && i == m_free_list.end())
m_free_list.push_back(chunk);
if (do_merge)
do_merge_list();
}
void do_merge_list()
{
std::cout << "DO MERGE" << std::endl;
auto i = m_free_list.begin();
Chunk *prev = *(i++), *curr;
print_free_list();
while (i != m_free_list.end())
{
curr = *i;
if ((prev->m_start + prev->m_size) == curr->m_start)
{
Chunk *merged = new Chunk(
prev->m_size + curr->m_size,
prev->m_start
);
// replace current and previous with merged
i = m_free_list.erase(i);
i = m_free_list.erase(--i);
m_free_list.insert(i, merged);
prev = merged;
}
else
{
prev = curr;
i++;
}
print_free_list();
}
print_free_list();
}
void print_free_list()
{
std::cout << "free-list count: " << m_free_list.size() << "\n";
auto iter = m_free_list.begin();
size_t cnt = 1;
while (iter != m_free_list.end())
{
std::cout << "C" << cnt << ":\n\tstart: " << (*iter)->m_start
<< "\n\tsize: " << (*iter)->m_size << "\n";
iter++;
cnt++;
}
std::cout << std::endl;
}

View file

@ -0,0 +1,87 @@
#include <stdio.h>
#include <stdint.h>
#include "heap.hpp"
#define allocNode static_cast<Node *>(GC::Heap::alloc(sizeof(Node)))
using std::cout, std::endl;
struct Node // sizeof(Node) = 16
{
int value;
Node *next {nullptr};
};
Node *create_list(size_t length)
{
Node *head = allocNode;
head->value = 0;
Node *prev = head;
for (size_t i = 1; i < length; i++)
{
Node *next = allocNode;
next->value = i;
prev->next = next;
prev = next;
}
return head;
}
void print_list(Node* head)
{
cout << "\nPrinting list...\n";
while (head != nullptr)
{
cout << head->value << " ";
head = head->next;
}
cout << endl;
}
void clear_list(Node *head)
{
while (head != nullptr)
{
Node *tmp = head->next;
head->next = nullptr;
head = tmp;
}
}
void run_list_test1()
{
Node *list_a = create_list(10);
print_list(list_a);
}
void run_list_test2()
{
Node *list_b = create_list(10);
print_list(list_b);
}
void run_list_test3()
{
Node *list_c = create_list(10);
print_list(list_c);
}
int main()
{
GC::Heap::init();
GC::Heap &heap = GC::Heap::the();
heap.set_profiler(true);
GC::Profiler::set_log_options(GC::FunctionCalls);
run_list_test1();
run_list_test2();
run_list_test3();
GC::Heap::dispose();
return 0;
}

16
src/GC/tests/linker.cpp Normal file
View file

@ -0,0 +1,16 @@
#include <stdio.h>
#include "heap.hpp"
struct Obj {
int a;
int b;
int c;
};
int main() {
return 0;
}