Skip to content

Commit

Permalink
Change arena end ptr (#1177)
Browse files Browse the repository at this point in the history
* arena_end_ptr() now returns char *; changes percolated through
collector
* young_alloc_ptr() and old_alloc_ptr() inlined
* evacuate() made a member function of class arena
  • Loading branch information
stevenmeker authored Dec 10, 2024
1 parent c362cf0 commit 0bc565f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 24 deletions.
10 changes: 6 additions & 4 deletions include/runtime/arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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__
//
Expand Down
1 change: 1 addition & 0 deletions runtime/alloc/arena.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 11 additions & 11 deletions runtime/collect/collect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#include <cstring>

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);

Expand Down Expand Up @@ -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);
Expand All @@ -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.
Expand Down Expand Up @@ -293,43 +293,43 @@ 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
for (int i = 0; i < 2048; i++) {
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) {
scan_ptr = oldspace_ptr();
} 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
ssize_t numBytesAllocedSinceLastCollection
= 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]),
Expand Down
10 changes: 1 addition & 9 deletions runtime/lto/alloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 0bc565f

Please sign in to comment.