Skip to content

Commit

Permalink
fix formatting; clean code and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenmeker committed Dec 4, 2024
1 parent bfba1ca commit 8e31c96
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
24 changes: 11 additions & 13 deletions include/runtime/arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,21 @@ class arena {
void *kore_arena_alloc(size_t requested);

// Returns the address of the first byte that belongs in the given arena.
// Returns 0 if nothing has been allocated ever in that arena.
// Returns nullptr if nothing has been allocated ever in that arena.
char *arena_start_ptr() const { return current_addr_ptr; }

// Returns a pointer to a location holding the address of last allocated
// byte in the given arena plus 1.
// This address is 0 if nothing has been allocated ever in that arena.
// This address is nullptr if nothing has been allocated ever in that arena.
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
// arena. Resets the tripwire.
void arena_clear();

// Resizes the last allocation as long as the resize does not require a new
// block allocation.
// Returns the address of the byte following the last newlly allocated byte.
// Resizes the last allocation.
// Returns the address of the byte following the last newly allocated byte.
void *arena_resize_last_alloc(ssize_t increase) {
return (allocation_ptr += increase);
}
Expand All @@ -61,10 +60,8 @@ class arena {
void arena_swap_and_clear();

// Given two pointers to objects allocated in the same arena, return the number
// of bytes they are separated by within the virtual block of memory represented
// by the blocks of that arena. This difference will include blocks containing
// sentinel bytes. Undefined behavior will result if the pointers belong to
// different arenas.
// of bytes they are apart. Undefined behavior will result if the pointers
// don't belong to the same arena
static ssize_t ptr_diff(char *ptr1, char *ptr2) { return ptr1 - ptr2; }

// Given a starting pointer to an address allocated in an arena and a size in
Expand All @@ -74,11 +71,11 @@ class arena {
// 1st argument: the starting pointer
// 2nd argument: the size in bytes to add to the starting pointer
// 3rd argument: the address of last allocated byte in the arena plus 1
// Return value: the address allocated in the arena after size bytes from the
// starting pointer, or 0 if this is equal to the 3rd argument.
// 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) {
char *next_ptr = ptr + size;
return (next_ptr == arena_end_ptr) ? 0 : next_ptr;
return (next_ptr == arena_end_ptr) ? nullptr : next_ptr;
}

// Returns the ID of the semispace where the given address was allocated.
Expand Down Expand Up @@ -128,7 +125,8 @@ inline char arena::get_arena_semispace_id_of_object(void *ptr) {
//
// Set the low bits to 1 to get the address of the last byte in the hyperblock.
//
uintptr_t end_address = reinterpret_cast<uintptr_t>(ptr) | (HYPERBLOCK_SIZE - 1);
uintptr_t end_address
= reinterpret_cast<uintptr_t>(ptr) | (HYPERBLOCK_SIZE - 1);
return *reinterpret_cast<char *>(end_address);
}

Expand Down
5 changes: 3 additions & 2 deletions runtime/alloc/arena.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,15 @@ void arena::initialize_semispace() {
}
//
// We allocated 2 * HYPERBLOCK_SIZE worth of address space but we're only going to use 1, aligned on a
// HYPERBLOCK_SIZE boundry. This is so we can get the start of the hyperblock by masking any address within it.
// HYPERBLOCK_SIZE boundry. This is so we can get end of the hyperblock by setting the low bits of any
// address within the space to 1.
// We don't worry about unused address space either side of our aligned address space because there will be no
// memory mapped to it.
//
current_addr_ptr = reinterpret_cast<char *>(
std::align(HYPERBLOCK_SIZE, HYPERBLOCK_SIZE, addr, request));
//
// We put a semispace id in the last byte of the hyperblock so we can identify which semispace a pointer
// We put a semispace id in the last byte of the hyperblock so we can identify which semispace an address
// belongs to by setting the low bits to 1 to access this id.
//
current_addr_ptr[HYPERBLOCK_SIZE - 1] = allocation_semispace_id;
Expand Down

0 comments on commit 8e31c96

Please sign in to comment.