Skip to content

Commit f098535

Browse files
do not garbage collect empty map/list/set (#1168)
We move the empty map/list/set allocations to be allocated using `new` and never deallocated or relocated in order to assist in the thread safety of the llvm backend. When we make each allocation arena thread-local, this change will be required, otherwise garbage collection in one thread will corrupt collections in other threads. --------- Co-authored-by: F-WRunTime <[email protected]>
1 parent 0bc565f commit f098535

File tree

6 files changed

+14
-14
lines changed

6 files changed

+14
-14
lines changed

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ jobs:
8686
include:
8787
- runner: [self-hosted, linux, normal]
8888
os: ubuntu-24.04
89-
- runner: MacM1
90-
os: self-macos-12
89+
- runner: [self-hosted, self-macos-latest]
90+
os: self-macos-latest
9191

9292
runs-on: ${{ matrix.runner }}
9393
steps:

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ jobs:
1717
include:
1818
- runner: [self-hosted, linux, normal]
1919
os: ubuntu-24.04
20-
- runner: MacM1
21-
os: self-macos-12
20+
- runner: [self-hosted, self-macos-latest]
21+
os: self-macos-latest
2222
runs-on: ${{ matrix.runner }}
2323
steps:
2424
- name: 'Check out code'

include/runtime/header.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ struct kore_alloc_heap {
9191
template <typename... Tags>
9292
static void *allocate(size_t size, Tags...) {
9393
if (during_gc()) {
94-
return ::operator new(size);
94+
auto *result = (string *)::operator new(size + sizeof(blockheader));
95+
init_with_len(result, size);
96+
result->h.hdr |= NOT_YOUNG_OBJECT_BIT;
97+
return result->data;
9598
}
9699
bool enabled = gc_enabled;
97100
gc_enabled = false;
@@ -103,7 +106,7 @@ struct kore_alloc_heap {
103106

104107
static void deallocate(size_t size, void *data) {
105108
if (during_gc()) {
106-
::operator delete(data);
109+
::operator delete((char *)data - sizeof(blockheader));
107110
}
108111
}
109112
};

runtime/collect/collect.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,11 @@ static bool should_collect_old_gen() {
274274
}
275275

276276
void init_static_objects(void) {
277+
is_gc = true;
277278
map m = map();
278279
list l = list();
279280
set s = set();
281+
is_gc = false;
280282
set_kore_memory_functions_for_gmp();
281283
}
282284

runtime/collect/migrate_collection.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
void migrate_collection_node(void **node_ptr) {
88
string *curr_block = STRUCT_BASE(string, data, *node_ptr);
9+
if (!is_heap_block(curr_block)) {
10+
return;
11+
}
912
if (youngspace_collection_id()
1013
!= arena::get_arena_semispace_id_of_object((void *)curr_block)
1114
&& oldspace_collection_id()

runtime/collect/migrate_static_roots.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@ extern thread_local bool kllvm_rand_state_initialized;
1010
extern "C" {
1111

1212
void migrate_static_roots() {
13-
auto &l1 = list_impl::empty_root();
14-
migrate_collection_node((void **)&l1);
15-
auto &l2 = list_impl::empty_tail();
16-
migrate_collection_node((void **)&l2);
17-
auto &s = set_impl::empty();
18-
migrate_collection_node((void **)&s);
19-
auto &m = map_impl::empty();
20-
migrate_collection_node((void **)&m);
2113
if (kllvm_rand_state_initialized) {
2214
auto &rand = kllvm_rand_state->_mp_seed->_mp_d;
2315
string *limbs = STRUCT_BASE(string, data, rand);

0 commit comments

Comments
 (0)