diff --git a/src/GC/Makefile b/src/GC/Makefile index e92237f..4aac430 100644 --- a/src/GC/Makefile +++ b/src/GC/Makefile @@ -69,6 +69,7 @@ static_lib: $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -c -o lib/event.o lib/event.cpp -fPIC $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -c -o lib/profiler.o lib/profiler.cpp -fPIC $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -c -o lib/heap.o lib/heap.cpp -fPIC +<<<<<<< HEAD # create static library ar r lib/gcoll.a lib/event.o lib/profiler.o lib/heap.o @@ -92,6 +93,8 @@ wrapper: $(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 <<<<<<< HEAD +======= +>>>>>>> 7e93aab6268ac896dea03dcd045b8bc249f2576c # create static library ar r lib/gcoll.a lib/event.o lib/profiler.o lib/heap.o ======= @@ -100,19 +103,35 @@ wrapper: alloc_free_list: static_lib $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -o tests/alloc_fl.out tests/alloc_free_list.cpp lib/gcoll.a +<<<<<<< HEAD linked_list_test: static_lib $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -o tests/linkedlist.out tests/linkedlist.cpp lib/gcoll.a <<<<<<< HEAD >>>>>>> d7ea27e (testing testing...) ======= +======= +# create test program +static_lib_test: static_lib + $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -o tests/extern_lib.out tests/extern_lib.cpp lib/gcoll.a + +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 + +>>>>>>> 7e93aab6268ac896dea03dcd045b8bc249f2576c revrange: static_lib $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -o tests/revrange.out tests/revrange.cpp lib/gcoll.a pointers: static_lib $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -o tests/pointers.out tests/pointers.cpp lib/gcoll.a +<<<<<<< HEAD >>>>>>> a910d54 (rip gc) +======= +>>>>>>> 7e93aab6268ac896dea03dcd045b8bc249f2576c wrapper: # remove old files rm -f lib/event.o lib/profiler.o lib/heap.o lib/coll.a tests/wrapper.out @@ -122,8 +141,14 @@ wrapper: $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -c -o lib/heap.o lib/heap.cpp -fPIC $(CC) $(STDFLAGS) $(WFLAGS) $(LIB_INCL) -O3 -c -o lib/cheap.o lib/cheap.cpp -fPIC # compile object files into library +<<<<<<< HEAD >>>>>>> 01c93a6 (Wrapper docs) ar rcs lib/gcoll.a lib/event.o lib/profiler.o lib/heap.o lib/cheap.o # compile test program wrapper.c with normal clang clang -stdlib=libc++ $(WFLAGS) $(LIB_INCL) -o tests/wrapper.out tests/wrapper.c lib/gcoll.a -lstdc++ >>>>>>> 12816ea (Wrapper works) +======= + ar rcs lib/gcoll.a lib/event.o lib/profiler.o lib/heap.o lib/cheap.o +# compile test program wrapper.c with normal clang + clang -stdlib=libc++ $(WFLAGS) $(LIB_INCL) -o tests/wrapper.out tests/wrapper.c lib/gcoll.a -lstdc++ +>>>>>>> 7e93aab6268ac896dea03dcd045b8bc249f2576c diff --git a/src/GC/include/cheap.h b/src/GC/include/cheap.h index 7bfab4e..9affefc 100644 --- a/src/GC/include/cheap.h +++ b/src/GC/include/cheap.h @@ -7,11 +7,16 @@ extern "C" { #endif +<<<<<<< HEAD <<<<<<< HEAD #define WRAPPER_DEBUG ======= #define DEBUG >>>>>>> 74e0282 (Added Hash map marking) +======= +#define DEBUG +#define WRAPPER_DEBUG +>>>>>>> 7e93aab6268ac896dea03dcd045b8bc249f2576c #ifdef WRAPPER_DEBUG typedef struct cheap diff --git a/src/GC/include/heap.hpp b/src/GC/include/heap.hpp index 6052966..9b8a100 100644 --- a/src/GC/include/heap.hpp +++ b/src/GC/include/heap.hpp @@ -12,6 +12,7 @@ <<<<<<< HEAD <<<<<<< HEAD <<<<<<< HEAD +<<<<<<< HEAD #define HEAP_SIZE 320//65536 #define FREE_THRESH (uint) 0 ======= @@ -28,6 +29,11 @@ #define FREE_THRESH (uint) 100000 //1000 #define DEBUG >>>>>>> 74e0282 (Added Hash map marking) +======= +#define HEAP_SIZE 160000//65536 +#define FREE_THRESH (uint) 5 +// #define HEAP_DEBUG +>>>>>>> 7e93aab6268ac896dea03dcd045b8bc249f2576c namespace GC { @@ -81,11 +87,16 @@ namespace GC std::vector m_allocated_chunks; std::vector m_freed_chunks; +<<<<<<< HEAD <<<<<<< HEAD std::list m_free_list; ======= std::unordered_map m_chunk_table; >>>>>>> 74e0282 (Added Hash map marking) +======= + std::list m_free_list; + std::unordered_map m_chunk_table; +>>>>>>> 7e93aab6268ac896dea03dcd045b8bc249f2576c static bool profiler_enabled(); // static Chunk *get_at(std::vector &list, size_t n); diff --git a/src/GC/lib/heap.cpp b/src/GC/lib/heap.cpp index cc393b1..f44f1f3 100644 --- a/src/GC/lib/heap.cpp +++ b/src/GC/lib/heap.cpp @@ -3,6 +3,10 @@ #include #include <<<<<<< HEAD +<<<<<<< HEAD +======= +#include +>>>>>>> 7e93aab6268ac896dea03dcd045b8bc249f2576c #include #include #include @@ -12,6 +16,7 @@ #define time_now std::chrono::high_resolution_clock::now() #define to_us std::chrono::duration_cast +<<<<<<< HEAD using std::cout, std::endl, std::vector, std::hex, std::dec; ======= #include @@ -20,6 +25,9 @@ using std::cout, std::endl, std::vector, std::hex, std::dec; 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 { @@ -104,6 +112,7 @@ namespace GC 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) { @@ -114,14 +123,19 @@ namespace GC ======= ======= >>>>>>> 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) { @@ -256,6 +270,7 @@ namespace GC // create_table(); // mark_hash(stack_bottom, stack_top); +<<<<<<< HEAD vector roots; // cout << "\nb4 find_roots\n"; find_roots(stack_bottom, roots); @@ -263,6 +278,16 @@ namespace GC // cout << "b4 mark\n";'' mark(roots); +======= + create_table(); + vector roots; + // cout << "\nb4 find_roots\n"; + find_roots(stack_bottom, roots); + + // cout << "b4 mark\n";'' + mark(roots); + +>>>>>>> 7e93aab6268ac896dea03dcd045b8bc249f2576c // cout << "b4 sweep\n"; sweep(heap); @@ -320,6 +345,7 @@ namespace GC } void Heap::find_chunks(uintptr_t *stack_addr, std::queue> &chunk_spaces) +<<<<<<< HEAD { auto iter = m_allocated_chunks.begin(); auto end = m_allocated_chunks.end(); @@ -346,12 +372,70 @@ namespace GC <<<<<<< HEAD <<<<<<< HEAD void Heap::mark_range(vector &ranges, vector &worklist) +======= +>>>>>>> 7e93aab6268ac896dea03dcd045b8bc249f2576c { Heap &heap = Heap::the(); + + auto it = heap.m_chunk_table.find(*stack_addr); + if (it != heap.m_chunk_table.end()) + { + auto chunk = it->second; + + if (!chunk->m_marked) + { + auto c_start = reinterpret_cast(chunk->m_start); + auto c_size = reinterpret_cast(chunk->m_size); + auto c_end = reinterpret_cast(c_start + c_size); + + chunk->m_marked = true; + chunk_spaces.push(std::make_pair(c_start, c_end)); + } + + } + +/* 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(chunk->m_start); + auto c_size = reinterpret_cast(chunk->m_size); + auto c_end = reinterpret_cast(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)); + } + } */ + + } + + void Heap::create_table() + { + Heap &heap = Heap::the(); + unordered_map chunk_table; + for (auto chunk : heap.m_allocated_chunks) { + auto pair = std::make_pair(reinterpret_cast(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(); + bool profiler_enabled = heap.m_profiler_enable; if (profiler_enabled) Profiler::record(MarkStart); +<<<<<<< HEAD auto iter = ranges.begin(); auto stop = ranges.end(); @@ -419,6 +503,41 @@ namespace GC 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(chunk->m_start); + auto c_size = reinterpret_cast(chunk->m_size); + auto c_end = reinterpret_cast(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(next->m_start); + auto c_size = reinterpret_cast(next->m_size); + auto c_end = reinterpret_cast(c_start + c_size); + next = find_pointer_hash((uintptr_t *) c_start, (uintptr_t *) c_end); + } + } +>>>>>>> 7e93aab6268ac896dea03dcd045b8bc249f2576c } } } @@ -440,6 +559,7 @@ namespace GC } } +<<<<<<< HEAD void Heap::mark_hash(uintptr_t *start, const uintptr_t* const end) { Heap &heap = Heap::the(); @@ -486,6 +606,8 @@ 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.