Skip to content

Commit 30a99c9

Browse files
committed
Implement a good enough version of rb_gc_register_address.
1 parent 8701c46 commit 30a99c9

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/truffle/truffle/cext.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,4 +1945,15 @@ def rb_exception_set_message(e, mesg)
19451945
def rb_global_variable(obj)
19461946
GLOBALLY_PRESERVED_VALUES << obj
19471947
end
1948+
1949+
GC_REGISTERED_ADDRESSES = {}
1950+
def rb_gc_register_address(address, obj)
1951+
Truffle::Interop.to_native(address) unless Truffle::Interop.pointer?(address)
1952+
GC_REGISTERED_ADDRESSES[address] = obj
1953+
end
1954+
1955+
def rb_gc_unregister_address(address)
1956+
Truffle::Interop.to_native(address) unless Truffle::Interop.pointer?(address)
1957+
GC_REGISTERED_ADDRESSES.delete(address)
1958+
end
19481959
end

src/main/c/cext/gc.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313
// GC, rb_gc_*
1414

1515
void rb_gc_register_address(VALUE *address) {
16+
/* TODO: This should guard the address of the pointer, not the
17+
object pointed to, but we haven't yet found a good way to implement
18+
that, or a real world use case where it is required. */
19+
polyglot_invoke(RUBY_CEXT, "rb_gc_register_address", address, rb_tr_unwrap(*address));
1620
}
1721

1822
void rb_gc_unregister_address(VALUE *address) {
19-
// VALUE is only ever in managed memory. So, it is already garbage collected.
23+
polyglot_invoke(RUBY_CEXT, "rb_gc_unregister_address", address);
2024
}
2125

2226
void rb_gc_mark(VALUE ptr) {

0 commit comments

Comments
 (0)