Skip to content

Commit 0bc565f

Browse files
authored
Change arena end ptr (#1177)
* 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
1 parent c362cf0 commit 0bc565f

File tree

4 files changed

+19
-24
lines changed

4 files changed

+19
-24
lines changed

include/runtime/arena.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class arena {
2222
initialize_semispace();
2323
}
2424

25+
char *evacuate(char *scan_ptr);
26+
2527
// Allocates the requested number of bytes as a contiguous region and returns a
2628
// pointer to the first allocated byte.
2729
void *kore_arena_alloc(size_t requested);
@@ -33,7 +35,7 @@ class arena {
3335
// Returns a pointer to a location holding the address of last allocated
3436
// byte in the given arena plus 1.
3537
// This address is nullptr if nothing has been allocated ever in that arena.
36-
char **arena_end_ptr() { return &allocation_ptr; }
38+
char *arena_end_ptr() { return allocation_ptr; }
3739

3840
// Clears the current allocation space by setting its start back to its first
3941
// block. It is used during garbage collection to effectively collect all of the
@@ -73,9 +75,9 @@ class arena {
7375
// 3rd argument: the address of last allocated byte in the arena plus 1
7476
// Return value: starting pointer + size unless this points to unallocated space
7577
// in which case nullptr is returned
76-
static char *move_ptr(char *ptr, size_t size, char const *arena_end_ptr) {
78+
static char *move_ptr(char *ptr, size_t size, char const *end_ptr) {
7779
char *next_ptr = ptr + size;
78-
return (next_ptr == arena_end_ptr) ? nullptr : next_ptr;
80+
return (next_ptr == end_ptr) ? nullptr : next_ptr;
7981
}
8082

8183
// 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) {
132134

133135
// Macro to define a new arena with the given ID. Supports IDs ranging from 0 to
134136
// 127.
135-
#define REGISTER_ARENA(name, id) static thread_local arena name(id)
137+
#define REGISTER_ARENA(name, id) thread_local arena name(id)
136138

137139
#ifdef __MACH__
138140
//

runtime/alloc/arena.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "runtime/alloc.h"
99
#include "runtime/arena.h"
10+
#include "runtime/collect.h"
1011
#include "runtime/header.h"
1112

1213
extern size_t const VAR_BLOCK_SIZE = BLOCK_SIZE;

runtime/collect/collect.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
#include <cstring>
1111

1212
extern "C" {
13+
extern thread_local arena youngspace;
14+
extern thread_local arena oldspace;
1315

14-
char **young_alloc_ptr(void);
15-
char **old_alloc_ptr(void);
1616
char *youngspace_ptr(void);
1717
char *oldspace_ptr(void);
1818

@@ -245,7 +245,7 @@ static void migrate_root(void *curr_block, layoutitem *args, unsigned i) {
245245
}
246246
}
247247

248-
static char *evacuate(char *scan_ptr, char **alloc_ptr) {
248+
char *arena::evacuate(char *scan_ptr) {
249249
auto *curr_block = (block *)scan_ptr;
250250
uint64_t const hdr = curr_block->h.hdr;
251251
uint16_t layout_int = layout_hdr(hdr);
@@ -255,7 +255,7 @@ static char *evacuate(char *scan_ptr, char **alloc_ptr) {
255255
migrate_child(curr_block, layout_data->args, i, false);
256256
}
257257
}
258-
return arena::move_ptr(scan_ptr, get_size(hdr, layout_int), *alloc_ptr);
258+
return move_ptr(scan_ptr, get_size(hdr, layout_int), arena_end_ptr());
259259
}
260260

261261
// Contains the decision logic for collecting the old generation.
@@ -293,43 +293,43 @@ void kore_collect(
293293
if (!last_alloc_ptr) {
294294
last_alloc_ptr = youngspace_ptr();
295295
}
296-
char *current_alloc_ptr = *young_alloc_ptr();
296+
char *current_alloc_ptr = youngspace.arena_end_ptr();
297297
#endif
298298
kore_alloc_swap(collect_old);
299299
#ifdef GC_DBG
300300
for (int i = 0; i < 2048; i++) {
301301
numBytesLiveAtCollection[i] = 0;
302302
}
303303
#endif
304-
char *previous_oldspace_alloc_ptr = *old_alloc_ptr();
304+
char *previous_oldspace_alloc_ptr = oldspace.arena_end_ptr();
305305
for (int i = 0; i < nroots; i++) {
306306
migrate_root(roots, type_info, i);
307307
}
308308
migrate_static_roots();
309309
char *scan_ptr = youngspace_ptr();
310-
if (scan_ptr != *young_alloc_ptr()) {
310+
if (scan_ptr != youngspace.arena_end_ptr()) {
311311
MEM_LOG("Evacuating young generation\n");
312312
while (scan_ptr) {
313-
scan_ptr = evacuate(scan_ptr, young_alloc_ptr());
313+
scan_ptr = youngspace.evacuate(scan_ptr);
314314
}
315315
}
316316
if (collect_old || !previous_oldspace_alloc_ptr) {
317317
scan_ptr = oldspace_ptr();
318318
} else {
319319
scan_ptr = previous_oldspace_alloc_ptr;
320320
}
321-
if (scan_ptr != *old_alloc_ptr()) {
321+
if (scan_ptr != oldspace.arena_end_ptr()) {
322322
MEM_LOG("Evacuating old generation\n");
323323
while (scan_ptr) {
324-
scan_ptr = evacuate(scan_ptr, old_alloc_ptr());
324+
scan_ptr = oldspace.evacuate(scan_ptr);
325325
}
326326
}
327327
#ifdef GC_DBG
328328
ssize_t numBytesAllocedSinceLastCollection
329329
= arena::ptr_diff(current_alloc_ptr, last_alloc_ptr);
330330
assert(numBytesAllocedSinceLastCollection >= 0);
331331
fwrite(&numBytesAllocedSinceLastCollection, sizeof(ssize_t), 1, stderr);
332-
last_alloc_ptr = *young_alloc_ptr();
332+
last_alloc_ptr = youngspace.arena_end_ptr();
333333
fwrite(
334334
numBytesLiveAtCollection, sizeof(numBytesLiveAtCollection[0]),
335335
sizeof(numBytesLiveAtCollection) / sizeof(numBytesLiveAtCollection[0]),

runtime/lto/alloc.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@ char *oldspace_ptr() {
2323
return oldspace.arena_start_ptr();
2424
}
2525

26-
char **young_alloc_ptr() {
27-
return youngspace.arena_end_ptr();
28-
}
29-
30-
char **old_alloc_ptr() {
31-
return oldspace.arena_end_ptr();
32-
}
33-
3426
char youngspace_collection_id() {
3527
return youngspace.get_arena_collection_semispace_id();
3628
}
@@ -81,7 +73,7 @@ kore_resize_last_alloc(void *oldptr, size_t newrequest, size_t last_size) {
8173
newrequest = (newrequest + 7) & ~7;
8274
last_size = (last_size + 7) & ~7;
8375

84-
if (oldptr != *(youngspace.arena_end_ptr()) - last_size) {
76+
if (oldptr != youngspace.arena_end_ptr() - last_size) {
8577
MEM_LOG(
8678
"May only reallocate last allocation. Tried to reallocate %p to %zd\n",
8779
oldptr, newrequest);

0 commit comments

Comments
 (0)