diff --git a/include/runtime/arena.h b/include/runtime/arena.h index bac3968a3..4bb5ac315 100644 --- a/include/runtime/arena.h +++ b/include/runtime/arena.h @@ -22,6 +22,8 @@ class arena { initialize_semispace(); } + char *evacuate(char *scan_ptr); + // Allocates the requested number of bytes as a contiguous region and returns a // pointer to the first allocated byte. void *kore_arena_alloc(size_t requested); @@ -33,7 +35,7 @@ class arena { // Returns a pointer to a location holding the address of last allocated // byte in the given arena plus 1. // This address is nullptr if nothing has been allocated ever in that arena. - char **arena_end_ptr() { return &allocation_ptr; } + char *arena_end_ptr() { return allocation_ptr; } // Clears the current allocation space by setting its start back to its first // block. It is used during garbage collection to effectively collect all of the @@ -73,9 +75,9 @@ class arena { // 3rd argument: the address of last allocated byte in the arena plus 1 // Return value: starting pointer + size unless this points to unallocated space // in which case nullptr is returned - static char *move_ptr(char *ptr, size_t size, char const *arena_end_ptr) { + static char *move_ptr(char *ptr, size_t size, char const *end_ptr) { char *next_ptr = ptr + size; - return (next_ptr == arena_end_ptr) ? nullptr : next_ptr; + return (next_ptr == end_ptr) ? nullptr : next_ptr; } // Returns the ID of the semispace where the given address was allocated. @@ -132,7 +134,7 @@ inline char arena::get_arena_semispace_id_of_object(void *ptr) { // Macro to define a new arena with the given ID. Supports IDs ranging from 0 to // 127. -#define REGISTER_ARENA(name, id) static thread_local arena name(id) +#define REGISTER_ARENA(name, id) thread_local arena name(id) #ifdef __MACH__ // diff --git a/runtime/alloc/arena.cpp b/runtime/alloc/arena.cpp index c657dada4..d10bcaad9 100644 --- a/runtime/alloc/arena.cpp +++ b/runtime/alloc/arena.cpp @@ -7,6 +7,7 @@ #include "runtime/alloc.h" #include "runtime/arena.h" +#include "runtime/collect.h" #include "runtime/header.h" extern size_t const VAR_BLOCK_SIZE = BLOCK_SIZE; diff --git a/runtime/collect/collect.cpp b/runtime/collect/collect.cpp index c439fdd0e..25a580ccb 100644 --- a/runtime/collect/collect.cpp +++ b/runtime/collect/collect.cpp @@ -10,9 +10,9 @@ #include extern "C" { +extern thread_local arena youngspace; +extern thread_local arena oldspace; -char **young_alloc_ptr(void); -char **old_alloc_ptr(void); char *youngspace_ptr(void); char *oldspace_ptr(void); @@ -245,7 +245,7 @@ static void migrate_root(void *curr_block, layoutitem *args, unsigned i) { } } -static char *evacuate(char *scan_ptr, char **alloc_ptr) { +char *arena::evacuate(char *scan_ptr) { auto *curr_block = (block *)scan_ptr; uint64_t const hdr = curr_block->h.hdr; uint16_t layout_int = layout_hdr(hdr); @@ -255,7 +255,7 @@ static char *evacuate(char *scan_ptr, char **alloc_ptr) { migrate_child(curr_block, layout_data->args, i, false); } } - return arena::move_ptr(scan_ptr, get_size(hdr, layout_int), *alloc_ptr); + return move_ptr(scan_ptr, get_size(hdr, layout_int), arena_end_ptr()); } // Contains the decision logic for collecting the old generation. @@ -293,7 +293,7 @@ void kore_collect( if (!last_alloc_ptr) { last_alloc_ptr = youngspace_ptr(); } - char *current_alloc_ptr = *young_alloc_ptr(); + char *current_alloc_ptr = youngspace.arena_end_ptr(); #endif kore_alloc_swap(collect_old); #ifdef GC_DBG @@ -301,16 +301,16 @@ void kore_collect( numBytesLiveAtCollection[i] = 0; } #endif - char *previous_oldspace_alloc_ptr = *old_alloc_ptr(); + char *previous_oldspace_alloc_ptr = oldspace.arena_end_ptr(); for (int i = 0; i < nroots; i++) { migrate_root(roots, type_info, i); } migrate_static_roots(); char *scan_ptr = youngspace_ptr(); - if (scan_ptr != *young_alloc_ptr()) { + if (scan_ptr != youngspace.arena_end_ptr()) { MEM_LOG("Evacuating young generation\n"); while (scan_ptr) { - scan_ptr = evacuate(scan_ptr, young_alloc_ptr()); + scan_ptr = youngspace.evacuate(scan_ptr); } } if (collect_old || !previous_oldspace_alloc_ptr) { @@ -318,10 +318,10 @@ void kore_collect( } else { scan_ptr = previous_oldspace_alloc_ptr; } - if (scan_ptr != *old_alloc_ptr()) { + if (scan_ptr != oldspace.arena_end_ptr()) { MEM_LOG("Evacuating old generation\n"); while (scan_ptr) { - scan_ptr = evacuate(scan_ptr, old_alloc_ptr()); + scan_ptr = oldspace.evacuate(scan_ptr); } } #ifdef GC_DBG @@ -329,7 +329,7 @@ void kore_collect( = arena::ptr_diff(current_alloc_ptr, last_alloc_ptr); assert(numBytesAllocedSinceLastCollection >= 0); fwrite(&numBytesAllocedSinceLastCollection, sizeof(ssize_t), 1, stderr); - last_alloc_ptr = *young_alloc_ptr(); + last_alloc_ptr = youngspace.arena_end_ptr(); fwrite( numBytesLiveAtCollection, sizeof(numBytesLiveAtCollection[0]), sizeof(numBytesLiveAtCollection) / sizeof(numBytesLiveAtCollection[0]), diff --git a/runtime/lto/alloc.cpp b/runtime/lto/alloc.cpp index 2928ad62f..aaafc932d 100644 --- a/runtime/lto/alloc.cpp +++ b/runtime/lto/alloc.cpp @@ -23,14 +23,6 @@ char *oldspace_ptr() { return oldspace.arena_start_ptr(); } -char **young_alloc_ptr() { - return youngspace.arena_end_ptr(); -} - -char **old_alloc_ptr() { - return oldspace.arena_end_ptr(); -} - char youngspace_collection_id() { return youngspace.get_arena_collection_semispace_id(); } @@ -81,7 +73,7 @@ kore_resize_last_alloc(void *oldptr, size_t newrequest, size_t last_size) { newrequest = (newrequest + 7) & ~7; last_size = (last_size + 7) & ~7; - if (oldptr != *(youngspace.arena_end_ptr()) - last_size) { + if (oldptr != youngspace.arena_end_ptr() - last_size) { MEM_LOG( "May only reallocate last allocation. Tried to reallocate %p to %zd\n", oldptr, newrequest);